From 49b639228ed3caacd12aebcf416766ea8a88f2b8 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 13:51:20 +0300 Subject: [PATCH 01/11] Fix sgx wallet sign hash --- skale/wallets/sgx_wallet.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index a0a16ed6..50a90235 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -20,6 +20,7 @@ from sgx import SgxClient from skale.utils.web3_utils import get_eth_nonce from skale.wallets.common import BaseWallet +from eth_account import messages class SgxWallet(BaseWallet): @@ -42,7 +43,9 @@ def sign_and_send(self, tx_dict) -> str: 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) + unsigned_message = messages.encode_defunct(hexstr=unsigned_hash) + return self.sgx_client.sign_hash(unsigned_message, self._key_name, + self._web3.eth.chainId) @property def address(self): From e1db62873d7cc5ef72fd7c80dc816d5d9c757543 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:16:46 +0300 Subject: [PATCH 02/11] Update sign_hash param --- skale/wallets/sgx_wallet.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 50a90235..5d91afaf 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -43,9 +43,8 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - unsigned_message = messages.encode_defunct(hexstr=unsigned_hash) - return self.sgx_client.sign_hash(unsigned_message, self._key_name, - self._web3.eth.chainId) + unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body().hex() + return self.sgx_client.sign_hash(unsigned_message, self._key_name, None) @property def address(self): From 281f665fa25a16373e992ffe1c69ba5ee0501c53 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:20:49 +0300 Subject: [PATCH 03/11] Replace body call with body attribute --- skale/wallets/sgx_wallet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 5d91afaf..e5b3f6e8 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -43,7 +43,7 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body().hex() + unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() return self.sgx_client.sign_hash(unsigned_message, self._key_name, None) @property From bf5a438d9d0f31ea3beaec88bf56fbcdb1a21845 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:34:15 +0300 Subject: [PATCH 04/11] Leave only chainId == None --- skale/wallets/sgx_wallet.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index e5b3f6e8..3dd67a45 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -20,7 +20,7 @@ from sgx import SgxClient from skale.utils.web3_utils import get_eth_nonce from skale.wallets.common import BaseWallet -from eth_account import messages +# from eth_account import messages class SgxWallet(BaseWallet): @@ -43,7 +43,8 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() + # unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() + unsigned_message = unsigned_hash return self.sgx_client.sign_hash(unsigned_message, self._key_name, None) @property From e6be463e71527e076ef12fefa39b35de3969ea61 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:52:48 +0300 Subject: [PATCH 05/11] Remove chainId=None --- skale/wallets/sgx_wallet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 3dd67a45..0a6dd12f 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -20,7 +20,7 @@ from sgx import SgxClient from skale.utils.web3_utils import get_eth_nonce from skale.wallets.common import BaseWallet -# from eth_account import messages +from eth_account import messages class SgxWallet(BaseWallet): @@ -43,9 +43,9 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - # unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() - unsigned_message = unsigned_hash - return self.sgx_client.sign_hash(unsigned_message, self._key_name, None) + unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() + # unsigned_message = unsigned_hash + return self.sgx_client.sign_hash(unsigned_message, self._key_name, self._web3.eth.chainId) @property def address(self): From 98ce2568fc31e0554dd983e52e87bd4feb5fb2e0 Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:56:41 +0300 Subject: [PATCH 06/11] Add some debug logs --- skale/wallets/sgx_wallet.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 0a6dd12f..38959a38 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -17,12 +17,17 @@ # You should have received a copy of the GNU Affero General Public License # along with SKALE.py. If not, see . +import logging + from sgx import SgxClient from skale.utils.web3_utils import get_eth_nonce from skale.wallets.common import BaseWallet from eth_account import messages +logger = logging.getLogger(__name__) + + class SgxWallet(BaseWallet): def __init__(self, sgx_endpoint, web3, key_name=None, path_to_cert=None): self.sgx_client = SgxClient(sgx_endpoint, path_to_cert=path_to_cert) @@ -43,9 +48,11 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body.hex() - # unsigned_message = unsigned_hash - return self.sgx_client.sign_hash(unsigned_message, self._key_name, self._web3.eth.chainId) + unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body + unsigned_hash = unsigned_message.hex() + logger.info(f'IVD unsigned_message {unsigned_message}') + logger.info(f'IVD unsigned_message {unsigned_hash}') + return self.sgx_client.sign_hash(unsigned_hash, self._key_name, self._web3.eth.chainId) @property def address(self): From 7c949c16f276034f488200f2e28270ae0e81dc7d Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 14:57:57 +0300 Subject: [PATCH 07/11] Fix log message --- skale/wallets/sgx_wallet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 38959a38..75b715d6 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -50,8 +50,8 @@ def sign_and_send(self, tx_dict) -> str: def sign_hash(self, unsigned_hash: str): unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body unsigned_hash = unsigned_message.hex() - logger.info(f'IVD unsigned_message {unsigned_message}') - logger.info(f'IVD unsigned_message {unsigned_hash}') + logger.info(f'IVD unsigned_message bytes {unsigned_message}') + logger.info(f'IVD unsigned_message hex {unsigned_hash}') return self.sgx_client.sign_hash(unsigned_hash, self._key_name, self._web3.eth.chainId) @property From 0c080e25f497ef52187af1f5acda80922aa81a2d Mon Sep 17 00:00:00 2001 From: badrogger Date: Mon, 27 Apr 2020 17:31:00 +0300 Subject: [PATCH 08/11] Change chainId back to None --- skale/wallets/sgx_wallet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index 75b715d6..db5c1355 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -52,7 +52,7 @@ def sign_hash(self, unsigned_hash: str): unsigned_hash = unsigned_message.hex() logger.info(f'IVD unsigned_message bytes {unsigned_message}') logger.info(f'IVD unsigned_message hex {unsigned_hash}') - return self.sgx_client.sign_hash(unsigned_hash, self._key_name, self._web3.eth.chainId) + return self.sgx_client.sign_hash(unsigned_hash, self._key_name, None) @property def address(self): From c3a2ca370f77b81637a39354d8a80749e5f6be7b Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 29 Apr 2020 10:20:36 +0300 Subject: [PATCH 09/11] Add etherium header to hash --- skale/wallets/sgx_wallet.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index db5c1355..b606a665 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -20,9 +20,10 @@ import logging from sgx import SgxClient +from web3 import Web3 + from skale.utils.web3_utils import get_eth_nonce from skale.wallets.common import BaseWallet -from eth_account import messages logger = logging.getLogger(__name__) @@ -48,11 +49,12 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - unsigned_message = messages.encode_defunct(hexstr=unsigned_hash).body - unsigned_hash = unsigned_message.hex() - logger.info(f'IVD unsigned_message bytes {unsigned_message}') - logger.info(f'IVD unsigned_message hex {unsigned_hash}') - return self.sgx_client.sign_hash(unsigned_hash, self._key_name, None) + body = bytes.fromhex(unsigned_hash[2:]) + header = b'\x19Ethereum Signed Message:\n32' + normalized_hash = header + body + hash_to_sign = Web3.keccak(hexstr='0x' + normalized_hash.hex()) + chain_id = None + return self.sgx_client.sign_hash(hash_to_sign, self._key_name, chain_id) @property def address(self): From 547a497ced8ee09052e4b91be32af5a53f508370 Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 29 Apr 2020 13:05:39 +0300 Subject: [PATCH 10/11] Fix tests --- skale/wallets/sgx_wallet.py | 5 ++++- tests/wallets/sgx_test.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/skale/wallets/sgx_wallet.py b/skale/wallets/sgx_wallet.py index b606a665..fb380fef 100644 --- a/skale/wallets/sgx_wallet.py +++ b/skale/wallets/sgx_wallet.py @@ -49,7 +49,10 @@ def sign_and_send(self, tx_dict) -> str: return self._web3.eth.sendRawTransaction(signed_tx.rawTransaction).hex() def sign_hash(self, unsigned_hash: str): - body = bytes.fromhex(unsigned_hash[2:]) + if unsigned_hash.startswith('0x'): + unsigned_hash = unsigned_hash[2:] + + body = bytes.fromhex(unsigned_hash) header = b'\x19Ethereum Signed Message:\n32' normalized_hash = header + body hash_to_sign = Web3.keccak(hexstr='0x' + normalized_hash.hex()) diff --git a/tests/wallets/sgx_test.py b/tests/wallets/sgx_test.py index d9320b2c..0c21b291 100644 --- a/tests/wallets/sgx_test.py +++ b/tests/wallets/sgx_test.py @@ -45,11 +45,11 @@ def sign(self, transaction_dict, key_name): def sign_hash(self, message, key_name, chain_id): return AttributeDict({ - 'messageHash': HexBytes('0x0'), + 'messageHash': HexBytes('0x31323331'), 'r': 123, 's': 123, 'v': 27, - 'signature': HexBytes('0x0') + 'signature': HexBytes('0x6161616161613131313131') }) @@ -127,9 +127,9 @@ def test_sgx_sign_hash(): new=SgxClient): web3 = init_web3(ENDPOINT) wallet = SgxWallet('TEST_ENDPOINT', web3, key_name='TEST_KEY') - unsigned_hash = '0x0' + unsigned_hash = '0x31323331' signed_message = wallet.sign_hash(unsigned_hash) - assert signed_message.signature == HexBytes('0x0') + assert signed_message.signature == HexBytes('0x6161616161613131313131') def test_sgx_key_init(): From 43d629e8c03972b994415a8923feaea6a50e53b4 Mon Sep 17 00:00:00 2001 From: badrogger Date: Wed, 29 Apr 2020 13:08:28 +0300 Subject: [PATCH 11/11] Add tests for token_state contract --- tests/contracts/delegation/token_state_test.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tests/contracts/delegation/token_state_test.py diff --git a/tests/contracts/delegation/token_state_test.py b/tests/contracts/delegation/token_state_test.py new file mode 100644 index 00000000..22387385 --- /dev/null +++ b/tests/contracts/delegation/token_state_test.py @@ -0,0 +1,4 @@ +def test_get_and_update_locked_amount(skale): + res = skale.token_state.get_and_update_locked_amount( + skale.wallet.address) + assert res > 0