Skip to content

Commit

Permalink
Merge pull request #149 from skalenetwork/fix-sgx-wallet-sign-hash
Browse files Browse the repository at this point in the history
Fix sgx wallet sign hash
  • Loading branch information
dmytrotkk authored Apr 29, 2020
2 parents 2cad2b0 + 43d629e commit 5f4ff7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 16 additions & 1 deletion skale/wallets/sgx_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
# You should have received a copy of the GNU Affero General Public License
# along with SKALE.py. If not, see <https://www.gnu.org/licenses/>.

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


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)
Expand All @@ -42,7 +49,15 @@ 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)
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())
chain_id = None
return self.sgx_client.sign_hash(hash_to_sign, self._key_name, chain_id)

@property
def address(self):
Expand Down
4 changes: 4 additions & 0 deletions tests/contracts/delegation/token_state_test.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/wallets/sgx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})


Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 5f4ff7e

Please sign in to comment.