From 4c8f024e55f0a85a87307169556a7c6e3be1f906 Mon Sep 17 00:00:00 2001 From: CedarMist <134699267+CedarMist@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:52:05 +0100 Subject: [PATCH] examples/onchain-signer: fixed example to work with docs --- examples/onchain-signer/contracts/Gasless.sol | 15 +++++-------- examples/onchain-signer/test/CommentBox.ts | 22 +++++++++++++++++-- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/examples/onchain-signer/contracts/Gasless.sol b/examples/onchain-signer/contracts/Gasless.sol index 03dccb56e..cb0478888 100644 --- a/examples/onchain-signer/contracts/Gasless.sol +++ b/examples/onchain-signer/contracts/Gasless.sol @@ -2,7 +2,6 @@ pragma solidity ^0.8.19; import {EIP155Signer} from "@oasisprotocol/sapphire-contracts/contracts/EIP155Signer.sol"; -import {EthereumUtils} from "@oasisprotocol/sapphire-contracts/contracts/EthereumUtils.sol"; struct EthereumKeypair { address addr; @@ -12,14 +11,13 @@ struct EthereumKeypair { // Proxy for gasless transaction. contract Gasless { - error KeyPairNotSet(); - EthereumKeypair private kp; - constructor () payable { - (kp.addr, kp.secret) = EthereumUtils.generateKeypair(); - - payable(kp.addr).transfer(msg.value); + constructor (EthereumKeypair memory keypair) payable { + kp = keypair; + if( msg.value > 0 ) { + payable(kp.addr).transfer(msg.value); + } } function makeProxyTx(address innercallAddr, bytes memory innercall) @@ -28,9 +26,6 @@ contract Gasless { returns (bytes memory output) { bytes memory data = abi.encode(innercallAddr, innercall); - if (kp.secret == 0) { - revert KeyPairNotSet(); - } // Call will invoke proxy(). return diff --git a/examples/onchain-signer/test/CommentBox.ts b/examples/onchain-signer/test/CommentBox.ts index 7b7c92b39..f932cc19f 100644 --- a/examples/onchain-signer/test/CommentBox.ts +++ b/examples/onchain-signer/test/CommentBox.ts @@ -1,19 +1,37 @@ import { expect } from 'chai'; -import { ethers } from 'hardhat'; +import { ethers, config } from 'hardhat'; import { CommentBox, Gasless } from '../typechain-types'; +import { EthereumKeypairStruct } from "../typechain-types/contracts/Gasless" import { parseEther } from 'ethers/lib/utils'; +import { HDAccountsUserConfig } from 'hardhat/types'; describe('CommentBox', function () { let commentBox: CommentBox; let gasless: Gasless; before(async () => { + // Derive the private key of the 1st (counting from 0) builtin hardhat test account. + const accounts = config.networks.hardhat + .accounts as unknown as HDAccountsUserConfig; + const wallet1 = ethers.Wallet.fromMnemonic( + accounts.mnemonic, + accounts.path + `/1`, + ); + + // Use it as the relayer private key. + // NOTE can be done by the contract with EthereumUtils.generateKeypair() + const keypair : EthereumKeypairStruct = { + addr: wallet1.address, + secret: wallet1.privateKey, + nonce: ethers.provider.getTransactionCount(wallet1.address), + }; + const CommentBoxFactory = await ethers.getContractFactory('CommentBox'); commentBox = await CommentBoxFactory.deploy(); await commentBox.deployed(); const GaslessFactory = await ethers.getContractFactory('Gasless'); - gasless = await GaslessFactory.deploy({value: parseEther('0.1')}); + gasless = await GaslessFactory.deploy(keypair, {value: parseEther('0.1')}); await gasless.deployed(); });