Skip to content

Instantly share code, notes, and snippets.

@pinheadmz
Last active March 1, 2020 21:05
Show Gist options
  • Save pinheadmz/c27157de8d1be465d66d35f204645224 to your computer and use it in GitHub Desktop.
Save pinheadmz/c27157de8d1be465d66d35f204645224 to your computer and use it in GitHub Desktop.
Create, sign and broadcast a raw TX using hsd RPC calls.

Get a wallet address, generate coins to it:

--> hsw-cli account get default
{
...
  "receiveAddress": "rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl"
...
}

--> hsd-rpc generatetoaddress 100 rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl

List available coins in the wallet to spend:

--> hsw-cli coins

[
...
  {
    "version": 0,
    "height": 128,
    "value": 2000000000,
    "address": "rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl",
    "covenant": {
      "type": 0,
      "action": "NONE",
      "items": []
    },
    "coinbase": true,
    "hash": "0135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a1",
    "index": 0
  }
]

Get the private key for the address that received the coin:

--> hsw-cli dump rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl
EPJvC2wDaHh8RhYjX5FBdSEYM8U4xGaHmJrfzLyhdmZ5Z5UkC59H

Now create a transaction spending that coin with two outputs, a recipient and a change address. Be sure the leftover amount (fee) is a rational value! Note that this is a NODE API CALL, not a wallet call.

txhash='0135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a1';
txindex=0;
amount=1;
address='rs1q7rvnwj3vaqxrwuv87j7xc6ye83tpevfkvhzsap';
changeamount=1998.999;
changeaddress='rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl';

hsd-cli rpc createrawtransaction \
  '[{ "txid": "'$txhash'", "vout": '$txindex' }]' \
  '{ "'$address'": '$amount', "'$changeaddress'": '$changeamount'}'

00000000010135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a100000000ffffffff0240420f00000000000014f0d9374a2ce80c377187f4bc6c68993c561cb1360000d84d2677000000000014e92c8c403a10242f240efc0d3b9ee0743eeaebdb00000000000000

Sign the raw transaction with the private key we obtained earlier. Notice again this is a FULL NODE function, not wallet:

rawtx='00000000010135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a100000000ffffffff0240420f00000000000014f0d9374a2ce80c377187f4bc6c68993c561cb1360000d84d2677000000000014e92c8c403a10242f240efc0d3b9ee0743eeaebdb00000000000000';
txhash='0135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a1';
txindex=0;
address='rs1qaykgcsp6zqjz7fqwlsxnh8hqwslw467md3vssl';
amount=2000;
privkey='EPJvC2wDaHh8RhYjX5FBdSEYM8U4xGaHmJrfzLyhdmZ5Z5UkC59H';


hsd-cli rpc signrawtransaction $rawtx \
  '[{ "txid": "'$txhash'", "vout": '$txindex', "address": "'$address'", "amount": '$amount' }]' \
  '[ "'$privkey'" ]'

{
  "hex": "00000000010135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a100000000ffffffff0240420f00000000000014f0d9374a2ce80c377187f4bc6c68993c561cb1360000d84d2677000000000014e92c8c403a10242f240efc0d3b9ee0743eeaebdb00000000000002416c4bd7b88c21d2bae38baa0de7b5ad28cbd1ba831398dbdd892d576158eef22978e0e0d2a3293da754f1544c880016aea7b814d34297d234e86d4820f5f1bb1e0121037f1c67355d5efb5d232646695c0baa2406ec1660c6b79915e2b491b7f62eb324",
  "complete": true
}

Boradcast the final tx!

--> hsd-cli broadcast 00000000010135dc860964f4ab51299f97ad033c3655b19b6ff563aa575d82f01b95d117a100000000ffffffff0240420f00000000000014f0d9374a2ce80c377187f4bc6c68993c561cb1360000d84d2677000000000014e92c8c403a10242f240efc0d3b9ee0743eeaebdb00000000000002416c4bd7b88c21d2bae38baa0de7b5ad28cbd1ba831398dbdd892d576158eef22978e0e0d2a3293da754f1544c880016aea7b814d34297d234e86d4820f5f1bb1e0121037f1c67355d5efb5d232646695c0baa2406ec1660c6b79915e2b491b7f62eb324
Broadcasted:
{
  "success": true
}

Check the full node log for any errors / success:

[debug] (node-http) Request for method=POST path=/broadcast (127.0.0.1).
[debug] (mempool) Added c9b4642eb4c010447f3f32621aeafbcf95785ecbf08de15dce5a593657a3b15f to mempool (txs=1).
[info] (wallet) Incoming transaction for 1 wallets in WalletDB (c9b4642eb4c010447f3f32621aeafbcf95785ecbf08de15dce5a593657a3b15f).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment