예제 #1
0
 def key(self, key: str):
     if not isinstance(key, str):
         raise SDKException(ErrorCode.other_error('Invalid key type.'))
     self.__key = key
예제 #2
0
 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))
예제 #3
0
 def head(self, kid: str):
     if not isinstance(kid, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__head = Header(kid)
예제 #4
0
 def __check_event(event: dict):
     if not isinstance(event, dict):
         raise SDKException(ErrorCode.require_dict_params)
예제 #5
0
 def type(self, claim_type: ClmType):
     if not isinstance(claim_type, ClmType):
         raise SDKException(ErrorCode.invalid_claim_head_params)
     self.__type = claim_type
예제 #6
0
 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]
예제 #7
0
 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)
예제 #8
0
 def key(self, key: str):
     if not isinstance(key, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__key = key
예제 #9
0
 def b58_address(self, b58_address: str):
     if not isinstance(b58_address, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__address = b58_address
예제 #10
0
 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'))
예제 #11
0
 def kid(self, kid: str):
     if not isinstance(kid, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__kid = kid
예제 #12
0
 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
예제 #13
0
 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
예제 #14
0
 def label(self, label: str):
     if not isinstance(label, str):
         raise SDKException(ErrorCode.other_error('Invalid label.'))
     self.__label = label
예제 #15
0
 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)
예제 #16
0
 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
예제 #17
0
 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
예제 #18
0
 def salt(self, salt: str):
     if not isinstance(salt, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__salt = salt
예제 #19
0
 def set_identities(self, identities: list):
     if not isinstance(identities, list):
         raise SDKException(ErrorCode.param_error)
     self.identities = identities
예제 #20
0
 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.'))
예제 #21
0
 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)
예제 #22
0
 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)
예제 #23
0
 def alg(self, alg: ClmAlg):
     if not isinstance(alg, ClmAlg):
         raise SDKException(ErrorCode.invalid_claim_head_params)
     self.__alg = alg
예제 #24
0
 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.'))
예제 #25
0
 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'))
예제 #26
0
 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)
예제 #29
0
 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))
예제 #30
0
 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