本文整理匯總了Python中pycoin.serialize.b2h_rev方法的典型用法代碼示例。如果您正苦於以下問題:Python serialize.b2h_rev方法的具體用法?Python serialize.b2h_rev怎麽用?Python serialize.b2h_rev使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pycoin.serialize
的用法示例。
在下文中一共展示了serialize.b2h_rev方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_blockheader_with_transaction_hashes
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def get_blockheader_with_transaction_hashes(self, block_hash):
URL = "%s/api/block/%s" % (self.base_url, b2h_rev(block_hash))
r = json.loads(urlopen(URL).read().decode("utf8"))
version = r.get("version")
previous_block_hash = h2b_rev(r.get("previousblockhash"))
merkle_root = h2b_rev(r.get("merkleroot"))
timestamp = r.get("time")
difficulty = int(r.get("bits"), 16)
nonce = int(r.get("nonce"))
tx_hashes = [h2b_rev(tx_hash) for tx_hash in r.get("tx")]
blockheader = BlockHeader(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce)
if blockheader.hash() != block_hash:
return None, None
calculated_hash = merkle(tx_hashes, double_sha256)
if calculated_hash != merkle_root:
return None, None
blockheader.height = r.get("height")
return blockheader, tx_hashes
示例2: __str__
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def __str__(self):
INV_TYPES = [None, "Tx", "Block", "Merkle"]
return "%s [%s]" % (INV_TYPES[self.item_type], b2h_rev(self.data))
示例3: as_json
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def as_json(self):
return json.dumps([[t[0], b2h_rev(t[1]), t[2]] for t in self.node_tuples])
示例4: do_headers_improve_path
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def do_headers_improve_path(self, headers):
"""
Raises ValueError if headers path don't extend from anywhere in this view.
Returns False if the headers don't improve the path.
If the headers DO improve the path, return the value of the block index of
the first header.
So you need to rewind to "new_start_idx" before applying the new blocks.
"""
tuples = []
if len(self.node_tuples) == 0:
if headers[0].previous_block_hash != HASH_INITIAL_BLOCK:
return False
the_tuple = GENESIS_TUPLE
else:
the_tuple = self.tuple_for_hash(headers[0].previous_block_hash)
if the_tuple is None:
return False
new_start_idx = the_tuple[0] + 1
total_work = the_tuple[-1]
expected_prior_hash = the_tuple[1]
for idx, h in enumerate(headers):
if h.previous_block_hash != expected_prior_hash:
raise ValueError(
"headers are not properly linked: no known block with hash %s" % b2h_rev(
h.previous_block_hash))
total_work += 1 # TODO: make this difficulty/work instead of path size
expected_prior_hash = h.hash()
tuples.append((idx + new_start_idx, expected_prior_hash, total_work))
if total_work <= self.last_block_tuple()[-1]:
return False
# the headers DO improve things
old_tuples = self.node_tuples
self._set_tuples(t for t in old_tuples if t[0] < new_start_idx)
self._add_tuples(tuples)
return new_start_idx
示例5: _path_for_hash
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def _path_for_hash(self, key):
return os.path.join(self.dir_path, "%s%s" % (self.prefix, b2h_rev(key)))
示例6: post_unpack_merkleblock
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def post_unpack_merkleblock(d, f):
"""
A post-processing "post_unpack" to merkleblock messages.
It validates the merkle proofs (throwing an exception if there's
an error), and returns the list of transaction hashes in "tx_hashes".
The transactions are supposed to be sent immediately after the merkleblock message.
"""
level_widths = []
count = d["total_transactions"]
while count > 1:
level_widths.append(count)
count += 1
count //= 2
level_widths.append(1)
level_widths.reverse()
tx_acc = []
flags = d["flags"]
hashes = list(reversed(d["hashes"]))
left_hash, flag_index = _recurse(level_widths, 0, 0, hashes, flags, 0, tx_acc)
if len(hashes) > 0:
raise ValueError("extra hashes: %s" % hashes)
idx, r = divmod(flag_index-1, 8)
if idx != len(flags) - 1:
raise ValueError("not enough flags consumed")
if flags[idx] > (1 << (r+1))-1:
raise ValueError("unconsumed 1 flag bits set")
if left_hash != d["header"].merkle_root:
raise ValueError(
"merkle root %s does not match calculated hash %s" % (
b2h_rev(d["header"].merkle_root), b2h_rev(left_hash)))
d["tx_hashes"] = tx_acc
return d
示例7: __str__
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def __str__(self):
BLOCK = "Block"
TX = "Tx"
INV_TYPES = {0: "?", 1: TX, 2: BLOCK, 3: "Merkle", 1073741825: TX, 1073741826: BLOCK}
idx = self.item_type
if idx not in INV_TYPES.keys():
idx = 0
return "InvItem %s [%s]" % (INV_TYPES[idx], b2h_rev(self.data))
示例8: broadcast_tx
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def broadcast_tx(self, transaction):
as_hex = transaction.as_hex()
transaction = CTransaction.deserialize(h2b(as_hex))
tx_id = bitcoin.rpc.Proxy().sendrawtransaction(transaction)
# reverse endianness for bitcoind
return b2h_rev(tx_id)
示例9: test_block
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def test_block(self):
expected_checksum = '0000000000089F7910F6755C10EA2795EC368A29B435D80770AD78493A6FECF1'.lower()
block_data = h2b('010000007480150B299A16BBCE5CCDB1D1BBC65CFC5893B01E6619107C55200000000000790'\
'0A2B203D24C69710AB6A94BEB937E1B1ADD64C2327E268D8C3E5F8B41DBED8796974CED66471B204C3247030'\
'1000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF0804ED6'\
'6471B024001FFFFFFFF0100F2052A010000004341045FEE68BAB9915C4EDCA4C680420ED28BBC369ED84D48A'\
'C178E1F5F7EEAC455BBE270DABA06802145854B5E29F0A7F816E2DF906E0FE4F6D5B4C9B92940E4F0EDAC000'\
'000000100000001F7B30415D1A7BF6DB91CB2A272767C6799D721A4178AA328E0D77C199CB3B57F010000008'\
'A4730440220556F61B84F16E637836D2E74B8CB784DE40C28FE3EF93CCB7406504EE9C7CAA5022043BD4749D'\
'4F3F7F831AC696748AD8D8E79AEB4A1C539E742AA3256910FC88E170141049A414D94345712893A828DE57B4C'\
'2054E2F596CDCA9D0B4451BA1CA5F8847830B9BE6E196450E6ABB21C540EA31BE310271AA00A49ED0BA930743'\
'D1ED465BAD0FFFFFFFF0200E1F505000000001976A914529A63393D63E980ACE6FA885C5A89E4F27AA08988AC'\
'C0ADA41A000000001976A9145D17976537F308865ED533CCCFDD76558CA3C8F088AC000000000100000001651'\
'48D894D3922EF5FFDA962BE26016635C933D470C8B0AB7618E869E3F70E3C000000008B48304502207F5779EB'\
'F4834FEAEFF4D250898324EB5C0833B16D7AF4C1CB0F66F50FCF6E85022100B78A65377FD018281E77285EFC3'\
'1E5B9BA7CB7E20E015CF6B7FA3E4A466DD195014104072AD79E0AA38C05FA33DD185F84C17F611E58A8658CE'\
'996D8B04395B99C7BE36529CAB7606900A0CD5A7AEBC6B233EA8E0FE60943054C63620E05E5B85F0426FFFFF'\
'FFF02404B4C00000000001976A914D4CAA8447532CA8EE4C80A1AE1D230A01E22BFDB88AC8013A0DE0100000'\
'01976A9149661A79AE1F6D487AF3420C13E649D6DF3747FC288AC00000000')
# try to parse a block
block = Block.parse(io.BytesIO(block_data))
print(block)
assert b2h_rev(block.hash()) == expected_checksum
for tx in block.txs:
print(tx)
for t in tx.txs_in:
print(" %s" % t)
for t in tx.txs_out:
print(" %s" % t)
block.check_merkle_hash()
示例10: get_tx
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def get_tx(self, tx_hash):
URL = "%s/api/tx/%s" % (self.base_url, b2h_rev(tx_hash))
r = json.loads(urlopen(URL).read().decode("utf8"))
tx = tx_from_json_dict(r)
if tx.hash() == tx_hash:
return tx
return None
示例11: get_json_for_hash
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def get_json_for_hash(the_hash):
d = urlopen("http://blockexplorer.com/rawtx/%s" % b2h_rev(the_hash)).read()
return json.loads(d.decode("utf8"))
示例12: tx_for_tx_hash
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def tx_for_tx_hash(tx_hash):
json_response = fetch_json("transactions/%s" % b2h_rev(tx_hash))
tx = json_to_tx(json_response)
if tx.hash() == tx_hash:
return tx
return None
示例13: put
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def put(self, tx):
name = b2h_rev(tx.hash())
if self.writable_cache_path:
try:
path = os.path.join(self.writable_cache_path, "%s_tx.bin" % name)
with open(path, "wb") as f:
tx.stream(f)
except IOError:
pass
示例14: __setitem__
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def __setitem__(self, key, val):
if val.hash() != key:
raise ValueError("bad key %s for %s" % (b2h_rev(key), val))
self.put(val)
示例15: unspent_to_bitcoind_dict
# 需要導入模塊: from pycoin import serialize [as 別名]
# 或者: from pycoin.serialize import b2h_rev [as 別名]
def unspent_to_bitcoind_dict(tx_in, tx_out):
return dict(
txid=b2h_rev(tx_in.previous_hash),
vout=tx_in.previous_index,
scriptPubKey=b2h(tx_out.script)
)