当前位置: 首页>>代码示例>>Python>>正文


Python serialize.h2b函数代码示例

本文整理汇总了Python中pycoin.serialize.h2b函数的典型用法代码示例。如果您正苦于以下问题:Python h2b函数的具体用法?Python h2b怎么用?Python h2b使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了h2b函数的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
开发者ID:Stevengu999,项目名称:pycoin,代码行数:30,代码来源:insight.py

示例2: check_bip143_tx

 def check_bip143_tx(self, tx_u_hex, tx_s_hex, txs_out_value_scripthex_pair, tx_in_count, tx_out_count, version, lock_time):
     tx_u = Tx.from_hex(tx_u_hex)
     tx_s = Tx.from_hex(tx_s_hex)
     txs_out = [
         TxOut(int(coin_value * 1e8), h2b(script_hex)) for coin_value, script_hex in txs_out_value_scripthex_pair
     ]
     for tx in (tx_u, tx_s):
         self.assertEqual(len(tx.txs_in), tx_in_count)
         self.assertEqual(len(tx.txs_out), tx_out_count)
         self.assertEqual(tx.version, version)
         self.assertEqual(tx.lock_time, lock_time)
         tx.set_unspents(txs_out)
     self.check_unsigned(tx_u)
     self.check_signed(tx_s)
     tx_hex = tx_u.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     tx_hex = tx_s.as_hex()
     self.assertEqual(tx_hex, tx_s_hex)
     tx_u_prime = self.unsigned_copy(tx_s)
     tx_hex = tx_u_prime.as_hex()
     self.assertEqual(tx_hex, tx_u_hex)
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_s_hex))), tx_s.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.w_id())
     self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.id())
     return tx_u, tx_s
开发者ID:Stevengu999,项目名称:pycoin,代码行数:25,代码来源:segwit_test.py

示例3: parse_scripts

def parse_scripts(args):
    scripts = []
    warnings = []

    for p2s in args.pay_to_script or []:
        try:
            scripts.append(h2b(p2s))
        except Exception:
            warnings.append("warning: error parsing pay-to-script value %s" % p2s)

    hex_re = re.compile(r"[0-9a-fA-F]+")
    for f in args.pay_to_script_file or []:
        count = 0
        for l in f:
            try:
                m = hex_re.search(l)
                if m:
                    p2s = m.group(0)
                    scripts.append(h2b(p2s))
                    count += 1
            except Exception:
                warnings.append("warning: error parsing pay-to-script file %s" % f.name)
        if count == 0:
            warnings.append("warning: no scripts found in %s" % f.name)
    return scripts, warnings
开发者ID:onestarshang,项目名称:pycoin,代码行数:25,代码来源:tx.py

示例4: test_to_from_long

    def test_to_from_long(self):
        def do_test(as_int, prefix, as_rep, base):
            self.assertEqual((as_int, prefix), encoding.to_long(base, encoding.byte_to_int, as_rep))
            self.assertEqual(as_rep, encoding.from_long(as_int, prefix, base, lambda v:v))

        do_test(10000101, 2, h2b("00009896e5"), 256)
        do_test(10000101, 3, h2b("0000009896e5"), 256)
        do_test(1460765565493402645157733592332121663123460211377, 1, b'\0\xff\xde\xfeOHu\xcf\x11\x9f\xc3\xd8\xf4\xa0\x9a\xe3~\xc4\xccB\xb1', 256)
开发者ID:Bluejudy,项目名称:pycoin,代码行数:8,代码来源:encoding_test.py

示例5: deserialize

 def deserialize(class_, blob):
     """
     Deserialize from a JSON blob, so that the transaction can be retried.
     Includes transaction, unspents and chain paths.
     """
     d = json.loads(blob)
     tx = class_.parse_with_paths(io.BytesIO(h2b(d['tx'])), d['input_chain_paths'], d['output_chain_paths'])
     tx.parse_unspents(io.BytesIO(h2b(d['unspents'])))
     return tx
开发者ID:tomholub,项目名称:multisig-core,代码行数:9,代码来源:hierarchy.py

示例6: create_tx_io

def create_tx_io(amount, addr, wallet_nodes, change_nodes, fee):
    txins = []
    txouts = []
    spendables = []
    wifs = []

    dust_threshold = Decimal('0.00000543')
    collected = Decimal(0)
    single = None
    
    assert amount > dust_threshold
    for op in unspents:
        op_amount = convention.satoshi_to_btc( op.as_dict()['coin_value'] )
        if op_amount >= amount + fee:
            single = op
            collected = op_amount
    
    if not single:
        for op in unspents:
            spendables.append(op)
            op_amount = convention.satoshi_to_btc( op.as_dict()['coin_value'] )
            collected += op_amount
            if collected >= amount + fee:
                break
        else:
            raise Exception("Insufficient funds!")
    else:
        spendables.append(single)
    
    for op in spendables:
        txin = op.tx_in()
        txins.append( txin )
        
        op_script = serialize.h2b( op.as_dict()['script_hex'] )
        hash160 = serialize.h2b( script.tools.opcode_list(op_script)[2] )
        prev_addr = Key(hash160=hash160, netcode=NETCODE).address()
        # from pycoin import networks
        # address_prefix = networks.address_prefix_for_netcode(NETCODE)
        # prev_addr = txin.bitcoin_address(address_prefix=address_prefix)
        
        for nodes in (wallet_nodes, change_nodes):
            if prev_addr in nodes['used'].keys():
                wifs.append( nodes['used'][prev_addr]['key'].wif() )
    
    change = collected - (amount + fee)
    
    amount_btc = convention.btc_to_satoshi(amount)
    spend = TxOut( amount_btc, standard_tx_out_script(addr) )
    txouts.append(spend)
    
    if change > dust_threshold:
        change_addr = get_unused_node(change_nodes, change=True)['key'].address()
        change_btc = convention.btc_to_satoshi(change)
        change_txout = TxOut( change_btc, standard_tx_out_script(change_addr) )
        txouts.append(change_txout)
    
    return (txins, txouts, spendables, wifs)
开发者ID:metamarcdw,项目名称:pycoin_wallet,代码行数:57,代码来源:wallet.py

示例7: test_verify_transaction

    def test_verify_transaction(self):
        cost = TransactionCosts(0.0001, 0.0000275, 3)
        tx_input = Spendable(200, '18eKkAWyU9kvRNHPKxnZb6wwtPMrNmRRRA',
                             h2b('8443b07464c762d7fb404ea918a5ac9b3618d5cd6a0c5ea6e4dd5d7bbe28b154'), 0)
        tx_outs = [trx_utils.create_transaction_output('mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', 0.0000275)]
        op_return_val = h2b('e9cee71ab932fde863338d08be4de9dfe39ea049bdafb342ce659ec5450b69ae')
        tx = trx_utils.create_trx(op_return_val, cost, 'mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', tx_outs, tx_input)

        hextx = hexlify(tx.serialize())

        trx_utils.verify_transaction(hextx, hexlify(op_return_val))
开发者ID:digital-certificates,项目名称:cert-issuer,代码行数:11,代码来源:test_trx_utils.py

示例8: test_verify_transaction

    def test_verify_transaction(self):
        bitcoin.SelectParams('testnet')
        b = h2b('8443b07464c762d7fb404ea918a5ac9b3618d5cd6a0c5ea6e4dd5d7bbe28b154')
        tx_input = Spendable(200, b'18eKkAWyU9kvRNHPKxnZb6wwtPMrNmRRRA', b, 0)
        tx_outs = [tx_utils.create_transaction_output('mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', 0.0000275)]
        op_return_val = h2b('e9cee71ab932fde863338d08be4de9dfe39ea049bdafb342ce659ec5450b69ae')
        tx = tx_utils.create_trx(op_return_val, 3, 'mgAqW5ZCnEp7fjvpj8RUL3WxsBy8rcDcCi', tx_outs, [tx_input])

        hextx = b2h(tx.serialize())

        tx_utils.verify_transaction(hextx, b2h(op_return_val))
开发者ID:NunoEdgarGub1,项目名称:cert-issuer,代码行数:11,代码来源:test_tx_utils.py

示例9: do_test

        def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58):

            secret_exponent = int(exp_hex, 16)
            sec = h2b(public_pair_sec)
            c_sec = h2b(c_public_pair_sec)

            keys_wif = [
                Key(secret_exponent=secret_exponent),
                Key.from_text(wif),
                Key.from_text(c_wif),
            ]

            key_sec = Key.from_sec(sec)
            key_sec_c = Key.from_sec(c_sec)
            keys_sec = [key_sec, key_sec_c]

            for key in keys_wif:
                self.assertEqual(key.secret_exponent(), secret_exponent)
                self.assertEqual(key.public_copy().secret_exponent(), None)
                repr(key)
                if key._prefer_uncompressed:
                    self.assertEqual(key.wif(), wif)
                else:
                    self.assertEqual(key.wif(), c_wif)
                self.assertEqual(key.wif(use_uncompressed=True), wif)
                self.assertEqual(key.wif(use_uncompressed=False), c_wif)

            for key in keys_wif + keys_sec:
                if key._prefer_uncompressed:
                    self.assertEqual(key.sec(), sec)
                else:
                    self.assertEqual(key.sec(), c_sec)
                self.assertEqual(key.sec(use_uncompressed=True), sec)
                self.assertEqual(key.sec(use_uncompressed=False), c_sec)
                if key._prefer_uncompressed:
                    self.assertEqual(key.address(), address_b58)
                else:
                    self.assertEqual(key.address(), c_address_b58)
                self.assertEqual(key.address(use_uncompressed=False), c_address_b58)
                self.assertEqual(key.address(use_uncompressed=True), address_b58)

            key_pub = Key.from_text(address_b58, is_compressed=False)
            key_pub_c = Key.from_text(c_address_b58, is_compressed=True)

            self.assertEqual(key_pub.address(), address_b58)
            self.assertEqual(key_pub.address(use_uncompressed=True), address_b58)
            self.assertEqual(key_pub.address(use_uncompressed=False), None)

            self.assertEqual(key_pub_c.address(), c_address_b58)
            self.assertEqual(key_pub_c.address(use_uncompressed=True), None)
            self.assertEqual(key_pub_c.address(use_uncompressed=False), c_address_b58)
开发者ID:onestarshang,项目名称:pycoin,代码行数:51,代码来源:key_test.py

示例10: test_sign_bitcoind_partially_signed_2_of_2

 def test_sign_bitcoind_partially_signed_2_of_2(self):
     # Finish signing a 2 of 2 transaction, that already has one signature signed by bitcoind
     # This tx can be found on testnet3 blockchain, txid: 9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2
     raw_script = "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae"
     p2sh_lookup = build_p2sh_lookup([h2b(raw_script)])
     partially_signed_raw_tx = "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f7802fa8706983a76a51467bfa36f8b88ac00000000"
     tx = Tx.from_hex(partially_signed_raw_tx)
     tx_out = TxOut(1000000, h2b("a914a10dfa21ee8c33b028b92562f6fe04e60563d3c087"))
     tx.set_unspents([tx_out])
     key = Key.from_text("cThRBRu2jAeshWL3sH3qbqdq9f4jDiDbd1SVz4qjTZD2xL1pdbsx")
     hash160_lookup = build_hash160_lookup([key.secret_exponent()])
     self.assertEqual(tx.bad_signature_count(), 1)
     tx.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup)
     self.assertEqual(tx.bad_signature_count(), 0)
     self.assertEqual(tx.id(), "9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2")
开发者ID:Stevengu999,项目名称:pycoin,代码行数:15,代码来源:pay_to_test.py

示例11: spendables_for_address

 def spendables_for_address(self, bitcoin_address):
     """
     Return a list of Spendable objects for the
     given bitcoin address.
     """
     URL = "https://blockchain.info/unspent?active=%s" % bitcoin_address
     r = json.loads(urlopen(URL).read().decode("utf8"))
     spendables = []
     for u in r["unspent_outputs"]:
         coin_value = u["value"]
         script = h2b(u["script"])
         previous_hash = h2b(u["tx_hash"])
         previous_index = u["tx_output_n"]
         spendables.append(Spendable(coin_value, script, previous_hash, previous_index))
     return spendables
开发者ID:onestarshang,项目名称:pycoin,代码行数:15,代码来源:blockchain_info.py

示例12: test_p2sh_multisig_sequential_signing

 def test_p2sh_multisig_sequential_signing(self):
     raw_scripts = [h2b('52210234abcffd2e80ad01c2ec0276ad02682808169c6fafdd25ebfb60703df272b4612102e5baaafff8094e4d77ce8b009d5ebc3de9110085ebd3d96e50cc7ce70faf1752210316ee25e80eb6e6fc734d9c86fa580cbb9c4bfd94a19f0373a22353ececd4db6853ae')]
     txs_in = [TxIn(previous_hash=h2b('43c95d14724437bccc102ccf86aba1ac02415524fd1aefa787db886bba52a10c'), previous_index=0)]
     txs_out = [TxOut(10000, standard_tx_out_script('3KeGeLFmsbmbVdeMLrWp7WYKcA3tdsB4AR'))]
     spendable = {'script_hex': 'a914c4ed4de526461e3efbb79c8b688a6f9282c0464687', 'does_seem_spent': 0,
                  'block_index_spent': 0, 'tx_hash_hex': '0ca152ba6b88db87a7ef1afd24554102aca1ab86cf2c10ccbc374472145dc943',
                  'coin_value': 10000, 'block_index_available': 0, 'tx_out_index': 0}
     tx__prototype = Tx(version=DEFAULT_VERSION, txs_in=txs_in, txs_out=txs_out, unspents=[Spendable.from_dict(spendable)])
     key_1, key_2 = 'Kz6pytJCigYHeMsGLmfHQPJhN5og2wpeSVrU43xWwgHLCAvpsprh', 'Kz7NHgX7MBySA3RSKj9GexUSN6NepEDoPNugSPr5absRDoKgn2dT'
     for ordered_keys in [(key_1, key_2), (key_2, key_1)]:
         tx = copy.deepcopy(tx__prototype)
         for key in ordered_keys:
             self.assertEqual(tx.bad_signature_count(), 1)
             tx.sign(LazySecretExponentDB([key], {}), p2sh_lookup=build_p2sh_lookup(raw_scripts))
         self.assertEqual(tx.bad_signature_count(), 0)
开发者ID:XertroV,项目名称:pycoin,代码行数:15,代码来源:pay_to_test.py

示例13: 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()
开发者ID:dzyk,项目名称:pycoinnet,代码行数:34,代码来源:spvclient.py

示例14: 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(u["txid"])
            previous_index = u["output_no"]
            spendables.append(Spendable(coin_value, script, previous_hash, previous_index))

        return spendables
开发者ID:ashmoran,项目名称:dashman,代码行数:16,代码来源:chain_so.py

示例15: 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
开发者ID:metamarcdw,项目名称:pycoin_wallet,代码行数:28,代码来源:wallet.py


注:本文中的pycoin.serialize.h2b函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。