Skip to content

Commit

Permalink
decouple verifier from sender
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Feb 15, 2024
1 parent 952914d commit 92756cd
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
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
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

0 comments on commit 92756cd

Please sign in to comment.