def parse_block(chain, ds): d = chain.parse_block_header(ds) d['transactions'] = [] nTransactions = ds.read_compact_size() for i in xrange(nTransactions): d['transactions'].append(deserialize.parse_Transaction(ds)) return d
def deserialize_tx(tx_hash, tx_height, raw_tx): vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) tx = deserialize.parse_Transaction(vds) tx['height'] = tx_height tx['tx_hash'] = tx_hash return tx
def deserialize_tx(self, tx_hash, tx_height, raw_tx): assert tx_hash == hash_encode(Hash(raw_tx.decode('hex'))) import deserialize vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) d = deserialize.parse_Transaction(vds) d['height'] = tx_height d['tx_hash'] = tx_hash return d
def deserialize_tx(self, tx_hash, tx_height, raw_tx): assert tx_hash == hash_encode(Hash(raw_tx.decode('hex'))) import deserialize vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) d = deserialize.parse_Transaction(vds) d['tx_hash'] = tx_hash d['timestamp'] = self.wallet.verifier.get_timestamp(tx_height) return d
def get_mempool_transaction(self, txid): try: raw_tx = self.bitcoind('getrawtransaction', [txid, 0, -1]) except: return None vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) out = deserialize.parse_Transaction(vds, is_coinbase = False) return out
def parse_block(raw_block): vds = deserialize.BCDataStream() vds.write(raw_block) block = deserialize.parse_BlockHeader(vds) block["transactions"] = [] number_tx = vds.read_compact_size() for i in xrange(number_tx): tx = deserialize.parse_Transaction(vds) block["transactions"].append(tx) return block
def get_mempool_transaction(self, txid): try: raw_tx = self.bitcoind('getrawtransaction', (txid, 0)) except: return None vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) try: return deserialize.parse_Transaction(vds, is_coinbase=False) except: print_log("ERROR: cannot parse", txid) return None
def get_mempool_transaction(self, txid): try: raw_tx = self.quebecoind('getrawtransaction', (txid, 0)) except: return None vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) try: return deserialize.parse_Transaction(vds, is_coinbase=False) except: print_log("ERROR: cannot parse", txid) return None
def sendbitcoin(address, amount=0, comment="", comment_to=""): txid = sendtoaddress(address, amount, comment, comment_to) if txid['result']: _raw = getrawtransaction(txid['result']) if _raw['result']: ds = BCDataStream() ds.write(binascii.unhexlify(_raw['result'])) _raw['result'] = deserialize.deserialize_TransactionRaw(deserialize.parse_Transaction(ds)) return _raw else: if txid['error']['code']==-4: txid['error']['message']="Insufficient funds" return txid
def deserialize_block(self, block): txlist = block.get('tx') tx_hashes = [] # ordered txids txdict = {} # deserialized tx is_coinbase = True for raw_tx in txlist: tx_hash = hash_encode(Hash(raw_tx.decode('hex'))) tx_hashes.append(tx_hash) vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) tx = deserialize.parse_Transaction(vds, is_coinbase) txdict[tx_hash] = tx is_coinbase = False return tx_hashes, txdict
def sendbitcoin(address, amount=0, comment="", comment_to=""): txid = sendtoaddress(address, amount, comment, comment_to) if txid['result']: _raw = getrawtransaction(txid['result']) if _raw['result']: ds = BCDataStream() ds.write(binascii.unhexlify(_raw['result'])) _raw['result'] = deserialize.deserialize_TransactionRaw( deserialize.parse_Transaction(ds)) return _raw else: if txid['error']['code'] == -4: txid['error']['message'] = "Insufficient funds" return txid
def deserialize_block(block): txlist = block.get('tx') tx_hashes = [] # ordered txids txdict = {} # deserialized tx is_coinbase = True for raw_tx in txlist: tx_hash = hash_encode(Hash(raw_tx.decode('hex'))) vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) try: tx = deserialize.parse_Transaction(vds, is_coinbase) except: print_log("ERROR: cannot parse", tx_hash) continue tx_hashes.append(tx_hash) txdict[tx_hash] = tx is_coinbase = False return tx_hashes, txdict
def deserialize(self): import deserialize vds = deserialize.BCDataStream() vds.write(self.raw.decode('hex')) self.d = deserialize.parse_Transaction(vds) return self.d
def ds_parse_transaction(chain, ds): return deserialize.parse_Transaction(ds)
def get_transaction(self, txid, block_height=-1, is_coinbase = False): raw_tx = self.bitcoind('getrawtransaction', [txid, 0, block_height]) vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) out = deserialize.parse_Transaction(vds, is_coinbase) return out
def ds_parse_transaction(chain, ds): return deserialize.parse_Transaction(ds, has_nTime=True)
COIN=100000000 #constant used to determine which part of the transaction is hashed. SIGHASH_ALL=1 #private key whose public key hashes to the hash contained in scriptPubKey of output number *OUTPUT_INDEX* in the transaction described in HEX_TRANSACTION PRIVATE_KEY=0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 def dsha256(data): return sha256.new(sha256.new(data).digest()).digest() tx_data=HEX_TRANSACTION.decode('hex_codec') tx_hash=dsha256(tx_data) #here we use bitcointools to parse a transaction. this gives easy access to the various fields of the transaction from which we want to redeem an output stream = BCDataStream() stream.write(tx_data) tx_info = parse_Transaction(stream) if len(tx_info['txOut']) < (OUTPUT_INDEX+1): raise RuntimeError, "there are only %d output(s) in the transaction you're trying to redeem from. you want to redeem output index %d" % (len(tx_info['txOut']), OUTPUT_INDEX) #this dictionary is used to store the values of the various transaction fields # this is useful because we need to construct one transaction to hash and sign # and another that will be the final transaction tx_fields = {} ##here we start creating the transaction that we hash and sign sign_tx = BCDataStream() ##first we write the version number, which is 1 tx_fields['version'] = 1 sign_tx.write_int32(tx_fields['version']) ##then we write the number of transaction inputs, which is one
def parse_transaction(raw_tx): vds = deserialize.BCDataStream() vds.write(raw_tx) tx = deserialize.parse_Transaction(vds) tx["hash"] = deserialize.Hash(raw_tx)[::-1] return tx