mirror of
https://github.com/seigler/dash-docs
synced 2025-07-27 01:36:13 +00:00
2024 lines
232 KiB
HTML
2024 lines
232 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en">
|
||
|
||
<head>
|
||
|
||
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<meta property="og:image" content="https://www.dash.org/assets/img/graphics/dash_circle_normal.png" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||
<title>Developer Examples - Dash</title>
|
||
|
||
<link rel="stylesheet" href="/css/font-awesome-4.4.0/css/font-awesome.min.css">
|
||
<link rel="stylesheet" href="/css/main.css">
|
||
<!--[if lt IE 8]><link rel="stylesheet" href="/css/ie.css"><script type="text/javascript" src="/js/ie.js"></script><![endif]-->
|
||
<!--[if IE 8]><link rel="stylesheet" href="/css/ie8.css"><![endif]-->
|
||
|
||
|
||
<script type="text/javascript" src="/js/base.js"></script>
|
||
<script type="text/javascript" src="/js/main.js"></script>
|
||
|
||
<script src="/js/jquery/jquery-1.11.2.min.js"></script>
|
||
<script src="/js/jquery/jquery.qrcode.min.js"></script>
|
||
|
||
<link rel="shortcut icon" href="/favicon.png">
|
||
<link rel="apple-touch-icon-precomposed" href="/img/icons/logo_dash_circle_normal.png"/>
|
||
|
||
<!-- Turn off cookie/analytic stuff
|
||
<script>
|
||
window.cookieconsent_options = {
|
||
theme: 'light-floating'
|
||
}
|
||
</script>
|
||
<script type="text/javascript" src="/js/cookieconsent/cookieconsent.js"></script>
|
||
<script>
|
||
if( navigator.doNotTrack != "yes" && navigator.doNotTrack != "1" && window.doNotTrack != "1" && navigator.msDoNotTrack != "1" ){
|
||
var script = document.createElement('script');
|
||
script.src = '/js/analytics.js'
|
||
document.getElementsByTagName("head")[0].appendChild(script);
|
||
}
|
||
</script>
|
||
-->
|
||
|
||
</head>
|
||
|
||
<body>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div id="detectmobile" class="detectmobile"></div>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div class="head"><div>
|
||
|
||
<ul class="lang">
|
||
<li><a>English</a>
|
||
<ul>
|
||
<li><ul>
|
||
|
||
|
||
|
||
<li><a href="/en/" class="active">English</a></li>
|
||
|
||
</ul></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
|
||
<a id="menumobile" class="menumobile" onclick="mobileMenuShow(event);" ontouchstart="mobileMenuShow(event);"></a>
|
||
|
||
|
||
|
||
<a class="logo" href="/en/"><img src="/img/icons/logotop.svg" alt="Dash"></a>
|
||
|
||
|
||
<div id="langselect" class="langselect"><select onchange="window.location=this.value;">
|
||
|
||
<option value="/en/" selected="selected">English</option>
|
||
|
||
</select></div>
|
||
|
||
|
||
|
||
<ul id="menusimple" class="menusimple menumain" onclick="mobileMenuHover(event);" ontouchstart="mobileMenuHover(event);">
|
||
<li><a href="/en/bitcoin-for-developers">Developers</a></li>
|
||
<!--<li><a href="/en/developer-documentation">Documentation</a></li>-->
|
||
|
||
<li><a>Documentation</a>
|
||
<ul>
|
||
<li><a href="/en/developer-guide">Guide</a></li>
|
||
<li><a href="/en/developer-reference">Reference</a></li>
|
||
<li class="active"><a href="/en/developer-examples">Examples</a></li>
|
||
<li><a href="/en/developer-glossary">Glossary</a></li>
|
||
<li><a href="/en/doxygen/html/index.html">Source Code</a></li>
|
||
|
||
<!--
|
||
<li><a href="/en/getting-started">Getting started</a></li>
|
||
<li><a href="/en/how-it-works">How it works</a></li>
|
||
<li><a href="/en/you-need-to-know">You need to know</a></li>
|
||
-->
|
||
</ul>
|
||
</li>
|
||
<li><a>Resources</a>
|
||
<ul>
|
||
<li><a rel="noopener noreferrer" target="_blank" href="https://www.dash.org/community/">Community</a></li>
|
||
<li><a href="/en/vocabulary">Vocabulary</a></li>
|
||
</ul>
|
||
</li>
|
||
<!-- <li><a href="/en/innovation">Innovation</a></li> -->
|
||
</ul>
|
||
|
||
</div></div>
|
||
|
||
<div class="body">
|
||
|
||
|
||
<div class="breadcrumbs">
|
||
|
||
|
||
<a href="/en/">Developer Documentation</a>
|
||
|
||
>
|
||
|
||
|
||
|
||
Examples
|
||
|
||
|
||
|
||
</div>
|
||
|
||
|
||
|
||
<div id="content" class="content">
|
||
<link rel="stylesheet" href="/css/jquery-ui.min.css" />
|
||
|
||
<h1 id="dash-developer-examples">Dash Developer Examples</h1>
|
||
|
||
<p class="summary">Find examples of how to build programs using Dash.</p>
|
||
|
||
<div id="toc" class="toc">
|
||
<div>
|
||
|
||
<ul id="markdown-toc">
|
||
<li><a href="#testing-applications" id="markdown-toc-testing-applications">Testing Applications</a> <ul>
|
||
<li><a href="#testnet" id="markdown-toc-testnet">Testnet</a></li>
|
||
<li><a href="#regtest-mode" id="markdown-toc-regtest-mode">Regtest Mode</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a href="#transactions" id="markdown-toc-transactions">Transactions</a> <ul>
|
||
<li><a href="#transaction-tutorial" id="markdown-toc-transaction-tutorial">Transaction Tutorial</a> <ul>
|
||
<li><a href="#simple-spending" id="markdown-toc-simple-spending">Simple Spending</a> <ul>
|
||
<li><a href="#simple-spending-script" id="markdown-toc-simple-spending-script">Simple Spending Script</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a href="#simple-raw-transaction" id="markdown-toc-simple-raw-transaction">Simple Raw Transaction</a></li>
|
||
<li><a href="#complex-raw-transaction" id="markdown-toc-complex-raw-transaction">Complex Raw Transaction</a></li>
|
||
<li><a href="#offline-signing" id="markdown-toc-offline-signing">Offline Signing</a></li>
|
||
<li><a href="#p2sh-multisig" id="markdown-toc-p2sh-multisig">P2SH Multisig</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li><a href="#p2p-network" id="markdown-toc-p2p-network">P2P Network</a> <ul>
|
||
<li><a href="#creating-a-bloom-filter" id="markdown-toc-creating-a-bloom-filter">Creating A Bloom Filter</a></li>
|
||
<li><a href="#evaluating-a-bloom-filter" id="markdown-toc-evaluating-a-bloom-filter">Evaluating A Bloom Filter</a></li>
|
||
<li><a href="#bloom-filter-script" id="markdown-toc-bloom-filter-script">Bloom Filter Script</a></li>
|
||
<li><a href="#retrieving-a-merkleblock" id="markdown-toc-retrieving-a-merkleblock">Retrieving A MerkleBlock</a></li>
|
||
<li><a href="#parsing-a-merkleblock" id="markdown-toc-parsing-a-merkleblock">Parsing A MerkleBlock</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<ul class="goback"><li><a href="/en/">Return To Overview</a></li></ul>
|
||
<ul class="reportissue"><li><a href="https://github.com/dash-docs/dash-docs/issues/new" onmouseover="updateIssue(event);">Report An Issue</a></li></ul>
|
||
<ul class="editsource"><li><a href="https://github.com/dash-docs/dash-docs/tree/master/_includes" onmouseover="updateSource(event);">Edit On GitHub</a></li></ul>
|
||
|
||
</div>
|
||
</div>
|
||
<div class="toccontent">
|
||
|
||
<!--Temporary disclaimer BEGIN-->
|
||
<div id="develdocdisclaimer" class="develdocdisclaimer"><div>
|
||
<b>BETA</b>: This documentation has not been extensively reviewed by Dash experts and so likely contains numerous errors. Please use the <em>Issue</em> and <em>Edit</em> links on the bottom left menu to help us improve. <a href="#" onclick="disclaimerClose(event);">Click here</a> to close this disclaimer.
|
||
<a class="develdocdisclaimerclose" onclick="disclaimerClose(event);">X</a>
|
||
</div></div>
|
||
<script>disclaimerAutoClose();</script>
|
||
|
||
<!--Temporary disclaimer END-->
|
||
|
||
<p><input id="glossary_term" class="glossary_term" placeholder="Search the glossary, RPCs, and more" /></p>
|
||
|
||
<p>The following guide aims to provide examples to help you start
|
||
building Dash-based applications. To make the best use of this document,
|
||
you may want to install the current version of Dash Core, either from
|
||
<a href="https://github.com/dashpay/dash/">source</a> or from a <a href="https://www.dash.org/wallets/#wallets">pre-compiled executable</a>.</p>
|
||
|
||
<p>Once installed, you’ll have access to three programs: <code class="highlighter-rouge">dashd</code>,
|
||
<code class="highlighter-rouge">dash-qt</code>, and <code class="highlighter-rouge">dash-cli</code>.</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<p><code class="highlighter-rouge">dash-qt</code> provides a combination full Dash <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peer</a> and <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>
|
||
frontend. From the Help menu, you can access a console where you can
|
||
enter the <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a> commands used throughout this document.</p>
|
||
</li>
|
||
<li>
|
||
<p><code class="highlighter-rouge">dashd</code> is more useful for programming: it provides a full <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peer</a>
|
||
which you can interact with through <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPCs</a> to port 9998 (or 19998
|
||
for <a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">testnet</a> / 18332 for <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest</a>).</p>
|
||
</li>
|
||
<li>
|
||
<p><code class="highlighter-rouge">dash-cli</code> allows you to send <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a> commands to <code class="highlighter-rouge">dashd</code> from the
|
||
command line. For example, <code class="highlighter-rouge">dash-cli help</code></p>
|
||
</li>
|
||
</ul>
|
||
|
||
<p>All three programs get settings from <code class="highlighter-rouge">dash.conf</code> in the <code class="highlighter-rouge">DashCore</code>
|
||
application directory:</p>
|
||
|
||
<ul>
|
||
<li>
|
||
<p>Windows: <code class="highlighter-rouge">%APPDATA%\DashCore\</code></p>
|
||
</li>
|
||
<li>
|
||
<p>OSX: <code class="highlighter-rouge">$HOME/Library/Application Support/DashCore/</code></p>
|
||
</li>
|
||
<li>
|
||
<p>Linux: <code class="highlighter-rouge">$HOME/.dashcore/</code></p>
|
||
</li>
|
||
</ul>
|
||
|
||
<p>To use <code class="highlighter-rouge">dashd</code> and <code class="highlighter-rouge">dash-cli</code>, you will need to add a <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a> password
|
||
to your <code class="highlighter-rouge">dash.conf</code> file. Both programs will read from the same file
|
||
if both run on the same system as the same user, so any long random
|
||
password will work:</p>
|
||
|
||
<div class="highlighter-rouge"><pre><code>rpcpassword=change_this_to_a_long_random_password
|
||
</code></pre>
|
||
</div>
|
||
|
||
<p>You should also make the <code class="highlighter-rouge">dash.conf</code> file only readable to its
|
||
owner. On Linux, Mac OSX, and other Unix-like systems, this can be
|
||
accomplished by running the following command in the Dash Core application
|
||
directory:</p>
|
||
|
||
<div class="highlighter-rouge"><pre><code>chmod 0600 dash.conf
|
||
</code></pre>
|
||
</div>
|
||
|
||
<p>For development, it’s safer and cheaper to use Dash’s test <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> (<a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">testnet</a>)
|
||
or <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regression test mode</a> (<a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest</a>) described below.</p>
|
||
|
||
<p>Questions about Dash use are best sent to the <a href="https://www.dash.org/forum/categories/dash-support.61/">Dash forum</a> and <a href="http://www.dashchat.org">Discord channels</a>. Errors or suggestions related to
|
||
documentation on dash-docs.github.io can be <a href="https://github.com/dash-docs/dash-docs/issues">submitted as an issue</a>.</p>
|
||
|
||
<p>In the following documentation, some strings have been shortened or wrapped: “[…]”
|
||
indicates extra data was removed, and lines ending in a single backslash “\”
|
||
are continued below. If you hover your mouse over a paragraph, cross-reference
|
||
links will be shown in blue. If you hover over a cross-reference link, a brief
|
||
definition of the term will be displayed in a tooltip.</p>
|
||
|
||
<h2 id="testing-applications">Testing Applications</h2>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_testing.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_testing.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_testing.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_testing.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<!-- __ -->
|
||
|
||
<p>Dash Core provides testing tools designed to let developers
|
||
test their applications with reduced risks and limitations.</p>
|
||
|
||
<h3 id="testnet">Testnet</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_testing.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_testing.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_testing.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_testing.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>When run with no arguments, all Dash Core programs default to Dash’s main
|
||
<a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> (<a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." id="term-mainnet" class="term">mainnet</a>). However, for development,
|
||
it’s safer and cheaper to use Dash’s test <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> (<a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">testnet</a>)
|
||
where the <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> spent have no real-world value. <a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">Testnet</a> also relaxes some
|
||
restrictions (such as <a href="/en/glossary/standard-transaction" title="A transaction that passes Dash Core's IsStandard() and IsStandardTx() tests. Only standard transactions are mined or broadcast by peers running the default Dash Core software." class="auto-link">standard transaction</a> checks) so you can test functions
|
||
which might currently be disabled by default on <a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>.</p>
|
||
|
||
<p>To use <a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">testnet</a>, use the argument <code class="highlighter-rouge">-testnet</code> with <code class="highlighter-rouge">dash-cli</code>, <code class="highlighter-rouge">dashd</code>
|
||
or <code class="highlighter-rouge">dash-qt</code> or add <code class="highlighter-rouge">testnet=1</code> to your <code class="highlighter-rouge">dash.conf</code> file as
|
||
<a href="/en/developer-examples">described earlier</a>. To get free <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> for testing,
|
||
check the faucets listed below. They are community supported and due to
|
||
potentially frequent <a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">Testnet</a> changes, one or more of them may be unavailable at
|
||
a given time:</p>
|
||
|
||
<ul>
|
||
<li><a href="https://test.faucet.dash.org/">Testnet Faucet - Dash.org</a></li>
|
||
<li><a href="http://test.faucet.masternode.io/">Testnet Faucet - Masternode.io</a></li>
|
||
<li><a href="https://test.faucet.dashninja.pl/">Testnet Faucet - Dashninja.pl</a></li>
|
||
</ul>
|
||
|
||
<p><a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">Testnet</a> is a public resource provided for free by members of the community,
|
||
so please don’t abuse it.</p>
|
||
|
||
<h3 id="regtest-mode">Regtest Mode</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_testing.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_testing.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_testing.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_testing.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>For situations where interaction with random <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peers</a> and <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a> is unnecessary or
|
||
unwanted, Dash Core’s <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regression test mode</a> (<a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a>) lets you
|
||
instantly create a brand-new private <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a> with the same basic
|
||
rules as <a href="/en/glossary/testnet" title="A global testing environment in which developers can obtain and spend duffs that have no real-world value on a network that is very similar to the Dash mainnet." class="auto-link">testnet</a>—but one major difference: you choose when to create
|
||
new <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a>, so you have complete control over the environment.</p>
|
||
|
||
<p>Many developers consider <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a> the preferred way to develop new
|
||
applications. The following example will let you create a <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest</a>
|
||
environment after you first <a href="/en/developer-examples">configure dashd</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dashd -regtest -daemon
|
||
Dash Core server starting</code></pre></figure>
|
||
|
||
<p>Start <code class="highlighter-rouge">dashd</code> in <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a> to create a private <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a>.</p>
|
||
|
||
<div class="highlighter-rouge"><pre><code>## Dash Core
|
||
dash-cli -regtest generate 101
|
||
</code></pre>
|
||
</div>
|
||
|
||
<p>Generate 101 <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a> using a special <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a>
|
||
which is only available in <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a>. This takes less than a second on
|
||
a generic PC. Because this is a new <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a> using Dash’s default
|
||
rules, the first <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a> pay a <a href="/en/glossary/block-reward" title="The amount that miners may claim as a reward for creating a block. Equal to the sum of the block subsidy (newly available duffs) plus the transactions fees paid by transactions included in the block." class="auto-link">block reward</a> of 500 dash. Unlike
|
||
<a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>, in <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a> only the first 150 <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a> pay a reward of 500 dash.
|
||
However, a <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> must have 100 <a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">confirmations</a> before that reward can be
|
||
spent, so we generate 101 <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">blocks</a> to get access to the <a href="/en/glossary/coinbase-transaction" title="The first transaction in a block. Always created by a miner, it includes a single coinbase." class="auto-link">coinbase
|
||
transaction</a> from <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> #1.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">dash-cli -regtest getbalance
|
||
500.00000000</code></pre></figure>
|
||
|
||
<p>Verify that we now have 500 dash available to spend.</p>
|
||
|
||
<p>You can now use Dash Core <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPCs</a> prefixed with <code class="highlighter-rouge">dash-cli -regtest</code>.</p>
|
||
|
||
<p><a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">Regtest</a> <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallets</a> and <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a> state (chainstate) are saved in the <code class="highlighter-rouge">regtest</code>
|
||
subdirectory of the Dash Core configuration directory. You can safely
|
||
delete the <code class="highlighter-rouge">regtest</code> subdirectory and restart Dash Core to
|
||
start a new <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest</a>. (See the <a href="/en/developer-examples">Developer Examples Introduction</a> for default
|
||
configuration directory locations on various operating systems. Always back up
|
||
<a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a> <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallets</a> before performing dangerous operations such as deleting.)</p>
|
||
|
||
<!-- __ -->
|
||
|
||
<h2 id="transactions">Transactions</h2>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<h3 id="transaction-tutorial">Transaction Tutorial</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>Creating transactions is something most Dash applications do.
|
||
This section describes how to use Dash Core’s <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a> interface to
|
||
create transactions with various attributes.</p>
|
||
|
||
<p>Your applications may use something besides Dash Core to create
|
||
transactions, but in any system, you will need to provide the same kinds
|
||
of data to create transactions with the same attributes as those
|
||
described below.</p>
|
||
|
||
<p>In order to use this tutorial, you will need to setup <a href="https://www.dash.org/wallets/#wallets">Dash Core</a>
|
||
and create a <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regression test mode</a> environment with 500 DASH in your test
|
||
<a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>.</p>
|
||
|
||
<h4 id="simple-spending">Simple Spending</h4>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>Dash Core provides several <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPCs</a> which handle all the details of
|
||
spending, including creating <a href="/en/glossary/change-address" title="An output in a transaction which returns duffs to the spender, thus preventing too much of the input value from going to transaction fees." class="auto-link">change outputs</a> and paying appropriate fees.
|
||
Even advanced users should use these <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPCs</a> whenever possible to decrease
|
||
the chance that <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> will be lost by mistake.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getnewaddress
|
||
yLp6ZJueuigiF4s9E1Pv8tEunDPEsjyQfd
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS</span><span class="o">=</span>yLp6ZJueuigiF4s9E1Pv8tEunDPEsjyQfd</code></pre></figure>
|
||
|
||
<p>Get a new Dash <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> and save it in the shell variable <code class="highlighter-rouge">$NEW_ADDRESS</code>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendtoaddress <span class="nv">$NEW_ADDRESS</span> 10.00
|
||
c7e5ae1240fdd83bb94c94a93816ed2ab7bcb56ec3ff8a9725c5c1e0482684ea</code></pre></figure>
|
||
|
||
<p>Send 10 dash to the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> using the <a href="/en/developer-reference#sendtoaddress" class="auto-link"><code class="highlighter-rouge">sendtoaddress</code> RPC</a>. The
|
||
returned hex string is the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">transaction identifier</a> (<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a>).</p>
|
||
|
||
<p>The <a href="/en/developer-reference#sendtoaddress" class="auto-link"><code class="highlighter-rouge">sendtoaddress</code> RPC</a> automatically selects an unspent transaction
|
||
<a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> (<a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>) from which to spend the <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a>. In this case, it
|
||
withdrew the <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> from our only available <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>, the <a href="/en/glossary/coinbase-transaction" title="The first transaction in a block. Always created by a miner, it includes a single coinbase." class="auto-link">coinbase
|
||
transaction</a> for <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> #1 which matured with the creation of <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> #101.
|
||
To spend a specific <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>, you could use the <a href="/en/developer-reference#sendfrom" class="auto-link"><code class="highlighter-rouge">sendfrom</code> RPC</a> instead.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest listunspent
|
||
<span class="o">[</span>
|
||
<span class="o">]</span></code></pre></figure>
|
||
|
||
<p>Use the <a href="/en/developer-reference#listunspent" class="auto-link"><code class="highlighter-rouge">listunspent</code> RPC</a> to display the <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a> belonging to this <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>.
|
||
The list is empty because it defaults to only showing <a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">confirmed</a>
|
||
<a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a> and we just spent our only <a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">confirmed</a> <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest listunspent 0</code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="s2">"c7e5ae1240fdd83bb94c94a93816ed2ab7bcb56ec3ff8a9725c5c1e0482684ea"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="s2">"yLp6ZJueuigiF4s9E1Pv8tEunDPEsjyQfd"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"account"</span><span class="p">:</span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="s2">"76a914056b1fe57914236149feb21dcbc6b86f4bdd9f4988ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="mf">10.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="s2">"c7e5ae1240fdd83bb94c94a93816ed2ab7bcb56ec3ff8a9725c5c1e0482684ea"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="s2">"yeP6Tw2uW4nWAFWRytw8TyshErTq59dUkN"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="s2">"76a914c622e98a6ccf34d02620612f58f20a50061cf4b188ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="mf">490.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Re-running the <a href="/en/developer-reference#listunspent" class="auto-link"><code class="highlighter-rouge">listunspent</code> RPC</a> with the argument “0” to also display
|
||
<a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">unconfirmed transactions</a> shows that we have two <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a>, both with the
|
||
same <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a>. The first <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> shown is a <a href="/en/glossary/change-address" title="An output in a transaction which returns duffs to the spender, thus preventing too much of the input value from going to transaction fees." class="auto-link">change output</a> that <a href="/en/developer-reference#sendtoaddress" class="auto-link"><code class="highlighter-rouge">sendtoaddress</code></a>
|
||
created using a new <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> from the key pool. The second <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> shown is
|
||
the spend to the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> we provided. If we had spent those <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> to
|
||
someone else, that second transaction would not be displayed in our
|
||
list of <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest generate 1
|
||
|
||
<span class="gp">> </span><span class="nb">unset </span>NEW_ADDRESS</code></pre></figure>
|
||
|
||
<p>Create a new <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> to confirm the transaction above (takes less than a
|
||
second) and clear the shell variable.</p>
|
||
|
||
<h5 id="simple-spending-script">Simple Spending Script</h5>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<!-- Shell script to run previous example -->
|
||
<p>Shell script to run the previous example:</p>
|
||
|
||
<div class="multicode">
|
||
<script src="https://gist.github.com/dash-docs/f40bddfc0844ec0d66d196720dc936f8.js"></script>
|
||
|
||
</div>
|
||
|
||
<h4 id="simple-raw-transaction">Simple Raw Transaction</h4>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>The <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> <a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPCs</a> allow users to create custom transactions and
|
||
delay broadcasting those transactions. However, mistakes made in <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw
|
||
transactions</a> may not be detected by Dash Core, and a number of <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw
|
||
transaction</a> users have permanently lost large numbers of <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a>, so
|
||
please be careful using <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transactions</a> on <a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>.</p>
|
||
|
||
<p>This subsection covers one of the simplest possible <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transactions</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest listunspent</code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f700d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yRdk89fwSW1mUBxQo5fCmEfTva7b4wh2H5"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"account"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a9143a4e8960f26c1fa82d937046959b656e4dd7966688ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">10.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f700d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yavnyFMebbfX4F2VC25P18FW6LS66h2wqJ"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914a0411dbed3eab4341d5c41496d61b4fa1b22037e88ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">490.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"9036265a8f577421e556cd4f729752d73469953deea759de11efa9ba354936a8"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yWtgzKSckhedxtJ8NXhShWGjfBivkvBGgG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"21023fff9c9dc9088c0aeba90d75413705091111311d761054de23ac\
|
||
dd217450869aac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">500.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">101</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">UTXO_TXID</span><span class="o">=</span>9036265a8f577421e556cd4f729752d73469953deea759de11ef[...]
|
||
<span class="gp">> </span><span class="nv">UTXO_VOUT</span><span class="o">=</span>0</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Re-rerun <a href="/en/developer-reference#listunspent" class="auto-link"><code class="highlighter-rouge">listunspent</code></a>. We now have three <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a>: the two transactions we
|
||
created before plus the <a href="/en/glossary/coinbase-transaction" title="The first transaction in a block. Always created by a miner, it includes a single coinbase." class="auto-link">coinbase transaction</a> from <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> #2. We save the
|
||
<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> and <a href="/en/developer-guide#term-output-index" title="The sequentially-numbered index of outputs in a single transaction starting from 0" class="auto-link">output index</a> number (vout) of that <a href="/en/glossary/coinbase" title="A special field used as the sole input for coinbase transactions. The coinbase allows claiming the block reward and provides up to 100 bytes for arbitrary data." class="auto-link">coinbase</a> <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> to shell
|
||
variables.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">> dash-cli -regtest getnewaddress
|
||
yfV9Wirf5RkYHgNDttjpBz8Wdi8BavLHcP
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS</span><span class="o">=</span>yfV9Wirf5RkYHgNDttjpBz8Wdi8BavLHcP</code></pre></figure>
|
||
|
||
<p>Get a new <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> to use in the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">## Outputs - inputs = transaction fee, so always double-check your math!</span>
|
||
<span class="gp">> </span>dash-cli -regtest createrawtransaction <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">'
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
{
|
||
"'</span><span class="nv">$NEW_ADDRESS</span><span class="s1">'": 499.9999
|
||
}'''</span>
|
||
0100000001a8364935baa9ef11de59a7ee3d956934d75297724fcd56e5217457<span class="se">\</span>
|
||
8f5a2636900000000000ffffffff01f04c3ba40b0000001976a914d240140859<span class="se">\</span>
|
||
744755d73e5967081c3bedceffc5db88ac00000000
|
||
|
||
<span class="gp">> </span><span class="nv">RAW_TX</span><span class="o">=</span>0100000001a8364935baa9ef11de59a7ee3d956934d75297724fcd5[...]</code></pre></figure>
|
||
|
||
<p>Using two arguments to the <a href="/en/developer-reference#createrawtransaction" class="auto-link"><code class="highlighter-rouge">createrawtransaction</code> RPC</a>, we create a new
|
||
<a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw format transaction</a>. The first argument (a JSON array) references
|
||
the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> of the <a href="/en/glossary/coinbase-transaction" title="The first transaction in a block. Always created by a miner, it includes a single coinbase." class="auto-link">coinbase transaction</a> from <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> #2 and the index
|
||
number (0) of the <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> from that transaction we want to spend. The
|
||
second argument (a JSON object) creates the <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> with the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a>
|
||
(<a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a> hash) and number of dash we want to transfer.
|
||
We save the resulting <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw format transaction</a> to a shell variable.</p>
|
||
|
||
<p><img src="/img/icons/icon_warning.svg" alt="Warning icon" />
|
||
<strong>Warning:</strong> <a href="/en/developer-reference#createrawtransaction" class="auto-link"><code class="highlighter-rouge">createrawtransaction</code></a> does not automatically create <a href="/en/glossary/change-address" title="An output in a transaction which returns duffs to the spender, thus preventing too much of the input value from going to transaction fees." class="auto-link">change
|
||
outputs</a>, so you can easily accidentally pay a large <a href="/en/glossary/transaction-fee" title="The amount remaining when the value of all outputs in a transaction are subtracted from all inputs in a transaction; the fee is paid to the miner who includes that transaction in a block." class="auto-link">transaction fee</a>. In
|
||
this example, our <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a> had 500.0000 dash and our <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a>
|
||
(<code class="highlighter-rouge">$NEW_ADDRESS</code>) is being paid 499.9999 dash, so the transaction will
|
||
include a fee of 0.0001 dash. If we had paid <code class="highlighter-rouge">$NEW_ADDRESS</code> only 100
|
||
dash with no other changes to this transaction, the <a href="/en/glossary/transaction-fee" title="The amount remaining when the value of all outputs in a transaction are subtracted from all inputs in a transaction; the fee is paid to the miner who includes that transaction in a block." class="auto-link">transaction fee</a>
|
||
would be a whopping 400 dash. See the Complex <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">Raw Transaction</a>
|
||
subsection below for how to create a transaction with multiple <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a> so you
|
||
can send the change back to yourself.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest decoderawtransaction <span class="nv">$RAW_TX</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"7cbd2245ee5d824c00fc08b3bf2f694ad9a215d38d897fcf2df64a43c59bb97b"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"size"</span><span class="p">:</span><span class="w"> </span><span class="mi">85</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"version"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"locktime"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vin"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"9036265a8f577421e556cd4f729752d73469953deea759de11efa9ba354936a8"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">],</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"value"</span><span class="p">:</span><span class="w"> </span><span class="mf">499.99990000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"valueSat"</span><span class="p">:</span><span class="w"> </span><span class="mi">49999990000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"n"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OP_DUP OP_HASH160 d240140859744755d73e5967081c3bedceffc5db\
|
||
OP_EQUALVERIFY OP_CHECKSIG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914d240140859744755d73e5967081c3bedceffc5db88ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"reqSigs"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"pubkeyhash"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"addresses"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"yfV9Wirf5RkYHgNDttjpBz8Wdi8BavLHcP"</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Use the <a href="/en/developer-reference#decoderawtransaction" class="auto-link"><code class="highlighter-rouge">decoderawtransaction</code> RPC</a> to see exactly what the transaction
|
||
we just created does.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$RAW_TX</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000001a8364935baa9ef11de59a7ee3d956934d75297724fcd\
|
||
56e52174578f5a2636900000000049483045022100b4e5e9224afa\
|
||
de8686bb22a957d1ec1587a66ee84943761b2d9061d5f751cd7602\
|
||
203c88d4064641a413ce3d0824264d6d87908960487afe9a3a133e\
|
||
7d67a22fd05101ffffffff01f04c3ba40b0000001976a914d24014\
|
||
0859744755d73e5967081c3bedceffc5db88ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">SIGNED_RAW_TX</span><span class="o">=</span>0100000001a8364935baa9ef11de59a7ee3d956934d75297[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Use the <a href="/en/developer-reference#signrawtransaction" class="auto-link"><code class="highlighter-rouge">signrawtransaction</code> RPC</a> to sign the transaction created by
|
||
<a href="/en/developer-reference#createrawtransaction" class="auto-link"><code class="highlighter-rouge">createrawtransaction</code></a> and save the returned “hex” raw format signed
|
||
transaction to a shell variable.</p>
|
||
|
||
<p>Even though the transaction is now complete, the Dash Core <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> we’re
|
||
connected to doesn’t know anything about the transaction, nor does any
|
||
other part of the <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a>. We’ve created a spend, but we haven’t
|
||
actually spent anything because we could simply unset the
|
||
<code class="highlighter-rouge">$SIGNED_RAW_TX</code> variable to eliminate the transaction.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendrawtransaction <span class="nv">$SIGNED_RAW_TX</span>
|
||
fa0f4105b0a2b2706d65581c5e6411d3970253c7f231944fa2f978b4a3d9010d</code></pre></figure>
|
||
|
||
<p>Send the signed transaction to the connected <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> using the
|
||
<a href="/en/developer-reference#sendrawtransaction" class="auto-link"><code class="highlighter-rouge">sendrawtransaction</code> RPC</a>. After accepting the transaction, the <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>
|
||
would usually then broadcast it to other <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peers</a>, but we’re not currently
|
||
connected to other <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peers</a> because we started in <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest mode</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest generate 1
|
||
|
||
<span class="gp">> </span><span class="nb">unset </span>UTXO_TXID UTXO_VOUT NEW_ADDRESS RAW_TX SIGNED_RAW_TX</code></pre></figure>
|
||
|
||
<p>Generate a <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> to confirm the transaction and clear our shell
|
||
variables.</p>
|
||
|
||
<h4 id="complex-raw-transaction">Complex Raw Transaction</h4>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>In this example, we’ll create a transaction with two <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> and two
|
||
<a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a>. We’ll sign each of the <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> separately, as might happen if
|
||
the two <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> belonged to different people who agreed to create a
|
||
transaction together (such as a CoinJoin transaction).</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest listunspent</code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"fa0f4105b0a2b2706d65581c5e6411d3970253c7f231944fa2f978b4a3d9010d"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yfV9Wirf5RkYHgNDttjpBz8Wdi8BavLHcP"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"account"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914d240140859744755d73e5967081c3bedceffc5db88ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">499.99990000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f700d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yRdk89fwSW1mUBxQo5fCmEfTva7b4wh2H5"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"account"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a9143a4e8960f26c1fa82d937046959b656e4dd7966688ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">10.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f700d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yavnyFMebbfX4F2VC25P18FW6LS66h2wqJ"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914a0411dbed3eab4341d5c41496d61b4fa1b22037e88ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">490.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ea6d596da55a137846f8b08bfd414b4667ce456f9e3b3182e6f05810e8613d84"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yWtgzKSckhedxtJ8NXhShWGjfBivkvBGgG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"21023fff9c9dc9088c0aeba90d75413705091111311d761054de23\
|
||
acdd217450869aac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"amount"</span><span class="p">:</span><span class="w"> </span><span class="mf">500.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"confirmations"</span><span class="p">:</span><span class="w"> </span><span class="mi">101</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ps_rounds"</span><span class="p">:</span><span class="w"> </span><span class="mi">-2</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"spendable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"solvable"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">UTXO1_TXID</span><span class="o">=</span>ea6d596da55a137846f8b08bfd414b4667ce456f9e3b3182e6f05810e8613d84
|
||
<span class="gp">> </span><span class="nv">UTXO1_VOUT</span><span class="o">=</span>0
|
||
<span class="gp">> </span><span class="nv">UTXO1_ADDRESS</span><span class="o">=</span>yWtgzKSckhedxtJ8NXhShWGjfBivkvBGgG
|
||
|
||
<span class="gp">> </span><span class="nv">UTXO2_TXID</span><span class="o">=</span>f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f700d130
|
||
<span class="gp">> </span><span class="nv">UTXO2_VOUT</span><span class="o">=</span>0
|
||
<span class="gp">> </span><span class="nv">UTXO2_ADDRESS</span><span class="o">=</span>yRdk89fwSW1mUBxQo5fCmEfTva7b4wh2H5</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>For our two <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a>, we select two <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a> by placing the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> and <a href="/en/developer-guide#term-output-index" title="The sequentially-numbered index of outputs in a single transaction starting from 0" class="auto-link">output
|
||
index</a> numbers (vouts) in shell variables. We also save the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a>
|
||
corresponding to the <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> (hashed or unhashed) used in those
|
||
transactions. We need the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> so we can get the corresponding
|
||
<a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> from our <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest dumpprivkey <span class="nv">$UTXO1_ADDRESS</span>
|
||
cNL522MEQUnQxsZJo4ryPH8sPd2uVZaFKjKnZivo9DyVjpAGU7qP
|
||
|
||
<span class="gp">> </span>dash-cli -regtest dumpprivkey <span class="nv">$UTXO2_ADDRESS</span>
|
||
cPtZ9nagmjQ5bRKMuqoDz8xni6hRPfZ1zp3TSrqH3j3RyUThTYGN
|
||
|
||
<span class="gp">> </span><span class="nv">UTXO1_PRIVATE_KEY</span><span class="o">=</span>cNL522MEQUnQxsZJo4ryPH8sPd2uVZaFKjKnZivo9DyVjpAGU7qP
|
||
|
||
<span class="gp">> </span><span class="nv">UTXO2_PRIVATE_KEY</span><span class="o">=</span>cPtZ9nagmjQ5bRKMuqoDz8xni6hRPfZ1zp3TSrqH3j3RyUThTYGN</code></pre></figure>
|
||
|
||
<p>Use the <a href="/en/developer-reference#dumpprivkey" class="auto-link"><code class="highlighter-rouge">dumpprivkey</code> RPC</a> to get the <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> corresponding to the
|
||
<a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> used in the two <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a> out <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> we will be spending. We need
|
||
the <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> so we can sign each of the <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> separately.</p>
|
||
|
||
<p><img src="/img/icons/icon_warning.svg" alt="Warning icon" />
|
||
<strong>Warning:</strong> Users should never manually manage <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> on <a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>.
|
||
As dangerous as <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transactions</a> are (see warnings above), making a
|
||
mistake with a <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private key</a> can be much worse—as in the case of a <a href="/en/glossary/hd-protocol" title="The Hierarchical Deterministic (HD) key creation and transfer protocol (BIP32), which allows creating child keys from parent keys in a hierarchy. Wallets using the HD protocol are called HD wallets." class="auto-link">HD
|
||
wallet</a> <a href="/en/developer-guide#hardened-keys">cross-generational key compromise</a>.
|
||
<strong>These examples are to help you learn, not for you to emulate on
|
||
<a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>.</strong></p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getnewaddress
|
||
yhshGrdbh3rWt9EPaSi7xSGRFMvFdzTZ8n
|
||
<span class="gp">> </span>dash-cli -regtest getnewaddress
|
||
yesLaP5XFTaLZiWAo2zK8mFfUCtV8rRhKw
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS1</span><span class="o">=</span>yhshGrdbh3rWt9EPaSi7xSGRFMvFdzTZ8n
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS2</span><span class="o">=</span>yesLaP5XFTaLZiWAo2zK8mFfUCtV8rRhKw</code></pre></figure>
|
||
|
||
<p>For our two <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a>, get two new <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">## Outputs - inputs = transaction fee, so always double-check your math!</span>
|
||
<span class="gp">> </span>dash-cli -regtest createrawtransaction <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO1_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO1_VOUT</span><span class="s1">'
|
||
},
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO2_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO2_VOUT</span><span class="s1">'
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
{
|
||
"'</span><span class="nv">$NEW_ADDRESS1</span><span class="s1">'": 499.9999,
|
||
"'</span><span class="nv">$NEW_ADDRESS2</span><span class="s1">'": 10
|
||
}'''</span>
|
||
0100000002843d61e81058f0e682313b9e6f45ce67464b41fd8bb0f84678135a<span class="se">\</span>
|
||
a56d596dea0000000000ffffffff30d100f7762956100a2396403c60e13e7a13<span class="se">\</span>
|
||
520167acc6d38978ec33ada44cf80000000000ffffffff02f04c3ba40b000000<span class="se">\</span>
|
||
1976a914ec73fe6129b249617bb5f20c8760708055fb6fdb88ac00ca9a3b0000<span class="se">\</span>
|
||
00001976a914cb7a56b046479f8c247875d672d3e1aed18c33f488ac00000000
|
||
|
||
<span class="gp">> </span><span class="nv">RAW_TX</span><span class="o">=</span>0100000002843d61e81058f0e682313b9e6f45ce67464b41fd8bb0f[...]</code></pre></figure>
|
||
|
||
<p>Create the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> using <a href="/en/developer-reference#createrawtransaction" class="auto-link"><code class="highlighter-rouge">createrawtransaction</code></a> much the same as
|
||
before, except now we have two <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> and two <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$RAW_TX</span> <span class="s1">'[]'</span> <span class="s1">'''
|
||
[
|
||
"'</span><span class="nv">$UTXO1_PRIVATE_KEY</span><span class="s1">'"
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000002843d61e81058f0e682313b9e6f45ce67464b41fd8bb0\
|
||
f84678135aa56d596dea00000000494830450221009f7f356c0cc2d3337b5f\
|
||
76dfc6de9f9be7c8c5ac2074cbeeba4815b90329602002207790f23361480e\
|
||
2a5a2d1fa6e293ccd5cd01279ad301176f091b84d6dd8e8f6501ffffffff30\
|
||
d100f7762956100a2396403c60e13e7a13520167acc6d38978ec33ada44cf8\
|
||
0000000000ffffffff02f04c3ba40b0000001976a914ec73fe6129b249617b\
|
||
b5f20c8760708055fb6fdb88ac00ca9a3b000000001976a914cb7a56b04647\
|
||
9f8c247875d672d3e1aed18c33f488ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"errors"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a1\
|
||
0562976f700d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"error"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Operation not valid with the current stack size"</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">PARTLY_SIGNED_RAW_TX</span><span class="o">=</span>0100000002843d61e81058f0e682313b9e6f45ce6[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Signing the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> with <a href="/en/developer-reference#signrawtransaction" class="auto-link"><code class="highlighter-rouge">signrawtransaction</code></a> gets more
|
||
complicated as we now have three arguments:</p>
|
||
|
||
<ol>
|
||
<li>
|
||
<p>The unsigned <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a>.</p>
|
||
</li>
|
||
<li>
|
||
<p>An empty array. We don’t do anything with this argument in this
|
||
operation, but some valid JSON must be provided to get access to the
|
||
later positional arguments.</p>
|
||
</li>
|
||
<li>
|
||
<p>The <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private key</a> we want to use to sign one of the <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a>.</p>
|
||
</li>
|
||
</ol>
|
||
|
||
<p>The result is a <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> with only one <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a> signed; the fact
|
||
that the transaction isn’t fully signed is indicated by value of the
|
||
<code class="highlighter-rouge">complete</code> JSON field. We save the incomplete, partly-signed <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw
|
||
transaction</a> hex to a shell variable.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$PARTLY_SIGNED_RAW_TX</span> <span class="s1">'[]'</span> <span class="s1">'''
|
||
[
|
||
"'</span><span class="nv">$UTXO2_PRIVATE_KEY</span><span class="s1">'"
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000002843d61e81058f0e682313b9e6f45ce67464b41fd8bb0\
|
||
f84678135aa56d596dea00000000494830450221009f7f356c0cc2d3337b5f\
|
||
76dfc6de9f9be7c8c5ac2074cbeeba4815b90329602002207790f23361480e\
|
||
2a5a2d1fa6e293ccd5cd01279ad301176f091b84d6dd8e8f6501ffffffff30\
|
||
d100f7762956100a2396403c60e13e7a13520167acc6d38978ec33ada44cf8\
|
||
000000006a47304402207867e88e3fe2c926df29376d77eba81daf9f4a5573\
|
||
44d4f02e9c7dcee96a51e4022076274c2365dc069e7ef797c95c75ab6e01ca\
|
||
3757342f3e6f21a3d9d01086efb7012102ff9005f79aa4c22ac48fa93d9b7f\
|
||
40f321db1c13cd70cf08bdab3e23c8d19620ffffffff02f04c3ba40b000000\
|
||
1976a914ec73fe6129b249617bb5f20c8760708055fb6fdb88ac00ca9a3b00\
|
||
0000001976a914cb7a56b046479f8c247875d672d3e1aed18c33f488ac0000\
|
||
0000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>To sign the second <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a>, we repeat the process we used to sign the
|
||
first <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a> using the second <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private key</a>. Now that both <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a> are
|
||
signed, the <code class="highlighter-rouge">complete</code> result is <em>true</em>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nb">unset </span>PARTLY_SIGNED_RAW_TX RAW_TX NEW_ADDRESS1 <span class="o">[</span>...]</code></pre></figure>
|
||
|
||
<p>Clean up the shell variables used. Unlike previous subsections, we’re
|
||
not going to send this transaction to the connected <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> with
|
||
<a href="/en/developer-reference#sendrawtransaction" class="auto-link"><code class="highlighter-rouge">sendrawtransaction</code></a>. This will allow us to illustrate in the Offline
|
||
Signing subsection below how to spend a transaction which is not yet in
|
||
the <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a> or memory pool.</p>
|
||
|
||
<h4 id="offline-signing">Offline Signing</h4>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>We will now spend the transaction created in the Complex <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">Raw Transaction</a>
|
||
subsection above without sending it to the local <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> first. This is the
|
||
same basic process used by <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a> programs for offline
|
||
signing—which generally means signing a transaction without access
|
||
to the current <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> set.</p>
|
||
|
||
<p>Offline signing is safe. However, in this example we will also be
|
||
spending an <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> which is not part of the <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a> because the
|
||
transaction containing it has never been broadcast. That can be unsafe:</p>
|
||
|
||
<p><img src="/img/icons/icon_warning.svg" alt="Warning icon" />
|
||
<strong>Warning:</strong> Transactions which spend <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a> from <a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">unconfirmed
|
||
transactions</a> are vulnerable to <a href="/en/glossary/malleability" title="The ability of someone to change (mutate) unconfirmed transactions without making them invalid, which changes the transaction's txid, making child transactions invalid." class="auto-link">transaction malleability</a>. Be sure to read
|
||
about <a href="/en/glossary/malleability" title="The ability of someone to change (mutate) unconfirmed transactions without making them invalid, which changes the transaction's txid, making child transactions invalid." class="auto-link">transaction malleability</a> and adopt good practices before spending
|
||
<a href="/en/glossary/confirmation-score" title="A score indicating the number of blocks on the best block chain that would need to be modified to remove or modify a particular transaction. A confirmed transaction has a confirmation score of one or higher." class="auto-link">unconfirmed transactions</a> on <a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">OLD_SIGNED_RAW_TX</span><span class="o">=</span>0100000002843d61e81058f0e682313b9e6f45ce67464b41fd8bb0<span class="se">\</span>
|
||
f84678135aa56d596dea00000000494830450221009f7f356c0cc2d3337b5f76dfc6<span class="se">\</span>
|
||
de9f9be7c8c5ac2074cbeeba4815b90329602002207790f23361480e2a5a2d1fa6e2<span class="se">\</span>
|
||
93ccd5cd01279ad301176f091b84d6dd8e8f6501ffffffff30d100f7762956100a23<span class="se">\</span>
|
||
96403c60e13e7a13520167acc6d38978ec33ada44cf8000000006a47304402207867<span class="se">\</span>
|
||
e88e3fe2c926df29376d77eba81daf9f4a557344d4f02e9c7dcee96a51e402207627<span class="se">\</span>
|
||
4c2365dc069e7ef797c95c75ab6e01ca3757342f3e6f21a3d9d01086efb7012102ff<span class="se">\</span>
|
||
9005f79aa4c22ac48fa93d9b7f40f321db1c13cd70cf08bdab3e23c8d19620ffffff<span class="se">\</span>
|
||
ff02f04c3ba40b0000001976a914ec73fe6129b249617bb5f20c8760708055fb6fdb<span class="se">\</span>
|
||
88ac00ca9a3b000000001976a914cb7a56b046479f8c247875d672d3e1aed18c33f4<span class="se">\</span>
|
||
88ac00000000</code></pre></figure>
|
||
|
||
<p>Put the previously signed (but not sent) transaction into a shell
|
||
variable.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest decoderawtransaction <span class="nv">$OLD_SIGNED_RAW_TX</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"5efd61cf24c9644d79646627c4d1e192e9b2a760a5c720db8f4a9f76cb781077"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"size"</span><span class="p">:</span><span class="w"> </span><span class="mi">339</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"version"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"locktime"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vin"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ea6d596da55a137846f8b08bfd414b4667ce456f9e3b3182e6f05810e86\
|
||
13d84"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"30450221009f7f356c0cc2d3337b5f76dfc6de9f9be7c8c5ac2074cbee\
|
||
ba4815b90329602002207790f23361480e2a5a2d1fa6e293ccd5cd0127\
|
||
9ad301176f091b84d6dd8e8f65[ALL]"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"4830450221009f7f356c0cc2d3337b5f76dfc6de9f9be7c8c5ac2074cb\
|
||
eeba4815b90329602002207790f23361480e2a5a2d1fa6e293ccd5cd01\
|
||
279ad301176f091b84d6dd8e8f6501"</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f70\
|
||
0d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"304402207867e88e3fe2c926df29376d77eba81daf9f4a557344d4f02e\
|
||
9c7dcee96a51e4022076274c2365dc069e7ef797c95c75ab6e01ca3757\
|
||
342f3e6f21a3d9d01086efb7[ALL] 02ff9005f79aa4c22ac48fa93d9b\
|
||
7f40f321db1c13cd70cf08bdab3e23c8d19620"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"47304402207867e88e3fe2c926df29376d77eba81daf9f4a557344d4f0\
|
||
2e9c7dcee96a51e4022076274c2365dc069e7ef797c95c75ab6e01ca37\
|
||
57342f3e6f21a3d9d01086efb7012102ff9005f79aa4c22ac48fa93d9b\
|
||
7f40f321db1c13cd70cf08bdab3e23c8d19620"</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">],</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"value"</span><span class="p">:</span><span class="w"> </span><span class="mf">499.99990000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"valueSat"</span><span class="p">:</span><span class="w"> </span><span class="mi">49999990000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"n"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OP_DUP OP_HASH160 ec73fe6129b249617bb5f20c8760708055fb6fdb\
|
||
OP_EQUALVERIFY OP_CHECKSIG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914ec73fe6129b249617bb5f20c8760708055fb6fdb88ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"reqSigs"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"pubkeyhash"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"addresses"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"yhshGrdbh3rWt9EPaSi7xSGRFMvFdzTZ8n"</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"value"</span><span class="p">:</span><span class="w"> </span><span class="mf">10.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"valueSat"</span><span class="p">:</span><span class="w"> </span><span class="mi">1000000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"n"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OP_DUP OP_HASH160 cb7a56b046479f8c247875d672d3e1aed18c33f4\
|
||
OP_EQUALVERIFY OP_CHECKSIG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a914cb7a56b046479f8c247875d672d3e1aed18c33f488ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"reqSigs"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"pubkeyhash"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"addresses"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"yesLaP5XFTaLZiWAo2zK8mFfUCtV8rRhKw"</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">UTXO_TXID</span><span class="o">=</span>5efd61cf24c9644d79646627c4d1e192e9b2a760a5c720db8f4a9f76cb781077
|
||
<span class="gp">> </span><span class="nv">UTXO_VOUT</span><span class="o">=</span>1
|
||
<span class="gp">> </span><span class="nv">UTXO_OUTPUT_SCRIPT</span><span class="o">=</span>76a914cb7a56b046479f8c247875d672d3e1aed18c33f488ac</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Decode the signed <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> so we can get its <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a>. Also, choose a
|
||
specific one of its <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXOs</a> to spend and save that <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO’s</a> <a href="/en/developer-guide#term-output-index" title="The sequentially-numbered index of outputs in a single transaction starting from 0" class="auto-link">output index</a> number
|
||
(vout) and hex <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a> (<a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">scriptPubKey</a>) into shell variables.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getnewaddress
|
||
yfijhy7gYY34J2U77xFKdMwfA8k5mVnSRa
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS</span><span class="o">=</span>yfijhy7gYY34J2U77xFKdMwfA8k5mVnSRa</code></pre></figure>
|
||
|
||
<p>Get a new <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> to spend the <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> to.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">## Outputs - inputs = transaction fee, so always double-check your math!</span>
|
||
<span class="gp">> </span>dash-cli -regtest createrawtransaction <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">'
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
{
|
||
"'</span><span class="nv">$NEW_ADDRESS</span><span class="s1">'": 9.9999
|
||
}'''</span>
|
||
0100000001771078cb769f4a8fdb20c7a560a7b2e992e1d1c4276664794d64c9<span class="se">\</span>
|
||
24cf61fd5e0100000000ffffffff01f0a29a3b000000001976a914d4d2078580<span class="se">\</span>
|
||
a9eea0ca9368d1c99c097279b8081f88ac00000000
|
||
|
||
<span class="gp">> </span><span class="nv">RAW_TX</span><span class="o">=</span>0100000001771078cb769f4a8fdb20c7a560a7b2e992e1d1c427666[...]</code></pre></figure>
|
||
|
||
<p>Create the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> the same way we’ve done in the previous
|
||
subsections.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$RAW_TX</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000001771078cb769f4a8fdb20c7a560a7b2e992e1d1c4276664794d64c9\
|
||
24cf61fd5e0100000000ffffffff01f0a29a3b000000001976a914d4d2078580\
|
||
a9eea0ca9368d1c99c097279b8081f88ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"errors"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"5efd61cf24c9644d79646627c4d1e192e9b2a760a5c720db8f4a9f76cb7\
|
||
81077"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"error"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Input not found or already spent"</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Attempt to sign the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> without any special arguments, the
|
||
way we successfully signed the the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> in the Simple <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">Raw
|
||
Transaction</a> subsection. If you’ve read the <a href="/en/developer-guide#transactions" title="A transaction spending satoshis">Transaction section</a> of
|
||
the guide, you may know why the call fails and leaves the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw
|
||
transaction</a> hex unchanged.</p>
|
||
|
||
<p><img src="/img/dev/en-signing-output-to-spend.svg" alt="Old Transaction Data Required To Be Signed" /></p>
|
||
|
||
<p>As illustrated above, the data that gets signed includes the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> and
|
||
vout from the previous transaction. That information is included in the
|
||
<a href="/en/developer-reference#createrawtransaction" class="auto-link"><code class="highlighter-rouge">createrawtransaction</code></a> <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a>. But the data that gets signed
|
||
also includes the <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a> from the previous transaction, even
|
||
though it doesn’t appear in either the unsigned or signed transaction.</p>
|
||
|
||
<p>In the other <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> subsections above, the previous <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> was
|
||
part of the <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> set known to the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>, so the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a> was able to use
|
||
the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> and <a href="/en/developer-guide#term-output-index" title="The sequentially-numbered index of outputs in a single transaction starting from 0" class="auto-link">output index</a> number to find the previous <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a> and
|
||
insert it automatically.</p>
|
||
|
||
<p>In this case, you’re spending an <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> which is unknown to the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>,
|
||
so it can’t automatically insert the previous <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$RAW_TX</span> <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">',
|
||
"scriptPubKey": "'</span><span class="nv">$UTXO_OUTPUT_SCRIPT</span><span class="s1">'"
|
||
}
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000001771078cb769f4a8fdb20c7a560a7b2e992e1d1c4276664794d64c9\
|
||
24cf61fd5e010000006a47304402204ee3aae064dccedb511a84fcade3f35f2d\
|
||
95119283e2e9f23659d91ce799cb6d02203a4cecbd7c154de8394b9505814f1e\
|
||
c842e890980e7c4c20ed182f09a71d65f3012103b0b12fb25b6382b0680ce4b0\
|
||
379bc201c4cbb391d3c0e171181f24c9a5df1468ffffffff01f0a29a3b000000\
|
||
001976a914d4d2078580a9eea0ca9368d1c99c097279b8081f88ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">SIGNED_RAW_TX</span><span class="o">=</span>0100000001771078cb769f4a8fdb20c7a560a7b2e992e1d1[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Successfully sign the transaction by providing the previous <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey
|
||
script</a> and other required <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a> data.</p>
|
||
|
||
<p>This specific operation is typically what offline signing <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallets</a> do.
|
||
The online <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a> creates the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> and gets the previous
|
||
<a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey scripts</a> for all the <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">inputs</a>. The user brings this information to
|
||
the offline <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>. After displaying the transaction details to the
|
||
user, the offline <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a> signs the transaction as we did above. The
|
||
user takes the signed transaction back to the online <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>, which
|
||
broadcasts it.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendrawtransaction <span class="nv">$SIGNED_RAW_TX</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash">error code: -25
|
||
error message:
|
||
Missing inputs
|
||
|
||
error: <span class="o">{</span><span class="s2">"code"</span>:-22,<span class="s2">"message"</span>:<span class="s2">"TX rejected"</span><span class="o">}</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Attempt to broadcast the second transaction before we’ve broadcast the
|
||
first transaction. The <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> rejects this attempt because the second
|
||
transaction spends an <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> which is not a <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> the <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> knows about.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendrawtransaction <span class="nv">$OLD_SIGNED_RAW_TX</span>
|
||
5efd61cf24c9644d79646627c4d1e192e9b2a760a5c720db8f4a9f76cb781077
|
||
<span class="gp">> </span>dash-cli -regtest sendrawtransaction <span class="nv">$SIGNED_RAW_TX</span>
|
||
f89deefb927fbd03c5acab194de2ba8f98ab160b9c4b3f57bde63073c4b5f060</code></pre></figure>
|
||
|
||
<p>Broadcast the first transaction, which succeeds, and then broadcast the
|
||
second transaction—which also now succeeds because the <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> now sees
|
||
the <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getrawmempool</code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"f89deefb927fbd03c5acab194de2ba8f98ab160b9c4b3f57bde63073c4b5f060"</span><span class="p">,</span><span class="w">
|
||
</span><span class="s2">"5efd61cf24c9644d79646627c4d1e192e9b2a760a5c720db8f4a9f76cb781077"</span><span class="w">
|
||
</span><span class="p">]</span></code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>We have once again not generated an additional <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a>, so the transactions
|
||
above have not yet become part of the <a href="/en/glossary/regression-test-mode" title="A local testing environment in which developers can almost instantly generate blocks on demand for testing events, and can create private duffs with no real-world value." class="auto-link">regtest</a> <a href="/en/glossary/block-chain" title="A chain of blocks with each block referencing the block that preceded it. The most-difficult-to-recreate chain is the best block chain." class="auto-link">block chain</a>. However, they
|
||
are part of the local <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node’s</a> memory pool.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nb">unset </span>OLD_SIGNED_RAW_TX SIGNED_RAW_TX RAW_TX <span class="o">[</span>...]</code></pre></figure>
|
||
|
||
<p>Remove old shell variables.</p>
|
||
|
||
<h4 id="p2sh-multisig">P2SH Multisig</h4>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_transactions.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_transactions.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_transactions.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_transactions.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>In this subsection, we will create a <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH multisig</a> <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a>, spend
|
||
<a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> to it, and then spend those <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> from it to another
|
||
<a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a>.</p>
|
||
|
||
<p>Creating a <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig</a> <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> is easy. <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">Multisig</a> <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a> have two
|
||
parameters, the <em>minimum</em> number of <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a> required (<em>m</em>) and the
|
||
<em>number</em> of <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> to use to validate those <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a>. This is
|
||
called <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">m-of-n</a>, and in this case we’ll be using 2-of-3.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"> > dash-cli -regtest getnewaddress
|
||
yYtWtpW7akCc2a5En8NsXeTGENyYbNgv9q
|
||
> dash-cli -regtest getnewaddress
|
||
yarm2x9eDFd9dKCycyPigwwj1vfJcYFxsH
|
||
> dash-cli -regtest getnewaddress
|
||
yLknHbtnjJRVWQr78aTfCPfNB42jfNkDWK
|
||
|
||
> <span class="nv">NEW_ADDRESS1</span><span class="o">=</span>yYtWtpW7akCc2a5En8NsXeTGENyYbNgv9q
|
||
> <span class="nv">NEW_ADDRESS2</span><span class="o">=</span>yarm2x9eDFd9dKCycyPigwwj1vfJcYFxsH
|
||
> <span class="nv">NEW_ADDRESS3</span><span class="o">=</span>yLknHbtnjJRVWQr78aTfCPfNB42jfNkDWK</code></pre></figure>
|
||
|
||
<p>Generate three new <a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH addresses</a>. <a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH addresses</a> cannot be used with
|
||
the <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig</a> <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> created below. (Hashing each <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a> is
|
||
unnecessary anyway—all the <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> are protected by a hash when
|
||
the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> is hashed.) However, Dash Core uses <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> as a
|
||
way to reference the underlying full (unhashed) <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> it knows
|
||
about, so we get the three new <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> above in order to use their
|
||
<a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a>.</p>
|
||
|
||
<p>Recall from the Guide that the hashed <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> used in <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a>
|
||
obfuscate the full <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a>, so you cannot give an <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> to another
|
||
person or device as part of creating a typical <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig output</a> or <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH multisig
|
||
redeem script</a>. You must give them a full <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest validateaddress <span class="nv">$NEW_ADDRESS3</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"isvalid"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"yLknHbtnjJRVWQr78aTfCPfNB42jfNkDWK"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a91404caa000366b99780f8e606ccc818883ca7f48f888ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"ismine"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"iswatchonly"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"isscript"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"pubkey"</span><span class="p">:</span><span class="w"> </span><span class="s2">"038007ef6fd812d73da054271b68a42dae06672cff2a30b2814935537e593\
|
||
0ebf6"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"iscompressed"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"account"</span><span class="p">:</span><span class="w"> </span><span class="s2">""</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">NEW_ADDRESS3_PUBLIC_KEY</span><span class="o">=</span>038007ef6fd812d73da054271b68a42dae0667[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Use the <a href="/en/developer-reference#validateaddress" class="auto-link"><code class="highlighter-rouge">validateaddress</code> RPC</a> to display the full (unhashed) <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a>
|
||
for one of the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a>. This is the information which will
|
||
actually be included in the <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig</a> <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a>. This is also the
|
||
information you would give another person or device as part of creating
|
||
a <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig output</a> or <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH multisig redeem script</a>.</p>
|
||
|
||
<p>We save the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> returned to a shell variable.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest createmultisig 2 <span class="s1">'''
|
||
[
|
||
"'</span><span class="nv">$NEW_ADDRESS1</span><span class="s1">'",
|
||
"'</span><span class="nv">$NEW_ADDRESS2</span><span class="s1">'",
|
||
"'</span><span class="nv">$NEW_ADDRESS3_PUBLIC_KEY</span><span class="s1">'"
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"address"</span><span class="p">:</span><span class="w"> </span><span class="s2">"8meEZF54K7GxhHhdLCCeNwFQjHENv4CK86"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"redeemScript"</span><span class="p">:</span><span class="w"> </span><span class="s2">"522103fa8866cccae3c975a72884443a351801a0ea9721cbe721558\
|
||
6ddd6fab5f39f262103b2259f42a241f4870e794521594f2af7aadf0\
|
||
e4c580a43582e58630e4618634621038007ef6fd812d73da054271b6\
|
||
8a42dae06672cff2a30b2814935537e5930ebf653ae"</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">P2SH_ADDRESS</span><span class="o">=</span>8meEZF54K7GxhHhdLCCeNwFQjHENv4CK86
|
||
<span class="gp">> </span><span class="nv">P2SH_REDEEM_SCRIPT</span><span class="o">=</span>522103fa8866cccae3c975a72884443a351801a0ea9[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>Use the <a href="/en/developer-reference#createmultisig" class="auto-link"><code class="highlighter-rouge">createmultisig</code> RPC</a> with two arguments, the number (<em>n</em>) of
|
||
<a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a> required and a list of <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> or <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a>. Because
|
||
<a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH addresses</a> can’t be used in the <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig</a> <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> created by this
|
||
<a href="/en/developer-reference#remote-procedure-calls-rpcs" class="auto-link">RPC</a>, the only <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> which can be provided are those belonging to a
|
||
<a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a> in the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>. In this case, we provide two <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">addresses</a> and
|
||
one <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public key</a>—all of which will be converted to <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> in the
|
||
<a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a>.</p>
|
||
|
||
<p>The <a href="/en/glossary/p2sh-address" title="A Dash payment address comprising a hashed script, allowing the spender to create a standard pubkey script that Pays To Script Hash (P2SH). The script can be almost any valid pubkey script." class="auto-link">P2SH address</a> is returned along with the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> which must be
|
||
provided when we spend <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> sent to the <a href="/en/glossary/p2sh-address" title="A Dash payment address comprising a hashed script, allowing the spender to create a standard pubkey script that Pays To Script Hash (P2SH). The script can be almost any valid pubkey script." class="auto-link">P2SH address</a>.</p>
|
||
|
||
<p><img src="/img/icons/icon_warning.svg" alt="Warning icon" />
|
||
<strong>Warning:</strong> You must not lose the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a>, especially if you
|
||
don’t have a record of which <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> you used to create the <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH
|
||
multisig</a> <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a>. You need the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> to spend any dash sent
|
||
to the <a href="/en/glossary/p2sh-address" title="A Dash payment address comprising a hashed script, allowing the spender to create a standard pubkey script that Pays To Script Hash (P2SH). The script can be almost any valid pubkey script." class="auto-link">P2SH address</a>. If you lose the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a>, you can recreate it
|
||
by running the same command above, with the <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> listed in the
|
||
same order. However, if you lose both the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> and even one of
|
||
the <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a>, you will never be able to spend <a href="/en/glossary/denominations" title="Denominations of Dash value, usually measured in fractions of a dash but sometimes measured in multiples of a duff. One dash equals 100,000,000 duffs." class="auto-link">duffs</a> sent to that
|
||
<a href="/en/glossary/p2sh-address" title="A Dash payment address comprising a hashed script, allowing the spender to create a standard pubkey script that Pays To Script Hash (P2SH). The script can be almost any valid pubkey script." class="auto-link">P2SH address</a>.</p>
|
||
|
||
<p>Neither the <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> nor the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> are stored in the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a> when
|
||
you use <a href="/en/developer-reference#createmultisig" class="auto-link"><code class="highlighter-rouge">createmultisig</code></a>. To store them in the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>, use the
|
||
<a href="/en/developer-reference#addmultisigaddress" class="auto-link"><code class="highlighter-rouge">addmultisigaddress</code> RPC</a> instead. If you add an <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> to the <a href="/en/glossary/wallet" title="Software that stores private keys and monitors the block chain (sometimes as a client of a server that does the processing) to allow users to spend and receive duffs." class="auto-link">wallet</a>,
|
||
you should also make a new backup.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendtoaddress <span class="nv">$P2SH_ADDRESS</span> 10.00
|
||
ddb2a2eb2402a9ae61d7db93a9a48c0747859d899e704b10f5b72145779f9c52
|
||
|
||
<span class="gp">> </span><span class="nv">UTXO_TXID</span><span class="o">=</span>ddb2a2eb2402a9ae61d7db93a9a48c0747859d899e704b10f5b7[...]</code></pre></figure>
|
||
|
||
<p>Paying the <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH multisig</a> <a href="/en/glossary/address" title="A 20-byte hash formatted using base58check to produce either a P2PKH or P2SH Dash address. Currently the most common way users exchange payment information." class="auto-link">address</a> with Dash Core is as simple as
|
||
paying a more common <a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH address</a>. Here we use the same command (but
|
||
different variable) we used in the Simple Spending subsection. As
|
||
before, this command automatically selects an <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a>, creates a <a href="/en/glossary/change-address" title="An output in a transaction which returns duffs to the spender, thus preventing too much of the input value from going to transaction fees." class="auto-link">change
|
||
output</a> to a new one of our <a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH addresses</a> if necessary, and pays a
|
||
<a href="/en/glossary/transaction-fee" title="The amount remaining when the value of all outputs in a transaction are subtracted from all inputs in a transaction; the fee is paid to the miner who includes that transaction in a block." class="auto-link">transaction fee</a> if necessary.</p>
|
||
|
||
<p>We save that <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> to a shell variable as the <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">txid</a> of the <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> we plan to spend next.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getrawtransaction <span class="nv">$UTXO_TXID</span> 1</code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"010000000130d100f7762956100a2396403c60e13e7a13520167acc6d38978ec\
|
||
33ada44cf8010000006b48304502210084effe3132550e6ba43a7f4cc54ad30d\
|
||
001c0dbc3ea66d638e5f3d6039a28c2b022044c8cd89cf455b8650fe259306eb\
|
||
2a30b0112969717e469a722bca0263e0975d01210324c2226564b19f0948306b\
|
||
b7160a735c28001bbd046cd46059df9f8434f41254feffffff0200ca9a3b0000\
|
||
000017a9144f334f26e350c8903c92ff25b733670902cfad5a8700e0052d0b00\
|
||
00001976a91479165c2155b8fec5c702ec7f251d0982f27b402988ac67000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ddb2a2eb2402a9ae61d7db93a9a48c0747859d899e704b10f5b72145779f9c52"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"size"</span><span class="p">:</span><span class="w"> </span><span class="mi">224</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"version"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"locktime"</span><span class="p">:</span><span class="w"> </span><span class="mi">103</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vin"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"f84ca4ad33ec7889d3c6ac670152137a3ee1603c4096230a10562976f70\
|
||
0d130"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"304502210084effe3132550e6ba43a7f4cc54ad30d001c0dbc3ea66d63\
|
||
8e5f3d6039a28c2b022044c8cd89cf455b8650fe259306eb2a30b01129\
|
||
69717e469a722bca0263e0975d[ALL] 0324c2226564b19f0948306bb7\
|
||
160a735c28001bbd046cd46059df9f8434f41254"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"48304502210084effe3132550e6ba43a7f4cc54ad30d001c0dbc3ea66d\
|
||
638e5f3d6039a28c2b022044c8cd89cf455b8650fe259306eb2a30b011\
|
||
2969717e469a722bca0263e0975d01210324c2226564b19f0948306bb7\
|
||
160a735c28001bbd046cd46059df9f8434f41254"</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967294</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">],</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"value"</span><span class="p">:</span><span class="w"> </span><span class="mf">10.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"valueSat"</span><span class="p">:</span><span class="w"> </span><span class="mi">1000000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"n"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OP_HASH160 4f334f26e350c8903c92ff25b733670902cfad5a OP_EQUAL"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"a9144f334f26e350c8903c92ff25b733670902cfad5a87"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"reqSigs"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"scripthash"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"addresses"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"8meEZF54K7GxhHhdLCCeNwFQjHENv4CK86"</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">},</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"value"</span><span class="p">:</span><span class="w"> </span><span class="mf">480.00000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"valueSat"</span><span class="p">:</span><span class="w"> </span><span class="mi">48000000000</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"n"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptPubKey"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"asm"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OP_DUP OP_HASH160 79165c2155b8fec5c702ec7f251d0982f27b4029\
|
||
OP_EQUALVERIFY OP_CHECKSIG"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"76a91479165c2155b8fec5c702ec7f251d0982f27b402988ac"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"reqSigs"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"pubkeyhash"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"addresses"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="s2">"yXMhQ1L5q3PcnJgEhyAFztQPPRaEr8Mh8s"</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">UTXO_VOUT</span><span class="o">=</span>0
|
||
<span class="gp">> </span><span class="nv">UTXO_OUTPUT_SCRIPT</span><span class="o">=</span>a9144f334f26e350c8903c92ff25b733670902cfad5a87</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>We use the <a href="/en/developer-reference#getrawtransaction" class="auto-link"><code class="highlighter-rouge">getrawtransaction</code> RPC</a> with the optional second argument
|
||
(<em>true</em>) to get the decoded transaction we just created with
|
||
<a href="/en/developer-reference#sendtoaddress" class="auto-link"><code class="highlighter-rouge">sendtoaddress</code></a>. We choose one of the <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">outputs</a> to be our <a href="/en/glossary/unspent-transaction-output" title="An Unspent Transaction Output (UTXO) that can be spent as an input in a new transaction." class="auto-link">UTXO</a> and get
|
||
its <a href="/en/developer-guide#term-output-index" title="The sequentially-numbered index of outputs in a single transaction starting from 0" class="auto-link">output index</a> number (vout) and <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a> (<a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">scriptPubKey</a>).</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest getnewaddress
|
||
yZSxAakpoWGG3vcsvpk9qNtsYREhump4Cr
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS4</span><span class="o">=</span>yZSxAakpoWGG3vcsvpk9qNtsYREhump4Cr</code></pre></figure>
|
||
|
||
<p>We generate a new <a href="/en/glossary/p2pkh-address" title="A Dash payment address comprising a hashed public key, allowing the spender to create a standard pubkey script that Pays To PubKey Hash (P2PKH)." class="auto-link">P2PKH address</a> to use in the <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> we’re about to
|
||
create.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">## Outputs - inputs = transaction fee, so always double-check your math!</span>
|
||
<span class="gp">> </span>dash-cli -regtest createrawtransaction <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">'
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
{
|
||
"'</span><span class="nv">$NEW_ADDRESS4</span><span class="s1">'": 9.998
|
||
}'''</span>
|
||
|
||
0100000001529c9f774521b7f5104b709e899d8547078ca4a993dbd761aea902<span class="se">\</span>
|
||
24eba2b2dd0000000000ffffffff01c0bc973b000000001976a914900504f96c<span class="se">\</span>
|
||
55d6ebe1c33581ba9430ca05b12a1488ac00000000
|
||
|
||
010000000175e1769813db8418fea17576694af1ff31cb2b512b7333e6eb42f0<span class="se">\</span>
|
||
30d0d778720000000000ffffffff01c0bc973b000000001976a914b6f64f5bf3<span class="se">\</span>
|
||
e38f25ead28817df7929c06fe847ee88ac00000000
|
||
|
||
<span class="gp">> </span><span class="nv">RAW_TX</span><span class="o">=</span>0100000001529c9f774521b7f5104b709e899d8547078ca4a993dbd[...]</code></pre></figure>
|
||
|
||
<p>We generate the <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">raw transaction</a> the same way we did in the Simple <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">Raw
|
||
Transaction</a> subsection.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest dumpprivkey <span class="nv">$NEW_ADDRESS1</span>
|
||
cThhxbQUtBDzHZbZrW6XAR4XkXfaQf4Abo7BQaTK2zVp7sVrHdmv
|
||
<span class="gp">> </span>dash-cli -regtest dumpprivkey <span class="nv">$NEW_ADDRESS3</span>
|
||
cUbYymPeHhRszTn64Xg7dzYKez8YC83M39ZTPJDiBDu8dRD3EjzF
|
||
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS1_PRIVATE_KEY</span><span class="o">=</span>cThhxbQUtBDzHZbZrW6XAR4XkXfaQf4Abo7BQ[...]
|
||
<span class="gp">> </span><span class="nv">NEW_ADDRESS3_PRIVATE_KEY</span><span class="o">=</span>cUbYymPeHhRszTn64Xg7dzYKez8YC83M39ZTP[...]</code></pre></figure>
|
||
|
||
<p>We get the <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> for two of the <a href="/en/glossary/public-key" title="The public portion of a keypair which can be used to verify signatures made with the private portion of the keypair." class="auto-link">public keys</a> we used to create the
|
||
transaction, the same way we got <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> in the Complex <a href="/en/glossary/serialized-transaction" title="Complete transactions in their binary format; often represented using hexadecimal. Sometimes called raw format because of the various Dash Core commands with "raw" in their names." class="auto-link">Raw
|
||
Transaction</a> subsection. Recall that we created a 2-of-3 <a href="/en/glossary/multisig" title="A pubkey script that provides *n* number of pubkeys and requires the corresponding signature script provide *m* minimum number signatures corresponding to the provided pubkeys." class="auto-link">multisig</a> <a href="/en/glossary/pubkey-script" title="A script included in outputs which sets the conditions that must be fulfilled for those duffs to be spent. Data for fulfilling the conditions can be provided in a signature script. Pubkey Scripts are called a scriptPubKey in code." class="auto-link">pubkey script</a>,
|
||
so <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a> from two <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> are needed.</p>
|
||
|
||
<p><img src="/img/icons/icon_warning.svg" alt="Warning icon" />
|
||
<strong>Reminder:</strong> Users should never manually manage <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private keys</a> on
|
||
<a href="/en/glossary/mainnet" title="The original and main network for Dash transactions, where duffs have real economic value." class="auto-link">mainnet</a>. See the warning in the <a href="/en/developer-examples#complex-raw-transaction">complex raw transaction section</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$RAW_TX</span> <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">',
|
||
"scriptPubKey": "'</span><span class="nv">$UTXO_OUTPUT_SCRIPT</span><span class="s1">'",
|
||
"redeemScript": "'</span><span class="nv">$P2SH_REDEEM_SCRIPT</span><span class="s1">'"
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
[
|
||
"'</span><span class="nv">$NEW_ADDRESS1_PRIVATE_KEY</span><span class="s1">'"
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000001529c9f774521b7f5104b709e899d8547078ca4a993dbd761aea902\
|
||
24eba2b2dd00000000b40047304402201cc50eac6d2db04dabd8ccd68b3116c0\
|
||
a8d37e7e41335e0d0ab441a5aa08cdcd02204011d184dca2489758c05e01556f\
|
||
f2ff9c48c39ff434fdfb1d9e0284fbde7701014c69522103fa8866cccae3c975\
|
||
a72884443a351801a0ea9721cbe7215586ddd6fab5f39f262103b2259f42a241\
|
||
f4870e794521594f2af7aadf0e4c580a43582e58630e4618634621038007ef6f\
|
||
d812d73da054271b68a42dae06672cff2a30b2814935537e5930ebf653aeffff\
|
||
ffff01c0bc973b000000001976a914900504f96c55d6ebe1c33581ba9430ca05\
|
||
b12a1488ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"errors"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
|
||
</span><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"txid"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ddb2a2eb2402a9ae61d7db93a9a48c0747859d899e704b10f5b72145779\
|
||
f9c52"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"vout"</span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"scriptSig"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0047304402201cc50eac6d2db04dabd8ccd68b3116c0a8d37e7e41\
|
||
335e0d0ab441a5aa08cdcd02204011d184dca2489758c05e01556f\
|
||
f2ff9c48c39ff434fdfb1d9e0284fbde7701014c69522103fa8866\
|
||
cccae3c975a72884443a351801a0ea9721cbe7215586ddd6fab5f3\
|
||
9f262103b2259f42a241f4870e794521594f2af7aadf0e4c580a43\
|
||
582e58630e4618634621038007ef6fd812d73da054271b68a42dae\
|
||
06672cff2a30b2814935537e5930ebf653ae"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"sequence"</span><span class="p">:</span><span class="w"> </span><span class="mi">4294967295</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"error"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Operation not valid with the current stack size"</span><span class="w">
|
||
</span><span class="p">}</span><span class="w">
|
||
</span><span class="p">]</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">PARTLY_SIGNED_RAW_TX</span><span class="o">=</span>010000000175e1769813db8418fea17576694af1f[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>We make the first <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signature</a>. The <a href="/en/glossary/input" title="An input in a transaction which contains three fields: an outpoint, a signature script, and a sequence number. The outpoint references a previous output and the signature script allows spending it." class="auto-link">input</a> argument (JSON object) takes the
|
||
additional <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a> parameter so that it can append the <a href="/en/glossary/redeem-script" title="A script similar in function to a pubkey script. One copy of it is hashed to create a P2SH address (used in an actual pubkey script) and another copy is placed in the spending signature script to enforce its conditions." class="auto-link">redeem script</a>
|
||
to the <a href="/en/glossary/signature-script" title="Data generated by a spender which is almost always used as variables to satisfy a pubkey script. Signature Scripts are called scriptSig in code." class="auto-link">signature script</a> after the two <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a>.</p>
|
||
|
||
<div class="multicode">
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest signrawtransaction <span class="nv">$PARTLY_SIGNED_RAW_TX</span> <span class="s1">'''
|
||
[
|
||
{
|
||
"txid": "'</span><span class="nv">$UTXO_TXID</span><span class="s1">'",
|
||
"vout": '</span><span class="nv">$UTXO_VOUT</span><span class="s1">',
|
||
"scriptPubKey": "'</span><span class="nv">$UTXO_OUTPUT_SCRIPT</span><span class="s1">'",
|
||
"redeemScript": "'</span><span class="nv">$P2SH_REDEEM_SCRIPT</span><span class="s1">'"
|
||
}
|
||
]
|
||
'''</span> <span class="s1">'''
|
||
[
|
||
"'</span><span class="nv">$NEW_ADDRESS3_PRIVATE_KEY</span><span class="s1">'"
|
||
]'''</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-json" data-lang="json"><span class="p">{</span><span class="w">
|
||
</span><span class="nt">"hex"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0100000001529c9f774521b7f5104b709e899d8547078ca4a993dbd761aea902\
|
||
24eba2b2dd00000000fdfd000047304402201cc50eac6d2db04dabd8ccd68b31\
|
||
16c0a8d37e7e41335e0d0ab441a5aa08cdcd02204011d184dca2489758c05e01\
|
||
556ff2ff9c48c39ff434fdfb1d9e0284fbde770101483045022100e0e1f95f1a\
|
||
b85814ee0920d5bd28c6831086e838af4bec344fd8654a0b58525f022075989f\
|
||
d3a677e1522aa85d45c41720aec9e7c127acadb6c14338c3b1a768ab28014c69\
|
||
522103fa8866cccae3c975a72884443a351801a0ea9721cbe7215586ddd6fab5\
|
||
f39f262103b2259f42a241f4870e794521594f2af7aadf0e4c580a43582e5863\
|
||
0e4618634621038007ef6fd812d73da054271b68a42dae06672cff2a30b28149\
|
||
35537e5930ebf653aeffffffff01c0bc973b000000001976a914900504f96c55\
|
||
d6ebe1c33581ba9430ca05b12a1488ac00000000"</span><span class="p">,</span><span class="w">
|
||
</span><span class="nt">"complete"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
|
||
</span><span class="p">}</span></code></pre></figure>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span><span class="nv">SIGNED_RAW_TX</span><span class="o">=</span>0100000001529c9f774521b7f5104b709e899d8547078ca4[...]</code></pre></figure>
|
||
|
||
</div>
|
||
|
||
<p>The <a href="/en/developer-reference#signrawtransaction" class="auto-link"><code class="highlighter-rouge">signrawtransaction</code></a> call used here is nearly identical to the one
|
||
used above. The only difference is the <a href="/en/glossary/private-key" title="The private portion of a keypair which can create signatures that other people can verify using the public key." class="auto-link">private key</a> used. Now that the
|
||
two required <a href="/en/glossary/signature" title="A value related to a public key which could only have reasonably been created by someone who has the private key that created that public key. Used in Dash to authorize spending duffs previously sent to a public key." class="auto-link">signatures</a> have been provided, the transaction is marked as
|
||
complete.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="gp">> </span>dash-cli -regtest sendrawtransaction <span class="nv">$SIGNED_RAW_TX</span>
|
||
483061b32894aacf6c4050291252a480c2a4c869eb85bd45082fb87d6b175ae8</code></pre></figure>
|
||
|
||
<p>We send the transaction spending the <a href="/en/glossary/p2sh-multisig" title="A P2SH output where the redeem script uses one of the multisig opcodes. Up until Bitcoin Core 0.10.0, P2SH multisig scripts were standard transactions, but most other P2SH scripts were not." class="auto-link">P2SH multisig output</a> to the local
|
||
<a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>, which accepts it.</p>
|
||
|
||
<!-- __ -->
|
||
|
||
<h2 id="p2p-network">P2P Network</h2>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_p2p_networking.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_p2p_networking.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_p2p_networking.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_p2p_networking.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<h3 id="creating-a-bloom-filter">Creating A Bloom Filter</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_p2p_networking.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_p2p_networking.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_p2p_networking.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_p2p_networking.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>In this section, we’ll use variable names that correspond to the field
|
||
names in the <a href="/en/developer-reference#filterclear" title="A P2P protocol message used to send a filter to a remote peer, requesting that they only send transactions which match the filter."><code class="highlighter-rouge">filterload</code> message documentation</a>.
|
||
Each code <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a> precedes the paragraph describing it.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c">#!/usr/bin/env python</span>
|
||
|
||
<span class="n">BYTES_MAX</span> <span class="o">=</span> <span class="mi">36000</span>
|
||
<span class="n">FUNCS_MAX</span> <span class="o">=</span> <span class="mi">50</span>
|
||
|
||
<span class="n">nFlags</span> <span class="o">=</span> <span class="mi">0</span></code></pre></figure>
|
||
|
||
<p>We start by setting some maximum values defined in <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" class="auto-link">BIP37</a>: the maximum
|
||
number of bytes allowed in a filter and the maximum number of hash
|
||
functions used to hash each piece of data. We also set nFlags to zero,
|
||
indicating we don’t want the remote <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> to update the filter for us.
|
||
(We won’t use nFlags again in the sample program, but real programs will
|
||
need to use it.)</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">n</span> <span class="o">=</span> <span class="mi">1</span>
|
||
<span class="n">p</span> <span class="o">=</span> <span class="mf">0.0001</span></code></pre></figure>
|
||
|
||
<p>We define the number (n) of elements we plan to insert into the filter
|
||
and the false positive rate (p) we want to help protect our privacy. For
|
||
this example, we will set <em>n</em> to one element and <em>p</em> to a rate of
|
||
1-in-10,000 to produce a small and precise filter for illustration
|
||
purposes. In actual use, your filters will probably be much larger.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">log</span>
|
||
<span class="n">nFilterBytes</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">min</span><span class="p">((</span><span class="o">-</span><span class="mi">1</span> <span class="o">/</span> <span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="n">log</span><span class="p">(</span><span class="n">p</span><span class="p">))</span> <span class="o">/</span> <span class="mi">8</span><span class="p">,</span> <span class="n">BYTES_MAX</span><span class="p">))</span>
|
||
<span class="n">nHashFuncs</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">nFilterBytes</span> <span class="o">*</span> <span class="mi">8</span> <span class="o">/</span> <span class="n">n</span> <span class="o">*</span> <span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">FUNCS_MAX</span><span class="p">))</span>
|
||
|
||
<span class="kn">from</span> <span class="nn">bitarray</span> <span class="kn">import</span> <span class="n">bitarray</span> <span class="c"># from pypi.python.org/pypi/bitarray</span>
|
||
<span class="n">vData</span> <span class="o">=</span> <span class="n">nFilterBytes</span> <span class="o">*</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">bitarray</span><span class="p">(</span><span class="s">'0'</span><span class="p">,</span> <span class="n">endian</span><span class="o">=</span><span class="s">"little"</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>Using the formula described in <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" class="auto-link">BIP37</a>, we calculate the ideal size of the
|
||
filter (in bytes) and the ideal number of hash functions to use. Both
|
||
are truncated down to the nearest whole number and both are also
|
||
constrained to the maximum values we defined earlier. The results of
|
||
this particular fixed computation are 2 filter bytes and 11 hash
|
||
functions. We then use <em>nFilterBytes</em> to create a little-endian bit
|
||
array of the appropriate size.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">nTweak</span> <span class="o">=</span> <span class="mi">0</span></code></pre></figure>
|
||
|
||
<p>We also should choose a value for <em>nTweak</em>. In this case, we’ll simply
|
||
use zero.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">pyhash</span> <span class="c"># from https://github.com/flier/pyfasthash</span>
|
||
<span class="n">murmur3</span> <span class="o">=</span> <span class="n">pyhash</span><span class="o">.</span><span class="n">murmur3_32</span><span class="p">()</span>
|
||
|
||
<span class="k">def</span> <span class="nf">bloom_hash</span><span class="p">(</span><span class="n">nHashNum</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
|
||
<span class="n">seed</span> <span class="o">=</span> <span class="p">(</span><span class="n">nHashNum</span> <span class="o">*</span> <span class="mh">0xfba4c795</span> <span class="o">+</span> <span class="n">nTweak</span><span class="p">)</span> <span class="o">&</span> <span class="mh">0xffffffff</span>
|
||
<span class="k">return</span><span class="p">(</span> <span class="n">murmur3</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">nFilterBytes</span> <span class="o">*</span> <span class="mi">8</span><span class="p">)</span> <span class="p">)</span></code></pre></figure>
|
||
|
||
<p>We setup our hash function template using the formula and 0xfba4c795
|
||
constant set in <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" class="auto-link">BIP37</a>. Note that we limit the size of the seed to four
|
||
bytes and that we’re returning the result of the hash modulo the size of
|
||
the filter in bits.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">data_to_hash</span> <span class="o">=</span> <span class="s">"019f5b01d4195ecbc9398fbf3c3b1fa9"</span> \
|
||
<span class="o">+</span> <span class="s">"bb3183301d7a1fb3bd174fcfa40a2b65"</span>
|
||
<span class="n">data_to_hash</span> <span class="o">=</span> <span class="n">data_to_hash</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>For the data to add to the filter, we’re adding a <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a>. Note that the
|
||
<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> is in <a href="/en/glossary/internal-byte-order" title="The standard order in which hash digests are displayed as strings---the same format used in serialized blocks and transactions." class="auto-link">internal byte order</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">print</span> <span class="s">" Filter (As Bits)"</span>
|
||
<span class="k">print</span> <span class="s">"nHashNum nIndex Filter 0123456789abcdef"</span>
|
||
<span class="k">print</span> <span class="s">"~~~~~~~~ ~~~~~~ ~~~~~~ ~~~~~~~~~~~~~~~~"</span>
|
||
<span class="k">for</span> <span class="n">nHashNum</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nHashFuncs</span><span class="p">):</span>
|
||
<span class="n">nIndex</span> <span class="o">=</span> <span class="n">bloom_hash</span><span class="p">(</span><span class="n">nHashNum</span><span class="p">,</span> <span class="n">data_to_hash</span><span class="p">)</span>
|
||
|
||
<span class="c">## Set the bit at nIndex to 1</span>
|
||
<span class="n">vData</span><span class="p">[</span><span class="n">nIndex</span><span class="p">]</span> <span class="o">=</span> <span class="bp">True</span>
|
||
|
||
<span class="c">## Debug: print current state</span>
|
||
<span class="k">print</span> <span class="s">' {0:2} {1:2} {2} {3}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
|
||
<span class="n">nHashNum</span><span class="p">,</span>
|
||
<span class="nb">hex</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">nIndex</span><span class="p">)),</span>
|
||
<span class="n">vData</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">),</span>
|
||
<span class="n">vData</span><span class="o">.</span><span class="n">to01</span><span class="p">()</span>
|
||
<span class="p">)</span>
|
||
|
||
<span class="k">print</span>
|
||
<span class="k">print</span> <span class="s">"Bloom filter:"</span><span class="p">,</span> <span class="n">vData</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>Now we use the hash function template to run a slightly different hash
|
||
function for <em>nHashFuncs</em> times. The result of each function being run
|
||
on the transaction is used as an index number: the bit at that index is
|
||
set to 1. We can see this in the printed debugging <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a>:</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-text" data-lang="text"> Filter (As Bits)
|
||
nHashNum nIndex Filter 0123456789abcdef
|
||
~~~~~~~~ ~~~~~~ ~~~~~~ ~~~~~~~~~~~~~~~~
|
||
0 0x7 8000 0000000100000000
|
||
1 0x9 8002 0000000101000000
|
||
2 0xa 8006 0000000101100000
|
||
3 0x2 8406 0010000101100000
|
||
4 0xb 840e 0010000101110000
|
||
5 0x5 a40e 0010010101110000
|
||
6 0x0 a50e 1010010101110000
|
||
7 0x8 a50f 1010010111110000
|
||
8 0x5 a50f 1010010111110000
|
||
9 0x8 a50f 1010010111110000
|
||
10 0x4 b50f 1010110111110000
|
||
|
||
Bloom filter: b50f</code></pre></figure>
|
||
|
||
<p>Notice that in iterations 8 and 9, the filter did not change because the
|
||
corresponding bit was already set in a previous iteration (5 and 7,
|
||
respectively). This is a normal part of <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a> operation.</p>
|
||
|
||
<p>We only added one element to the filter above, but we could repeat the
|
||
process with additional elements and continue to add them to the same
|
||
filter. (To maintain the same false-positive rate, you would need a
|
||
larger filter size as computed earlier.)</p>
|
||
|
||
<p>Note: for a more optimized Python implementation with fewer external
|
||
dependencies, see <a href="https://github.com/petertodd/python-bitcoinlib">python-bitcoinlib’s</a> <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a>
|
||
module which is based directly on Bitcoin Core’s C++ implementation.</p>
|
||
|
||
<p>Using the <a href="/en/developer-reference#filterclear" title="A P2P protocol message used to send a filter to a remote peer, requesting that they only send transactions which match the filter." class="auto-link"><code class="highlighter-rouge">filterload</code> message</a> format, the complete filter created above
|
||
would be the binary form of the annotated hexdump shown below:</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-text" data-lang="text">02 ......... Filter bytes: 2
|
||
b50f ....... Filter: 1010 1101 1111 0000
|
||
0b000000 ... nHashFuncs: 11
|
||
00000000 ... nTweak: 0/none
|
||
00 ......... nFlags: BLOOM_UPDATE_NONE</code></pre></figure>
|
||
|
||
<h3 id="evaluating-a-bloom-filter">Evaluating A Bloom Filter</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_p2p_networking.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_p2p_networking.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_p2p_networking.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_p2p_networking.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>Using a <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a> to find matching data is nearly identical to
|
||
constructing a <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a>—except that at each step we check to see
|
||
if the calculated index bit is set in the existing filter.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">vData</span> <span class="o">=</span> <span class="n">bitarray</span><span class="p">(</span><span class="n">endian</span><span class="o">=</span><span class="s">'little'</span><span class="p">)</span>
|
||
<span class="n">vData</span><span class="o">.</span><span class="n">frombytes</span><span class="p">(</span><span class="s">"b50f"</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">))</span>
|
||
<span class="n">nHashFuncs</span> <span class="o">=</span> <span class="mi">11</span>
|
||
<span class="n">nTweak</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="n">nFlags</span> <span class="o">=</span> <span class="mi">0</span></code></pre></figure>
|
||
|
||
<p>Using the <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a> created above, we import its various parameters.
|
||
Note, as indicated in the section above, we won’t actually use <em>nFlags</em>
|
||
to update the filter.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">contains</span><span class="p">(</span><span class="n">nHashFuncs</span><span class="p">,</span> <span class="n">data_to_hash</span><span class="p">):</span>
|
||
<span class="k">for</span> <span class="n">nHashNum</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nHashFuncs</span><span class="p">):</span>
|
||
<span class="c">## bloom_hash as defined in previous section</span>
|
||
<span class="n">nIndex</span> <span class="o">=</span> <span class="n">bloom_hash</span><span class="p">(</span><span class="n">nHashNum</span><span class="p">,</span> <span class="n">data_to_hash</span><span class="p">)</span>
|
||
|
||
<span class="k">if</span> <span class="n">vData</span><span class="p">[</span><span class="n">nIndex</span><span class="p">]</span> <span class="o">!=</span> <span class="bp">True</span><span class="p">:</span>
|
||
<span class="k">print</span> <span class="s">"MATCH FAILURE: Index {0} not set in {1}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
|
||
<span class="nb">hex</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">nIndex</span><span class="p">)),</span>
|
||
<span class="n">vData</span><span class="o">.</span><span class="n">to01</span><span class="p">()</span>
|
||
<span class="p">)</span>
|
||
<span class="k">return</span> <span class="bp">False</span></code></pre></figure>
|
||
|
||
<p>We define a function to check an element against the provided filter.
|
||
When checking whether the filter might contain an element, we test to
|
||
see whether a particular bit in the filter is already set to 1 (if it
|
||
isn’t, the match fails).</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c">## Test 1: Same TXID as previously added to filter</span>
|
||
<span class="n">data_to_hash</span> <span class="o">=</span> <span class="s">"019f5b01d4195ecbc9398fbf3c3b1fa9"</span> \
|
||
<span class="o">+</span> <span class="s">"bb3183301d7a1fb3bd174fcfa40a2b65"</span>
|
||
<span class="n">data_to_hash</span> <span class="o">=</span> <span class="n">data_to_hash</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">)</span>
|
||
<span class="n">contains</span><span class="p">(</span><span class="n">nHashFuncs</span><span class="p">,</span> <span class="n">data_to_hash</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>Testing the filter against the data element we previously added, we get
|
||
no <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> (indicating a possible match). Recall that <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filters</a> have
|
||
a zero false negative rate—so they should always match the inserted
|
||
elements.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c">## Test 2: Arbitrary string</span>
|
||
<span class="n">data_to_hash</span> <span class="o">=</span> <span class="s">"1/10,000 chance this ASCII string will match"</span>
|
||
<span class="n">contains</span><span class="p">(</span><span class="n">nHashFuncs</span><span class="p">,</span> <span class="n">data_to_hash</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>Testing the filter against an arbitrary element, we get the failure
|
||
<a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> below. Note: we created the filter with a 1-in-10,000 false
|
||
positive rate (which was rounded up somewhat when we truncated), so it
|
||
was possible this arbitrary string would’ve matched the filter anyway.
|
||
It is not possible to set a <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a> to a false positive rate of
|
||
zero, so your program will always have to deal with false positives.
|
||
The <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a> below shows us that one of the hash functions returned an
|
||
index number of 0x06, but that bit wasn’t set in the filter, causing the
|
||
match failure:</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-text" data-lang="text">MATCH FAILURE: Index 0x6 not set in 1010110111110000</code></pre></figure>
|
||
|
||
<h3 id="bloom-filter-script">Bloom Filter Script</h3>
|
||
<!-- no subhead-links here -->
|
||
|
||
<!-- Shell script to run previous examples -->
|
||
<p>Complete Python script demonstrating the
|
||
<a href="#creating-a-bloom-filter">Creating</a>/
|
||
<a href="#evaluating-a-bloom-filter">Evaluating</a> bloom filter examples:</p>
|
||
|
||
<div class="multicode">
|
||
<script src="https://gist.github.com/dash-docs/5d63f095352a717f90d41a5e7fbbdac1.js"></script>
|
||
|
||
</div>
|
||
|
||
<h3 id="retrieving-a-merkleblock">Retrieving A MerkleBlock</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_p2p_networking.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_p2p_networking.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_p2p_networking.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_p2p_networking.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>For the <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs" class="auto-link"><code class="highlighter-rouge">merkleblock</code> message</a> documentation on the reference page, an
|
||
actual <a href="/en/glossary/merkle-block" title="A partial merkle tree connecting transactions matching a bloom filter to the merkle root of a block." class="auto-link">merkle block</a> was retrieved from the <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> and manually
|
||
processed. This section walks through each step of the process,
|
||
demonstrating basic <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> communication and <a href="/en/glossary/merkle-block" title="A partial merkle tree connecting transactions matching a bloom filter to the merkle root of a block." class="auto-link">merkle block</a> processing.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c">#!/usr/bin/env python</span>
|
||
|
||
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">sleep</span>
|
||
<span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">sha256</span>
|
||
<span class="kn">import</span> <span class="nn">struct</span>
|
||
<span class="kn">import</span> <span class="nn">sys</span>
|
||
|
||
<span class="n">network_string</span> <span class="o">=</span> <span class="s">"f9beb4d9"</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">)</span> <span class="c"># Mainnet</span>
|
||
|
||
<span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span><span class="n">payload</span><span class="p">):</span>
|
||
<span class="c">## Command is ASCII text, null padded to 12 bytes</span>
|
||
<span class="n">command</span> <span class="o">=</span> <span class="n">msg</span> <span class="o">+</span> <span class="p">(</span> <span class="p">(</span> <span class="mi">12</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="p">)</span> <span class="o">*</span> <span class="s">"</span><span class="se">\00</span><span class="s">"</span> <span class="p">)</span>
|
||
|
||
<span class="c">## Payload length is a uint32_t</span>
|
||
<span class="n">payload_raw</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">"hex"</span><span class="p">)</span>
|
||
<span class="n">payload_len</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s">"I"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">payload_raw</span><span class="p">))</span>
|
||
|
||
<span class="c">## Checksum is first 4 bytes of SHA256(SHA256(<payload>))</span>
|
||
<span class="n">checksum</span> <span class="o">=</span> <span class="n">sha256</span><span class="p">(</span><span class="n">sha256</span><span class="p">(</span><span class="n">payload_raw</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">())</span><span class="o">.</span><span class="n">digest</span><span class="p">()[:</span><span class="mi">4</span><span class="p">]</span>
|
||
|
||
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
|
||
<span class="n">network_string</span>
|
||
<span class="o">+</span> <span class="n">command</span>
|
||
<span class="o">+</span> <span class="n">payload_len</span>
|
||
<span class="o">+</span> <span class="n">checksum</span>
|
||
<span class="o">+</span> <span class="n">payload_raw</span>
|
||
<span class="p">)</span>
|
||
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span></code></pre></figure>
|
||
|
||
<p>To connect to the P2P <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a>, the trivial Python function above was
|
||
developed to compute <a href="/en/glossary/message-header" title="The four header fields prefixed to all messages on the Dash P2P network." class="auto-link">message headers</a> and send payloads decoded from hex.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="c">## Create a version message</span>
|
||
<span class="n">send</span><span class="p">(</span><span class="s">"version"</span><span class="p">,</span>
|
||
<span class="s">"71110100"</span> <span class="c"># ........................ Protocol Version: 70001</span>
|
||
<span class="o">+</span> <span class="s">"0000000000000000"</span> <span class="c"># ................ Services: Headers Only (SPV)</span>
|
||
<span class="o">+</span> <span class="s">"c6925e5400000000"</span> <span class="c"># ................ Time: 1415484102</span>
|
||
<span class="o">+</span> <span class="s">"00000000000000000000000000000000"</span>
|
||
<span class="o">+</span> <span class="s">"0000ffff7f000001208d"</span> <span class="c"># ............ Receiver IP Address/Port</span>
|
||
<span class="o">+</span> <span class="s">"00000000000000000000000000000000"</span>
|
||
<span class="o">+</span> <span class="s">"0000ffff7f000001208d"</span> <span class="c"># ............ Sender IP Address/Port</span>
|
||
<span class="o">+</span> <span class="s">"0000000000000000"</span> <span class="c"># ................ Nonce (not used here)</span>
|
||
<span class="o">+</span> <span class="s">"1b"</span> <span class="c"># .............................. Bytes in version string</span>
|
||
<span class="o">+</span> <span class="s">"2f426974636f696e2e6f726720457861"</span>
|
||
<span class="o">+</span> <span class="s">"6d706c653a302e392e332f"</span> <span class="c"># .......... Version string</span>
|
||
<span class="o">+</span> <span class="s">"93050500"</span> <span class="c"># ........................ Starting block height: 329107</span>
|
||
<span class="o">+</span> <span class="s">"00"</span> <span class="c"># .............................. Relay transactions: false</span>
|
||
<span class="p">)</span></code></pre></figure>
|
||
|
||
<p><a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">Peers</a> on the <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a> will not accept any requests until you send them a
|
||
<a href="/en/developer-reference#version" title="A P2P network message sent at the begining of a connection to allow protocol version negotiation" class="auto-link"><code class="highlighter-rouge">version</code> message</a>. The receiving <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> will reply with their <a href="/en/developer-reference#version" title="A P2P network message sent at the begining of a connection to allow protocol version negotiation" class="auto-link"><code class="highlighter-rouge">version</code>
|
||
message</a> and a <a href="/en/developer-reference#verack" title="A P2P network message sent in reply to a version message to confirm a connection has been established" class="auto-link"><code class="highlighter-rouge">verack</code> message</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="n">send</span><span class="p">(</span><span class="s">"verack"</span><span class="p">,</span> <span class="s">""</span><span class="p">)</span></code></pre></figure>
|
||
|
||
<p>We’re not going to validate their <a href="/en/developer-reference#version" title="A P2P network message sent at the begining of a connection to allow protocol version negotiation" class="auto-link"><code class="highlighter-rouge">version</code> message</a> with this simple
|
||
script, but we will sleep a short bit and send back our own <a href="/en/developer-reference#verack" title="A P2P network message sent in reply to a version message to confirm a connection has been established" class="auto-link"><code class="highlighter-rouge">verack</code>
|
||
message</a> as if we had accepted their <a href="/en/developer-reference#version" title="A P2P network message sent at the begining of a connection to allow protocol version negotiation" class="auto-link"><code class="highlighter-rouge">version</code> message</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">send</span><span class="p">(</span><span class="s">"filterload"</span><span class="p">,</span>
|
||
<span class="s">"02"</span> <span class="c"># ........ Filter bytes: 2</span>
|
||
<span class="o">+</span> <span class="s">"b50f"</span> <span class="c"># ....... Filter: 1010 1101 1111 0000</span>
|
||
<span class="o">+</span> <span class="s">"0b000000"</span> <span class="c"># ... nHashFuncs: 11</span>
|
||
<span class="o">+</span> <span class="s">"00000000"</span> <span class="c"># ... nTweak: 0/none</span>
|
||
<span class="o">+</span> <span class="s">"00"</span> <span class="c"># ......... nFlags: BLOOM_UPDATE_NONE</span>
|
||
<span class="p">)</span></code></pre></figure>
|
||
|
||
<p>We set a <a href="/en/glossary/bloom-filter" title="A filter used primarily by SPV clients to request only matching transactions and merkle blocks from full nodes." class="auto-link">bloom filter</a> with the <a href="/en/developer-reference#filterclear" title="A P2P protocol message used to send a filter to a remote peer, requesting that they only send transactions which match the filter." class="auto-link"><code class="highlighter-rouge">filterload</code> message</a>. This filter is
|
||
described in the two preceding sections.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">send</span><span class="p">(</span><span class="s">"getdata"</span><span class="p">,</span>
|
||
<span class="s">"01"</span> <span class="c"># ................................. Number of inventories: 1</span>
|
||
<span class="o">+</span> <span class="s">"03000000"</span> <span class="c"># ........................... Inventory type: filtered block</span>
|
||
<span class="o">+</span> <span class="s">"a4deb66c0d726b0aefb03ed51be407fb"</span>
|
||
<span class="o">+</span> <span class="s">"ad7331c6e8f9eef231b7000000000000"</span> <span class="c"># ... Block header hash</span>
|
||
<span class="p">)</span></code></pre></figure>
|
||
|
||
<p>We request a <a href="/en/glossary/merkle-block" title="A partial merkle tree connecting transactions matching a bloom filter to the merkle root of a block." class="auto-link">merkle block</a> for transactions matching our filter,
|
||
completing our script.</p>
|
||
|
||
<p>To run the script, we simply pipe it to the Unix <a href="https://en.wikipedia.org/wiki/Netcat"><code class="highlighter-rouge">netcat</code>
|
||
command</a> or one of its many clones, one of which is available
|
||
for practically any platform. For example, with the original netcat and
|
||
using hexdump (<code class="highlighter-rouge">hd</code>) to display the <a href="/en/glossary/output" title="An output in a transaction which contains two fields: a value field for transferring zero or more duffs and a pubkey script for indicating what conditions must be fulfilled for those duffs to be further spent." class="auto-link">output</a>:</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">## Connect to the Bitcoin Core peer running on localhost</span>
|
||
python get-merkle.py | nc localhost 8333 | hd</code></pre></figure>
|
||
|
||
<p>Part of the response is shown in the section below.</p>
|
||
|
||
<h3 id="parsing-a-merkleblock">Parsing A MerkleBlock</h3>
|
||
<div class="subhead-links sourcefile" data-sourcefile="_includes/devdoc/example_p2p_networking.md"><a href="https://github.com/dash-docs/dash-docs/edit/master/_includes/devdoc/example_p2p_networking.md">Edit</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/commits/master/_includes/devdoc/example_p2p_networking.md">History</a>
|
||
| <a href="https://github.com/dash-docs/dash-docs/issues/new?body=Source%20File%3A%20_includes/devdoc/example_p2p_networking.md%0A%0A">Report Issue</a>
|
||
| <a href="https://www.dash.org/forum/">Discuss</a></div>
|
||
|
||
<p>In the section above, we retrieved a <a href="/en/glossary/merkle-block" title="A partial merkle tree connecting transactions matching a bloom filter to the merkle root of a block." class="auto-link">merkle block</a> from the <a href="/en/developer-guide#term-network" title="The Dash P2P network which broadcasts transactions and blocks" class="auto-link">network</a>; now
|
||
we will parse it. Most of the <a href="/en/glossary/block-header" title="An 80-byte header belonging to a single block which is hashed repeatedly to create proof of work." class="auto-link">block header</a> has been omitted. For
|
||
a more complete hexdump, see the example in the <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs"><code class="highlighter-rouge">merkleblock</code> message
|
||
section</a>.</p>
|
||
|
||
<figure class="highlight"><pre><code class="language-text" data-lang="text">7f16c5962e8bd963659c793ce370d95f
|
||
093bc7e367117b3c30c1f8fdd0d97287 ... Merkle root
|
||
|
||
07000000 ........................... Transaction count: 7
|
||
04 ................................. Hash count: 4
|
||
|
||
3612262624047ee87660be1a707519a4
|
||
43b1c1ce3d248cbfc6c15870f6c5daa2 ... Hash #1
|
||
019f5b01d4195ecbc9398fbf3c3b1fa9
|
||
bb3183301d7a1fb3bd174fcfa40a2b65 ... Hash #2
|
||
41ed70551dd7e841883ab8f0b16bf041
|
||
76b7d1480e4f0af9f3d4c3595768d068 ... Hash #3
|
||
20d2a7bc994987302e5b1ac80fc425fe
|
||
25f8b63169ea78e68fbaaefa59379bbf ... Hash #4
|
||
|
||
01 ................................. Flag bytes: 1
|
||
1d ................................. Flags: 1 0 1 1 1 0 0 0</code></pre></figure>
|
||
|
||
<p>We parse the above <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs" class="auto-link"><code class="highlighter-rouge">merkleblock</code> message</a> using the following
|
||
instructions. Each illustration is described in the paragraph below it.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-001.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>We start by building the structure of a <a href="/en/glossary/merkle-tree" title="A tree constructed by hashing paired data (the leaves), then pairing and hashing the results until a single hash remains, the merkle root. In Dash, the leaves are almost always transactions from a single block." class="auto-link">merkle tree</a> based on the number
|
||
of transactions in the <a href="/en/glossary/block" title="One or more transactions prefaced by a block header and protected by proof of work. Blocks are the data stored on the block chain." class="auto-link">block</a>.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-002.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>The first flag is a 1 and the <a href="/en/glossary/merkle-root" title="The root node of a merkle tree, a descendant of all the hashed pairs in the tree. Block headers must include a valid merkle root descended from all transactions in that block." class="auto-link">merkle root</a> is (as always) a non-<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a>
|
||
<a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>, so we will need to compute the hash later based on this <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node’s</a>
|
||
children. Accordingly, we descend into the <a href="/en/glossary/merkle-root" title="The root node of a merkle tree, a descendant of all the hashed pairs in the tree. Block headers must include a valid merkle root descended from all transactions in that block." class="auto-link">merkle root’s</a> left child and
|
||
look at the next flag for instructions.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-003.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>The next flag in the example is a 0 and this is also a non-<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>, so
|
||
we apply the first hash from the <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs" class="auto-link"><code class="highlighter-rouge">merkleblock</code> message</a> to this <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>. We
|
||
also don’t process any child <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">nodes</a>—according to the <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">peer</a> which created
|
||
the <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs" class="auto-link"><code class="highlighter-rouge">merkleblock</code> message</a>, none of those <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">nodes</a> will lead to <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXIDs</a> of
|
||
transactions that match our filter, so we don’t need them. We go back up
|
||
to the <a href="/en/glossary/merkle-root" title="The root node of a merkle tree, a descendant of all the hashed pairs in the tree. Block headers must include a valid merkle root descended from all transactions in that block." class="auto-link">merkle root</a> and then descend into its right child and look at the
|
||
next (third) flag for instructions.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-004.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>The third flag in the example is another 1 on another non-<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>, so
|
||
we descend into its left child.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-005.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>The fourth flag is also a 1 on another non-<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>, so we descend
|
||
again—we will always continue descending until we reach a <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> or
|
||
a non-<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> with a 0 flag (or we finish filling out the tree).</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-006.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>Finally, on the fifth flag in the example (a 1), we reach a <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a> <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>.
|
||
The 1 flag indicates this <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID’s</a> transaction matches our filter and
|
||
that we should take the next (second) hash and use it as this <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node’s</a>
|
||
<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a>.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-007.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>The sixth flag also applies to a <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a>, but it’s a 0 flag, so this
|
||
<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID’s</a> transaction doesn’t match our filter; still, we take the next
|
||
(third) hash and use it as this <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node’s</a> <a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXID</a>.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-008.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>We now have enough information to compute the hash for the fourth <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a>
|
||
we encountered—it’s the hash of the concatenated hashes of the two
|
||
<a href="/en/glossary/txid" title="An identifier used to uniquely identify a particular transaction; specifically, the sha256d hash of the transaction." class="auto-link">TXIDs</a> we filled out.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-009.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>Moving to the right child of the third <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">node</a> we encountered, we fill it
|
||
out using the seventh flag and final hash—and discover there are no
|
||
more child <a href="/en/glossary/node" title="A computer that connects to the Dash network." class="auto-link">nodes</a> to process.</p>
|
||
|
||
<p><img src="/img/dev/gifs/en-merkleblock-parsing/en-merkleblock-parsing-011.svg" alt="Parsing A MerkleBlock" /></p>
|
||
|
||
<p>We hash as appropriate to fill out the tree. Note that the eighth flag is
|
||
not used—this is acceptable as it was required to pad out a flag byte.</p>
|
||
|
||
<p>The final steps would be to ensure the computed <a href="/en/glossary/merkle-root" title="The root node of a merkle tree, a descendant of all the hashed pairs in the tree. Block headers must include a valid merkle root descended from all transactions in that block." class="auto-link">merkle root</a>
|
||
is identical to the <a href="/en/glossary/merkle-root" title="The root node of a merkle tree, a descendant of all the hashed pairs in the tree. Block headers must include a valid merkle root descended from all transactions in that block." class="auto-link">merkle root</a> in the <a href="/en/glossary/block-header" title="An 80-byte header belonging to a single block which is hashed repeatedly to create proof of work." class="auto-link">header</a> and check the other steps
|
||
of the parsing checklist in the <a href="/en/developer-reference#merkleblock" title="A P2P protocol message used to request a filtered block useful for SPV proofs" class="auto-link"><code class="highlighter-rouge">merkleblock</code> message</a> section.</p>
|
||
|
||
<!-- Services like Blockcyper are more likely to be used by most than building a ground-up payment system
|
||
|
||
-->
|
||
|
||
<!-- Inventory Messages -->
|
||
<!-- Inventory Messages: Dash Specific -->
|
||
|
||
<!--[bitcoin-documentation mailing list]: https://groups.google.com/forum/#!forum/bitcoin-documentation-->
|
||
|
||
</div>
|
||
|
||
</div>
|
||
|
||
<div class="footer">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div class="footersponsor">
|
||
<!--<div><span>A community website sponsored by</span> <a href="https://bitcoinfoundation.org/"><img src="/img/brand/bitcoinfoundation.png" alt="Bitcoin Foundation"></a></div>-->
|
||
</div>
|
||
|
||
|
||
|
||
<div class="footerlicense">© Bitcoin Project 2009-2018 Released under the <a href="http://opensource.org/licenses/mit-license.php" target="_blank">MIT license</a>
|
||
<br>© Dash Project 2017-2018 Released under the <a href="http://opensource.org/licenses/mit-license.php" target="_blank">MIT license</a></div>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<script type="text/javascript">
|
||
fallbackSVG();
|
||
addAnchorLinks();
|
||
trackOutgoingLinks();
|
||
</script>
|
||
|
||
|
||
|
||
<script src="/js/jquery/jquery-1.11.2.min.js"></script>
|
||
<script src="/js/jquery/jquery-ui.min.js"></script>
|
||
<script src="/js/devsearch.js"></script>
|
||
<script>updateToc();</script>
|
||
|
||
|
||
|
||
</body>
|
||
|
||
</html>
|