bitcoind – How can I create a easy P2SH transaction in regtest?

0
61


Sure we might try this simply, first we launch bitcoin core on regtest bitcoind -regtest -fallbackfee=0.0002 -daemon

then we generate 100 blocks to get some cash bitcoin-cli -regtest generatetoaddress 101 "$(bitcoin-cli -regtest getnewaddress)"

then we gonna want 3 new addresses for our 2-of-3 multisig tackle so lets create them

ADDR1=$(bitcoin-cli -regtest getnewaddress)
ADDR2=$(bitcoin-cli -regtest getnewaddress)
ADDR3=$(bitcoin-cli -regtest getnewaddress)

We get the general public keys related ( you may want jq right here )

PUBKEY1=$(bitcoin-cli -regtest getaddressinfo $ADDR1 | jq -r '.pubkey')
PUBKEY2=$(bitcoin-cli -regtest getaddressinfo $ADDR2 | jq -r '.pubkey')
PUBKEY3=$(bitcoin-cli -regtest getaddressinfo $ADDR3 | jq -r '.pubkey')

then we create our 2-of-3 multisig tackle P2SH_MULTISIG=$(bitcoin-cli -regtest createmultisig 2 "["$PUBKEY1","$PUBKEY2","$PUBKEY3"]" | jq -r '.tackle')

We ship some cash to it bitcoin-cli -regtest sendtoaddress $P2SH_MULTISIG 1

Then we generate new blocks to substantiate the transaction bitcoin-cli -regtest generatetoaddress 1 "$(bitcoin-cli -regtest getnewaddress)"

edit: Now we’ve our P2SH multisig tackle and there’s cash on it, lets create and signal a transaction that spends the P2SH output.

first we import our multisig tackle into our pockets

bitcoin-cli -regtest importaddress $P2SH_MULTISIG

then we listing the unspent transactions

UTXO=$(bitcoin-cli -regtest listunspent | jq -r ".[] | choose(.tackle == "$P2SH_MULTISIG")")

then we get the id, vout, and quantity from the UTXO

TXID=$(echo $UTXO | jq -r '.txid')
VOUT=$(echo $UTXO | jq -r '.vout')
AMOUNT=$(echo $UTXO | jq -r '.quantity')

then we make a vacation spot tackle that can obtain the spent cash.

DESTADDR=$(bitcoin-cli -regtest getnewaddress)

then we calculate the change quantity after deducting a charge ( lets take 0.0002 bitcoins, additionally discover that this command can change relying in your working system and settings, im on Ubuntu )

CHANGE=$(echo "$AMOUNT - 0.0002" | bc | LC_ALL=C awk '{printf "%.8f", $0}')

then we create the uncooked transaction

RAW_TX=$(bitcoin-cli -regtest createrawtransaction "[{"txid":"$TXID","vout":$VOUT}]" "{"$DESTADDR":$CHANGE}")

we signal it utilizing the personal key of two of our personal keys

PRIVKEY1=$(bitcoin-cli -regtest getaddressinfo $ADDR1 | jq -r '.desc' | sed -e 's/^.*(#)/1/' -e 's/]//')
PRIVKEY2=$(bitcoin-cli -regtest getaddressinfo $ADDR2 | jq -r '.desc' | sed -e 's/^.*(#)/1/' -e 's/]//')



SIGNED_TX=$(bitcoin-cli -regtest signrawtransactionwithkey $RAW_TX "["$PRIVKEY1","$PRIVKEY2"]" "[ jq -r '.redeemScript')","quantity":$AMOUNT]")

then we ship it

bitcoin-cli -regtest sendrawtransaction $(echo $SIGNED_TX | jq -r '.hex')

and finaly we generate block to substantiate our transaction

bitcoin-cli -regtest generatetoaddress 1 "$(bitcoin-cli -regtest getnewaddress)"

LEAVE A REPLY

Please enter your comment!
Please enter your name here