@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.
- package/README.md +25 -3
- package/package.json +9 -5
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
- package/.github/ISSUE_TEMPLATE/discussion.md +0 -24
- package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +0 -23
- package/CHANGELOG.md +0 -72
- package/CONTRIBUTING.md +0 -85
- package/ROADMAP.md +0 -3
- package/docs/getting-started/COMMONJS.md +0 -94
- package/docs/getting-started/REACT-TS.md +0 -131
- package/docs/getting-started/TS-NODE.md +0 -106
- package/docs/getting-started/VUE.md +0 -103
- package/jest.config.js +0 -6
- package/mod.ts +0 -8
- package/src/compat/BSM.ts +0 -51
- package/src/compat/ECIES.ts +0 -557
- package/src/compat/HD.ts +0 -348
- package/src/compat/Mnemonic.ts +0 -295
- package/src/compat/__tests/BSM.test.ts +0 -38
- package/src/compat/__tests/ECIES.test.ts +0 -90
- package/src/compat/__tests/HD.test.ts +0 -405
- package/src/compat/__tests/Mnemonic.test.ts +0 -177
- package/src/compat/__tests/Mnemonic.vectors.ts +0 -172
- package/src/compat/bip-39-wordlist-en.ts +0 -2053
- package/src/compat/index.ts +0 -4
- package/src/messages/EncryptedMessage.ts +0 -70
- package/src/messages/SignedMessage.ts +0 -87
- package/src/messages/__tests/EncryptedMessage.test.ts +0 -36
- package/src/messages/__tests/SignedMessage.test.ts +0 -53
- package/src/messages/index.ts +0 -2
- package/src/primitives/AESGCM.ts +0 -479
- package/src/primitives/BasePoint.ts +0 -21
- package/src/primitives/BigNumber.ts +0 -4619
- package/src/primitives/Curve.ts +0 -1163
- package/src/primitives/DRBG.ts +0 -102
- package/src/primitives/ECDSA.ts +0 -164
- package/src/primitives/Hash.ts +0 -1420
- package/src/primitives/JacobianPoint.ts +0 -410
- package/src/primitives/K256.ts +0 -116
- package/src/primitives/Mersenne.ts +0 -123
- package/src/primitives/MontgomoryMethod.ts +0 -160
- package/src/primitives/Point.ts +0 -852
- package/src/primitives/PrivateKey.ts +0 -195
- package/src/primitives/PublicKey.ts +0 -154
- package/src/primitives/Random.ts +0 -55
- package/src/primitives/ReductionContext.ts +0 -528
- package/src/primitives/Signature.ts +0 -235
- package/src/primitives/SymmetricKey.ts +0 -75
- package/src/primitives/TransactionSignature.ts +0 -189
- package/src/primitives/__tests/AESGCM.test.ts +0 -338
- package/src/primitives/__tests/BRC42.private.vectors.ts +0 -33
- package/src/primitives/__tests/BRC42.public.vectors.ts +0 -33
- package/src/primitives/__tests/BigNumber.arithmatic.test.ts +0 -572
- package/src/primitives/__tests/BigNumber.binary.test.ts +0 -203
- package/src/primitives/__tests/BigNumber.constructor.test.ts +0 -176
- package/src/primitives/__tests/BigNumber.dhGroup.test.ts +0 -18
- package/src/primitives/__tests/BigNumber.fixtures.ts +0 -264
- package/src/primitives/__tests/BigNumber.serializers.test.ts +0 -157
- package/src/primitives/__tests/BigNumber.utils.test.ts +0 -347
- package/src/primitives/__tests/Curve.unit.test.ts +0 -192
- package/src/primitives/__tests/DRBG.test.ts +0 -18
- package/src/primitives/__tests/DRBG.vectors.ts +0 -167
- package/src/primitives/__tests/ECDH.test.ts +0 -31
- package/src/primitives/__tests/ECDSA.test.ts +0 -58
- package/src/primitives/__tests/HMAC.test.ts +0 -59
- package/src/primitives/__tests/Hash.test.ts +0 -121
- package/src/primitives/__tests/PBKDF2.vectors.ts +0 -119
- package/src/primitives/__tests/PrivateKey.test.ts +0 -17
- package/src/primitives/__tests/PublicKey.test.ts +0 -66
- package/src/primitives/__tests/Random.test.ts +0 -14
- package/src/primitives/__tests/Reader.test.ts +0 -296
- package/src/primitives/__tests/ReductionContext.test.ts +0 -279
- package/src/primitives/__tests/SymmetricKey.test.ts +0 -58
- package/src/primitives/__tests/SymmetricKey.vectors.ts +0 -40
- package/src/primitives/__tests/Writer.test.ts +0 -198
- package/src/primitives/__tests/sighash.vectors.ts +0 -3503
- package/src/primitives/__tests/utils.test.ts +0 -108
- package/src/primitives/index.ts +0 -8
- package/src/primitives/utils.ts +0 -665
- package/src/script/LockingScript.ts +0 -30
- package/src/script/OP.ts +0 -219
- package/src/script/Script.ts +0 -426
- package/src/script/ScriptChunk.ts +0 -7
- package/src/script/ScriptTemplate.ts +0 -36
- package/src/script/Spend.ts +0 -1379
- package/src/script/UnlockingScript.ts +0 -30
- package/src/script/__tests/Script.test.ts +0 -369
- package/src/script/__tests/Spend.test.ts +0 -248
- package/src/script/__tests/script.invalid.vectors.ts +0 -925
- package/src/script/__tests/script.valid.vectors.ts +0 -1120
- package/src/script/__tests/scriptFromVector.ts +0 -42
- package/src/script/__tests/spend.valid.vectors.ts +0 -2288
- package/src/script/index.ts +0 -7
- package/src/script/templates/P2PKH.ts +0 -109
- package/src/script/templates/RPuzzle.ts +0 -140
- package/src/script/templates/index.ts +0 -2
- package/src/transaction/Broadcaster.ts +0 -42
- package/src/transaction/ChainTracker.ts +0 -22
- package/src/transaction/FeeModel.ts +0 -13
- package/src/transaction/MerklePath.ts +0 -259
- package/src/transaction/Transaction.ts +0 -602
- package/src/transaction/TransactionInput.ts +0 -63
- package/src/transaction/TransactionOutput.ts +0 -37
- package/src/transaction/__tests/MerklePath.test.ts +0 -181
- package/src/transaction/__tests/Transaction.test.ts +0 -413
- package/src/transaction/__tests/bigtx.vectors.ts +0 -4
- package/src/transaction/__tests/bump.invalid.vectors.ts +0 -8
- package/src/transaction/__tests/bump.valid.vectors.ts +0 -4
- package/src/transaction/__tests/tx.invalid.vectors.ts +0 -281
- package/src/transaction/__tests/tx.valid.vectors.ts +0 -364
- package/src/transaction/broadcasters/ARC.ts +0 -106
- package/src/transaction/broadcasters/__tests/ARC.test.ts +0 -115
- package/src/transaction/broadcasters/index.ts +0 -1
- package/src/transaction/fee-models/SatoshisPerKilobyte.ts +0 -71
- package/src/transaction/fee-models/index.ts +0 -1
- package/src/transaction/index.ts +0 -6
- package/ts2md.json +0 -5
- package/tsconfig.base.json +0 -26
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.eslint.json +0 -12
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -17
- package/tsconfig.types.json +0 -11
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
export default [{
|
|
2
|
-
"key": "password",
|
|
3
|
-
"salt": "salt",
|
|
4
|
-
"iterations": 1,
|
|
5
|
-
"dkLen": 32,
|
|
6
|
-
"results": {
|
|
7
|
-
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252"
|
|
8
|
-
}
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"key": "password",
|
|
12
|
-
"salt": "salt",
|
|
13
|
-
"iterations": 2,
|
|
14
|
-
"dkLen": 32,
|
|
15
|
-
"results": {
|
|
16
|
-
"sha512": "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53c"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
"key": "password",
|
|
21
|
-
"salt": "salt",
|
|
22
|
-
"iterations": 1,
|
|
23
|
-
"dkLen": 64,
|
|
24
|
-
"results": {
|
|
25
|
-
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
"key": "password",
|
|
30
|
-
"salt": "salt",
|
|
31
|
-
"iterations": 2,
|
|
32
|
-
"dkLen": 64,
|
|
33
|
-
"results": {
|
|
34
|
-
"sha512": "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"key": "password",
|
|
39
|
-
"salt": "salt",
|
|
40
|
-
"iterations": 4096,
|
|
41
|
-
"dkLen": 32,
|
|
42
|
-
"results": {
|
|
43
|
-
"sha512": "d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5"
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
"key": "passwordPASSWORDpassword",
|
|
48
|
-
"salt": "saltSALTsaltSALTsaltSALTsaltSALTsalt",
|
|
49
|
-
"iterations": 4096,
|
|
50
|
-
"dkLen": 40,
|
|
51
|
-
"results": {
|
|
52
|
-
"sha512": "8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd953"
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
"key": "pass\u00000word",
|
|
57
|
-
"salt": "sa\u00000lt",
|
|
58
|
-
"iterations": 4096,
|
|
59
|
-
"dkLen": 16,
|
|
60
|
-
"results": {
|
|
61
|
-
"sha512": "336d14366099e8aac2c46c94a8f178d2"
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
"keyHex": "63ffeeddccbbaa",
|
|
66
|
-
"salt": "salt",
|
|
67
|
-
"iterations": 1,
|
|
68
|
-
"dkLen": 32,
|
|
69
|
-
"results": {
|
|
70
|
-
"sha512": "f69de451247225a7b30cc47632899572bb980f500d7c606ac9b1c04f928a3488"
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
"description": "Unicode salt, no truncation due to hex",
|
|
75
|
-
"key": "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
|
|
76
|
-
"saltHex": "6d6e656d6f6e6963e383a1e383bce38388e383abe382abe38299e3838fe38299e382a6e38299e382a1e381afe3829ae381afe38299e3818fe38299e3829de38299e381a1e381a1e38299e58d81e4babae58d81e889b2",
|
|
77
|
-
"iterations": 2048,
|
|
78
|
-
"dkLen": 64,
|
|
79
|
-
"results": {
|
|
80
|
-
"sha512": "ba553eedefe76e67e2602dc20184c564010859faada929a090dd2c57aacb204ceefd15404ab50ef3e8dbeae5195aeae64b0def4d2eead1cdc728a33ced520ffd"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
"key": "password",
|
|
85
|
-
"salt": "salt",
|
|
86
|
-
"iterations": 1,
|
|
87
|
-
"dkLen": 10,
|
|
88
|
-
"results": {
|
|
89
|
-
"sha512": "867f70cf1ade02cff375"
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
"key": "password",
|
|
94
|
-
"salt": "salt",
|
|
95
|
-
"iterations": 1,
|
|
96
|
-
"dkLen": 100,
|
|
97
|
-
"results": {
|
|
98
|
-
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce7b532e206c2967d4c7d2ffa460539fc4d4e5eec70125d74c6c7cf86d25284f297907fcea"
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
"keyUint8Array": [112, 97, 115, 115, 119, 111, 114, 100],
|
|
103
|
-
"salt": "salt",
|
|
104
|
-
"iterations": 1,
|
|
105
|
-
"dkLen": 32,
|
|
106
|
-
"results": {
|
|
107
|
-
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252"
|
|
108
|
-
}
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
"key": "password",
|
|
112
|
-
"saltUint8Array": [115, 97, 108, 116],
|
|
113
|
-
"iterations": 1,
|
|
114
|
-
"dkLen": 32,
|
|
115
|
-
"results": {
|
|
116
|
-
"sha512": "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252"
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
]
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import PublicKey from '../../../dist/cjs/src/primitives/PublicKey'
|
|
2
|
-
import PrivateKey from '../../../dist/cjs/src/primitives/PrivateKey'
|
|
3
|
-
import BRC42Private from './BRC42.private.vectors'
|
|
4
|
-
|
|
5
|
-
describe('PrivateKey', () => {
|
|
6
|
-
describe('BRC42 vectors', () => {
|
|
7
|
-
for (let i = 0; i < BRC42Private.length; i++) {
|
|
8
|
-
it(`Passes BRC42 private vector #${i + 1}`, () => {
|
|
9
|
-
const v = BRC42Private[i]
|
|
10
|
-
const publicKey = PublicKey.fromString(v.senderPublicKey)
|
|
11
|
-
const privateKey = PrivateKey.fromString(v.recipientPrivateKey, 16)
|
|
12
|
-
const derived = privateKey.deriveChild(publicKey, v.invoiceNumber)
|
|
13
|
-
expect(derived.toHex(32)).toEqual(v.privateKey)
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
})
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import PublicKey from '../../../dist/cjs/src/primitives/PublicKey'
|
|
2
|
-
import PrivateKey from '../../../dist/cjs/src/primitives/PrivateKey'
|
|
3
|
-
import Point from '../../../dist/cjs/src/primitives/Point'
|
|
4
|
-
import BigNumber from '../../../dist/cjs/src/primitives/BigNumber'
|
|
5
|
-
import BRC42Public from './BRC42.public.vectors'
|
|
6
|
-
|
|
7
|
-
describe('PublicKey', () => {
|
|
8
|
-
let privateKey
|
|
9
|
-
let publicKey
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
privateKey = PrivateKey.fromRandom()
|
|
13
|
-
publicKey = PublicKey.fromPrivateKey(privateKey)
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
describe('Static methods', () => {
|
|
17
|
-
test('fromPrivateKey should return a valid PublicKey', () => {
|
|
18
|
-
expect(publicKey).toBeInstanceOf(PublicKey)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
test('fromDER should create a PublicKey from a string', () => {
|
|
22
|
-
const pubKeyString = publicKey.toString()
|
|
23
|
-
const newPublicKey = PublicKey.fromString(pubKeyString)
|
|
24
|
-
expect(newPublicKey).toBeInstanceOf(PublicKey)
|
|
25
|
-
expect(newPublicKey.x.toHex()).toEqual(publicKey.x.toHex())
|
|
26
|
-
expect(newPublicKey.y.toHex()).toEqual(publicKey.y.toHex())
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
describe('Instance methods', () => {
|
|
31
|
-
test('deriveSharedSecret should derive a shared secret Point', () => {
|
|
32
|
-
const sharedSecret = publicKey.deriveSharedSecret(privateKey)
|
|
33
|
-
expect(sharedSecret).toBeInstanceOf(Point)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('deriveSharedSecret should throw error for invalid public key', () => {
|
|
37
|
-
const invalidPublicKey = new PublicKey(10, 13)
|
|
38
|
-
expect(() => {
|
|
39
|
-
invalidPublicKey.deriveSharedSecret(privateKey)
|
|
40
|
-
}).toThrow('Public key not valid for ECDH secret derivation')
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test('verify should return true for valid signature', () => {
|
|
44
|
-
const message = new BigNumber('deadbeef', 16)
|
|
45
|
-
const signature = privateKey.sign(message)
|
|
46
|
-
expect(publicKey.verify(message, signature)).toBe(true)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('toDER should return DER encoded string of public key', () => {
|
|
50
|
-
const derString = publicKey.toDER()
|
|
51
|
-
expect(typeof derString).toBe('string')
|
|
52
|
-
expect(derString.length).toBe(66)
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
describe('BRC42 vectors', () => {
|
|
56
|
-
for (let i = 0; i < BRC42Public.length; i++) {
|
|
57
|
-
it(`Passes BRC42 public vector #${i + 1}`, () => {
|
|
58
|
-
const v = BRC42Public[i]
|
|
59
|
-
const publicKey = PublicKey.fromString(v.recipientPublicKey)
|
|
60
|
-
const privateKey = PrivateKey.fromString(v.senderPrivateKey, 16)
|
|
61
|
-
const derived = publicKey.deriveChild(privateKey, v.invoiceNumber)
|
|
62
|
-
expect(derived.toString()).toEqual(v.publicKey)
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/* eslint-env jest */
|
|
2
|
-
import Random from '../../../dist/cjs/src/primitives/Random'
|
|
3
|
-
|
|
4
|
-
describe('Random', () => {
|
|
5
|
-
it('Produces random bytes of correct length', () => {
|
|
6
|
-
expect(Random(3).length).toBe(3)
|
|
7
|
-
expect(Random(10).length).toBe(10)
|
|
8
|
-
})
|
|
9
|
-
it('Does not produce the same thing every time', () => {
|
|
10
|
-
// While this test may fail once every few hundred trillion years or so,
|
|
11
|
-
// I haven't seen it fail yet. If you see it fail, please let me know.
|
|
12
|
-
expect(Random(32)).not.toEqual(Random(32))
|
|
13
|
-
})
|
|
14
|
-
})
|
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
import BigNumber from '../../../dist/cjs/src/primitives/BigNumber'
|
|
2
|
-
import { Reader, Writer, toHex } from '../../../dist/cjs/src/primitives/utils'
|
|
3
|
-
|
|
4
|
-
describe('Reader', () => {
|
|
5
|
-
it('should make a new Br', () => {
|
|
6
|
-
let br = new Reader()
|
|
7
|
-
expect(br).toBeDefined()
|
|
8
|
-
br = new Reader()
|
|
9
|
-
expect(br).toBeDefined()
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
it('should create a new Reader with an Array', () => {
|
|
13
|
-
const arr = []
|
|
14
|
-
const br = new Reader(arr)
|
|
15
|
-
expect(br).toBeDefined()
|
|
16
|
-
expect(Array.isArray(br.bin)).toBeTruthy()
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
describe('#eof', () => {
|
|
20
|
-
it('should return true for a blank br', () => {
|
|
21
|
-
const br = new Reader(Buffer.from([]))
|
|
22
|
-
expect(br.eof()).toBeTruthy()
|
|
23
|
-
})
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
describe('#read', () => {
|
|
27
|
-
it('should return the same buffer', () => {
|
|
28
|
-
const buf = Buffer.from([0])
|
|
29
|
-
const br = new Reader(buf)
|
|
30
|
-
expect(br.read().toString('hex')).toEqual(buf.toString('hex'))
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should return a buffer of this length', () => {
|
|
34
|
-
const buf = Buffer.alloc(10)
|
|
35
|
-
buf.fill(0)
|
|
36
|
-
const br = new Reader(buf)
|
|
37
|
-
const buf2 = br.read(2)
|
|
38
|
-
expect(buf2.length).toEqual(2)
|
|
39
|
-
expect(br.eof()).toBeFalsy()
|
|
40
|
-
expect(br.pos).toEqual(2)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should be able to read 0 bytes', () => {
|
|
44
|
-
const buf = Buffer.from('0101', 'hex')
|
|
45
|
-
expect(new Reader(buf).read(0).length).toEqual(0)
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
describe('#readReverse', () => {
|
|
50
|
-
it('should reverse this [0, 1]', () => {
|
|
51
|
-
const buf = Buffer.from([0, 1])
|
|
52
|
-
const br = new Reader(buf)
|
|
53
|
-
expect(toHex(br.readReverse())).toEqual('0100')
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('should be able to read 0 bytes', () => {
|
|
57
|
-
const buf = Buffer.from('0101', 'hex')
|
|
58
|
-
expect(new Reader(buf).readReverse(0).length).toEqual(0)
|
|
59
|
-
})
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
describe('#readUInt8', () => {
|
|
63
|
-
it('should return 1', () => {
|
|
64
|
-
const buf = Buffer.alloc(1)
|
|
65
|
-
buf.writeUInt8(1, 0)
|
|
66
|
-
const br = new Reader(buf)
|
|
67
|
-
expect(br.readUInt8()).toEqual(1)
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
describe('#readInt8', () => {
|
|
72
|
-
it('should return 1', () => {
|
|
73
|
-
const buf = Buffer.alloc(1)
|
|
74
|
-
buf.writeInt8(1, 0)
|
|
75
|
-
const br = new Reader(buf)
|
|
76
|
-
expect(br.readInt8()).toEqual(1)
|
|
77
|
-
expect(new Reader(Buffer.from('ff', 'hex')).readInt8()).toEqual(-1)
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
describe('#readUInt16BE', () => {
|
|
82
|
-
it('should return 1', () => {
|
|
83
|
-
const buf = Buffer.alloc(2)
|
|
84
|
-
buf.writeUInt16BE(1, 0)
|
|
85
|
-
const br = new Reader(buf)
|
|
86
|
-
expect(br.readUInt16BE()).toEqual(1)
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
describe('#readInt16BE', () => {
|
|
91
|
-
it('should return 1', () => {
|
|
92
|
-
const buf = Buffer.alloc(2)
|
|
93
|
-
buf.writeInt16BE(1, 0)
|
|
94
|
-
const br = new Reader(buf)
|
|
95
|
-
expect(br.readInt16BE()).toEqual(1)
|
|
96
|
-
expect(new Reader(Buffer.from('ffff', 'hex')).readInt16BE()).toEqual(-1)
|
|
97
|
-
})
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
describe('#readUInt16LE', () => {
|
|
101
|
-
it('should return 1', () => {
|
|
102
|
-
const buf = Buffer.alloc(2)
|
|
103
|
-
buf.writeUInt16LE(1, 0)
|
|
104
|
-
const br = new Reader(buf)
|
|
105
|
-
expect(br.readUInt16LE()).toEqual(1)
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
describe('#readInt16LE', () => {
|
|
110
|
-
it('should return 1', () => {
|
|
111
|
-
const buf = Buffer.alloc(2)
|
|
112
|
-
buf.writeInt16LE(1, 0)
|
|
113
|
-
const br = new Reader(buf)
|
|
114
|
-
expect(br.readInt16LE()).toEqual(1)
|
|
115
|
-
expect(new Reader(Buffer.from('ffff', 'hex')).readInt16LE()).toEqual(-1)
|
|
116
|
-
})
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
describe('#readUInt32BE', () => {
|
|
120
|
-
it('should return 1', () => {
|
|
121
|
-
const buf = Buffer.alloc(4)
|
|
122
|
-
buf.writeUInt32BE(1, 0)
|
|
123
|
-
const br = new Reader(buf)
|
|
124
|
-
expect(br.readUInt32BE()).toEqual(1)
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
describe('#readInt32BE', () => {
|
|
129
|
-
it('should return 1', () => {
|
|
130
|
-
const buf = Buffer.alloc(4)
|
|
131
|
-
buf.writeInt32BE(1, 0)
|
|
132
|
-
const br = new Reader(buf)
|
|
133
|
-
expect(br.readInt32BE()).toEqual(1)
|
|
134
|
-
expect(new Reader(Buffer.from('ffffffff', 'hex')).readInt32BE()).toEqual(-1)
|
|
135
|
-
})
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
describe('#readUInt32LE', () => {
|
|
139
|
-
it('should return 1', () => {
|
|
140
|
-
const buf = Buffer.alloc(4)
|
|
141
|
-
buf.writeUInt32LE(1, 0)
|
|
142
|
-
const br = new Reader(buf)
|
|
143
|
-
expect(br.readUInt32LE()).toEqual(1)
|
|
144
|
-
})
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
describe('#readInt32LE', () => {
|
|
148
|
-
it('should return 1', () => {
|
|
149
|
-
const buf = Buffer.alloc(4)
|
|
150
|
-
buf.writeInt32LE(1, 0)
|
|
151
|
-
const br = new Reader([...buf])
|
|
152
|
-
expect(br.readInt32LE()).toEqual(1)
|
|
153
|
-
expect(new Reader([...Buffer.from('ffffffff', 'hex')]).readInt32LE()).toEqual(-1)
|
|
154
|
-
})
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
describe('#readUInt64BEBn', () => {
|
|
158
|
-
it('should return 1', () => {
|
|
159
|
-
const buf = Buffer.alloc(8)
|
|
160
|
-
buf.fill(0)
|
|
161
|
-
buf.writeUInt32BE(1, 4)
|
|
162
|
-
const br = new Reader(buf)
|
|
163
|
-
expect(br.readUInt64BEBn().toNumber()).toEqual(1)
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
it('should return 2^64', () => {
|
|
167
|
-
const buf = Buffer.alloc(8)
|
|
168
|
-
buf.fill(0xff)
|
|
169
|
-
const br = new Reader(buf)
|
|
170
|
-
expect(br.readUInt64BEBn().toHex()).toEqual('ffffffffffffffff')
|
|
171
|
-
})
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
describe('#readUInt64LEBn', () => {
|
|
175
|
-
it('should return 1', () => {
|
|
176
|
-
const buf = Buffer.alloc(8)
|
|
177
|
-
buf.fill(0)
|
|
178
|
-
buf.writeUInt32LE(1, 0)
|
|
179
|
-
const br = new Reader(buf)
|
|
180
|
-
expect(br.readUInt64LEBn().toNumber()).toEqual(1)
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
it('should return 2^30', () => {
|
|
184
|
-
const buf = Buffer.alloc(8)
|
|
185
|
-
buf.fill(0)
|
|
186
|
-
buf.writeUInt32LE(Math.pow(2, 30), 0)
|
|
187
|
-
const br = new Reader(buf)
|
|
188
|
-
expect(br.readUInt64LEBn().toNumber()).toEqual(Math.pow(2, 30))
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
it('should return 0', () => {
|
|
192
|
-
const buf = Buffer.alloc(8)
|
|
193
|
-
buf.fill(0)
|
|
194
|
-
const br = new Reader(buf)
|
|
195
|
-
expect(br.readUInt64LEBn().toNumber()).toEqual(0)
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
it('should return 2^64', () => {
|
|
199
|
-
const buf = Buffer.alloc(8)
|
|
200
|
-
buf.fill(0xff)
|
|
201
|
-
const br = new Reader(buf)
|
|
202
|
-
expect(br.readUInt64LEBn().toHex()).toEqual('ffffffffffffffff')
|
|
203
|
-
})
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
describe('#readVarInt', () => {
|
|
207
|
-
it('should read a 1 byte varInt', () => {
|
|
208
|
-
const buf = Buffer.from([50])
|
|
209
|
-
const br = new Reader(buf)
|
|
210
|
-
expect(br.readVarInt().length).toEqual(1)
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
it('should read a 3 byte varInt', () => {
|
|
214
|
-
const buf = Buffer.from([253, 253, 0])
|
|
215
|
-
const br = new Reader(buf)
|
|
216
|
-
expect(br.readVarInt().length).toEqual(3)
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
it('should read a 5 byte varInt', () => {
|
|
220
|
-
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
221
|
-
buf.writeUInt32LE(50000, 1)
|
|
222
|
-
const br = new Reader(buf)
|
|
223
|
-
expect(br.readVarInt().length).toEqual(5)
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
it('should read a 9 byte varInt', () => {
|
|
227
|
-
const buf = new Writer().writeVarIntBn(new BigNumber(Math.pow(2, 54).toString())).toArray()
|
|
228
|
-
const br = new Reader(buf)
|
|
229
|
-
expect(br.readVarInt().length).toEqual(9)
|
|
230
|
-
})
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
describe('#readVarIntNum', () => {
|
|
234
|
-
it('should read a 1 byte varInt', () => {
|
|
235
|
-
const buf = Buffer.from([50])
|
|
236
|
-
const br = new Reader(buf)
|
|
237
|
-
expect(br.readVarIntNum()).toEqual(50)
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
it('should read a 3 byte varInt', () => {
|
|
241
|
-
const buf = Buffer.from([253, 253, 0])
|
|
242
|
-
const br = new Reader(buf)
|
|
243
|
-
expect(br.readVarIntNum()).toEqual(253)
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
it('should read a 5 byte varInt', () => {
|
|
247
|
-
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
248
|
-
buf.writeUInt32LE(50000, 1)
|
|
249
|
-
const br = new Reader(buf)
|
|
250
|
-
expect(br.readVarIntNum()).toEqual(50000)
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
it('should throw an error on a 9 byte varInt over the javascript uint precision limit', () => {
|
|
254
|
-
const buf = new Writer().writeVarIntBn(new BigNumber(Math.pow(2, 54).toString())).toArray()
|
|
255
|
-
const br = new Reader(buf)
|
|
256
|
-
expect(() => {
|
|
257
|
-
br.readVarIntNum()
|
|
258
|
-
}).toThrow('number too large to retain precision - use readVarIntBn')
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
it('should not throw an error on a 9 byte varInt not over the javascript uint precision limit', () => {
|
|
262
|
-
const buf = new Writer().writeVarIntBn(new BigNumber(Math.pow(2, 53).toString())).toArray()
|
|
263
|
-
const br = new Reader(buf)
|
|
264
|
-
expect(() => {
|
|
265
|
-
br.readVarIntNum()
|
|
266
|
-
}).not.toThrow('number too large to retain precision - use readVarIntBn')
|
|
267
|
-
})
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
describe('#readVarIntBn', () => {
|
|
271
|
-
it('should read a 1 byte varInt', () => {
|
|
272
|
-
const buf = Buffer.from([50])
|
|
273
|
-
const br = new Reader(buf)
|
|
274
|
-
expect(br.readVarIntBn().toNumber()).toEqual(50)
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
it('should read a 3 byte varInt', () => {
|
|
278
|
-
const buf = Buffer.from([253, 253, 0])
|
|
279
|
-
const br = new Reader(buf)
|
|
280
|
-
expect(br.readVarIntBn().toNumber()).toEqual(253)
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
it('should read a 5 byte varInt', () => {
|
|
284
|
-
const buf = Buffer.from([254, 0, 0, 0, 0])
|
|
285
|
-
buf.writeUInt32LE(50000, 1)
|
|
286
|
-
const br = new Reader(buf)
|
|
287
|
-
expect(br.readVarIntBn().toNumber()).toEqual(50000)
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
it('should read a 9 byte varInt', () => {
|
|
291
|
-
const buf = Buffer.concat([Buffer.from([255]), Buffer.from('ffffffffffffffff', 'hex')])
|
|
292
|
-
const br = new Reader(buf)
|
|
293
|
-
expect(br.readVarIntBn().toHex()).toEqual('ffffffffffffffff')
|
|
294
|
-
})
|
|
295
|
-
})
|
|
296
|
-
})
|