def bciInputs(cur, blob, ins): data = [] hdr = getBlobHdr( blob, sqc.cfg) # hdrsz,ins,outs,size,version,locktime,stdSeq,nosigs if ins >= 0xC0: ins = ((ins & 0x3F) << 8) + hdr[1] if ins == 0: # no inputs return [{}], ins # only sequence and script here else: buf = readBlob(blob + hdr[0], ins * 7, sqc.cfg) if len(buf ) < ins * 7 or buf == '\0' * ins * 7: # means missing blob data return [{'error': 'missing data'}], ins for n in range(ins): in_id, = unpack('<Q', buf[n * 7:n * 7 + 7] + '\0') cur.execute( "select value,addr_id from outputs where id=%s limit 1;", (in_id, )) rows = cur.fetchall() for value, aid in rows: cur.execute( "select addr from {0} where id=%s limit 1;".format( 'bech32' if is_BL32(int(aid)) else 'address'), (aid, )) for addr, in cur: data.append({ 'prev_out': { 'spent': True, 'type': 0, 'n': in_id % MAX_IO_TX, 'value': int(value), 'tx_index': in_id // MAX_IO_TX, 'addr': mkaddr(addr, int(aid)) } }) return data, ins
def txoAddr(cur, txhash, n): txid = txh2id(txhash.decode('hex')[::-1]) cur.execute("select addr_id from outputs o where o.id>=%s*{0} and o.id<%s*{0} and o.id%%{0}=%s limit 1;".format(MAX_IO_TX), (txid,txid+1,int(n))) aids = cur.fetchall() for aid, in aids: cur.execute("select addr from {0} where id=%s limit 1;".format('bech32' if is_BL32(int(aid)) else 'address'), (aid,)) addr = cur.fetchone()[0] return mkaddr(addr,int(aid)) return None
def txAddrs(cur, txhash): data = [] txid = txh2id(txhash.decode('hex')[::-1]) cur.execute("select addr_id from outputs o where o.id>=%s*{0} and o.id<%s*{0};".format(MAX_IO_TX), (txid,txid+1)) for aid, in cur: cur.execute("select addr from {0} where id=%s limit 1;".format('bech32' if is_BL32(int(aid)) else 'address'), (aid,)) addr = cur.fetchone()[0] data.append( mkaddr(addr,int(aid)) ) cur.execute("select txdata,ins from trxs where id=%s limit 1;", (txid,)) txins = cur.fetchall() for txdata,ins in txins: blob = getBlobData(int(txdata), ins) if ins > 0: for _,xin in enumerate(blob['ins']): cur.execute("select addr_id from outputs o where o.id=%s limit 1;", (xin['outid'],)) aids = cur.fetchall() for aid, in aids: cur.execute("select addr from {0} where id=%s limit 1;".format('bech32' if is_BL32(int(aid)) else 'address'), (aid,)) addr = cur.fetchone()[0] data.append(mkaddr(addr,int(aid))) return data
def apiInputs(cur, height, ins): total,data = 0,[] if len(ins) == 0: cur.execute("select coinbase from blocks where id=%s;", (height,)) return 0,[{ 'n':0, 'coinbase':cur.fetchone()[0].encode('hex') }] else: for n,xin in enumerate(ins): cur.execute("select value,addr_id,hash from outputs o, trxs t where o.id=%s and t.id=o.id div %s limit 1;", (xin['outid'], MAX_IO_TX)) rows = cur.fetchall() for value,aid,txhash in rows: cur.execute("select addr from {0} where id=%s limit 1;".format('bech32' if is_BL32(int(aid)) else 'address'), (aid,)) for addr, in cur: btc = float(value)/1e8 data.append({ 'n':n, 'vout':xin['outid']%MAX_IO_TX, 'value':round(btc,8), 'valueSat':int(value), 'txid':txhash[::-1].encode('hex'), 'addr':mkaddr(addr,int(aid)), 'sequence':unpack('<I',xin['seq'])[0] }) if 'sigs' in xin: data[n]['scriptSig'] = { 'hex': xin['sigs'].encode('hex'), 'asm': mkOpCodeStr(xin['sigs']) } total += btc return round(total,8),data
def apiOutputs(cur, txid, outs): total,data = 0,[] cur.execute("select o.id,o.id%%{0},value,addr_id,o.tx_id from outputs o where o.id>=%s*{0} and o.id<%s*{0};".format(MAX_IO_TX), (txid,txid+1)) rows = cur.fetchall() for out_id,n,value,aid,in_id in rows: btc = float(value)/1e8 total += btc vout = { 'n':int(n), 'value':"%1.8f" % btc, 'scriptPubKey':{} } if aid == 0: vout['scriptPubKey']['hex'] = outs[int(n)] else: cur.execute("select addr from {0} where id=%s limit 1;".format('bech32' if is_BL32(int(aid)) else 'address'), (aid,)) for addr, in cur: vout['scriptPubKey']['addresses'] = [ mkaddr(addr,int(aid)) ] vout['scriptPubKey']['hex'] = mkSPK(addr,int(aid))[1] vout['scriptPubKey']['asm'] = mkOpCodeStr(vout['scriptPubKey']['hex'], sepPUSH=' ') vout['scriptPubKey']['hex'] = vout['scriptPubKey']['hex'].encode('hex') if in_id: vout.update(apiSpent(cur, int(in_id), int(out_id))) data.append(vout) return round(total,8),data
def bciOutputs(cur, txid, blob): data = [] cur.execute( "select o.tx_id,o.id%%{0},value,addr_id from outputs o where o.id>=%s*{0} and o.id<%s*{0};" .format(MAX_IO_TX), (txid, txid + 1)) outs = cur.fetchall() for in_id, n, value, aid in outs: cur.execute( "select addr from {0} where id=%s limit 1;".format( 'bech32' if is_BL32(int(aid)) else 'address'), (aid, )) for addr, in cur: vout = { 'n': int(n), 'value': int(value), 'addr': mkaddr(addr, int(aid)), 'type': 0, 'tx_index': txid } if in_id: vout['spent'] = True data.append(vout) return data, len(outs)
def test_mkaddr(): #p2pkh assert mkaddr('0c2f3eb0fa5f65269236658bc361187dfaa964bb'.decode( 'hex')) == '127RhwC5vQJN4cJ6UaHc1A9NCSpz1e9B4i' assert mkaddr('bf362d4dda191483e789ccf3059d6447cd64bb9c'.decode( 'hex')) == '1JS2xvSfG2hD3rnMGd3xxEeYSoBs8r7eKh' assert mkaddr('870a76dd469ab77084229a61984db634abaafb8b'.decode( 'hex')) == '1DK2kyHNMUx8XoWZm9t2GWqJGzqBNxUYuv' #p2sh assert mkaddr('1c6426545908803de2a4ed61caf805ccc282900f'.decode('hex'), p2sh=True) == '34H8pSTwFNEngG5xfadqctdQykcGgRmSgf' assert mkaddr('c161e4848786150e2add1a93f084fa94a7259b97'.decode('hex'), p2sh=True) == '3KKXcGTmxvedJr9GrzWayA8GVnS5AXm8tj' assert mkaddr('4df2e66aeb640a642c8476185f63e433ad074220'.decode('hex'), p2sh=True) == '38oAwJnDWRTWf1GUg7FJ112bjVRoMjvCmV' #p2wpkh-p2sh assert mkaddr('924b50fdfc0e0afab1b1d12acae31c3b4a215154'.decode('hex'), p2sh=True) == '3F2YodB6PAzbov1rAkYVMNu6KBB1g9AHrG' assert mkaddr('32eaeff4e7e856e74dcf0926724d04324320eb75'.decode('hex'), p2sh=True) == '36LF9sFUJQAzGgxKtrVFDcXqmTF9yyVeow' assert mkaddr('271c19a61825788201434354d2a3a6b03d23e316'.decode('hex'), p2sh=True) == '35FowTfm9qpeKGX9VQuuSrcgDiBd9SczAi' #p2wsh-p2sh - unavailble #p2wpkh assert mkaddr( '751e76e8199196d454941c45d1b3a323f1433bd6'.decode('hex'), bech32=True) == 'BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4'.lower() assert mkaddr('a7c47325325d7a2b0b75bff2dad00146395d2ef7'.decode('hex'), bech32=True) == 'bc1q5lz8xffjt4azkzm4hled45qpgcu46thhl6j7vm' assert mkaddr('7906d7156a0932c2d649c9005deebd7c3696c1c9'.decode('hex'), bech32=True) == 'bc1q0yrdw9t2pyev94jfeyq9mm4a0smfdswfweht6t' #p2wsh assert mkaddr('df938440bc4235a1ba8b429de811fb7f52b5ce7cc0e85be361e9fd0c89bed159'.decode('hex'),bech32=True) \ == 'bc1qm7fcgs9ugg66rw5tg2w7sy0m0afttnnucr59hcmpa87sezd769vsac7pmy' assert mkaddr('a202a7fd3cb895d6300eef35043b7db0c341a80cd68d4d0d232ae2fbf7ecab13'.decode('hex'),bech32=True) \ == 'bc1q5gp20lfuhz2avvqwau6sgwmakrp5r2qv66x56rfr9t30halv4vfs283f6e' assert mkaddr('8519909c5810f434c2c08e782ad4529b1f416ef609eb2bdb89e260d2288848fc'.decode('hex'),bech32=True) \ == 'bc1qs5vep8zczr6rfskq3euz44zjnv05zmhkp84jhkufufsdy2ygfr7qr8x759'