1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-28 00:58:09 +00:00

txapi cleanup

This commit is contained in:
Pavol Rusnak 2014-03-29 21:31:17 +01:00
parent 7a1e2827c8
commit 49eec8b78e

View File

@ -12,6 +12,33 @@ except:
import types_pb2 as proto_types
def op_push(i):
if i<0x4c:
return chr(i)
elif i<0xff:
return '\x4c' + chr(i)
elif i<0xffff:
return '\x4d' + struct.pack("<H", i)
else:
return '\x4e' + struct.pack("<I", i)
def opcode_serialize(opcode):
# TODO: this function supports just small subset of script for now (enough for most transactions)
if opcode == 'OP_DUP':
return '\x76'
if opcode == 'OP_HASH160':
return '\xa9'
if opcode == 'OP_EQUALVERIFY':
return '\x88'
if opcode == 'OP_CHECKSIG':
return '\xac'
# it's probably hex data
try:
x = binascii.unhexlify(opcode)
return op_push(len(x)) + x
except:
raise Exception('Unknown script opcode: %s' % opcode)
def bitcore_tx(url):
f = urllib2.urlopen(url)
data = json.load(f)
@ -24,17 +51,16 @@ def bitcore_tx(url):
i = t.inputs.add()
i.prev_hash = binascii.unhexlify(vin['txid'])
i.prev_index = vin['vout']
asm = [ binascii.unhexlify(x) for x in vin['scriptSig']['asm'].split(' ') ]
i.script_sig = chr(len(asm[0])) + asm[0] + chr(len(asm[1])) + asm[1] # TODO: should be op_push(x) instead of chr(len(x))
asm = vin['scriptSig']['asm'].split(' ')
asm = [ opcode_serialize(x) for x in asm ]
i.script_sig = ''.join(asm)
for vout in data['vout']:
o = t.outputs.add()
o.amount = int(vout['value'] * 100000000)
asm = vout['scriptPubKey']['asm'].split(' ')
# we suppose it's OP_DUP OP_HASH160 pubkey OP_EQUALVERIFY OP_CHECKSIG
if len(asm) != 5 or asm[0] != 'OP_DUP' or asm[1] != 'OP_HASH160' or asm[3] != 'OP_EQUALVERIFY' or asm[4] != 'OP_CHECKSIG':
raise Exception('Unknown scriptPubKey asm: %s' % asm)
o.script_pubkey = binascii.unhexlify('76a914' + asm[2] + '88ac')
asm = [ opcode_serialize(x) for x in asm ]
o.script_pubkey = ''.join(asm)
return t