@transia/ripple-binary-codec 1.4.6-alpha.8 → 2.5.1-quantum.0
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/dist/binary.d.ts +30 -16
- package/dist/binary.js +36 -16
- package/dist/binary.js.map +1 -1
- package/dist/enums/bytes.d.ts +1 -2
- package/dist/enums/bytes.js +1 -2
- package/dist/enums/bytes.js.map +1 -1
- package/dist/enums/constants.d.ts +1 -0
- package/dist/enums/constants.js +6 -5
- package/dist/enums/constants.js.map +1 -1
- package/dist/enums/definitions.json +1565 -898
- package/dist/enums/field.d.ts +1 -2
- package/dist/enums/field.js +1 -2
- package/dist/enums/field.js.map +1 -1
- package/dist/enums/index.d.ts +1 -2
- package/dist/enums/index.js +5 -27
- package/dist/enums/index.js.map +1 -1
- package/dist/enums/src/enums/definitions.json +1566 -899
- package/dist/enums/xrpl-definitions-base.d.ts +5 -3
- package/dist/enums/xrpl-definitions-base.js +26 -6
- package/dist/enums/xrpl-definitions-base.js.map +1 -1
- package/dist/enums/xrpl-definitions.d.ts +2 -1
- package/dist/enums/xrpl-definitions.js +2 -1
- package/dist/enums/xrpl-definitions.js.map +1 -1
- package/dist/hash-prefixes.d.ts +1 -2
- package/dist/hash-prefixes.js +8 -6
- package/dist/hash-prefixes.js.map +1 -1
- package/dist/hashes.d.ts +6 -7
- package/dist/hashes.js +6 -7
- package/dist/hashes.js.map +1 -1
- package/dist/index.d.ts +16 -11
- package/dist/index.js +53 -58
- package/dist/index.js.map +1 -1
- package/dist/ledger-hashes.d.ts +1 -2
- package/dist/ledger-hashes.js +11 -30
- package/dist/ledger-hashes.js.map +1 -1
- package/dist/quality.d.ts +3 -4
- package/dist/quality.js +10 -8
- package/dist/quality.js.map +1 -1
- package/dist/serdes/binary-parser.d.ts +2 -3
- package/dist/serdes/binary-parser.js +16 -32
- package/dist/serdes/binary-parser.js.map +1 -1
- package/dist/serdes/binary-serializer.d.ts +6 -7
- package/dist/serdes/binary-serializer.js +12 -35
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/shamap.d.ts +4 -5
- package/dist/shamap.js +5 -5
- package/dist/shamap.js.map +1 -1
- package/dist/src/binary.d.ts +106 -0
- package/dist/src/binary.js +154 -0
- package/dist/src/binary.js.map +1 -0
- package/dist/src/coretypes.d.ts +9 -0
- package/dist/src/coretypes.js +48 -0
- package/dist/src/coretypes.js.map +1 -0
- package/dist/src/enums/bytes.d.ts +25 -0
- package/dist/src/enums/bytes.js +63 -0
- package/dist/src/enums/bytes.js.map +1 -0
- package/dist/src/enums/constants.d.ts +5 -0
- package/dist/src/enums/constants.js +9 -0
- package/dist/src/enums/constants.js.map +1 -0
- package/dist/src/enums/definitions.json +3385 -0
- package/dist/src/enums/field.d.ts +28 -0
- package/dist/src/enums/field.js +58 -0
- package/dist/src/enums/field.js.map +1 -0
- package/dist/src/enums/index.d.ts +12 -0
- package/dist/src/enums/index.js +31 -0
- package/dist/src/enums/index.js.map +1 -0
- package/dist/src/enums/utils-renumber.d.ts +101 -0
- package/dist/src/enums/utils-renumber.js +127 -0
- package/dist/src/enums/utils-renumber.js.map +1 -0
- package/dist/src/enums/xrpl-definitions-base.d.ts +47 -0
- package/dist/src/enums/xrpl-definitions-base.js +83 -0
- package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/src/enums/xrpl-definitions.d.ts +22 -0
- package/dist/src/enums/xrpl-definitions.js +30 -0
- package/dist/src/enums/xrpl-definitions.js.map +1 -0
- package/dist/src/hash-prefixes.d.ts +5 -0
- package/dist/src/hash-prefixes.js +43 -0
- package/dist/src/hash-prefixes.js.map +1 -0
- package/dist/src/hashes.d.ts +50 -0
- package/dist/src/hashes.js +76 -0
- package/dist/src/hashes.js.map +1 -0
- package/dist/src/index.d.ts +69 -0
- package/dist/src/index.js +134 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/ledger-hashes.d.ts +48 -0
- package/dist/src/ledger-hashes.js +141 -0
- package/dist/src/ledger-hashes.js.map +1 -0
- package/dist/src/quality.d.ts +21 -0
- package/dist/src/quality.js +42 -0
- package/dist/src/quality.js.map +1 -0
- package/dist/src/serdes/binary-parser.d.ts +100 -0
- package/dist/src/serdes/binary-parser.js +194 -0
- package/dist/src/serdes/binary-parser.js.map +1 -0
- package/dist/src/serdes/binary-serializer.d.ts +81 -0
- package/dist/src/serdes/binary-serializer.js +149 -0
- package/dist/src/serdes/binary-serializer.js.map +1 -0
- package/dist/src/shamap.d.ts +102 -0
- package/dist/src/shamap.js +172 -0
- package/dist/src/shamap.js.map +1 -0
- package/dist/src/types/account-id.d.ts +35 -0
- package/dist/src/types/account-id.js +69 -0
- package/dist/src/types/account-id.js.map +1 -0
- package/dist/src/types/amount.d.ts +91 -0
- package/dist/src/types/amount.js +289 -0
- package/dist/src/types/amount.js.map +1 -0
- package/dist/src/types/blob.d.ts +24 -0
- package/dist/src/types/blob.js +43 -0
- package/dist/src/types/blob.js.map +1 -0
- package/dist/src/types/currency.d.ts +28 -0
- package/dist/src/types/currency.js +127 -0
- package/dist/src/types/currency.js.map +1 -0
- package/dist/src/types/hash-128.d.ts +16 -0
- package/dist/src/types/hash-128.js +32 -0
- package/dist/src/types/hash-128.js.map +1 -0
- package/dist/src/types/hash-160.d.ts +10 -0
- package/dist/src/types/hash-160.js +19 -0
- package/dist/src/types/hash-160.js.map +1 -0
- package/dist/src/types/hash-192.d.ts +10 -0
- package/dist/src/types/hash-192.js +19 -0
- package/dist/src/types/hash-192.js.map +1 -0
- package/dist/src/types/hash-256.d.ts +10 -0
- package/dist/src/types/hash-256.js +16 -0
- package/dist/src/types/hash-256.js.map +1 -0
- package/dist/src/types/hash.d.ts +40 -0
- package/dist/src/types/hash.js +76 -0
- package/dist/src/types/hash.js.map +1 -0
- package/dist/src/types/index.d.ts +19 -0
- package/dist/src/types/index.js +66 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +48 -0
- package/dist/src/types/issue.js +104 -0
- package/dist/src/types/issue.js.map +1 -0
- package/dist/src/types/path-set.d.ts +36 -0
- package/dist/src/types/path-set.js +233 -0
- package/dist/src/types/path-set.js.map +1 -0
- package/dist/src/types/serialized-type.d.ts +70 -0
- package/dist/src/types/serialized-type.js +105 -0
- package/dist/src/types/serialized-type.js.map +1 -0
- package/dist/src/types/st-array.d.ts +31 -0
- package/dist/src/types/st-array.js +86 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-number.d.ts +54 -0
- package/dist/src/types/st-number.js +208 -0
- package/dist/src/types/st-number.js.map +1 -0
- package/dist/src/types/st-object.d.ts +32 -0
- package/dist/src/types/st-object.js +173 -0
- package/dist/src/types/st-object.js.map +1 -0
- package/dist/src/types/uint-16.d.ts +24 -0
- package/dist/src/types/uint-16.js +45 -0
- package/dist/src/types/uint-16.js.map +1 -0
- package/dist/src/types/uint-32.d.ts +24 -0
- package/dist/src/types/uint-32.js +50 -0
- package/dist/src/types/uint-32.js.map +1 -0
- package/dist/src/types/uint-64.d.ts +38 -0
- package/dist/src/types/uint-64.js +107 -0
- package/dist/src/types/uint-64.js.map +1 -0
- package/dist/src/types/uint-8.d.ts +24 -0
- package/dist/src/types/uint-8.js +46 -0
- package/dist/src/types/uint-8.js.map +1 -0
- package/dist/src/types/uint.d.ts +29 -0
- package/dist/src/types/uint.js +47 -0
- package/dist/src/types/uint.js.map +1 -0
- package/dist/src/types/vector-256.d.ts +30 -0
- package/dist/src/types/vector-256.js +73 -0
- package/dist/src/types/vector-256.js.map +1 -0
- package/dist/src/types/xchain-bridge.d.ts +44 -0
- package/dist/src/types/xchain-bridge.js +102 -0
- package/dist/src/types/xchain-bridge.js.map +1 -0
- package/dist/src/utils.d.ts +79 -0
- package/dist/src/utils.js +181 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/account-id.d.ts +1 -2
- package/dist/types/account-id.js +4 -6
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +31 -8
- package/dist/types/amount.js +125 -43
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +1 -2
- package/dist/types/blob.js +5 -2
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +1 -2
- package/dist/types/currency.js +13 -13
- package/dist/types/currency.js.map +1 -1
- package/dist/types/hash-128.d.ts +1 -2
- package/dist/types/hash-128.js +3 -3
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +1 -2
- package/dist/types/hash-160.js +1 -2
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-192.d.ts +10 -0
- package/dist/types/hash-192.js +19 -0
- package/dist/types/hash-192.js.map +1 -0
- package/dist/types/hash-256.d.ts +1 -2
- package/dist/types/hash-256.js +1 -2
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/hash.d.ts +2 -3
- package/dist/types/hash.js +8 -4
- package/dist/types/hash.js.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.js +11 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +48 -0
- package/dist/types/issue.js +104 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/path-set.js +12 -12
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +23 -16
- package/dist/types/serialized-type.js +15 -7
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +5 -2
- package/dist/types/st-array.js +17 -11
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-number.d.ts +54 -0
- package/dist/types/st-number.js +208 -0
- package/dist/types/st-number.js.map +1 -0
- package/dist/types/st-object.js +32 -8
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +1 -2
- package/dist/types/uint-16.js +6 -5
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +1 -2
- package/dist/types/uint-32.js +7 -6
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +6 -7
- package/dist/types/uint-64.js +44 -24
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +1 -2
- package/dist/types/uint-8.js +7 -5
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/uint.d.ts +5 -6
- package/dist/types/uint.js +5 -0
- package/dist/types/uint.js.map +1 -1
- package/dist/types/vector-256.d.ts +1 -2
- package/dist/types/vector-256.js +2 -4
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +44 -0
- package/dist/types/xchain-bridge.js +102 -0
- package/dist/types/xchain-bridge.js.map +1 -0
- package/dist/utils.d.ts +79 -0
- package/dist/utils.js +181 -0
- package/dist/utils.js.map +1 -0
- package/package.json +21 -17
- package/src/README.md +5 -0
- package/src/binary.ts +235 -0
- package/src/coretypes.ts +31 -0
- package/src/enums/README.md +144 -0
- package/src/enums/bytes.ts +74 -0
- package/src/enums/constants.ts +5 -0
- package/src/enums/definitions.json +3390 -0
- package/src/enums/field.ts +84 -0
- package/src/enums/index.ts +34 -0
- package/src/enums/utils-renumber.ts +134 -0
- package/src/enums/xrpl-definitions-base.ts +150 -0
- package/src/enums/xrpl-definitions.ts +33 -0
- package/src/hash-prefixes.ts +42 -0
- package/src/hashes.ts +75 -0
- package/src/index.ts +166 -0
- package/src/ledger-hashes.ts +191 -0
- package/src/quality.ts +38 -0
- package/src/serdes/binary-parser.ts +228 -0
- package/src/serdes/binary-serializer.ts +166 -0
- package/src/shamap.ts +186 -0
- package/src/types/account-id.ts +86 -0
- package/src/types/amount.ts +362 -0
- package/src/types/blob.ts +46 -0
- package/src/types/currency.ts +140 -0
- package/src/types/hash-128.ts +33 -0
- package/src/types/hash-160.ts +19 -0
- package/src/types/hash-192.ts +19 -0
- package/src/types/hash-256.ts +15 -0
- package/src/types/hash.ts +86 -0
- package/src/types/index.ts +68 -0
- package/src/types/issue.ts +136 -0
- package/src/types/path-set.ts +290 -0
- package/src/types/serialized-type.ts +122 -0
- package/src/types/st-array.ts +113 -0
- package/src/types/st-number.ts +233 -0
- package/src/types/st-object.ts +215 -0
- package/src/types/uint-16.ts +53 -0
- package/src/types/uint-32.ts +59 -0
- package/src/types/uint-64.ts +135 -0
- package/src/types/uint-8.ts +52 -0
- package/src/types/uint.ts +60 -0
- package/src/types/vector-256.ts +79 -0
- package/src/types/xchain-bridge.ts +128 -0
- package/src/utils.ts +220 -0
- package/test/amount.test.js +0 -43
- package/test/binary-json.test.js +0 -45
- package/test/binary-parser.test.js +0 -396
- package/test/binary-serializer.test.js +0 -307
- package/test/definitions.test.js +0 -100
- package/test/fixtures/account-tx-transactions.db +0 -0
- package/test/fixtures/codec-fixtures.json +0 -4466
- package/test/fixtures/data-driven-tests.json +0 -2919
- package/test/fixtures/delivermin-tx-binary.json +0 -1
- package/test/fixtures/delivermin-tx.json +0 -98
- package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx.json +0 -58
- package/test/fixtures/escrow-cancel-binary.json +0 -1
- package/test/fixtures/escrow-cancel-tx.json +0 -6
- package/test/fixtures/escrow-create-binary.json +0 -1
- package/test/fixtures/escrow-create-tx.json +0 -10
- package/test/fixtures/escrow-finish-binary.json +0 -1
- package/test/fixtures/escrow-finish-meta-binary.json +0 -1
- package/test/fixtures/escrow-finish-tx.json +0 -95
- package/test/fixtures/ledger-full-38129.json +0 -1
- package/test/fixtures/ledger-full-40000.json +0 -1
- package/test/fixtures/negative-unl.json +0 -12
- package/test/fixtures/nf-token.json +0 -547
- package/test/fixtures/payment-channel-claim-binary.json +0 -1
- package/test/fixtures/payment-channel-claim-tx.json +0 -8
- package/test/fixtures/payment-channel-create-binary.json +0 -1
- package/test/fixtures/payment-channel-create-tx.json +0 -11
- package/test/fixtures/payment-channel-fund-binary.json +0 -1
- package/test/fixtures/payment-channel-fund-tx.json +0 -7
- package/test/fixtures/signerlistset-tx-binary.json +0 -1
- package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
- package/test/fixtures/signerlistset-tx.json +0 -94
- package/test/fixtures/ticket-create-binary.json +0 -1
- package/test/fixtures/ticket-create-tx.json +0 -7
- package/test/fixtures/unl-report-binary.json +0 -1
- package/test/fixtures/unl-report-meta-binary.json +0 -1
- package/test/fixtures/unl-report.json +0 -89
- package/test/fixtures/x-codec-fixtures.json +0 -188
- package/test/hash.test.js +0 -135
- package/test/ledger.test.js +0 -29
- package/test/lower-case-hex.test.js +0 -46
- package/test/pseudo-transaction.test.js +0 -38
- package/test/quality.test.js +0 -15
- package/test/shamap.test.js +0 -89
- package/test/signing-data-encoding.test.js +0 -213
- package/test/tx-encode-decode.test.js +0 -119
- package/test/types.test.js +0 -34
- package/test/uint.test.js +0 -148
- package/test/utils.js +0 -30
- package/test/x-address.test.js +0 -181
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { UInt } from './uint'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { bytesToHex, concat, hexToBytes } from '@transia/isomorphic/utils'
|
|
4
|
+
import { readUInt32BE, writeUInt32BE } from '../utils'
|
|
5
|
+
import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
|
|
6
|
+
|
|
7
|
+
const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
|
|
8
|
+
const BASE10_REGEX = /^[0-9]{1,20}$/
|
|
9
|
+
const mask = BigInt(0x00000000ffffffff)
|
|
10
|
+
|
|
11
|
+
const BASE10_AMOUNT_FIELDS = new Set([
|
|
12
|
+
'MaximumAmount',
|
|
13
|
+
'OutstandingAmount',
|
|
14
|
+
'MPTAmount',
|
|
15
|
+
'LockedAmount',
|
|
16
|
+
])
|
|
17
|
+
|
|
18
|
+
function isBase10(fieldName: string): boolean {
|
|
19
|
+
return BASE10_AMOUNT_FIELDS.has(fieldName)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Derived UInt class for serializing/deserializing 64 bit UInt
|
|
24
|
+
*/
|
|
25
|
+
class UInt64 extends UInt {
|
|
26
|
+
protected static readonly width: number = 64 / 8 // 8
|
|
27
|
+
static readonly defaultUInt64: UInt64 = new UInt64(
|
|
28
|
+
new Uint8Array(UInt64.width),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
constructor(bytes: Uint8Array) {
|
|
32
|
+
super(bytes ?? UInt64.defaultUInt64.bytes)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static fromParser(parser: BinaryParser): UInt {
|
|
36
|
+
return new UInt64(parser.read(UInt64.width))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Construct a UInt64 object
|
|
41
|
+
*
|
|
42
|
+
* @param val A UInt64, hex-string, bigInt, or number
|
|
43
|
+
* @returns A UInt64 object
|
|
44
|
+
*/
|
|
45
|
+
static from<T extends UInt64 | string | bigint | number>(
|
|
46
|
+
val: T,
|
|
47
|
+
fieldName = '',
|
|
48
|
+
): UInt64 {
|
|
49
|
+
if (val instanceof UInt64) {
|
|
50
|
+
return val
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let buf = new Uint8Array(UInt64.width)
|
|
54
|
+
|
|
55
|
+
if (typeof val === 'number') {
|
|
56
|
+
if (val < 0) {
|
|
57
|
+
throw new Error('value must be an unsigned integer')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const number = BigInt(val)
|
|
61
|
+
|
|
62
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
63
|
+
writeUInt32BE(intBuf[0], Number(number >> BigInt(32)), 0)
|
|
64
|
+
writeUInt32BE(intBuf[1], Number(number & BigInt(mask)), 0)
|
|
65
|
+
|
|
66
|
+
return new UInt64(concat(intBuf))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (typeof val === 'string') {
|
|
70
|
+
if (isBase10(fieldName)) {
|
|
71
|
+
if (!BASE10_REGEX.test(val)) {
|
|
72
|
+
throw new Error(`${fieldName} ${val} is not a valid base 10 string`)
|
|
73
|
+
}
|
|
74
|
+
val = BigInt(val).toString(16) as T
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof val === 'string' && !HEX_REGEX.test(val)) {
|
|
78
|
+
throw new Error(`${val} is not a valid hex-string`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const strBuf = (val as string).padStart(16, '0')
|
|
82
|
+
buf = hexToBytes(strBuf)
|
|
83
|
+
return new UInt64(buf)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof val === 'bigint') {
|
|
87
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
88
|
+
writeUInt32BE(intBuf[0], Number(Number(val >> BigInt(32))), 0)
|
|
89
|
+
writeUInt32BE(intBuf[1], Number(val & BigInt(mask)), 0)
|
|
90
|
+
|
|
91
|
+
return new UInt64(concat(intBuf))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
throw new Error('Cannot construct UInt64 from given value')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The JSON representation of a UInt64 object
|
|
99
|
+
*
|
|
100
|
+
* @returns a hex-string
|
|
101
|
+
*/
|
|
102
|
+
toJSON(
|
|
103
|
+
_definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
|
|
104
|
+
fieldName = '',
|
|
105
|
+
): string {
|
|
106
|
+
const hexString = bytesToHex(this.bytes)
|
|
107
|
+
if (isBase10(fieldName)) {
|
|
108
|
+
return BigInt('0x' + hexString).toString(10)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return hexString
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get the value of the UInt64
|
|
116
|
+
*
|
|
117
|
+
* @returns the number represented buy this.bytes
|
|
118
|
+
*/
|
|
119
|
+
valueOf(): bigint {
|
|
120
|
+
const msb = BigInt(readUInt32BE(this.bytes.slice(0, 4), 0))
|
|
121
|
+
const lsb = BigInt(readUInt32BE(this.bytes.slice(4), 0))
|
|
122
|
+
return (msb << BigInt(32)) | lsb
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Get the bytes representation of the UInt64 object
|
|
127
|
+
*
|
|
128
|
+
* @returns 8 bytes representing the UInt64
|
|
129
|
+
*/
|
|
130
|
+
toBytes(): Uint8Array {
|
|
131
|
+
return this.bytes
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export { UInt64 }
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { UInt } from './uint'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { bytesToHex } from '@transia/isomorphic/utils'
|
|
4
|
+
import { writeUInt8 } from '../utils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Derived UInt class for serializing/deserializing 8 bit UInt
|
|
8
|
+
*/
|
|
9
|
+
class UInt8 extends UInt {
|
|
10
|
+
protected static readonly width: number = 8 / 8 // 1
|
|
11
|
+
static readonly defaultUInt8: UInt8 = new UInt8(new Uint8Array(UInt8.width))
|
|
12
|
+
|
|
13
|
+
constructor(bytes: Uint8Array) {
|
|
14
|
+
super(bytes ?? UInt8.defaultUInt8.bytes)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static fromParser(parser: BinaryParser): UInt {
|
|
18
|
+
return new UInt8(parser.read(UInt8.width))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Construct a UInt8 object from a number
|
|
23
|
+
*
|
|
24
|
+
* @param val UInt8 object or number
|
|
25
|
+
*/
|
|
26
|
+
static from<T extends UInt8 | number>(val: T): UInt8 {
|
|
27
|
+
if (val instanceof UInt8) {
|
|
28
|
+
return val
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof val === 'number') {
|
|
32
|
+
UInt8.checkUintRange(val, 0, 0xff)
|
|
33
|
+
|
|
34
|
+
const buf = new Uint8Array(UInt8.width)
|
|
35
|
+
writeUInt8(buf, val, 0)
|
|
36
|
+
return new UInt8(buf)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw new Error('Cannot construct UInt8 from given value')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* get the value of a UInt8 object
|
|
44
|
+
*
|
|
45
|
+
* @returns the number represented by this.bytes
|
|
46
|
+
*/
|
|
47
|
+
valueOf(): number {
|
|
48
|
+
return parseInt(bytesToHex(this.bytes), 16)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { UInt8 }
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Comparable } from './serialized-type'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Compare numbers and bigInts n1 and n2
|
|
5
|
+
*
|
|
6
|
+
* @param n1 First object to compare
|
|
7
|
+
* @param n2 Second object to compare
|
|
8
|
+
* @returns -1, 0, or 1, depending on how the two objects compare
|
|
9
|
+
*/
|
|
10
|
+
function compare(n1: number | bigint, n2: number | bigint): number {
|
|
11
|
+
return n1 < n2 ? -1 : n1 == n2 ? 0 : 1
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Base class for serializing and deserializing unsigned integers.
|
|
16
|
+
*/
|
|
17
|
+
abstract class UInt extends Comparable<UInt | number> {
|
|
18
|
+
protected static width: number
|
|
19
|
+
|
|
20
|
+
constructor(bytes: Uint8Array) {
|
|
21
|
+
super(bytes)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Overload of compareTo for Comparable
|
|
26
|
+
*
|
|
27
|
+
* @param other other UInt to compare this to
|
|
28
|
+
* @returns -1, 0, or 1 depending on how the objects relate to each other
|
|
29
|
+
*/
|
|
30
|
+
compareTo(other: UInt | number): number {
|
|
31
|
+
return compare(this.valueOf(), other.valueOf())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Convert a UInt object to JSON
|
|
36
|
+
*
|
|
37
|
+
* @returns number or string represented by this.bytes
|
|
38
|
+
*/
|
|
39
|
+
toJSON(): number | string {
|
|
40
|
+
const val = this.valueOf()
|
|
41
|
+
return typeof val === 'number' ? val : val.toString()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the value of the UInt represented by this.bytes
|
|
46
|
+
*
|
|
47
|
+
* @returns the value
|
|
48
|
+
*/
|
|
49
|
+
abstract valueOf(): number | bigint
|
|
50
|
+
|
|
51
|
+
static checkUintRange(val: number, min: number, max: number): void {
|
|
52
|
+
if (val < min || val > max) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Invalid ${this.constructor.name}: ${val} must be >= ${min} and <= ${max}`,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { UInt }
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { SerializedType } from './serialized-type'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { Hash256 } from './hash-256'
|
|
4
|
+
import { BytesList } from '../serdes/binary-serializer'
|
|
5
|
+
import { bytesToHex } from '@transia/isomorphic/utils'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* TypeGuard for Array<string>
|
|
9
|
+
*/
|
|
10
|
+
function isStrings(arg): arg is Array<string> {
|
|
11
|
+
return Array.isArray(arg) && (arg.length === 0 || typeof arg[0] === 'string')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Class for serializing and deserializing vectors of Hash256
|
|
16
|
+
*/
|
|
17
|
+
class Vector256 extends SerializedType {
|
|
18
|
+
constructor(bytes: Uint8Array) {
|
|
19
|
+
super(bytes)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Construct a Vector256 from a BinaryParser
|
|
24
|
+
*
|
|
25
|
+
* @param parser BinaryParser to
|
|
26
|
+
* @param hint length of the vector, in bytes, optional
|
|
27
|
+
* @returns a Vector256 object
|
|
28
|
+
*/
|
|
29
|
+
static fromParser(parser: BinaryParser, hint?: number): Vector256 {
|
|
30
|
+
const bytesList = new BytesList()
|
|
31
|
+
const bytes = hint ?? parser.size()
|
|
32
|
+
const hashes = bytes / 32
|
|
33
|
+
for (let i = 0; i < hashes; i++) {
|
|
34
|
+
Hash256.fromParser(parser).toBytesSink(bytesList)
|
|
35
|
+
}
|
|
36
|
+
return new Vector256(bytesList.toBytes())
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Construct a Vector256 object from an array of hashes
|
|
41
|
+
*
|
|
42
|
+
* @param value A Vector256 object or array of hex-strings representing Hash256's
|
|
43
|
+
* @returns a Vector256 object
|
|
44
|
+
*/
|
|
45
|
+
static from<T extends Vector256 | Array<string>>(value: T): Vector256 {
|
|
46
|
+
if (value instanceof Vector256) {
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (isStrings(value)) {
|
|
51
|
+
const bytesList = new BytesList()
|
|
52
|
+
value.forEach((hash) => {
|
|
53
|
+
Hash256.from(hash).toBytesSink(bytesList)
|
|
54
|
+
})
|
|
55
|
+
return new Vector256(bytesList.toBytes())
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
throw new Error('Cannot construct Vector256 from given value')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Return an Array of hex-strings represented by this.bytes
|
|
63
|
+
*
|
|
64
|
+
* @returns An Array of strings representing the Hash256 objects
|
|
65
|
+
*/
|
|
66
|
+
toJSON(): Array<string> {
|
|
67
|
+
if (this.bytes.byteLength % 32 !== 0) {
|
|
68
|
+
throw new Error('Invalid bytes for Vector256')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const result: Array<string> = []
|
|
72
|
+
for (let i = 0; i < this.bytes.byteLength; i += 32) {
|
|
73
|
+
result.push(bytesToHex(this.bytes.slice(i, i + 32)))
|
|
74
|
+
}
|
|
75
|
+
return result
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { Vector256 }
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
+
|
|
3
|
+
import { AccountID } from './account-id'
|
|
4
|
+
import { JsonObject, SerializedType } from './serialized-type'
|
|
5
|
+
import { Issue, IssueObject } from './issue'
|
|
6
|
+
import { concat } from '@transia/isomorphic/utils'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Interface for JSON objects that represent cross-chain bridges
|
|
10
|
+
*/
|
|
11
|
+
interface XChainBridgeObject extends JsonObject {
|
|
12
|
+
LockingChainDoor: string
|
|
13
|
+
LockingChainIssue: IssueObject | string
|
|
14
|
+
IssuingChainDoor: string
|
|
15
|
+
IssuingChainIssue: IssueObject | string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type guard for XChainBridgeObject
|
|
20
|
+
*/
|
|
21
|
+
function isXChainBridgeObject(arg): arg is XChainBridgeObject {
|
|
22
|
+
const keys = Object.keys(arg).sort()
|
|
23
|
+
return (
|
|
24
|
+
keys.length === 4 &&
|
|
25
|
+
keys[0] === 'IssuingChainDoor' &&
|
|
26
|
+
keys[1] === 'IssuingChainIssue' &&
|
|
27
|
+
keys[2] === 'LockingChainDoor' &&
|
|
28
|
+
keys[3] === 'LockingChainIssue'
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Class for serializing/deserializing XChainBridges
|
|
34
|
+
*/
|
|
35
|
+
class XChainBridge extends SerializedType {
|
|
36
|
+
static readonly ZERO_XCHAIN_BRIDGE: XChainBridge = new XChainBridge(
|
|
37
|
+
concat([
|
|
38
|
+
Uint8Array.from([0x14]),
|
|
39
|
+
new Uint8Array(40),
|
|
40
|
+
Uint8Array.from([0x14]),
|
|
41
|
+
new Uint8Array(40),
|
|
42
|
+
]),
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
|
|
46
|
+
[
|
|
47
|
+
{ name: 'LockingChainDoor', type: AccountID },
|
|
48
|
+
{ name: 'LockingChainIssue', type: Issue },
|
|
49
|
+
{ name: 'IssuingChainDoor', type: AccountID },
|
|
50
|
+
{ name: 'IssuingChainIssue', type: Issue },
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
constructor(bytes: Uint8Array) {
|
|
54
|
+
super(bytes ?? XChainBridge.ZERO_XCHAIN_BRIDGE.bytes)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Construct a cross-chain bridge from a JSON
|
|
59
|
+
*
|
|
60
|
+
* @param value XChainBridge or JSON to parse into an XChainBridge
|
|
61
|
+
* @returns An XChainBridge object
|
|
62
|
+
*/
|
|
63
|
+
static from<T extends XChainBridge | XChainBridgeObject>(
|
|
64
|
+
value: T,
|
|
65
|
+
): XChainBridge {
|
|
66
|
+
if (value instanceof XChainBridge) {
|
|
67
|
+
return value
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!isXChainBridgeObject(value)) {
|
|
71
|
+
throw new Error('Invalid type to construct an XChainBridge')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const bytes: Array<Uint8Array> = []
|
|
75
|
+
this.TYPE_ORDER.forEach((item) => {
|
|
76
|
+
const { name, type } = item
|
|
77
|
+
if (type === AccountID) {
|
|
78
|
+
bytes.push(Uint8Array.from([0x14]))
|
|
79
|
+
}
|
|
80
|
+
const object = type.from(value[name])
|
|
81
|
+
bytes.push(object.toBytes())
|
|
82
|
+
})
|
|
83
|
+
return new XChainBridge(concat(bytes))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read an XChainBridge from a BinaryParser
|
|
88
|
+
*
|
|
89
|
+
* @param parser BinaryParser to read the XChainBridge from
|
|
90
|
+
* @returns An XChainBridge object
|
|
91
|
+
*/
|
|
92
|
+
static fromParser(parser: BinaryParser): XChainBridge {
|
|
93
|
+
const bytes: Array<Uint8Array> = []
|
|
94
|
+
|
|
95
|
+
this.TYPE_ORDER.forEach((item) => {
|
|
96
|
+
const { type } = item
|
|
97
|
+
if (type === AccountID) {
|
|
98
|
+
parser.skip(1)
|
|
99
|
+
bytes.push(Uint8Array.from([0x14]))
|
|
100
|
+
}
|
|
101
|
+
const object = type.fromParser(parser)
|
|
102
|
+
bytes.push(object.toBytes())
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
return new XChainBridge(concat(bytes))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the JSON representation of this XChainBridge
|
|
110
|
+
*
|
|
111
|
+
* @returns the JSON interpretation of this.bytes
|
|
112
|
+
*/
|
|
113
|
+
toJSON(): XChainBridgeObject {
|
|
114
|
+
const parser = new BinaryParser(this.toString())
|
|
115
|
+
const json = {}
|
|
116
|
+
XChainBridge.TYPE_ORDER.forEach((item) => {
|
|
117
|
+
const { name, type } = item
|
|
118
|
+
if (type === AccountID) {
|
|
119
|
+
parser.skip(1)
|
|
120
|
+
}
|
|
121
|
+
const object = type.fromParser(parser).toJSON()
|
|
122
|
+
json[name] = object
|
|
123
|
+
})
|
|
124
|
+
return json as XChainBridgeObject
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { XChainBridge, XChainBridgeObject }
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Even though this comes from NodeJS it is valid in the browser
|
|
2
|
+
import TypedArray = NodeJS.TypedArray
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Writes value to array at the specified offset. The value must be a valid unsigned 8-bit integer.
|
|
6
|
+
* @param array Uint8Array to be written to
|
|
7
|
+
* @param value Number to be written to array.
|
|
8
|
+
* @param offset plus the number of bytes written.
|
|
9
|
+
*/
|
|
10
|
+
export function writeUInt8(
|
|
11
|
+
array: Uint8Array,
|
|
12
|
+
value: number,
|
|
13
|
+
offset: number,
|
|
14
|
+
): void {
|
|
15
|
+
value = Number(value)
|
|
16
|
+
array[offset] = value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 16-bit integer.
|
|
21
|
+
* @param array Uint8Array to be written to
|
|
22
|
+
* @param value Number to be written to array.
|
|
23
|
+
* @param offset plus the number of bytes written.
|
|
24
|
+
*/
|
|
25
|
+
export function writeUInt16BE(
|
|
26
|
+
array: Uint8Array,
|
|
27
|
+
value: number,
|
|
28
|
+
offset: number,
|
|
29
|
+
): void {
|
|
30
|
+
value = Number(value)
|
|
31
|
+
|
|
32
|
+
array[offset] = value >>> 8
|
|
33
|
+
array[offset + 1] = value
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 32-bit integer.
|
|
38
|
+
* @param array Uint8Array to be written to
|
|
39
|
+
* @param value Number to be written to array.
|
|
40
|
+
* @param offset plus the number of bytes written.
|
|
41
|
+
*/
|
|
42
|
+
export function writeUInt32BE(
|
|
43
|
+
array: Uint8Array,
|
|
44
|
+
value: number,
|
|
45
|
+
offset: number,
|
|
46
|
+
): void {
|
|
47
|
+
array[offset] = (value >>> 24) & 0xff
|
|
48
|
+
array[offset + 1] = (value >>> 16) & 0xff
|
|
49
|
+
array[offset + 2] = (value >>> 8) & 0xff
|
|
50
|
+
array[offset + 3] = value & 0xff
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Writes a signed 32-bit integer to a Uint8Array at the specified offset (big-endian).
|
|
55
|
+
*
|
|
56
|
+
* @param array - The Uint8Array to write to.
|
|
57
|
+
* @param value - The signed 32-bit integer to write.
|
|
58
|
+
* @param offset - The offset at which to write.
|
|
59
|
+
*/
|
|
60
|
+
export function writeInt32BE(
|
|
61
|
+
array: Uint8Array,
|
|
62
|
+
value: number,
|
|
63
|
+
offset: number,
|
|
64
|
+
): void {
|
|
65
|
+
new DataView(array.buffer, array.byteOffset, array.byteLength).setInt32(
|
|
66
|
+
offset,
|
|
67
|
+
value,
|
|
68
|
+
false,
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Writes a signed 64-bit integer (BigInt) to a Uint8Array at the specified offset (big-endian).
|
|
74
|
+
*
|
|
75
|
+
* @param array - The Uint8Array to write to.
|
|
76
|
+
* @param value - The signed 64-bit integer (BigInt) to write.
|
|
77
|
+
* @param offset - The offset at which to write.
|
|
78
|
+
*/
|
|
79
|
+
export function writeInt64BE(
|
|
80
|
+
array: Uint8Array,
|
|
81
|
+
value: bigint,
|
|
82
|
+
offset: number,
|
|
83
|
+
): void {
|
|
84
|
+
new DataView(array.buffer, array.byteOffset, array.byteLength).setBigInt64(
|
|
85
|
+
offset,
|
|
86
|
+
value,
|
|
87
|
+
false,
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
|
|
93
|
+
* @param array Uint8Array to read
|
|
94
|
+
* @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
|
|
95
|
+
*/
|
|
96
|
+
export function readUInt16BE(array: Uint8Array, offset: number): string {
|
|
97
|
+
return new DataView(array.buffer).getUint16(offset, false).toString(10)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
|
|
102
|
+
* @param array Uint8Array to read
|
|
103
|
+
* @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
|
|
104
|
+
*/
|
|
105
|
+
export function readUInt32BE(array: Uint8Array, offset: number): string {
|
|
106
|
+
return new DataView(array.buffer).getUint32(offset, false).toString(10)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Reads a signed 32-bit integer from a Uint8Array at the specified offset (big-endian).
|
|
111
|
+
*
|
|
112
|
+
* @param array - The Uint8Array to read from.
|
|
113
|
+
* @param offset - The offset at which to start reading.
|
|
114
|
+
* @returns The signed 32-bit integer.
|
|
115
|
+
*/
|
|
116
|
+
export function readInt32BE(array: Uint8Array, offset: number): number {
|
|
117
|
+
return new DataView(
|
|
118
|
+
array.buffer,
|
|
119
|
+
array.byteOffset,
|
|
120
|
+
array.byteLength,
|
|
121
|
+
).getInt32(offset, false)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Reads a signed 64-bit integer (BigInt) from a Uint8Array at the specified offset (big-endian).
|
|
126
|
+
*
|
|
127
|
+
* @param array - The Uint8Array to read from.
|
|
128
|
+
* @param offset - The offset at which to start reading.
|
|
129
|
+
* @returns The signed 64-bit integer (BigInt).
|
|
130
|
+
*/
|
|
131
|
+
export function readInt64BE(array: Uint8Array, offset: number): bigint {
|
|
132
|
+
return new DataView(
|
|
133
|
+
array.buffer,
|
|
134
|
+
array.byteOffset,
|
|
135
|
+
array.byteLength,
|
|
136
|
+
).getBigInt64(offset, false)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Compares two Uint8Array or ArrayBuffers
|
|
141
|
+
* @param a first array to compare
|
|
142
|
+
* @param b second array to compare
|
|
143
|
+
*/
|
|
144
|
+
export function equal(
|
|
145
|
+
a: Uint8Array | ArrayBuffer,
|
|
146
|
+
b: Uint8Array | ArrayBuffer,
|
|
147
|
+
): boolean {
|
|
148
|
+
const aUInt = a instanceof ArrayBuffer ? new Uint8Array(a, 0) : a
|
|
149
|
+
const bUInt = b instanceof ArrayBuffer ? new Uint8Array(b, 0) : b
|
|
150
|
+
if (aUInt.byteLength != bUInt.byteLength) return false
|
|
151
|
+
if (aligned32(aUInt) && aligned32(bUInt)) return compare32(aUInt, bUInt) === 0
|
|
152
|
+
if (aligned16(aUInt) && aligned16(bUInt)) return compare16(aUInt, bUInt) === 0
|
|
153
|
+
return compare8(aUInt, bUInt) === 0
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Compares two 8 bit aligned arrays
|
|
158
|
+
* @param a first array to compare
|
|
159
|
+
* @param b second array to compare
|
|
160
|
+
*/
|
|
161
|
+
function compare8(a, b) {
|
|
162
|
+
const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
|
|
163
|
+
const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
164
|
+
return compare(ua, ub)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Compares two 16 bit aligned arrays
|
|
169
|
+
* @param a first array to compare
|
|
170
|
+
* @param b second array to compare
|
|
171
|
+
*/
|
|
172
|
+
function compare16(a: Uint8Array, b: Uint8Array) {
|
|
173
|
+
const ua = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2)
|
|
174
|
+
const ub = new Uint16Array(b.buffer, b.byteOffset, b.byteLength / 2)
|
|
175
|
+
return compare(ua, ub)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Compares two 32 bit aligned arrays
|
|
180
|
+
* @param a first array to compare
|
|
181
|
+
* @param b second array to compare
|
|
182
|
+
*/
|
|
183
|
+
function compare32(a: Uint8Array, b: Uint8Array) {
|
|
184
|
+
const ua = new Uint32Array(a.buffer, a.byteOffset, a.byteLength / 4)
|
|
185
|
+
const ub = new Uint32Array(b.buffer, b.byteOffset, b.byteLength / 4)
|
|
186
|
+
return compare(ua, ub)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Compare two TypedArrays
|
|
191
|
+
* @param a first array to compare
|
|
192
|
+
* @param b second array to compare
|
|
193
|
+
*/
|
|
194
|
+
export function compare(a: TypedArray, b: TypedArray): 1 | -1 | 0 {
|
|
195
|
+
if (a.byteLength !== b.byteLength) {
|
|
196
|
+
throw new Error('Cannot compare arrays of different length')
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for (let i = 0; i < a.length - 1; i += 1) {
|
|
200
|
+
if (a[i] > b[i]) return 1
|
|
201
|
+
if (a[i] < b[i]) return -1
|
|
202
|
+
}
|
|
203
|
+
return 0
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Determine if TypedArray is 16 bit aligned
|
|
208
|
+
* @param array The array to check
|
|
209
|
+
*/
|
|
210
|
+
function aligned16(array: TypedArray) {
|
|
211
|
+
return array.byteOffset % 2 === 0 && array.byteLength % 2 === 0
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Determine if TypedArray is 32 bit aligned
|
|
216
|
+
* @param array The array to check
|
|
217
|
+
*/
|
|
218
|
+
function aligned32(array: TypedArray) {
|
|
219
|
+
return array.byteOffset % 4 === 0 && array.byteLength % 4 === 0
|
|
220
|
+
}
|