まずアドレスの作成
from ecc import PrivateKey from helper import hash256, little_endian_to_int secret = little_endian_to_int(hash256(b'hpscript secret')) private_key = PrivateKey(secret) print(private_key.point.address(testnet=True))
mtWT4Tga6ru2HXokJJYSp3wJKGXukTRaFk
「testnet bitcoin faucet」で検索して出てきた以下のサイトからテストネットのコインを受け取ります。
https://coinfaucet.eu/en/btc-testnet/
—
We sent 0.01947729 bitcoins to address
mtWT4Tga6ru2HXokJJYSp3wJKGXukTRaFk
tx: 7c671e8faaab0569acee26d76ae158adcd8f4806ec08c480d596e2aa78095026
FEES 0.00017 BTC
—-
1つのUTXOの60%を指定したアドレス「mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv」に送付する
残額から手数料を引いた額をお釣り用のアドレスに送る必要があるため、もう一つ、お釣り用の bitcoin testnetのアドレスを作ります。hpscript blog
mmEjxM1RvKquR6FvUNibQe8ah4FFkphrdZ
from ecc import PrivateKey from helper import decode_base58, SIGHASH_ALL from script import p2pkh_script, Script from tx import TxIn, TxOut, Tx prev_tx = bytes.fromhex('7c671e8faaab0569acee26d76ae158adcd8f4806ec08c480d596e2aa78095026') prev_index = 1 target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv' target_amount = 0.01 change_address = 'mtWT4Tga6ru2HXokJJYSp3wJKGXukTRaFk' change_amount = 0.009 secret = 21059908846034780412291472495725985072617958337763972172781872669894507067750 priv = PrivateKey(secret=secret) tx_ins = [] tx_ins.append(TxIn(prev_tx, prev_index)) tx_outs = [] h160 = decode_base58(target_address) script_pubkey = p2pkh_script(h160) target_satoshis = int(target_amount*100000000) tx_outs.append(TxOut(target_satoshis, script_pubkey)) h160 = decode_base58(change_address) script_pubkey = p2pkh_script(h160) change_satoshis = int(target_amount*100000000) tx_outs.append(TxOut(change_satoshis, script_pubkey)) tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True) print(tx_obj.sign_input(0, priv)) print(tx_obj.serialize().hex())
TxOutは配列で持っておき、amountとscript_pubkeyのセットをoutsごとにappendしていく。
$ python3 main.py
True
010000000126500978aae296d580c408ec06488fcdad58e16ad726eeac6905abaa8f1e677c010000006a473044022010b3ba160737a3a84a06cb431acc7b72393947c686af9fab83e6e5169dc4b58d02200233d9a6b4b3c93ddfbfb730fdc1b15563f2822a09b335f976f0f0abf7cc4c1701210284d4b1384aa13140d99f36a4143adf74c8d487dd13acc6d17def7081c2034cd2ffffffff0240420f00000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac40420f00000000001976a9148e813b482fc151baaf0c6b254aa02d10ee49ad5888ac00000000
### 2つのinputs, 1つのoutputsの場合
同じアドレスでfaucetからbitcoinを取得しようとすると、7hour後となる。
from ecc import PrivateKey from helper import decode_base58, SIGHASH_ALL from script import p2pkh_script, Script from tx import TxIn, TxOut, Tx prev_tx_1 = bytes.fromhex('11d05ce707c1120248370d1cbf5561d22c4f83aeba04367\ 92c82e0bd57fe2a2f') prev_index_1 = 1 prev_tx_2 = bytes.fromhex('51f61f77bd061b9a0da60d4bedaaf1b1fad0c11e65fdc74\ 4797ee22d20b03d15') prev_index_2 = 1 target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv' target_amount = 0.0429 secret = 8675309 priv = PrivateKey(secret=secret) tx_ins = [] tx_ins.append(TxIn(prev_tx_1, prev_index_1)) tx_ins.append(TxIn(prev_tx_2, prev_index_2)) tx_outs = [] h160 = decode_base58(target_address) script_pubkey = p2pkh_script(h160) target_satoshis = int(target_amount*100000000) tx_outs.append(TxOut(target_satoshis, script_pubkey)) tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True) print(tx_obj.sign_input(0, priv)) print(tx_obj.serialize().hex())
$ python3 main.py
True
01000000022f2afe57bde0822c793604baae834f2cd26155bf1c0d37480212c107e75cd011010000006a47304402204cc5fe11b2b025f8fc9f6073b5e3942883bbba266b71751068badeb8f11f0364022070178363f5dea4149581a4b9b9dbad91ec1fd990e3fa14f9de3ccb421fa5b269012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff153db0202de27e7944c7fd651ec1d0fab1f1aaed4b0da60d9a1b06bd771ff6510100000000ffffffff01d0754100000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000
inputsが複数の場合は、tx_ins[]にappendしていく。