Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigaMr committed Sep 24, 2024
1 parent d5e90ad commit 7536053
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 29 deletions.
108 changes: 79 additions & 29 deletions clients/py/sapphirepy/sapphire.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ def _encrypt_tx_params(pk: CalldataPublicKey,
raise TypeError("Invalid 'data' type", type(data))
encrypted_data = c.encrypt(data_bytes)

# if False:
if params[0]['from']: # and params[0]['from'] == account.address:
data_pack = _new_signed_call_data_pack(encrypted_data, data_bytes, params, web3, account)
params[0]['data'] = cbor2.dumps(data_pack, canonical=True)
params[0]['data'] = '0x' + params[0]['data'].hex()
return c

params[0]['data'] = HexStr('0x' + hexlify(encrypted_data).decode('ascii'))
if params[0]['from']: # and params[0]['from'] == account.address:
data_pack = _new_signed_call_data_pack(encrypted_data,
data_bytes,
params,
web3,
account)
params[0]['data'] = HexStr('0x' + hexlify(cbor2.dumps(data_pack)).decode('ascii'))
# params[0]['data'] = '0x' + params[0]['data'].hex()
else:
params[0]['data'] = HexStr('0x' + hexlify(encrypted_data).decode('ascii'))
return c


Expand All @@ -106,12 +108,10 @@ def _new_signed_call_data_pack(encrypted_data: bytes,
params: tuple[TxParams],
web3: Web3,
account: LocalAccount) -> dict:

# Update params with default values, these get used outside the scope of this function
params[0]['gas'] = params[0].get('gas', DEFAULT_GAS_LIMIT)
params[0]['gas'] = params[0].get('gas', DEFAULT_GAS_LIMIT)
params[0]['gasPrice'] = params[0].get('gasPrice', DEFAULT_GAS_PRICE)


domain_data = {
"name": "oasis-runtime-sdk/evm: signed query",
"version": "1.0.0",
Expand All @@ -120,6 +120,11 @@ def _new_signed_call_data_pack(encrypted_data: bytes,
# "salt": "",
}
msg_types = {
"EIP712Domain": [
{"name": "name", "type": "string"},
{"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"},
],
"Call": [
{"name": "from", "type": "address"},
{"name": "to", "type": "address"},
Expand All @@ -130,10 +135,10 @@ def _new_signed_call_data_pack(encrypted_data: bytes,
{"name": "leash", "type": "Leash"},
],
"Leash": [
{"name": "Nonce", "type": "uint64"},
{"name": "blockNumber", "type": "uint64"},
{"name": "blockHash", "type": "bytes32"},
{"name": "blockRange", "type": "uint64"},
{"name": "nonce", "type": "uint64"},
{"name": "block_number", "type": "uint64"},
{"name": "block_hash", "type": "bytes32"},
{"name": "block_range", "type": "uint64"},
],
}
nonce = web3.eth.get_transaction_count(params[0]['from'])
Expand All @@ -143,28 +148,73 @@ def _new_signed_call_data_pack(encrypted_data: bytes,
"from": params[0].get('from'),
"to": params[0].get('to'),
"value": params[0].get('value', 0),
"gasLimit": params[0]['gas'],
"gasPrice": params[0]['gasPrice'],
"gasLimit": params[0].get('gas', DEFAULT_GAS_LIMIT),
"gasPrice": params[0].get('gasPrice', DEFAULT_GAS_PRICE),
"data": data_bytes,
"leash":
{
"Nonce": nonce,
"blockNumber": block_number - 1,
"blockHash": unhexlify(block_hash[2:]),
"blockRange": DEFAULT_BLOCK_RANGE,
"nonce": nonce,
"block_number": block_number - 1,
"block_hash": unhexlify(block_hash[2:]),
"block_range": DEFAULT_BLOCK_RANGE,
}
}

# sign the message with the private key:
signed_msg = Account.sign_typed_data(account.key, domain_data, msg_types, msg_data)
# Testing
domain_data_test = {
"name": "oasis-runtime-sdk/evm: signed query",
"version": "1.0.0",
"chainId": 0x5aff,
# "verifyingContract": "",
# "salt": "",
}

leash = {
"nonce": nonce,
"block_number": block_number - 1,
"block_hash": unhexlify(block_hash[2:]),
"block_range": DEFAULT_BLOCK_RANGE,
msg_data_test = {
"from": '0xDce075E1C39b1ae0b75D554558b6451A226ffe00',
# "to": params[0].get('to', '0x'),
"to": '0x595cce2312b7dfb068eb7dbb8c2b0b593b5c8883',
"value": params[0].get('value', 0),
"gasLimit": params[0].get('gas', DEFAULT_GAS_LIMIT),
"gasPrice": params[0].get('gasPrice', DEFAULT_GAS_PRICE),
"data": unhexlify('e21f37ce'),
"leash":
{
"nonce": 0x12,
"blockNumber": 0x1234,
"blockHash": unhexlify('2ec361fee28d09a3ad2c4d5f7f95d409ce2b68c39b5d647edf0ea651e069e4a8'),
"blockRange": 15,
}
}
full_message = {
"types": msg_types,
"primaryType": "Call",
"domain": domain_data,
"message": msg_data,
}
data_pack = {

# sign the message with the private key:
signed_msg = Account.sign_typed_data(account.key,
full_message=full_message)

# Testing
# signed_msg = Account.sign_typed_data('c07b151fbc1e7a11dff926111188f8d872f62eba0396da97c0a24adb75161750', full_message=full_message)
# signed_msg = Account.sign_typed_data('c07b151fbc1e7a11dff926111188f8d872f62eba0396da97c0a24adb75161750',
# domain_data, msg_types, msg_data)

# leash = {
# "nonce": nonce,
# "block_number": block_number - 1,
# "block_hash": unhexlify(block_hash[2:]),
# "block_range": DEFAULT_BLOCK_RANGE,
# }
leash = msg_data['leash']

class RequestPack(TypedDict):
Data: bytes
Leash: dict
Signature: HexStr

data_pack: RequestPack = {
'data': cbor2.loads(encrypted_data),
'leash': leash,
'signature': signed_msg['signature'],
Expand Down
1 change: 1 addition & 0 deletions clients/py/sapphirepy/tests/testdata/Greeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;

contract Greeter {
string public greeting;
address public owner;

constructor() {
greeting = 'Hello';
Expand Down

0 comments on commit 7536053

Please sign in to comment.