def test_new_add_remove_public_key_transaction(self):
        ont_id = sdk.native_vm().ont_id()
        private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
        acct = Account(private_key, SignatureScheme.SHA256withECDSA)
        hex_public_key = acct.get_public_key_hex()
        rand_private_key = util.get_random_bytes(32).hex()
        rand_acct = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        hex_new_public_key = rand_acct.get_public_key_hex()
        b58_address = acct.get_address_base58()
        acct_did = "did:ont:" + b58_address
        gas_limit = 20000
        gas_price = 500
        tx = ont_id.new_add_public_key_transaction(acct_did, hex_public_key,
                                                   hex_new_public_key,
                                                   b58_address, gas_limit,
                                                   gas_price)
        tx = sdk.sign_transaction(tx, acct)
        tx_hash = sdk.rpc.send_raw_transaction(tx)
        time.sleep(6)
        notify = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)['Notify']
        self.assertEqual('PublicKey', notify[0]['States'][0])
        self.assertEqual('add', notify[0]['States'][1])
        self.assertEqual(acct_did, notify[0]['States'][2])
        self.assertEqual(hex_new_public_key, notify[0]['States'][4])
        try:
            sdk.rpc.send_raw_transaction(tx)
        except SDKException as e:
            msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] service execute error!:' \
                  ' [Invoke] Native serivce function execute error!: add key failed: already exists'
            self.assertEqual(59000, e.args[0])
            self.assertEqual(msg, e.args[1])

        tx = ont_id.new_remove_public_key_transaction(acct_did, hex_public_key,
                                                      hex_new_public_key,
                                                      b58_address, gas_limit,
                                                      gas_price)
        tx = sdk.sign_transaction(tx, acct)
        tx_hash = sdk.rpc.send_raw_transaction(tx)
        time.sleep(6)
        notify = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)['Notify']
        self.assertEqual('PublicKey', notify[0]['States'][0])
        self.assertEqual('remove', notify[0]['States'][1])
        self.assertEqual(acct_did, notify[0]['States'][2])
        self.assertEqual(hex_new_public_key, notify[0]['States'][4])
        try:
            sdk.rpc.send_raw_transaction(tx)
        except SDKException as e:
            msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] service execute ' \
                  'error!: [Invoke] Native serivce function execute error!: remove key failed: ' \
                  'public key has already been revoked'
            self.assertEqual(59000, e.args[0])
            self.assertEqual(msg, e.args[1])
 def test_new_add_recovery_transaction(self):
     ont_id = sdk.native_vm().ont_id()
     gas_limit = 20000
     gas_price = 500
     private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
     acct = Account(private_key, SignatureScheme.SHA256withECDSA)
     b58_address = acct.get_address_base58()
     acct_did = "did:ont:" + b58_address
     hex_public_key = acct.get_public_key_hex()
     rand_private_key = util.get_random_bytes(32).hex()
     recovery = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
     b58_recovery_address = recovery.get_address_base58()
     tx = ont_id.new_add_recovery_transaction(acct_did, hex_public_key,
                                              b58_recovery_address,
                                              b58_address, gas_limit,
                                              gas_price)
     tx = sdk.sign_transaction(tx, acct)
     try:
         sdk.rpc.send_raw_transaction(tx)
     except SDKException as e:
         msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] service execute ' \
               'error!: [Invoke] Native serivce function execute error!: add recovery failed: already ' \
               'set recovery'
         self.assertEqual(59000, e.args[0])
         self.assertEqual(msg, e.args[1])
 def test_new_remove_attribute_transaction(self):
     ont_id = sdk.native_vm().ont_id()
     private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
     acct = Account(private_key, SignatureScheme.SHA256withECDSA)
     hex_public_key = acct.get_public_key_hex()
     b58_address = acct.get_address_base58()
     acct_did = "did:ont:" + b58_address
     gas_limit = 20000
     gas_price = 500
     path = 'try'
     tx = ont_id.new_remove_attribute_transaction(acct_did, hex_public_key,
                                                  path, b58_address,
                                                  gas_limit, gas_price)
     tx = sdk.sign_transaction(tx, acct)
     try:
         tx_hash = sdk.rpc.send_raw_transaction(tx)
         self.assertEqual(tx.hash256_explorer(), tx_hash)
         time.sleep(6)
         notify = sdk.rpc.get_smart_contract_event_by_tx_hash(
             tx_hash)['Notify']
         self.assertEqual('Attribute', notify[0]['States'][0])
         self.assertEqual('remove', notify[0]['States'][1])
         self.assertEqual(acct_did, notify[0]['States'][2])
         self.assertEqual('try',
                          bytes.fromhex(notify[0]['States'][3]).decode())
     except SDKException as e:
         msg = 'Other Error, [NeoVmService] service system call error!: ' \
               '[SystemCall] service execute error!: [Invoke] Native serivce function execute error!: ' \
               'remove attribute failed: attribute not exist'
         self.assertEqual(59000, e.args[0])
         self.assertEqual(msg, e.args[1])
Exemple #4
0
    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
Exemple #5
0
 def add_control(self, ont_id: str, password: str):
     WalletManager.__check_ont_id(ont_id)
     private_key = get_random_hex_str(64)
     salt = get_random_hex_str(16)
     b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii')
     account = Account(private_key, self.scheme)
     key = account.export_gcm_encrypted_private_key(password, salt)
     b58_address = account.get_address_base58()
     public_key = account.get_public_key_hex()
     ctrl = Control(kid='', key=key, salt=b64_salt, address=b58_address, public_key=public_key)
     identity = self.get_identity_by_ont_id(ont_id)
     identity.add_control(ctrl)
    def test_send_add_remove_public_key_transaction(self):
        ont_id = sdk.native_vm().ont_id()
        label = 'label'
        password = '******'
        private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
        acct = Account(private_key, SignatureScheme.SHA256withECDSA)
        identity = sdk.wallet_manager.create_identity_from_private_key(
            label, password, private_key)
        rand_private_key = util.get_random_bytes(32).hex()
        rand_acct = Account(rand_private_key, SignatureScheme.SHA256withECDSA)
        hex_new_public_key = rand_acct.get_public_key_hex()
        password = '******'
        gas_limit = 20000
        gas_price = 500
        tx_hash = ont_id.send_add_public_key_transaction(
            identity, password, hex_new_public_key, acct, gas_limit, gas_price)
        time.sleep(6)
        notify = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)['Notify']
        self.assertEqual('PublicKey', notify[0]['States'][0])
        self.assertEqual('add', notify[0]['States'][1])
        self.assertEqual(identity.ont_id, notify[0]['States'][2])
        self.assertEqual(hex_new_public_key, notify[0]['States'][4])
        try:
            ont_id.send_add_public_key_transaction(identity, password,
                                                   hex_new_public_key, acct,
                                                   gas_limit, gas_price)
        except SDKException as e:
            msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] service execute' \
                  ' error!: [Invoke] Native serivce function execute error!: add key failed: already exists'
            self.assertEqual(59000, e.args[0])
            self.assertEqual(msg, e.args[1])

        tx_hash = ont_id.send_remove_public_key_transaction(
            identity, password, hex_new_public_key, acct, gas_limit, gas_price)
        time.sleep(6)
        notify = sdk.rpc.get_smart_contract_event_by_tx_hash(tx_hash)['Notify']
        self.assertEqual('PublicKey', notify[0]['States'][0])
        self.assertEqual('remove', notify[0]['States'][1])
        self.assertEqual(identity.ont_id, notify[0]['States'][2])
        self.assertEqual(hex_new_public_key, notify[0]['States'][4])
        try:
            ont_id.send_remove_public_key_transaction(identity, password,
                                                      hex_new_public_key, acct,
                                                      gas_limit, gas_price)
        except SDKException as e:
            msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] service execute ' \
                  'error!: [Invoke] Native serivce function execute error!: remove key failed: ' \
                  'public key has already been revoked'
            self.assertEqual(59000, e.args[0])
            self.assertEqual(msg, e.args[1])
Exemple #7
0
 def add_control_by_hex_private_key(self, ont_id: str, password: str, hex_private_key: str) -> Account:
     WalletManager.__check_ont_id(ont_id)
     if not isinstance(password, str):
         raise SDKException(ErrorCode.require_str_params)
     if not isinstance(hex_private_key, str):
         raise SDKException(ErrorCode.require_str_params)
     salt = get_random_hex_str(16)
     b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii')
     account = Account(hex_private_key, self.scheme)
     key = account.export_gcm_encrypted_private_key(password, salt)
     b58_address = account.get_address_base58()
     public_key = account.get_public_key_hex()
     ctrl = Control(kid='', key=key, salt=b64_salt, address=b58_address, public_key=public_key)
     identity = self.get_identity_by_ont_id(ont_id)
     identity.add_control(ctrl)
     return account
 def test_new_add_attribute_transaction(self):
     ont_id = sdk.native_vm().ont_id()
     attribute = {'key': 'try', 'type': 'string', 'value': 'attribute'}
     attribute_list = [attribute]
     private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
     acct = Account(private_key, SignatureScheme.SHA256withECDSA)
     hex_public_key = acct.get_public_key_hex()
     b58_address = acct.get_address_base58()
     acct_did = "did:ont:" + b58_address
     gas_limit = 20000
     gas_price = 500
     tx = ont_id.new_add_attribute_transaction(acct_did, hex_public_key, attribute_list, b58_address, gas_limit,
                                               gas_price)
     tx = sdk.sign_transaction(tx, acct)
     tx_hash = sdk.rpc.send_raw_transaction(tx)
     self.assertEqual(tx.hash256_explorer(), tx_hash)
 def test_new_registry_ont_id_transaction(self):
     ont_id = sdk.native_vm().ont_id()
     private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf'
     acct = Account(private_key, SignatureScheme.SHA256withECDSA)
     hex_public_key = acct.get_public_key_hex()
     b58_address = acct.get_address_base58()
     acct_did = "did:ont:" + b58_address
     gas_limit = 20000
     gas_price = 500
     tx = ont_id.new_registry_ont_id_transaction(acct_did, hex_public_key, b58_address, gas_limit, gas_price)
     tx = sdk.sign_transaction(tx, acct)
     self.assertEqual(64, len(tx.hash256_hex()))
     self.assertEqual(600, len(tx.serialize(is_hex=True)))
     try:
         sdk.rpc.send_raw_transaction(tx)
     except SDKException as e:
         self.assertEqual(59000, e.args[0])
         msg = 'Other Error, [NeoVmService] service system call error!: [SystemCall] ' \
               'service execute error!: [Invoke] Native serivce function execute error!: ' \
               'register ONT ID error: already registered'
         self.assertEqual(msg, e.args[1])
Exemple #10
0
 def __add_control(self,
                   ont_id: str,
                   password: str,
                   private_key: str = '',
                   salt: str = '') -> Account:
     if len(private_key) == 0:
         private_key = get_random_hex_str(64)
     if len(salt) == 0:
         salt = get_random_hex_str(16)
     account = Account(private_key, self.scheme)
     key = account.export_gcm_encrypted_private_key(password, salt)
     b58_address = account.get_address_base58()
     public_key = account.get_public_key_hex()
     b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii')
     ctrl = Control(kid='',
                    key=key,
                    salt=b64_salt,
                    address=b58_address,
                    public_key=public_key)
     identity = self.get_identity_by_ont_id(ont_id)
     identity.add_control(ctrl)
     return account