-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathtransport-utils.test.ts
218 lines (208 loc) · 5.87 KB
/
transport-utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as starkwareCrypto from '@authereum/starkware-crypto'
import {
PriceDataPoint,
getKeyPair,
requireNormalizedPrice,
getPricePayload,
} from '../../src/transport/utils'
import { AdapterInputError } from '@chainlink/external-adapter-framework/validation/error'
import { LoggerFactoryProvider } from '@chainlink/external-adapter-framework/util'
//Since the test is directly using transport functions, we need to initialize the logger here
LoggerFactoryProvider.set()
describe('starkex', () => {
describe('getKeyPair', () => {
type KeyPairTest = {
name: string
testData: {
privateKey: string
starkMessage: string
expected: string
}
}
const tests: KeyPairTest[] = [
{
name: 'Test key derivation #1',
testData: {
privateKey: '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d',
starkMessage: 'StarkKeyDerivation',
expected: '0x1895a6a77ae14e7987b9cb51329a5adfb17bd8e7c638f92d6892d76e51cebcf',
},
},
]
tests.forEach((t) => {
it(`${t.name}`, async () => {
const keyPair = await getKeyPair(t.testData.privateKey, t.testData.starkMessage)
const pk = starkwareCrypto.getStarkPublicKey(keyPair)
const pkNormalized = '0x' + pk.substring(3)
expect(pkNormalized).toEqual(t.testData.expected)
})
})
})
describe('requireNormalizedPrice', () => {
type PriceNormalizationTest = {
name: string
testData: {
price: number
expected: undefined | string
error: boolean
}
}
const tests: PriceNormalizationTest[] = [
{
name: 'price (zero)',
testData: {
price: 0,
expected: '0',
error: false,
},
},
{
name: 'price (no decimals)',
testData: {
price: 11512,
expected: '11512000000000000000000',
error: false,
},
},
{
name: 'price (decimals)',
testData: {
price: 11512.34,
expected: '11512340000000000000000',
error: false,
},
},
{
name: 'Error: price number negative',
testData: {
price: -1151234,
expected: undefined,
error: true,
},
},
{
name: 'price number max safe number',
testData: {
price: Number.MAX_SAFE_INTEGER,
expected: '9007199254740991000000000000000000',
error: false,
},
},
{
name: 'Error: price number over max safe number',
testData: {
price: Number.MAX_SAFE_INTEGER + 1,
expected: '9007199254740992000000000000000000',
error: false,
},
},
{
name: 'price many decimals',
testData: {
price: 0.1234567899999999,
expected: '123456789999999900',
error: false,
},
},
{
name: 'price many decimals #2',
testData: {
price: 12.34567899999999,
expected: '12345678999999990000',
error: false,
},
},
{
name: 'price with 18 decimals',
testData: {
price: 0.000000000000000001,
expected: '1',
error: false,
},
},
{
name: 'price with 18 decimals #2',
testData: {
price: 1.000000000000000001,
expected: '1000000000000000000', // TODO: can we detect precision loss and throw?
error: false,
},
},
{
name: 'price scientific notation',
testData: {
price: 2.32323300000012e-12,
expected: '2323233',
error: false,
},
},
{
name: 'price with more than 18 decimals',
testData: {
price: 0.0000000000000000001,
expected: '0', // TODO: can we detect precision loss and throw?
error: false,
},
},
]
tests.forEach((t) => {
it(`${t.name}`, async () => {
try {
const normalizedPrice = requireNormalizedPrice(t.testData.price)
expect(normalizedPrice).toEqual(t.testData.expected)
expect(t.testData.error).toBe(false)
} catch (err) {
if (!(err instanceof AdapterInputError)) throw err
expect(t.testData.error).toBe(true)
}
})
})
})
describe('getPricePayload', () => {
type PricePayloadTest = {
name: string
testData: {
privateKey: string
starkMessage: string
data: PriceDataPoint
expected: {
signatureR: string
signatureS: string
starkKey: string
}
}
}
const tests: PricePayloadTest[] = [
{
name: 'signature construction #1',
testData: {
privateKey: '0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d',
starkMessage: 'StarkKeyDerivation',
data: {
oracleName: 'Maker',
assetName: 'BTCUSD',
timestamp: 1577836800,
price: requireNormalizedPrice(11512.34),
},
expected: {
signatureR: '0x6a7a118a6fa508c4f0eb77ea0efbc8d48a64d4a570d93f5c61cd886877cb920',
signatureS: '0x6de9006a7bbf610d583d514951c98d15b1a0f6c78846986491d2c8ca049fd55',
starkKey: '0x1895a6a77ae14e7987b9cb51329a5adfb17bd8e7c638f92d6892d76e51cebcf',
},
},
},
]
tests.forEach((t) => {
it(`${t.name}`, async () => {
const payload = await getPricePayload(
t.testData.privateKey,
t.testData.starkMessage,
t.testData.data,
)
expect(payload.starkKey).toEqual(t.testData.expected.starkKey)
expect(payload.signatureR).toEqual(t.testData.expected.signatureR)
expect(payload.signatureS).toEqual(t.testData.expected.signatureS)
})
})
})
})