Beispiel #1
0
 def __create_identity(self, ont_id: str):
     for item in self.identities:
         if item.ont_id == ont_id:
             return item
     identity = Identity(ont_id=ont_id)
     self.identities.append(identity)
     return identity
 def test_deep_copy(self):
     wallet_1 = WalletData()
     size = 10
     id_list = list()
     for i in range(size):
         rand_id = DID_ONT + str(randint(0, 1000000000))
         identity = Identity(ont_id=rand_id)
         wallet_1.add_identity(identity)
         id_list.append(rand_id)
         self.assertEqual(len(wallet_1.get_identities()), i + 1)
     wallet_2 = copy.deepcopy(wallet_1)
     self.assertNotEqual(id(wallet_1), id(wallet_2))
     self.assertEqual(wallet_1.name, wallet_2.name)
     wallet_2.name = 'newWallet'
     self.assertNotEqual(id(wallet_1.name), id(wallet_2.name))
     for i in range(size):
         self.assertEqual(wallet_1.identities[i].ont_id,
                          wallet_2.identities[i].ont_id)
         rand_id = DID_ONT + str(randint(0, 1000000000))
         wallet_1.identities[i].ont_id = rand_id
         try:
             wallet_1.identities[i].ont_id = str(randint(0, 1000000000))
         except SDKException as e:
             self.assertTrue(isinstance(e, SDKException))
         self.assertNotEqual(wallet_1.identities[i].ont_id,
                             wallet_2.identities[i].ont_id)
         self.assertNotEqual(id(wallet_1.identities[i]),
                             id(wallet_2.identities[i]))
 def test_add_identity(self):
     test_id = "test_ont_id"
     wallet = WalletData(default_id=test_id)
     size = 10
     for i in range(size):
         rand_id = DID_ONT + str(randint(0, 1000000000))
         identity = Identity(ont_id=rand_id)
         wallet.add_identity(identity)
         self.assertEqual(len(wallet.get_identities()), i + 1)
    def __create_account(self, label: str, pwd: str, salt: str, private_key: str, account_flag: bool) -> Account:
        account = Account(private_key, self.scheme)
        if self.scheme == SignatureScheme.SHA256withECDSA:
            acct_data = AccountData()
        else:
            raise SDKException(ErrorCode.other_error('Scheme type is error.'))
        if pwd is not None:
            acct_data.key = account.export_gcm_encrypted_private_key(pwd, salt)
        else:
            acct_data.key = account.get_private_key_hex()

        acct_data.b58_address = account.get_address_base58()
        if len(label) == 0 or label is None:
            label = uuid.uuid4().hex[0:8]
        if account_flag:
            for memory_acct in self.wallet_in_mem.accounts:
                if memory_acct.b58_address == account.get_address_base58():
                    raise SDKException(ErrorCode.other_error('Wallet account exists.'))
            if len(self.wallet_in_mem.accounts) == 0:
                acct_data.is_default = True
                self.wallet_in_mem.default_account_address = acct_data.b58_address
            acct_data.label = label
            acct_data.salt = base64.b64encode(salt.encode('latin-1')).decode('ascii')
            acct_data.public_key = account.get_public_key_hex()
            self.wallet_in_mem.accounts.append(acct_data)
        else:
            for identity in self.wallet_in_mem.identities:
                if identity.ont_id == DID_ONT + acct_data.b58_address:
                    raise SDKException(ErrorCode.other_error('Wallet identity exists.'))
            idt = Identity()
            idt.ont_id = DID_ONT + acct_data.b58_address
            idt.label = label
            if len(self.wallet_in_mem.identities) == 0:
                idt.is_default = True
                self.wallet_in_mem.default_ont_id = idt.ont_id
            ctl = Control(kid='keys-1', key=acct_data.key, salt=base64.b64encode(salt.encode()).decode('ascii'),
                          address=acct_data.b58_address, public_key=account.get_public_key_hex())
            idt.controls.append(ctl)
            self.wallet_in_mem.identities.append(idt)
        return account
    def test_remove_identity(self):
        test_id = "test_ont_id"
        wallet = WalletData(default_id=test_id)
        size = 10
        id_list = list()
        for i in range(size):
            try:
                rand_id = str(randint(0, 1000000000))
                Identity(ont_id=rand_id)
            except SDKException as e:
                self.assertTrue(isinstance(e, SDKException))
            rand_id = DID_ONT + str(randint(0, 1000000000))
            identity = Identity(ont_id=rand_id)
            wallet.add_identity(identity)
            id_list.append(rand_id)
            self.assertEqual(len(wallet.get_identities()), i + 1)

        for i in range(size):
            rand_id = choice(id_list)
            wallet.remove_identity(rand_id)
            id_list.remove(rand_id)
            self.assertEqual(len(wallet.get_identities()), size - i - 1)
 def test_get_identities(self):
     test_id = "test_ont_id"
     wallet = WalletData(default_id=test_id)
     size = 10
     id_list = list()
     for i in range(size):
         rand_id = DID_ONT + str(randint(0, 1000000000))
         identity = Identity(ont_id=rand_id)
         wallet.add_identity(identity)
         id_list.append(rand_id)
         self.assertEqual(len(wallet.get_identities()), i + 1)
     identities = wallet.get_identities()
     self.assertEqual(len(identities), size)
Beispiel #7
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['did'],
                                     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