From b8ec6e6bf86e6ea0200a8254c508c7a7c643013f Mon Sep 17 00:00:00 2001 From: "K.Matsuzawa" <49175372+k-matsuzawa@users.noreply.github.com> Date: Fri, 30 Oct 2020 20:00:40 +0900 Subject: [PATCH] update to v0.2.1 (#2) update from cryptogarageinc v0.2.1 (#2) --- VERSION | 2 +- cfd/confidential_address.py | 19 +++- cfd/confidential_transaction.py | 22 ++++- cfd/transaction.py | 13 ++- tests/data/bitcoin_coin_test.json | 44 +++++++++ tests/data/elements_coin_test.json | 146 +++++++++++++++++++++++++++++ 6 files changed, 242 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 341cf11..7dff5b8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 \ No newline at end of file +0.2.1 \ No newline at end of file diff --git a/cfd/confidential_address.py b/cfd/confidential_address.py index 9b2e167..09dfbae 100644 --- a/cfd/confidential_address.py +++ b/cfd/confidential_address.py @@ -3,7 +3,7 @@ # @file confidential_address.py # @brief elements confidential address function implements file. # @note Copyright 2020 CryptoGarage -from .util import get_util, to_hex_string +from .util import get_util, to_hex_string, CfdError ## @@ -20,6 +20,23 @@ class ConfidentialAddress: # @var confidential_key # confidential key + ## + # @brief check confidential address. + # @param[in] confidential_address confidential address + # @retval True confidential address + # @retval False other + @classmethod + def valid(cls, confidential_address): + util = get_util() + try: + with util.create_handle() as handle: + _, _, _ = util.call_func( + 'CfdParseConfidentialAddress', handle.get_handle(), + str(confidential_address)) + return True + except CfdError: + return False + ## # @brief parse confidential address. # @param[in] confidential_address confidential address diff --git a/cfd/confidential_transaction.py b/cfd/confidential_transaction.py index 8471f61..61de8ba 100644 --- a/cfd/confidential_transaction.py +++ b/cfd/confidential_transaction.py @@ -5,10 +5,12 @@ # @note Copyright 2020 CryptoGarage from .util import ReverseByteData, CfdError, JobHandle,\ CfdErrorCode, to_hex_string, get_util, ByteData +from .address import Address, AddressUtil from .key import Network, SigHashType, Privkey from .script import HashType from .transaction import UtxoData, OutPoint, Txid, TxIn, TxOut, _FundTxOpt,\ _TransactionBase +from .confidential_address import ConfidentialAddress from enum import Enum import copy import ctypes @@ -1379,10 +1381,28 @@ def set_opt(handle, tx_handle, key, i_val=0, f_val=0, b_val=False): tx_handle.get_handle(), key.value, int(i_val), float(f_val), b_val) + network = self.NETWORK + for target in target_list: + if len(str(target.reserved_address)) > 0: + check_addr = target.reserved_address + if ConfidentialAddress.valid(check_addr): + if isinstance(check_addr, ConfidentialAddress): + check_addr = check_addr.address + else: + check_addr = ConfidentialAddress.parse( + check_addr).address + if not isinstance(check_addr, Address): + check_addr = AddressUtil.parse(check_addr) + temp_network = Network.get(check_addr.network) + if temp_network in [Network.LIQUID_V1, + Network.ELEMENTS_REGTEST]: + network = temp_network.value + break + with util.create_handle() as handle: work_handle = util.call_func( 'CfdInitializeFundRawTx', handle.get_handle(), - self.NETWORK, len(target_list), str(fee_asset)) + network, len(target_list), str(fee_asset)) with JobHandle(handle, work_handle, 'CfdFreeFundRawTxHandle') as tx_handle: for utxo in txin_utxo_list: diff --git a/cfd/transaction.py b/cfd/transaction.py index 761061f..c59de70 100644 --- a/cfd/transaction.py +++ b/cfd/transaction.py @@ -1085,10 +1085,21 @@ def set_opt(handle, tx_handle, key, i_val=0, f_val=0, b_val=False): tx_handle.get_handle(), int(key.value), int(i_val), float(f_val), b_val) + network = self.NETWORK + if len(str(reserved_address)) > 0: + if isinstance(reserved_address, Address): + check_addr = reserved_address + else: + check_addr = AddressUtil.parse(reserved_address) + temp_network = Network.get(check_addr.network) + if temp_network in [Network.MAINNET, + Network.TESTNET, Network.REGTEST]: + network = temp_network.value + with util.create_handle() as handle: word_handle = util.call_func( 'CfdInitializeFundRawTx', handle.get_handle(), - self.NETWORK, 1, '') + network, 1, '') with JobHandle(handle, word_handle, 'CfdFreeFundRawTxHandle') as tx_handle: for utxo in txin_utxo_list: diff --git a/tests/data/bitcoin_coin_test.json b/tests/data/bitcoin_coin_test.json index ab0431b..39a1a60 100644 --- a/tests/data/bitcoin_coin_test.json +++ b/tests/data/bitcoin_coin_test.json @@ -449,6 +449,50 @@ ], "feeAmount": 7460 } + }, + { + "case": "regtest address", + "request": { + "utxoFile": "utxo_3", + "selectUtxos": [], + "tx": "02000000000280969800000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00093d00000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "regtest", + "targetAmount": 0, + "reserveAddress": "bcrt1q0r4elskfu8xlvvlvkergtzagv2ep8p9tfkvkm6", + "feeInfo": { + "feeRate": 20.0, + "longTermFeeRate": 20.0, + "knapsackMinChange": -1, + "dustFeeRate": 3.0 + } + }, + "expect": { + "hex": "02000000010af4768e14f820cb9063f55833b5999119e53390ecf4bf181842909b11d0974d0000000000ffffffff0380969800000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00093d00000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470ce47f19000000000016001478eb9fc2c9e1cdf633ecb646858ba862b21384ab00000000", + "usedAddresses": [ + "bcrt1q0r4elskfu8xlvvlvkergtzagv2ep8p9tfkvkm6" + ], + "feeAmount": 3860 + } + }, + { + "case": "error: liquid address", + "request": { + "utxoFile": "utxo_3", + "selectUtxos": [], + "tx": "02000000000280969800000000001600144352a1a6e86311f22274f7ebb2746de21b09b15d00093d00000000001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "regtest", + "targetAmount": 0, + "reserveAddress": "ert1q0r4elskfu8xlvvlvkergtzagv2ep8p9t3zeg96", + "feeInfo": { + "feeRate": 20.0, + "longTermFeeRate": 20.0, + "knapsackMinChange": -1, + "dustFeeRate": 3.0 + } + }, + "error": { + "cfd": "Base58 decode error." + } } ] } diff --git a/tests/data/elements_coin_test.json b/tests/data/elements_coin_test.json index 3101f4c..c010fab 100644 --- a/tests/data/elements_coin_test.json +++ b/tests/data/elements_coin_test.json @@ -634,6 +634,152 @@ ], "feeAmount": 883 } + }, + { + "case": "regtest address", + "request": { + "utxoFile": "elements_utxo_3", + "selectUtxos": [], + "tx": "020000000000020100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "liquidv1", + "targets": [ + { + "asset": "aa00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "ert1q0r4elskfu8xlvvlvkergtzagv2ep8p9t3zeg96" + }, + { + "asset": "bb00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "ert1q55a7gqgnhdg09w9j6zl7585z8e6kx26lum2leh" + } + ], + "feeInfo": { + "feeRate": 0.1, + "longTermFeeRate": 0.1, + "knapsackMinChange": -1, + "dustFeeRate": 3.0, + "feeAsset": "aa00000000000000000000000000000000000000000000000000000000000000", + "isBlindEstimateFee": true, + "exponent": 0, + "minimumBits": 52 + } + }, + "expect": { + "hex": "0200000000020bfa8774c5f753ce2f801a8106413b470af94edbff5b4242ed4c5a26d20e72b90000000000ffffffff040b0000000000000000000000000000000000000000000000000000000000000000000000ffffffff050100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c0100000000000000000000000000000000000000000000000000000000000000aa0100000000000001ff00000100000000000000000000000000000000000000000000000000000000000000bb010000000001124c1e00160014a53be40113bb50f2b8b2d0bfea1e823e75632b5f0100000000000000000000000000000000000000000000000000000000000000aa0100000000004b595f0016001478eb9fc2c9e1cdf633ecb646858ba862b21384ab00000000", + "usedAddresses": [ + "ert1q0r4elskfu8xlvvlvkergtzagv2ep8p9t3zeg96", + "ert1q55a7gqgnhdg09w9j6zl7585z8e6kx26lum2leh" + ], + "feeAmount": 511 + } + }, + { + "case": "confidential address", + "request": { + "utxoFile": "elements_utxo_3", + "selectUtxos": [], + "tx": "020000000000020100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "liquidv1", + "targets": [ + { + "asset": "aa00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "el1qqdqg9pua7svrx9u5ujj4hp7djnfdy0837g42quyp4g5tj8pgkhj6z78tnlpvncwd7ce7edjxsk96sc4jzwz2ky5z0zegze59c" + }, + { + "asset": "bb00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "ert1q55a7gqgnhdg09w9j6zl7585z8e6kx26lum2leh" + } + ], + "feeInfo": { + "feeRate": 0.1, + "longTermFeeRate": 0.1, + "knapsackMinChange": -1, + "dustFeeRate": 3.0, + "feeAsset": "aa00000000000000000000000000000000000000000000000000000000000000", + "isBlindEstimateFee": true, + "exponent": 0, + "minimumBits": 52 + } + }, + "expect": { + "hex": "0200000000020bfa8774c5f753ce2f801a8106413b470af94edbff5b4242ed4c5a26d20e72b90000000000ffffffff040b0000000000000000000000000000000000000000000000000000000000000000000000ffffffff050100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c0100000000000000000000000000000000000000000000000000000000000000aa0100000000000001ff00000100000000000000000000000000000000000000000000000000000000000000bb010000000001124c1e00160014a53be40113bb50f2b8b2d0bfea1e823e75632b5f0100000000000000000000000000000000000000000000000000000000000000aa0100000000004b595f034082879df418331794e4a55b87cd94d2d23cf1f22aa07081aa28b91c28b5e5a116001478eb9fc2c9e1cdf633ecb646858ba862b21384ab00000000", + "usedAddresses": [ + "el1qqdqg9pua7svrx9u5ujj4hp7djnfdy0837g42quyp4g5tj8pgkhj6z78tnlpvncwd7ce7edjxsk96sc4jzwz2ky5z0zegze59c", + "ert1q55a7gqgnhdg09w9j6zl7585z8e6kx26lum2leh" + ], + "feeAmount": 511 + } + }, + { + "case": "unmatch address network type", + "request": { + "utxoFile": "elements_utxo_3", + "selectUtxos": [], + "tx": "020000000000020100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "liquidv1", + "targets": [ + { + "asset": "aa00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "ex1q0r4elskfu8xlvvlvkergtzagv2ep8p9ttsns6q" + }, + { + "asset": "bb00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "ert1q0r4elskfu8xlvvlvkergtzagv2ep8p9t3zeg96" + } + ], + "feeInfo": { + "feeRate": 0.1, + "longTermFeeRate": 0.1, + "knapsackMinChange": -1, + "dustFeeRate": 3.0, + "feeAsset": "aa00000000000000000000000000000000000000000000000000000000000000", + "isBlindEstimateFee": true, + "exponent": 0, + "minimumBits": 52 + } + }, + "error": { + "cfd": "Failed to FundRawTransaction. Input address and network is unmatch." + } + }, + { + "case": "bitcoin address", + "request": { + "utxoFile": "elements_utxo_3", + "selectUtxos": [], + "tx": "020000000000020100000000000000000000000000000000000000000000000000000000000000aa010000000000989680001600144352a1a6e86311f22274f7ebb2746de21b09b15d0100000000000000000000000000000000000000000000000000000000000000bb01000000000007a120001600148beaaac4654cf4ebd8e46ca5062b0e7fb3e7470c00000000", + "network": "liquidv1", + "targets": [ + { + "asset": "aa00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "bc1q0r4elskfu8xlvvlvkergtzagv2ep8p9tpewghq" + }, + { + "asset": "bb00000000000000000000000000000000000000000000000000000000000000", + "amount": 0, + "reservedAddress": "bc1q55a7gqgnhdg09w9j6zl7585z8e6kx26lvqaltd" + } + ], + "feeInfo": { + "feeRate": 0.1, + "longTermFeeRate": 0.1, + "knapsackMinChange": -1, + "dustFeeRate": 3.0, + "feeAsset": "aa00000000000000000000000000000000000000000000000000000000000000", + "isBlindEstimateFee": true, + "exponent": 0, + "minimumBits": 52 + } + }, + "error": { + "cfd": "Base58 decode error." + } } ] }