def create_bitcoin_address(test_obj):
    # fee address
    pk = str(test_obj.hdwallet.get_pubkey(path=FEE_PATH).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = FEE_PATH
    test_obj.addr_dic['fee'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set fee addr: ' + str(addr))
    path2 = FEE_PATH + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set fee ct_addr: ' + str(ct_addr))

    # gen address
    pk = str(test_obj.hdwallet.get_pubkey(path=GEN_PATH).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = FEE_PATH
    test_obj.addr_dic['gen'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set gen addr: ' + str(addr))
    path2 = GEN_PATH + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set gen ct_addr: ' + str(ct_addr))

    # wpkh main address
    path = '{}/0/0'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['main'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set main addr: ' + str(addr))
    path2 = path + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set main ct_addr: ' + str(ct_addr))

    # pkh address
    path = '{}/0/1'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2pkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2pkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('pkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set p2pkh addr: ' + str(addr))
    path2 = path + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2pkh ct_addr: ' + str(ct_addr))
    # wpkh address
    path = '{}/0/2'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2wpkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set p2wpkh addr: ' + str(addr))
    path2 = path + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2wpkh ct_addr: ' + str(ct_addr))
    # p2sh-p2wpkh address
    path = '{}/0/3'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2sh_p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2sh-p2wpkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('sh(wpkh({}))'.format(
        str(pk)),
                                                    network=NETWORK)
    print('set p2sh-p2wpkh addr: ' + str(addr))
    path2 = path + '/0'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2sh-p2wpkh ct_addr: ' + str(ct_addr))

    # multisig_key
    path = '{}/0/'.format(ROOT_PATH)
    path_list = [path + str(i + 1) for i in range(3)]
    pk1 = str(test_obj.hdwallet.get_pubkey(path=path_list[0]).pubkey)
    pk2 = str(test_obj.hdwallet.get_pubkey(path=path_list[1]).pubkey)
    pk3 = str(test_obj.hdwallet.get_pubkey(path=path_list[2]).pubkey)
    pk_list = [pk1, pk2, pk3]
    req_num = 2
    desc_multi = 'multi({},{},{},{})'.format(req_num, pk1, pk2, pk3)
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2SH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2sh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'sh({})'.format(desc_multi), network=NETWORK)
    print('set p2sh addr: ' + str(addr))
    path2 = MULTISIG_CT_PATH_BASE + '1'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2sh ct_addr: ' + str(ct_addr))
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2WSH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2wsh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'wsh({})'.format(desc_multi), network=NETWORK)
    print('set p2wsh addr: ' + str(addr))
    path2 = MULTISIG_CT_PATH_BASE + '2'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2wsh ct_addr: ' + str(ct_addr))
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2SH_P2WSH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2sh-p2wsh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'sh(wsh({}))'.format(desc_multi), network=NETWORK)
    print('set p2sh-p2wsh addr: ' + str(addr))
    path2 = MULTISIG_CT_PATH_BASE + '3'
    sk = test_obj.hdwallet.get_privkey(path=path2).privkey
    test_obj.blind_key_dic[str(addr)] = sk
    ct_addr = ConfidentialAddress(addr, sk.pubkey)
    test_obj.ct_addr_dic[str(addr)] = ct_addr
    print('set p2sh-p2wsh ct_addr: ' + str(ct_addr))

    # master blinding key
    path = '{}/0/1001'.format(ROOT_PATH)
    sk = str(test_obj.hdwallet.get_privkey(path=path).privkey)
    test_obj.master_blinding_key = sk
    print('set master blinding key: ' + sk)
    def test_create_raw_transaction(self):
        privkey = ExtPrivkey(
            'xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV'
        )  # noqa: E501
        addr1 = AddressUtil.p2wpkh(
            privkey.derive_pubkey(number=1).pubkey, Network.REGTEST)
        addr2 = AddressUtil.p2wpkh(
            privkey.derive_pubkey(number=2).pubkey, Network.REGTEST)
        addr3 = AddressUtil.p2wpkh(
            privkey.derive_pubkey(number=3).pubkey, Network.REGTEST)

        outpoint1 = OutPoint(
            '0000000000000000000000000000000000000000000000000000000000000001',
            2)
        outpoint2 = OutPoint(
            '0000000000000000000000000000000000000000000000000000000000000001',
            3)
        txin1 = TxIn(outpoint=outpoint1)
        txout1 = TxOut(amount=10000, locking_script=addr1.locking_script)
        txout2 = TxOut(amount=10000, address=addr2)
        self.assertEqual(str(outpoint1), str(txin1))
        self.assertEqual(str(addr1.locking_script), str(txout1))
        self.assertEqual(str(addr2), str(txout2))
        self.assertEqual(str(addr1), str(txout1.get_address(Network.REGTEST)))

        tx = Transaction.create(version=2,
                                locktime=0,
                                txins=[
                                    txin1,
                                    TxIn(outpoint=outpoint2),
                                ],
                                txouts=[
                                    txout1,
                                    txout2,
                                ])
        tx.add_txout(amount=50000, address=addr3)
        self.assertEqual(
            "020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000",  # noqa: E501
            tx.hex)

        privkey1 = privkey.derive(number=11).privkey
        pubkey1 = privkey1.pubkey
        sighash_type = SigHashType.ALL
        sighash = tx.get_sighash(outpoint=outpoint1,
                                 hash_type=HashType.P2WPKH,
                                 pubkey=pubkey1,
                                 amount=50000,
                                 sighashtype=sighash_type)
        signature = privkey1.calculate_ec_signature(sighash)
        tx.add_sign(outpoint=outpoint1,
                    hash_type=HashType.P2WPKH,
                    sign_data=signature,
                    clear_stack=True,
                    use_der_encode=True,
                    sighashtype=sighash_type)
        tx.add_sign(outpoint=outpoint1,
                    hash_type=HashType.P2WPKH,
                    sign_data=pubkey1)
        self.assertEqual(
            "0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000",  # noqa: E501
            tx.hex)
Beispiel #3
0
def create_bitcoin_address(test_obj):
    # fee address
    pk = str(test_obj.hdwallet.get_pubkey(path=FEE_PATH).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = FEE_PATH
    test_obj.addr_dic['fee'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set fee addr: ' + str(addr))

    # wpkh main address
    path = '{}/0/0'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['main'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set main addr: ' + str(addr))
    # pkh address
    path = '{}/0/1'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2pkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2pkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('pkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set p2pkh addr: ' + str(addr))
    # wpkh address
    path = '{}/0/2'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2wpkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('wpkh({})'.format(str(pk)),
                                                    network=NETWORK)
    print('set p2wpkh addr: ' + str(addr))
    # p2sh-p2wpkh address
    path = '{}/0/3'.format(ROOT_PATH)
    pk = str(test_obj.hdwallet.get_pubkey(path=path).pubkey)
    addr = AddressUtil.p2sh_p2wpkh(pk, network=NETWORK)
    test_obj.path_dic[str(addr)] = path
    test_obj.addr_dic['p2sh-p2wpkh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor('sh(wpkh({}))'.format(
        str(pk)),
                                                    network=NETWORK)
    print('set p2sh-p2wpkh addr: ' + str(addr))

    # multisig_key
    path = '{}/0/'.format(ROOT_PATH)
    path_list = [path + str(i + 1) for i in range(3)]
    pk1 = str(test_obj.hdwallet.get_pubkey(path=path_list[0]).pubkey)
    pk2 = str(test_obj.hdwallet.get_pubkey(path=path_list[1]).pubkey)
    pk3 = str(test_obj.hdwallet.get_pubkey(path=path_list[2]).pubkey)
    pk_list = [pk1, pk2, pk3]
    req_num = 2
    desc_multi = 'multi({},{},{},{})'.format(req_num, pk1, pk2, pk3)
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2SH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2sh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'sh({})'.format(desc_multi), network=NETWORK)
    print('set p2sh addr: ' + str(addr))
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2WSH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2wsh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'wsh({})'.format(desc_multi), network=NETWORK)
    print('set p2wsh addr: ' + str(addr))
    addr = AddressUtil.multisig(req_num,
                                pk_list,
                                HashType.P2SH_P2WSH,
                                network=NETWORK)
    test_obj.path_dic[str(addr)] = path_list
    test_obj.addr_dic['p2sh-p2wsh'] = addr
    test_obj.desc_dic[str(addr)] = parse_descriptor(
        'sh(wsh({}))'.format(desc_multi), network=NETWORK)
    print('set p2sh-p2wsh addr: ' + str(addr))
Beispiel #4
0
def test_address_func(obj, name, case, req, exp, error):
    try:
        resp = None
        _network = req.get('network', 'mainnet')
        if req.get('isElements', False) and (_network.lower()
                                             == Network.REGTEST.as_str()):
            _network = Network.ELEMENTS_REGTEST

        if name == 'Address.Create':
            _hash_type = HashType.get(req['hashType'])
            if _hash_type == HashType.P2PKH:
                resp = AddressUtil.p2pkh(req['keyData']['hex'],
                                         network=_network)
            elif _hash_type == HashType.P2WPKH:
                resp = AddressUtil.p2wpkh(req['keyData']['hex'],
                                          network=_network)
            elif _hash_type == HashType.P2SH_P2WPKH:
                resp = AddressUtil.p2sh_p2wpkh(req['keyData']['hex'],
                                               network=_network)
            elif _hash_type == HashType.P2SH:
                resp = AddressUtil.p2sh(req['keyData']['hex'],
                                        network=_network)
            elif _hash_type == HashType.P2WSH:
                resp = AddressUtil.p2wsh(req['keyData']['hex'],
                                         network=_network)
            elif _hash_type == HashType.P2SH_P2WSH:
                resp = AddressUtil.p2sh_p2wsh(req['keyData']['hex'],
                                              network=_network)
        elif name == 'Address.GetInfo':
            resp = AddressUtil.parse(req['address'])
        elif name == 'Address.MultisigAddresses':