@com-chain/jsc3l 2.0.1-rc.2 → 2.0.1-rc.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +12 -2
  2. package/build/bcRead.d.ts +8 -10
  3. package/build/bcRead.js +162 -49
  4. package/build/bcRead.js.map +1 -1
  5. package/build/bcTransaction.d.ts +8 -2
  6. package/build/bcTransaction.js +141 -108
  7. package/build/bcTransaction.js.map +1 -1
  8. package/build/blockies.js.map +1 -1
  9. package/build/config/transactions.d.ts +44 -0
  10. package/build/config/transactions.js +29 -0
  11. package/build/config/transactions.js.map +1 -0
  12. package/build/connection.d.ts +1 -1
  13. package/build/connection.js +44 -13
  14. package/build/connection.js.map +1 -1
  15. package/build/customization.js +13 -6
  16. package/build/customization.js.map +1 -1
  17. package/build/ethereum/cipher.js +3 -3
  18. package/build/ethereum/cipher.js.map +1 -1
  19. package/build/ethereum/ethFuncs.js +37 -1
  20. package/build/ethereum/ethFuncs.js.map +1 -1
  21. package/build/ethereum/etherUnits.js +5 -5
  22. package/build/ethereum/etherUnits.js.map +1 -1
  23. package/build/ethereum/myetherwallet.d.ts +1 -2
  24. package/build/ethereum/myetherwallet.js.map +1 -1
  25. package/build/ethereum/uiFuncs.d.ts +1 -3
  26. package/build/ethereum/uiFuncs.js +13 -23
  27. package/build/ethereum/uiFuncs.js.map +1 -1
  28. package/build/exception.d.ts +8 -0
  29. package/build/exception.js +14 -0
  30. package/build/exception.js.map +1 -0
  31. package/build/index.d.ts +8 -5
  32. package/build/index.js +38 -19
  33. package/build/index.js.map +1 -1
  34. package/build/memo.js +6 -3
  35. package/build/memo.js.map +1 -1
  36. package/build/qr.d.ts +31 -6
  37. package/build/qr.js +32 -12
  38. package/build/qr.js.map +1 -1
  39. package/build/rest/ajaxReq.d.ts +3 -10
  40. package/build/rest/ajaxReq.js +16 -33
  41. package/build/rest/ajaxReq.js.map +1 -1
  42. package/build/rest/http.js +6 -1
  43. package/build/rest/http.js.map +1 -1
  44. package/build/rest/serializer.js.map +1 -1
  45. package/build/type.d.ts +4 -4
  46. package/build/utils.d.ts +15 -0
  47. package/build/utils.js +270 -0
  48. package/build/utils.js.map +1 -0
  49. package/build/wallet.d.ts +6 -8
  50. package/build/wallet.js +35 -39
  51. package/build/wallet.js.map +1 -1
  52. package/package.json +10 -5
  53. package/skip-prod-transpilation.ts +48 -0
  54. package/src/bcRead.ts +184 -63
  55. package/src/bcTransaction.ts +143 -114
  56. package/src/config/transactions.ts +30 -0
  57. package/src/connection.ts +42 -9
  58. package/src/customization.ts +17 -2
  59. package/src/ethereum/ethFuncs.ts +42 -1
  60. package/src/ethereum/etherUnits.ts +5 -5
  61. package/src/ethereum/uiFuncs.ts +16 -25
  62. package/src/exception.ts +16 -0
  63. package/src/index.ts +53 -27
  64. package/src/qr.ts +86 -12
  65. package/src/rest/ajaxReq.ts +18 -40
  66. package/src/utils.ts +300 -0
  67. package/src/wallet.ts +37 -48
  68. package/tests/environment.ts +27 -0
  69. package/tests/setup.ts +12 -0
  70. package/tsconfig.json +8 -2
  71. package/vitest.config.ts +14 -0
package/src/utils.ts ADDED
@@ -0,0 +1,300 @@
1
+ export class SplitError extends Error {
2
+ constructor (message) {
3
+ super(message)
4
+ this.name = 'SplitError'
5
+ }
6
+ }
7
+
8
+
9
+ export class InsufficientBalanceError extends SplitError {
10
+ missingAmount: number
11
+ constructor (message, missingAmount) {
12
+ super(message)
13
+ this.name = 'InsufficientBalanceError'
14
+ this.missingAmount = missingAmount
15
+ }
16
+ }
17
+
18
+ export class CmSpendLimitError extends SplitError {
19
+ missingAmount: number
20
+ constructor (message, missingAmount) {
21
+ super(message)
22
+ this.name = 'CmSpendLimitError'
23
+ this.missingAmount = missingAmount
24
+ }
25
+ }
26
+
27
+
28
+ function min(a, b) {
29
+ return a < b ? a : b
30
+ }
31
+
32
+
33
+ export let getSplitting = function (amount, bals, cmSrcMin, cmSpendMax = null) {
34
+
35
+ const zero = amount - amount // Required for agnosticity
36
+
37
+ const split = { nant: zero, cm: zero }
38
+ const cmPosSpendMax = cmSpendMax !== null ? min(bals.cm, cmSpendMax) : bals.cm
39
+
40
+ if (cmPosSpendMax >= 0) {
41
+ if (cmPosSpendMax >= amount) {
42
+ split.cm += amount
43
+ return split
44
+ }
45
+ split.cm += cmPosSpendMax
46
+ amount -= cmPosSpendMax
47
+ bals.cm -= cmPosSpendMax
48
+ if (cmSpendMax !== null) {
49
+ cmSpendMax -= cmPosSpendMax
50
+ }
51
+ }
52
+
53
+ if (bals.nant >= amount) {
54
+ split.nant += amount
55
+ return split
56
+ }
57
+
58
+ split.nant = bals.nant
59
+ amount -= bals.nant
60
+
61
+ let cmNegAvailable = bals.cm - cmSrcMin
62
+ const cmNegSpendMax = cmSpendMax !== null ? min(cmNegAvailable, cmSpendMax) : cmNegAvailable
63
+ if (cmNegSpendMax >= amount) {
64
+ split.cm += amount
65
+ return split
66
+ }
67
+
68
+ amount -= cmNegSpendMax
69
+ bals.cm -= cmNegSpendMax
70
+ if (cmSpendMax !== null) {
71
+ cmSpendMax -= cmNegSpendMax
72
+ }
73
+
74
+ cmNegAvailable = (bals.cm - cmSrcMin)
75
+ if (cmNegAvailable >= amount && cmSpendMax == 0) {
76
+ throw new CmSpendLimitError(
77
+ `Cm spend limit preventing to complete amount (remaining: ${amount}).`,
78
+ amount
79
+ )
80
+ }
81
+
82
+ throw new InsufficientBalanceError(
83
+ `Missing ${amount - cmNegAvailable} to complete amount`,
84
+ amount - cmNegAvailable
85
+ )
86
+ }
87
+
88
+
89
+ /* @skip-prod-transpilation */
90
+ if (import.meta.vitest) {
91
+ const { it, expect, describe, beforeAll, afterAll } = import.meta.vitest
92
+
93
+ function testSuite (numberFactory: any = null, label: any = null) {
94
+ let msg
95
+ if (numberFactory) {
96
+ msg = `check with ${label}`
97
+ } else {
98
+ msg = "check with standard number"
99
+ }
100
+ describe(msg, () => {
101
+
102
+ let getSplittingOrig = getSplitting
103
+ let toStrictEqual
104
+
105
+ beforeAll(() => {
106
+
107
+ if (numberFactory) {
108
+ getSplitting = function (amount, bals, cmSrcMin, cmSpendMax = null) {
109
+ amount = numberFactory(amount)
110
+ bals.cm = numberFactory(bals.cm)
111
+ bals.nant = numberFactory(bals.nant)
112
+ cmSrcMin = numberFactory(cmSrcMin)
113
+ if (cmSpendMax !== null) {
114
+ cmSpendMax = numberFactory(cmSpendMax)
115
+ }
116
+ return getSplittingOrig(amount, bals, cmSrcMin, cmSpendMax)
117
+ }
118
+ toStrictEqual = function (actual, expected) {
119
+ let { cm, nant } = expected
120
+ expect(actual).toStrictEqual({ cm: numberFactory(cm), nant: numberFactory(nant) })
121
+ }
122
+ } else {
123
+ toStrictEqual = function (actual, expected) {
124
+ expect(actual).toStrictEqual(expected)
125
+ }
126
+ }
127
+ })
128
+
129
+ afterAll(() => {
130
+ getSplitting = getSplittingOrig
131
+ })
132
+
133
+
134
+ describe('split without cmSpendMax', () => {
135
+ it('should split 0 to cm: 0, nant: 0', () => {
136
+ toStrictEqual(getSplitting(0, { cm: 0, nant: 0 }, 0), { nant: 0, cm: 0 })
137
+ });
138
+ it('should NOT split 1 with no funds', () => {
139
+ expect(() => getSplitting(1, { cm: 0, nant: 0 }, 0))
140
+ .toThrow("Missing 1 to complete amount")
141
+ });
142
+ // Only Cm
143
+ it('should split 1 to cm: 1, nant: 0 with bal cm: 2, nant: 0', () => {
144
+ toStrictEqual(getSplitting(1, {cm: 2, nant: 0}, 0), { nant: 0, cm: 1 })
145
+ });
146
+ it('should split 2 to cm: 2, nant: 0 with bal cm: 2, nant: 0', () => {
147
+ toStrictEqual(getSplitting(2, { cm: 2, nant: 0 }, 0), { nant: 0, cm: 2 })
148
+ });
149
+ it('should NOT split 3 with bal cm: 2, nant: 0', () => {
150
+ expect(() => getSplitting(3, { cm: 2, nant: 0 }, 0))
151
+ .toThrow("Missing 1 to complete amount")
152
+ });
153
+ // Only Nant
154
+ it('should split 1 to cm: 0, nant: 1 with bal cm: 0, nant: 2', () => {
155
+ toStrictEqual(getSplitting(1, { cm: 0, nant: 2 }, 0), { nant: 1, cm: 0 })
156
+ });
157
+ it('should split 2 to cm: 0, nant: 2 with bal cm: 0, nant: 2', () => {
158
+ toStrictEqual(getSplitting(2, { cm: 0, nant: 2 }, 0), { nant: 2, cm: 0 })
159
+ });
160
+ it('should NOT split 3 with bal cm: 0, nant: 2', () => {
161
+ expect(() => getSplitting(3, { cm: 0, nant: 2 }, 0))
162
+ .toThrow("Missing 1 to complete amount")
163
+ });
164
+ // Both Nant and Cm
165
+ it('should split 2 to cm: 1, nant: 1 with bal cm: 1, nant: 1', () => {
166
+ toStrictEqual(getSplitting(2, { cm: 1, nant: 1 }, 0), { nant: 1, cm: 1 })
167
+ });
168
+ it('should split 2 to cm: 0, nant: 2 with bal cm: 0, nant: 2 (limSrcCm: -2)', () => {
169
+ toStrictEqual(getSplitting(2, { cm: 0, nant: 2 }, -2), { nant: 2, cm: 0 })
170
+ });
171
+ it('should split 2 to cm: 1, nant: 1 with bal cm: 0, nant: 1 (limSrcCm: -1)', () => {
172
+ toStrictEqual(getSplitting(2, { cm: 0, nant: 1 }, -1), { nant: 1, cm: 1 })
173
+ });
174
+ it('should NOT split 3 with bal cm: 0, nant: 1 (limSrcCm: -1)', () => {
175
+ expect(() => getSplitting(3, { cm: 0, nant: 1 }, -1))
176
+ .toThrow("Missing 1 to complete amount")
177
+ });
178
+ it('should split 3 to cm: 2, nant: 1 with bal cm: 1, nant: 1 (limSrcCm: -1)', () => {
179
+ toStrictEqual(getSplitting(3, { cm: 1, nant: 1 }, -1), { nant: 1, cm: 2 })
180
+ });
181
+ // Negative cm bal and limSrcCm
182
+ it('should split 1 to cm: -1, nant: 0 with bal cm: -1, nant: 0 (limSrcCm: -2)', () => {
183
+ toStrictEqual(getSplitting(1, { cm: -1, nant: 0 }, -2), { nant: 0, cm: 1 })
184
+ });
185
+ it('should NOT split 2 with bal cm: -1, nant: 0 (limSrcCm: -2)', () => {
186
+ expect(() => getSplitting(2, { cm: -1, nant: 0 }, -2))
187
+ .toThrow("Missing 1 to complete amount")
188
+ });
189
+ });
190
+ describe('split with cmSpendMax', () => {
191
+ it('should split 0 to cm: 0, nant: 0 (cmSpendMax: 0)', () => {
192
+ toStrictEqual(getSplitting(0, { cm: 0, nant: 0 }, 0, 0), { nant: 0, cm: 0})
193
+ });
194
+ it('should split 0 to cm: 0, nant: 0 (cmSpendMax: 1)', () => {
195
+ toStrictEqual(getSplitting(0, { cm: 0, nant: 0 }, 0, 0), { nant: 0, cm: 0})
196
+ });
197
+ it('should NOT split 1 with no funds (cmSpendMax: 1)', () => {
198
+ expect(() => getSplitting(1, { cm: 0, nant: 0 }, 0, 1))
199
+ .toThrow("Missing 1 to complete amount")
200
+ });
201
+ it('should NOT split 1 with no funds (cmSpendMax: 0)', () => {
202
+ expect(() => getSplitting(1, { cm: 0, nant: 0 }, 0, 0))
203
+ .toThrow("Missing 1 to complete amount")
204
+ });
205
+ // Only Cm
206
+ it('should split 1 to cm: 1, nant: 0 with bal cm: 2, nant: 0 (cmSpendMax: 1)', () => {
207
+ toStrictEqual(getSplitting(1, {cm: 2, nant: 0}, 0, 1), { nant: 0, cm: 1 })
208
+ });
209
+ it('should split 2 to cm: 2, nant: 0 with bal cm: 2, nant: 0 (cmSpendMax: 2)', () => {
210
+ toStrictEqual(getSplitting(2, { cm: 2, nant: 0 }, 0, 2), { nant: 0, cm: 2 })
211
+ });
212
+ it('should NOT split 2 to cm: 2, nant: 0 with bal cm: 2, nant: 0 (cmSpendMax: 1)', () => {
213
+ expect(() => getSplitting(2, { cm: 2, nant: 0 }, 0, 1))
214
+ .toThrow("Cm spend limit preventing to complete amount (remaining: 1)")
215
+ });
216
+ it('should NOT split 3 with bal cm: 2, nant: 0 (cmSpendMax: 5)', () => {
217
+ expect(() => getSplitting(3, { cm: 2, nant: 0 }, 0, 5))
218
+ .toThrow("Missing 1 to complete amount")
219
+ });
220
+ it('should NOT split 3 with bal cm: 2, nant: 0 (cmSpendMax: 2)', () => {
221
+ expect(() => getSplitting(3, { cm: 2, nant: 0 }, 0, 2))
222
+ .toThrow("Missing 1 to complete amount")
223
+ });
224
+ it('should NOT split 3 with bal cm: 2, nant: 0 (cmSpendMax: 1)', () => {
225
+ expect(() => getSplitting(3, { cm: 2, nant: 0 }, 0, 1))
226
+ .toThrow("Missing 1 to complete amount")
227
+ });
228
+ // Only Nant
229
+ it('should split 1 to cm: 0, nant: 1 with bal cm: 0, nant: 2 (cmSpendMax: 0)', () => {
230
+ toStrictEqual(getSplitting(1, { cm: 0, nant: 2 }, 0, 0), { nant: 1, cm: 0 })
231
+ });
232
+ it('should split 2 to cm: 0, nant: 2 with bal cm: 0, nant: 2 (cmSpendMax: 1)', () => {
233
+ toStrictEqual(getSplitting(2, { cm: 0, nant: 2 }, 0, 1), { nant: 2, cm: 0 })
234
+ });
235
+ it('should NOT split 3 with bal cm: 0, nant: 2 (cmSpendMax: 0)', () => {
236
+ expect(() => getSplitting(3, { cm: 0, nant: 2 }, 0, 0))
237
+ .toThrow("Missing 1 to complete amount")
238
+ });
239
+ // Both Nant and Cm
240
+ it('should split 2 to cm: 1, nant: 1 with bal cm: 1, nant: 1 (cmSpendMax: 1)', () => {
241
+ toStrictEqual(getSplitting(2, { cm: 1, nant: 1 }, 0, 1), { nant: 1, cm: 1})
242
+ });
243
+ it('should NOT split 2 to cm: 1, nant: 1 with bal cm: 1, nant: 1 (cmSpendMax: 0)', () => {
244
+ expect(() => getSplitting(2, { cm: 1, nant: 1 }, 0, 0))
245
+ .toThrow("Cm spend limit preventing to complete amount (remaining: 1)")
246
+ });
247
+ it('should split 2 to cm: 1, nant: 1 with bal cm: 0, nant: 1 (limSrcCm: -1, cmSpendMax: 1)', () => {
248
+ toStrictEqual(getSplitting(2, { cm: 0, nant: 1 }, -1, 1), { nant: 1, cm: 1})
249
+ });
250
+ it('should NOT split 2 to cm: 1, nant: 1 with bal cm: 0, nant: 1 (limSrcCm: -1, cmSpendMax: 0)', () => {
251
+ expect(() => getSplitting(2, { cm: 0, nant: 1 }, -1, 0))
252
+ .toThrow("Cm spend limit preventing to complete amount (remaining: 1)")
253
+ });
254
+ it('should NOT split 3 with bal cm: 0, nant: 1 (limSrcCm: -1, cmSpendMax: 5)', () => {
255
+ expect(() => getSplitting(3, { cm: 0, nant: 1 }, -1, 5))
256
+ .toThrow("Missing 1 to complete amount")
257
+ });
258
+ it('should NOT split 3 with bal cm: 0, nant: 1 (limSrcCm: -1, cmSpendMax: 0)', () => {
259
+ expect(() => getSplitting(3, { cm: 0, nant: 1 }, -1, 0))
260
+ .toThrow("Missing 1 to complete amount")
261
+ });
262
+ it('should split 3 to cm: 2, nant: 1 with bal cm: 1, nant: 1 (limSrcCm: -1, cmSpendMax: 2)', () => {
263
+ toStrictEqual(getSplitting(3, { cm: 1, nant: 1 }, -1, 2), { nant: 1, cm: 2 })
264
+ });
265
+ it('should NOT split 3 to cm: 2, nant: 1 with bal cm: 1, nant: 1 (limSrcCm: -1, cmSpendMax: 1)', () => {
266
+ expect(() => getSplitting(3, { cm: 1, nant: 1 }, -1, 1))
267
+ .toThrow("Cm spend limit preventing to complete amount (remaining: 1)")
268
+ });
269
+ // If the recipient can't receive cm, then pay with nant if possible.
270
+ it('should split 2 to cm: 0, nant: 2 with bal cm: 2, nant: 2 (limSrcCm: 0, cmSpendMax: 0)', () => {
271
+ toStrictEqual(getSplitting(2, { cm: 2, nant: 2 }, 0, 0), { nant: 2, cm: 0 })
272
+ });
273
+ it('should split 3 to cm: 1, nant: 2 with bal cm: 2, nant: 2 (limSrcCm: 0, cmSpendMax: 1)', () => {
274
+ toStrictEqual(getSplitting(3, { cm: 2, nant: 2 }, 0, 1), { nant: 2, cm: 1 })
275
+ });
276
+ it('should split 3 to cm: 2, nant: 2 with bal cm: 2, nant: 2 (limSrcCm: 0, cmSpendMax: 2)', () => {
277
+ toStrictEqual(getSplitting(3, { cm: 2, nant: 2 }, 0, 2), { nant: 1, cm: 2 })
278
+ });
279
+ // Negative cm bal and limSrcCm
280
+ it('should split 1 to cm: -1, nant: 0 with bal cm: -1, nant: 0 (limSrcCm: -2, cmSpendMax: 1)', () => {
281
+ toStrictEqual(getSplitting(1, { cm: -1, nant: 0 }, -2, 1), { nant: 0, cm: 1 })
282
+ });
283
+ it('should NOT split 2 with bal cm: -1, nant: 0 (limSrcCm: -2, cmSpendMax: 2)', () => {
284
+ expect(() => getSplitting(2, { cm: -1, nant: 0 }, -2, 2))
285
+ .toThrow("Missing 1 to complete amount")
286
+ });
287
+ it('should NOT split 2 with bal cm: -1, nant: 0 (limSrcCm: -2, cmSpendMax: 0)', () => {
288
+ expect(() => getSplitting(2, { cm: -1, nant: 0 }, -2, 2))
289
+ .toThrow("Missing 1 to complete amount")
290
+ });
291
+ it('should NOT split 2 with bal cm: -1, nant: 0 (limSrcCm: -3, cmSpendMax: 1)', () => {
292
+ expect(() => getSplitting(2, { cm: -1, nant: 0 }, -3, 1))
293
+ .toThrow("Cm spend limit preventing to complete amount (remaining: 1)")
294
+ });
295
+ })
296
+ })
297
+ }
298
+ testSuite()
299
+ testSuite(BigInt, "BigInt")
300
+ }
package/src/wallet.ts CHANGED
@@ -1,6 +1,7 @@
1
- import ethUtil from 'ethereumjs-util'
2
-
3
1
  import AjaxReq from './rest/ajaxReq'
2
+ import {
3
+ checkSignedQR, SignedQR, makeSignedQRContent, makeSignedQRFragments
4
+ } from './qr'
4
5
  import { shortenAddress, cipherMsg, decipherMsg } from './ethereum/cipher'
5
6
  import Wallet from './ethereum/myetherwallet'
6
7
 
@@ -95,7 +96,7 @@ export default abstract class MessagingWalletAbstract extends Wallet {
95
96
  const newKey = Wallet.generate(false)
96
97
  const mPub = newKey.getPublicKeyString()
97
98
  const mPriv = newKey.getPrivateKeyString()
98
- return { pub: mPub, priv: cipherMsg(mPub, mPriv) }
99
+ return { pub: mPub, priv: cipherMsg(this.getPublicKeyString(), mPriv) }
99
100
  }
100
101
 
101
102
  public messageKeysFromCrypted (cipheredKey) {
@@ -121,10 +122,14 @@ export default abstract class MessagingWalletAbstract extends Wallet {
121
122
  })
122
123
  }
123
124
 
124
- public enrollAddress (codeId, token) {
125
- return this.ajaxReq.enrollAddress(
126
- codeId, this.getAddressString(),
127
- this.currencyName, token)
125
+ public enrollAddress (id, token) {
126
+ return this.ajaxReq.enrollPost({
127
+ id,
128
+ token,
129
+ currency: this.currencyName,
130
+ // XXXvlab: Yes, typo is intentional here (in PHP API):
131
+ adresse: this.getAddressString(),
132
+ })
128
133
  }
129
134
 
130
135
  public requestUnlock () {
@@ -148,54 +153,38 @@ export default abstract class MessagingWalletAbstract extends Wallet {
148
153
  }
149
154
  }
150
155
 
151
-
152
156
  //
153
- // QR Codes requires EthUtils
157
+ // QR Code helpers
154
158
  //
155
159
 
156
- public makeSignedQRWithPubKey (objContent, pubKey) {
157
- objContent.message_key = cipherMsg(
158
- pubKey, this.messageKeysFromWallet()
159
- )
160
- objContent.address = this.getAddressString()
161
- return this.makeSignedQR(objContent)
160
+ public makeSignedQRContent (obj, pubKey: string | null) {
161
+ return makeSignedQRContent({
162
+ server: this.currencyName,
163
+ address: this.getAddressString(),
164
+ ...obj,
165
+ ...pubKey && {
166
+ message_key: cipherMsg(pubKey, this.messageKeysFromWallet())
167
+ },
168
+ }, this.privKey)
162
169
  }
163
170
 
164
- public makeSignedQR (obj) {
165
-
166
- // Values expected:
167
- const {
168
- server, destinary, begin, end,
169
- viewbalance, viewoldtran, pub_key
170
- } = obj
171
- const formatDate = (date) =>
172
- `${begin.getFullYear()}/${begin.getMonth()}/${begin.getDate()}`
173
- const objContent = Object.assign(obj, {
174
- address: this.getAddressString(),
175
- begin: formatDate(begin),
176
- end: formatDate(end)
177
- })
171
+ public makeSignedQRFragments (
172
+ obj, fragmentCount: number, pubKey: string | null
173
+ ) {
174
+ return makeSignedQRFragments(
175
+ this.makeSignedQRContent(obj, pubKey),
176
+ fragmentCount,
177
+ )
178
+ }
178
179
 
179
- const hash = ethUtil.sha3(JSON.stringify(objContent))
180
- const signature = ethUtil.ecsign(hash, this.privKey)
181
- return {
182
- signature,
183
- qrContent: JSON.stringify({
184
- data: objContent,
185
- signature: {
186
- v: signature.v,
187
- r: '0x' + signature.r.toString('hex'),
188
- s: '0x' + signature.s.toString('hex')
189
- }
190
- })
180
+ public checkSignedQRFromString (qrString: string) {
181
+ let qrContent: SignedQR
182
+ try {
183
+ qrContent = JSON.parse(qrString)
184
+ } catch (e) {
185
+ return 'InvalidFormat'
191
186
  }
187
+ return checkSignedQR(qrContent, this.getAddressString())
192
188
  }
193
189
 
194
190
  }
195
-
196
- // // TODO: What to do with this validateEnrollment
197
- // public static validateEnrollment (codeId, signature) {
198
- // return this.ajaxReq.validateEnrollmentLetter(
199
- // codeId, this.currencyName, signature)
200
- // }
201
-
@@ -0,0 +1,27 @@
1
+ import type { Environment } from 'vitest'
2
+
3
+ export default <Environment>{
4
+ name: 'custom',
5
+ transformMode: 'ssr',
6
+ // optional - only if you support "experimental-vm" pool
7
+ async setupVM() {
8
+ const vm = await import('node:vm')
9
+ const context = vm.createContext()
10
+ return {
11
+ getVmContext() {
12
+ return context
13
+ },
14
+ teardown() {
15
+ // called after all tests with this env have been run
16
+ }
17
+ }
18
+ },
19
+ setup() {
20
+ // custom setup
21
+ return {
22
+ teardown() {
23
+ // called after all tests with this env have been run
24
+ }
25
+ }
26
+ }
27
+ }
package/tests/setup.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Buffer } from 'buffer'
2
+ import { afterAll, beforeAll } from 'vitest';
3
+
4
+ beforeAll(() => {
5
+ global.Buffer = Buffer
6
+ });
7
+
8
+ afterAll(() => {
9
+ delete global.Buffer
10
+ });
11
+
12
+
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2017",
3
+ "target": "es2020",
4
4
  "module": "esnext",
5
5
  "moduleResolution": "node",
6
6
  "declaration": true,
@@ -24,7 +24,13 @@
24
24
  "outDir": "build",
25
25
  "paths": {
26
26
  "*": ["src/types/*"]
27
- }
27
+ },
28
+ "types": [
29
+ "vitest/importMeta"
30
+ ],
31
+ "plugins": [
32
+ { "transform": "./skip-prod-transpilation.ts" }
33
+ ]
28
34
  },
29
35
  "include": ["src/**/*", "references.d.ts"],
30
36
  "exclude": ["node_modules", "platforms", "**/angular"]
@@ -0,0 +1,14 @@
1
+ /// <reference types="vitest" />
2
+ import { defineConfig } from 'vite'
3
+
4
+ const cfg = defineConfig({
5
+ test: {
6
+ include: ['src/**/*.{js,ts}'],
7
+ setupFiles: ['./tests/setup.ts'],
8
+ environment: 'jsdom',
9
+ passWithNoTests: true,
10
+ bail: 1,
11
+ },
12
+ })
13
+
14
+ export default cfg