def test_send_mixed_inputs(self, client): # First is non-segwit, second is segwit. inp1 = proto.TxInputType( address_n=parse_path("44'/156'/11'/0/0"), amount=38448607, prev_hash=TXHASH_db77c2, prev_index=0, script_type=proto.InputScriptType.SPENDADDRESS, ) inp2 = proto.TxInputType( address_n=parse_path("49'/156'/0'/1/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDP2SHWITNESS, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=38448607 + 1252382934 - 1000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) with client: _, serialized_tx = btc.sign_tx(client, "Bgold", [inp1, inp2], [out1], prev_txes=TX_API) assert ( btc_hash(serialized_tx)[::-1].hex() == "2c64109fba890657e37f0782efda29bbc277dfd521658f185d302ddffcacffd2")
def test_spend_lelantus(client): inp1 = messages.TxInputType( # THgGLVqfzJcaxRVPWE5fd8YJ1GpVePq2Uk address_n=parse_path("m/44'/1'/0'/0/4"), amount=1_000_000_000, prev_hash=TXHASH_8a34cc, prev_index=0, ) out1 = messages.TxOutputType( # m/44'/1'/0'/0/5 address="TPypFKi3aziXmiH1MiwagaK71apv5XARGY", amount=1_000_000_000 - 1_000, script_type=messages.OutputScriptType.PAYTOADDRESS, ) _, serialized_tx = btc.sign_tx( client, "Firo Testnet", [inp1], [out1], prev_txes=TX_API ) assert ( serialized_tx.hex() == "010000000125d6f69fdb2e1715a3eb9c71a60ecb7178b3af4033309843d18f13afcecc348a000000006a47304402207b490135583a2ac6650806c706dfd15954f9ac85b64a75d7264653e4b1cd4e29022052946b28f97a415bd0b2b02c3a71ac8cb26f9a9387ac82856b4c7116848d090c01210313a443e806f25052ac7363adc689fcfa72893f2a51a35ab5e096ed5e6cd8517effffffff0118c69a3b000000001976a91499af2ecbf5892079e0297c59b91981b067da36a988ac00000000" ) # accepted by network: https://testblockbook.firo.org/tx/866bc7041989ad038e5b38b7577325d015b67238ea9387cde6ba837fff4a61be assert ( btc_hash(serialized_tx)[::-1].hex() == "866bc7041989ad038e5b38b7577325d015b67238ea9387cde6ba837fff4a61be" )
def test_lots_of_inputs(self): self.setup_mnemonic_nopin_nopassphrase() # Tests if device implements serialization of len(inputs) correctly # tx 4a7b7e0403ae5607e473949cfa03f09f2cd8b0f404bf99ce10b7303d86280bf7 : 100 UTXO for spending for unit tests inputs = [] for i in range(100): inputs.append( proto.TxInputType( address_n=[4], # 1NwN6UduuVkJi6sw3gSiKZaCY5rHgVXC2h prev_hash=TXHASH_4a7b7e, prev_index=i, ) ) out = proto.TxOutputType( address="19dvDdyxxptP9dGvozYe8BP6tgFV9L4jg5", amount=100 * 26000 - 15 * 10000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) _, serialized_tx = check_sign_tx( self.client, "Bitcoin", inputs, [out], unknown_path=True ) # Accepted by network: tx 23d9d8eecf3abf6c0f0f3f8b0976a04792d7f1c9a4ea9b0a8931734949e27c92 # too big put in unit test, only check hash assert ( btc_hash(serialized_tx)[::-1].hex() == "23d9d8eecf3abf6c0f0f3f8b0976a04792d7f1c9a4ea9b0a8931734949e27c92" )
def test_send_btg_external_presigned(self, client): inp1 = proto.TxInputType( address_n=parse_path("44'/156'/0'/1/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDADDRESS, ) inp2 = proto.TxInputType( # address_n=parse_path("49'/156'/0'/0/0"), # AXibjT5r96ZaVA8Lu4BQZocdTx7p5Ud8ZP amount=58456, prev_hash=TXHASH_f55c5b, prev_index=0, script_type=proto.InputScriptType.EXTERNAL, script_sig=bytes.fromhex("1600147c5edda9b293db2c8894b9d81efd77764910c445"), witness=bytes.fromhex( "024730440220091eece828409b3a9aa92dd2f9b032f9fb3a12b21b323a3fdea3cb18d08249af022065412107afcf76b0d28b90188c802f8f17b41790ed81c868d0ee23f1dd2ec53441210386789a34fe1a49bfc3e174adc6706c6222b0d80de76b884a0e3d32f8e9c4dc3e" ), ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=1252382934 + 58456 - 1000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) with client: client.set_expected_responses( [ request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(1), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(1), request_meta(TXHASH_f55c5b), request_input(0, TXHASH_f55c5b), request_output(0, TXHASH_f55c5b), request_output(1, TXHASH_f55c5b), request_input(0), request_input(1), request_output(0), request_input(1), request_finished(), ] ) _, serialized_tx = btc.sign_tx( client, "Bgold", [inp1, inp2], [out1], prev_txes=TX_API ) assert ( btc_hash(serialized_tx)[::-1].hex() == "95ebe5cdfb8dc3c112eb0107fc3bd7701689ac5ec4a74a3d12e203333d0832d3" )
def serialize(node, version=0x0488B21E): s = b"" s += struct.pack(">I", version) s += struct.pack(">B", node.depth) s += struct.pack(">I", node.fingerprint) s += struct.pack(">I", node.child_num) s += node.chain_code if node.private_key: s += b"\x00" + node.private_key else: s += node.public_key s += tools.btc_hash(s)[:4] return tools.b58encode(s)
def test_send_bitcoin_gold_nochange(self, client): inp1 = proto.TxInputType( address_n=parse_path("44'/156'/0'/1/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDADDRESS, ) inp2 = proto.TxInputType( address_n=parse_path("44'/156'/0'/0/1"), # 1LRspCZNFJcbuNKQkXgHMDucctFRQya5a3 amount=38448607, prev_hash=TXHASH_db77c2, prev_index=0, script_type=proto.InputScriptType.SPENDADDRESS, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=1252382934 + 38448607 - 1000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) with client: client.set_expected_responses( [ request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(1), request_meta(TXHASH_db77c2), request_input(0, TXHASH_db77c2), request_input(1, TXHASH_db77c2), request_output(0, TXHASH_db77c2), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(0), request_input(1), request_output(0), request_finished(), ] ) _, serialized_tx = btc.sign_tx( client, "Bgold", [inp1, inp2], [out1], prev_txes=TX_API ) assert ( btc_hash(serialized_tx)[::-1].hex() == "ac9d452b900eb747d3137e1f3044bb0f46efaeb6e0fc8c27b02d1d08d238a904" )
def test_send_p2sh(self, client): inp1 = proto.TxInputType( address_n=parse_path("49'/156'/0'/1/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDP2SHWITNESS, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=12300000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) out2 = proto.TxOutputType( address="GZFLExxrvWFuFT1xRzhfwQWSE2bPDedBfn", script_type=proto.OutputScriptType.PAYTOADDRESS, amount=1252382934 - 11000 - 12300000, ) with client: client.set_expected_responses( [ request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(0), request_output(0), request_output(1), request_input(0), request_finished(), ] ) _, serialized_tx = btc.sign_tx( client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API ) assert ( btc_hash(serialized_tx)[::-1].hex() == "d5732fc8a594ae3b7ba695d7b276b2186f8572b0eb157120e0ba35d3511c6060" )
def test_send_p2sh_witness_change(self, client): inp1 = proto.TxInputType( address_n=parse_path("49'/156'/0'/1/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDP2SHWITNESS, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=12300000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) out2 = proto.TxOutputType( address_n=parse_path("49'/156'/0'/1/0"), script_type=proto.OutputScriptType.PAYTOP2SHWITNESS, amount=1252382934 - 11000 - 12300000, ) with client: client.set_expected_responses( [ request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.SignTx), request_input(0), request_output(0), request_output(1), request_input(0), request_finished(), ] ) _, serialized_tx = btc.sign_tx( client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API ) assert ( btc_hash(serialized_tx)[::-1].hex() == "eed4ef86a408602e35ae416591f349847db38cdaddef1429a9bb0e39520d100d" )
def test_send_bitcoin_gold_change(self, client): inp1 = proto.TxInputType( address_n=parse_path("44'/156'/0'/0/0"), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDADDRESS, ) out1 = proto.TxOutputType( address_n=parse_path("44'/156'/0'/1/0"), amount=1896050, script_type=proto.OutputScriptType.PAYTOADDRESS, ) out2 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=1252382934 - 1896050 - 1000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) with client: client.set_expected_responses( [ request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_output(0), request_output(1), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(0), request_output(0), request_output(1), request_finished(), ] ) _, serialized_tx = btc.sign_tx( client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API ) assert ( btc_hash(serialized_tx)[::-1].hex() == "39a0716c361610724c7c40916baa20808cbdd7538b6c38689ce80cb73e7f51d1" )
def deserialize(xpub): data = tools.b58decode(xpub, None) if tools.btc_hash(data[:-4])[:4] != data[-4:]: raise ValueError("Checksum failed") node = messages.HDNodeType() node.depth = struct.unpack(">B", data[4:5])[0] node.fingerprint = struct.unpack(">I", data[5:9])[0] node.child_num = struct.unpack(">I", data[9:13])[0] node.chain_code = data[13:45] key = data[45:-4] if key[0] == 0: node.private_key = key[1:] else: node.public_key = key return node
def test_send_multisig_1(self, client): nodes = [ btc.get_public_node(client, parse_path(f"49'/156'/{i}'"), coin_name="Bgold").node for i in range(1, 4) ] multisig = proto.MultisigRedeemScriptType(nodes=nodes, address_n=[1, 0], signatures=[b"", b"", b""], m=2) inp1 = proto.TxInputType( address_n=parse_path("49'/156'/1'/1/0"), prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDP2SHWITNESS, multisig=multisig, amount=1252382934, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=1252382934 - 1000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) with client: client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_input(0), request_finished(), ]) signatures, _ = btc.sign_tx(client, "Bgold", [inp1], [out1], prev_txes=TX_API) # store signature inp1.multisig.signatures[0] = signatures[0] # sign with third key inp1.address_n[2] = H_(3) client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_input(0), request_finished(), ]) _, serialized_tx = btc.sign_tx(client, "Bgold", [inp1], [out1], prev_txes=TX_API) assert ( btc_hash(serialized_tx)[::-1].hex() == "efa5b21916ac7ea5316c38b2d7d5520d80cbe563c58304f956ea6ddb241001d1")
def test_send_btg_multisig_change(self, client): nodes = [ btc.get_public_node(client, parse_path(f"48'/156'/{i}'"), coin_name="Bgold").node for i in range(1, 4) ] def getmultisig(chain, nr, signatures=[b"", b"", b""], nodes=nodes): return proto.MultisigRedeemScriptType(nodes=nodes, address_n=[chain, nr], signatures=signatures, m=2) inp1 = proto.TxInputType( address_n=parse_path("48'/156'/3'/0/0"), multisig=getmultisig(0, 0), # 33Ju286QvonBz5N1V754ZekQv4GLJqcc5R amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDMULTISIG, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=24000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) out2 = proto.TxOutputType( address_n=parse_path("48'/156'/3'/1/0"), multisig=getmultisig(1, 0), script_type=proto.OutputScriptType.PAYTOMULTISIG, amount=1252382934 - 24000 - 1000, ) with client: client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_output(1), request_finished(), ]) signatures, serialized_tx = btc.sign_tx(client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API) assert ( signatures[0].hex() == "30440220271c46ef3d7c37bd499804128623da3aa0014f3b4447dd39c4573b23c4537f6902205329167b9eb48427af62b04bf5138295f4c38613f6ba955934d15c499bff0d8a" ) inp1 = proto.TxInputType( address_n=parse_path("48'/156'/1'/0/0"), multisig=getmultisig(0, 0, [b"", b"", signatures[0]]), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDMULTISIG, ) out2.address_n[2] = H_(1) with client: client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_output(1), request_finished(), ]) signatures, serialized_tx = btc.sign_tx(client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API) assert ( signatures[0].hex() == "3045022100bdf8c4bd14217c183612c7e8f79691f6479bcb35aa403f77d70182f58633ac6e02205b10a79b7894bb40248a120c8fcb7096a46950593e4c5b48a20bc5ebe53d85fa" ) assert ( btc_hash(serialized_tx)[::-1].hex() == "b45ba21b69e1e1c6ad79f5408fe70d92fb861742d239cb6a952213f60c253e40")
def test_send_btg_multisig_change(self, client): nodes = [ btc.get_public_node(client, parse_path(f"48'/156'/{i}'/0'"), coin_name="Bgold").node for i in range(1, 4) ] EMPTY_SIGS = [b"", b"", b""] def getmultisig(chain, nr, signatures): return proto.MultisigRedeemScriptType(nodes=nodes, address_n=[chain, nr], signatures=signatures, m=2) inp1 = proto.TxInputType( address_n=parse_path("48'/156'/3'/0'/0/0"), multisig=getmultisig(0, 0, EMPTY_SIGS), # 33Ju286QvonBz5N1V754ZekQv4GLJqcc5R amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDMULTISIG, ) out1 = proto.TxOutputType( address="GfDB1tvjfm3bukeoBTtfNqrJVFohS2kCTe", amount=24000, script_type=proto.OutputScriptType.PAYTOADDRESS, ) out2 = proto.TxOutputType( address_n=parse_path("48'/156'/3'/0'/1/0"), multisig=getmultisig(1, 0, EMPTY_SIGS), script_type=proto.OutputScriptType.PAYTOMULTISIG, amount=1252382934 - 24000 - 1000, ) with client: client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_output(1), request_finished(), ]) signatures, serialized_tx = btc.sign_tx(client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API) assert ( signatures[0].hex() == "30440220263c427e6e889c161206edee39b9b969350c154ddd8eb76d2ab8ca8e0fc083b702200fb1d0ef430fa2d0293dcbb0b237775d4f9748222a6ed9fc3ff747837b99020a" ) inp1 = proto.TxInputType( address_n=parse_path("48'/156'/1'/0'/0/0"), multisig=getmultisig(0, 0, [b"", b"", signatures[0]]), amount=1252382934, prev_hash=TXHASH_25526b, prev_index=0, script_type=proto.InputScriptType.SPENDMULTISIG, ) out2.address_n[2] = H_(1) with client: client.set_expected_responses([ request_input(0), request_output(0), proto.ButtonRequest(code=B.ConfirmOutput), request_output(1), proto.ButtonRequest(code=B.SignTx), request_input(0), request_meta(TXHASH_25526b), request_input(0, TXHASH_25526b), request_output(0, TXHASH_25526b), request_output(1, TXHASH_25526b), request_input(0), request_output(0), request_output(1), request_finished(), ]) signatures, serialized_tx = btc.sign_tx(client, "Bgold", [inp1], [out1, out2], prev_txes=TX_API) assert ( signatures[0].hex() == "3045022100c9094b060b4b095e78403493912b0e06ca12ffbdc0f2fbeec20b02d7eaa73f8702206813e33e04a2b9c4493ecfa2024f2e9d69b5a2ab5c10433d9ab762add5bdde27" ) assert ( btc_hash(serialized_tx)[::-1].hex() == "2677130ec0c5eea2249787fe17b85770cfb35dfce550830a7fb6c6acd9375114")
def create_inputs(self, numinputs, txsize): idx = 0 sum = 0 self.inputs = [] self.txs = {} for nr in range(numinputs): t = proto_types.TransactionType() t.version = 1 t.lock_time = 0 i = t.inputs.add() i.prev_hash = os.urandom(32) i.prev_index = random.randint(0, 4) i.script_sig = os.urandom(100) i.sequence = 0xffffffff if nr % 50 == 0: print(nr) myout = random.randint(0, txsize - 1) segwit = random.randint(0, 2) for vout in range(txsize): o = t.bin_outputs.add() o.amount = random.randint(10000, 1000000) if vout == myout: amount = o.amount sum = sum + o.amount node = self.node path = [0, idx] node = bip32.public_ckd(node, path) idx = idx + 1 pubkey = tools.hash_160(node.public_key) else: pubkey = os.urandom(20) if segwit == 2: # p2sh segwit o.script_pubkey = b'\xa9\x14' + hash160(b'\x00\x14' + pubkey) + b'\x87' elif segwit == 1: o.script_pubkey = b'\x00\x14' + pubkey else: o.script_pubkey = b'\x76\xa9\x14' + pubkey + b'\x88\xac' txser = self.serialize_tx(t) txhash = tools.btc_hash(txser)[::-1] self.inputs.append( proto_types.TxInputType( address_n=self.client.expand_path("44'/0'/0'/0/%d" % idx), script_type=(proto_types.SPENDWITNESS if segwit == 1 else proto_types.SPENDP2SHWITNESS if segwit == 2 else proto_types.SPENDADDRESS), prev_hash=txhash, prev_index=myout, amount=amount if segwit > 0 else 0)) # print(binascii.hexlify(txser)) # print(binascii.hexlify(txhash)) self.txs[binascii.hexlify(txhash)] = t self.outputs = [ proto_types.TxOutputType( amount=sum, script_type=proto_types.PAYTOADDRESS, address_n=self.client.expand_path("44'/0'/0'/1/0")) ]