From bdf4a008e7fdda44c60f3a4c83a3d0ac624de2b5 Mon Sep 17 00:00:00 2001 From: Dmytro Tkachuk Date: Thu, 23 Apr 2020 19:42:37 +0300 Subject: [PATCH] SKALE-2468 Add sign_hash method to sgx wallet --- setup.py | 2 +- skale/wallets/ledger_wallet.py | 3 +++ skale/wallets/sgx_wallet.py | 3 +++ tests/wallets/sgx_test.py | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index b6c868e1..2d6a2a17 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,7 @@ "web3==5.5.1", "asyncio==3.4.3", "pyyaml==5.3.1", - "sgx.py==0.4.dev6", + "sgx.py==0.5dev0", ], python_requires='>=3.6,<4', diff --git a/skale/wallets/ledger_wallet.py b/skale/wallets/ledger_wallet.py index 9c294dab..ec694155 100644 --- a/skale/wallets/ledger_wallet.py +++ b/skale/wallets/ledger_wallet.py @@ -139,6 +139,9 @@ def sign_and_send(self, tx) -> str: signed_tx = self.sign(tx) return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() + def sign_hash(self, unsigned_hash: str): + raise NotImplementedError('sign_hash is not implemented for hardware wallet') + @classmethod def parse_derive_result(cls, exchange_result): pk_len = exchange_result[0] diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index b8aeac34..a0a16ed6 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -41,6 +41,9 @@ def sign_and_send(self, tx_dict) -> str: signed_tx = self.sign(tx_dict) return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() + def sign_hash(self, unsigned_hash: str): + return self.sgx_client.sign_hash(unsigned_hash, self._key_name, self._web3.eth.chainId) + @property def address(self): return self._address diff --git a/tests/wallets/sgx_test.py b/tests/wallets/sgx_test.py index 96b5d7ab..d9320b2c 100644 --- a/tests/wallets/sgx_test.py +++ b/tests/wallets/sgx_test.py @@ -43,6 +43,15 @@ def sign(self, transaction_dict, key_name): 'v': 37, }) + def sign_hash(self, message, key_name, chain_id): + return AttributeDict({ + 'messageHash': HexBytes('0x0'), + 'r': 123, + 's': 123, + 'v': 27, + 'signature': HexBytes('0x0') + }) + def test_sgx_sign(): with mock.patch('skale.wallets.sgx_wallet.SgxClient', @@ -113,6 +122,16 @@ def test_sgx_sign_with_key(): wallet.sign(tx_dict) +def test_sgx_sign_hash(): + with mock.patch('skale.wallets.sgx_wallet.SgxClient', + new=SgxClient): + web3 = init_web3(ENDPOINT) + wallet = SgxWallet('TEST_ENDPOINT', web3, key_name='TEST_KEY') + unsigned_hash = '0x0' + signed_message = wallet.sign_hash(unsigned_hash) + assert signed_message.signature == HexBytes('0x0') + + def test_sgx_key_init(): with mock.patch('skale.wallets.sgx_wallet.SgxClient', new=SgxClient):