def key(self, key: str): if not isinstance(key, str): raise SDKException(ErrorCode.other_error('Invalid key type.')) self.__key = key
def from_base64(b64_head: str): if not isinstance(b64_head, str): raise SDKException(ErrorCode.require_str_params) return Header.from_bytes(base64.b64decode(b64_head))
def head(self, kid: str): if not isinstance(kid, str): raise SDKException(ErrorCode.require_str_params) self.__head = Header(kid)
def __check_event(event: dict): if not isinstance(event, dict): raise SDKException(ErrorCode.require_dict_params)
def type(self, claim_type: ClmType): if not isinstance(claim_type, ClmType): raise SDKException(ErrorCode.invalid_claim_head_params) self.__type = claim_type
def get_account_by_index(self, index: int): if index < 0 or index >= len(self.accounts): raise SDKException(ErrorCode.get_account_by_index_err) return self.accounts[index]
def add_identity(self, id: Identity): for identity in self.identities: if identity.ont_id == id.ont_id: raise SDKException(ErrorCode.other_error('add identity failed, OntId conflict.')) self.identities.append(id)
def key(self, key: str): if not isinstance(key, str): raise SDKException(ErrorCode.require_str_params) self.__key = key
def b58_address(self, b58_address: str): if not isinstance(b58_address, str): raise SDKException(ErrorCode.require_str_params) self.__address = b58_address
def wallet_manager(self, wallet_manager: WalletManager): if isinstance(self.wallet_manager, WalletManager): self.__wallet_manager = wallet_manager else: raise SDKException( ErrorCode.other_error('Invalid WalletManager instance'))
def kid(self, kid: str): if not isinstance(kid, str): raise SDKException(ErrorCode.require_str_params) self.__kid = kid
def ont_id(self, ont_id: str): if not isinstance(ont_id, str): raise SDKException(ErrorCode.require_str_params) if len(ont_id) != 0 and not ont_id.startswith(DID_ONT): raise SDKException(ErrorCode.invalid_ont_id_format(ont_id)) self.__ont_id = ont_id
def hex_contract_address(self, hex_contract_address: str): if not isinstance(hex_contract_address, str) and len(hex_contract_address) == 40: raise SDKException(ErrorCode.require_str_params) self.__hex_contract_address = hex_contract_address
def label(self, label: str): if not isinstance(label, str): raise SDKException(ErrorCode.other_error('Invalid label.')) self.__label = label
def set_params_value(self, *objs): if len(self.parameters) != len(objs): raise SDKException(ErrorCode.other_error('Param error.')) for i, v in enumerate(objs): self.parameters[i].set_value(v)
def public_key(self, b64_pub_key: str): if not isinstance(b64_pub_key, str): raise SDKException(ErrorCode.other_error('Invalid public key.')) self.__public_key = b64_pub_key
def __init__(self, script_hash: bytes): if not isinstance(script_hash, bytes): raise SDKException(ErrorCode.other_error('Invalid script hash.')) if len(script_hash) != 20: raise SDKException(ErrorCode.other_error('Invalid script hash.')) self.ZERO = script_hash
def salt(self, salt: str): if not isinstance(salt, str): raise SDKException(ErrorCode.require_str_params) self.__salt = salt
def set_identities(self, identities: list): if not isinstance(identities, list): raise SDKException(ErrorCode.param_error) self.identities = identities
def get_account_by_b58_address(self, b58_address: str) -> AccountData: for acct in self.accounts: if acct.b58_address == b58_address: return acct raise SDKException(ErrorCode.other_error('Get account failed.'))
def remove_identity(self, ont_id): for index in range(len(self.identities)): if self.identities[index].ont_id == ont_id: del self.identities[index] return raise SDKException(ErrorCode.param_error)
def remove_identity(self, ont_id): for identity in self.identities: if identity.ont_id == ont_id: self.identities.remove(identity) return raise SDKException(ErrorCode.param_error)
def alg(self, alg: ClmAlg): if not isinstance(alg, ClmAlg): raise SDKException(ErrorCode.invalid_claim_head_params) self.__alg = alg
def get_identity_by_ont_id(self, ont_id: str) -> Identity: for identity in self.identities: if identity.ont_id == ont_id: return identity raise SDKException(ErrorCode.other_error('Get identity failed.'))
def from_bytes(bytes_head: bytes): if not isinstance(bytes_head, bytes): raise SDKException(ErrorCode.require_bytes_params) return Header.from_json(bytes_head.decode('utf-8'))
def __init__(self, name: str = 'MyWallet', version: str = '1.1', create_time: str = '', default_id: str = '', default_address='', scrypt: Scrypt = Scrypt(), identities: List[Identity] = None, accounts: List[AccountData] = None): if not isinstance(scrypt, Scrypt): raise SDKException( ErrorCode.other_error('Wallet Data init failed')) if identities is None: identities = list() if accounts is None: accounts = list() self.name = name self.version = version self.create_time = create_time self.default_ont_id = default_id self.default_account_address = default_address self.scrypt = scrypt self.identities = list() self.accounts = list() for dict_identity in identities: if isinstance(dict_identity, dict): list_controls = list() is_default = dict_identity.get('isDefault', False) for ctrl_data in dict_identity['controls']: hash_value = ctrl_data.get('hash', 'sha256') public_key = ctrl_data.get('publicKey', '') try: ctrl = Control(kid=ctrl_data['id'], address=ctrl_data['address'], enc_alg=ctrl_data['enc-alg'], key=ctrl_data['key'], algorithm=ctrl_data['algorithm'], salt=ctrl_data['salt'], param=ctrl_data['parameters'], hash_value=hash_value, public_key=public_key) except KeyError: raise SDKException( ErrorCode.other_error('invalid parameters.')) list_controls.append(ctrl) try: identity = Identity(ont_id=dict_identity['ontid'], label=dict_identity['label'], lock=dict_identity['lock'], controls=list_controls, is_default=is_default) except KeyError: raise SDKException( ErrorCode.other_error('invalid parameters.')) self.identities.append(identity) else: self.identities = identities break for dict_account in accounts: if isinstance(dict_account, dict): try: public_key = dict_account['publicKey'] except KeyError: public_key = '' try: acct = AccountData( b58_address=dict_account['address'], enc_alg=dict_account['enc-alg'], key=dict_account['key'], algorithm=dict_account['algorithm'], salt=dict_account['salt'], param=dict_account['parameters'], label=dict_account['label'], public_key=public_key, sig_scheme=dict_account['signatureScheme'], is_default=dict_account['isDefault'], lock=dict_account['lock']) except KeyError: raise SDKException(ErrorCode.param_error) self.accounts.append(acct) else: self.accounts = accounts break
def get_default_identity(self) -> Identity: for identity in self.wallet_in_mem.identities: if identity.is_default: return identity raise SDKException(ErrorCode.param_error)
def get_default_account(self) -> AccountData: for acct in self.wallet_in_mem.accounts: if acct.isDefault: return acct raise SDKException(ErrorCode.get_default_account_err)
def hex_contract_address(self, hex_contract_address): if isinstance(hex_contract_address, str) and len(hex_contract_address) == 40: self.__hex_contract_address = hex_contract_address else: raise SDKException(ErrorCode.invalid_contract_address(hex_contract_address))
def is_default(self, is_default: bool): if not isinstance(is_default, bool): raise SDKException(ErrorCode.other_error('Invalid default account state.')) self.__is_default = is_default