1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-27 01:48:17 +00:00

make txapi class more universal

This commit is contained in:
Pavol Rusnak 2014-04-02 19:31:47 +02:00
parent c3f74420a4
commit 60df691bb2

View File

@ -12,32 +12,41 @@ except:
import types_pb2 as proto_types import types_pb2 as proto_types
def op_push(i): def op_push_data(data):
if i<0x4c: l = len(data)
return chr(i) if l < 0x4C:
elif i<0xff: return chr(l) + data
return '\x4c' + chr(i) elif i < 0xFF:
elif i<0xffff: return '\x4C' + chr(l) + data
return '\x4d' + struct.pack("<H", i) elif i < 0xFFFF:
return '\x4D' + struct.pack("<H", i) + data
else: else:
return '\x4e' + struct.pack("<I", i) return '\x4E' + struct.pack("<I", i) + data
def opcode_serialize(opcode): def opcode_serialize(opcode):
# TODO: this function supports just small subset of script for now (enough for most transactions) mapping = {
if opcode == 'OP_DUP': 'OP_TRUE' : '\x51',
return '\x76' 'OP_RETURN' : '\x6A',
if opcode == 'OP_HASH160': 'OP_DUP' : '\x76',
return '\xa9' 'OP_EQUAL' : '\x87',
if opcode == 'OP_EQUAL': 'OP_EQUALVERIFY' : '\x88',
return '\x87' 'OP_RIPEMD160' : '\xA6',
if opcode == 'OP_EQUALVERIFY': 'OP_SHA1' : '\xA7',
return '\x88' 'OP_SHA256' : '\xA8',
if opcode == 'OP_CHECKSIG': 'OP_HASH160' : '\xA9',
return '\xac' 'OP_HASH256' : '\xAA',
'OP_CHECKSIG' : '\xAC',
'OP_CHECKSIGVERIFY' : '\xAD',
'OP_CHECKMULTISIG' : '\xAE',
'OP_CHECKMULTISIGVERIFY' : '\xAF',
}
# check if it is known opcode
if mapping.has_key(opcode):
return mapping[opcode]
# it's probably hex data # it's probably hex data
try: try:
x = binascii.unhexlify(opcode) x = binascii.unhexlify(opcode)
return op_push(len(x)) + x return op_push_data(x)
except: except:
raise Exception('Unknown script opcode: %s' % opcode) raise Exception('Unknown script opcode: %s' % opcode)