Skip to content

Commit

Permalink
examples/onchain-signer: fixed example to work with docs
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Oct 19, 2023
1 parent be85b2c commit 4c8f024
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 5 additions & 10 deletions examples/onchain-signer/contracts/Gasless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand Down
22 changes: 20 additions & 2 deletions examples/onchain-signer/test/CommentBox.ts
Original file line number Diff line number Diff line change
@@ -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();
});

Expand Down

0 comments on commit 4c8f024

Please sign in to comment.