mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-19 13:35:17 +01:00
parent
f254c39301
commit
c401f6ea89
14 changed files with 82 additions and 82 deletions
|
@ -80,7 +80,7 @@ reason to keep using legacy transaction formats.</p>
|
|||
<span class="hljs-keyword">val</span> privkey = <span class="hljs-type">ECPrivateKey</span>()
|
||||
<span class="hljs-comment">// privkey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> pubkey = privkey.publicKey
|
||||
<span class="hljs-comment">// pubkey: crypto.ECPublicKey = ECPublicKey(0398fbf21239401c38d1cd47a6c2e29e804f4a35f87978f1e4a5e01694122532a0)</span>
|
||||
<span class="hljs-comment">// pubkey: crypto.ECPublicKey = ECPublicKey(02f6baaf1b3a61931af2be8a8f527bfb2f9018c2d93c38cfb361a940cfc6df57a1)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> segwitAddress = {
|
||||
<span class="hljs-comment">// see https://bitcoin.org/en/glossary/pubkey-script</span>
|
||||
|
@ -89,10 +89,10 @@ reason to keep using legacy transaction formats.</p>
|
|||
<span class="hljs-keyword">val</span> scriptPubKey = <span class="hljs-type">P2WPKHWitnessSPKV0</span>(pubkey)
|
||||
<span class="hljs-type">Bech32Address</span>(scriptPubKey, <span class="hljs-type">TestNet3</span>)
|
||||
}
|
||||
<span class="hljs-comment">// segwitAddress: Bech32Address = Bech32Address(tb1q5vla9sz2sa7pad9u5seujfn2r3vnht7sxwef96)</span>
|
||||
<span class="hljs-comment">// segwitAddress: Bech32Address = Bech32Address(tb1qndl5ttezpndjynhrfxjceyktk6pdwnuw64zw2h)</span>
|
||||
|
||||
println(segwitAddress.toString)
|
||||
<span class="hljs-comment">// Bech32Address(tb1q5vla9sz2sa7pad9u5seujfn2r3vnht7sxwef96)</span>
|
||||
<span class="hljs-comment">// Bech32Address(tb1qndl5ttezpndjynhrfxjceyktk6pdwnuw64zw2h)</span>
|
||||
</code></pre>
|
||||
<h2><a class="anchor" aria-hidden="true" id="generating-legacy-base58-addresses"></a><a href="#generating-legacy-base58-addresses" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Generating legacy (base58) addresses</h2>
|
||||
<p>If you need to generate legacy addresses for backwards
|
||||
|
@ -101,10 +101,10 @@ Take a look:</p>
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-comment">// we're reusing the same private/public key pair</span>
|
||||
<span class="hljs-comment">// from before. don't do this in an actual application!</span>
|
||||
<span class="hljs-keyword">val</span> legacyAddress = <span class="hljs-type">P2PKHAddress</span>(pubkey, <span class="hljs-type">TestNet3</span>)
|
||||
<span class="hljs-comment">// legacyAddress: P2PKHAddress = mvQ8tqdegse5WMYbACPqrDfSMdQjnzzbCW</span>
|
||||
<span class="hljs-comment">// legacyAddress: P2PKHAddress = muh9VpFVNthX33zxogSKMxjdKEoaVdS94V</span>
|
||||
|
||||
println(legacyAddress.toString)
|
||||
<span class="hljs-comment">// mvQ8tqdegse5WMYbACPqrDfSMdQjnzzbCW</span>
|
||||
<span class="hljs-comment">// muh9VpFVNthX33zxogSKMxjdKEoaVdS94V</span>
|
||||
</code></pre>
|
||||
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/core/core-intro"><span class="arrow-prev">← </span><span>Core Module</span></a><a class="docs-next button" href="/docs/next/core/hd-keys"><span>HD Key Generation</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#generating-segwit-bech32-addresses">Generating SegWit (bech32) addresses</a></li><li><a href="#generating-legacy-base58-addresses">Generating legacy (base58) addresses</a></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/favicon.ico" alt="bitcoin-s" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/en/getting-started">Getting Started</a><a href="/docs/en/core/core-intro">Guides</a><a href="/api/org/bitcoins">API Reference</a></div><div><h5>Community</h5><a href="/en/users.html">User Showcase</a><a href="https://join.slack.com/t/suredbits/shared_invite/enQtNDEyMjY3MTg1MTg3LTYyYjkwOGUzMDQ4NDAwZjE1M2I3MmQyNWNlZjNlYjg4OGRjYTRjNWUwNjRjNjg4Y2NjZjAxYjU1N2JjMTU1YWM" target="_blank" rel="noreferrer noopener">Slack</a><a href="https://gitter.im/bitcoin-s-core/">Gitter chat</a></div><div><h5>More</h5><a href="https://github.com/bitcoin-s/bitcoin-s">GitHub</a><a class="github-button" href="https://github.com/bitcoin-s/bitcoin-s" data-icon="octicon-star" data-count-href="/bitcoin-s/bitcoin-s-core/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2020 Suredbits & the bitcoin-s developers</section></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script><script>
|
||||
document.addEventListener('keyup', function(e) {
|
||||
|
|
|
@ -80,7 +80,7 @@ reason to keep using legacy transaction formats.</p>
|
|||
<span class="hljs-keyword">val</span> privkey = <span class="hljs-type">ECPrivateKey</span>()
|
||||
<span class="hljs-comment">// privkey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> pubkey = privkey.publicKey
|
||||
<span class="hljs-comment">// pubkey: crypto.ECPublicKey = ECPublicKey(0398fbf21239401c38d1cd47a6c2e29e804f4a35f87978f1e4a5e01694122532a0)</span>
|
||||
<span class="hljs-comment">// pubkey: crypto.ECPublicKey = ECPublicKey(02f6baaf1b3a61931af2be8a8f527bfb2f9018c2d93c38cfb361a940cfc6df57a1)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> segwitAddress = {
|
||||
<span class="hljs-comment">// see https://bitcoin.org/en/glossary/pubkey-script</span>
|
||||
|
@ -89,10 +89,10 @@ reason to keep using legacy transaction formats.</p>
|
|||
<span class="hljs-keyword">val</span> scriptPubKey = <span class="hljs-type">P2WPKHWitnessSPKV0</span>(pubkey)
|
||||
<span class="hljs-type">Bech32Address</span>(scriptPubKey, <span class="hljs-type">TestNet3</span>)
|
||||
}
|
||||
<span class="hljs-comment">// segwitAddress: Bech32Address = Bech32Address(tb1q5vla9sz2sa7pad9u5seujfn2r3vnht7sxwef96)</span>
|
||||
<span class="hljs-comment">// segwitAddress: Bech32Address = Bech32Address(tb1qndl5ttezpndjynhrfxjceyktk6pdwnuw64zw2h)</span>
|
||||
|
||||
println(segwitAddress.toString)
|
||||
<span class="hljs-comment">// Bech32Address(tb1q5vla9sz2sa7pad9u5seujfn2r3vnht7sxwef96)</span>
|
||||
<span class="hljs-comment">// Bech32Address(tb1qndl5ttezpndjynhrfxjceyktk6pdwnuw64zw2h)</span>
|
||||
</code></pre>
|
||||
<h2><a class="anchor" aria-hidden="true" id="generating-legacy-base58-addresses"></a><a href="#generating-legacy-base58-addresses" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Generating legacy (base58) addresses</h2>
|
||||
<p>If you need to generate legacy addresses for backwards
|
||||
|
@ -101,10 +101,10 @@ Take a look:</p>
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-comment">// we're reusing the same private/public key pair</span>
|
||||
<span class="hljs-comment">// from before. don't do this in an actual application!</span>
|
||||
<span class="hljs-keyword">val</span> legacyAddress = <span class="hljs-type">P2PKHAddress</span>(pubkey, <span class="hljs-type">TestNet3</span>)
|
||||
<span class="hljs-comment">// legacyAddress: P2PKHAddress = mvQ8tqdegse5WMYbACPqrDfSMdQjnzzbCW</span>
|
||||
<span class="hljs-comment">// legacyAddress: P2PKHAddress = muh9VpFVNthX33zxogSKMxjdKEoaVdS94V</span>
|
||||
|
||||
println(legacyAddress.toString)
|
||||
<span class="hljs-comment">// mvQ8tqdegse5WMYbACPqrDfSMdQjnzzbCW</span>
|
||||
<span class="hljs-comment">// muh9VpFVNthX33zxogSKMxjdKEoaVdS94V</span>
|
||||
</code></pre>
|
||||
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/core/core-intro"><span class="arrow-prev">← </span><span>Core Module</span></a><a class="docs-next button" href="/docs/next/core/hd-keys"><span>HD Key Generation</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#generating-segwit-bech32-addresses">Generating SegWit (bech32) addresses</a></li><li><a href="#generating-legacy-base58-addresses">Generating legacy (base58) addresses</a></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/favicon.ico" alt="bitcoin-s" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/en/getting-started">Getting Started</a><a href="/docs/en/core/core-intro">Guides</a><a href="/api/org/bitcoins">API Reference</a></div><div><h5>Community</h5><a href="/en/users.html">User Showcase</a><a href="https://join.slack.com/t/suredbits/shared_invite/enQtNDEyMjY3MTg1MTg3LTYyYjkwOGUzMDQ4NDAwZjE1M2I3MmQyNWNlZjNlYjg4OGRjYTRjNWUwNjRjNjg4Y2NjZjAxYjU1N2JjMTU1YWM" target="_blank" rel="noreferrer noopener">Slack</a><a href="https://gitter.im/bitcoin-s-core/">Gitter chat</a></div><div><h5>More</h5><a href="https://github.com/bitcoin-s/bitcoin-s">GitHub</a><a class="github-button" href="https://github.com/bitcoin-s/bitcoin-s" data-icon="octicon-star" data-count-href="/bitcoin-s/bitcoin-s-core/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2020 Suredbits & the bitcoin-s developers</section></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script><script>
|
||||
document.addEventListener('keyup', function(e) {
|
||||
|
|
|
@ -86,13 +86,13 @@ use that to generate further private and public keys:</p>
|
|||
<span class="hljs-comment">// how long our phrase ends up being</span>
|
||||
<span class="hljs-comment">// 256 bits of entropy results in 24 words</span>
|
||||
<span class="hljs-keyword">val</span> entropy: <span class="hljs-type">BitVector</span> = <span class="hljs-type">MnemonicCode</span>.getEntropy256Bits
|
||||
<span class="hljs-comment">// entropy: BitVector = BitVector(256 bits, 0x5edce67e8c6420575fc8feca94c94f76e9e051057ab8bdf56c3ea90ba46e57fe)</span>
|
||||
<span class="hljs-comment">// entropy: BitVector = BitVector(256 bits, 0xa393c0bd5ebdcb69b83e9c714b6ddaceb8603a365328d12214e15da0cb832192)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mnemonicCode = <span class="hljs-type">MnemonicCode</span>.fromEntropy(entropy)
|
||||
<span class="hljs-comment">// mnemonicCode: MnemonicCode = Masked(MnemonicCodeImpl)</span>
|
||||
|
||||
mnemonicCode.words <span class="hljs-comment">// the phrase the user should write down</span>
|
||||
<span class="hljs-comment">// res0: Vector[String] = Vector(gain, transfer, panic, boat, dragon, close, left, cabin, skin, play, fatigue, universe, own, chuckle, bicycle, finish, sadness, problem, autumn, faith, ripple, damp, garment, sponsor) // the phrase the user should write down</span>
|
||||
<span class="hljs-comment">// res0: Vector[String] = Vector(phone, own, consider, runway, syrup, regular, thought, squeeze, image, fork, issue, output, maid, brown, sun, govern, spell, awful, debate, robust, arrow, screen, main, elbow) // the phrase the user should write down</span>
|
||||
|
||||
<span class="hljs-comment">// the password argument is an optional, extra security</span>
|
||||
<span class="hljs-comment">// measure. all MnemonicCode instances will give you a</span>
|
||||
|
@ -108,7 +108,7 @@ mnemonicCode.words <span class="hljs-comment">// the phrase the user should writ
|
|||
bip39Seed)
|
||||
<span class="hljs-comment">// xpriv: ExtPrivateKey = Masked(ExtPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> xpub = xpriv.extPublicKey
|
||||
<span class="hljs-comment">// xpub: ExtPublicKey = zpub6jftahH18ngZyCztNYkKnn6ZMbYQoBRLVtbToWzN6q5V2ZnXpNedYnax7fBEaSqPst6aR9x39kkTmmLhZpXEQvvaZgGNHmXBnrykJmFCc3v</span>
|
||||
<span class="hljs-comment">// xpub: ExtPublicKey = zpub6jftahH18ngZwWS14LcyzALYxQRNM6XdvhGx4N2dJAdXg95J3wduJRZ65D1RDfPN2S25485AwYuBfxs9QitpieoT8Y79ESU2ardN3S5wQZB</span>
|
||||
|
||||
<span class="hljs-comment">// you can now use the generated xpriv to derive further</span>
|
||||
<span class="hljs-comment">// private or public keys</span>
|
||||
|
@ -154,7 +154,7 @@ spend or steal any of your money.</p>
|
|||
<span class="hljs-comment">// can generate addresses with it!</span>
|
||||
accountXpriv.extPublicKey
|
||||
}
|
||||
<span class="hljs-comment">// accountXpub: ExtPublicKey = zpub6rJhkT4Ui12TUkLeF3Vq1TAVFmsFPfrjvEPUmHDeg1Bexxb27WUcXx6fQyCmNo7KhJG4MGUXKeSGR8ZriPQgoVTYBoqAcs3JsyPZaa8LFaR</span>
|
||||
<span class="hljs-comment">// accountXpub: ExtPublicKey = zpub6rhryunTxHALpoJ48BwzLmWqGUhLQgCqQYZLBkYYKPXz9NU625bMdSXBShS4AmeRXD594aFvugvbo3hyncZ9HLQnMLStgXWf66qcviFiRJk</span>
|
||||
|
||||
<span class="hljs-comment">// address no. 0 ---------------┐</span>
|
||||
<span class="hljs-comment">// external address ----------┐ |</span>
|
||||
|
@ -177,12 +177,12 @@ spend or steal any of your money.</p>
|
|||
<span class="hljs-keyword">val</span> scriptPubKey = <span class="hljs-type">P2WPKHWitnessSPKV0</span>(pubkey)
|
||||
<span class="hljs-type">Bech32Address</span>(scriptPubKey, <span class="hljs-type">TestNet3</span>)
|
||||
}
|
||||
<span class="hljs-comment">// firstAccountAddress: Bech32Address = Bech32Address(tb1q73fv2f8fyks3f9rstatv6yapjutq0t8rg7cn7u)</span>
|
||||
<span class="hljs-comment">// firstAccountAddress: Bech32Address = Bech32Address(tb1qmskhe7q6qverdygy47xlzjjdcttfjecrkhkkny)</span>
|
||||
|
||||
<span class="hljs-comment">// tada! We just generated an address you can send money to,</span>
|
||||
<span class="hljs-comment">// without having access to the private key!</span>
|
||||
firstAccountAddress.value
|
||||
<span class="hljs-comment">// res2: String = tb1q73fv2f8fyks3f9rstatv6yapjutq0t8rg7cn7u</span>
|
||||
<span class="hljs-comment">// res2: String = tb1qmskhe7q6qverdygy47xlzjjdcttfjecrkhkkny</span>
|
||||
|
||||
<span class="hljs-comment">// you can now continue deriving addresses from the same public</span>
|
||||
<span class="hljs-comment">// key, by imitating what we did above. To get the next</span>
|
||||
|
|
|
@ -86,13 +86,13 @@ use that to generate further private and public keys:</p>
|
|||
<span class="hljs-comment">// how long our phrase ends up being</span>
|
||||
<span class="hljs-comment">// 256 bits of entropy results in 24 words</span>
|
||||
<span class="hljs-keyword">val</span> entropy: <span class="hljs-type">BitVector</span> = <span class="hljs-type">MnemonicCode</span>.getEntropy256Bits
|
||||
<span class="hljs-comment">// entropy: BitVector = BitVector(256 bits, 0x5edce67e8c6420575fc8feca94c94f76e9e051057ab8bdf56c3ea90ba46e57fe)</span>
|
||||
<span class="hljs-comment">// entropy: BitVector = BitVector(256 bits, 0xa393c0bd5ebdcb69b83e9c714b6ddaceb8603a365328d12214e15da0cb832192)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mnemonicCode = <span class="hljs-type">MnemonicCode</span>.fromEntropy(entropy)
|
||||
<span class="hljs-comment">// mnemonicCode: MnemonicCode = Masked(MnemonicCodeImpl)</span>
|
||||
|
||||
mnemonicCode.words <span class="hljs-comment">// the phrase the user should write down</span>
|
||||
<span class="hljs-comment">// res0: Vector[String] = Vector(gain, transfer, panic, boat, dragon, close, left, cabin, skin, play, fatigue, universe, own, chuckle, bicycle, finish, sadness, problem, autumn, faith, ripple, damp, garment, sponsor) // the phrase the user should write down</span>
|
||||
<span class="hljs-comment">// res0: Vector[String] = Vector(phone, own, consider, runway, syrup, regular, thought, squeeze, image, fork, issue, output, maid, brown, sun, govern, spell, awful, debate, robust, arrow, screen, main, elbow) // the phrase the user should write down</span>
|
||||
|
||||
<span class="hljs-comment">// the password argument is an optional, extra security</span>
|
||||
<span class="hljs-comment">// measure. all MnemonicCode instances will give you a</span>
|
||||
|
@ -108,7 +108,7 @@ mnemonicCode.words <span class="hljs-comment">// the phrase the user should writ
|
|||
bip39Seed)
|
||||
<span class="hljs-comment">// xpriv: ExtPrivateKey = Masked(ExtPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> xpub = xpriv.extPublicKey
|
||||
<span class="hljs-comment">// xpub: ExtPublicKey = zpub6jftahH18ngZyCztNYkKnn6ZMbYQoBRLVtbToWzN6q5V2ZnXpNedYnax7fBEaSqPst6aR9x39kkTmmLhZpXEQvvaZgGNHmXBnrykJmFCc3v</span>
|
||||
<span class="hljs-comment">// xpub: ExtPublicKey = zpub6jftahH18ngZwWS14LcyzALYxQRNM6XdvhGx4N2dJAdXg95J3wduJRZ65D1RDfPN2S25485AwYuBfxs9QitpieoT8Y79ESU2ardN3S5wQZB</span>
|
||||
|
||||
<span class="hljs-comment">// you can now use the generated xpriv to derive further</span>
|
||||
<span class="hljs-comment">// private or public keys</span>
|
||||
|
@ -154,7 +154,7 @@ spend or steal any of your money.</p>
|
|||
<span class="hljs-comment">// can generate addresses with it!</span>
|
||||
accountXpriv.extPublicKey
|
||||
}
|
||||
<span class="hljs-comment">// accountXpub: ExtPublicKey = zpub6rJhkT4Ui12TUkLeF3Vq1TAVFmsFPfrjvEPUmHDeg1Bexxb27WUcXx6fQyCmNo7KhJG4MGUXKeSGR8ZriPQgoVTYBoqAcs3JsyPZaa8LFaR</span>
|
||||
<span class="hljs-comment">// accountXpub: ExtPublicKey = zpub6rhryunTxHALpoJ48BwzLmWqGUhLQgCqQYZLBkYYKPXz9NU625bMdSXBShS4AmeRXD594aFvugvbo3hyncZ9HLQnMLStgXWf66qcviFiRJk</span>
|
||||
|
||||
<span class="hljs-comment">// address no. 0 ---------------┐</span>
|
||||
<span class="hljs-comment">// external address ----------┐ |</span>
|
||||
|
@ -177,12 +177,12 @@ spend or steal any of your money.</p>
|
|||
<span class="hljs-keyword">val</span> scriptPubKey = <span class="hljs-type">P2WPKHWitnessSPKV0</span>(pubkey)
|
||||
<span class="hljs-type">Bech32Address</span>(scriptPubKey, <span class="hljs-type">TestNet3</span>)
|
||||
}
|
||||
<span class="hljs-comment">// firstAccountAddress: Bech32Address = Bech32Address(tb1q73fv2f8fyks3f9rstatv6yapjutq0t8rg7cn7u)</span>
|
||||
<span class="hljs-comment">// firstAccountAddress: Bech32Address = Bech32Address(tb1qmskhe7q6qverdygy47xlzjjdcttfjecrkhkkny)</span>
|
||||
|
||||
<span class="hljs-comment">// tada! We just generated an address you can send money to,</span>
|
||||
<span class="hljs-comment">// without having access to the private key!</span>
|
||||
firstAccountAddress.value
|
||||
<span class="hljs-comment">// res2: String = tb1q73fv2f8fyks3f9rstatv6yapjutq0t8rg7cn7u</span>
|
||||
<span class="hljs-comment">// res2: String = tb1qmskhe7q6qverdygy47xlzjjdcttfjecrkhkkny</span>
|
||||
|
||||
<span class="hljs-comment">// you can now continue deriving addresses from the same public</span>
|
||||
<span class="hljs-comment">// key, by imitating what we did above. To get the next</span>
|
||||
|
|
|
@ -96,13 +96,13 @@
|
|||
<span class="hljs-comment">// extPrivKey: ExtPrivateKey = Masked(ExtPrivateKeyImpl)</span>
|
||||
|
||||
extPrivKey.sign(<span class="hljs-type">DoubleSha256Digest</span>.empty.bytes)
|
||||
<span class="hljs-comment">// res0: ECDigitalSignature = ECDigitalSignature(30440220044bd0f93fc016a29b26b3877d1e402a7f79296a7acf08cc73e62bc6efa9476c022028e8f58310dc1a3b1823cee9b3a4a5896a93e3abb5669b18f89b174e2948b1ce)</span>
|
||||
<span class="hljs-comment">// res0: ECDigitalSignature = ECDigitalSignature(3045022100ac4fed5f222e6d7f2c83722155b71464945f723304c842b84a78ea03350af4e902200c7549ce5182f686fb2bd6804454393a19e03e412cbeca83fc3c80191b1da9c3)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> path = <span class="hljs-type">BIP32Path</span>(<span class="hljs-type">Vector</span>(<span class="hljs-type">BIP32Node</span>(<span class="hljs-number">0</span>,<span class="hljs-literal">false</span>)))
|
||||
<span class="hljs-comment">// path: BIP32Path = m/0</span>
|
||||
|
||||
extPrivKey.sign(<span class="hljs-type">DoubleSha256Digest</span>.empty.bytes,path)
|
||||
<span class="hljs-comment">// res1: ECDigitalSignature = ECDigitalSignature(3045022100ee5ba982278214d43448eba1baaee54898dbb5de5ae888d701ed0e6db49f242702207722da17ce33e038f6b54f1d62a70200d395879cfb18c06f783f36828099ddb4)</span>
|
||||
<span class="hljs-comment">// res1: ECDigitalSignature = ECDigitalSignature(304402202d27f7b0cefa571dce9af91cbadc932ac522242612fd0987a963e93366b4e94302202dac1fea37502b38155fd2714713948c3f02bbbe45ecd47271b23bdabd47c904)</span>
|
||||
</code></pre>
|
||||
<p>With <code>ExtSign</code>, you can use <code>ExtPrivateKey</code> to sign transactions inside of <code>TxBuilder</code> since <code>UTXOSpendingInfo</code> takes in <code>Sign</code> as a parameter.</p>
|
||||
<p>You can also provide a <code>path</code> to use to derive a child <code>ExtPrivateKey</code>, and then sign with that child private key</p>
|
||||
|
|
|
@ -96,13 +96,13 @@
|
|||
<span class="hljs-comment">// extPrivKey: ExtPrivateKey = Masked(ExtPrivateKeyImpl)</span>
|
||||
|
||||
extPrivKey.sign(<span class="hljs-type">DoubleSha256Digest</span>.empty.bytes)
|
||||
<span class="hljs-comment">// res0: ECDigitalSignature = ECDigitalSignature(30440220044bd0f93fc016a29b26b3877d1e402a7f79296a7acf08cc73e62bc6efa9476c022028e8f58310dc1a3b1823cee9b3a4a5896a93e3abb5669b18f89b174e2948b1ce)</span>
|
||||
<span class="hljs-comment">// res0: ECDigitalSignature = ECDigitalSignature(3045022100ac4fed5f222e6d7f2c83722155b71464945f723304c842b84a78ea03350af4e902200c7549ce5182f686fb2bd6804454393a19e03e412cbeca83fc3c80191b1da9c3)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> path = <span class="hljs-type">BIP32Path</span>(<span class="hljs-type">Vector</span>(<span class="hljs-type">BIP32Node</span>(<span class="hljs-number">0</span>,<span class="hljs-literal">false</span>)))
|
||||
<span class="hljs-comment">// path: BIP32Path = m/0</span>
|
||||
|
||||
extPrivKey.sign(<span class="hljs-type">DoubleSha256Digest</span>.empty.bytes,path)
|
||||
<span class="hljs-comment">// res1: ECDigitalSignature = ECDigitalSignature(3045022100ee5ba982278214d43448eba1baaee54898dbb5de5ae888d701ed0e6db49f242702207722da17ce33e038f6b54f1d62a70200d395879cfb18c06f783f36828099ddb4)</span>
|
||||
<span class="hljs-comment">// res1: ECDigitalSignature = ECDigitalSignature(304402202d27f7b0cefa571dce9af91cbadc932ac522242612fd0987a963e93366b4e94302202dac1fea37502b38155fd2714713948c3f02bbbe45ecd47271b23bdabd47c904)</span>
|
||||
</code></pre>
|
||||
<p>With <code>ExtSign</code>, you can use <code>ExtPrivateKey</code> to sign transactions inside of <code>TxBuilder</code> since <code>UTXOSpendingInfo</code> takes in <code>Sign</code> as a parameter.</p>
|
||||
<p>You can also provide a <code>path</code> to use to derive a child <code>ExtPrivateKey</code>, and then sign with that child private key</p>
|
||||
|
|
|
@ -65,25 +65,25 @@
|
|||
</script></nav></div><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/bitcoin-s/bitcoin-s/blob/master/docs/core/txbuilder.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 id="__docusaurus" class="postHeaderTitle">TxBuilder Example</h1></header><article><div><span><p>Bitcoin-S features a transaction builder that constructs and signs Bitcoin
|
||||
transactions. Here's an example of how to use it</p>
|
||||
<pre><code class="hljs css language-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> ec: <span class="hljs-type">ExecutionContext</span> = <span class="hljs-type">ExecutionContext</span>.<span class="hljs-type">Implicits</span>.global
|
||||
<span class="hljs-comment">// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@2d449de9[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]</span>
|
||||
<span class="hljs-comment">// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@430bbf4b[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]</span>
|
||||
|
||||
<span class="hljs-comment">// generate a fresh private key that we are going to use in the scriptpubkey</span>
|
||||
<span class="hljs-keyword">val</span> privKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> pubKey = privKey.publicKey
|
||||
<span class="hljs-comment">// pubKey: ECPublicKey = ECPublicKey(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7)</span>
|
||||
<span class="hljs-comment">// pubKey: ECPublicKey = ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56)</span>
|
||||
|
||||
<span class="hljs-comment">// this is the script that the TxBuilder is going to create a</span>
|
||||
<span class="hljs-comment">// script signature that validly spends this scriptPubKey</span>
|
||||
<span class="hljs-keyword">val</span> creditingSpk = <span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = privKey.publicKey)
|
||||
<span class="hljs-comment">// creditingSpk: P2PKHScriptPubKey = pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6)</span>
|
||||
<span class="hljs-comment">// creditingSpk: P2PKHScriptPubKey = pkh(5830771fe741a85013f5c1048293791c1f084355)</span>
|
||||
<span class="hljs-keyword">val</span> amount = <span class="hljs-number">10000.</span>satoshis
|
||||
<span class="hljs-comment">// amount: Satoshis = 10000 sats</span>
|
||||
|
||||
<span class="hljs-comment">// this is the UTXO we are going to be spending</span>
|
||||
<span class="hljs-keyword">val</span> utxo =
|
||||
<span class="hljs-type">TransactionOutput</span>(value = amount, scriptPubKey = creditingSpk)
|
||||
<span class="hljs-comment">// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6))</span>
|
||||
<span class="hljs-comment">// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))</span>
|
||||
|
||||
<span class="hljs-comment">// the private key that locks the funds for the script we are spending too</span>
|
||||
<span class="hljs-keyword">val</span> destinationPrivKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
|
@ -96,7 +96,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-comment">// the script that corresponds to destination private key, this is what is protecting the money</span>
|
||||
<span class="hljs-keyword">val</span> destinationSPK =
|
||||
<span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = destinationPrivKey.publicKey)
|
||||
<span class="hljs-comment">// destinationSPK: P2PKHScriptPubKey = pkh(37753773876bd819d38cf08812c685ea727d562f)</span>
|
||||
<span class="hljs-comment">// destinationSPK: P2PKHScriptPubKey = pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)</span>
|
||||
|
||||
<span class="hljs-comment">// this is where we are sending money too</span>
|
||||
<span class="hljs-comment">// we could add more destinations here if we</span>
|
||||
|
@ -107,7 +107,7 @@ transactions. Here's an example of how to use it</p>
|
|||
|
||||
<span class="hljs-type">Vector</span>(destination1)
|
||||
}
|
||||
<span class="hljs-comment">// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f)))</span>
|
||||
<span class="hljs-comment">// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)))</span>
|
||||
|
||||
<span class="hljs-comment">// we have to fabricate a transaction that contains the</span>
|
||||
<span class="hljs-comment">// UTXO we are trying to spend. If this were a real blockchain</span>
|
||||
|
@ -116,12 +116,12 @@ transactions. Here's an example of how to use it</p>
|
|||
inputs = <span class="hljs-type">Vector</span>.empty,
|
||||
outputs = <span class="hljs-type">Vector</span>(utxo),
|
||||
lockTime = <span class="hljs-type">UInt32</span>.zero)
|
||||
<span class="hljs-comment">// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6))),UInt32Impl(0))</span>
|
||||
<span class="hljs-comment">// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))),UInt32Impl(0))</span>
|
||||
|
||||
<span class="hljs-comment">// this is the information we need from the crediting TX</span>
|
||||
<span class="hljs-comment">// to properly "link" it in the transaction we are creating</span>
|
||||
<span class="hljs-keyword">val</span> outPoint = <span class="hljs-type">TransactionOutPoint</span>(creditingTx.txId, <span class="hljs-type">UInt32</span>.zero)
|
||||
<span class="hljs-comment">// outPoint: TransactionOutPoint = TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0)</span>
|
||||
<span class="hljs-comment">// outPoint: TransactionOutPoint = TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0)</span>
|
||||
|
||||
<span class="hljs-comment">// this contains all the information we need to</span>
|
||||
<span class="hljs-comment">// validly sign the UTXO above</span>
|
||||
|
@ -134,12 +134,12 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-type">HashType</span>.sigHashAll,
|
||||
conditionalPath =
|
||||
<span class="hljs-type">ConditionalPath</span>.<span class="hljs-type">NoConditionsLeft</span>)
|
||||
<span class="hljs-comment">// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))</span>
|
||||
<span class="hljs-comment">// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))</span>
|
||||
|
||||
<span class="hljs-comment">// all of the UTXO spending information, since we are only</span>
|
||||
<span class="hljs-comment">//spending one UTXO, this is just one element</span>
|
||||
<span class="hljs-keyword">val</span> utxos: <span class="hljs-type">Vector</span>[<span class="hljs-type">BitcoinUTXOSpendingInfoFull</span>] = <span class="hljs-type">Vector</span>(utxoSpendingInfo)
|
||||
<span class="hljs-comment">// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))</span>
|
||||
<span class="hljs-comment">// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))</span>
|
||||
|
||||
<span class="hljs-comment">// this is how much we are going to pay as a fee to the network</span>
|
||||
<span class="hljs-comment">// for this example, we are going to pay 1 satoshi per byte</span>
|
||||
|
@ -149,7 +149,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-keyword">val</span> changePrivKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// changePrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> changeSPK = <span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = changePrivKey.publicKey)
|
||||
<span class="hljs-comment">// changeSPK: P2PKHScriptPubKey = pkh(954c8e5b17f220557409cd91ce375ff8f34b3855)</span>
|
||||
<span class="hljs-comment">// changeSPK: P2PKHScriptPubKey = pkh(da629e94594b79e41b8c6d25768073b89ac56f13)</span>
|
||||
|
||||
<span class="hljs-comment">// the network we are on, for this example we are using</span>
|
||||
<span class="hljs-comment">// the regression test network. This is a network you control</span>
|
||||
|
@ -168,7 +168,7 @@ transactions. Here's an example of how to use it</p>
|
|||
network = networkParams)
|
||||
<span class="hljs-type">Await</span>.result(builderF, <span class="hljs-number">30.</span>seconds)
|
||||
}
|
||||
<span class="hljs-comment">// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f))),Map(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0) -> P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(954c8e5b17f220557409cd91ce375ff8f34b3855),RegTest)</span>
|
||||
<span class="hljs-comment">// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab))),Map(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0) -> P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(da629e94594b79e41b8c6d25768073b89ac56f13),RegTest)</span>
|
||||
|
||||
<span class="hljs-comment">// Let's finally produce a validly signed tx!</span>
|
||||
<span class="hljs-comment">// The 'sign' method is going produce a validly signed transaction</span>
|
||||
|
@ -182,7 +182,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-keyword">val</span> signF = txBuilder.sign
|
||||
<span class="hljs-type">Await</span>.result(signF, <span class="hljs-number">30.</span>seconds)
|
||||
}
|
||||
<span class="hljs-comment">// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),P2PKHScriptSignature(ECPublicKey(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7), ECDigitalSignature(3045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e601)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f)), TransactionOutput(4774 sats,pkh(954c8e5b17f220557409cd91ce375ff8f34b3855))),UInt32Impl(0))</span>
|
||||
<span class="hljs-comment">// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),P2PKHScriptSignature(ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56), ECDigitalSignature(3045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)), TransactionOutput(4774 sats,pkh(da629e94594b79e41b8c6d25768073b89ac56f13))),UInt32Impl(0))</span>
|
||||
</code></pre>
|
||||
<pre><code class="hljs css language-scala">signedTx.inputs.length
|
||||
<span class="hljs-comment">// res0: Int = 1</span>
|
||||
|
@ -192,7 +192,7 @@ signedTx.outputs.length
|
|||
|
||||
<span class="hljs-comment">//remember, you can call .hex on any bitcoin-s data structure to get the hex representation!</span>
|
||||
signedTx.hex
|
||||
<span class="hljs-comment">// res2: String = 02000000011589c69fe62f79a2bff36789b33ee756f308ddad7c9eeaafd62e403d7717e285000000006b483045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e6012103dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7000000000288130000000000001976a91437753773876bd819d38cf08812c685ea727d562f88aca6120000000000001976a914954c8e5b17f220557409cd91ce375ff8f34b385588ac00000000</span>
|
||||
<span class="hljs-comment">// res2: String = 0200000001e6d2c246a7b0ffa3a0b8ba57844e6f7bba8a38309800bce9b2d37f5a6d8a6922000000006b483045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501210226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56000000000288130000000000001976a914500a2a16b5fa3c4661c26bed6605f6da905f1bab88aca6120000000000001976a914da629e94594b79e41b8c6d25768073b89ac56f1388ac00000000</span>
|
||||
</code></pre>
|
||||
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/core/psbts"><span class="arrow-prev">← </span><span>Partially Signed Bitcoin Transactions</span></a><a class="docs-next button" href="/docs/next/key-manager/key-manager"><span>Key Manager</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/favicon.ico" alt="bitcoin-s" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/en/getting-started">Getting Started</a><a href="/docs/en/core/core-intro">Guides</a><a href="/api/org/bitcoins">API Reference</a></div><div><h5>Community</h5><a href="/en/users.html">User Showcase</a><a href="https://join.slack.com/t/suredbits/shared_invite/enQtNDEyMjY3MTg1MTg3LTYyYjkwOGUzMDQ4NDAwZjE1M2I3MmQyNWNlZjNlYjg4OGRjYTRjNWUwNjRjNjg4Y2NjZjAxYjU1N2JjMTU1YWM" target="_blank" rel="noreferrer noopener">Slack</a><a href="https://gitter.im/bitcoin-s-core/">Gitter chat</a></div><div><h5>More</h5><a href="https://github.com/bitcoin-s/bitcoin-s">GitHub</a><a class="github-button" href="https://github.com/bitcoin-s/bitcoin-s" data-icon="octicon-star" data-count-href="/bitcoin-s/bitcoin-s-core/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2020 Suredbits & the bitcoin-s developers</section></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script><script>
|
||||
document.addEventListener('keyup', function(e) {
|
||||
|
|
|
@ -65,25 +65,25 @@
|
|||
</script></nav></div><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/bitcoin-s/bitcoin-s/blob/master/docs/core/txbuilder.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 id="__docusaurus" class="postHeaderTitle">TxBuilder Example</h1></header><article><div><span><p>Bitcoin-S features a transaction builder that constructs and signs Bitcoin
|
||||
transactions. Here's an example of how to use it</p>
|
||||
<pre><code class="hljs css language-scala"><span class="hljs-keyword">implicit</span> <span class="hljs-keyword">val</span> ec: <span class="hljs-type">ExecutionContext</span> = <span class="hljs-type">ExecutionContext</span>.<span class="hljs-type">Implicits</span>.global
|
||||
<span class="hljs-comment">// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@2d449de9[Running, parallelism = 2, size = 2, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]</span>
|
||||
<span class="hljs-comment">// ec: ExecutionContext = scala.concurrent.impl.ExecutionContextImpl$$anon$3@430bbf4b[Running, parallelism = 2, size = 1, active = 0, running = 0, steals = 9, tasks = 0, submissions = 0]</span>
|
||||
|
||||
<span class="hljs-comment">// generate a fresh private key that we are going to use in the scriptpubkey</span>
|
||||
<span class="hljs-keyword">val</span> privKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> pubKey = privKey.publicKey
|
||||
<span class="hljs-comment">// pubKey: ECPublicKey = ECPublicKey(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7)</span>
|
||||
<span class="hljs-comment">// pubKey: ECPublicKey = ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56)</span>
|
||||
|
||||
<span class="hljs-comment">// this is the script that the TxBuilder is going to create a</span>
|
||||
<span class="hljs-comment">// script signature that validly spends this scriptPubKey</span>
|
||||
<span class="hljs-keyword">val</span> creditingSpk = <span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = privKey.publicKey)
|
||||
<span class="hljs-comment">// creditingSpk: P2PKHScriptPubKey = pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6)</span>
|
||||
<span class="hljs-comment">// creditingSpk: P2PKHScriptPubKey = pkh(5830771fe741a85013f5c1048293791c1f084355)</span>
|
||||
<span class="hljs-keyword">val</span> amount = <span class="hljs-number">10000.</span>satoshis
|
||||
<span class="hljs-comment">// amount: Satoshis = 10000 sats</span>
|
||||
|
||||
<span class="hljs-comment">// this is the UTXO we are going to be spending</span>
|
||||
<span class="hljs-keyword">val</span> utxo =
|
||||
<span class="hljs-type">TransactionOutput</span>(value = amount, scriptPubKey = creditingSpk)
|
||||
<span class="hljs-comment">// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6))</span>
|
||||
<span class="hljs-comment">// utxo: TransactionOutput = TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))</span>
|
||||
|
||||
<span class="hljs-comment">// the private key that locks the funds for the script we are spending too</span>
|
||||
<span class="hljs-keyword">val</span> destinationPrivKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
|
@ -96,7 +96,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-comment">// the script that corresponds to destination private key, this is what is protecting the money</span>
|
||||
<span class="hljs-keyword">val</span> destinationSPK =
|
||||
<span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = destinationPrivKey.publicKey)
|
||||
<span class="hljs-comment">// destinationSPK: P2PKHScriptPubKey = pkh(37753773876bd819d38cf08812c685ea727d562f)</span>
|
||||
<span class="hljs-comment">// destinationSPK: P2PKHScriptPubKey = pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)</span>
|
||||
|
||||
<span class="hljs-comment">// this is where we are sending money too</span>
|
||||
<span class="hljs-comment">// we could add more destinations here if we</span>
|
||||
|
@ -107,7 +107,7 @@ transactions. Here's an example of how to use it</p>
|
|||
|
||||
<span class="hljs-type">Vector</span>(destination1)
|
||||
}
|
||||
<span class="hljs-comment">// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f)))</span>
|
||||
<span class="hljs-comment">// destinations: Vector[TransactionOutput] = Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)))</span>
|
||||
|
||||
<span class="hljs-comment">// we have to fabricate a transaction that contains the</span>
|
||||
<span class="hljs-comment">// UTXO we are trying to spend. If this were a real blockchain</span>
|
||||
|
@ -116,12 +116,12 @@ transactions. Here's an example of how to use it</p>
|
|||
inputs = <span class="hljs-type">Vector</span>.empty,
|
||||
outputs = <span class="hljs-type">Vector</span>(utxo),
|
||||
lockTime = <span class="hljs-type">UInt32</span>.zero)
|
||||
<span class="hljs-comment">// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6))),UInt32Impl(0))</span>
|
||||
<span class="hljs-comment">// creditingTx: BaseTransaction = BaseTransactionImpl(Int32Impl(1),Vector(),Vector(TransactionOutput(10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355))),UInt32Impl(0))</span>
|
||||
|
||||
<span class="hljs-comment">// this is the information we need from the crediting TX</span>
|
||||
<span class="hljs-comment">// to properly "link" it in the transaction we are creating</span>
|
||||
<span class="hljs-keyword">val</span> outPoint = <span class="hljs-type">TransactionOutPoint</span>(creditingTx.txId, <span class="hljs-type">UInt32</span>.zero)
|
||||
<span class="hljs-comment">// outPoint: TransactionOutPoint = TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0)</span>
|
||||
<span class="hljs-comment">// outPoint: TransactionOutPoint = TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0)</span>
|
||||
|
||||
<span class="hljs-comment">// this contains all the information we need to</span>
|
||||
<span class="hljs-comment">// validly sign the UTXO above</span>
|
||||
|
@ -134,12 +134,12 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-type">HashType</span>.sigHashAll,
|
||||
conditionalPath =
|
||||
<span class="hljs-type">ConditionalPath</span>.<span class="hljs-type">NoConditionsLeft</span>)
|
||||
<span class="hljs-comment">// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))</span>
|
||||
<span class="hljs-comment">// utxoSpendingInfo: BitcoinUTXOSpendingInfoFull = P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))</span>
|
||||
|
||||
<span class="hljs-comment">// all of the UTXO spending information, since we are only</span>
|
||||
<span class="hljs-comment">//spending one UTXO, this is just one element</span>
|
||||
<span class="hljs-keyword">val</span> utxos: <span class="hljs-type">Vector</span>[<span class="hljs-type">BitcoinUTXOSpendingInfoFull</span>] = <span class="hljs-type">Vector</span>(utxoSpendingInfo)
|
||||
<span class="hljs-comment">// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))</span>
|
||||
<span class="hljs-comment">// utxos: Vector[BitcoinUTXOSpendingInfoFull] = Vector(P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1))))</span>
|
||||
|
||||
<span class="hljs-comment">// this is how much we are going to pay as a fee to the network</span>
|
||||
<span class="hljs-comment">// for this example, we are going to pay 1 satoshi per byte</span>
|
||||
|
@ -149,7 +149,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-keyword">val</span> changePrivKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// changePrivKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> changeSPK = <span class="hljs-type">P2PKHScriptPubKey</span>(pubKey = changePrivKey.publicKey)
|
||||
<span class="hljs-comment">// changeSPK: P2PKHScriptPubKey = pkh(954c8e5b17f220557409cd91ce375ff8f34b3855)</span>
|
||||
<span class="hljs-comment">// changeSPK: P2PKHScriptPubKey = pkh(da629e94594b79e41b8c6d25768073b89ac56f13)</span>
|
||||
|
||||
<span class="hljs-comment">// the network we are on, for this example we are using</span>
|
||||
<span class="hljs-comment">// the regression test network. This is a network you control</span>
|
||||
|
@ -168,7 +168,7 @@ transactions. Here's an example of how to use it</p>
|
|||
network = networkParams)
|
||||
<span class="hljs-type">Await</span>.result(builderF, <span class="hljs-number">30.</span>seconds)
|
||||
}
|
||||
<span class="hljs-comment">// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f))),Map(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0) -> P2PKHSpendingInfo(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),10000 sats,pkh(1ea74312a61bdd48723ad875cd240cebd5cfaef6),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(954c8e5b17f220557409cd91ce375ff8f34b3855),RegTest)</span>
|
||||
<span class="hljs-comment">// txBuilder: BitcoinTxBuilder = BitcoinTxBuilderImpl(Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab))),Map(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0) -> P2PKHSpendingInfo(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),10000 sats,pkh(5830771fe741a85013f5c1048293791c1f084355),Masked(ECPrivateKeyImpl),SIGHASH_ALL(Int32Impl(1)))),SatoshisPerByte(1 sat),pkh(da629e94594b79e41b8c6d25768073b89ac56f13),RegTest)</span>
|
||||
|
||||
<span class="hljs-comment">// Let's finally produce a validly signed tx!</span>
|
||||
<span class="hljs-comment">// The 'sign' method is going produce a validly signed transaction</span>
|
||||
|
@ -182,7 +182,7 @@ transactions. Here's an example of how to use it</p>
|
|||
<span class="hljs-keyword">val</span> signF = txBuilder.sign
|
||||
<span class="hljs-type">Await</span>.result(signF, <span class="hljs-number">30.</span>seconds)
|
||||
}
|
||||
<span class="hljs-comment">// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(85e217773d402ed6afea9e7caddd08f356e73eb38967f3bfa2792fe69fc68915:0),P2PKHScriptSignature(ECPublicKey(03dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7), ECDigitalSignature(3045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e601)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(37753773876bd819d38cf08812c685ea727d562f)), TransactionOutput(4774 sats,pkh(954c8e5b17f220557409cd91ce375ff8f34b3855))),UInt32Impl(0))</span>
|
||||
<span class="hljs-comment">// signedTx: Transaction = BaseTransactionImpl(Int32Impl(2),List(TransactionInputImpl(TransactionOutPoint(22698a6d5a7fd3b2e9bc009830388aba7b6f4e8457bab8a0a3ffb0a746c2d2e6:0),P2PKHScriptSignature(ECPublicKey(0226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56), ECDigitalSignature(3045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501)),UInt32Impl(0))),Vector(TransactionOutput(5000 sats,pkh(500a2a16b5fa3c4661c26bed6605f6da905f1bab)), TransactionOutput(4774 sats,pkh(da629e94594b79e41b8c6d25768073b89ac56f13))),UInt32Impl(0))</span>
|
||||
</code></pre>
|
||||
<pre><code class="hljs css language-scala">signedTx.inputs.length
|
||||
<span class="hljs-comment">// res0: Int = 1</span>
|
||||
|
@ -192,7 +192,7 @@ signedTx.outputs.length
|
|||
|
||||
<span class="hljs-comment">//remember, you can call .hex on any bitcoin-s data structure to get the hex representation!</span>
|
||||
signedTx.hex
|
||||
<span class="hljs-comment">// res2: String = 02000000011589c69fe62f79a2bff36789b33ee756f308ddad7c9eeaafd62e403d7717e285000000006b483045022100e3d34674e38c389f6cc406d13ead08171c53530dd7ee6149977e2763d76e32620220527f3900fe16078cd83fbabf3d36b53b80e31464b7b3c7043044200bd2d343e6012103dc36663d0d4d83751fbad4bc3b5297578b7fbcec18735fe125ed53b76181f5f7000000000288130000000000001976a91437753773876bd819d38cf08812c685ea727d562f88aca6120000000000001976a914954c8e5b17f220557409cd91ce375ff8f34b385588ac00000000</span>
|
||||
<span class="hljs-comment">// res2: String = 0200000001e6d2c246a7b0ffa3a0b8ba57844e6f7bba8a38309800bce9b2d37f5a6d8a6922000000006b483045022100dfb8f794c83522fb018400ccf0a25fff400204aeeb8014061d6e20373535482602203f2d9b8be0a3b09b4277df2a6c5c6d1f8bdf27373868c215e8088b961775c65501210226f3848952e3a0eefaae6998d4ae588f4998ed5981679c6602aae073a5145b56000000000288130000000000001976a914500a2a16b5fa3c4661c26bed6605f6da905f1bab88aca6120000000000001976a914da629e94594b79e41b8c6d25768073b89ac56f1388ac00000000</span>
|
||||
</code></pre>
|
||||
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/next/core/psbts"><span class="arrow-prev">← </span><span>Partially Signed Bitcoin Transactions</span></a><a class="docs-next button" href="/docs/next/key-manager/key-manager"><span>Key Manager</span><span class="arrow-next"> →</span></a></div></div></div><nav class="onPageNav"></nav></div><footer class="nav-footer" id="footer"><section class="sitemap"><a href="/" class="nav-home"><img src="/img/favicon.ico" alt="bitcoin-s" width="66" height="58"/></a><div><h5>Docs</h5><a href="/docs/en/getting-started">Getting Started</a><a href="/docs/en/core/core-intro">Guides</a><a href="/api/org/bitcoins">API Reference</a></div><div><h5>Community</h5><a href="/en/users.html">User Showcase</a><a href="https://join.slack.com/t/suredbits/shared_invite/enQtNDEyMjY3MTg1MTg3LTYyYjkwOGUzMDQ4NDAwZjE1M2I3MmQyNWNlZjNlYjg4OGRjYTRjNWUwNjRjNjg4Y2NjZjAxYjU1N2JjMTU1YWM" target="_blank" rel="noreferrer noopener">Slack</a><a href="https://gitter.im/bitcoin-s-core/">Gitter chat</a></div><div><h5>More</h5><a href="https://github.com/bitcoin-s/bitcoin-s">GitHub</a><a class="github-button" href="https://github.com/bitcoin-s/bitcoin-s" data-icon="octicon-star" data-count-href="/bitcoin-s/bitcoin-s-core/stargazers" data-show-count="true" data-count-aria-label="# stargazers on GitHub" aria-label="Star this project on GitHub">Star</a></div></section><section class="copyright">Copyright © 2020 Suredbits & the bitcoin-s developers</section></footer></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script><script>
|
||||
document.addEventListener('keyup', function(e) {
|
||||
|
|
|
@ -120,7 +120,7 @@ libraryDependencies += <span class="hljs-string">"org.bitcoin-s"</span> %% <span
|
|||
<h3><a class="anchor" aria-hidden="true" id="nightly-builds"></a><a href="#nightly-builds" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Nightly builds</h3>
|
||||
<p>You can also run on the bleeding edge of Bitcoin-S, by
|
||||
adding a snapshot build to your <code>build.sbt</code>. The most
|
||||
recent snapshot published is <code>0.3.0+29-44507733-SNAPSHOT</code>.</p>
|
||||
recent snapshot published is <code>0.3.0+30-13407538-SNAPSHOT</code>.</p>
|
||||
<p>To fetch snapshots, you will need to add the correct
|
||||
resolver in your <code>build.sbt</code>:</p>
|
||||
<p>```sbt
|
||||
|
|
|
@ -120,7 +120,7 @@ libraryDependencies += <span class="hljs-string">"org.bitcoin-s"</span> %% <span
|
|||
<h3><a class="anchor" aria-hidden="true" id="nightly-builds"></a><a href="#nightly-builds" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Nightly builds</h3>
|
||||
<p>You can also run on the bleeding edge of Bitcoin-S, by
|
||||
adding a snapshot build to your <code>build.sbt</code>. The most
|
||||
recent snapshot published is <code>0.3.0+29-44507733-SNAPSHOT</code>.</p>
|
||||
recent snapshot published is <code>0.3.0+30-13407538-SNAPSHOT</code>.</p>
|
||||
<p>To fetch snapshots, you will need to add the correct
|
||||
resolver in your <code>build.sbt</code>:</p>
|
||||
<p>```sbt
|
||||
|
|
|
@ -85,14 +85,14 @@ as a logical distinction for further development in subsequent releases.</p>
|
|||
|
||||
<span class="hljs-comment">//get 256 bits of random entropy</span>
|
||||
<span class="hljs-keyword">val</span> entropy = <span class="hljs-type">MnemonicCode</span>.getEntropy256Bits
|
||||
<span class="hljs-comment">// entropy: scodec.bits.BitVector = BitVector(256 bits, 0xd188c2f29daa8fb60e2a6432b317d12c17f8a6003506609863d41b72c667ff32)</span>
|
||||
<span class="hljs-comment">// entropy: scodec.bits.BitVector = BitVector(256 bits, 0x43926c5d456877739a12960a2d2527b96c3f6adccb2b966b4b1e6ce5200a5f39)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mnemonic = <span class="hljs-type">MnemonicCode</span>.fromEntropy(entropy)
|
||||
<span class="hljs-comment">// mnemonic: MnemonicCode = Masked(MnemonicCodeImpl)</span>
|
||||
|
||||
<span class="hljs-comment">//you can print that mnemonic seed with this</span>
|
||||
println(mnemonic.words)
|
||||
<span class="hljs-comment">// Vector(spice, ecology, royal, deputy, pottery, swallow, december, erode, crawl, obvious, violin, flag, legend, fantasy, absorb, patch, gauge, seek, vital, dad, cluster, crop, wolf, explain)</span>
|
||||
<span class="hljs-comment">// Vector(drum, nature, blanket, member, manual, right, hair, fan, any, hard, enemy, index, sentence, rely, industry, grace, nose, fog, shy, supply, faith, ahead, language, dash)</span>
|
||||
</code></pre>
|
||||
<p>Now that we have a <code>MnemonicCode</code> that was securely generated, we need to now create <code>KeyManagerParams</code> which tells us how to generate
|
||||
generate specific kinds of addresses for wallets.</p>
|
||||
|
@ -107,7 +107,7 @@ generate specific kinds of addresses for wallets.</p>
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-comment">//this will create a temp directory with the prefix 'key-manager-example` that will</span>
|
||||
<span class="hljs-comment">//have a file in it called "encrypted-bitcoin-s-seed.json"</span>
|
||||
<span class="hljs-keyword">val</span> seedPath = <span class="hljs-type">Files</span>.createTempDirectory(<span class="hljs-string">"key-manager-example"</span>).resolve(<span class="hljs-type">WalletStorage</span>.<span class="hljs-type">ENCRYPTED_SEED_FILE_NAME</span>)
|
||||
<span class="hljs-comment">// seedPath: Path = /tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json</span>
|
||||
<span class="hljs-comment">// seedPath: Path = /tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json</span>
|
||||
|
||||
<span class="hljs-comment">//let's create a native segwit key manager</span>
|
||||
<span class="hljs-keyword">val</span> purpose = <span class="hljs-type">HDPurposes</span>.<span class="hljs-type">SegWit</span>
|
||||
|
@ -118,16 +118,16 @@ generate specific kinds of addresses for wallets.</p>
|
|||
<span class="hljs-comment">// network: RegTest.type = RegTest</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> kmParams = <span class="hljs-type">KeyManagerParams</span>(seedPath, purpose, network)
|
||||
<span class="hljs-comment">// kmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',RegTest)</span>
|
||||
<span class="hljs-comment">// kmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',RegTest)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> km = <span class="hljs-type">BIP39KeyManager</span>.initializeWithMnemonic(mnemonic, <span class="hljs-type">None</span>, kmParams)
|
||||
<span class="hljs-comment">// km: Either[KeyManagerInitializeError, BIP39KeyManager] = Right(BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',RegTest),None))</span>
|
||||
<span class="hljs-comment">// km: Either[KeyManagerInitializeError, BIP39KeyManager] = Right(BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',RegTest),None))</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> rootXPub = km.right.get.getRootXPub
|
||||
<span class="hljs-comment">// rootXPub: ExtPublicKey = vpub5SLqN2bLY4WeYS9zBv6gWAaMtAotySTVkcJC8qB4gSgGYbLuD4kkptLhgjTFUbGmUebgPG3meUGJFsoc4qZdw41BU1HzU2SjKJAsYcdAob8</span>
|
||||
<span class="hljs-comment">// rootXPub: ExtPublicKey = vpub5SLqN2bLY4WeYF7x2B87JHSAh8SvGbbJw7isuzQMjoBGicfq3V9pm26BYSvaa89j7oLcyDz7wXRYMrTxy1c8GMHbMruSvrje5YE8W7xXTUe</span>
|
||||
|
||||
println(rootXPub)
|
||||
<span class="hljs-comment">// vpub5SLqN2bLY4WeYS9zBv6gWAaMtAotySTVkcJC8qB4gSgGYbLuD4kkptLhgjTFUbGmUebgPG3meUGJFsoc4qZdw41BU1HzU2SjKJAsYcdAob8</span>
|
||||
<span class="hljs-comment">// vpub5SLqN2bLY4WeYF7x2B87JHSAh8SvGbbJw7isuzQMjoBGicfq3V9pm26BYSvaa89j7oLcyDz7wXRYMrTxy1c8GMHbMruSvrje5YE8W7xXTUe</span>
|
||||
</code></pre>
|
||||
<p>Which should print something that looks like this</p>
|
||||
<p><code>vpub5SLqN2bLY4WeXxMqwJHJFBEwxSscGB2uDUnsTS3edVjZEwTrQDFDNqoR2xLqARQPabGaXsHSTenTRcqm2EnB9MpuC4vSk3LqSgNmGGZtuq7</code></p>
|
||||
|
@ -136,17 +136,17 @@ println(rootXPub)
|
|||
again after initializing it once. You can use the same <code>mnemonic</code> for different networks, which you control <code>KeyManagerParams</code>.</p>
|
||||
<pre><code class="hljs css language-scala"><span class="hljs-comment">//let's create a nested segwit key manager for mainnet</span>
|
||||
<span class="hljs-keyword">val</span> mainnetKmParams = <span class="hljs-type">KeyManagerParams</span>(seedPath, <span class="hljs-type">HDPurposes</span>.<span class="hljs-type">SegWit</span>, <span class="hljs-type">MainNet</span>)
|
||||
<span class="hljs-comment">// mainnetKmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',MainNet)</span>
|
||||
<span class="hljs-comment">// mainnetKmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',MainNet)</span>
|
||||
|
||||
<span class="hljs-comment">//we do not need to all `initializeWithMnemonic()` again as we have saved the seed to dis</span>
|
||||
<span class="hljs-keyword">val</span> mainnetKeyManager = <span class="hljs-type">BIP39KeyManager</span>(mnemonic, mainnetKmParams, <span class="hljs-type">None</span>)
|
||||
<span class="hljs-comment">// mainnetKeyManager: BIP39KeyManager = BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',MainNet),None)</span>
|
||||
<span class="hljs-comment">// mainnetKeyManager: BIP39KeyManager = BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',MainNet),None)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mainnetXpub = mainnetKeyManager.getRootXPub
|
||||
<span class="hljs-comment">// mainnetXpub: ExtPublicKey = zpub6jftahH18ngZwcvTXMFBLWxNa3PgjvRVR4P5GQkcCUBnkzbpDhR1K8yFmZHbUDtT7D4uPAS1V7gVo2FrwdDh7zjawN5gofigQCRT6vktuBN</span>
|
||||
<span class="hljs-comment">// mainnetXpub: ExtPublicKey = zpub6jftahH18ngZwRtRMcGc8dpBP12i35ZJbZom3ZyuFpgnw1vk47p5FGijdGkvZkmQkMoqy8NMnAqjtzvDqoGBTJ1zqDh9GW1bASUi4Pvea7a</span>
|
||||
|
||||
println(mainnetXpub)
|
||||
<span class="hljs-comment">// zpub6jftahH18ngZwcvTXMFBLWxNa3PgjvRVR4P5GQkcCUBnkzbpDhR1K8yFmZHbUDtT7D4uPAS1V7gVo2FrwdDh7zjawN5gofigQCRT6vktuBN</span>
|
||||
<span class="hljs-comment">// zpub6jftahH18ngZwRtRMcGc8dpBP12i35ZJbZom3ZyuFpgnw1vk47p5FGijdGkvZkmQkMoqy8NMnAqjtzvDqoGBTJ1zqDh9GW1bASUi4Pvea7a</span>
|
||||
</code></pre>
|
||||
<p>Which gives us something that looks like this</p>
|
||||
<p><code>zpub6jftahH18ngZw98KGjRo5XcxeKTQ2eztsvskb1dC9XF5TLimQquTs6Ry7nBBA425D9joXmfgJJCexmJ1u2SELJZJfRi95gcnXadLpZzYb5c</code></p>
|
||||
|
|
|
@ -85,14 +85,14 @@ as a logical distinction for further development in subsequent releases.</p>
|
|||
|
||||
<span class="hljs-comment">//get 256 bits of random entropy</span>
|
||||
<span class="hljs-keyword">val</span> entropy = <span class="hljs-type">MnemonicCode</span>.getEntropy256Bits
|
||||
<span class="hljs-comment">// entropy: scodec.bits.BitVector = BitVector(256 bits, 0xd188c2f29daa8fb60e2a6432b317d12c17f8a6003506609863d41b72c667ff32)</span>
|
||||
<span class="hljs-comment">// entropy: scodec.bits.BitVector = BitVector(256 bits, 0x43926c5d456877739a12960a2d2527b96c3f6adccb2b966b4b1e6ce5200a5f39)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mnemonic = <span class="hljs-type">MnemonicCode</span>.fromEntropy(entropy)
|
||||
<span class="hljs-comment">// mnemonic: MnemonicCode = Masked(MnemonicCodeImpl)</span>
|
||||
|
||||
<span class="hljs-comment">//you can print that mnemonic seed with this</span>
|
||||
println(mnemonic.words)
|
||||
<span class="hljs-comment">// Vector(spice, ecology, royal, deputy, pottery, swallow, december, erode, crawl, obvious, violin, flag, legend, fantasy, absorb, patch, gauge, seek, vital, dad, cluster, crop, wolf, explain)</span>
|
||||
<span class="hljs-comment">// Vector(drum, nature, blanket, member, manual, right, hair, fan, any, hard, enemy, index, sentence, rely, industry, grace, nose, fog, shy, supply, faith, ahead, language, dash)</span>
|
||||
</code></pre>
|
||||
<p>Now that we have a <code>MnemonicCode</code> that was securely generated, we need to now create <code>KeyManagerParams</code> which tells us how to generate
|
||||
generate specific kinds of addresses for wallets.</p>
|
||||
|
@ -107,7 +107,7 @@ generate specific kinds of addresses for wallets.</p>
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-comment">//this will create a temp directory with the prefix 'key-manager-example` that will</span>
|
||||
<span class="hljs-comment">//have a file in it called "encrypted-bitcoin-s-seed.json"</span>
|
||||
<span class="hljs-keyword">val</span> seedPath = <span class="hljs-type">Files</span>.createTempDirectory(<span class="hljs-string">"key-manager-example"</span>).resolve(<span class="hljs-type">WalletStorage</span>.<span class="hljs-type">ENCRYPTED_SEED_FILE_NAME</span>)
|
||||
<span class="hljs-comment">// seedPath: Path = /tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json</span>
|
||||
<span class="hljs-comment">// seedPath: Path = /tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json</span>
|
||||
|
||||
<span class="hljs-comment">//let's create a native segwit key manager</span>
|
||||
<span class="hljs-keyword">val</span> purpose = <span class="hljs-type">HDPurposes</span>.<span class="hljs-type">SegWit</span>
|
||||
|
@ -118,16 +118,16 @@ generate specific kinds of addresses for wallets.</p>
|
|||
<span class="hljs-comment">// network: RegTest.type = RegTest</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> kmParams = <span class="hljs-type">KeyManagerParams</span>(seedPath, purpose, network)
|
||||
<span class="hljs-comment">// kmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',RegTest)</span>
|
||||
<span class="hljs-comment">// kmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',RegTest)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> km = <span class="hljs-type">BIP39KeyManager</span>.initializeWithMnemonic(mnemonic, <span class="hljs-type">None</span>, kmParams)
|
||||
<span class="hljs-comment">// km: Either[KeyManagerInitializeError, BIP39KeyManager] = Right(BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',RegTest),None))</span>
|
||||
<span class="hljs-comment">// km: Either[KeyManagerInitializeError, BIP39KeyManager] = Right(BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',RegTest),None))</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> rootXPub = km.right.get.getRootXPub
|
||||
<span class="hljs-comment">// rootXPub: ExtPublicKey = vpub5SLqN2bLY4WeYS9zBv6gWAaMtAotySTVkcJC8qB4gSgGYbLuD4kkptLhgjTFUbGmUebgPG3meUGJFsoc4qZdw41BU1HzU2SjKJAsYcdAob8</span>
|
||||
<span class="hljs-comment">// rootXPub: ExtPublicKey = vpub5SLqN2bLY4WeYF7x2B87JHSAh8SvGbbJw7isuzQMjoBGicfq3V9pm26BYSvaa89j7oLcyDz7wXRYMrTxy1c8GMHbMruSvrje5YE8W7xXTUe</span>
|
||||
|
||||
println(rootXPub)
|
||||
<span class="hljs-comment">// vpub5SLqN2bLY4WeYS9zBv6gWAaMtAotySTVkcJC8qB4gSgGYbLuD4kkptLhgjTFUbGmUebgPG3meUGJFsoc4qZdw41BU1HzU2SjKJAsYcdAob8</span>
|
||||
<span class="hljs-comment">// vpub5SLqN2bLY4WeYF7x2B87JHSAh8SvGbbJw7isuzQMjoBGicfq3V9pm26BYSvaa89j7oLcyDz7wXRYMrTxy1c8GMHbMruSvrje5YE8W7xXTUe</span>
|
||||
</code></pre>
|
||||
<p>Which should print something that looks like this</p>
|
||||
<p><code>vpub5SLqN2bLY4WeXxMqwJHJFBEwxSscGB2uDUnsTS3edVjZEwTrQDFDNqoR2xLqARQPabGaXsHSTenTRcqm2EnB9MpuC4vSk3LqSgNmGGZtuq7</code></p>
|
||||
|
@ -136,17 +136,17 @@ println(rootXPub)
|
|||
again after initializing it once. You can use the same <code>mnemonic</code> for different networks, which you control <code>KeyManagerParams</code>.</p>
|
||||
<pre><code class="hljs css language-scala"><span class="hljs-comment">//let's create a nested segwit key manager for mainnet</span>
|
||||
<span class="hljs-keyword">val</span> mainnetKmParams = <span class="hljs-type">KeyManagerParams</span>(seedPath, <span class="hljs-type">HDPurposes</span>.<span class="hljs-type">SegWit</span>, <span class="hljs-type">MainNet</span>)
|
||||
<span class="hljs-comment">// mainnetKmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',MainNet)</span>
|
||||
<span class="hljs-comment">// mainnetKmParams: KeyManagerParams = KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',MainNet)</span>
|
||||
|
||||
<span class="hljs-comment">//we do not need to all `initializeWithMnemonic()` again as we have saved the seed to dis</span>
|
||||
<span class="hljs-keyword">val</span> mainnetKeyManager = <span class="hljs-type">BIP39KeyManager</span>(mnemonic, mainnetKmParams, <span class="hljs-type">None</span>)
|
||||
<span class="hljs-comment">// mainnetKeyManager: BIP39KeyManager = BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example1027152079734190530/encrypted-bitcoin-s-seed.json,m/84',MainNet),None)</span>
|
||||
<span class="hljs-comment">// mainnetKeyManager: BIP39KeyManager = BIP39KeyManager(Masked(MnemonicCodeImpl),KeyManagerParams(/tmp/key-manager-example9925622924260363227/encrypted-bitcoin-s-seed.json,m/84',MainNet),None)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> mainnetXpub = mainnetKeyManager.getRootXPub
|
||||
<span class="hljs-comment">// mainnetXpub: ExtPublicKey = zpub6jftahH18ngZwcvTXMFBLWxNa3PgjvRVR4P5GQkcCUBnkzbpDhR1K8yFmZHbUDtT7D4uPAS1V7gVo2FrwdDh7zjawN5gofigQCRT6vktuBN</span>
|
||||
<span class="hljs-comment">// mainnetXpub: ExtPublicKey = zpub6jftahH18ngZwRtRMcGc8dpBP12i35ZJbZom3ZyuFpgnw1vk47p5FGijdGkvZkmQkMoqy8NMnAqjtzvDqoGBTJ1zqDh9GW1bASUi4Pvea7a</span>
|
||||
|
||||
println(mainnetXpub)
|
||||
<span class="hljs-comment">// zpub6jftahH18ngZwcvTXMFBLWxNa3PgjvRVR4P5GQkcCUBnkzbpDhR1K8yFmZHbUDtT7D4uPAS1V7gVo2FrwdDh7zjawN5gofigQCRT6vktuBN</span>
|
||||
<span class="hljs-comment">// zpub6jftahH18ngZwRtRMcGc8dpBP12i35ZJbZom3ZyuFpgnw1vk47p5FGijdGkvZkmQkMoqy8NMnAqjtzvDqoGBTJ1zqDh9GW1bASUi4Pvea7a</span>
|
||||
</code></pre>
|
||||
<p>Which gives us something that looks like this</p>
|
||||
<p><code>zpub6jftahH18ngZw98KGjRo5XcxeKTQ2eztsvskb1dC9XF5TLimQquTs6Ry7nBBA425D9joXmfgJJCexmJ1u2SELJZJfRi95gcnXadLpZzYb5c</code></p>
|
||||
|
|
|
@ -114,12 +114,12 @@ println(<span class="hljs-string">s"Verified with NativeSecp256k1 again=<span cl
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-keyword">val</span> privKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> publicKey = privKey.publicKeyWithBouncyCastle
|
||||
<span class="hljs-comment">// publicKey: ECPublicKey = ECPublicKey(03957ad09429c4fa19cdb3c0fb84a918644360a587f93e25785667451630803885)</span>
|
||||
<span class="hljs-comment">// publicKey: ECPublicKey = ECPublicKey(0330babcd894632274677ddc2eb220ebef77529a1a27db3a978de7a4d0c77e9c01)</span>
|
||||
<span class="hljs-keyword">val</span> dataToSign = <span class="hljs-type">DoubleSha256Digest</span>.empty
|
||||
<span class="hljs-comment">// dataToSign: DoubleSha256Digest = DoubleSha256Digest(0000000000000000000000000000000000000000000000000000000000000000)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> signature = privKey.signWithBouncyCastle(dataToSign.bytes)
|
||||
<span class="hljs-comment">// signature: ECDigitalSignature = ECDigitalSignature(3044022010695416cddd9c236fbabb87ec0292e47f81ef71885e56703cdcbd3ca23154990220423192a2ed85a882c318bc20bb982e2ede406bb585fb1de3a00c82227b587417)</span>
|
||||
<span class="hljs-comment">// signature: ECDigitalSignature = ECDigitalSignature(3045022100c6d314ecd81531c1edce5396528e3b7b765ac10b55dc109aecfc4eb14231bfec022007b5965fc3c3dcd15efcb4ec34546978409dd66d8407886dd3442a335db0e5e9)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> verified = publicKey.verifyWithBouncyCastle(dataToSign.bytes, signature)
|
||||
<span class="hljs-comment">// verified: Boolean = true</span>
|
||||
|
|
|
@ -114,12 +114,12 @@ println(<span class="hljs-string">s"Verified with NativeSecp256k1 again=<span cl
|
|||
<pre><code class="hljs css language-scala"><span class="hljs-keyword">val</span> privKey = <span class="hljs-type">ECPrivateKey</span>.freshPrivateKey
|
||||
<span class="hljs-comment">// privKey: ECPrivateKey = Masked(ECPrivateKeyImpl)</span>
|
||||
<span class="hljs-keyword">val</span> publicKey = privKey.publicKeyWithBouncyCastle
|
||||
<span class="hljs-comment">// publicKey: ECPublicKey = ECPublicKey(03957ad09429c4fa19cdb3c0fb84a918644360a587f93e25785667451630803885)</span>
|
||||
<span class="hljs-comment">// publicKey: ECPublicKey = ECPublicKey(0330babcd894632274677ddc2eb220ebef77529a1a27db3a978de7a4d0c77e9c01)</span>
|
||||
<span class="hljs-keyword">val</span> dataToSign = <span class="hljs-type">DoubleSha256Digest</span>.empty
|
||||
<span class="hljs-comment">// dataToSign: DoubleSha256Digest = DoubleSha256Digest(0000000000000000000000000000000000000000000000000000000000000000)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> signature = privKey.signWithBouncyCastle(dataToSign.bytes)
|
||||
<span class="hljs-comment">// signature: ECDigitalSignature = ECDigitalSignature(3044022010695416cddd9c236fbabb87ec0292e47f81ef71885e56703cdcbd3ca23154990220423192a2ed85a882c318bc20bb982e2ede406bb585fb1de3a00c82227b587417)</span>
|
||||
<span class="hljs-comment">// signature: ECDigitalSignature = ECDigitalSignature(3045022100c6d314ecd81531c1edce5396528e3b7b765ac10b55dc109aecfc4eb14231bfec022007b5965fc3c3dcd15efcb4ec34546978409dd66d8407886dd3442a335db0e5e9)</span>
|
||||
|
||||
<span class="hljs-keyword">val</span> verified = publicKey.verifyWithBouncyCastle(dataToSign.bytes, signature)
|
||||
<span class="hljs-comment">// verified: Boolean = true</span>
|
||||
|
|
Loading…
Add table
Reference in a new issue