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 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))
def test_pegin(test_obj): btc_rpc = test_obj.btcConn.get_rpc() elm_rpc = test_obj.elmConn.get_rpc() # generate pegin address path = '{}/0/0'.format(ROOT_PATH) main_ext_sk = test_obj.hdwallet.get_privkey(path=path) main_sk = str(main_ext_sk.privkey) main_pk = str(main_ext_sk.privkey.pubkey) pegin_address, claim_script, _ = AddressUtil.get_pegin_address( fedpeg_script=test_obj.fedpegscript, pubkey=main_pk, mainchain_network=Network.REGTEST) pegin_address = str(pegin_address) claim_script = claim_script.hex # pegin_addr_info = elm_rpc.getpeginaddress() # pegin_address = pegin_addr_info['mainchain_address'] # claim_script = pegin_addr_info['claim_script'] for i in range(3): try: blk_cnt = btc_rpc.getblockcount() + 1 # send bitcoin utxos = get_utxo(btc_rpc, []) amount = 0 for utxo in utxos: amount += utxo['amount'] amount -= 1 if amount > 100: amount = 100 txid = btc_rpc.sendtoaddress(pegin_address, amount) # generate bitcoin 100 block addr = str(test_obj.addr_dic['btc']) btc_rpc.generatetoaddress(101, addr) max_blk_cnt = btc_rpc.getblockcount() txout_proof = None for i in range(max_blk_cnt - blk_cnt): blk_hash = btc_rpc.getblockhash(blk_cnt + i) block_hex = btc_rpc.getblock(blk_hash, 0) block = Block(block_hex) if block.exist_txid(txid): tx_data, txout_proof = block.get_tx_data(txid) print(f'pegin block: {str(block)}') break if txout_proof is None: raise Exception('txoutproof is empty.') # pegin transaction for fee address # tx_data = btc_rpc.gettransaction(txid)['hex'] tx = Transaction(tx_data) vout = tx.get_txout_index(pegin_address) pegged_amount = tx.txout_list[vout].amount # txout_proof = btc_rpc.gettxoutproof([txid]) # pegin_tx = elm_rpc.createrawpegin( # tx_data, txout_proof, claim_script)['hex'] # pegin_tx = update_pegin_tx( # test_obj, pegin_tx, tx_data, pegin_address, txout_proof) pegin_tx = create_pegin_tx(test_obj, tx, pegin_address, txout_proof, claim_script) ct = ConfidentialTransaction(pegin_tx) ct.sign_with_privkey(OutPoint(txid, vout), HashType.P2WPKH, main_sk, pegged_amount) pegin_tx = str(ct) # broadcast print( ConfidentialTransaction.parse_to_json(pegin_tx, network=NETWORK)) txid = elm_rpc.sendrawtransaction(pegin_tx) test_obj.tx_dic[txid] = pegin_tx # generatetoaddress -> gen address addr = str(test_obj.addr_dic['gen']) elm_rpc.generatetoaddress(2, addr) time.sleep(2) except Exception as err: print('Exception({})'.format(i)) raise err # generatetoaddress -> gen address addr = str(test_obj.addr_dic['gen']) elm_rpc.generatetoaddress(100, addr) elm_rpc.generatetoaddress(5, addr) time.sleep(2) fee_addr = test_obj.addr_dic['fee'] utxos = get_utxo(elm_rpc, [str(fee_addr)]) # utxos = get_utxo(elm_rpc, []) print('UTXO: {}'.format(utxos))
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)
if 'multisig p2wsh' == case: print(str(resp)) elif name == 'ConfidentialTransaction.VerifySign': resp, txin = get_tx() err_list = [] for txin in req.get('txins', []): hash_type = HashType.P2WPKH addr = txin.get('address', '') desc = txin.get('descriptor', '') if desc != '': desc = parse_descriptor(desc, network=Network.LIQUID_V1) addr = desc.data.address hash_type = desc.data.hash_type elif addr != '': addr = AddressUtil.parse(addr) hash_type = addr.hash_type try: resp.verify_sign( OutPoint(txin['txid'], txin['vout']), addr, hash_type, txin.get('confidentialValueCommitment', txin.get('amount', 0))) except CfdError as err: _dict = {'txid': txin['txid'], 'vout': txin['vout']} _dict['reason'] = err.message err_list.append(_dict) success = (len(err_list) == 0) resp = {'success': success, 'failTxins': err_list}
from cfd.script import HashType from cfd.util import CfdError 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)
control_block=txin['controlBlock'], annex=txin.get('annex', None)) elif name == 'Transaction.VerifySign': err_list = [] utxos = load_utxo_list(req) for txin in req.get('txins', []): hash_type = HashType.P2WPKH addr = txin.get('address', '') desc = txin.get('descriptor', '') if desc != '': desc = parse_descriptor(desc) addr = desc.data.address hash_type = desc.data.hash_type elif addr != '': addr = AddressUtil.parse(addr) hash_type = addr.hash_type try: resp.verify_sign(OutPoint(txin['txid'], txin['vout']), addr, hash_type, txin.get('amount', 0), utxos) except CfdError as err: _dict = {'txid': txin['txid'], 'vout': txin['vout']} _dict['reason'] = err.message err_list.append(_dict) success = (len(err_list) == 0) resp = {'success': success, 'failTxins': err_list} elif name == 'Transaction.VerifySignature':
def test_taproot_single_key(test_obj: 'TestBitcoin'): btc_rpc = test_obj.conn.get_rpc() main_addr = test_obj.addr_dic['main'] main_pk, _ = SchnorrPubkey.from_pubkey(str(main_addr.pubkey)) pkh_addr = test_obj.addr_dic['p2pkh'] spk1, _ = SchnorrPubkey.from_pubkey(str(pkh_addr.pubkey)) wpkh_addr = test_obj.addr_dic['p2wpkh'] spk2, _ = SchnorrPubkey.from_pubkey(str(wpkh_addr.pubkey)) main_path = str(test_obj.path_dic[str(main_addr)]) main_sk = test_obj.hdwallet.get_privkey(path=main_path).privkey pkh_path = str(test_obj.path_dic[str(pkh_addr)]) sk1 = test_obj.hdwallet.get_privkey(path=pkh_path).privkey wpkh_path = str(test_obj.path_dic[str(wpkh_addr)]) sk2 = test_obj.hdwallet.get_privkey(path=wpkh_path).privkey branch = TapBranch() tr_addr1 = AddressUtil.taproot(main_pk, script_tree=branch, network=NETWORK) tr_sk1 = branch.get_privkey(main_sk) tr_addr2 = AddressUtil.taproot(spk1, script_tree=branch, network=NETWORK) tr_sk2 = branch.get_privkey(sk1) tr_addr3 = AddressUtil.taproot(spk2, script_tree=branch, network=NETWORK) tr_sk3 = branch.get_privkey(sk2) txouts = [ TxOut(100000, str(tr_addr1)), TxOut(150000, str(tr_addr2)), TxOut(200000, str(tr_addr3)), ] tx = Transaction.create(2, 0, [], txouts) # fundrawtransaction fee_addr = str(test_obj.addr_dic['fee']) fee_desc = test_obj.desc_dic[fee_addr] fee_sk = test_obj.hdwallet.get_privkey(path=FEE_PATH).privkey utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx.fund_raw_transaction([], utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign for txin in tx.txin_list: utxo = search_utxos(test_obj, utxo_list, txin.outpoint) tx.sign_with_privkey(txin.outpoint, fee_desc.data.hash_type, fee_sk, amount=utxo.amount, sighashtype=SigHashType.ALL) # broadcast print(Transaction.parse_to_json(str(tx), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) txid = tx.txid utxo1 = UtxoData(txid=txid, vout=0, amount=txouts[0].amount, descriptor=f'raw({str(tr_addr1.locking_script)})') utxo2 = UtxoData(txid=txid, vout=1, amount=txouts[1].amount, descriptor=f'raw({str(tr_addr2.locking_script)})') utxo3 = UtxoData(txid=txid, vout=2, amount=txouts[2].amount, descriptor=f'raw({str(tr_addr3.locking_script)})') # send taproot singleKey1 txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=0)) txin_utxo_list.append(utxo1) txouts2 = [ TxOut(txouts[0].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list: List['UtxoData'] = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sighash = tx2.get_sighash(txin.outpoint, HashType.TAPROOT, pubkey=tr_addr1.pubkey, sighashtype=SigHashType.DEFAULT, utxos=join_utxo_list) sig = SchnorrUtil.sign(sighash, tr_sk1) sign_param = SignParameter(sig, sighashtype=SigHashType.DEFAULT) tx2.add_taproot_sign(txin.outpoint, sign_param) else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) # send taproot singleKey2 txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=1)) txin_utxo_list.append(utxo2) txouts2 = [ TxOut(txouts[1].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sighash = tx2.get_sighash(txin.outpoint, HashType.TAPROOT, pubkey=tr_addr2.pubkey, sighashtype=SigHashType.DEFAULT, utxos=join_utxo_list) sig = SchnorrUtil.sign(sighash, tr_sk2) sign_param = SignParameter(sig, sighashtype=SigHashType.DEFAULT) tx2.add_taproot_sign(txin.outpoint, sign_param) else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) # send taproot singleKey3 txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=2)) txin_utxo_list.append(utxo3) txouts2 = [ TxOut(txouts[2].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sighash = tx2.get_sighash(txin.outpoint, HashType.TAPROOT, pubkey=tr_addr3.pubkey, sighashtype=SigHashType.DEFAULT, utxos=join_utxo_list) sig = SchnorrUtil.sign(sighash, tr_sk3) sign_param = SignParameter(sig, sighashtype=SigHashType.DEFAULT) tx2.add_taproot_sign(txin.outpoint, sign_param) else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) utxos = get_utxo(btc_rpc, [str(main_addr)]) print('UTXO: {}'.format(utxos))
from cfd.taproot import TapBranch, TaprootScriptTree from cfd.util import CfdError, ByteData 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)
def test_taproot_tapscript(test_obj: 'TestBitcoin'): btc_rpc = test_obj.conn.get_rpc() main_addr = test_obj.addr_dic['main'] main_pk, _ = SchnorrPubkey.from_pubkey(str(main_addr.pubkey)) pkh_addr = test_obj.addr_dic['p2pkh'] spk1, _ = SchnorrPubkey.from_pubkey(str(pkh_addr.pubkey)) wpkh_addr = test_obj.addr_dic['p2wpkh'] spk2, _ = SchnorrPubkey.from_pubkey(str(wpkh_addr.pubkey)) main_path = str(test_obj.path_dic[str(main_addr)]) main_sk = test_obj.hdwallet.get_privkey(path=main_path).privkey pkh_path = str(test_obj.path_dic[str(pkh_addr)]) sk1 = test_obj.hdwallet.get_privkey(path=pkh_path).privkey # wpkh_path = str(test_obj.path_dic[str(wpkh_addr)]) # sk2 = test_obj.hdwallet.get_privkey(path=wpkh_path).privkey script1 = Script.from_asm([str(spk1), 'OP_CHECKSIG']) script2 = Script.from_asm([str(spk2), 'OP_CHECKSIG']) op_true_script = Script('51') op_true_sub_tree1 = TaprootScriptTree(op_true_script) op_true_sub_tree1.add_branch(script1) script1_tree = TaprootScriptTree(script1) script1_tree.add_branches([op_true_script, script2]) script1_tree.internal_pubkey = main_pk op_true_tree = TaprootScriptTree(op_true_script) op_true_tree.add_branches([script1, script2]) op_true_tree.internal_pubkey = main_pk script2_tree = TaprootScriptTree(script2) script2_tree.add_branch(op_true_sub_tree1) script2_tree.internal_pubkey = main_pk tr_addr1 = AddressUtil.taproot(script1_tree, network=NETWORK) tr_addr2 = AddressUtil.taproot(op_true_tree, network=NETWORK) tr_addr3 = AddressUtil.taproot(script2_tree, network=NETWORK) txouts = [ TxOut(100000, str(tr_addr1)), TxOut(150000, str(tr_addr2)), TxOut(200000, str(tr_addr3)), ] tx = Transaction.create(2, 0, [], txouts) # fundrawtransaction fee_addr = str(test_obj.addr_dic['fee']) fee_desc = test_obj.desc_dic[fee_addr] fee_sk = test_obj.hdwallet.get_privkey(path=FEE_PATH).privkey utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx.fund_raw_transaction([], utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign for txin in tx.txin_list: utxo = search_utxos(test_obj, utxo_list, txin.outpoint) tx.sign_with_privkey(txin.outpoint, fee_desc.data.hash_type, fee_sk, amount=utxo.amount, sighashtype=SigHashType.ALL) # broadcast print(Transaction.parse_to_json(str(tx), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) txid = tx.txid utxo1 = UtxoData(txid=txid, vout=0, amount=txouts[0].amount, descriptor=f'raw({str(tr_addr1.locking_script)})') utxo2 = UtxoData(txid=txid, vout=1, amount=txouts[1].amount, descriptor=f'raw({str(tr_addr2.locking_script)})') utxo3 = UtxoData(txid=txid, vout=2, amount=txouts[2].amount, descriptor=f'raw({str(tr_addr3.locking_script)})') # send tapscript script1 txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=0)) txin_utxo_list.append(utxo1) txouts2 = [ TxOut(txouts[0].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list: List['UtxoData'] = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sk = sk1 sighash = tx2.get_sighash(txin.outpoint, HashType.TAPROOT, redeem_script=script1, sighashtype=SigHashType.DEFAULT, utxos=join_utxo_list) sig = SchnorrUtil.sign(sighash, sk1) sign_param = SignParameter(sig, sighashtype=SigHashType.DEFAULT) _, _, _, control_block = script1_tree.get_taproot_data() tx2.add_tapscript_sign(txin.outpoint, [sign_param], script1, control_block) else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) # send tapscript OP_TRUE txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=1)) txin_utxo_list.append(utxo2) txouts2 = [ TxOut(txouts[1].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: _, _, _, control_block = op_true_tree.get_taproot_data() tx2.add_tapscript_sign(txin.outpoint, [], op_true_script, control_block) else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) # send tapscript internal_pubkey txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=2)) txin_utxo_list.append(utxo3) txouts2 = [ TxOut(txouts[2].amount, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign join_utxo_list = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sk = script2_tree.get_privkey(main_sk) hash_type = tr_addr3.hash_type else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) utxos = get_utxo(btc_rpc, [str(main_addr)]) print('UTXO: {}'.format(utxos))
def test_taproot_schnorr(test_obj: 'TestBitcoin'): btc_rpc = test_obj.conn.get_rpc() main_addr = test_obj.addr_dic['main'] main_pk, _ = SchnorrPubkey.from_pubkey(str(main_addr.pubkey)) tr_addr = AddressUtil.taproot(main_pk, network=NETWORK) main_path = str(test_obj.path_dic[str(main_addr)]) main_sk = test_obj.hdwallet.get_privkey(path=main_path).privkey txouts = [ TxOut(100000000, str(tr_addr)), ] tx = Transaction.create(2, 0, [], txouts) # fundrawtransaction fee_addr = str(test_obj.addr_dic['fee']) fee_desc = test_obj.desc_dic[fee_addr] fee_sk = test_obj.hdwallet.get_privkey(path=FEE_PATH).privkey utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx.fund_raw_transaction([], utxo_list, fee_addr, target_amount=0, effective_fee_rate=2.0, knapsack_min_change=0) # add sign for txin in tx.txin_list: utxo = search_utxos(test_obj, utxo_list, txin.outpoint) tx.sign_with_privkey(txin.outpoint, fee_desc.data.hash_type, fee_sk, amount=utxo.amount, sighashtype=SigHashType.ALL) # broadcast print(Transaction.parse_to_json(str(tx), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) # create tx (output wpkh only, input tx1-3) txid = tx.txid txin_list = [] txin_utxo_list = [] txin_list.append(TxIn(txid=txid, vout=0)) desc = f'raw({str(tr_addr.locking_script)})' txin_utxo_list.append( UtxoData(txid=txid, vout=0, amount=txouts[0].amount, descriptor=desc)) txouts2 = [ TxOut(100000000, str(test_obj.addr_dic['main'])), ] tx2 = Transaction.create(2, 0, txin_list, txouts2) main_addr = test_obj.addr_dic['main'] utxos = get_utxo(btc_rpc, [fee_addr]) utxo_list = convert_bitcoin_utxos(test_obj, utxos) tx2.fund_raw_transaction(txin_utxo_list, utxo_list, fee_addr, target_amount=0, effective_fee_rate=20.0, knapsack_min_change=1) # add sign join_utxo_list: List['UtxoData'] = [] join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list for txin in tx2.txin_list: for utxo in utxo_list: if utxo.outpoint == txin.outpoint: join_utxo_list.append(utxo) for index, txin in enumerate(tx2.txin_list): utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint) if index == 0: sk = main_sk hash_type = main_addr.hash_type else: path = str(test_obj.path_dic[str(utxo.descriptor.data.address)]) sk = test_obj.hdwallet.get_privkey(path=path).privkey hash_type = utxo.descriptor.data.hash_type tx2.sign_with_privkey(txin.outpoint, hash_type, sk, amount=utxo.amount, sighashtype=SigHashType.ALL, utxos=join_utxo_list) # broadcast print(Transaction.parse_to_json(str(tx2), network=NETWORK)) btc_rpc.sendrawtransaction(str(tx2)) # generate block btc_rpc.generatetoaddress(2, fee_addr) time.sleep(2) utxos = get_utxo(btc_rpc, [str(main_addr)]) print('UTXO: {}'.format(utxos))
def test_elements_dynafed(test_obj): btc_rpc = test_obj.btcConn.get_rpc() elm_rpc = test_obj.elmConn.get_rpc() # generate block chaininfo = elm_rpc.getblockchaininfo() epoch_length = chaininfo['epoch_length'] epoch_age = chaininfo['epoch_age'] gen_num = epoch_length - epoch_age - 1 addr = str(test_obj.addr_dic['gen']) elm_rpc.generatetoaddress(gen_num, addr) # generate dynafed block block_data = elm_rpc.getnewblockhex( 0, { "signblockscript": WSH_OP_TRUE, "max_block_witness": 500, "fedpegscript": new_fedpeg_script, "extension_space": [pak1], }) elm_rpc.submitblock(block_data) elm_rpc.getblockchaininfo() elm_rpc.getsidechaininfo() elm_rpc.getblock(chaininfo['bestblockhash']) generatetoaddress_dynafed(test_obj, epoch_length) time.sleep(2) chaininfo = elm_rpc.getblockchaininfo() sidechaininfo = elm_rpc.getsidechaininfo() # generate pegin address path = '{}/0/0'.format(ROOT_PATH) main_ext_sk = test_obj.hdwallet.get_privkey(path=path) main_sk = str(main_ext_sk.privkey) main_pk = str(main_ext_sk.privkey.pubkey) pegin_address, claim_script, tweaked = AddressUtil.get_pegin_address( fedpeg_script=new_fedpeg_script, pubkey=main_pk, mainchain_network=Network.REGTEST, hash_type=HashType.P2WSH) # TODO: Dynafed mode (need p2wsh) pegin_address = str(pegin_address) claim_script = claim_script.hex print(f'pegin_address[{pegin_address}]') print(f'claim_script[{claim_script}]') print(f'tweaked_fedpeg_script[{tweaked}]') # pegin_addr_info = elm_rpc.getpeginaddress() # pegin_address = pegin_addr_info['mainchain_address'] # claim_script = pegin_addr_info['claim_script'] for i in range(3): try: blk_cnt = btc_rpc.getblockcount() + 1 # send bitcoin utxos = get_utxo(btc_rpc, []) amount = 0 for utxo in utxos: amount += utxo['amount'] amount -= 1 if amount > 100: amount = 100 txid = btc_rpc.sendtoaddress(pegin_address, amount) # generate bitcoin 100 block addr = str(test_obj.addr_dic['btc']) btc_rpc.generatetoaddress(101, addr) max_blk_cnt = btc_rpc.getblockcount() txout_proof = None for i in range(max_blk_cnt - blk_cnt): blk_hash = btc_rpc.getblockhash(blk_cnt + i) block_hex = btc_rpc.getblock(blk_hash, 0) block = Block(block_hex) if block.exist_txid(txid): tx_data, txout_proof = block.get_tx_data(txid) print(f'pegin block: {str(block)}') break if txout_proof is None: raise Exception('txoutproof is empty.') # pegin transaction for fee address # tx_data = btc_rpc.gettransaction(txid)['hex'] tx = Transaction(tx_data) vout = tx.get_txout_index(pegin_address) pegged_amount = tx.txout_list[vout].amount # txout_proof = btc_rpc.gettxoutproof([txid]) # pegin_tx = elm_rpc.createrawpegin( # tx_data, txout_proof, claim_script)['hex'] # pegin_tx = update_pegin_tx( # test_obj, pegin_tx, tx_data, pegin_address, txout_proof) pegin_tx = create_pegin_tx(test_obj, tx, pegin_address, txout_proof, claim_script) ct = ConfidentialTransaction(pegin_tx) ct.sign_with_privkey(OutPoint(txid, vout), HashType.P2WPKH, main_sk, pegged_amount) pegin_tx = str(ct) # broadcast print( ConfidentialTransaction.parse_to_json(pegin_tx, network=NETWORK)) txid = elm_rpc.sendrawtransaction(pegin_tx) test_obj.tx_dic[txid] = pegin_tx # generatetoaddress -> gen address addr = str(test_obj.addr_dic['gen']) # elm_rpc.generatetoaddress(2, addr) generatetoaddress_dynafed(test_obj, 2) time.sleep(2) except Exception as err: print('Exception({})'.format(i)) raise err # generatetoaddress -> gen address addr = str(test_obj.addr_dic['gen']) # elm_rpc.generatetoaddress(100, addr) generatetoaddress_dynafed(test_obj, 100) # elm_rpc.generatetoaddress(5, addr) generatetoaddress_dynafed(test_obj, 5) time.sleep(2) fee_addr = test_obj.addr_dic['fee'] utxos = get_utxo(elm_rpc, [str(fee_addr)]) # utxos = get_utxo(elm_rpc, []) print('UTXO: {}'.format(utxos)) # pegout pegout_amount = 1000000 counter = 3 mainchain_bip32 = 'tpubDDbMfNVnS7fmrTyv98A1bPydovdx2GhaxVAfvgPztEw3R3J2bZ7c2yy3oHx1D3ivjEH5tidRdA766QC83omWBtoUN7CBrk6vyogkTEPUb5b' # noqa: E501 pegout_descriptor = f'pkh({mainchain_bip32}/0/*)' online_key = 'cVSf1dmLm1XjafyXSXn955cyb2uabdtXxjBXx6fHMQLPQKzHCpT7' online_pubkey = \ '024fb0908ea9263bedb5327da23ff914ce1883f851337d71b3ca09b32701003d05' whitelist = ''.join(chaininfo['extension_space']) txouts = [ ConfidentialTxOut(100000000, test_obj.ct_addr_dic[str( test_obj.addr_dic['p2wsh'])], asset=test_obj.pegged_asset), ] tx = ConfidentialTransaction.create(2, 0, [], txouts) tx.add_pegout_output( asset=test_obj.pegged_asset, amount=pegout_amount, mainchain_network_type=Network.REGTEST, elements_network_type=Network.ELEMENTS_REGTEST, mainchain_genesis_block_hash=sidechaininfo['parent_blockhash'], online_pubkey=online_pubkey, master_online_key=online_key, mainchain_output_descriptor=pegout_descriptor, bip32_counter=counter, whitelist=whitelist, ) # fundrawtransaction fee_addr = str(test_obj.addr_dic['fee']) fee_desc = test_obj.desc_dic[fee_addr] fee_ct_addr = test_obj.ct_addr_dic[fee_addr] fee_sk = test_obj.hdwallet.get_privkey(path=FEE_PATH).privkey # utxos = get_utxo(elm_rpc, [fee_addr]) utxo_list = convert_elements_utxos(test_obj, utxos) target_list = [ TargetAmountData(amount=1, asset=test_obj.pegged_asset, reserved_address=fee_ct_addr) ] tx.fund_raw_transaction([], utxo_list, fee_asset=test_obj.pegged_asset, target_list=target_list, effective_fee_rate=0.1, knapsack_min_change=1) # blind blind_utxo_list = [] for txin in tx.txin_list: blind_utxo_list.append(search_utxos(test_obj, utxo_list, txin.outpoint)) tx.blind_txout(blind_utxo_list) # add sign for txin in tx.txin_list: utxo = search_utxos(test_obj, utxo_list, txin.outpoint) tx.sign_with_privkey(txin.outpoint, fee_desc.data.hash_type, fee_sk, value=utxo.value, sighashtype=SigHashType.ALL) # broadcast print(ConfidentialTransaction.parse_to_json(str(tx), network=NETWORK)) elm_rpc.sendrawtransaction(str(tx)) # generate block elm_rpc.generatetoaddress(2, fee_addr) time.sleep(2)
from cfd.address import AddressUtil PUBKEY = '027592aab5d43618dda13fba71e3993cd7517a712d3da49664c06ee1bd3d1f70af' SCRIPT_PUBKEY = '76a914925d4028880bd0c9d68fbc7fc7dfee976698629c88ac' if __name__ == '__main__': addr = AddressUtil.p2pkh(PUBKEY) if str(addr) != '1ELuNB5fLNUcrLzb93oJDPmjxjnsVwhNHn': print('invalid address: ' + str(addr)) else: print('address: ' + str(addr)) if str(addr.locking_script) != SCRIPT_PUBKEY: print('invalid script: ' + str(addr.locking_script)) else: print('locking script: ' + str(addr.locking_script))
psbt = Psbt(req['psbt'], network=req.get( 'network', Network.MAINNET)) psbt.sign(privkey=req['privkey'], has_grind_r=req.get('hasGrindR', True)) resp = psbt elif name == 'Psbt.AddPsbtData': net_type = Network.get(req.get('network', Network.MAINNET)) psbt = Psbt(req['psbt'], network=net_type) for input_data in req.get('inputs', []): txin = input_data['txin'] input = input_data['input'] utxo = TxOut(0) if 'witnessUtxo' in input: addr = '' if 'address' in input['witnessUtxo']: addr = AddressUtil.parse( input['witnessUtxo']['address']) utxo = TxOut( input['witnessUtxo']['amount'], address=addr, locking_script=input['witnessUtxo'].get( 'directLockingScript', '')) script = '' if 'redeemScript' not in input else Script( input['redeemScript']) tx = '' if 'utxoFullTx' not in input else Transaction( input['utxoFullTx']) outpoint = OutPoint(txin['txid'], txin['vout']) psbt.add_input( outpoint, utxo=utxo, redeem_script=script,