本文整理汇总了Python中pycoin.serialize.h2b_rev函数的典型用法代码示例。如果您正苦于以下问题:Python h2b_rev函数的具体用法?Python h2b_rev怎么用?Python h2b_rev使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了h2b_rev函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tx_from_json_dict
def tx_from_json_dict(r):
version = r.get("version")
lock_time = r.get("locktime")
txs_in = []
for vin in r.get("vin"):
if "coinbase" in vin:
previous_hash = b'\0' * 32
script = h2b(vin.get("coinbase"))
previous_index = 4294967295
else:
previous_hash = h2b_rev(vin.get("txid"))
scriptSig = vin.get("scriptSig")
if "hex" in scriptSig:
script = h2b(scriptSig.get("hex"))
else:
script = tools.compile(scriptSig.get("asm"))
previous_index = vin.get("vout")
sequence = vin.get("sequence")
txs_in.append(TxIn(previous_hash, previous_index, script, sequence))
txs_out = []
for vout in r.get("vout"):
coin_value = btc_to_satoshi(decimal.Decimal(vout.get("value")))
script = tools.compile(vout.get("scriptPubKey").get("asm"))
txs_out.append(TxOut(coin_value, script))
tx = Tx(version, txs_in, txs_out, lock_time)
bh = r.get("blockhash")
if bh:
bh = h2b_rev(bh)
tx.confirmation_block_hash = bh
return tx
示例2: main
def main():
parser = argparse.ArgumentParser(description="Add a transaction to tx cache.")
parser.add_argument("tx_id_or_path", nargs="+",
help='The id of the transaction to fetch from web services or the path to it.')
args = parser.parse_args()
TX_RE = re.compile(r"^[0-9a-fA-F]{64}$")
tx_db = get_tx_db()
for p in args.tx_id_or_path:
if TX_RE.match(p):
tx = tx_db.get(h2b_rev(p))
if not tx:
parser.error("can't find Tx with id %s" % p)
else:
f = open(p, "rb")
try:
if f.name.endswith("hex"):
f = io.BytesIO(codecs.getreader("hex_codec")(f).read())
tx = Tx.parse(f)
except Exception:
parser.error("can't parse %s" % f.name)
tx_db[tx.hash()] = tx
print("cached %s" % tx.id())
示例3: main
def main():
logging.basicConfig(
level=logging.DEBUG,
format=('%(asctime)s [%(process)d] [%(levelname)s] '
'%(filename)s:%(lineno)d %(message)s'))
from pycoinnet.helpers.networks import MAINNET
from pycoinnet.util.BlockChainView import BlockChainView
from pycoinnet.bloom import BloomFilter
from pycoin.tx import Spendable
from pycoin.serialize import h2b_rev, h2b
network = MAINNET
initial_blockchain_view = BlockChainView()
bloom_filter = BloomFilter(2048, hash_function_count=8, tweak=3)
bloom_filter.add_address("14gZfnEn8Xd3ofkjr5s7rKoC3bi8J4Yfyy")
# bloom_filter.add_address("1GL6i1ty44RnERgqYLKS1CrnhrahW4JhQZ")
bloom_filter.add_item(h2b("0478abb18c0c7c95348fa77eb5fd43ce963e450d797cf4878894230ca528e6c8e866c3"
"8ad93746e04f2161a01787c82a858ee24940e9a06e41fddb3494dfe29380"))
spendable = Spendable(
0, b'', h2b_rev("0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9"), 0)
bloom_filter.add_spendable(spendable)
merkle_block_index_queue = asyncio.Queue()
spv = SPVClient(
network, initial_blockchain_view, bloom_filter, merkle_block_index_queue, host_port_q=None)
def fetch(merkle_block_index_queue):
while True:
merkle_block, index = yield from merkle_block_index_queue.get()
logging.info(
"block #%d %s with %d transactions", index, merkle_block.id(), len(merkle_block.txs))
t = asyncio.Task(fetch(merkle_block_index_queue))
asyncio.get_event_loop().run_forever()
示例4: populate_unspents
def populate_unspents(nodes):
global balance
global unspents
if nodes['used'] == {}:
return
addresses = nodes['used'].keys()
response = json.load( urlopen("http://%s.blockr.io/api/v1/address/unspent/" % BLOCKR + \
','.join(addresses)) )
data = response['data']
if type(data) == type({}):
data = [data]
for entry in data:
if entry['unspent'] == []:
continue
for unspent in entry['unspent']:
balance += Decimal(unspent['amount'])
amount = convention.btc_to_satoshi(unspent['amount'])
script = serialize.h2b(unspent['script'])
txid = serialize.h2b_rev(unspent['tx'])
unspent_spendable = Spendable( amount, script, txid, unspent['n'] )
unspents.append(unspent_spendable)
time.sleep(1) # Don't overload blockr.io API
示例5: test_h2b
def test_h2b(self):
h = "000102"
b = b"\x00\x01\x02"
self.assertEqual(h2b(h), b)
self.assertEqual(b2h(b), h)
self.assertEqual(h2b_rev(h), b[::-1])
self.assertEqual(b2h_rev(b), "020100")
示例6: parse_tx
def parse_tx(arg, parser, tx_db, network):
# hex transaction id
tx = None
if TX_ID_RE.match(arg):
if tx_db is None:
tx_db = create_tx_db(network)
tx = tx_db.get(h2b_rev(arg))
if not tx:
parser.error("can't find Tx with id %s" % arg)
return tx, tx_db
# hex transaction data
try:
return Tx.from_hex(arg), tx_db
except Exception:
pass
if os.path.exists(arg):
try:
with open(arg, "rb") as f:
if f.name.endswith("hex"):
f = io.BytesIO(codecs.getreader("hex_codec")(f).read())
tx = Tx.parse(f)
tx.parse_unspents(f)
except Exception:
pass
return tx, tx_db
示例7: request_blocks
def request_blocks(self, block_ids):
invs = []
for id in block_ids:
inv = bitcoin.net.CInv()
inv.type = self.INV_BLOCK
inv.hash = h2b_rev(id)
invs.append(inv)
self.request_objects(invs)
示例8: get_blockheader_with_transaction_hashes
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
示例9: spendables_for_address
def spendables_for_address(self, bitcoin_address):
url = "{0}/addr/{1}/utxo".format(self.base_url, bitcoin_address)
result = json.loads(urlopen(url).read().decode("utf8"))
spendables = []
for utxo in result:
value = btc_to_satoshi(str(utxo["amount"]))
prev_index = utxo["vout"]
prev_hash = h2b_rev(utxo["txid"])
script = h2b(utxo["scriptPubKey"])
spendable = Spendable(value, script, prev_hash, prev_index)
spendables.append(spendable)
return spendables
示例10: spendables_for_address
def spendables_for_address(self, bitcoin_address):
URL = "%s/api/addr/%s/utxo" % (self.base_url, bitcoin_address)
r = json.loads(urlopen(URL).read().decode("utf8"))
spendables = []
for u in r:
value = btc_to_satoshi(str(u.get("amount")))
script = h2b(u.get("scriptPubKey"))
prev_hash = h2b_rev(u.get("txid"))
prev_index = u.get("vout")
spendable = Spendable(value, script, prev_hash, prev_index)
spendables.append(spendable)
return spendables
示例11: spendables_for_address
def spendables_for_address(self, bitcoin_address):
"""
Return a list of Spendable objects for the
given bitcoin address.
"""
URL = "%s/api/addr/%s/utxo" % (self.base_url, bitcoin_address)
r = json.loads(urlopen(URL).read().decode("utf8"))
spendables = []
for u in r:
coin_value = btc_to_satoshi(str(u.get("amount")))
script = h2b(u.get("scriptPubKey"))
previous_hash = h2b_rev(u.get("txid"))
previous_index = u.get("vout")
spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
return spendables
示例12: spendables_for_address
def spendables_for_address(bitcoin_address):
"""
Return a list of Spendable objects for the
given bitcoin address.
"""
URL = "http://btc.blockr.io/api/v1/address/unspent/%s" % bitcoin_address
r = json.loads(urlopen(URL).read().decode("utf8"))
spendables = []
for u in r.get("data", {}).get("unspent", []):
coin_value = btc_to_satoshi(u.get("amount"))
script = h2b(u.get("script"))
previous_hash = h2b_rev(u.get("tx"))
previous_index = u.get("n")
spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
return spendables
示例13: unspents_for_address
def unspents_for_address(self, address):
"""
Return a list of Spendable objects for the
given bitcoin address.
"""
spendables = []
r = json.loads(urlopen(self.base_url('get_tx_unspent', address)).read().decode("utf8"))
for u in r['data']['txs']:
coin_value = int (float (u['value']) * 100000000)
script = h2b(u["script_hex"])
previous_hash = h2b_rev(u["txid"])
previous_index = u["output_no"]
spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
return spendables
示例14: spendables_for_address
def spendables_for_address(self, address):
"""
Return a list of Spendable objects for the
given bitcoin address.
"""
spendables = []
url_append = "?unspentOnly=true&token=%s&includeScript=true" % self.api_key
url = self.base_url("addrs/%s%s" % (address, url_append))
result = json.loads(urlopen(url).read().decode("utf8"))
for txn in result.get("txrefs", []):
coin_value = txn.get("value")
script = h2b(txn.get("script"))
previous_hash = h2b_rev(txn.get("tx_hash"))
previous_index = txn.get("tx_output_n")
spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
return spendables
示例15: spendables_for_address
def spendables_for_address(bitcoin_address):
"""
Return a list of Spendable objects for the
given bitcoin address.
"""
json_response = fetch_json(
"addresses/%s/unspent-outputs" % bitcoin_address)
spendables = []
for tx_out_info in json_response.get("data", {}).get("outputs"):
if tx_out_info.get("to_address") == bitcoin_address:
coin_value = tx_out_info["value"]
script = tools.compile(tx_out_info.get("script_pub_key"))
previous_hash = h2b_rev(tx_out_info.get("transaction_hash"))
previous_index = tx_out_info.get("transaction_index")
spendables.append(
Spendable(coin_value, script, previous_hash, previous_index))
return spendables