ripple-binary-codec 0.2.7 → 1.0.0-mpt-beta
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 +104 -2
- package/dist/binary.d.ts +90 -0
- package/dist/binary.js +128 -0
- package/dist/binary.js.map +1 -0
- package/dist/coretypes.d.ts +9 -0
- package/dist/coretypes.js +48 -0
- package/dist/coretypes.js.map +1 -0
- package/dist/enums/bytes.d.ts +25 -0
- package/dist/enums/bytes.js +63 -0
- package/dist/enums/bytes.js.map +1 -0
- package/dist/enums/constants.d.ts +4 -0
- package/dist/enums/constants.js +8 -0
- package/dist/enums/constants.js.map +1 -0
- package/dist/enums/definitions.json +2956 -0
- package/dist/enums/field.d.ts +28 -0
- package/dist/enums/field.js +58 -0
- package/dist/enums/field.js.map +1 -0
- package/dist/enums/index.d.ts +12 -0
- package/dist/enums/index.js +31 -0
- package/dist/enums/index.js.map +1 -0
- package/{distrib/npm → dist/enums/src}/enums/definitions.json +1709 -438
- package/dist/enums/utils-renumber.d.ts +101 -0
- package/dist/enums/utils-renumber.js +127 -0
- package/dist/enums/utils-renumber.js.map +1 -0
- package/dist/enums/xrpl-definitions-base.d.ts +44 -0
- package/dist/enums/xrpl-definitions-base.js +59 -0
- package/dist/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/enums/xrpl-definitions.d.ts +21 -0
- package/dist/enums/xrpl-definitions.js +29 -0
- package/dist/enums/xrpl-definitions.js.map +1 -0
- package/dist/hash-prefixes.d.ts +5 -0
- package/dist/hash-prefixes.js +41 -0
- package/dist/hash-prefixes.js.map +1 -0
- package/dist/hashes.d.ts +50 -0
- package/dist/hashes.js +76 -0
- package/dist/hashes.js.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/dist/ledger-hashes.d.ts +48 -0
- package/dist/ledger-hashes.js +141 -0
- package/dist/ledger-hashes.js.map +1 -0
- package/dist/quality.d.ts +21 -0
- package/dist/quality.js +42 -0
- package/dist/quality.js.map +1 -0
- package/dist/serdes/binary-parser.d.ts +100 -0
- package/dist/serdes/binary-parser.js +194 -0
- package/dist/serdes/binary-parser.js.map +1 -0
- package/dist/serdes/binary-serializer.d.ts +81 -0
- package/dist/serdes/binary-serializer.js +149 -0
- package/dist/serdes/binary-serializer.js.map +1 -0
- package/dist/shamap.d.ts +102 -0
- package/dist/shamap.js +172 -0
- package/dist/shamap.js.map +1 -0
- package/dist/src/binary.d.ts +90 -0
- package/dist/src/binary.js +128 -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 +4 -0
- package/dist/src/enums/constants.js +8 -0
- package/dist/src/enums/constants.js.map +1 -0
- package/dist/src/enums/definitions.json +2956 -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 +44 -0
- package/dist/src/enums/xrpl-definitions-base.js +59 -0
- package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/src/enums/xrpl-definitions.d.ts +21 -0
- package/dist/src/enums/xrpl-definitions.js +29 -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 +41 -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 +64 -0
- package/dist/src/index.js +123 -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 +40 -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 +73 -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 +64 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +38 -0
- package/dist/src/types/issue.js +81 -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 +83 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-object.d.ts +32 -0
- package/dist/src/types/st-object.js +155 -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 +37 -0
- package/dist/src/types/uint-64.js +86 -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 +47 -0
- package/dist/src/utils.js +137 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/account-id.d.ts +35 -0
- package/dist/types/account-id.js +69 -0
- package/dist/types/account-id.js.map +1 -0
- package/dist/types/amount.d.ts +91 -0
- package/dist/types/amount.js +289 -0
- package/dist/types/amount.js.map +1 -0
- package/dist/types/blob.d.ts +24 -0
- package/dist/types/blob.js +40 -0
- package/dist/types/blob.js.map +1 -0
- package/dist/types/currency.d.ts +28 -0
- package/dist/types/currency.js +127 -0
- package/dist/types/currency.js.map +1 -0
- package/dist/types/hash-128.d.ts +16 -0
- package/dist/types/hash-128.js +32 -0
- package/dist/types/hash-128.js.map +1 -0
- package/dist/types/hash-160.d.ts +10 -0
- package/dist/types/hash-160.js +19 -0
- package/dist/types/hash-160.js.map +1 -0
- 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 +10 -0
- package/dist/types/hash-256.js +16 -0
- package/dist/types/hash-256.js.map +1 -0
- package/dist/types/hash.d.ts +40 -0
- package/dist/types/hash.js +73 -0
- package/dist/types/hash.js.map +1 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.js +64 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/issue.d.ts +38 -0
- package/dist/types/issue.js +81 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/path-set.d.ts +36 -0
- package/dist/types/path-set.js +233 -0
- package/dist/types/path-set.js.map +1 -0
- package/dist/types/serialized-type.d.ts +70 -0
- package/dist/types/serialized-type.js +105 -0
- package/dist/types/serialized-type.js.map +1 -0
- package/dist/types/st-array.d.ts +31 -0
- package/dist/types/st-array.js +83 -0
- package/dist/types/st-array.js.map +1 -0
- package/dist/types/st-object.d.ts +32 -0
- package/dist/types/st-object.js +155 -0
- package/dist/types/st-object.js.map +1 -0
- package/dist/types/uint-16.d.ts +24 -0
- package/dist/types/uint-16.js +45 -0
- package/dist/types/uint-16.js.map +1 -0
- package/dist/types/uint-32.d.ts +24 -0
- package/dist/types/uint-32.js +50 -0
- package/dist/types/uint-32.js.map +1 -0
- package/dist/types/uint-64.d.ts +37 -0
- package/dist/types/uint-64.js +86 -0
- package/dist/types/uint-64.js.map +1 -0
- package/dist/types/uint-8.d.ts +24 -0
- package/dist/types/uint-8.js +46 -0
- package/dist/types/uint-8.js.map +1 -0
- package/dist/types/uint.d.ts +29 -0
- package/dist/types/uint.js +47 -0
- package/dist/types/uint.js.map +1 -0
- package/dist/types/vector-256.d.ts +30 -0
- package/dist/types/vector-256.js +73 -0
- package/dist/types/vector-256.js.map +1 -0
- 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 +47 -0
- package/dist/utils.js +137 -0
- package/dist/utils.js.map +1 -0
- package/package.json +28 -33
- package/src/README.md +5 -0
- package/src/binary.ts +194 -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 +4 -0
- package/src/enums/definitions.json +2961 -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 +111 -0
- package/src/enums/xrpl-definitions.ts +32 -0
- package/src/hash-prefixes.ts +40 -0
- package/src/hashes.ts +75 -0
- package/src/index.ts +153 -0
- package/src/ledger-hashes.ts +191 -0
- package/src/quality.ts +38 -0
- package/src/serdes/binary-parser.ts +224 -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 +43 -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 +83 -0
- package/src/types/index.ts +66 -0
- package/src/types/issue.ts +96 -0
- package/src/types/path-set.ts +290 -0
- package/src/types/serialized-type.ts +122 -0
- package/src/types/st-array.ts +107 -0
- package/src/types/st-object.ts +192 -0
- package/src/types/uint-16.ts +53 -0
- package/src/types/uint-32.ts +59 -0
- package/src/types/uint-64.ts +104 -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 +152 -0
- package/HISTORY.md +0 -66
- package/distrib/npm/binary.js +0 -68
- package/distrib/npm/coretypes.js +0 -22
- package/distrib/npm/enums/index.js +0 -125
- package/distrib/npm/enums/utils-renumber.js +0 -134
- package/distrib/npm/hash-prefixes.js +0 -30
- package/distrib/npm/hashes.js +0 -42
- package/distrib/npm/index.js +0 -72
- package/distrib/npm/ledger-hashes.js +0 -71
- package/distrib/npm/quality.js +0 -20
- package/distrib/npm/serdes/binary-parser.js +0 -98
- package/distrib/npm/serdes/binary-serializer.js +0 -106
- package/distrib/npm/shamap.js +0 -107
- package/distrib/npm/types/account-id.js +0 -41
- package/distrib/npm/types/amount.js +0 -215
- package/distrib/npm/types/blob.js +0 -28
- package/distrib/npm/types/currency.js +0 -91
- package/distrib/npm/types/hash-128.js +0 -10
- package/distrib/npm/types/hash-160.js +0 -10
- package/distrib/npm/types/hash-256.js +0 -15
- package/distrib/npm/types/hash.js +0 -45
- package/distrib/npm/types/index.js +0 -45
- package/distrib/npm/types/path-set.js +0 -112
- package/distrib/npm/types/serialized-type.js +0 -63
- package/distrib/npm/types/st-array.js +0 -37
- package/distrib/npm/types/st-object.js +0 -65
- package/distrib/npm/types/uint-16.js +0 -10
- package/distrib/npm/types/uint-32.js +0 -10
- package/distrib/npm/types/uint-64.js +0 -49
- package/distrib/npm/types/uint-8.js +0 -10
- package/distrib/npm/types/uint.js +0 -60
- package/distrib/npm/types/vector-256.js +0 -31
- package/distrib/npm/utils/bytes-utils.js +0 -112
- package/distrib/npm/utils/make-class.js +0 -83
- package/test/amount-test.js +0 -43
- package/test/binary-json-test.js +0 -43
- package/test/binary-parser-test.js +0 -341
- package/test/binary-serializer-test.js +0 -194
- package/test/bytes-utils-test.js +0 -72
- 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 -3721
- 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/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/hash-test.js +0 -58
- package/test/ledger-test.js +0 -26
- package/test/mocha.opts +0 -1
- package/test/quality-test.js +0 -16
- package/test/shamap-test.js +0 -88
- package/test/signing-data-encoding-test.js +0 -120
- package/test/tx-encode-decode-test.js +0 -124
- package/test/types-test.js +0 -38
- package/test/uint-test.js +0 -38
- package/test/utils.js +0 -93
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Hash160 } from './hash-160'
|
|
2
|
+
import { bytesToHex, hexToBytes, hexToString } from '@xrplf/isomorphic/utils'
|
|
3
|
+
|
|
4
|
+
const XRP_HEX_REGEX = /^0{40}$/
|
|
5
|
+
const ISO_REGEX = /^[A-Z0-9a-z?!@#$%^&*(){}[\]|]{3}$/
|
|
6
|
+
const HEX_REGEX = /^[A-F0-9]{40}$/
|
|
7
|
+
// eslint-disable-next-line no-control-regex
|
|
8
|
+
const STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert an ISO code to a currency bytes representation
|
|
12
|
+
*/
|
|
13
|
+
function isoToBytes(iso: string): Uint8Array {
|
|
14
|
+
const bytes = new Uint8Array(20)
|
|
15
|
+
if (iso !== 'XRP') {
|
|
16
|
+
const isoBytes = iso.split('').map((c) => c.charCodeAt(0))
|
|
17
|
+
bytes.set(isoBytes, 12)
|
|
18
|
+
}
|
|
19
|
+
return bytes
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Tests if ISO is a valid iso code
|
|
24
|
+
*/
|
|
25
|
+
function isIsoCode(iso: string): boolean {
|
|
26
|
+
return ISO_REGEX.test(iso)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isoCodeFromHex(code: Uint8Array): string | null {
|
|
30
|
+
const iso = hexToString(bytesToHex(code))
|
|
31
|
+
if (iso === 'XRP') {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
if (isIsoCode(iso)) {
|
|
35
|
+
return iso
|
|
36
|
+
}
|
|
37
|
+
return null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Tests if hex is a valid hex-string
|
|
42
|
+
*/
|
|
43
|
+
function isHex(hex: string): boolean {
|
|
44
|
+
return HEX_REGEX.test(hex)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Tests if a string is a valid representation of a currency
|
|
49
|
+
*/
|
|
50
|
+
function isStringRepresentation(input: string): boolean {
|
|
51
|
+
return input.length === 3 || isHex(input)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Tests if a Uint8Array is a valid representation of a currency
|
|
56
|
+
*/
|
|
57
|
+
function isBytesArray(bytes: Uint8Array): boolean {
|
|
58
|
+
return bytes.byteLength === 20
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Ensures that a value is a valid representation of a currency
|
|
63
|
+
*/
|
|
64
|
+
function isValidRepresentation(input: Uint8Array | string): boolean {
|
|
65
|
+
return input instanceof Uint8Array
|
|
66
|
+
? isBytesArray(input)
|
|
67
|
+
: isStringRepresentation(input)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Generate bytes from a string or UInt8Array representation of a currency
|
|
72
|
+
*/
|
|
73
|
+
function bytesFromRepresentation(input: string): Uint8Array {
|
|
74
|
+
if (!isValidRepresentation(input)) {
|
|
75
|
+
throw new Error(`Unsupported Currency representation: ${input}`)
|
|
76
|
+
}
|
|
77
|
+
return input.length === 3 ? isoToBytes(input) : hexToBytes(input)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Class defining how to encode and decode Currencies
|
|
82
|
+
*/
|
|
83
|
+
class Currency extends Hash160 {
|
|
84
|
+
static readonly XRP = new Currency(new Uint8Array(20))
|
|
85
|
+
private readonly _iso: string | null
|
|
86
|
+
|
|
87
|
+
constructor(byteBuf: Uint8Array) {
|
|
88
|
+
super(byteBuf ?? Currency.XRP.bytes)
|
|
89
|
+
const hex = bytesToHex(this.bytes)
|
|
90
|
+
|
|
91
|
+
if (XRP_HEX_REGEX.test(hex)) {
|
|
92
|
+
this._iso = 'XRP'
|
|
93
|
+
} else if (STANDARD_FORMAT_HEX_REGEX.test(hex)) {
|
|
94
|
+
this._iso = isoCodeFromHex(this.bytes.slice(12, 15))
|
|
95
|
+
} else {
|
|
96
|
+
this._iso = null
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Return the ISO code of this currency
|
|
102
|
+
*
|
|
103
|
+
* @returns ISO code if it exists, else null
|
|
104
|
+
*/
|
|
105
|
+
iso(): string | null {
|
|
106
|
+
return this._iso
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Constructs a Currency object
|
|
111
|
+
*
|
|
112
|
+
* @param val Currency object or a string representation of a currency
|
|
113
|
+
*/
|
|
114
|
+
static from<T extends Hash160 | string>(value: T): Currency {
|
|
115
|
+
if (value instanceof Currency) {
|
|
116
|
+
return value
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (typeof value === 'string') {
|
|
120
|
+
return new Currency(bytesFromRepresentation(value))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
throw new Error('Cannot construct Currency from value given')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Gets the JSON representation of a currency
|
|
128
|
+
*
|
|
129
|
+
* @returns JSON representation
|
|
130
|
+
*/
|
|
131
|
+
toJSON(): string {
|
|
132
|
+
const iso = this.iso()
|
|
133
|
+
if (iso !== null) {
|
|
134
|
+
return iso
|
|
135
|
+
}
|
|
136
|
+
return bytesToHex(this.bytes)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { Currency }
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
import { bytesToHex } from '@xrplf/isomorphic/utils'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hash with a width of 128 bits
|
|
6
|
+
*/
|
|
7
|
+
class Hash128 extends Hash {
|
|
8
|
+
static readonly width = 16
|
|
9
|
+
static readonly ZERO_128: Hash128 = new Hash128(new Uint8Array(Hash128.width))
|
|
10
|
+
|
|
11
|
+
constructor(bytes: Uint8Array) {
|
|
12
|
+
if (bytes && bytes.byteLength === 0) {
|
|
13
|
+
bytes = Hash128.ZERO_128.bytes
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
super(bytes ?? Hash128.ZERO_128.bytes)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get the hex representation of a hash-128 bytes, allowing unset
|
|
21
|
+
*
|
|
22
|
+
* @returns hex String of this.bytes
|
|
23
|
+
*/
|
|
24
|
+
toHex(): string {
|
|
25
|
+
const hex = bytesToHex(this.toBytes())
|
|
26
|
+
if (/^0+$/.exec(hex)) {
|
|
27
|
+
return ''
|
|
28
|
+
}
|
|
29
|
+
return hex
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { Hash128 }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hash with a width of 160 bits
|
|
5
|
+
*/
|
|
6
|
+
class Hash160 extends Hash {
|
|
7
|
+
static readonly width = 20
|
|
8
|
+
static readonly ZERO_160: Hash160 = new Hash160(new Uint8Array(Hash160.width))
|
|
9
|
+
|
|
10
|
+
constructor(bytes?: Uint8Array) {
|
|
11
|
+
if (bytes && bytes.byteLength === 0) {
|
|
12
|
+
bytes = Hash160.ZERO_160.bytes
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
super(bytes ?? Hash160.ZERO_160.bytes)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { Hash160 }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hash with a width of 192 bits
|
|
5
|
+
*/
|
|
6
|
+
class Hash192 extends Hash {
|
|
7
|
+
static readonly width = 24
|
|
8
|
+
static readonly ZERO_192: Hash192 = new Hash192(new Uint8Array(Hash192.width))
|
|
9
|
+
|
|
10
|
+
constructor(bytes?: Uint8Array) {
|
|
11
|
+
if (bytes && bytes.byteLength === 0) {
|
|
12
|
+
bytes = Hash192.ZERO_192.bytes
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
super(bytes ?? Hash192.ZERO_192.bytes)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { Hash192 }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hash with a width of 256 bits
|
|
5
|
+
*/
|
|
6
|
+
class Hash256 extends Hash {
|
|
7
|
+
static readonly width = 32
|
|
8
|
+
static readonly ZERO_256 = new Hash256(new Uint8Array(Hash256.width))
|
|
9
|
+
|
|
10
|
+
constructor(bytes: Uint8Array) {
|
|
11
|
+
super(bytes ?? Hash256.ZERO_256.bytes)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { Hash256 }
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Comparable } from './serialized-type'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { hexToBytes } from '@xrplf/isomorphic/utils'
|
|
4
|
+
import { compare } from '../utils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base class defining how to encode and decode hashes
|
|
8
|
+
*/
|
|
9
|
+
class Hash extends Comparable<Hash | string> {
|
|
10
|
+
static readonly width: number
|
|
11
|
+
|
|
12
|
+
constructor(bytes: Uint8Array) {
|
|
13
|
+
super(bytes)
|
|
14
|
+
if (this.bytes.length !== (this.constructor as typeof Hash).width) {
|
|
15
|
+
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Construct a Hash object from an existing Hash object or a hex-string
|
|
21
|
+
*
|
|
22
|
+
* @param value A hash object or hex-string of a hash
|
|
23
|
+
*/
|
|
24
|
+
static from<T extends Hash | string>(value: T): Hash {
|
|
25
|
+
if (value instanceof this) {
|
|
26
|
+
return value
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
return new this(hexToBytes(value))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
throw new Error('Cannot construct Hash from given value')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read a Hash object from a BinaryParser
|
|
38
|
+
*
|
|
39
|
+
* @param parser BinaryParser to read the hash from
|
|
40
|
+
* @param hint length of the bytes to read, optional
|
|
41
|
+
*/
|
|
42
|
+
static fromParser(parser: BinaryParser, hint?: number): Hash {
|
|
43
|
+
return new this(parser.read(hint ?? this.width))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Overloaded operator for comparing two hash objects
|
|
48
|
+
*
|
|
49
|
+
* @param other The Hash to compare this to
|
|
50
|
+
*/
|
|
51
|
+
compareTo(other: Hash): number {
|
|
52
|
+
return compare(
|
|
53
|
+
this.bytes,
|
|
54
|
+
(this.constructor as typeof Hash).from(other).bytes,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @returns the hex-string representation of this Hash
|
|
60
|
+
*/
|
|
61
|
+
toString(): string {
|
|
62
|
+
return this.toHex()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns four bits at the specified depth within a hash
|
|
67
|
+
*
|
|
68
|
+
* @param depth The depth of the four bits
|
|
69
|
+
* @returns The number represented by the four bits
|
|
70
|
+
*/
|
|
71
|
+
nibblet(depth: number): number {
|
|
72
|
+
const byteIx = depth > 0 ? (depth / 2) | 0 : 0
|
|
73
|
+
let b = this.bytes[byteIx]
|
|
74
|
+
if (depth % 2 === 0) {
|
|
75
|
+
b = (b & 0xf0) >>> 4
|
|
76
|
+
} else {
|
|
77
|
+
b = b & 0x0f
|
|
78
|
+
}
|
|
79
|
+
return b
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { Hash }
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { AccountID } from './account-id'
|
|
2
|
+
import { Amount } from './amount'
|
|
3
|
+
import { Blob } from './blob'
|
|
4
|
+
import { Currency } from './currency'
|
|
5
|
+
import { Hash128 } from './hash-128'
|
|
6
|
+
import { Hash160 } from './hash-160'
|
|
7
|
+
import { Hash192 } from './hash-192'
|
|
8
|
+
import { Hash256 } from './hash-256'
|
|
9
|
+
import { Issue } from './issue'
|
|
10
|
+
import { PathSet } from './path-set'
|
|
11
|
+
import { STArray } from './st-array'
|
|
12
|
+
import { STObject } from './st-object'
|
|
13
|
+
import { UInt16 } from './uint-16'
|
|
14
|
+
import { UInt32 } from './uint-32'
|
|
15
|
+
import { UInt64 } from './uint-64'
|
|
16
|
+
import { UInt8 } from './uint-8'
|
|
17
|
+
import { Vector256 } from './vector-256'
|
|
18
|
+
import { XChainBridge } from './xchain-bridge'
|
|
19
|
+
import { type SerializedType } from './serialized-type'
|
|
20
|
+
import { DEFAULT_DEFINITIONS } from '../enums'
|
|
21
|
+
|
|
22
|
+
const coreTypes: Record<string, typeof SerializedType> = {
|
|
23
|
+
AccountID,
|
|
24
|
+
Amount,
|
|
25
|
+
Blob,
|
|
26
|
+
Currency,
|
|
27
|
+
Hash128,
|
|
28
|
+
Hash160,
|
|
29
|
+
Hash192,
|
|
30
|
+
Hash256,
|
|
31
|
+
Issue,
|
|
32
|
+
PathSet,
|
|
33
|
+
STArray,
|
|
34
|
+
STObject,
|
|
35
|
+
UInt8,
|
|
36
|
+
UInt16,
|
|
37
|
+
UInt32,
|
|
38
|
+
UInt64,
|
|
39
|
+
Vector256,
|
|
40
|
+
XChainBridge,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Ensures that the DEFAULT_DEFINITIONS object connects these types to fields for serializing/deserializing
|
|
44
|
+
// This is done here instead of in enums/index.ts to avoid a circular dependency
|
|
45
|
+
// because some of the above types depend on BinarySerializer which depends on enums/index.ts.
|
|
46
|
+
DEFAULT_DEFINITIONS.associateTypes(coreTypes)
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
coreTypes,
|
|
50
|
+
AccountID,
|
|
51
|
+
Amount,
|
|
52
|
+
Blob,
|
|
53
|
+
Currency,
|
|
54
|
+
Hash128,
|
|
55
|
+
Hash160,
|
|
56
|
+
Hash192,
|
|
57
|
+
Hash256,
|
|
58
|
+
PathSet,
|
|
59
|
+
STArray,
|
|
60
|
+
STObject,
|
|
61
|
+
UInt8,
|
|
62
|
+
UInt16,
|
|
63
|
+
UInt32,
|
|
64
|
+
UInt64,
|
|
65
|
+
Vector256,
|
|
66
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { concat } from '@xrplf/isomorphic/utils'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
|
|
4
|
+
import { AccountID } from './account-id'
|
|
5
|
+
import { Currency } from './currency'
|
|
6
|
+
import { JsonObject, SerializedType } from './serialized-type'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Interface for JSON objects that represent amounts
|
|
10
|
+
*/
|
|
11
|
+
interface IssueObject extends JsonObject {
|
|
12
|
+
currency: string
|
|
13
|
+
issuer?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Type guard for AmountObject
|
|
18
|
+
*/
|
|
19
|
+
function isIssueObject(arg): arg is IssueObject {
|
|
20
|
+
const keys = Object.keys(arg).sort()
|
|
21
|
+
if (keys.length === 1) {
|
|
22
|
+
return keys[0] === 'currency'
|
|
23
|
+
}
|
|
24
|
+
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Class for serializing/Deserializing Amounts
|
|
29
|
+
*/
|
|
30
|
+
class Issue extends SerializedType {
|
|
31
|
+
static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(new Uint8Array(20))
|
|
32
|
+
|
|
33
|
+
constructor(bytes: Uint8Array) {
|
|
34
|
+
super(bytes ?? Issue.ZERO_ISSUED_CURRENCY.bytes)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Construct an amount from an IOU or string amount
|
|
39
|
+
*
|
|
40
|
+
* @param value An Amount, object representing an IOU, or a string
|
|
41
|
+
* representing an integer amount
|
|
42
|
+
* @returns An Amount object
|
|
43
|
+
*/
|
|
44
|
+
static from<T extends Issue | IssueObject>(value: T): Issue {
|
|
45
|
+
if (value instanceof Issue) {
|
|
46
|
+
return value
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (isIssueObject(value)) {
|
|
50
|
+
const currency = Currency.from(value.currency).toBytes()
|
|
51
|
+
if (value.issuer == null) {
|
|
52
|
+
return new Issue(currency)
|
|
53
|
+
}
|
|
54
|
+
const issuer = AccountID.from(value.issuer).toBytes()
|
|
55
|
+
return new Issue(concat([currency, issuer]))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
throw new Error('Invalid type to construct an Amount')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Read an amount from a BinaryParser
|
|
63
|
+
*
|
|
64
|
+
* @param parser BinaryParser to read the Amount from
|
|
65
|
+
* @returns An Amount object
|
|
66
|
+
*/
|
|
67
|
+
static fromParser(parser: BinaryParser): Issue {
|
|
68
|
+
const currency = parser.read(20)
|
|
69
|
+
if (new Currency(currency).toJSON() === 'XRP') {
|
|
70
|
+
return new Issue(currency)
|
|
71
|
+
}
|
|
72
|
+
const currencyAndIssuer = [currency, parser.read(20)]
|
|
73
|
+
return new Issue(concat(currencyAndIssuer))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get the JSON representation of this Amount
|
|
78
|
+
*
|
|
79
|
+
* @returns the JSON interpretation of this.bytes
|
|
80
|
+
*/
|
|
81
|
+
toJSON(): IssueObject {
|
|
82
|
+
const parser = new BinaryParser(this.toString())
|
|
83
|
+
const currency = Currency.fromParser(parser) as Currency
|
|
84
|
+
if (currency.toJSON() === 'XRP') {
|
|
85
|
+
return { currency: currency.toJSON() }
|
|
86
|
+
}
|
|
87
|
+
const issuer = AccountID.fromParser(parser) as AccountID
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
currency: currency.toJSON(),
|
|
91
|
+
issuer: issuer.toJSON(),
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { Issue, IssueObject }
|