#!/usr/bin/env python import codecs from bitcoin.rpc import Proxy if __name__ == '__main__': url = raw_input('Please enter the RPC url: ') username = raw_input('Please enter the RPC username: '******'Please enter the RPC password: '******'out.csv', 'w', 'utf-8') for idx in xrange(numblocks): blockinfo = proxy.getblock(proxy.getblockhash(idx)) fp.write(','.join( map(str, [ blockinfo['height'], blockinfo['time'], blockinfo['difficulty'], ])) + '\n') fp.close()
# -*- coding: utf-8 -*- # See http://bitcoinlib.readthedocs.io/en/latest/ from bitcoin.rpc import Proxy from bitcoin.core import b2lx, b2x rpc = Proxy(btc_conf_file="/Users/sortega/Library/Application Support/Bitcoin/bitcoin.conf") num_blocks = rpc.getinfo()['blocks'] height = num_blocks / 3 block_id = rpc.getblockhash(height) block = rpc.getblock(block_id) last_tx = block.vtx[-1] print("Last transaction in block %s at height %d" % (b2lx(block_id), height)) print("Raw transaction: %s" % b2x(last_tx.serialize())) print("Parsed transaction: %s" % last_tx)
# else: # # print("unknown") # self.unknowns.append([txid, b2x(spk)]) self.type = type self.found |= self.type.value if self.printed & self.type.value: pass elif self.type.value: self.process(g1, g2, g3, k) else: #unknown type self.process('', '', '', k) if __name__ == "__main__": make_addresses() latest = proxy.getblockcount() i = -1 txn = Transaction() while True: i += 1 j = -1 if txn.found == 255: break bhash = proxy.getblockhash(latest - i) block = proxy.getblock(bhash) bhash = b2lx(bhash) for tx in block.vtx: j += 1 if txn.found == 255: break txid = b2lx(tx.GetTxid()) txn.gettxn(txid)
#!/usr/bin/env python import codecs from bitcoin.rpc import Proxy if __name__ == '__main__': url = raw_input('Please enter the RPC url: ') username = raw_input('Please enter the RPC username: '******'Please enter the RPC password: '******'out.csv', 'w', 'utf-8') for idx in xrange(numblocks): blockinfo = proxy.getblock(proxy.getblockhash(idx)) fp.write(','.join(map(str, [ blockinfo['height'], blockinfo['time'], blockinfo['difficulty'], ]))+'\n') fp.close()
class Blockchain: def __init__(self): self.proxy = Proxy('http://*****:*****@blockchain.infnote.com:8962') @staticmethod def deserialize_transaction(raw_tx): return CTransaction.deserialize(unhexlify(raw_tx.encode('utf8'))) def decode_transaction(self, tx): contents = [] for out in tx.vout: if out.nValue > 0: continue data = self.get_data_from_vout(out) if data: contents.append(data) return contents def send_transaction(self, raw_tx): transaction = self.deserialize_transaction(raw_tx) txid = self.proxy.sendrawtransaction(transaction) return txid @staticmethod def get_data_from_vout(vout): i = iter(vout.scriptPubKey) flag = next(i) # TODO: 需要区分类型,以填充至不同的模型 if flag == script.OP_RETURN or flag == script.OP_NOP8: data = next(i).decode('utf8') return json.loads(data) return None def get_transaction(self, txid): return self.proxy.getrawtransaction(txid) def get_block_count(self): return self.proxy.getblockcount() def get_block_by_height(self, height: int): return self.proxy.getblock(self.proxy.getblockhash(height)) def server_unspent(self): return self.proxy.listunspent() @staticmethod def freeze_coins_in_tx(tx: CTransaction): for vin in tx.vin: coin = Coin.objects.get(txid=b2lx(vin.prevout.hash), vout=vin.prevout.n) coin.frozen = True coin.save() def send_coin_to(self, address, coin: Coin): txin = CMutableTxIn(COutPoint(lx(coin.txid), coin.vout)) txout = CMutableTxOut(coin.value - 1e5, CBitcoinAddress(address).to_scriptPubKey()) tx = CMutableTransaction([txin], [txout]) tx = self.proxy.signrawtransaction(tx)['tx'] # sig_hash = SignatureHash(txin_script_pubkey, tx, 0, SIGHASH_ALL) # sig = seckey.sign(sig_hash) + bytes([SIGHASH_ALL]) # txin.scriptSig = CScript([sig, seckey.pub]) # VerifyScript(txin.scriptSig, txin_script_pubkey, tx, 0, (SCRIPT_VERIFY_P2SH,)) # print(b2x(tx.serialize())) self.proxy.sendrawtransaction(tx) coin.frozen = True coin.save()