@transia/ripple-binary-codec 1.4.6-alpha.8 → 2.5.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/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 +3 -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 +1650 -964
- 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 +1652 -966
- 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 +7 -8
- package/dist/serdes/binary-serializer.js +14 -37
- 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 +65 -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 +3404 -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 +37 -0
- package/dist/src/types/account-id.js +73 -0
- package/dist/src/types/account-id.js.map +1 -0
- package/dist/src/types/amount.d.ts +92 -0
- package/dist/src/types/amount.js +292 -0
- package/dist/src/types/amount.js.map +1 -0
- package/dist/src/types/blob.d.ts +25 -0
- package/dist/src/types/blob.js +46 -0
- package/dist/src/types/blob.js.map +1 -0
- package/dist/src/types/currency.d.ts +30 -0
- package/dist/src/types/currency.js +131 -0
- package/dist/src/types/currency.js.map +1 -0
- package/dist/src/types/data.d.ts +104 -0
- package/dist/src/types/data.js +387 -0
- package/dist/src/types/data.js.map +1 -0
- package/dist/src/types/dataType.d.ts +94 -0
- package/dist/src/types/dataType.js +145 -0
- package/dist/src/types/dataType.js.map +1 -0
- package/dist/src/types/hash-128.d.ts +18 -0
- package/dist/src/types/hash-128.js +36 -0
- package/dist/src/types/hash-128.js.map +1 -0
- package/dist/src/types/hash-160.d.ts +12 -0
- package/dist/src/types/hash-160.js +23 -0
- package/dist/src/types/hash-160.js.map +1 -0
- package/dist/src/types/hash-192.d.ts +12 -0
- package/dist/src/types/hash-192.js +23 -0
- package/dist/src/types/hash-192.js.map +1 -0
- package/dist/src/types/hash-256.d.ts +12 -0
- package/dist/src/types/hash-256.js +20 -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 +21 -0
- package/dist/src/types/index.js +72 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +49 -0
- package/dist/src/types/issue.js +107 -0
- package/dist/src/types/issue.js.map +1 -0
- package/dist/src/types/json.d.ts +169 -0
- package/dist/src/types/json.js +529 -0
- package/dist/src/types/json.js.map +1 -0
- package/dist/src/types/path-set.d.ts +37 -0
- package/dist/src/types/path-set.js +236 -0
- package/dist/src/types/path-set.js.map +1 -0
- package/dist/src/types/serialized-type.d.ts +117 -0
- package/dist/src/types/serialized-type.js +242 -0
- package/dist/src/types/serialized-type.js.map +1 -0
- package/dist/src/types/st-array.d.ts +32 -0
- package/dist/src/types/st-array.js +89 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-number.d.ts +55 -0
- package/dist/src/types/st-number.js +212 -0
- package/dist/src/types/st-number.js.map +1 -0
- package/dist/src/types/st-object.d.ts +33 -0
- package/dist/src/types/st-object.js +176 -0
- package/dist/src/types/st-object.js.map +1 -0
- package/dist/src/types/uint-16.d.ts +26 -0
- package/dist/src/types/uint-16.js +49 -0
- package/dist/src/types/uint-16.js.map +1 -0
- package/dist/src/types/uint-32.d.ts +26 -0
- package/dist/src/types/uint-32.js +54 -0
- package/dist/src/types/uint-32.js.map +1 -0
- package/dist/src/types/uint-64.d.ts +40 -0
- package/dist/src/types/uint-64.js +112 -0
- package/dist/src/types/uint-64.js.map +1 -0
- package/dist/src/types/uint-8.d.ts +26 -0
- package/dist/src/types/uint-8.js +50 -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 +31 -0
- package/dist/src/types/vector-256.js +76 -0
- package/dist/src/types/vector-256.js.map +1 -0
- package/dist/src/types/xchain-bridge.d.ts +45 -0
- package/dist/src/types/xchain-bridge.js +105 -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 +3 -2
- package/dist/types/account-id.js +8 -6
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +33 -9
- package/dist/types/amount.js +128 -43
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +3 -3
- package/dist/types/blob.js +8 -2
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +3 -2
- package/dist/types/currency.js +17 -13
- package/dist/types/currency.js.map +1 -1
- package/dist/types/data.d.ts +104 -0
- package/dist/types/data.js +387 -0
- package/dist/types/data.js.map +1 -0
- package/dist/types/dataType.d.ts +94 -0
- package/dist/types/dataType.js +145 -0
- package/dist/types/dataType.js.map +1 -0
- package/dist/types/hash-128.d.ts +3 -2
- package/dist/types/hash-128.js +7 -3
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +3 -2
- package/dist/types/hash-160.js +5 -2
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-192.d.ts +12 -0
- package/dist/types/hash-192.js +23 -0
- package/dist/types/hash-192.js.map +1 -0
- package/dist/types/hash-256.d.ts +3 -2
- package/dist/types/hash-256.js +5 -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 +4 -1
- package/dist/types/index.js +17 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +49 -0
- package/dist/types/issue.js +107 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/json.d.ts +169 -0
- package/dist/types/json.js +529 -0
- package/dist/types/json.js.map +1 -0
- package/dist/types/path-set.d.ts +2 -1
- package/dist/types/path-set.js +15 -12
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +70 -16
- package/dist/types/serialized-type.js +153 -8
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +7 -3
- package/dist/types/st-array.js +20 -11
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-number.d.ts +55 -0
- package/dist/types/st-number.js +212 -0
- package/dist/types/st-number.js.map +1 -0
- package/dist/types/st-object.d.ts +2 -1
- package/dist/types/st-object.js +35 -8
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +3 -2
- package/dist/types/uint-16.js +10 -5
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +3 -2
- package/dist/types/uint-32.js +11 -6
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +8 -7
- package/dist/types/uint-64.js +49 -24
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +3 -2
- package/dist/types/uint-8.js +11 -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 +3 -3
- package/dist/types/vector-256.js +5 -4
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +45 -0
- package/dist/types/xchain-bridge.js +105 -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 +80 -0
- package/src/enums/constants.ts +5 -0
- package/src/enums/definitions.json +3409 -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 +189 -0
- package/src/types/account-id.ts +91 -0
- package/src/types/amount.ts +366 -0
- package/src/types/blob.ts +50 -0
- package/src/types/currency.ts +145 -0
- package/src/types/data.ts +496 -0
- package/src/types/dataType.ts +178 -0
- package/src/types/hash-128.ts +38 -0
- package/src/types/hash-160.ts +24 -0
- package/src/types/hash-192.ts +24 -0
- package/src/types/hash-256.ts +20 -0
- package/src/types/hash.ts +86 -0
- package/src/types/index.ts +74 -0
- package/src/types/issue.ts +140 -0
- package/src/types/json.ts +641 -0
- package/src/types/path-set.ts +294 -0
- package/src/types/serialized-type.ts +271 -0
- package/src/types/st-array.ts +117 -0
- package/src/types/st-number.ts +238 -0
- package/src/types/st-object.ts +219 -0
- package/src/types/uint-16.ts +58 -0
- package/src/types/uint-32.ts +64 -0
- package/src/types/uint-64.ts +141 -0
- package/src/types/uint-8.ts +57 -0
- package/src/types/uint.ts +60 -0
- package/src/types/vector-256.ts +83 -0
- package/src/types/xchain-bridge.ts +132 -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,366 @@
|
|
|
1
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
+
|
|
3
|
+
import { AccountID } from './account-id'
|
|
4
|
+
import { Currency } from './currency'
|
|
5
|
+
import { JsonObject, SerializedType, SerializedTypeID } from './serialized-type'
|
|
6
|
+
import BigNumber from 'bignumber.js'
|
|
7
|
+
import { bytesToHex, concat, hexToBytes } from '@transia/isomorphic/utils'
|
|
8
|
+
import { readUInt32BE, writeUInt32BE } from '../utils'
|
|
9
|
+
import { Hash192 } from './hash-192'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Constants for validating amounts
|
|
13
|
+
*/
|
|
14
|
+
const MIN_IOU_EXPONENT = -96
|
|
15
|
+
const MAX_IOU_EXPONENT = 80
|
|
16
|
+
const MAX_IOU_PRECISION = 16
|
|
17
|
+
const MAX_DROPS = new BigNumber('1e17')
|
|
18
|
+
const MIN_XRP = new BigNumber('1e-6')
|
|
19
|
+
const mask = BigInt(0x00000000ffffffff)
|
|
20
|
+
const mptMask = BigInt(0x8000000000000000)
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* BigNumber configuration for Amount IOUs
|
|
24
|
+
*/
|
|
25
|
+
BigNumber.config({
|
|
26
|
+
EXPONENTIAL_AT: [
|
|
27
|
+
MIN_IOU_EXPONENT - MAX_IOU_PRECISION,
|
|
28
|
+
MAX_IOU_EXPONENT + MAX_IOU_PRECISION,
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
interface AmountObjectIOU extends JsonObject {
|
|
33
|
+
value: string
|
|
34
|
+
currency: string
|
|
35
|
+
issuer: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface AmountObjectMPT extends JsonObject {
|
|
39
|
+
value: string
|
|
40
|
+
mpt_issuance_id: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Interface for JSON objects that represent amounts
|
|
45
|
+
*/
|
|
46
|
+
type AmountObject = AmountObjectIOU | AmountObjectMPT
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Type guard for AmountObjectIOU
|
|
50
|
+
*/
|
|
51
|
+
function isAmountObjectIOU(arg): arg is AmountObjectIOU {
|
|
52
|
+
const keys = Object.keys(arg).sort()
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
keys.length === 3 &&
|
|
56
|
+
keys[0] === 'currency' &&
|
|
57
|
+
keys[1] === 'issuer' &&
|
|
58
|
+
keys[2] === 'value'
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Type guard for AmountObjectMPT
|
|
64
|
+
*/
|
|
65
|
+
function isAmountObjectMPT(arg): arg is AmountObjectMPT {
|
|
66
|
+
const keys = Object.keys(arg).sort()
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
keys.length === 2 && keys[0] === 'mpt_issuance_id' && keys[1] === 'value'
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Class for serializing/Deserializing Amounts
|
|
75
|
+
*/
|
|
76
|
+
class Amount extends SerializedType {
|
|
77
|
+
static defaultAmount: Amount = new Amount(hexToBytes('4000000000000000'))
|
|
78
|
+
|
|
79
|
+
constructor(bytes: Uint8Array) {
|
|
80
|
+
super(bytes ?? Amount.defaultAmount.bytes)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Construct an amount from an IOU, MPT or string amount
|
|
85
|
+
*
|
|
86
|
+
* @param value An Amount, object representing an IOU, or a string
|
|
87
|
+
* representing an integer amount
|
|
88
|
+
* @returns An Amount object
|
|
89
|
+
*/
|
|
90
|
+
static from<T extends Amount | AmountObject | string>(value: T): Amount {
|
|
91
|
+
if (value instanceof Amount) {
|
|
92
|
+
return value
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let amount = new Uint8Array(8)
|
|
96
|
+
if (typeof value === 'string') {
|
|
97
|
+
Amount.assertXrpIsValid(value)
|
|
98
|
+
|
|
99
|
+
const number = BigInt(value)
|
|
100
|
+
|
|
101
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
102
|
+
writeUInt32BE(intBuf[0], Number(number >> BigInt(32)), 0)
|
|
103
|
+
writeUInt32BE(intBuf[1], Number(number & BigInt(mask)), 0)
|
|
104
|
+
|
|
105
|
+
amount = concat(intBuf)
|
|
106
|
+
|
|
107
|
+
amount[0] |= 0x40
|
|
108
|
+
|
|
109
|
+
return new Amount(amount)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (isAmountObjectIOU(value)) {
|
|
113
|
+
const number = new BigNumber(value.value)
|
|
114
|
+
Amount.assertIouIsValid(number)
|
|
115
|
+
|
|
116
|
+
if (number.isZero()) {
|
|
117
|
+
amount[0] |= 0x80
|
|
118
|
+
} else {
|
|
119
|
+
const integerNumberString = number
|
|
120
|
+
.times(`1e${-((number.e || 0) - 15)}`)
|
|
121
|
+
.abs()
|
|
122
|
+
.toString()
|
|
123
|
+
|
|
124
|
+
const num = BigInt(integerNumberString)
|
|
125
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
126
|
+
writeUInt32BE(intBuf[0], Number(num >> BigInt(32)), 0)
|
|
127
|
+
writeUInt32BE(intBuf[1], Number(num & BigInt(mask)), 0)
|
|
128
|
+
|
|
129
|
+
amount = concat(intBuf)
|
|
130
|
+
|
|
131
|
+
amount[0] |= 0x80
|
|
132
|
+
|
|
133
|
+
if (number.gt(new BigNumber(0))) {
|
|
134
|
+
amount[0] |= 0x40
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const exponent = (number.e || 0) - 15
|
|
138
|
+
const exponentByte = 97 + exponent
|
|
139
|
+
amount[0] |= exponentByte >>> 2
|
|
140
|
+
amount[1] |= (exponentByte & 0x03) << 6
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const currency = Currency.from(value.currency).toBytes()
|
|
144
|
+
const issuer = AccountID.from(value.issuer).toBytes()
|
|
145
|
+
return new Amount(concat([amount, currency, issuer]))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (isAmountObjectMPT(value)) {
|
|
149
|
+
Amount.assertMptIsValid(value.value)
|
|
150
|
+
|
|
151
|
+
let leadingByte = new Uint8Array(1)
|
|
152
|
+
leadingByte[0] |= 0x60
|
|
153
|
+
|
|
154
|
+
const num = BigInt(value.value)
|
|
155
|
+
|
|
156
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)]
|
|
157
|
+
writeUInt32BE(intBuf[0], Number(num >> BigInt(32)), 0)
|
|
158
|
+
writeUInt32BE(intBuf[1], Number(num & BigInt(mask)), 0)
|
|
159
|
+
|
|
160
|
+
amount = concat(intBuf)
|
|
161
|
+
|
|
162
|
+
const mptIssuanceID = Hash192.from(value.mpt_issuance_id).toBytes()
|
|
163
|
+
return new Amount(concat([leadingByte, amount, mptIssuanceID]))
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
throw new Error('Invalid type to construct an Amount')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Read an amount from a BinaryParser
|
|
171
|
+
*
|
|
172
|
+
* @param parser BinaryParser to read the Amount from
|
|
173
|
+
* @returns An Amount object
|
|
174
|
+
*/
|
|
175
|
+
static fromParser(parser: BinaryParser): Amount {
|
|
176
|
+
const isIOU = parser.peek() & 0x80
|
|
177
|
+
if (isIOU) return new Amount(parser.read(48))
|
|
178
|
+
|
|
179
|
+
// the amount can be either MPT or XRP at this point
|
|
180
|
+
const isMPT = parser.peek() & 0x20
|
|
181
|
+
const numBytes = isMPT ? 33 : 8
|
|
182
|
+
return new Amount(parser.read(numBytes))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get the JSON representation of this Amount
|
|
187
|
+
*
|
|
188
|
+
* @returns the JSON interpretation of this.bytes
|
|
189
|
+
*/
|
|
190
|
+
toJSON(): AmountObject | string {
|
|
191
|
+
if (this.isNative()) {
|
|
192
|
+
const bytes = this.bytes
|
|
193
|
+
const isPositive = bytes[0] & 0x40
|
|
194
|
+
const sign = isPositive ? '' : '-'
|
|
195
|
+
bytes[0] &= 0x3f
|
|
196
|
+
|
|
197
|
+
const msb = BigInt(readUInt32BE(bytes.slice(0, 4), 0))
|
|
198
|
+
const lsb = BigInt(readUInt32BE(bytes.slice(4), 0))
|
|
199
|
+
const num = (msb << BigInt(32)) | lsb
|
|
200
|
+
|
|
201
|
+
return `${sign}${num.toString()}`
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (this.isIOU()) {
|
|
205
|
+
const parser = new BinaryParser(this.toString())
|
|
206
|
+
const mantissa = parser.read(8)
|
|
207
|
+
const currency = Currency.fromParser(parser) as Currency
|
|
208
|
+
const issuer = AccountID.fromParser(parser) as AccountID
|
|
209
|
+
|
|
210
|
+
const b1 = mantissa[0]
|
|
211
|
+
const b2 = mantissa[1]
|
|
212
|
+
|
|
213
|
+
const isPositive = b1 & 0x40
|
|
214
|
+
const sign = isPositive ? '' : '-'
|
|
215
|
+
const exponent = ((b1 & 0x3f) << 2) + ((b2 & 0xff) >> 6) - 97
|
|
216
|
+
|
|
217
|
+
mantissa[0] = 0
|
|
218
|
+
mantissa[1] &= 0x3f
|
|
219
|
+
const value = new BigNumber(`${sign}0x${bytesToHex(mantissa)}`).times(
|
|
220
|
+
`1e${exponent}`,
|
|
221
|
+
)
|
|
222
|
+
Amount.assertIouIsValid(value)
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
value: value.toString(),
|
|
226
|
+
currency: currency.toJSON(),
|
|
227
|
+
issuer: issuer.toJSON(),
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (this.isMPT()) {
|
|
232
|
+
const parser = new BinaryParser(this.toString())
|
|
233
|
+
const leadingByte = parser.read(1)
|
|
234
|
+
const amount = parser.read(8)
|
|
235
|
+
const mptID = Hash192.fromParser(parser) as Hash192
|
|
236
|
+
|
|
237
|
+
const isPositive = leadingByte[0] & 0x40
|
|
238
|
+
const sign = isPositive ? '' : '-'
|
|
239
|
+
|
|
240
|
+
const msb = BigInt(readUInt32BE(amount.slice(0, 4), 0))
|
|
241
|
+
const lsb = BigInt(readUInt32BE(amount.slice(4), 0))
|
|
242
|
+
const num = (msb << BigInt(32)) | lsb
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
value: `${sign}${num.toString()}`,
|
|
246
|
+
mpt_issuance_id: mptID.toString(),
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
throw new Error('Invalid amount to construct JSON')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Validate XRP amount
|
|
255
|
+
*
|
|
256
|
+
* @param amount String representing XRP amount
|
|
257
|
+
* @returns void, but will throw if invalid amount
|
|
258
|
+
*/
|
|
259
|
+
private static assertXrpIsValid(amount: string): void {
|
|
260
|
+
if (amount.indexOf('.') !== -1) {
|
|
261
|
+
throw new Error(`${amount.toString()} is an illegal amount`)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const decimal = new BigNumber(amount)
|
|
265
|
+
if (!decimal.isZero()) {
|
|
266
|
+
if (decimal.lt(MIN_XRP) || decimal.gt(MAX_DROPS)) {
|
|
267
|
+
throw new Error(`${amount.toString()} is an illegal amount`)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Validate IOU.value amount
|
|
274
|
+
*
|
|
275
|
+
* @param decimal BigNumber object representing IOU.value
|
|
276
|
+
* @returns void, but will throw if invalid amount
|
|
277
|
+
*/
|
|
278
|
+
private static assertIouIsValid(decimal: BigNumber): void {
|
|
279
|
+
if (!decimal.isZero()) {
|
|
280
|
+
const p = decimal.precision()
|
|
281
|
+
const e = (decimal.e || 0) - 15
|
|
282
|
+
if (
|
|
283
|
+
p > MAX_IOU_PRECISION ||
|
|
284
|
+
e > MAX_IOU_EXPONENT ||
|
|
285
|
+
e < MIN_IOU_EXPONENT
|
|
286
|
+
) {
|
|
287
|
+
throw new Error('Decimal precision out of range')
|
|
288
|
+
}
|
|
289
|
+
this.verifyNoDecimal(decimal)
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Validate MPT.value amount
|
|
295
|
+
*
|
|
296
|
+
* @param decimal BigNumber object representing MPT.value
|
|
297
|
+
* @returns void, but will throw if invalid amount
|
|
298
|
+
*/
|
|
299
|
+
private static assertMptIsValid(amount: string): void {
|
|
300
|
+
if (amount.indexOf('.') !== -1) {
|
|
301
|
+
throw new Error(`${amount.toString()} is an illegal amount`)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const decimal = new BigNumber(amount)
|
|
305
|
+
if (!decimal.isZero()) {
|
|
306
|
+
if (decimal < BigNumber(0)) {
|
|
307
|
+
throw new Error(`${amount.toString()} is an illegal amount`)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (Number(BigInt(amount) & BigInt(mptMask)) != 0) {
|
|
311
|
+
throw new Error(`${amount.toString()} is an illegal amount`)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Ensure that the value after being multiplied by the exponent does not
|
|
318
|
+
* contain a decimal.
|
|
319
|
+
*
|
|
320
|
+
* @param decimal a Decimal object
|
|
321
|
+
* @returns a string of the object without a decimal
|
|
322
|
+
*/
|
|
323
|
+
private static verifyNoDecimal(decimal: BigNumber): void {
|
|
324
|
+
const integerNumberString = decimal
|
|
325
|
+
.times(`1e${-((decimal.e || 0) - 15)}`)
|
|
326
|
+
.abs()
|
|
327
|
+
.toString()
|
|
328
|
+
|
|
329
|
+
if (integerNumberString.indexOf('.') !== -1) {
|
|
330
|
+
throw new Error('Decimal place found in integerNumberString')
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Test if this amount is in units of Native Currency(XRP)
|
|
336
|
+
*
|
|
337
|
+
* @returns true if Native (XRP)
|
|
338
|
+
*/
|
|
339
|
+
private isNative(): boolean {
|
|
340
|
+
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) === 0
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Test if this amount is in units of MPT
|
|
345
|
+
*
|
|
346
|
+
* @returns true if MPT
|
|
347
|
+
*/
|
|
348
|
+
private isMPT(): boolean {
|
|
349
|
+
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) !== 0
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Test if this amount is in units of IOU
|
|
354
|
+
*
|
|
355
|
+
* @returns true if IOU
|
|
356
|
+
*/
|
|
357
|
+
private isIOU(): boolean {
|
|
358
|
+
return (this.bytes[0] & 0x80) !== 0
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
getSType(): SerializedTypeID {
|
|
362
|
+
return SerializedTypeID.STI_AMOUNT
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export { Amount, AmountObject }
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { SerializedType, SerializedTypeID } from './serialized-type'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { hexToBytes } from '@transia/isomorphic/utils'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Variable length encoded type
|
|
7
|
+
*/
|
|
8
|
+
class Blob extends SerializedType {
|
|
9
|
+
constructor(bytes: Uint8Array) {
|
|
10
|
+
super(bytes)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Defines how to read a Blob from a BinaryParser
|
|
15
|
+
*
|
|
16
|
+
* @param parser The binary parser to read the Blob from
|
|
17
|
+
* @param hint The length of the blob, computed by readVariableLengthLength() and passed in
|
|
18
|
+
* @returns A Blob object
|
|
19
|
+
*/
|
|
20
|
+
static fromParser(parser: BinaryParser, hint: number): Blob {
|
|
21
|
+
return new Blob(parser.read(hint))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a Blob object from a hex-string
|
|
26
|
+
*
|
|
27
|
+
* @param value existing Blob object or a hex-string
|
|
28
|
+
* @returns A Blob object
|
|
29
|
+
*/
|
|
30
|
+
static from<T extends Blob | string>(value: T): Blob {
|
|
31
|
+
if (value instanceof Blob) {
|
|
32
|
+
return value
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof value === 'string') {
|
|
36
|
+
if (!/^[A-F0-9]*$/iu.test(value)) {
|
|
37
|
+
throw new Error('Cannot construct Blob from a non-hex string')
|
|
38
|
+
}
|
|
39
|
+
return new Blob(hexToBytes(value))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw new Error('Cannot construct Blob from value given')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getSType(): SerializedTypeID {
|
|
46
|
+
return SerializedTypeID.STI_VL
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { Blob }
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Hash160 } from './hash-160'
|
|
2
|
+
import { bytesToHex, hexToBytes, hexToString } from '@transia/isomorphic/utils'
|
|
3
|
+
import { SerializedTypeID } from './serialized-type'
|
|
4
|
+
|
|
5
|
+
const XRP_HEX_REGEX = /^0{40}$/
|
|
6
|
+
const ISO_REGEX = /^[A-Z0-9a-z?!@#$%^&*(){}[\]|]{3}$/
|
|
7
|
+
const HEX_REGEX = /^[A-F0-9]{40}$/
|
|
8
|
+
// eslint-disable-next-line no-control-regex
|
|
9
|
+
const STANDARD_FORMAT_HEX_REGEX = /^0{24}[\x00-\x7F]{6}0{10}$/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Convert an ISO code to a currency bytes representation
|
|
13
|
+
*/
|
|
14
|
+
function isoToBytes(iso: string): Uint8Array {
|
|
15
|
+
const bytes = new Uint8Array(20)
|
|
16
|
+
if (iso !== 'XRP') {
|
|
17
|
+
const isoBytes = iso.split('').map((c) => c.charCodeAt(0))
|
|
18
|
+
bytes.set(isoBytes, 12)
|
|
19
|
+
}
|
|
20
|
+
return bytes
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Tests if ISO is a valid iso code
|
|
25
|
+
*/
|
|
26
|
+
function isIsoCode(iso: string): boolean {
|
|
27
|
+
return ISO_REGEX.test(iso)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isoCodeFromHex(code: Uint8Array): string | null {
|
|
31
|
+
const iso = hexToString(bytesToHex(code))
|
|
32
|
+
if (iso === 'XRP') {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
if (isIsoCode(iso)) {
|
|
36
|
+
return iso
|
|
37
|
+
}
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Tests if hex is a valid hex-string
|
|
43
|
+
*/
|
|
44
|
+
function isHex(hex: string): boolean {
|
|
45
|
+
return HEX_REGEX.test(hex)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Tests if a string is a valid representation of a currency
|
|
50
|
+
*/
|
|
51
|
+
function isStringRepresentation(input: string): boolean {
|
|
52
|
+
return input.length === 3 || isHex(input)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Tests if a Uint8Array is a valid representation of a currency
|
|
57
|
+
*/
|
|
58
|
+
function isBytesArray(bytes: Uint8Array): boolean {
|
|
59
|
+
return bytes.byteLength === 20
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Ensures that a value is a valid representation of a currency
|
|
64
|
+
*/
|
|
65
|
+
function isValidRepresentation(input: Uint8Array | string): boolean {
|
|
66
|
+
return input instanceof Uint8Array
|
|
67
|
+
? isBytesArray(input)
|
|
68
|
+
: isStringRepresentation(input)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Generate bytes from a string or UInt8Array representation of a currency
|
|
73
|
+
*/
|
|
74
|
+
function bytesFromRepresentation(input: string): Uint8Array {
|
|
75
|
+
if (!isValidRepresentation(input)) {
|
|
76
|
+
throw new Error(`Unsupported Currency representation: ${input}`)
|
|
77
|
+
}
|
|
78
|
+
return input.length === 3 ? isoToBytes(input) : hexToBytes(input)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Class defining how to encode and decode Currencies
|
|
83
|
+
*/
|
|
84
|
+
class Currency extends Hash160 {
|
|
85
|
+
static readonly XRP = new Currency(new Uint8Array(20))
|
|
86
|
+
private readonly _iso: string | null
|
|
87
|
+
|
|
88
|
+
constructor(byteBuf: Uint8Array) {
|
|
89
|
+
super(byteBuf ?? Currency.XRP.bytes)
|
|
90
|
+
const hex = bytesToHex(this.bytes)
|
|
91
|
+
|
|
92
|
+
if (XRP_HEX_REGEX.test(hex)) {
|
|
93
|
+
this._iso = 'XRP'
|
|
94
|
+
} else if (STANDARD_FORMAT_HEX_REGEX.test(hex)) {
|
|
95
|
+
this._iso = isoCodeFromHex(this.bytes.slice(12, 15))
|
|
96
|
+
} else {
|
|
97
|
+
this._iso = null
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Return the ISO code of this currency
|
|
103
|
+
*
|
|
104
|
+
* @returns ISO code if it exists, else null
|
|
105
|
+
*/
|
|
106
|
+
iso(): string | null {
|
|
107
|
+
return this._iso
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Constructs a Currency object
|
|
112
|
+
*
|
|
113
|
+
* @param val Currency object or a string representation of a currency
|
|
114
|
+
*/
|
|
115
|
+
static from<T extends Hash160 | string>(value: T): Currency {
|
|
116
|
+
if (value instanceof Currency) {
|
|
117
|
+
return value
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (typeof value === 'string') {
|
|
121
|
+
return new Currency(bytesFromRepresentation(value))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
throw new Error('Cannot construct Currency from value given')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Gets the JSON representation of a currency
|
|
129
|
+
*
|
|
130
|
+
* @returns JSON representation
|
|
131
|
+
*/
|
|
132
|
+
toJSON(): string {
|
|
133
|
+
const iso = this.iso()
|
|
134
|
+
if (iso !== null) {
|
|
135
|
+
return iso
|
|
136
|
+
}
|
|
137
|
+
return bytesToHex(this.bytes)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
getSType(): SerializedTypeID {
|
|
141
|
+
return SerializedTypeID.STI_CURRENCY
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { Currency }
|