Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decouple verifier from sender #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ dist
# TernJS port file
.tern-port
artifacts
deployments
cache
.pnp.*
.yarn/*
Expand Down
1 change: 0 additions & 1 deletion packages/contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
deployments
artifacts
6 changes: 3 additions & 3 deletions packages/contracts/contracts/OffchainResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "./IExtendedResolver.sol";
import "./SignatureVerifier.sol";

interface IResolverService {
function resolve(bytes calldata name, bytes calldata data) external view returns(bytes memory result, uint64 expires, bytes memory sig);
function resolve(bytes calldata name, bytes calldata data, address verifier) external view returns(bytes memory result, uint64 expires, bytes memory sig);
}

/**
Expand Down Expand Up @@ -39,15 +39,15 @@ contract OffchainResolver is IExtendedResolver, SupportsInterface {
* @return The return data, ABI encoded identically to the underlying function.
*/
function resolve(bytes calldata name, bytes calldata data) external override view returns(bytes memory) {
bytes memory callData = abi.encodeWithSelector(IResolverService.resolve.selector, name, data);
bytes memory callData = abi.encodeWithSelector(IResolverService.resolve.selector, name, data, address(this));
string[] memory urls = new string[](1);
urls[0] = url;
revert OffchainLookup(
address(this),
urls,
callData,
OffchainResolver.resolveWithProof.selector,
abi.encode(callData, address(this))
callData
);
}

Expand Down
3 changes: 1 addition & 2 deletions packages/contracts/contracts/SignatureVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ library SignatureVerifier {
*/
function verify(bytes calldata request, bytes calldata response) internal view returns(address, bytes memory) {
(bytes memory result, uint64 expires, bytes memory sig) = abi.decode(response, (bytes, uint64, bytes));
(bytes memory extraData, address sender) = abi.decode(request, (bytes, address));
address signer = ECDSA.recover(makeSignatureHash(sender, expires, extraData, result), sig);
address signer = ECDSA.recover(makeSignatureHash(address(this), expires, request, result), sig);
require(
expires >= block.timestamp,
"SignatureVerifier: Signature expired");
Expand Down
36 changes: 21 additions & 15 deletions packages/contracts/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
// const { task } = require('hardhat/config');
require('@nomiclabs/hardhat-etherscan');
require('@nomiclabs/hardhat-ethers');
require('@nomiclabs/hardhat-waffle');
require('hardhat-deploy');
require('hardhat-deploy-ethers');
require("@nomiclabs/hardhat-etherscan");
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-waffle");
require("hardhat-deploy");
require("hardhat-deploy-ethers");

real_accounts = undefined;
if (process.env.DEPLOYER_KEY && process.env.OWNER_KEY) {
real_accounts = [process.env.OWNER_KEY, process.env.DEPLOYER_KEY];
}
const gatewayurl =
'https://offchain-resolver-example.uc.r.appspot.com/{sender}/{data}.json';
"https://offchain-resolver-example.uc.r.appspot.com/{sender}/{data}.json";

let devgatewayurl = 'http://localhost:8080/{sender}/{data}.json';
let devgatewayurl = "http://localhost:8080/{sender}/{data}.json";
if (process.env.REMOTE_GATEWAY) {
devgatewayurl =
`${process.env.REMOTE_GATEWAY}/{sender}/{data}.json`;
devgatewayurl = `${process.env.REMOTE_GATEWAY}/{sender}/{data}.json`;
}
/**
* @type import('hardhat/config').HardhatUserConfig
*/

module.exports = {
solidity: '0.8.10',
solidity: "0.8.10",
networks: {
hardhat: {
throwOnCallFailures: false,
gatewayurl: devgatewayurl,
},
ropsten: {
url: `https://ropsten.infura.io/v3/${process.env.INFURA_ID}`,
tags: ['test', 'demo'],
tags: ["test", "demo"],
chainId: 3,
accounts: real_accounts,
gatewayurl,
},
rinkeby: {
url: `https://rinkeby.infura.io/v3/${process.env.INFURA_ID}`,
tags: ['test', 'demo'],
tags: ["test", "demo"],
chainId: 4,
accounts: real_accounts,
gatewayurl,
},
goerli: {
url: `https://goerli.infura.io/v3/${process.env.INFURA_ID}`,
tags: ['test', 'demo'],
tags: ["test", "demo"],
chainId: 5,
accounts: real_accounts,
gatewayurl,
},
sepolia: {
url: `https://sepolia.infura.io/v3/${process.env.INFURA_ID}`,
tags: ["test", "demo"],
chainId: 11155111,
accounts: real_accounts,
gatewayurl,
},
mainnet: {
url: `https://mainnet.infura.io/v3/${process.env.INFURA_ID}`,
tags: ['demo'],
tags: ["demo"],
chainId: 1,
accounts: real_accounts,
gatewayurl,
Expand All @@ -62,7 +68,7 @@ module.exports = {
},
namedAccounts: {
signer: {
default: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
default: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
},
deployer: {
default: 1,
Expand Down
3 changes: 1 addition & 2 deletions packages/contracts/test/TestOffchainResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ describe('OffchainResolver', function (accounts) {
it('resolves an address given a valid signature', async () => {
// Generate the response data
const response = defaultAbiCoder.encode(['bytes', 'uint64', 'bytes'], [resultData, expires, hexConcat([sig.r, sig._vs])]);
const encodedData = ethers.utils.defaultAbiCoder.encode(['bytes', 'address'], [callData, resolver.address]);

// Call the function with the request and response
const [result] = iface.decodeFunctionResult("addr", await resolver.resolveWithProof(response, encodedData));
const [result] = iface.decodeFunctionResult("addr", await resolver.resolveWithProof(response, callData));
expect(result).to.equal(TEST_ADDRESS);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/gateway-worker/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function makeServer(
server.add(IResolverService_abi, [
{
type: 'resolve',
func: async ([encodedName, data]: Result, request) => {
func: async ([encodedName, data, verifier]: Result, request) => {
const name = decodeDnsName(Buffer.from(encodedName.slice(2), 'hex'));
// Query the database
const { result, validUntil } = await query(await db, name, data);
Expand All @@ -112,7 +112,7 @@ export function makeServer(
['bytes', 'address', 'uint64', 'bytes32', 'bytes32'],
[
'0x1900',
request?.to,
verifier,
validUntil,
ethers.utils.keccak256(request?.data || '0x'),
ethers.utils.keccak256(result),
Expand Down
1 change: 1 addition & 0 deletions packages/gateway-worker/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('makeServer', () => {
const outerData = IResolverService.encodeFunctionData('resolve', [
dnsName(name),
innerData,
TEST_ADDRESS,
]);
// Call the server with address and data
const { status, body } = await server.call({
Expand Down
4 changes: 2 additions & 2 deletions packages/gateway/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function makeServer(signer: ethers.utils.SigningKey, db: Database) {
server.add(IResolverService_abi, [
{
type: 'resolve',
func: async ([encodedName, data]: Result, request) => {
func: async ([encodedName, data, verifier]: Result, request) => {
const name = decodeDnsName(Buffer.from(encodedName.slice(2), 'hex'));
// Query the database
const { result, validUntil } = await query(db, name, data);
Expand All @@ -107,7 +107,7 @@ export function makeServer(signer: ethers.utils.SigningKey, db: Database) {
['bytes', 'address', 'uint64', 'bytes32', 'bytes32'],
[
'0x1900',
request?.to,
verifier,
validUntil,
ethers.utils.keccak256(request?.data || '0x'),
ethers.utils.keccak256(result),
Expand Down
1 change: 1 addition & 0 deletions packages/gateway/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('makeServer', () => {
const outerData = IResolverService.encodeFunctionData('resolve', [
dnsName(name),
innerData,
TEST_ADDRESS,
]);
// Call the server with address and data
const { status, body } = await server.call({
Expand Down
Loading