Skip to content

Commit

Permalink
add address to signature verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Sep 7, 2023
1 parent 0f0e56a commit 8814065
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
40 changes: 40 additions & 0 deletions contracts/verifier/signature/SignatureCcipVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,46 @@ contract SignatureCcipVerifier is CcipResponseVerifier {
return decodedResponse;
}

/**
* @param response The response bytes received from the AddrResolver.
* @return The Ethereum address resolved from the response bytes.
* @dev The AddrResolver stores addresses as bytes instead of Ethereum addresses.
* This is done to support other blockchain addresses, not just EVM addresses.
* However, the return type of `addr(bytes32)` is `address`,
* which means the client library expects an Ethereum address to be returned.
* To meet this expectation, we convert the bytes into an Ethereum address and return it.
*/
function resolveWithAddress(bytes calldata response, bytes calldata extraData) public view returns (address) {
(address signer, bytes memory result) = SignatureVerifier.verify(resolver, extraData, response);
require(signers[signer], "SignatureVerifier: Invalid signature");

/**
* The AddrResolver stores addresses as bytes instead of Ethereum addresses.
* This is to support other blockchain addresses and not just EVM addresses.
* However, the return type of `addr(bytes32)` is `address`,
* so the client library expects an Ethereum address to be returned.
* For that reason, we have to convert the bytes into an address.
*/
return address(bytes20(result));
}

/**
* @dev Can be called to determine what function to use to handle resolveWithProof. Returns the selector that then can be called via staticcall
* @return The four-byte function selector of the corresponding resolution function..
*/
function onResolveWithProof(bytes calldata, bytes calldata data) public pure override returns (bytes4) {
/**
* if the function addr(bytes32) is called, return the selector of resolveWithAddress.
*/
if (bytes4(data) == 0x3b3b57de) {
return this.resolveWithAddress.selector;
}
/**
* any other selector will be handled by the default resolveWithProof function.
*/
return this.resolveWithProof.selector;
}

/**
* @notice Get metadata about the CCIP Resolver
* @dev This function provides metadata about the CCIP Resolver, including its name, coin type, GraphQL URL, storage type, and encoded information.
Expand Down
2 changes: 2 additions & 0 deletions gateway/handler/optimism-bedrock/optimismBedrockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export async function optimismBedrockHandler(
);

console.log('Proof result: ', proofResult);
console.log('Handler result: ', result);
console.log(proof);

const proofParamType = await getProofParamType();
return ethers.utils.defaultAbiCoder.encode(['bytes', proofParamType], [result, proof]);
Expand Down
46 changes: 45 additions & 1 deletion test/gateway/signatureHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
SignatureCcipVerifier,
SignatureCcipVerifier__factory,
} from '../../typechain';
import { getGateWayUrl } from '../helper/getGatewayUrl';
import { getGateWayUrl, getGateWayUrlForAddress } from '../helper/getGatewayUrl';

global.logger = winston.createLogger({
level: process.env.LOG_LEVEL ?? 'info',
Expand Down Expand Up @@ -123,4 +123,48 @@ describe('Signature Handler', () => {

expect(resultString).to.equal(result);
});
it.only('Returns valid address from resolver', async () => {
process.env.SIGNER_PRIVATE_KEY = signer.privateKey;

const mock = new MockAdapter(axios);

await erc3668Resolver
.connect(vitalik)
.setVerifierForDomain(ethers.utils.namehash('vitalik.eth'), signatureCcipVerifier.address, [
'http://test/{sender}/{data}',
]);

const { callData } = await getGateWayUrlForAddress('vitalik.eth', erc3668Resolver);

//Pass the addr to the contract
const expected = ethers.utils.hexlify(vitalik.address)
console.log(expected)



mock.onGet(`http://test/${erc3668Resolver.address}/${callData}`).reply(200, expected);

const ccipConfig = {};
ccipConfig[erc3668Resolver.address] = {
type: 'signing',
handlerUrl: 'http://test',
};

const configReader = getConfigReader(JSON.stringify(ccipConfig));

config[erc3668Resolver.address] = ccipApp.use(ccipGateway(configReader));

const sender = erc3668Resolver.address;

// You the url returned by he contract to fetch the profile from the ccip gateway
const response = await request(ccipApp).get(`/${sender}/${callData}`).send();

expect(response.status).to.equal(200);
console.log('check');
const resultString = await erc3668Resolver.resolveWithProof(response.body.data, callData);

const ethersFormated = new ethers.providers.Formatter().callAddress(resultString)

expect(ethersFormated).to.equal(ethers.utils.getAddress(vitalik.address));
});
});
19 changes: 19 additions & 0 deletions test/helper/getGatewayUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ export const getGateWayUrl = async (ensName: string, recordName: string, offchai
return { gatewayUrl, sender, callData };
}
};
export const getGateWayUrlForAddress = async (ensName: string, offchainResolver: Contract) => {
try {
const addrData = new ethers.utils.Interface([
'function addr(bytes32 node) external view returns (address)',
]).encodeFunctionData('addr', [ethers.utils.namehash(ensName)]);

// This always revers and throws the OffchainLookup Exceptions hence we need to catch it
await offchainResolver.resolve(encodeEnsName(ensName), addrData);
return { gatewayUrl: '', callbackFunction: '', extraData: '' };
} catch (err: any) {
const { sender, urls, callData } = err.errorArgs;
// Decode call

// Replace template vars
const gatewayUrl = urls[0].replace('{sender}', sender).replace('{data}', callData);

return { gatewayUrl, sender, callData };
}
};

0 comments on commit 8814065

Please sign in to comment.