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 get_seq(self, address: PlatformAddress, block_number: int = None): if not PlatformAddress.check(address): raise ValueError( f"Expected the first argument of getSeq to be a PlatformAddress value but found {address}" ) if block_number is not None and block_number >= 0: raise ValueError( f"Expected the second argument of getSeq to be a number but found ${block_number}" ) return self.rpc.send_rpc_request("chain", "get_seq", str(PlatformAddress.ensure(address)), block_number)
def create_mint_asset_transaction( self, recipient: AssetAddress, scheme: AssetScheme = None, approvals: List[str] = None, network_id: str = None, shard_id: int = None, metadata=None, approver: PlatformAddress = None, registrar: PlatformAddress = None, allowed_script_hashes: List[H160] = None, supply: U64 = None, ): if approvals is None: approvals = [] if scheme is None and (shard_id is None or metadata is None): raise ValueError( f"Either scheme params or proper arguments should be provided {scheme}" ) network_id = self.network_id if network_id is None else network_id shard_id = shard_id supply = U64(U64.MAX_VALUE) if supply is None else supply check_metadata(metadata) metadata = metadata if isinstance(metadata, str) else str(metadata) check_asset_address_recipient(recipient) check_network_id(network_id) if shard_id is None: raise ValueError("shard_id is None") check_shard_id(shard_id) check_approver(approver) check_registrar(registrar) check_amount(supply) return MintAsset( network_id, shard_id, metadata, AssetMintOutput(U64(supply), None, None, AssetAddress.ensure(recipient)), 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, approvals, )
def approve_transaction( self, account: Union[str, PlatformAddress], transaction: AssetTransaction, keystore=None, passphrase="", ): if not is_keystore(keystore): raise ValueError( f"Expected keyStore param to be a KeyStore instance but found {keystore}" ) if not PlatformAddress.check(account): raise ValueError( f"Expected account param to be a PlatformAddress value but found {account}" ) account_id = PlatformAddress.ensure(account).account_id return keystore.platform.sign(account_id.to_string(), transaction.tracker(), passphrase)
def create_platform_address(self, keystore=None, passphrase=""): keystore = self.ensure_keystore() if not is_keystore(keystore): raise ValueError( f"Expected keyStore param to be a KeyStore instance but found {keystore}" ) account_id = keystore.platform.create_key(passphrase=passphrase) network_id = self.network_id return PlatformAddress.from_account_id(account_id, network_id=network_id)
def __init__( self, network_id: str, shard_id: int, asset_type: H160, seq: int, metadata, approver: Union[PlatformAddress, None], registrar: Union[PlatformAddress, None], allowed_script_hashes: List[H160], ): self.network_id = network_id self.shard_id = shard_id self.asset_type = asset_type self.seq = seq self.metatdata = metadata if isinstance(metadata, str) else str(metadata) self.approver = None if approver is None else PlatformAddress.ensure( approver) self.registrar = (None if registrar is None else PlatformAddress.ensure(registrar)) self.allowed_script_hashes = allowed_script_hashes
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, )
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 send_transaction( self, tx: Transaction, account: PlatformAddress = None, passphrase: str = None, seq: Union[int, None] = None, fee: U64 = None, block_number: int = None, ): if not isinstance(tx, Transaction): raise ValueError( f"Expected the first argument of sendTransaction to be a Transaction but found {tx}" ) account = self.transaction_signer if account is None else account fee = (self.get_mint_transaction_fee( tx.transaction_type(), block_number) if fee is None else fee) if account is None: raise ValueError("The account to sign the tx is not specified") elif not PlatformAddress.check(account): raise ValueError( f"Expected account param of sendTransaction to be a PlatformAddress value but found {account}" ) seq = self.get_seq(account) if seq is None else seq tx.seq = seq if fee is None: raise ValueError("The fee of the tx is not specified") tx.fee = fee address = PlatformAddress.ensure(account) sig = self.rpc.account.sign(tx.unsigned_hash(), address, passphrase) return self.send_signed_transaction(SignedTransaction(tx, sig))
def sign(self, message_digest: H256, address: PlatformAddress, passphrase: str = None): if not H256.check(message_digest): raise ValueError( f"Expected the first argument to be an H256 value but found {message_digest}" ) if not PlatformAddress.check(address): raise ValueError( f"Expected the second argument to be a PlatformAddress value but found {address}" ) if passphrase is not None and not isinstance(passphrase, str): raise ValueError( f"Expected the third argument to be a string but found {passphrase}" ) return self.rpc.send_rpc_request( "account", "sign", "0x" + str(H256(message_digest)), str(PlatformAddress.ensure(address)), passphrase, )
def import_raw(self, secret: H256, passphrase: str = None): if not H256.check(secret): raise ValueError( f"Expected the first argument to be an H256 value but found {secret}" ) if passphrase is not None and not isinstance(passphrase, str): raise ValueError( f"Expected the second argument to be a string but found {passphrase}" ) result = self.rpc.send_rpc_request("account", "import_raw", H256(secret).to_string(prefix=True), passphrase) return str(PlatformAddress.ensure(result))
def sign_transaction( self, tx: Transaction, account: Union[PlatformAddress, str], fee: Union[U64, str, int], seq: int, keystore=None, passphrase="", ): if not isinstance(tx, Transaction): raise ValueError( f"Expected the first argument of signTransaction to be a Transaction instance but found {tx}" ) keystore = self.ensure_keystore() if not is_keystore(keystore): raise ValueError( f"Expected keyStore param to be a KeyStore instance but found {keystore}" ) if not PlatformAddress.check(account): raise ValueError( f"Expected account param to be a PlatformAddress value but found {account}" ) if not U64.check(fee): raise ValueError( f"Expected fee param to be a U64 value but found {fee}") if not isinstance(seq, int): raise ValueError( f"Expected seq param to be a number value but found {seq}") tx.fee = fee tx.seq = seq account_id = PlatformAddress.ensure(account).account_id sig = keystore.platform.sign(account_id.to_string(), tx.unsigned_hash(), passphrase) return SignedTransaction(tx, sig)
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 test_invalid_checksum(self): invalid_checksum_address = "cccqpa4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgqqqqqq" with pytest.raises(ValueError) as e: PlatformAddress.from_string(invalid_checksum_address) assert "checksum" in str(e.value)
def test_testnet(self): address = PlatformAddress.from_string(testnet_address) assert address.account_id == account_id
def test_invalid_account_id(self): with pytest.raises(ValueError) as e: PlatformAddress.from_account_id("xxx", network_id="tc") assert "account_id" in str(e.value)
def test_invalid_network_id(self): with pytest.raises(ValueError) as e: PlatformAddress.from_account_id(account_id, network_id="x", version=1) assert "network_id" in str(e.value)
def test_testnet(self): address = PlatformAddress.from_account_id(account_id, network_id="tc") assert address.value == "tccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgcejvw8"
def check_registrar(registrar: Union[None, PlatformAddress]): if registrar is not None and not PlatformAddress.check(registrar): raise ValueError( f"Expected registrar param to be either null or a PlatformAddress value but found ${registrar}" )
def check_approver(approver: Union[None, PlatformAddress]): if approver is not None and not PlatformAddress.check(approver): raise ValueError( f"Expected approver param to be either null or a PlatformAddress value but found ${approver}" )
def test_mainnet(self): address = PlatformAddress.from_account_id(account_id, network_id="cc") assert address.value == "cccq9a4urhgv3xx7kzlc2tnvs2r9q9ytpz9qgs7q0a7"
def from_json(data: TextJSON): return Text(data.content, PlatformAddress.ensure(data.certifier))
def get_signer_address(self, network_id: str): return PlatformAddress.from_account_id(self.get_signer_account_id(), network_id=network_id)