@bsv/sdk 1.0.0 → 1.0.1

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 (123) hide show
  1. package/README.md +25 -3
  2. package/package.json +9 -5
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  4. package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
  5. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
  6. package/CHANGELOG.md +0 -72
  7. package/CONTRIBUTING.md +0 -85
  8. package/ROADMAP.md +0 -3
  9. package/docs/getting-started/COMMONJS.md +0 -94
  10. package/docs/getting-started/REACT-TS.md +0 -131
  11. package/docs/getting-started/TS-NODE.md +0 -106
  12. package/docs/getting-started/VUE.md +0 -103
  13. package/jest.config.js +0 -6
  14. package/mod.ts +0 -8
  15. package/src/compat/BSM.ts +0 -51
  16. package/src/compat/ECIES.ts +0 -557
  17. package/src/compat/HD.ts +0 -348
  18. package/src/compat/Mnemonic.ts +0 -295
  19. package/src/compat/__tests/BSM.test.ts +0 -38
  20. package/src/compat/__tests/ECIES.test.ts +0 -90
  21. package/src/compat/__tests/HD.test.ts +0 -405
  22. package/src/compat/__tests/Mnemonic.test.ts +0 -177
  23. package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
  24. package/src/compat/bip-39-wordlist-en.ts +0 -2053
  25. package/src/compat/index.ts +0 -4
  26. package/src/messages/EncryptedMessage.ts +0 -70
  27. package/src/messages/SignedMessage.ts +0 -87
  28. package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
  29. package/src/messages/__tests/SignedMessage.test.ts +0 -53
  30. package/src/messages/index.ts +0 -2
  31. package/src/primitives/AESGCM.ts +0 -479
  32. package/src/primitives/BasePoint.ts +0 -21
  33. package/src/primitives/BigNumber.ts +0 -4619
  34. package/src/primitives/Curve.ts +0 -1163
  35. package/src/primitives/DRBG.ts +0 -102
  36. package/src/primitives/ECDSA.ts +0 -164
  37. package/src/primitives/Hash.ts +0 -1420
  38. package/src/primitives/JacobianPoint.ts +0 -410
  39. package/src/primitives/K256.ts +0 -116
  40. package/src/primitives/Mersenne.ts +0 -123
  41. package/src/primitives/MontgomoryMethod.ts +0 -160
  42. package/src/primitives/Point.ts +0 -852
  43. package/src/primitives/PrivateKey.ts +0 -195
  44. package/src/primitives/PublicKey.ts +0 -154
  45. package/src/primitives/Random.ts +0 -55
  46. package/src/primitives/ReductionContext.ts +0 -528
  47. package/src/primitives/Signature.ts +0 -235
  48. package/src/primitives/SymmetricKey.ts +0 -75
  49. package/src/primitives/TransactionSignature.ts +0 -189
  50. package/src/primitives/__tests/AESGCM.test.ts +0 -338
  51. package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
  52. package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
  53. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
  54. package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
  55. package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
  56. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
  57. package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
  58. package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
  59. package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
  60. package/src/primitives/__tests/Curve.unit.test.ts +0 -192
  61. package/src/primitives/__tests/DRBG.test.ts +0 -18
  62. package/src/primitives/__tests/DRBG.vectors.ts +0 -167
  63. package/src/primitives/__tests/ECDH.test.ts +0 -31
  64. package/src/primitives/__tests/ECDSA.test.ts +0 -58
  65. package/src/primitives/__tests/HMAC.test.ts +0 -59
  66. package/src/primitives/__tests/Hash.test.ts +0 -121
  67. package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
  68. package/src/primitives/__tests/PrivateKey.test.ts +0 -17
  69. package/src/primitives/__tests/PublicKey.test.ts +0 -66
  70. package/src/primitives/__tests/Random.test.ts +0 -14
  71. package/src/primitives/__tests/Reader.test.ts +0 -296
  72. package/src/primitives/__tests/ReductionContext.test.ts +0 -279
  73. package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
  74. package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
  75. package/src/primitives/__tests/Writer.test.ts +0 -198
  76. package/src/primitives/__tests/sighash.vectors.ts +0 -3503
  77. package/src/primitives/__tests/utils.test.ts +0 -108
  78. package/src/primitives/index.ts +0 -8
  79. package/src/primitives/utils.ts +0 -665
  80. package/src/script/LockingScript.ts +0 -30
  81. package/src/script/OP.ts +0 -219
  82. package/src/script/Script.ts +0 -426
  83. package/src/script/ScriptChunk.ts +0 -7
  84. package/src/script/ScriptTemplate.ts +0 -36
  85. package/src/script/Spend.ts +0 -1379
  86. package/src/script/UnlockingScript.ts +0 -30
  87. package/src/script/__tests/Script.test.ts +0 -369
  88. package/src/script/__tests/Spend.test.ts +0 -248
  89. package/src/script/__tests/script.invalid.vectors.ts +0 -925
  90. package/src/script/__tests/script.valid.vectors.ts +0 -1120
  91. package/src/script/__tests/scriptFromVector.ts +0 -42
  92. package/src/script/__tests/spend.valid.vectors.ts +0 -2288
  93. package/src/script/index.ts +0 -7
  94. package/src/script/templates/P2PKH.ts +0 -109
  95. package/src/script/templates/RPuzzle.ts +0 -140
  96. package/src/script/templates/index.ts +0 -2
  97. package/src/transaction/Broadcaster.ts +0 -42
  98. package/src/transaction/ChainTracker.ts +0 -22
  99. package/src/transaction/FeeModel.ts +0 -13
  100. package/src/transaction/MerklePath.ts +0 -259
  101. package/src/transaction/Transaction.ts +0 -602
  102. package/src/transaction/TransactionInput.ts +0 -63
  103. package/src/transaction/TransactionOutput.ts +0 -37
  104. package/src/transaction/__tests/MerklePath.test.ts +0 -181
  105. package/src/transaction/__tests/Transaction.test.ts +0 -413
  106. package/src/transaction/__tests/bigtx.vectors.ts +0 -4
  107. package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
  108. package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
  109. package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
  110. package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
  111. package/src/transaction/broadcasters/ARC.ts +0 -106
  112. package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
  113. package/src/transaction/broadcasters/index.ts +0 -1
  114. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
  115. package/src/transaction/fee-models/index.ts +0 -1
  116. package/src/transaction/index.ts +0 -6
  117. package/ts2md.json +0 -5
  118. package/tsconfig.base.json +0 -26
  119. package/tsconfig.cjs.json +0 -11
  120. package/tsconfig.eslint.json +0 -12
  121. package/tsconfig.esm.json +0 -9
  122. package/tsconfig.json +0 -17
  123. package/tsconfig.types.json +0 -11
@@ -1,279 +0,0 @@
1
- /* eslint-env jest */
2
- import BigNumber from '../../../dist/cjs/src/primitives/BigNumber'
3
- import ReductionContext from '../../../dist/cjs/src/primitives/ReductionContext'
4
- import MontgomoryMethod from '../../../dist/cjs/src/primitives/MontgomoryMethod'
5
- import K256 from '../../../dist/cjs/src/primitives/K256'
6
-
7
- describe('BN.js/Reduction context', function () {
8
- const testMethod = (name: string, Method): void => {
9
- describe(name + ' method', function () {
10
- it('should support add, iadd, sub, isub operations', () => {
11
- const p = new BigNumber(257)
12
- const m = new Method(p)
13
- const a = new BigNumber(123).toRed(m)
14
- const b = new BigNumber(231).toRed(m)
15
-
16
- expect(a.redAdd(b).fromRed().toString(10)).toEqual('97')
17
- expect(a.redSub(b).fromRed().toString(10)).toEqual('149')
18
- expect(b.redSub(a).fromRed().toString(10)).toEqual('108')
19
-
20
- expect(a.clone().redIAdd(b).fromRed().toString(10)).toEqual('97')
21
- expect(a.clone().redISub(b).fromRed().toString(10)).toEqual('149')
22
- expect(b.clone().redISub(a).fromRed().toString(10)).toEqual('108')
23
- })
24
-
25
- it('should support pow and mul operations', () => {
26
- const p192 = new BigNumber(
27
- 'fffffffffffffffffffffffffffffffeffffffffffffffff',
28
- 16)
29
- const m = new Method(p192)
30
- const a = new BigNumber(123)
31
- const b = new BigNumber(231)
32
- const c = a.toRed(m).redMul(b.toRed(m)).fromRed()
33
- expect(c.cmp(a.mul(b).mod(p192))).toEqual(0)
34
-
35
- expect(a.toRed(m).redPow(new BigNumber(0)).fromRed()
36
- .cmp(new BigNumber(1))).toEqual(0)
37
- expect(a.toRed(m).redPow(new BigNumber(3)).fromRed()
38
- .cmp(a.sqr().mul(a))).toEqual(0)
39
- expect(a.toRed(m).redPow(new BigNumber(4)).fromRed()
40
- .cmp(a.sqr().sqr())).toEqual(0)
41
- expect(a.toRed(m).redPow(new BigNumber(8)).fromRed()
42
- .cmp(a.sqr().sqr().sqr())).toEqual(0)
43
- expect(a.toRed(m).redPow(new BigNumber(9)).fromRed()
44
- .cmp(a.sqr().sqr().sqr().mul(a))).toEqual(0)
45
- expect(a.toRed(m).redPow(new BigNumber(17)).fromRed()
46
- .cmp(a.sqr().sqr().sqr().sqr().mul(a))).toEqual(0)
47
- expect(
48
- a.toRed(m).redPow(new BigNumber('deadbeefabbadead', 16)).fromRed()
49
- .toString(16)).toEqual('3aa0e7e304e320b68ef61592bcb00341866d6fa66e11a4d6')
50
- })
51
-
52
- it('should sqrtm numbers', () => {
53
- let p = new BigNumber(263)
54
- let m = new Method(p)
55
- let q = new BigNumber(11).toRed(m)
56
-
57
- let qr = q.redSqrt()
58
- expect(qr.redSqr().cmp(q)).toEqual(0)
59
-
60
- qr = q.redSqrt()
61
- expect(qr.redSqr().cmp(q)).toEqual(0)
62
-
63
- p = new BigNumber(
64
- 'fffffffffffffffffffffffffffffffeffffffffffffffff',
65
- 16)
66
- m = new Method(p)
67
-
68
- q = new BigNumber(13).toRed(m)
69
- qr = q.redSqrt(/* true, p */)
70
- expect(qr.redSqr().cmp(q)).toEqual(0)
71
-
72
- qr = q.redSqrt(/* false, p */)
73
- expect(qr.redSqr().cmp(q)).toEqual(0)
74
-
75
- // Tonelli-shanks
76
- p = new BigNumber(13)
77
- m = new Method(p)
78
- q = new BigNumber(10).toRed(m)
79
- expect(q.redSqrt().fromRed().toString(10)).toEqual('7')
80
- })
81
-
82
- it('should invm numbers', function () {
83
- const p = new BigNumber(257)
84
- const m = new Method(p)
85
- const a = new BigNumber(3).toRed(m)
86
- const b = a.redInvm()
87
- expect(a.redMul(b).fromRed().toString(16)).toEqual('1')
88
- })
89
-
90
- it('should invm numbers (regression)', function () {
91
- const p = new BigNumber(
92
- 'ffffffff00000001000000000000000000000000ffffffffffffffffffffffff',
93
- 16)
94
- let a = new BigNumber(
95
- 'e1d969b8192fbac73ea5b7921896d6a2263d4d4077bb8e5055361d1f7f8163f3',
96
- 16)
97
-
98
- const m = new Method(p)
99
- a = a.toRed(m)
100
-
101
- expect(a.redInvm().fromRed().negative).toEqual(0)
102
- })
103
-
104
- it('should imul numbers', function () {
105
- const p = new BigNumber(
106
- 'fffffffffffffffffffffffffffffffeffffffffffffffff',
107
- 16)
108
- const m = new Method(p)
109
-
110
- const a = new BigNumber('deadbeefabbadead', 16)
111
- const b = new BigNumber('abbadeadbeefdead', 16)
112
- const c = a.mul(b).mod(p)
113
-
114
- expect(a.toRed(m).redIMul(b.toRed(m)).fromRed().toString(16))
115
- .toEqual(c.toString(16))
116
- })
117
-
118
- it('should pow(base, 0) == 1', function () {
119
- const base = new BigNumber(256).toRed(new ReductionContext('k256'))
120
- const exponent = new BigNumber(0)
121
- const result = base.redPow(exponent)
122
- expect(result.toString()).toEqual('1')
123
- })
124
-
125
- it('should shl numbers', function () {
126
- const base = new BigNumber(256).toRed(new ReductionContext('k256'))
127
- const result = base.redShl(1)
128
- expect(result.toString()).toEqual('512')
129
- })
130
-
131
- it('should reduce when converting to red', function () {
132
- const p = new BigNumber(257)
133
- const m = new Method(p)
134
- const a = new BigNumber(5).toRed(m)
135
-
136
- expect(() => {
137
- const b = a.redISub(new BigNumber(512).toRed(m))
138
- b.redISub(new BigNumber(512).toRed(m))
139
- }).not.toThrow()
140
- })
141
-
142
- it('redNeg and zero value', function () {
143
- const a = new BigNumber(0).toRed(new ReductionContext('k256')).redNeg()
144
- expect(a.isZero()).toEqual(true)
145
- })
146
-
147
- it('should not allow modulus <= 1', function () {
148
- expect(() => {
149
- return new ReductionContext(new BigNumber(0))
150
- }).toThrow(new Error('modulus must be greater than 1'))
151
-
152
- expect(() => {
153
- return new ReductionContext(new BigNumber(1))
154
- }).toThrow(new Error('modulus must be greater than 1'))
155
-
156
- expect(() => {
157
- return new ReductionContext(new BigNumber(2))
158
- }).not.toThrow()
159
- })
160
- })
161
- }
162
-
163
- testMethod('Plain', ReductionContext)
164
- testMethod('Montgomery', MontgomoryMethod)
165
-
166
- describe('Pseudo-Mersenne Primes', function () {
167
- it('should reduce numbers mod k256', function () {
168
- const p = new K256()
169
-
170
- expect(p.ireduce(new BigNumber(0xdead)).toString(16)).toEqual('dead')
171
- expect(p.ireduce(new BigNumber('deadbeef', 16)).toString(16)).toEqual('deadbeef')
172
-
173
- const num = new BigNumber(
174
- 'fedcba9876543210fedcba9876543210dead' +
175
- 'fedcba9876543210fedcba9876543210dead',
176
- 16)
177
- let exp = num.mod(p.p).toString(16)
178
- expect(p.ireduce(num).toString(16)).toEqual(exp)
179
-
180
- const regr = new BigNumber(
181
- 'f7e46df64c1815962bf7bc9c56128798' +
182
- '3f4fcef9cb1979573163b477eab93959' +
183
- '335dfb29ef07a4d835d22aa3b6797760' +
184
- '70a8b8f59ba73d56d01a79af9',
185
- 16)
186
- exp = regr.mod(p.p).toString(16)
187
-
188
- expect(p.ireduce(regr).toString(16)).toEqual(exp)
189
- })
190
-
191
- it('should not fail to invm number mod k256', function () {
192
- let regr2 = new BigNumber(
193
- '6c150c4aa9a8cf1934485d40674d4a7cd494675537bda36d49405c5d2c6f496f', 16)
194
- regr2 = regr2.toRed(new ReductionContext('k256'))
195
- expect(regr2.redInvm().redMul(regr2).fromRed().cmpn(1)).toEqual(0)
196
- })
197
-
198
- it('should correctly square the number', function () {
199
- const p = new K256().p
200
- const red = new ReductionContext('k256')
201
-
202
- const n = new BigNumber(
203
- '9cd8cb48c3281596139f147c1364a3ed' +
204
- 'e88d3f310fdb0eb98c924e599ca1b3c9',
205
- 16)
206
- const expected = n.sqr().mod(p)
207
- const actual = n.toRed(red).redSqr().fromRed()
208
-
209
- expect(actual.toString(16)).toEqual(expected.toString(16))
210
- })
211
-
212
- it('redISqr should return right result', function () {
213
- const n = new BigNumber('30f28939', 16)
214
- const actual = n.toRed(new ReductionContext('k256')).redISqr().fromRed()
215
- expect(actual.toString(16)).toEqual('95bd93d19520eb1')
216
- })
217
- })
218
-
219
- it('should avoid 4.1.0 regresion', function () {
220
- const bits2int = (obits, q): BigNumber => {
221
- const bits = new BigNumber(obits)
222
- const shift = (obits.length << 3) - q.bitLength()
223
- if (shift > 0) {
224
- bits.ishrn(shift)
225
- }
226
- return bits
227
- }
228
- const t = Buffer.from('aff1651e4cd6036d57aa8b2a05ccf1a9d5a40166340ecbbdc55' +
229
- 'be10b568aa0aa3d05ce9a2fcec9df8ed018e29683c6051cb83e' +
230
- '46ce31ba4edb045356a8d0d80b', 'hex')
231
- const g = new BigNumber(
232
- '5c7ff6b06f8f143fe8288433493e4769c4d988ace5be25a0e24809670' +
233
- '716c613d7b0cee6932f8faa7c44d2cb24523da53fbe4f6ec3595892d1' +
234
- 'aa58c4328a06c46a15662e7eaa703a1decf8bbb2d05dbe2eb956c142a' +
235
- '338661d10461c0d135472085057f3494309ffa73c611f78b32adbb574' +
236
- '0c361c9f35be90997db2014e2ef5aa61782f52abeb8bd6432c4dd097b' +
237
- 'c5423b285dafb60dc364e8161f4a2a35aca3a10b1c4d203cc76a470a3' +
238
- '3afdcbdd92959859abd8b56e1725252d78eac66e71ba9ae3f1dd24871' +
239
- '99874393cd4d832186800654760e1e34c09e4d155179f9ec0dc4473f9' +
240
- '96bdce6eed1cabed8b6f116f7ad9cf505df0f998e34ab27514b0ffe7',
241
- 16)
242
- const p = new BigNumber(
243
- '9db6fb5951b66bb6fe1e140f1d2ce5502374161fd6538df1648218642' +
244
- 'f0b5c48c8f7a41aadfa187324b87674fa1822b00f1ecf8136943d7c55' +
245
- '757264e5a1a44ffe012e9936e00c1d3e9310b01c7d179805d3058b2a9' +
246
- 'f4bb6f9716bfe6117c6b5b3cc4d9be341104ad4a80ad6c94e005f4b99' +
247
- '3e14f091eb51743bf33050c38de235567e1b34c3d6a5c0ceaa1a0f368' +
248
- '213c3d19843d0b4b09dcb9fc72d39c8de41f1bf14d4bb4563ca283716' +
249
- '21cad3324b6a2d392145bebfac748805236f5ca2fe92b871cd8f9c36d' +
250
- '3292b5509ca8caa77a2adfc7bfd77dda6f71125a7456fea153e433256' +
251
- 'a2261c6a06ed3693797e7995fad5aabbcfbe3eda2741e375404ae25b',
252
- 16)
253
- const q = new BigNumber('f2c3119374ce76c9356990b465374a17f23f9ed35089bd969f61c6dde' +
254
- '9998c1f', 16)
255
- const k = bits2int(t, q)
256
- const expectedR = '89ec4bb1400eccff8e7d9aa515cd1de7803f2daff09693ee7fd1353e' +
257
- '90a68307'
258
- const r = g.toRed(new MontgomoryMethod(p)).redPow(k).fromRed().mod(q)
259
- expect(r.toString(16)).toEqual(expectedR)
260
- })
261
-
262
- it('K256.split for 512 bits number should return equal numbers', function () {
263
- const red = new ReductionContext('k256')
264
- const input = new BigNumber(1).iushln(512).subn(1)
265
- expect(input.bitLength()).toEqual(512)
266
- const output = new BigNumber(0)
267
- red.prime?.split(input, output)
268
- expect(input.cmp(output)).toEqual(0)
269
- })
270
-
271
- it('imod should change host object', function () {
272
- const red = new ReductionContext(new BigNumber(13))
273
- const a = new BigNumber(2).toRed(red)
274
- const b = new BigNumber(7).toRed(red)
275
- const c = a.redIMul(b)
276
- expect(a.toNumber()).toEqual(1)
277
- expect(c.toNumber()).toEqual(1)
278
- })
279
- })
@@ -1,58 +0,0 @@
1
- import SymmetricKey from '../../../dist/cjs/src/primitives/SymmetricKey'
2
- import vectors from './SymmetricKey.vectors'
3
-
4
- const KEYS: SymmetricKey[] = [
5
- new SymmetricKey('5a90d59d829197983a54d887fdea2dc4c38098f00ba3110f2645633b6ea11458', 16),
6
- new SymmetricKey('bac6ac492f54d7c997fadc1be593a4ace26ecdf37d30b3ad12f34077fb2629e4', 16),
7
- new SymmetricKey('53dcdc6ea6a6910af35a48708f49228e0e6661ea885435080cbabc58e6a14f10', 16)
8
- ]
9
-
10
- const PLAINTEXT_1 = 'hello there'
11
- const CIPHERTEXT_1 = '8c8d25348dfd5240be833215a123173c64919779ab8845a700a4520311504c168ade2d4b728cc53a254f0aba857caaf6af97453ac2ff61487d0d52'
12
-
13
- describe('SymmetricKey', () => {
14
- it('Produces output that can be decrypted', () => {
15
- const originalValue = 'a thing to encrypt'
16
- const encryptedValue = KEYS[2].encrypt(originalValue)
17
- const decryptedValue = KEYS[2].decrypt(encryptedValue, 'utf8')
18
- expect(originalValue).toEqual(decryptedValue)
19
- })
20
- it('Encrypts values as an array', () => {
21
- const originalValue = [42, 99, 33, 0, 1]
22
- const encryptedValue = KEYS[2].encrypt(
23
- originalValue
24
- )
25
- const decryptedValue = KEYS[2].decrypt(
26
- encryptedValue
27
- )
28
- expect(originalValue).toEqual(decryptedValue)
29
- })
30
- it('Decrypts a correctly-encrypted value', () => {
31
- const result = KEYS[0].decrypt(CIPHERTEXT_1, 'hex') as string
32
- expect(Buffer.from(result, 'hex').toString('utf8')).toEqual(PLAINTEXT_1)
33
- })
34
- it('Throws a useful error when decryption fails', () => {
35
- expect(() => {
36
- KEYS[2].decrypt(
37
- CIPHERTEXT_1,
38
- 'hex'
39
- )
40
- }).toThrow(new Error('Decryption failed!'))
41
- })
42
- it('decrypts values encrypted with the encrypt function', () => {
43
- const originalValue = 'secret value'
44
- const encryptedValue = KEYS[1].encrypt(originalValue)
45
- const decryptedValue = KEYS[1].decrypt(encryptedValue, 'utf8')
46
- expect(originalValue).toEqual(decryptedValue)
47
- })
48
- vectors.forEach((vector, index) => {
49
- it(`Should pass test vector #${index + 1}`, () => {
50
- const key = new SymmetricKey([...Buffer.from(vector.key, 'base64')])
51
- const result = key.decrypt(
52
- [...Buffer.from(vector.ciphertext, 'base64')],
53
- 'hex'
54
- )
55
- expect(result).toEqual(Buffer.from(vector.plaintext).toString('hex'))
56
- })
57
- })
58
- })
@@ -1,40 +0,0 @@
1
- /**
2
- * These vectors check our implementation of AES-GCM encryption.
3
- *
4
- * The first 32 bytes of the ciphertext contain the initialization vector and
5
- * the rest is the AES-GCM ciphertext.
6
- *
7
- * The plaintext is whatever we want to encrypt represented as a string.
8
- *
9
- * Because the initialization vector is generated randomly during the
10
- * encryption process, we cannot use determanistic test vectors to test the
11
- * encrypt functionality. Therefore, only the test runner for the decrypt
12
- * function will execute and validate against the vectors.
13
- */
14
- export default [
15
- {
16
- ciphertext: '1cf74FpvW0koFZk5e1VQcCtF7UdLj9mtN/L9loFlXwhf6w/06THwVirsvDShuT/KlOjO/HFALj8AcGLU1KRs4zNJDaX2wNebuPkH+qp5N/0cp3fZxgFzHJB3jBPDcdFi8O9WXIBLx9jUQ5KFQk0mZCB2k90VniInWuzqqOQAQQlBy2rgBWp4xg==',
17
- key: 'LIe9CoVXxDDDKt9F4j2lE+GP4oPcMElwyX+LVsuRLqw=',
18
- plaintext: '5+w9tts+i14GDfPSEJwcaAfce7zVLC7wsRAMnCBqIczkqL08I05FZTl7n14H9hnPkS7HBm3EGWNDKCZ64ckCGg=='
19
- },
20
- {
21
- ciphertext: 'IFh45HxwvK7wgIZr5UDxvUiEkvjsXVV6VIksaEQoTNCPleaRxE1CE1eZj5ZSPa/Mo2HXa2kvEmVAMslY12gMb7qHAHT2fSORB8TJKubKcjwGUrRxqOWvk24lv7QKhq3uhKkJxZSkPBZS6UM+xX+x7Mb53CoC8Z+7Ork50wGRAA415C+T8FIluA==',
22
- key: 'Di30+CTH8yKVJfXmbkRU6DOesD042IkjZCbFL1lnNqY=',
23
- plaintext: '6pHqDrkIuGmWIpB1spu30PP848D04WlERSjrEZ/JD0jfdS814cOjs4MFkePT1IHeM4+qGFwAMk7HKgWShOKFDQ=='
24
- },
25
- {
26
- ciphertext: 'JeUMCTX3hW7uH7Njfqjtjxd/8jB0Uj4eLLbLNBSMqF3XJmtq2oyX/WWS1po8cwn7jrcK0k8mVxHax/DctH6CIDMc0udBxWYLDyftvIYr448otWmn2IKQN4d3Bh2PKdiIQOo36DO2wOy+T2OJSmJ2XvAkenSZIckCdPIQVpeIi7Bt2ZpHmkObkg==',
27
- key: 'v7kFn4JdB3OVVjy8lk7UTvWe0vY5Qyzn64Q0EVoezlU=',
28
- plaintext: 'bSYHdJn15pcsaI8CNmfjKQ3ZvMg7zBaxuxBqyWBmCLdqj29bK54C26G1mx5e605hDrFpuJoNSDTECrk67ebffA=='
29
- },
30
- {
31
- ciphertext: 'ktpzKolKsvtWrvLl0yMdGvh5ngd1hiaNcC1b5yuzo2DEKO/4S7gePO/CWOmW/dloHhzfbBQH9rKDFKK7xHHgqYRc',
32
- key: 'qIgnjD0FfGVMiWo107bP0oHsLA402lhC7AYUFIKY1KQ=',
33
- plaintext: 'A cat and a mouse.'
34
- },
35
- {
36
- ciphertext: 'vremTalPp+NxN/loEtLMB94tEymdFk2TfBoTWNYcf4sQqYSNkx2WPdJ4LxrIsGuIg9KMOt7FOcIpDb6rRVpP',
37
- key: 'K7E/bf3wp6hrVeW0V1KvFJS5JZMhyxwPHCIW6wKBTb0=',
38
- plaintext: 'üñîçø∂é'
39
- }
40
- ]
@@ -1,198 +0,0 @@
1
- import BigNumber from '../../../dist/cjs/src/primitives/BigNumber'
2
- import { Reader, Writer, encode } from '../../../dist/cjs/src/primitives/utils'
3
-
4
- describe('Writer', () => {
5
- it('should create a new buffer writer', () => {
6
- const bw = new Writer()
7
- expect(bw).toBeDefined()
8
- })
9
-
10
- describe('#getLength', () => {
11
- it('should compute length correctly of two 2 byte buffers', () => {
12
- const buf1 = Buffer.from('0000', 'hex')
13
- const buf2 = Buffer.from('0000', 'hex')
14
- const bw = new Writer().write(buf1).write(buf2)
15
- expect(bw.getLength()).toEqual(4)
16
- })
17
- })
18
-
19
- describe('#toArray', () => {
20
- it('should concat these two bufs', () => {
21
- const buf1 = [0]
22
- const buf2 = [1]
23
- const bw = new Writer([buf1, buf2])
24
- expect(encode(bw.toArray(), 'hex')).toEqual('0001')
25
- })
26
- })
27
-
28
- describe('#write', () => {
29
- it('should write a buffer', () => {
30
- const buf = [0]
31
- const bw = new Writer()
32
- bw.write(buf)
33
- expect(encode(bw.toArray(), 'hex')).toEqual('00')
34
- })
35
- })
36
-
37
- describe('#writeReverse', () => {
38
- it('should write a buffer in reverse', () => {
39
- const buf = [0, 1]
40
- const bw = new Writer()
41
- bw.writeReverse(buf)
42
- expect(encode(bw.toArray(), 'hex')).toEqual('0100')
43
- })
44
- })
45
-
46
- describe('#writeUInt8', () => {
47
- it('should write 1', () => {
48
- const bw = new Writer()
49
- expect(encode(bw.writeUInt8(1).toArray(), 'hex')).toEqual('01')
50
- })
51
- })
52
-
53
- describe('#writeInt8', () => {
54
- it('should write 1', () => {
55
- const bw = new Writer()
56
- expect(encode(bw.writeInt8(1).toArray(), 'hex')).toEqual('01')
57
- expect(
58
- encode(new Writer().writeInt8(-1).toArray(), 'hex')
59
- ).toEqual('ff')
60
- })
61
- })
62
-
63
- describe('#writeUInt16BE', () => {
64
- it('should write 1', () => {
65
- const bw = new Writer()
66
- expect(encode(bw.writeUInt16BE(1).toArray(), 'hex')).toEqual('0001')
67
- })
68
- })
69
-
70
- describe('#writeInt16BE', () => {
71
- it('should write 1', () => {
72
- const bw = new Writer()
73
- expect(encode(bw.writeInt16BE(1).toArray(), 'hex')).toEqual('0001')
74
- expect(encode(new Writer().writeInt16BE(-1).toArray(), 'hex')).toEqual('ffff')
75
- })
76
- })
77
-
78
- describe('#writeUInt16LE', () => {
79
- it('should write 1', () => {
80
- const bw = new Writer()
81
- expect(encode(bw.writeUInt16LE(1).toArray(), 'hex')).toEqual('0100')
82
- })
83
- })
84
-
85
- describe('#writeInt16LE', () => {
86
- it('should write 1', () => {
87
- const bw = new Writer()
88
- expect(encode(bw.writeInt16LE(1).toArray(), 'hex')).toEqual('0100')
89
- expect(encode(new Writer().writeInt16LE(-1).toArray(), 'hex')).toEqual('ffff')
90
- })
91
- })
92
-
93
- describe('#writeUInt32BE', () => {
94
- it('should write 1', () => {
95
- const bw = new Writer()
96
- expect(encode(bw.writeUInt32BE(1).toArray(), 'hex')).toEqual('00000001')
97
- })
98
- })
99
-
100
- describe('#writeInt32BE', () => {
101
- it('should write 1', () => {
102
- const bw = new Writer()
103
- expect(encode(bw.writeInt32BE(1).toArray(), 'hex')).toEqual('00000001')
104
- expect(encode(new Writer().writeInt32BE(-1).toArray(), 'hex')).toEqual('ffffffff')
105
- })
106
- })
107
-
108
- describe('#writeUInt32LE', () => {
109
- it('should write 1', () => {
110
- const bw = new Writer()
111
- expect(encode(bw.writeUInt32LE(1).toArray(), 'hex')).toEqual('01000000')
112
- })
113
- })
114
-
115
- describe('#writeInt32LE', () => {
116
- it('should write 1', () => {
117
- const bw = new Writer()
118
- expect(encode(bw.writeInt32LE(1).toArray(), 'hex')).toEqual('01000000')
119
- expect(encode(new Writer().writeInt32LE(-1).toArray(), 'hex')).toEqual('ffffffff')
120
- })
121
- })
122
-
123
- describe('#writeUInt64BEBn', () => {
124
- it('should write 1', () => {
125
- const bw = new Writer()
126
- expect(encode(bw.writeUInt64BEBn(new BigNumber(1)).toArray(), 'hex')).toEqual('0000000000000001')
127
- })
128
- })
129
-
130
- describe('#writeUInt64LEBn', () => {
131
- it('should write 1', () => {
132
- const bw = new Writer()
133
- expect(encode(bw.writeUInt64LEBn(new BigNumber(1)).toArray(), 'hex')).toEqual('0100000000000000')
134
- })
135
- })
136
-
137
- describe('#writeVarInt', () => {
138
- it('should write a 1 byte varInt', () => {
139
- const bw = new Writer()
140
- bw.writeVarIntNum(1)
141
- expect(bw.toArray().length).toEqual(1)
142
- })
143
-
144
- it('should write a 3 byte varInt', () => {
145
- const bw = new Writer()
146
- bw.writeVarIntNum(1000)
147
- expect(bw.toArray().length).toEqual(3)
148
- })
149
-
150
- it('should write a 5 byte varInt', () => {
151
- const bw = new Writer()
152
- bw.writeVarIntNum(Math.pow(2, 16 + 1))
153
- expect(bw.toArray().length).toEqual(5)
154
- })
155
-
156
- it('should write a 9 byte varInt', () => {
157
- const bw = new Writer()
158
- bw.writeVarIntNum(Math.pow(2, 32 + 1))
159
- expect(bw.toArray().length).toEqual(9)
160
- })
161
-
162
- it('should read back the same value it wrote for a 9 byte varInt', () => {
163
- const bw = new Writer()
164
- const n = Math.pow(2, 53)
165
- expect(n).toEqual(n + 1) // javascript number precision limit
166
- bw.writeVarIntNum(n)
167
- const br = new Reader(bw.toArray())
168
- expect(br.readVarIntBn().toHex()).toEqual('20000000000000')
169
- })
170
- })
171
-
172
- describe('#writeVarIntBn', () => {
173
- it('should write a 1 byte varInt', () => {
174
- const bw = new Writer()
175
- bw.writeVarIntBn(new BigNumber(1))
176
- expect(bw.toArray().length).toEqual(1)
177
- })
178
-
179
- it('should write a 3 byte varInt', () => {
180
- const bw = new Writer()
181
- bw.writeVarIntBn(new BigNumber(1000))
182
- expect(bw.toArray().length).toEqual(3)
183
- })
184
-
185
- it('should write a 5 byte varInt', () => {
186
- const bw = new Writer()
187
- const bn = new BigNumber(Math.pow(2, 16 + 1))
188
- bw.writeVarIntBn(bn)
189
- expect(bw.toArray().length).toEqual(5)
190
- })
191
-
192
- it('should write a 9 byte varInt', () => {
193
- const bw = new Writer()
194
- bw.writeVarIntBn(new BigNumber(Math.pow(2, 32 + 1)))
195
- expect(bw.toArray().length).toEqual(9)
196
- })
197
- })
198
- })