Ejemplo n.º 1
0
 def get_tx():
     resp = ''
     if 'tx' in req:
         resp = ConfidentialTransaction.from_hex(req['tx'])
     txins, txouts = [], []
     for input in req.get('txins', []):
         txins.append(TxIn(txid=input['txid'], vout=input['vout'],
                           sequence=input.get('sequence',
                                              TxIn.SEQUENCE_DISABLE)))
     for output in req.get('txouts', []):
         txouts.append(ConfidentialTxOut(
             output['amount'], address=output.get('address', ''),
             locking_script=output.get('directLockingScript', ''),
             asset=output.get('asset', ''),
             nonce=output.get('directNonce', '')))
     for output in req.get('destroyAmountTxouts', []):
         txouts.append(ConfidentialTxOut.for_destroy_amount(
             output['amount'], asset=output.get('asset', ''),
             nonce=output.get('directNonce', '')))
     if 'fee' in req:
         output = req['fee']
         if 'amount' in output:
             txouts.append(ConfidentialTxOut.for_fee(
                 output['amount'], asset=output.get('asset', '')))
     return resp, txins, txouts
Ejemplo n.º 2
0
 def get_tx():
     resp, txin = None, None
     if 'tx' in req:
         resp = ConfidentialTransaction.from_hex(req['tx'])
     if 'txin' in req:
         txin = req['txin']
     return resp, txin
Ejemplo n.º 3
0
def create_pegin_tx(test_obj, btc_tx: 'Transaction', pegin_address,
                    txout_proof, claim_script) -> str:
    btc_tx_obj = btc_tx
    btc_txid = btc_tx_obj.txid
    btc_txout_index = btc_tx_obj.get_txout_index(address=pegin_address)
    btc_amount = btc_tx_obj.txout_list[btc_txout_index].amount
    btc_size = len(str(btc_tx)) / 2
    txoutproof_size = len(str(txout_proof)) / 2

    # add txout
    tx = ConfidentialTransaction.create(2, 0)
    tx.add_pegin_input(outpoint=OutPoint(btc_txid, btc_txout_index),
                       amount=btc_amount,
                       asset=test_obj.pegged_asset,
                       mainchain_genesis_block_hash=test_obj.parent_blockhash,
                       claim_script=claim_script,
                       mainchain_tx=btc_tx,
                       txout_proof=txout_proof)
    fee_addr = test_obj.addr_dic['fee']
    tx.add_txout(amount=1,
                 address=test_obj.ct_addr_dic[str(fee_addr)],
                 asset=test_obj.pegged_asset)
    target_index = 0
    send2_amount = 1000
    tx.add_txout(amount=send2_amount,
                 address=test_obj.ct_addr_dic[str(test_obj.addr_dic['main'])],
                 asset=test_obj.pegged_asset)
    tx.add_fee_txout(amount=1, asset=test_obj.pegged_asset)

    # calc fee
    pegin_utxo = ElementsUtxoData(
        txid=btc_txid,
        vout=btc_txout_index,
        amount=btc_amount,
        descriptor='wpkh({})'.format('02' * 33),  # dummy
        asset=test_obj.pegged_asset,
        is_pegin=True,
        pegin_btc_tx_size=int(btc_size),
        pegin_txoutproof_size=int(txoutproof_size),
        claim_script=claim_script)
    utxo_list = [pegin_utxo]
    minimum_bits = 52
    calc_fee, _, _ = tx.estimate_fee(utxo_list,
                                     test_obj.pegged_asset,
                                     fee_rate=0.1,
                                     minimum_bits=minimum_bits)
    # update fee
    tx.update_txout_fee_amount(calc_fee)

    # change amount
    new_amount = btc_amount - calc_fee - send2_amount
    tx.update_txout_amount(target_index, new_amount)

    # blind
    print('before blind tx=', str(tx))
    tx.blind_txout(utxo_list, minimum_bits=minimum_bits)
    return str(tx)
Ejemplo n.º 4
0
def test_pegin(test_obj):
    btc_rpc = test_obj.btcConn.get_rpc()
    elm_rpc = test_obj.elmConn.get_rpc()

    # generate pegin address by RPC
    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:
            # 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)

            # pegin transaction for fee address
            tx_data = btc_rpc.gettransaction(txid)['hex']
            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)
            pegin_tx = elm_rpc.signrawtransactionwithwallet(pegin_tx)['hex']
            # 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))
Ejemplo n.º 5
0
    def test_parse_unblind_tx(self):
        tx_hex = '020000000001319bff5f4311e6255ecf4dd472650a6ef85fde7d11cd10d3e6ba5974174aeb560100000000ffffffff0201f38611eb688e6fcd06f25e2faf52b9f98364dc14c379ab085f1b57d56b4b1a6f0100000bd2cc1584c002deb65cc52301e1622f482a2f588b9800d2b8386ffabf74d6b2d73d17503a2f921976a9146a98a3f2935718df72518c00768ec67c589e0b2888ac01f38611eb688e6fcd06f25e2faf52b9f98364dc14c379ab085f1b57d56b4b1a6f0100000000004c4b40000000000000'  # noqa: E501
        tx = ConfidentialTransaction.from_hex(tx_hex)

        self.assertEqual(2, len(tx.txout_list))
        self.assertEqual(
            '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
            str(tx.txout_list[0].asset))
        self.assertEqual(
            12999995000000,
            tx.txout_list[0].value.amount)
        self.assertEqual(
            '0100000bd2cc1584c0',
            tx.txout_list[0].value.hex)
        self.assertEqual(
            '02deb65cc52301e1622f482a2f588b9800d2b8386ffabf74d6b2d73d17503a2f92',  # noqa: E501
            str(tx.txout_list[0].nonce))
        self.assertEqual(
            '6f1a4b6bd5571b5f08ab79c314dc6483f9b952af2f5ef206cd6f8e68eb1186f3',
            str(tx.txout_list[1].asset))
        self.assertEqual(
            5000000,
            tx.txout_list[1].value.amount)
        self.assertEqual(
            '0100000000004c4b40',
            tx.txout_list[1].value.hex)
        self.assertEqual(
            '',
            str(tx.txout_list[1].nonce))

        self.assertEqual(1, tx.get_txout_fee_index())
        self.assertEqual('Q6z1cAcrPxMCnsjAUjSgyT2DrSqRR6KZMr',
                         str(tx.txout_list[0].get_address()))
        self.assertEqual(
            'VTpz4UGuFrPeMdFvW6dzq1vH3ZumciG6jmGnCUidgqsY5RHRxbGfLjndgUjzECCzQnNwAGoP8ohYdHXv',  # noqa: E501
            str(tx.txout_list[0].get_confidential_address()))
        self.assertEqual(
            None,
            tx.txout_list[1].get_confidential_address())
Ejemplo n.º 6
0
def test_elements_multisig(test_obj):
    # btc_rpc = test_obj.btcConn.get_rpc()
    elm_rpc = test_obj.elmConn.get_rpc()
    # create tx (output multisig)
    txouts = [
        ConfidentialTxOut(100000000,
                          test_obj.ct_addr_dic[str(test_obj.addr_dic['p2sh'])],
                          asset=test_obj.pegged_asset),
        ConfidentialTxOut(100000000,
                          test_obj.ct_addr_dic[str(
                              test_obj.addr_dic['p2wsh'])],
                          asset=test_obj.pegged_asset),
        ConfidentialTxOut(100000000,
                          test_obj.ct_addr_dic[str(
                              test_obj.addr_dic['p2sh-p2wsh'])],
                          asset=test_obj.pegged_asset),
    ]
    tx = ConfidentialTransaction.create(2, 0, [], txouts)
    # 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)

    # create tx (output wpkh only, input multisig tx1-3)
    txid = tx.txid
    txin_list = []
    txin_utxo_list = []
    for index, txout in enumerate(tx.txout_list):
        if not txout.locking_script.hex:
            continue
        temp_addr = str(txout.get_address(network=NETWORK))
        if temp_addr == fee_addr:
            continue
        txin_list.append(ConfidentialTxIn(txid=txid, vout=index))
        if temp_addr not in test_obj.desc_dic:
            test_obj.assertTrue(
                False, 'addr not found. [{}]:[{}]'.format(index, temp_addr))
        desc = test_obj.desc_dic[temp_addr]
        blind_key = test_obj.blind_key_dic[temp_addr]
        unblind_data = tx.unblind_txout(index, blind_key)
        txin_utxo_list.append(
            ElementsUtxoData(txid=txid,
                             vout=index,
                             amount=unblind_data.value.amount,
                             descriptor=desc,
                             value=txout.value.hex,
                             asset=test_obj.pegged_asset,
                             asset_blinder=unblind_data.asset_blinder,
                             amount_blinder=unblind_data.amount_blinder))
    txouts2 = [
        ConfidentialTxOut(300000000,
                          test_obj.ct_addr_dic[str(test_obj.addr_dic['main'])],
                          asset=test_obj.pegged_asset),
    ]
    tx2 = ConfidentialTransaction.create(2, 0, txin_list, txouts2)
    main_addr = test_obj.addr_dic['main']
    utxos = get_utxo(elm_rpc, [fee_addr])
    utxo_list = convert_elements_utxos(test_obj, utxos)
    target_list = [
        TargetAmountData(amount=0,
                         asset=test_obj.pegged_asset,
                         reserved_address=fee_ct_addr)
    ]
    tx2.fund_raw_transaction(txin_utxo_list,
                             utxo_list,
                             fee_asset=test_obj.pegged_asset,
                             target_list=target_list,
                             effective_fee_rate=0.1,
                             knapsack_min_change=1)
    # blind
    join_utxo_list = []
    join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = utxo_list
    join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list
    blind_utxo_list = []
    for txin in tx2.txin_list:
        blind_utxo_list.append(
            search_utxos(test_obj, join_utxo_list, txin.outpoint))
    tx2.blind_txout(blind_utxo_list)

    def multisig_sign(tx_obj, utxo, path_list):
        sighash = tx_obj.get_sighash(
            outpoint=utxo.outpoint,
            hash_type=utxo.descriptor.data.hash_type,
            value=utxo.value,
            redeem_script=utxo.descriptor.data.redeem_script)
        signature_list = []
        for path in path_list:
            sk = test_obj.hdwallet.get_privkey(path=path).privkey
            sig = sk.calculate_ec_signature(sighash)
            sig.related_pubkey = sk.pubkey
            signature_list.append(sig)
            if len(signature_list) == 2:
                break
        tx_obj.add_multisig_sign(utxo.outpoint, utxo.descriptor.data.hash_type,
                                 utxo.descriptor.data.redeem_script,
                                 signature_list)

    # add sign
    join_utxo_list = []
    join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = utxo_list
    join_utxo_list[len(join_utxo_list):len(join_utxo_list)] = txin_utxo_list
    for index, txin in enumerate(tx2.txin_list):
        utxo = search_utxos(test_obj, join_utxo_list, txin.outpoint)
        if not utxo.descriptor.data.redeem_script:
            path = test_obj.path_dic[str(utxo.descriptor.data.address)]
            sk = test_obj.hdwallet.get_privkey(path=path).privkey
            tx2.sign_with_privkey(txin.outpoint,
                                  utxo.descriptor.data.hash_type,
                                  sk,
                                  value=utxo.value,
                                  sighashtype=SigHashType.ALL)
        else:
            path_list = test_obj.path_dic[str(utxo.descriptor.data.address)]
            multisig_sign(tx2, utxo, path_list)
    # broadcast
    print(ConfidentialTransaction.parse_to_json(str(tx2), network=NETWORK))
    elm_rpc.sendrawtransaction(str(tx2))
    # generate block
    elm_rpc.generatetoaddress(2, fee_addr)
    time.sleep(2)
    utxos = get_utxo(elm_rpc, [str(main_addr)])
    print('UTXO: {}'.format(utxos))
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
def update_pegin_tx(test_obj, pegin_tx, btc_tx, pegin_address, claim_script,
                    txout_proof) -> str:
    pegin_tx2 = pegin_tx
    btc_tx_obj = Transaction.from_hex(btc_tx)
    btc_txid = btc_tx_obj.txid
    btc_txout_index = btc_tx_obj.get_txout_index(address=pegin_address)
    btc_amount = btc_tx_obj.txout_list[btc_txout_index].amount
    btc_size = len(btc_tx) / 2
    txoutproof_size = len(txout_proof) / 2

    # decode
    tx = ConfidentialTransaction.from_hex(pegin_tx)
    target_script_pubkey = ''
    target_amount = 0
    target_index = 0
    # fee_index = -1
    fee_amount = 0
    has_fee = len(tx.txout_list) == 2
    for index, txout in enumerate(tx.txout_list):
        if len(txout.locking_script.hex) > 0:
            target_script_pubkey = str(txout.locking_script)
            target_amount = txout.amount
            target_index = index
        else:
            fee_amount = txout.amount
            # fee_index = index
    # change script pubkey (string replace)
    target_script_pubkey = '16' + target_script_pubkey

    fee_addr = test_obj.addr_dic['fee']
    new_script_pubkey = '16' + str(fee_addr.locking_script)
    pegin_tx2 = pegin_tx.replace(target_script_pubkey, new_script_pubkey)
    tx = ConfidentialTransaction.from_hex(pegin_tx2)
    total_amount = target_amount + fee_amount
    utxo_amount = 0
    if has_fee:
        utxo_amount = total_amount - btc_amount

    # add txout
    tx.add_txout(amount=1,
                 address=test_obj.ct_addr_dic[str(test_obj.addr_dic['main'])],
                 asset=test_obj.pegged_asset)

    # calc fee
    pegin_utxo = ElementsUtxoData(
        txid=btc_txid,
        vout=btc_txout_index,
        amount=btc_amount,
        descriptor='wpkh({})'.format('02' * 33),  # dummy
        asset=test_obj.pegged_asset,
        is_pegin=True,
        pegin_btc_tx_size=int(btc_size),
        pegin_txoutproof_size=int(txoutproof_size),
        claim_script=claim_script)
    utxo_list = [pegin_utxo]
    if utxo_amount > 0:
        for txin in tx.txin_list:
            if txin.outpoint.txid != btc_txid:
                utxo = ElementsUtxoData(outpoint=txin.outpoint,
                                        amount=utxo_amount,
                                        descriptor='',
                                        asset=test_obj.pegged_asset)
                utxo_list.append(utxo)
                break
    calc_fee, _, _ = tx.estimate_fee(utxo_list, test_obj.pegged_asset)
    # update fee
    tx.update_txout_fee_amount(calc_fee)

    # change amount
    new_amount = total_amount - calc_fee - 1
    tx.update_txout_amount(target_index, new_amount)

    # blind
    fee_ct_addr = test_obj.ct_addr_dic[str(fee_addr)]
    print('before blind tx=', str(tx))
    tx.blind_txout(utxo_list, confidential_address_list=[fee_ct_addr])
    return str(tx)
Ejemplo n.º 9
0
                    asset=output.get('asset', ''),
                    nonce=output.get('directNonce', '')))
            for output in req.get('destroyAmountTxouts', []):
                txouts.append(ConfidentialTxOut.for_destroy_amount(
                    output['amount'], asset=output.get('asset', ''),
                    nonce=output.get('directNonce', '')))
            if 'fee' in req:
                output = req['fee']
                if 'amount' in output:
                    txouts.append(ConfidentialTxOut.for_fee(
                        output['amount'], asset=output.get('asset', '')))
            return resp, txins, txouts

        if name == 'ConfidentialTransaction.Create':
            resp, txins, txouts = get_tx()
            resp = ConfidentialTransaction.create(
                req['version'], req['locktime'], txins, txouts)
        elif name == 'ConfidentialTransaction.Add':
            resp, txins, txouts = get_tx()
            if len(txins) + len(txouts) == 1:
                for input in req.get('txins', []):
                    resp.add_txin(txid=input['txid'], vout=input['vout'],
                                  sequence=input.get('sequence',
                                                     TxIn.SEQUENCE_DISABLE))
                for output in req.get('txouts', []):
                    resp.add_txout(
                        output['amount'], address=output.get('address', ''),
                        locking_script=output.get('directLockingScript', ''),
                        asset=output.get('asset', ''),
                        nonce=output.get('directNonce', ''))
                for output in req.get('destroyAmountTxouts', []):
                    resp.add_destroy_amount_txout(
Ejemplo n.º 10
0
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)