2014-03-28 18:47:53 +00:00
|
|
|
import binascii
|
|
|
|
import urllib2
|
|
|
|
import json
|
|
|
|
try:
|
|
|
|
from filecache import filecache, MONTH
|
|
|
|
except:
|
|
|
|
def filecache(x):
|
|
|
|
def _inner(y):
|
|
|
|
return y
|
|
|
|
return _inner
|
|
|
|
MONTH = None
|
|
|
|
|
|
|
|
import types_pb2 as proto_types
|
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
def bitcore_tx(url):
|
|
|
|
f = urllib2.urlopen(url)
|
|
|
|
data = json.load(f)
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
t = proto_types.TransactionType()
|
|
|
|
t.version = data['version']
|
|
|
|
t.lock_time = data['locktime']
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
for vin in data['vin']:
|
2014-03-28 18:47:53 +00:00
|
|
|
i = t.inputs.add()
|
2014-03-28 20:34:15 +00:00
|
|
|
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))
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
for vout in data['vout']:
|
2014-03-28 18:47:53 +00:00
|
|
|
o = t.outputs.add()
|
2014-03-28 20:34:15 +00:00
|
|
|
o.amount = int(vout['value'] * 100000000)
|
2014-03-28 20:48:48 +00:00
|
|
|
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)
|
2014-03-28 20:34:15 +00:00
|
|
|
o.script_pubkey = binascii.unhexlify('76a914' + asm[2] + '88ac')
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
return t
|
|
|
|
|
|
|
|
class TXAPIBitcoin(object):
|
|
|
|
|
|
|
|
@filecache(MONTH)
|
|
|
|
def get_tx(self, txhash):
|
|
|
|
url = 'http://live.bitcore.io/api/tx/%s' % txhash
|
|
|
|
return bitcore_tx(url)
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
class TXAPITestnet(object):
|
2014-03-28 18:47:53 +00:00
|
|
|
|
2014-03-28 20:34:15 +00:00
|
|
|
@filecache(MONTH)
|
|
|
|
def get_tx(self, txhash):
|
|
|
|
url = 'http://test.bitcore.io/api/tx/%s' % txhash
|
|
|
|
return bitcore_tx(url)
|