1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

tools/signtest: Added P2SH segwit inputs

This commit is contained in:
Jochen Hoenicke 2016-07-04 01:58:18 +02:00 committed by Pavol Rusnak
parent 6b17bf179b
commit 08511d3f81
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -16,6 +16,11 @@ from trezorlib.tx_api import TXAPIBitcoin
from trezorlib.transport_hid import HidTransport from trezorlib.transport_hid import HidTransport
from trezorlib.transport_bridge import BridgeTransport from trezorlib.transport_bridge import BridgeTransport
def hash160(x):
h = hashlib.new("ripemd160")
h.update(hashlib.sha256(x).digest())
return h.digest()
def pack_varint(x): def pack_varint(x):
if (x < 0xfd): if (x < 0xfd):
return chr(x) return chr(x)
@ -81,7 +86,7 @@ class MyTXAPIBitcoin(object):
if (nr % 50 == 0): if (nr % 50 == 0):
print(nr) print(nr)
myout = random.randint(0, txsize-1) myout = random.randint(0, txsize-1)
segwit = 1 #random.randint(0,1) segwit = random.randint(0,2)
for vout in range(txsize): for vout in range(txsize):
o = t.bin_outputs.add() o = t.bin_outputs.add()
o.amount = random.randint(10000,1000000) o.amount = random.randint(10000,1000000)
@ -95,29 +100,25 @@ class MyTXAPIBitcoin(object):
pubkey = tools.hash_160(node.public_key) pubkey = tools.hash_160(node.public_key)
else: else:
pubkey = os.urandom(20) pubkey = os.urandom(20)
if (segwit): if (segwit == 2):
o.script_pubkey = binascii.unhexlify('0014') + pubkey # 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: else:
o.script_pubkey = binascii.unhexlify('76a914') + pubkey + binascii.unhexlify('88ac') o.script_pubkey = b'\x76\xa9\x14' + pubkey + b'\x88\xac'
txser = self.serialize_tx(t) txser = self.serialize_tx(t)
txhash = tools.Hash(txser)[::-1] txhash = tools.Hash(txser)[::-1]
if (segwit):
outi = self.inputs.append( outi = self.inputs.append(
proto_types.TxInputType( proto_types.TxInputType(
address_n=self.client.expand_path("44'/0'/0'/0/"+str(idx)), address_n=self.client.expand_path("44'/0'/0'/0/"+str(idx)),
script_type = proto_types.SPENDWADDRESS, script_type = (proto_types.SPENDWADDRESS if segwit == 1 else
proto_types.SPENDP2SHWADDRESS if segwit == 2 else
proto_types.SPENDADDRESS),
prev_hash=txhash, prev_hash=txhash,
prev_index = myout, prev_index = myout,
amount = amount amount = amount if segwit > 0 else 0
))
else:
outi = self.inputs.append(
proto_types.TxInputType(
address_n=self.client.expand_path("44'/0'/0'/0/"+str(idx)),
script_type = proto_types.SPENDADDRESS,
prev_hash=txhash,
prev_index = myout
)) ))
#print(binascii.hexlify(txser)) #print(binascii.hexlify(txser))
#print(binascii.hexlify(txhash)) #print(binascii.hexlify(txhash))
@ -142,7 +143,7 @@ class MyTXAPIBitcoin(object):
return t return t
def main(): def main():
numinputs = 600 numinputs = 100
sizeinputtx = 10 sizeinputtx = 10
# List all connected TREZORs on USB # List all connected TREZORs on USB