1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 12:08:59 +00:00

Merge pull request #34 from jhoenicke/master

insight api: use hex code
This commit is contained in:
Pavol Rusnak 2015-05-10 16:41:12 +02:00
commit 04bc56e048

View File

@ -5,44 +5,6 @@ from decimal import Decimal
from filecache import filecache, DAY from filecache import filecache, DAY
import types_pb2 as proto_types import types_pb2 as proto_types
def op_push_data(data):
l = len(data)
if l < 0x4C:
return chr(l) + data
elif i < 0xFF:
return '\x4C' + chr(l) + data
elif i < 0xFFFF:
return '\x4D' + struct.pack("<H", i) + data
else:
return '\x4E' + struct.pack("<I", i) + data
def opcode_serialize(opcode):
mapping = {
'OP_TRUE' : '\x51',
'OP_RETURN' : '\x6A',
'OP_DUP' : '\x76',
'OP_EQUAL' : '\x87',
'OP_EQUALVERIFY' : '\x88',
'OP_RIPEMD160' : '\xA6',
'OP_SHA1' : '\xA7',
'OP_SHA256' : '\xA8',
'OP_HASH160' : '\xA9',
'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
try:
x = binascii.unhexlify(opcode)
return op_push_data(x)
except:
raise Exception('Unknown script opcode: %s' % opcode)
def insight_tx(url, rawdata=False): def insight_tx(url, rawdata=False):
if not rawdata: if not rawdata:
try: try:
@ -68,17 +30,13 @@ def insight_tx(url, rawdata=False):
else: else:
i.prev_hash = binascii.unhexlify(vin['txid']) i.prev_hash = binascii.unhexlify(vin['txid'])
i.prev_index = vin['vout'] i.prev_index = vin['vout']
asm = vin['scriptSig']['asm'].split(' ') i.script_sig = binascii.unhexlify(vin['scriptSig']['hex'])
asm = [ opcode_serialize(x) for x in asm ]
i.script_sig = ''.join(asm)
i.sequence = vin['sequence'] i.sequence = vin['sequence']
for vout in data['vout']: for vout in data['vout']:
o = t.bin_outputs.add() o = t.bin_outputs.add()
o.amount = int(Decimal(str(vout['value'])) * 100000000) o.amount = int(Decimal(str(vout['value'])) * 100000000)
asm = vout['scriptPubKey']['asm'].split(' ') o.script_pubkey = binascii.unhexlify(vout['scriptPubKey']['hex'])
asm = [ opcode_serialize(x) for x in asm ]
o.script_pubkey = ''.join(asm)
return t return t