diff --git a/lib/handlers/hard-quote/handler.ts b/lib/handlers/hard-quote/handler.ts index 2ca9fbe..cf39aa7 100644 --- a/lib/handlers/hard-quote/handler.ts +++ b/lib/handlers/hard-quote/handler.ts @@ -37,16 +37,22 @@ export class QuoteHandler extends APIGLambdaHandler< ): Promise> { const { requestInjected: { log, metric }, - containerInjected: { quoters, orderServiceProvider, cosignerAddress }, + containerInjected: { quoters, orderServiceProvider }, requestBody, } = params; const start = Date.now(); metric.putMetric(Metric.QUOTE_REQUESTED, 1, MetricLoggerUnit.Count); - log.info({ cosignerAddress: cosignerAddress }, 'cosignerAddress'); const request = HardQuoteRequest.fromHardRequestBody(requestBody); + // re-create KmsClient every call to avoid clock skew issue + // https://github.com/aws/aws-sdk-js-v3/issues/6400 + const kmsKeyId = checkDefined(process.env.KMS_KEY_ID, 'KMS_KEY_ID is not defined'); + const awsRegion = checkDefined(process.env.REGION, 'REGION is not defined'); + const cosigner = new KmsSigner(new KMSClient({ region: awsRegion }), kmsKeyId); + const cosignerAddress = await cosigner.getAddress(); + // we dont have access to the cosigner key, throw if (request.order.info.cosigner !== cosignerAddress) { log.error({ cosignerInReq: request.order.info.cosigner, expected: cosignerAddress }, 'Unknown cosigner'); @@ -92,10 +98,6 @@ export class QuoteHandler extends APIGLambdaHandler< cosignerData = getDefaultCosignerData(request); log.info({ cosignerData: cosignerData }, 'open order with default cosignerData'); } - - const kmsKeyId = checkDefined(process.env.KMS_KEY_ID, 'KMS_KEY_ID is not defined'); - const awsRegion = checkDefined(process.env.REGION, 'REGION is not defined'); - const cosigner = new KmsSigner(new KMSClient({ region: awsRegion }), kmsKeyId); const cosignature = await cosigner.signDigest(request.order.cosignatureHash(cosignerData)); const cosignedOrder = CosignedV2DutchOrder.fromUnsignedOrder(request.order, cosignerData, cosignature); diff --git a/lib/handlers/hard-quote/injector.ts b/lib/handlers/hard-quote/injector.ts index 0625903..43a0197 100644 --- a/lib/handlers/hard-quote/injector.ts +++ b/lib/handlers/hard-quote/injector.ts @@ -1,5 +1,3 @@ -import { KMSClient } from '@aws-sdk/client-kms'; -import { KmsSigner } from '@uniswap/signer'; import { IMetric, setGlobalLogger, setGlobalMetric } from '@uniswap/smart-order-router'; import { MetricsLogger } from 'aws-embedded-metrics'; import { APIGatewayProxyEvent, Context } from 'aws-lambda'; @@ -23,7 +21,6 @@ import { HardQuoteRequestBody } from './schema'; export interface ContainerInjected { quoters: Quoter[]; firehose: FirehoseLogger; - cosignerAddress: string; orderServiceProvider: OrderServiceProvider; } @@ -44,11 +41,6 @@ export class QuoteInjector extends ApiInjector): UnsignedV2DutchOrder => { const now = Math.floor(new Date().getTime() / 1000); return new UnsignedV2DutchOrder( @@ -70,7 +77,17 @@ export const getOrder = (data: Partial): UnsignedV2Dut describe('Quote handler', () => { const swapperWallet = Wallet.createRandom(); const cosignerWallet = Wallet.createRandom(); - + + const mockGetAddress = jest.fn().mockResolvedValue(cosignerWallet.address); + const mockSignDigest = jest.fn().mockImplementation((digest) => cosignerWallet.signMessage(ethers.utils.arrayify(digest))); + + (KmsSigner as jest.Mock).mockImplementation(() => ({ + getAddress: mockGetAddress, + signDigest: mockSignDigest, + })); + (KMSClient as jest.Mock).mockImplementation(() => jest.fn()); + + // Creating mocks for all the handler dependencies. const requestInjectedMock: Promise = new Promise( (resolve) => @@ -89,8 +106,6 @@ describe('Quote handler', () => { getContainerInjected: () => { return { quoters, - cosigner: cosignerWallet._signingKey(), - cosignerAddress: cosignerWallet.address, orderServiceProvider: new MockOrderServiceProvider(), }; },