Skip to content

Commit

Permalink
update reserves endpoint with new payload & verification
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcallister-cll committed Dec 11, 2024
1 parent 4be3596 commit 2808a0d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 143 deletions.
5 changes: 0 additions & 5 deletions packages/sources/bitgo-reserves/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ export const config = new AdapterConfig({
type: 'string',
default: 'https://reserves.usdstandard-test.com/por.json',
},
API_ENDPOINT2: {
description: 'An API endpoint for Data Provider',
type: 'string',
required: true,
},
VERIFICATION_PUBKEY: {
description: 'Public key used for verifying data signature',
type: 'string',
Expand Down
1 change: 0 additions & 1 deletion packages/sources/bitgo-reserves/src/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { endpoint as reserves } from './reserves'
export { endpoint as reserves2 } from './reserves2'
19 changes: 0 additions & 19 deletions packages/sources/bitgo-reserves/src/endpoint/reserves2.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/sources/bitgo-reserves/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
import { config } from './config'
import { reserves, reserves2 } from './endpoint'
import { reserves } from './endpoint'

export const adapter = new Adapter({
defaultEndpoint: reserves.name,
name: 'BITGO_RESERVES',
config,
endpoints: [reserves, reserves2],
endpoints: [reserves],
rateLimiting: {
tiers: {
default: {
Expand Down
60 changes: 49 additions & 11 deletions packages/sources/bitgo-reserves/src/transport/reserves.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { HttpTransport } from '@chainlink/external-adapter-framework/transports'
import { BaseEndpointTypes } from '../endpoint/reserves'
import * as crypto from 'crypto'

export interface ResponseSchema {
totalReserve: number
export interface DataSchema {
totalReserve?: string
reserveAmount?: string
cashReserve: string
investedReserve: string
lastUpdated: string
cashReserve: number
investedReserve: number
}

export interface ResponseSchema {
data: string // formatted & escaped DataSchema
dataSignature: string
ripcord: boolean
}

export type HttpTransportTypes = BaseEndpointTypes & {
Expand All @@ -27,26 +35,56 @@ export const httpTransport = new HttpTransport<HttpTransportTypes>({
}
})
},
parseResponse: (params, response) => {
const timestamps = {
providerIndicatedTimeUnixMs: new Date(response.data.lastUpdated).getTime(),
}
parseResponse: (params, response, adapterSettings) => {
const payload = response.data

if (!response.data) {
if (!payload || !payload.data) {
return params.map((param) => {
return {
params: param,
response: {
errorMessage: `The data provider didn't return any value`,
statusCode: 502,
timestamps,
},
}
})
}

if (payload.ripcord) {
return [
{
params: params[0],
response: {
errorMessage: 'Ripcord indicator true',
ripcord: response.data.ripcord,
statusCode: 502,
},
},
]
}

const publicKey = adapterSettings.VERIFICATION_PUBKEY
const verifier = crypto.createVerify('sha256')
verifier.update(payload.data)
if (!verifier.verify(publicKey, payload.dataSignature, 'base64')) {
return params.map((param) => {
return {
params: param,
response: {
errorMessage: `Data verification failed`,
statusCode: 502,
},
}
})
}

const data = JSON.parse(payload.data) as DataSchema
const timestamps = {
providerIndicatedTimeUnixMs: new Date(data.lastUpdated).getTime(),
}

return params.map((param) => {
const result = response.data.totalReserve
const result = Number(data.totalReserve) || Number(data.reserveAmount)
return {
params: param,
response: {
Expand Down
99 changes: 0 additions & 99 deletions packages/sources/bitgo-reserves/src/transport/reserves2.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
exports[`execute reserves endpoint should return success 1`] = `
{
"data": {
"result": 1234567.89,
"result": 12345678.9,
},
"result": 1234567.89,
"result": 12345678.9,
"statusCode": 200,
"timestamps": {
"providerDataReceivedUnixMs": 978347471111,
"providerDataRequestedUnixMs": 978347471111,
"providerIndicatedTimeUnixMs": 1727745825000,
"providerIndicatedTimeUnixMs": 1733793825000,
},
}
`;
`;
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ describe('execute', () => {
beforeAll(async () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.API_ENDPOINT = 'http://test-endpoint.com'
process.env.VERIFICATION_PUBKEY = 'test'

const mockDate = new Date('2001-01-01T11:11:11.111Z')
spy = jest.spyOn(Date, 'now').mockReturnValue(mockDate.getTime())

const adapter = (await import('./../../src')).adapter
jest.mock('crypto', () => ({
createVerify: jest.fn().mockImplementation((_algo) => ({
update: jest.fn().mockReturnThis(),
verify: jest.fn().mockImplementationOnce((a, b, c) => true),
})),
}))
const adapter = (await import('../../src')).adapter
adapter.rateLimiting = undefined
testAdapter = await TestAdapter.startWithMockedCache(adapter, {
testAdapter: {} as TestAdapter<never>,
Expand All @@ -43,4 +50,5 @@ describe('execute', () => {
expect(response.json()).toMatchSnapshot()
})
})
// Note: issues with mock verifier prevent further tests
})
3 changes: 2 additions & 1 deletion packages/sources/bitgo-reserves/test/integration/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const mockResponseSuccess = (): nock.Scope =>
.reply(
200,
() => ({
totalReserve: 1234567.89,
data: '{"reserveAmount":"12345678.90","cashReserve":"2345678.90","investedReserve":"10000000.00","lastUpdated":"2024-12-10T01:23:45Z"}',
dataSignature: 'testsig',
lastUpdated: '2024-10-01T01:23:45Z',
}),
[
Expand Down

0 comments on commit 2808a0d

Please sign in to comment.