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

Implement relayRequestId security scheme #1286

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .changeset/khaki-crabs-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@api3/airnode-adapter': patch
'@api3/airnode-examples': patch
'@api3/airnode-validator': patch
---

Implement relayRequestId security scheme and use it in the the relay security schemes example integration
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,21 @@ describe('relay metadata', () => {
cookies: {},
});
});

it('relays request Id', () => {
const ois = fixtures.buildOIS();
const scheme: ApiSecurityScheme = {
in: 'query',
type: 'relayRequestId',
name: 'myRequestId',
};
ois.apiSpecifications.components.securitySchemes.myApiSecurityScheme = scheme;
const options = fixtures.buildCacheRequestOptions({ ois });
const res = authentication.buildParameters(options);
expect(res).toEqual({
query: { myRequestId: '0xcf2816af81f9cc7c9879dc84ce29c00fe1e290bcb8d2e4b204be1eeb120811bf' },
headers: {},
cookies: {},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ function getSchemeAuthentication(
return getRelayAuthSchemeFromMetadata(apiSecurityScheme, options, 'sponsorAddress');
case 'relaySponsorWalletAddress':
return getRelayAuthSchemeFromMetadata(apiSecurityScheme, options, 'sponsorWalletAddress');
case 'relayRequestId':
return getRelayAuthSchemeFromMetadata(apiSecurityScheme, options, 'requestId');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import "@api3/airnode-protocol/contracts/rrp/requesters/RrpRequesterV0.sol";
// An example requester which expects the response from Airnode consists of multiple values.
contract Requester is RrpRequesterV0 {
mapping(bytes32 => bool) public incomingFulfillments;
mapping(bytes32 => address) public requesterAddress;
mapping(bytes32 => address) public sponsorAddress;
mapping(bytes32 => address) public sponsorWalletAddress;
mapping(bytes32 => uint256) public chainId;
mapping(bytes32 => bytes32) public chainType;
mapping(bytes32 => address) public relayedRequesterAddress;
mapping(bytes32 => address) public relayedSponsorAddress;
mapping(bytes32 => address) public relayedSponsorWalletAddress;
mapping(bytes32 => uint256) public relayedChainId;
mapping(bytes32 => bytes32) public relayedChainType;
mapping(bytes32 => bytes32) public relayedRequestId;

constructor(address airnodeAddress) RrpRequesterV0(airnodeAddress) {}

Expand Down Expand Up @@ -39,12 +40,22 @@ contract Requester is RrpRequesterV0 {
{
require(incomingFulfillments[requestId], "No such request made");
delete incomingFulfillments[requestId];
(address v1, address v2, address v3, uint256 v4, bytes32 v5) = abi
.decode(data, (address, address, address, uint256, bytes32));
requesterAddress[requestId] = v1;
sponsorAddress[requestId] = v2;
sponsorWalletAddress[requestId] = v3;
chainId[requestId] = v4;
chainType[requestId] = v5;
(
address v1,
address v2,
address v3,
uint256 v4,
bytes32 v5,
bytes32 v6
) = abi.decode(
data,
(address, address, address, uint256, bytes32, bytes32)
);
relayedRequesterAddress[requestId] = v1;
relayedSponsorAddress[requestId] = v2;
relayedSponsorWalletAddress[requestId] = v3;
relayedChainId[requestId] = v4;
relayedChainType[requestId] = v5;
relayedRequestId[requestId] = v6;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ demonstrate how the value can be sent via `query`, `header`, and `cookie`:
- relaySponsorWalletAddress
- relayChainId
- relayChainType
- relayRequestId

For more information on supported security schemes, refer to the
[docs](https://docs.api3.org/airnode/latest/grp-providers/guides/build-an-airnode/api-security.html).
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
"in": "cookie",
"type": "relayChainType",
"name": "chainType"
},
"relayRequestId": {
"in": "query",
"type": "relayRequestId",
"name": "requestId"
}
}
},
Expand All @@ -107,7 +112,8 @@
"relayChainType": [],
"relayRequesterAddress": [],
"relaySponsorAddress": [],
"relaySponsorWalletAddress": []
"relaySponsorWalletAddress": [],
"relayRequestId": []
}
},
"endpoints": [
Expand All @@ -121,15 +127,15 @@
"reservedParameters": [
{
"name": "_type",
"fixed": "address,address,address,uint256,string32"
"fixed": "address,address,address,uint256,string32,bytes32"
},
{
"name": "_path",
"fixed": "headers.Requesteraddress,args.sponsorAddress,args.sponsorWalletAddress,args.chainId,headers.Cookie"
"fixed": "headers.Requesteraddress,args.sponsorAddress,args.sponsorWalletAddress,args.chainId,headers.Cookie,args.requestId"
},
{
"name": "_times",
"fixed": ",,,,"
"fixed": ",,,,,"
}
],
"parameters": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const createConfig = async (generateExampleFile: boolean): Promise<Config> => ({
type: 'relayChainType',
name: 'chainType',
},
relayRequestId: {
in: 'query',
type: 'relayRequestId',
name: 'requestId',
},
},
},
security: {
Expand All @@ -115,6 +120,7 @@ const createConfig = async (generateExampleFile: boolean): Promise<Config> => ({
relayRequesterAddress: [],
relaySponsorAddress: [],
relaySponsorWalletAddress: [],
relayRequestId: [],
},
},
endpoints: [
Expand All @@ -128,16 +134,16 @@ const createConfig = async (generateExampleFile: boolean): Promise<Config> => ({
reservedParameters: [
{
name: '_type',
fixed: 'address,address,address,uint256,string32',
fixed: 'address,address,address,uint256,string32,bytes32',
},
{
name: '_path',
fixed:
'headers.Requesteraddress,args.sponsorAddress,args.sponsorWalletAddress,args.chainId,headers.Cookie',
'headers.Requesteraddress,args.sponsorAddress,args.sponsorWalletAddress,args.chainId,headers.Cookie,args.requestId',
},
{
name: '_times',
fixed: ',,,,',
fixed: ',,,,,',
},
],
parameters: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ export const printResponse = async (requestId: string) => {
const integrationInfo = readIntegrationInfo();
const requester = await getDeployedContract(`contracts/${integrationInfo.integration}/Requester.sol`);

const requesterAddress = await requester.requesterAddress(requestId);
const sponsorAddress = await requester.sponsorAddress(requestId);
const sponsorWalletAddress = await requester.sponsorWalletAddress(requestId);
const chainId = await requester.chainId(requestId);
const relayedRequesterAddress = await requester.relayedRequesterAddress(requestId);
const relayedSponsorAddress = await requester.relayedSponsorAddress(requestId);
const relayedSponsorWalletAddress = await requester.relayedSponsorWalletAddress(requestId);
const relayedChainId = await requester.relayedChainId(requestId);
const relayedRequestId = await requester.relayedRequestId(requestId);
// decode and extract chain from API response `chainType=evm;`
const chainType = ethers.utils
.parseBytes32String(await requester.chainType(requestId))
const relayedChainType = ethers.utils
.parseBytes32String(await requester.relayedChainType(requestId))
.split('=')[1]
.split(';')[0];

cliPrint.info(`The following was successfully relayed:
requesterAddress: ${requesterAddress}
sponsorAddress: ${sponsorAddress}
sponsorWalletAddress: ${sponsorWalletAddress}
chainId: ${chainId}
chainType: ${chainType}`);
cliPrint.info(`
For requestId: ${requestId}
the following properties were successfully relayed:

requesterAddress: ${relayedRequesterAddress}
sponsorAddress: ${relayedSponsorAddress}
sponsorWalletAddress: ${relayedSponsorWalletAddress}
chainId: ${relayedChainId}
chainType: ${relayedChainType}
requestId: ${relayedRequestId}
`);
};
1 change: 1 addition & 0 deletions packages/airnode-validator/src/ois/ois.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const RELAY_METADATA_TYPES = [
'relayRequesterAddress',
'relaySponsorAddress',
'relaySponsorWalletAddress',
'relayRequestId',
] as const;

export const apiSecuritySchemeSchema = z.discriminatedUnion('type', [
Expand Down