示例#1
0
 def get_account(self, b58_address_or_ontid: str, password: str) -> Account or None:
     """
     :param b58_address_or_ontid: a base58 encode address or ontid
     :param password: a password which is used to decrypt the encrypted private key.
     :return:
     """
     if b58_address_or_ontid.startswith(DID_ONT):
         for index in range(len(self.wallet_in_mem.identities)):
             if self.wallet_in_mem.identities[index].ont_id == b58_address_or_ontid:
                 addr = self.wallet_in_mem.identities[index].ont_id.replace(did_ont, "")
                 key = self.wallet_in_mem.identities[index].controls[0].key
                 salt = base64.b64decode(self.wallet_in_mem.identities[index].controls[0].salt)
                 private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt,
                                                                   self.wallet_in_mem.scrypt.get_n(), self.scheme)
                 return Account(private_key, self.scheme)
     else:
         for index in range(len(self.wallet_in_mem.accounts)):
             if self.wallet_in_mem.accounts[index].address == b58_address_or_ontid:
                 key = self.wallet_in_mem.accounts[index].key
                 addr = self.wallet_in_mem.accounts[index].address
                 salt = base64.b64decode(self.wallet_in_mem.accounts[index].salt)
                 private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt,
                                                                   self.wallet_in_mem.scrypt.get_n(), self.scheme)
                 return Account(private_key, self.scheme)
     return None
    def get_account(self, b58_address: str, password: str) -> Account or None:
        """

        :param b58_address: a base58 encode address
        :param password: a password which is used to decrypt the encrypted private key.
        :return:
        """
        b58_address = b58_address.replace(DID_ONT, '')
        for index in range(len(self.wallet_in_mem.accounts)):
            if self.wallet_in_mem.accounts[index].address == b58_address:
                key = self.wallet_in_mem.accounts[index].key
                address = self.wallet_in_mem.accounts[index].address
                salt = base64.b64decode(self.wallet_in_mem.accounts[index].salt)
                scrypt_n = Scrypt().get_n()
                private_key = Account.get_gcm_decoded_private_key(key, password, address, salt, scrypt_n, self.scheme)
                return Account(private_key, self.scheme)

        for index in range(len(self.wallet_in_mem.identities)):
            if self.wallet_in_mem.identities[index].ont_id == did_ont + b58_address:
                address = self.wallet_in_mem.identities[index].ont_id.replace(did_ont, "")
                key = self.wallet_in_mem.identities[index].controls[0].key
                salt = base64.b64decode(self.wallet_in_mem.identities[index].controls[0].salt)
                scrypt_n = Scrypt().get_n()
                private_key = Account.get_gcm_decoded_private_key(key, password, address, salt, scrypt_n, self.scheme)
                return Account(private_key, self.scheme)
        return None
示例#3
0
    def get_account(self, address: str, pwd: str) -> Account or None:
        address = address.replace(DID_ONT, '')
        for index in range(len(self.wallet_in_mem.accounts)):
            if self.wallet_in_mem.accounts[index].address == address:
                key = self.wallet_in_mem.accounts[index].key
                addr = self.wallet_in_mem.accounts[index].address
                salt = base64.b64decode(
                    self.wallet_in_mem.accounts[index].salt)
                private_key = Account.get_gcm_decoded_private_key(
                    key, pwd, addr, salt,
                    Scrypt().get_n(), self.scheme)
                return Account(private_key, self.scheme)

        for index in range(len(self.wallet_in_mem.identities)):
            if self.wallet_in_mem.identities[
                    index].ont_id == did_ont + address:
                addr = self.wallet_in_mem.identities[index].ont_id.replace(
                    did_ont, "")
                key = self.wallet_in_mem.identities[index].controls[0].key
                salt = base64.b64decode(
                    self.wallet_in_mem.identities[index].controls[0].salt)
                private_key = Account.get_gcm_decoded_private_key(
                    key, pwd, addr, salt,
                    Scrypt().get_n(), self.scheme)
                return Account(private_key, self.scheme)
        return None
    def import_account(self, label: str, encrypted_pri_key: str, pwd: str,
                       base58_address: str,
                       base64_salt: str) -> AccountData or None:
        """
        This interface is used to import account by providing account data.

        :param label: str, wallet label
        :param encrypted_pri_key: str, an encrypted private key in base64 encoding from
        :param pwd: str, a password which is used to encrypt and decrypt the private key
        :param base58_address: str, a base58 encode  wallet address value
        :param base64_salt: str, a base64 encode salt value which is used in the encryption of private key
        :return:
            if succeed, return an data structure which contain the information of a wallet account.
            if failed, return a None object.
        """
        salt = base64.b64decode(base64_salt.encode('ascii')).decode('latin-1')
        private_key = Account.get_gcm_decoded_private_key(
            encrypted_pri_key, pwd, base58_address, salt,
            Scrypt().get_n(), self.scheme)
        info = self.create_account_info(label, pwd, salt, private_key)
        for index in range(len(self.wallet_in_mem.accounts)):
            if info.address_base58 == self.wallet_in_mem.accounts[
                    index].address:
                return self.wallet_in_mem.accounts[index]
        return None
示例#5
0
    def import_account(self,
                       label: str,
                       encrypted_pri_key: str,
                       pwd: str,
                       b58_address: str,
                       b64_salt: str,
                       n: int = 16384) -> AccountData:
        """
        This interface is used to import account by providing account data.

        :param label: str, wallet label
        :param encrypted_pri_key: str, an encrypted private key in base64 encoding from
        :param pwd: str, a password which is used to encrypt and decrypt the private key
        :param b58_address: str, a base58 encode  wallet address value
        :param b64_salt: str, a base64 encode salt value which is used in the encryption of private key
        :param n: int,  CPU/Memory cost parameter. It must be a power of 2 and less than :math:`2^{32}`
        :return:
            if succeed, return an data structure which contain the information of a wallet account.
            if failed, return a None object.
        """
        salt = base64.b64decode(b64_salt.encode('ascii')).decode('latin-1')
        private_key = Account.get_gcm_decoded_private_key(
            encrypted_pri_key, pwd, b58_address, salt, n, self.scheme)
        acct_info = self.create_account_info(label, pwd, salt, private_key)
        for acct in self.wallet_in_mem.accounts:
            if not isinstance(acct, AccountData):
                raise SDKException(
                    ErrorCode.other_error('Invalid account data in memory.'))
            if acct_info.address_base58 == acct.b58_address:
                return acct
        raise SDKException(ErrorCode.other_error('Import account failed.'))
示例#6
0
 def get_default_account(self, password: str) -> Account:
     acct = self.get_default_account_data()
     salt = base64.b64decode(acct.salt)
     n = self.wallet_in_mem.scrypt.n
     private_key = Account.get_gcm_decoded_private_key(
         acct.key, password, acct.b58_address, salt, n, self.scheme)
     return Account(private_key, self.scheme)
 def get_prikey_from_qrcode(self, qr_code: str, password: str):
     d = json.loads(qr_code)
     key = d["key"]
     address = d["address"]
     salt = d["salt"]
     n = json.loads(d["scrypt"])["n"]
     return Account.get_gcm_decoded_private_key(key, password, address, base64.b64decode(salt), int(n), SignatureScheme.SHA256withECDSA)
 def import_identity(self, label: str, encrypted_pri_key: str, pwd: str, salt: str, address: str):
     pri_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, address, salt,
                                                   Scrypt().get_n(), self.scheme)
     info = self.__create_identity(label, pwd, salt, pri_key)
     for index in range(len(self.wallet_in_mem.identities)):
         if self.wallet_in_mem.identities[index].ont_id == info.ont_id:
             return self.wallet_in_mem.identities[index]
     return None
示例#9
0
 def __get_control_account(self, ctrl_info: Control,
                           password: str) -> Account:
     salt = base64.b64decode(ctrl_info.salt)
     n = self.wallet_in_mem.scrypt.n
     private_key = Account.get_gcm_decoded_private_key(
         ctrl_info.key, password, ctrl_info.b58_address, salt, n,
         self.scheme)
     return Account(private_key, self.scheme)
 def import_account(self, label: str, encrypted_pri_key: str, pwd: str, base58_addr: str, base64_salt: str):
     salt = base64.b64decode(base64_salt.encode('ascii')).decode('latin-1')
     private_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, base58_addr, salt, Scrypt().get_n(),
                                                       self.scheme)
     info = self.create_account_info(label, pwd, salt, private_key)
     for index in range(len(self.wallet_in_mem.accounts)):
         if info.address_base58 == self.wallet_in_mem.accounts[index].address:
             return self.wallet_in_mem.accounts[index]
     return None
示例#11
0
 def test_export_gcm_encrypted_private_key(self):
     private_key = utils.get_random_bytes(32).hex()
     account = Account(private_key, SignatureScheme.SHA256withECDSA)
     b58_address = account.get_address_base58()
     salt = utils.get_random_hex_str(16)
     enc_private_key = account.export_gcm_encrypted_private_key(password, salt, 16384)
     decoded_private_key = account.get_gcm_decoded_private_key(enc_private_key, password, b58_address, salt, 16384,
                                                               SignatureScheme.SHA256withECDSA)
     self.assertEqual(private_key, decoded_private_key)
示例#12
0
 def get_control_account_by_b58_address(self, ont_id: str, b58_address: str, password: str) -> Account:
     WalletManager.__check_ont_id(ont_id)
     ctrl = self.get_control_info_by_b58_address(ont_id, b58_address)
     salt = base64.b64decode(ctrl.salt)
     key = ctrl.key
     b58_address = ctrl.b58_address
     n = self.wallet_in_mem.scrypt.n
     private_key = Account.get_gcm_decoded_private_key(key, password, b58_address, salt, n, self.scheme)
     return Account(private_key, self.scheme)
 def get_private_key_from_qr_code(qr_code: str, password: str):
     data = json.loads(qr_code)
     key = data['key']
     address = data['address']
     salt = data['salt']
     n = json.loads(data['scrypt'])['n']
     acct = Account.get_gcm_decoded_private_key(key, password, address, base64.b64decode(salt), int(n),
                                                SignatureScheme.SHA256withECDSA)
     return acct
示例#14
0
 def test_export_and_get_gcm_decoded_private_key(self):
     hex_private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
     salt = base64.b64decode('pwLIUKAf2bAbTseH/WYrfQ=='.encode('ascii')).decode('latin-1')
     account = Account(hex_private_key, SignatureScheme.SHA256withECDSA)
     b58_address = account.get_address_base58()
     n = 16384
     enc_private_key = account.export_gcm_encrypted_private_key(password, salt, n)
     decoded_private_key = Account.get_gcm_decoded_private_key(enc_private_key, password, b58_address, salt, n,
                                                               SignatureScheme.SHA256withECDSA)
     self.assertEqual(hex_private_key, decoded_private_key)
示例#15
0
 def get_control_account_by_index(self, ont_id: str, index: int, password: str) -> Account:
     if not isinstance(password, str):
         raise SDKException(ErrorCode.require_str_params)
     ctrl = self.get_control_info_by_index(ont_id, index)
     salt = base64.b64decode(ctrl.salt)
     key = ctrl.key
     b58_address = ctrl.b58_address
     n = self.wallet_in_mem.scrypt.n
     private_key = Account.get_gcm_decoded_private_key(key, password, b58_address, salt, n, self.scheme)
     return Account(private_key, self.scheme)
 def test_export_gcm_encrypted_private_key(self):
     private_key = "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf"
     account = Account(private_key, SignatureScheme.SHA256withECDSA)
     salt = util.get_random_str(16)
     enc_private_key = account.export_gcm_encrypted_private_key(
         "1", salt, 16384)
     import_private_key = account.get_gcm_decoded_private_key(
         enc_private_key, "1", account.get_address_base58(), salt, 16384,
         SignatureScheme.SHA256withECDSA)
     self.assertEqual(import_private_key, private_key)
示例#17
0
 def test_get_gcm_decoded_private_key(self):
     encrypted_key_str = 'hhWuyE1jjKjBOT7T5Rlrea3ewJIR8i6UjTv67bnkHz5YsqgeCfXjrHJTBGQtE0bG'
     b58_address = 'AMeJEzSMSNMZThqGoxBVVFwKsGXpAdVriS'
     salt = base64.b64decode('GXIs0bRy50tEfFuCF/h/yA==')
     n = 4096
     private_key = Account.get_gcm_decoded_private_key(encrypted_key_str, password, b58_address, salt, n,
                                                       SignatureScheme.SHA256withECDSA)
     acct = Account(private_key)
     export_key = acct.export_gcm_encrypted_private_key(password, salt, n)
     self.assertEqual(encrypted_key_str, export_key)
示例#18
0
 def get_account_by_b58_address(self, b58_address: str, password: str) -> Account:
     """
     :param b58_address: a base58 encode address.
     :param password: a password which is used to decrypt the encrypted private key.
     :return:
     """
     acct = self.get_account_data_by_b58_address(b58_address)
     n = self.wallet_in_mem.scrypt.n
     salt = base64.b64decode(acct.salt)
     private_key = Account.get_gcm_decoded_private_key(acct.key, password, b58_address, salt, n, self.scheme)
     return Account(private_key, self.scheme)
示例#19
0
 def import_account(self, label: str, encrypted_prikey: str, pwd: str,
                    base58_addr: str, salt: bytes):
     private_key = Account.get_gcm_decoded_private_key(
         encrypted_prikey, pwd, base58_addr, salt,
         Scrypt().get_n(), self.scheme)
     info = self.create_account_info(label, pwd, salt, private_key)
     private_key, pwd = None, None
     for index in range(len(self.wallet_in_mem.accounts)):
         if info.address_base58 == self.wallet_in_mem.accounts[
                 index].address:
             return self.wallet_in_mem.accounts[index]
     return None
示例#20
0
 def decrypt_private_key(key, address, salt, n, password):
     try:
         saltbytes = base64.b64decode(salt)
     except Exception:
         print('Error:')
         print('salt is error')
         return
     private_key = Account.get_gcm_decoded_private_key(
         key, password, address, saltbytes, n,
         SignatureScheme.SHA256withECDSA)
     print('Result is:')
     print('\t', private_key)
示例#21
0
 def get_account_by_ont_id(self, ont_id: str, password: str) -> Account:
     """
     :param ont_id: OntId.
     :param password: a password which is used to decrypt the encrypted private key.
     :return:
     """
     WalletManager.__check_ont_id(ont_id)
     for identity in self.wallet_in_mem.identities:
         if identity.ont_id == ont_id:
             addr = identity.ont_id.replace(DID_ONT, "")
             key = identity.controls[0].key
             salt = base64.b64decode(identity.controls[0].salt)
             n = self.wallet_in_mem.scrypt.n
             private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt, n, self.scheme)
             return Account(private_key, self.scheme)
     raise SDKException(ErrorCode.other_error(f'Get account {ont_id} failed.'))
示例#22
0
    def import_identity(self, label: str, encrypted_pri_key: str, pwd: str, salt: str, b58_address: str) -> Identity:
        """
        This interface is used to import identity by providing encrypted private key, password, salt and
        base58 encode address which should be correspond to the encrypted private key provided.

        :param label: a label for identity.
        :param encrypted_pri_key: an encrypted private key in base64 encoding from.
        :param pwd: a password which is used to encrypt and decrypt the private key.
        :param salt: a salt value which will be used in the process of encrypt private key.
        :param b58_address: a base58 encode address which correspond with the encrypted private key provided.
        :return: if succeed, an Identity object will be returned.
        """
        scrypt_n = Scrypt().n
        pri_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, b58_address, salt, scrypt_n, self.scheme)
        info = self.__create_identity(label, pwd, salt, pri_key)
        for identity in self.wallet_in_mem.identities:
            if identity.ont_id == info.ont_id:
                return identity
        raise SDKException(ErrorCode.other_error('Import identity failed.'))