1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +00:00

use bitcore for tx api

This commit is contained in:
Pavol Rusnak 2014-03-28 21:34:15 +01:00
parent 860aa753c9
commit 38ed16934f
4 changed files with 37 additions and 63 deletions

4
cmd.py
View File

@ -6,7 +6,7 @@ import json
import base64
from trezorlib.client import TrezorClientDebug
from trezorlib.tx_api import TXAPIBlockchain
from trezorlib.tx_api import TXAPIBitcoin
from trezorlib.protobuf_json import pb2json
def parse_args(commands):
@ -324,7 +324,7 @@ def main():
transport = get_transport(args.transport, args.path)
client = TrezorClientDebug(transport)
client.set_tx_api(TXAPIBlockchain())
client.set_tx_api(TXAPIBitcoin())
cmds = Commands(client)
res = args.func(cmds, args)

View File

@ -2,7 +2,7 @@ import unittest
import config
from trezorlib.client import TrezorDebugClient
from trezorlib.tx_api import TXAPIBlockchain
from trezorlib.tx_api import TXAPIBitcoin
class TrezorTest(unittest.TestCase):
def setUp(self):
@ -10,7 +10,7 @@ class TrezorTest(unittest.TestCase):
self.transport = config.TRANSPORT(*config.TRANSPORT_ARGS, **config.TRANSPORT_KWARGS)
self.client = TrezorDebugClient(self.transport)
self.client.set_debuglink(self.debug_transport)
self.client.set_tx_api(TXAPIBlockchain())
self.client.set_tx_api(TXAPIBitcoin())
# 1 2 3 4 5 6 7 8 9 10 11 12
self.mnemonic12 = 'alcohol woman abuse must during monitor noble actual mixed trade anger aisle'

View File

@ -8,7 +8,7 @@ import binascii
import trezorlib.messages_pb2 as proto
import trezorlib.types_pb2 as proto_types
from trezorlib.client import CallException
from trezorlib.tx_api import TXAPITestnetFake
from trezorlib.tx_api import TXAPITestnet
class TestMsgSimplesigntx(common.TrezorTest):
@ -61,7 +61,7 @@ class TestMsgSimplesigntx(common.TrezorTest):
)
with self.client:
self.client.set_tx_api(TXAPITestnetFake())
self.client.set_tx_api(TXAPITestnet())
msg = self.client._prepare_simple_sign_tx('Testnet', [inp1, ], [out1, out2])
self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
# proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), # don't confirm change
@ -93,7 +93,7 @@ class TestMsgSimplesigntx(common.TrezorTest):
)
with self.client:
self.client.set_tx_api(TXAPITestnetFake())
self.client.set_tx_api(TXAPITestnet())
msg = self.client._prepare_simple_sign_tx('Testnet', [inp1, ], [out1, out2])
self.client.set_expected_responses([proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
# proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput), # don't confirm change

View File

@ -12,65 +12,39 @@ except:
import types_pb2 as proto_types
class TXAPIBlockchain(object):
def _raw_tx(self, txhash):
# Download tx data from blockchain.info
url = 'http://blockchain.info/rawtx/%s?scripts=true' % txhash
print "Downloading", url
f = urllib2.urlopen(url)
return json.load(f)
def bitcore_tx(url):
f = urllib2.urlopen(url)
data = json.load(f)
t = proto_types.TransactionType()
t.version = data['version']
t.lock_time = data['locktime']
for vin in data['vin']:
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))
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
o.script_pubkey = binascii.unhexlify('76a914' + asm[2] + '88ac')
return t
class TXAPIBitcoin(object):
@filecache(MONTH)
def get_tx(self, txhash):
# Build protobuf transaction structure from blockchain.info
d = self._raw_tx(txhash)
t = proto_types.TransactionType()
url = 'http://live.bitcore.io/api/tx/%s' % txhash
return bitcore_tx(url)
for inp in d['inputs']:
di = self._raw_tx(inp['prev_out']['tx_index'])
i = t.inputs.add()
i.prev_hash = binascii.unhexlify(di['hash'])
i.prev_index = inp['prev_out']['n']
i.script_sig = binascii.unhexlify(inp['script'])
class TXAPITestnet(object):
for output in d['out']:
o = t.outputs.add()
o.amount = output['value']
o.script_pubkey = binascii.unhexlify(output['script'])
t.version = 1
t.lock_time = 0
return t
class TXAPITestnetFake(object):
@filecache(MONTH)
def get_tx(self, txhash):
if txhash != '6f90f3c7cbec2258b0971056ef3fe34128dbde30daa9c0639a898f9977299d54':
raise Exception("Unexpected hash")
t = proto_types.TransactionType()
i = t.inputs.add()
i.prev_hash = binascii.unhexlify('ee336e79153d51f4f3e45278f1f77ab29fd5bb135dce467282e2aff22cb9c570')
i.prev_index = 1
i.script_sig = binascii.unhexlify('483045022066c418874dbe5628296700382d727ce1734928796068c26271472df09dccf1a20221009dec59d19f9d73db381fcd35c0fff757ad73e54ef59157b0d7c57e6739a092f00121033fef08c603943dc7d25f4ce65771762143b1cd8678343d660a1a76b9d1d3ced7')
i = t.inputs.add()
i.prev_hash = binascii.unhexlify('2fe4d8af2b44faccc10dd5a6578c923491d2d21269a1dfe8c83f492a30fb8f9f')
i.prev_index = 1
i.script_sig = binascii.unhexlify('47304402206fbb8e14be706b8557a2280d2a2a75c0a65c4f7936d90d510f0971c93f41f74402201b79c8c4e4ac4c944913611633c230193558296e70a36269b7fc3a80efa27d120121030cb5be79bdc36a4ff4443dbac43068cc43d638ea06ff2fa1b8dab389e39aefc7')
o = t.outputs.add()
o.amount = 403850989
o.script_pubkey = binascii.unhexlify('76a914f5a05c2664b40d3116b1c5086c9ba38ed15b742e88ac')
o = t.outputs.add()
o.amount = 1000000000
o.script_pubkey = binascii.unhexlify('76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac')
t.version = 1
t.lock_time = 0
return t
if __name__ == '__main__':
api = TXAPIBlockchain()
print api.get_tx('b9f382b8dfc34accc05491712a1ad8f7f075a02056dc4821d1f60702fb3fdb2f')
url = 'http://test.bitcore.io/api/tx/%s' % txhash
return bitcore_tx(url)