-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving signing templates from api-request folder
- Loading branch information
Showing
7 changed files
with
70 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { signTemplateResponses } from './sign-template-data'; | ||
import * as stateModule from './state'; | ||
import { config, nodarySignedTemplateResponses, nodaryTemplateResponses } from '../test/fixtures'; | ||
|
||
describe(signTemplateResponses.name, () => { | ||
it('signs template responses', async () => { | ||
const state = stateModule.getInitialState(config); | ||
jest.spyOn(stateModule, 'getState').mockReturnValue(state); | ||
jest.useFakeTimers().setSystemTime(new Date('2023-01-20')); | ||
|
||
const signedTemplateResponses = await signTemplateResponses(nodaryTemplateResponses); | ||
|
||
expect(signedTemplateResponses).toEqual(nodarySignedTemplateResponses); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ethers } from 'ethers'; | ||
import { go } from '@api3/promise-utils'; | ||
import * as node from '@api3/airnode-node'; | ||
import { isNil } from 'lodash'; | ||
import { logger } from './logger'; | ||
import { getState } from './state'; | ||
import { signWithTemplateId } from './utils'; | ||
import { SignedData, TemplateId } from './validation/schema'; | ||
|
||
export type SignedResponse = [TemplateId, SignedData]; | ||
|
||
export type TemplateResponse = [TemplateId, node.HttpGatewayApiCallSuccessResponse]; | ||
|
||
export const signTemplateResponses = async (templateResponses: TemplateResponse[]) => { | ||
logger.debug('Signing template responses', { templateResponses }); | ||
|
||
const signPromises = templateResponses.map(async ([templateId, response]) => { | ||
const encodedValue = response.data.encodedValue; | ||
const timestamp = Math.floor(Date.now() / 1000).toString(); | ||
|
||
const wallet = ethers.Wallet.fromMnemonic(getState().config.airnodeWalletMnemonic); | ||
const goSignWithTemplateId = await go(() => signWithTemplateId(wallet, templateId, timestamp, encodedValue)); | ||
if (!goSignWithTemplateId.success) { | ||
const message = `Failed to sign response. Error: "${goSignWithTemplateId.error}"`; | ||
logger.warn(message, { | ||
templateId, | ||
}); | ||
return null; | ||
} | ||
|
||
return [ | ||
templateId, | ||
{ | ||
timestamp: timestamp, | ||
encodedValue: encodedValue, | ||
signature: goSignWithTemplateId.data, | ||
}, | ||
]; | ||
}); | ||
const signedResponsesOrNull = await Promise.all(signPromises); | ||
const signedResponses = signedResponsesOrNull.filter((response): response is SignedResponse => !isNil(response)); | ||
|
||
return signedResponses; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters