비트코인 PSBT
PSBT (Partially Signed Bitcoin Transaction) 샘플
PSBT는 흔히 MultiSig 서명 및 Airgapped 서명 등에 사용되는데, 아래는 아주 간단한 샘플로서 단일 UXTO를 사용해서 하나의 비트코인 주소(TestNet)로 보내는 것을 예시한 것이다.
PSBT 생성 (createpsbt)
PSBT의 처음 단계는 어떤 UTXO를 사용해서 누구에게 얼마를 보낼 지를 지정하면서 새 PSBT 를 생성하는 작업이다. 아래는 TXID: d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf, VOut Index: 0 이라는 UTXO를 소비(spend)해서 tb1qqleu24tzsa5vlzfrfg7n3wqaxwgwmpgrrnkagw 수신 주소로 0.00018 BTC를 보내기 위한 PSBT 트랜잭션을 생성하는 예이다.
$ bitcoin-cli createpsbt "[{\"txid\":\"d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf\",\"vout\":0}]" "[{\"tb1qqleu24tzsa5vlzfrfg7n3wqaxwgwmpgrrnkagw\":\"0.00018\"}]" cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAAA $ bitcoin-cli decodepsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAAA { "tx": { "txid": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "hash": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "version": 2, "size": 82, "vsize": 82, "weight": 328, "locktime": 0, "vin": [ { "txid": "d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf", "vout": 0, "scriptSig": { "asm": "", "hex": "" }, "sequence": 4294967295 } ], "vout": [ { "value": 0.00018000, "n": 0, "scriptPubKey": { "asm": "0 07f3c555628768cf89234a3d38b81d3390ed8503", "hex": "001407f3c555628768cf89234a3d38b81d3390ed8503", "reqSigs": 1, "type": "witness_v0_keyhash", "addresses": [ "tb1qqleu24tzsa5vlzfrfg7n3wqaxwgwmpgrrnkagw" ] } } ] }, "unknown": { }, "inputs": [ { } ], "outputs": [ { } ] }
Segiwit UTXO 정보 수정 (utxoupdatepsbt)
utxoupdatepsbt는 PSBT안의 segwit Input/Output에 대한 정보를 추가한다.
$ bitcoin-cli utxoupdatepsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAAA cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEBHyBOAAAAAAAAFgAUH7OmCviKKDXHHbWLetC0hpnSLZMAAA== $ bitcoin-cli decodepsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEBHyBOAAAAAAAAFgAUH7OmCviKKDXHHbWLetC0hpnSLZMAAA== { "tx": { "txid": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "hash": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "version": 2, "size": 82, "vsize": 82, "weight": 328, "locktime": 0, "vin": [ { "txid": "d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf", "vout": 0, "scriptSig": { "asm": "", "hex": "" }, "sequence": 4294967295 } ], "vout": [ { "value": 0.00018000, "n": 0, "scriptPubKey": { "asm": "0 07f3c555628768cf89234a3d38b81d3390ed8503", "hex": "001407f3c555628768cf89234a3d38b81d3390ed8503", "reqSigs": 1, "type": "witness_v0_keyhash", "addresses": [ "tb1qqleu24tzsa5vlzfrfg7n3wqaxwgwmpgrrnkagw" ] } } ] }, "unknown": { }, "inputs": [ { "witness_utxo": { "amount": 0.00020000, "scriptPubKey": { "asm": "0 1fb3a60af88a2835c71db58b7ad0b48699d22d93", "hex": "00141fb3a60af88a2835c71db58b7ad0b48699d22d93", "type": "witness_v0_keyhash", "address": "tb1qr7e6vzhc3g5rt3cakk9h4595s6vaytvnu4ke0h" } } } ], "outputs": [ { } ], "fee": 0.00002000 }
PSBT 서명 처리 (walletprocesspsbt)
walletprocesspsbt은 PSBT에 필요한 정보를 추가 수정하고 자신이 가진 키를 사용하여 서명한다. MultiSig의 경우 여러 디바이스에서 서명을 따로 받아 진행할 수 있다. Non-Segwit의 경우 final_scriptsig, Segwit의 경우 final_scriptwitness에 서명을 저장한다.
$ bitcoin-cli walletprocesspsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEBHyBOAAAAAAAAFgAUH7OmCviKKDXHHbWLetC0hpnSLZMAAA== { "psbt": "cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEAcQIAAAABIC+e0U1jzKS9GUQi6ABgSvdWdrfDIAHIp562O8sOvz8AAAAAAP7///8CIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItk4kyAQAAAAAAFgAUUpegVZpLrV/f3uRy01Vdz/MLdKLJMSUAAQEfIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItkwEIawJHMEQCIBuh9p75xECi4VgxJliZ9vw1hUAKSxWgR7BMT14Ujxu3AiAIcpvYGyvcwbwhTEujXQFK+6GYC812S/Gq5/LkxzImAAEhA/JXC52M8fWBa+hU9TJQWYlMX49fXxQRi+hR9o4piWB9ACICAin7KtO48Opy903ibM2Leke6L9kmacWQzSjoT7/0JYXrEJ0UVu8AAACAAAAAgEMAAIAA", "complete": true } $ bitcoin-cli decodepsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEAcQIAAAABIC+e0U1jzKS9GUQi6ABgSvdWdrfDIAHIp562O8sOvz8AAAAAAP7///8CIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItk4kyAQAAAAAAFgAUUpegVZpLrV/f3uRy01Vdz/MLdKLJMSUAAQEfIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItkwEIawJHMEQCIBuh9p75xECi4VgxJliZ9vw1hUAKSxWgR7BMT14Ujxu3AiAIcpvYGyvcwbwhTEujXQFK+6GYC812S/Gq5/LkxzImAAEhA/JXC52M8fWBa+hU9TJQWYlMX49fXxQRi+hR9o4piWB9ACICAin7KtO48Opy903ibM2Leke6L9kmacWQzSjoT7/0JYXrEJ0UVu8AAACAAAAAgEMAAIAA { "tx": { "txid": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "hash": "0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0", "version": 2, "size": 82, "vsize": 82, "weight": 328, "locktime": 0, "vin": [ { "txid": "d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf", "vout": 0, "scriptSig": { "asm": "", "hex": "" }, "sequence": 4294967295 } ], "vout": [ { "value": 0.00018000, "n": 0, "scriptPubKey": { "asm": "0 07f3c555628768cf89234a3d38b81d3390ed8503", "hex": "001407f3c555628768cf89234a3d38b81d3390ed8503", "reqSigs": 1, "type": "witness_v0_keyhash", "addresses": [ "tb1qqleu24tzsa5vlzfrfg7n3wqaxwgwmpgrrnkagw" ] } } ] }, "unknown": { }, "inputs": [ { "witness_utxo": { "amount": 0.00020000, "scriptPubKey": { "asm": "0 1fb3a60af88a2835c71db58b7ad0b48699d22d93", "hex": "00141fb3a60af88a2835c71db58b7ad0b48699d22d93", "type": "witness_v0_keyhash", "address": "tb1qr7e6vzhc3g5rt3cakk9h4595s6vaytvnu4ke0h" } }, "non_witness_utxo": { "txid": "d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf", "hash": "d62f2643f6eb2f4a8b31d2aeafefc628931ccc246a14d56abeb08e675f29d7bf", "version": 2, "size": 113, "vsize": 113, "weight": 452, "locktime": 2437577, "vin": [ { "txid": "3fbf0ecb3bb69ea7c80120c3b77656f74a6000e8224419bda4cc634dd19e2f20", "vout": 0, "scriptSig": { "asm": "", "hex": "" }, "sequence": 4294967294 } ], "vout": [ { "value": 0.00020000, "n": 0, "scriptPubKey": { "asm": "0 1fb3a60af88a2835c71db58b7ad0b48699d22d93", "hex": "00141fb3a60af88a2835c71db58b7ad0b48699d22d93", "reqSigs": 1, "type": "witness_v0_keyhash", "addresses": [ "tb1qr7e6vzhc3g5rt3cakk9h4595s6vaytvnu4ke0h" ] } }, { "value": 0.00078473, "n": 1, "scriptPubKey": { "asm": "0 5297a0559a4bad5fdfdee472d3555dcff30b74a2", "hex": "00145297a0559a4bad5fdfdee472d3555dcff30b74a2", "reqSigs": 1, "type": "witness_v0_keyhash", "addresses": [ "tb1q22t6q4v6fwk4lh77u3edx42aeleska9zlqtlqe" ] } } ] }, "final_scriptwitness": [ "304402201ba1f69ef9c440a2e15831265899f6fc3585400a4b15a047b04c4f5e148f1bb7022008729bd81b2bdcc1bc214c4ba35d014afba1980bcd764bf1aae7f2e4c732260001", "03f2570b9d8cf1f5816be854f5325059894c5f8f5f5f14118be851f68e2989607d" ] } ], "outputs": [ { "bip32_derivs": [ { "pubkey": "0229fb2ad3b8f0ea72f74de26ccd8b7a47ba2fd92669c590cd28e84fbff42585eb", "master_fingerprint": "9d1456ef", "path": "m/0'/0'/67'" } ] } ], "fee": 0.00002000 }
PSBT 완성 (finalizepsbt)
finalizepsbt는 서명이 완료된 후에 Raw Transaction을 생성한다.
$ bitcoin-cli finalizepsbt cHNidP8BAFICAAAAAb/XKV9njrC+atUUaiTMHJMoxu+vrtIxi0ov6/ZDJi/WAAAAAAD/////AVBGAAAAAAAAFgAUB/PFVWKHaM+JI0o9OLgdM5DthQMAAAAAAAEAcQIAAAABIC+e0U1jzKS9GUQi6ABgSvdWdrfDIAHIp562O8sOvz8AAAAAAP7///8CIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItk4kyAQAAAAAAFgAUUpegVZpLrV/f3uRy01Vdz/MLdKLJMSUAAQEfIE4AAAAAAAAWABQfs6YK+IooNccdtYt60LSGmdItkwEIawJHMEQCIBuh9p75xECi4VgxJliZ9vw1hUAKSxWgR7BMT14Ujxu3AiAIcpvYGyvcwbwhTEujXQFK+6GYC812S/Gq5/LkxzImAAEhA/JXC52M8fWBa+hU9TJQWYlMX49fXxQRi+hR9o4piWB9ACICAin7KtO48Opy903ibM2Leke6L9kmacWQzSjoT7/0JYXrEJ0UVu8AAACAAAAAgEMAAIAA { "hex": "02000000000101bfd7295f678eb0be6ad5146a24cc1c9328c6efafaed2318b4a2febf643262fd60000000000ffffffff01504600000000000016001407f3c555628768cf89234a3d38b81d3390ed85030247304402201ba1f69ef9c440a2e15831265899f6fc3585400a4b15a047b04c4f5e148f1bb7022008729bd81b2bdcc1bc214c4ba35d014afba1980bcd764bf1aae7f2e4c7322600012103f2570b9d8cf1f5816be854f5325059894c5f8f5f5f14118be851f68e2989607d00000000", "complete": true }
완성된 트랜잭션 보내기 (sendrawtransaction)
PSBT가 완료되어 Raw Transaction 을 얻은 후, sendrawtransaction 을 사용하여 비트코인 네트워크에 Broadcast 한다.
$ bitcoin-cli sendrawtransaction 02000000000101bfd7295f678eb0be6ad5146a24cc1c9328c6efafaed2318b4a2febf643262fd60000000000ffffffff01504600000000000016001407f3c555628768cf89234a3d38b81d3390ed85030247304402201ba1f69ef9c440a2e15831265899f6fc3585400a4b15a047b04c4f5e148f1bb7022008729bd81b2bdcc1bc214c4ba35d014afba1980bcd764bf1aae7f2e4c7322600012103f2570b9d8cf1f5816be854f5325059894c5f8f5f5f14118be851f68e2989607d00000000 0406880f8a8f7e38f938551bd3c105b69584051663efd4780686d48310139ce0