def from_json(data: AssetTransferOutputJSON): return AssetTransferOutput( H160(data["lockScriptHash"]), list(map(lambda x: bytes.fromhex(x), data["parameters"])), H160(data["assetType"]), data["shardId"], U64(data["quantity"]), )
def from_json(data): return Asset( H256(data["tracker"]), data["transactionOutputIndex"], H160(data["assetType"]), data["shardId"], U64(data["quantity"]), H160(data["lockScriptHash"]), list(map(lambda x: bytes.fromhex(x), data["parameters"])), None if data["orderHash"] is None else H256(data["orderHash"]), )
def from_json(data: AssetOutPointJSON): return AssetOutPoint( H256(data.tracker), data.index, H160(data.asset_type), data.shard_id, U64(data.quantity), None if data.lock_script_hash is None else H160(data.lock_script_hash), None if data.parameters is None else list(map(lambda x: bytes.fromhex(x), data.parameters)), )
def test_from_type_and_payload_mutisig(): sig = MultiSig( 2, 1, [ H160("1111111111111111111111111111111111111111"), H160("2222222222222222222222222222222222222222"), ], ) address1 = AssetAddress.from_string( "tcaqypsyqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyfzyg3zyg3zyg3zyg3zyg3zyg3zyg3zygsn28hf0" ) address2 = AssetAddress.from_type_and_payload(3, sig, network_id="tc") assert address1.value == address2.value
def test_from_string_mutisig(): address = AssetAddress.from_string( "tcaqypsyqg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyfzyg3zyg3zyg3zyg3zyg3zyg3zyg3zygsn28hf0" ) payload = address.payload network_type = address.address_type assert network_type == 3 n, m, pubkeys = payload assert n == 2 assert m == 1 assert pubkeys[0] == H160("1111111111111111111111111111111111111111") assert pubkeys[1] == H160("2222222222222222222222222222222222222222")
def from_json(data: OrderJSON): return Order( H160(data["assetTypeFrom"]), H160(data["assetTypeTo"]), H160(data["assetTypeFee"]), data["shardIdFrom"], data["shardIdTo"], data["shardIdFee"], U64(data["assetQuantityFrom"]), U64(data["assetQuantityTo"]), U64(data["assetQuantityFee"]), list( map(lambda x: AssetOutPoint.from_json(x), data["originOutputs"])), U64(data["expiration"]), H160(data["lockScriptHashFrom"]), list(map(lambda x: bytes.fromhex(x), data["parametersFrom"])), H160(data["lockSCriptHashFee"]), list(map(lambda x: bytes.fromhex(x), data["parametersFee"])), )
def from_json(data: AssetSchemeJSON): return AssetScheme( data["metadata"], U64(data["supply"]), None if data["approver"] is None else PlatformAddress.ensure( data["approver"]), None if data["registrar"] is None else PlatformAddress.ensure( data["registrar"]), [] if data["allowedScriptHashes"] is None else list( map(lambda x: H160(x), data["allowedScriptHashes"])), list( map( lambda x: { "assetType": H160(x["assetType"]), "quantity": U64(x["quantity"]), }, data["pool"], )), None, None, data["seq"], )
def create_asset_scheme( self, shard_id: int, metadata, supply: U64, approver: PlatformAddress = None, registrar: PlatformAddress = None, allowed_script_hashes: List[H160] = None, pool: List[object] = None, ): if pool is None: pool = [] check_metadata(metadata) metadata = metadata if isinstance(metadata, str) else str(metadata) check_shard_id(shard_id) check_amount(supply) check_approver(approver) check_registrar(registrar) from .assetscheme import AssetScheme return AssetScheme( metadata, U64(supply), None if approver is None else PlatformAddress.ensure(approver), None if registrar is None else PlatformAddress.ensure(registrar), [] if allowed_script_hashes is None else allowed_script_hashes, list( map( lambda x: { "assetType": H160(x["assetType"]), "quantity": U64(x["quantity"]), }, pool, )), self.network_id, shard_id, )
import pytest from codechain.primitives import H160 from codechain.primitives import PlatformAddress account_id = H160("7b5e0ee8644c6f585fc297364143280a45844502") account_id_string = "7b5e0ee8644c6f585fc297364143280a45844502" mainnet_address = "cccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgs7q0a7" testnet_address = "tccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgcejvw8" class TestFromAccountId: def test_mainnet(self): address = PlatformAddress.from_account_id(account_id, network_id="cc") assert address.value == "cccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgs7q0a7" def test_testnet(self): address = PlatformAddress.from_account_id(account_id, network_id="tc") assert address.value == "tccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgcejvw8" def test_valid_version(self): try: PlatformAddress.from_account_id(account_id, network_id="tc", version=1) except Exception as e: raise pytest.fail(f"Unexpected exception: {e}") def test_invalid_version(self):
def from_json(data: AssetMintOutputJSON): return AssetMintOutput( U64(data["supply"]), H160(data["lockScriptHash"]), list(map(lambda x: bytes.fromhex(x), data["parameters"])), )
def get_lock_script_hash() -> H160: return H160("5f5960a7bca6ceeeb0c97bc717562914e7a1de04")
def from_json_to_transaction(result: SignedTransactionJSON): seq = result.seq fee = result.fee network_id = result.network_id action = result.action action_type = result.action_type tx: Transaction if action_type == "mintAsset": raise ValueError("Not implemented") elif action_type == "changeAssetScheme": metadata = action.metadata approvals = action.approvals shard_id = action.shard_id asset_scheme_seq = action.seq asset_type = H160(action.asset_type) approver = (None if action.approver is None else PlatformAddress.ensure(action.approver)) registrar = (None if action.registrar is None else PlatformAddress.ensure(action.registrar)) allowed_script_hashes = list( map(lambda x: H160(x), action.allowed_script_hashes)) tx = ChangeAssetScheme( network_id, asset_type, shard_id, asset_scheme_seq, metadata, approver, registrar, allowed_script_hashes, approvals, ) elif action_type == "increaseAssetSupply": raise ValueError("Not implemented") elif action_type == "transferAsset": raise ValueError("Not implemented") elif action_type == "unwrapCCC": raise ValueError("Not implemented") elif action_type == "pay": raise ValueError("Not implemented") elif action_type == "setRegularKey": raise ValueError("Not implemented") elif action_type == "createShard": raise ValueError("Not implemented") elif action_type == "setShardOwners": raise ValueError("Not implemented") elif action_type == "setShardUsers": raise ValueError("Not implemented") elif action_type == "wrapCCC": raise ValueError("Not implemented") elif action_type == "store": raise ValueError("Not implemented") elif action_type == "remove": raise ValueError("Not implemented") elif action_type == "custom": raise ValueError("Not implemented") else: raise ValueError(f"Unexpected action: {action}") if seq is not None: tx.seq = seq if fee is not None: tx.fee = fee return tx
def get_lock_script_hash() -> H160: return H160("37572bdcc22d39a59c0d12d301f6271ba3fdd451")
def get_asset_type(self): return H160(blake160(str(self.tracker())))
def __init__( self, asset_type_from: H160, asset_type_to: H160, asset_type_fee: H160, shard_id_from: int, shard_id_to: int, shard_id_fee: int, asset_quantity_from: U64, asset_quantity_to: U64, asset_quantity_fee: U64, origin_outputs: List[AssetOutPoint], expiration: U64, lock_script_hash_from: H160 = None, parameters_from: List[bytes] = None, recipient_from: AssetAddress = None, lock_script_hash_fee: H160 = None, parameters_fee: List[bytes] = None, recipient_fee: AssetAddress = None, ): if recipient_from is not None: self.lock_script_hash_from, self.parameters_from = decompose_recipient( recipient_from) else: if lock_script_hash_from is None or parameters_from is None: raise ValueError("recipient_from should not be None") self.lock_script_hash_from = lock_script_hash_from self.parameters_from = parameters_from if recipient_fee is not None: self.lock_script_hash_fee, self.parameters_fee = decompose_recipient( recipient_fee) else: if lock_script_hash_fee is None or parameters_fee is None: raise ValueError("recipient_fee should not be None") self.lock_script_hash_fee = lock_script_hash_fee self.parameters_fee = parameters_fee self.asset_type_from = asset_type_from self.asset_type_to = asset_type_to self.asset_type_fee = (H160(H160.ZERO) if asset_type_fee is None else asset_type_fee) self.shard_id_from = shard_id_from self.shard_id_to = shard_id_to self.shard_id_fee = 0 if shard_id_fee is None else shard_id_fee self.asset_quantity_from = asset_quantity_from self.asset_quantity_to = asset_quantity_to self.asset_quantity_fee = (U64(0) if asset_quantity_fee is None else asset_quantity_fee) self.origin_outputs = origin_outputs self.expiration = expiration asset_quantity_from_is_zero = asset_quantity_from == 0 asset_quantity_to_is_zero = asset_quantity_to == 0 asset_quantity_fee_is_zero = asset_quantity_fee == 0 if asset_type_from == asset_type_to and shard_id_from == shard_id_to: raise ValueError( f"assetTypeFrom and assetTypeTo is same: {asset_type_from}(shard {shard_id_from})" ) elif not asset_quantity_fee_is_zero: if asset_type_from == asset_type_fee and shard_id_from == shard_id_fee: raise ValueError( f"assetTypeFrom and assetTypeFee is same: {asset_type_from}(shard {shard_id_from})" ) if asset_type_to == asset_type_fee and shard_id_to == shard_id_fee: raise ValueError( f"assetTypeTo and assetTypeFee is same: {asset_type_to}(shard {shard_id_to})" ) if ((asset_quantity_from_is_zero and not asset_quantity_to_is_zero) or (not asset_quantity_from_is_zero and asset_quantity_to_is_zero) or (asset_quantity_from_is_zero and asset_quantity_fee_is_zero) or (not asset_quantity_from_is_zero and not (asset_quantity_fee % asset_quantity_from) == 0)): raise ValueError( f"The given quantity ratio is invalid: {asset_quantity_from}:{asset_quantity_to}:{asset_quantity_fee}" ) if len(origin_outputs) == 0: raise ValueError(f"originOutputs is empty")
def get_signer_account_id(self): public_key = recover_ecdsa(self.unsigned, self.signature) return H160(blake160(public_key))