ripple-binary-codec
Advanced tools
Comparing version 2.1.0 to 4.0.0-mpt-beta
@@ -21,3 +21,3 @@ { | ||
"UInt96": 20, | ||
"UInt192": 21, | ||
"Hash192": 21, | ||
"UInt384": 22, | ||
@@ -57,2 +57,4 @@ "UInt512": 23, | ||
"Oracle": 128, | ||
"MPTokenIssuance": 126, | ||
"MPToken": 127, | ||
"Any": -3, | ||
@@ -266,2 +268,12 @@ "Child": -2, | ||
[ | ||
"AssetScale", | ||
{ | ||
"nth": 20, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt8" | ||
} | ||
], | ||
[ | ||
"LedgerEntryType", | ||
@@ -1077,2 +1089,42 @@ { | ||
[ | ||
"MaximumAmount", | ||
{ | ||
"nth": 24, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"OutstandingAmount", | ||
{ | ||
"nth": 25, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"LockedAmount", | ||
{ | ||
"nth": 26, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"MPTAmount", | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"EmailHash", | ||
@@ -1128,2 +1180,12 @@ { | ||
[ | ||
"MPTokenIssuanceID", | ||
{ | ||
"nth": 1, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "Hash192" | ||
} | ||
], | ||
[ | ||
"LedgerHash", | ||
@@ -1989,2 +2051,12 @@ { | ||
[ | ||
"MPTokenMetadata", | ||
{ | ||
"nth": 30, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "Blob" | ||
} | ||
], | ||
[ | ||
"Account", | ||
@@ -2080,2 +2152,12 @@ { | ||
[ | ||
"MPTokenHolder", | ||
{ | ||
"nth": 11, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "AccountID" | ||
} | ||
], | ||
[ | ||
"HookAccount", | ||
@@ -2808,12 +2890,13 @@ { | ||
"temBAD_NFTOKEN_TRANSFER_FEE": -262, | ||
"temBAD_AMM_TOKENS": -261, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -260, | ||
"temXCHAIN_BAD_PROOF": -259, | ||
"temXCHAIN_BRIDGE_BAD_ISSUES": -258, | ||
"temXCHAIN_BRIDGE_NONDOOR_OWNER": -257, | ||
"temXCHAIN_BRIDGE_BAD_MIN_ACCOUNT_CREATE_AMOUNT": -256, | ||
"temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT": -255, | ||
"temEMPTY_DID": -254, | ||
"temARRAY_EMPTY": -253, | ||
"temARRAY_TOO_LARGE": -252, | ||
"temBAD_MPTOKEN_TRANSFER_FEE": -261, | ||
"temBAD_AMM_TOKENS": -260, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -259, | ||
"temXCHAIN_BAD_PROOF": -258, | ||
"temXCHAIN_BRIDGE_BAD_ISSUES": -257, | ||
"temXCHAIN_BRIDGE_NONDOOR_OWNER": -256, | ||
"temXCHAIN_BRIDGE_BAD_MIN_ACCOUNT_CREATE_AMOUNT": -255, | ||
"temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT": -254, | ||
"temEMPTY_DID": -253, | ||
"temARRAY_EMPTY": -252, | ||
"temARRAY_TOO_LARGE": -251, | ||
@@ -2934,3 +3017,7 @@ "tefFAILURE": -199, | ||
"tecARRAY_EMPTY": 190, | ||
"tecARRAY_TOO_LARGE": 191 | ||
"tecARRAY_TOO_LARGE": 191, | ||
"tecMPTOKEN_EXISTS": 192, | ||
"tecMPT_MAX_AMOUNT_EXCEEDED": 193, | ||
"tecMPT_LOCKED": 194, | ||
"tecMPT_NOT_SUPPORTED": 195 | ||
}, | ||
@@ -2986,2 +3073,6 @@ "TRANSACTION_TYPES": { | ||
"OracleDelete": 52, | ||
"MPTokenIssuanceCreate": 53, | ||
"MPTokenIssuanceDestroy": 54, | ||
"MPTokenAuthorize": 55, | ||
"MPTokenIssuanceSet": 56, | ||
"EnableAmendment": 100, | ||
@@ -2988,0 +3079,0 @@ "SetFee": 101, |
import { BinaryParser } from '../serdes/binary-parser'; | ||
import { JsonObject, SerializedType } from './serialized-type'; | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface AmountObject extends JsonObject { | ||
interface AmountObjectIOU extends JsonObject { | ||
value: string; | ||
@@ -11,3 +8,11 @@ currency: string; | ||
} | ||
interface AmountObjectMPT extends JsonObject { | ||
value: string; | ||
mpt_issuance_id: string; | ||
} | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
type AmountObject = AmountObjectIOU | AmountObjectMPT; | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -19,3 +24,3 @@ */ | ||
/** | ||
* Construct an amount from an IOU or string amount | ||
* Construct an amount from an IOU, MPT or string amount | ||
* | ||
@@ -55,2 +60,9 @@ * @param value An Amount, object representing an IOU, or a string | ||
/** | ||
* Validate MPT.value amount | ||
* | ||
* @param decimal BigNumber object representing MPT.value | ||
* @returns void, but will throw if invalid amount | ||
*/ | ||
private static assertMptIsValid; | ||
/** | ||
* Ensure that the value after being multiplied by the exponent does not | ||
@@ -69,3 +81,15 @@ * contain a decimal. | ||
private isNative; | ||
/** | ||
* Test if this amount is in units of MPT | ||
* | ||
* @returns true if MPT | ||
*/ | ||
private isMPT; | ||
/** | ||
* Test if this amount is in units of IOU | ||
* | ||
* @returns true if IOU | ||
*/ | ||
private isIOU; | ||
} | ||
export { Amount, AmountObject }; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const utils_2 = require("../utils"); | ||
const hash_192_1 = require("./hash-192"); | ||
/** | ||
@@ -24,2 +25,3 @@ * Constants for validating amounts | ||
const mask = BigInt(0x00000000ffffffff); | ||
const mptMask = BigInt(0x8000000000000000); | ||
/** | ||
@@ -35,5 +37,5 @@ * BigNumber configuration for Amount IOUs | ||
/** | ||
* Type guard for AmountObject | ||
* Type guard for AmountObjectIOU | ||
*/ | ||
function isAmountObject(arg) { | ||
function isAmountObjectIOU(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
@@ -46,2 +48,9 @@ return (keys.length === 3 && | ||
/** | ||
* Type guard for AmountObjectMPT | ||
*/ | ||
function isAmountObjectMPT(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
return (keys.length === 2 && keys[0] === 'mpt_issuance_id' && keys[1] === 'value'); | ||
} | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -54,3 +63,3 @@ */ | ||
/** | ||
* Construct an amount from an IOU or string amount | ||
* Construct an amount from an IOU, MPT or string amount | ||
* | ||
@@ -76,3 +85,3 @@ * @param value An Amount, object representing an IOU, or a string | ||
} | ||
if (isAmountObject(value)) { | ||
if (isAmountObjectIOU(value)) { | ||
const number = new bignumber_js_1.default(value.value); | ||
@@ -106,2 +115,14 @@ Amount.assertIouIsValid(number); | ||
} | ||
if (isAmountObjectMPT(value)) { | ||
Amount.assertMptIsValid(value.value); | ||
let leadingByte = new Uint8Array(1); | ||
leadingByte[0] |= 0x60; | ||
const num = BigInt(value.value); | ||
const intBuf = [new Uint8Array(4), new Uint8Array(4)]; | ||
(0, utils_2.writeUInt32BE)(intBuf[0], Number(num >> BigInt(32)), 0); | ||
(0, utils_2.writeUInt32BE)(intBuf[1], Number(num & BigInt(mask)), 0); | ||
amount = (0, utils_1.concat)(intBuf); | ||
const mptIssuanceID = hash_192_1.Hash192.from(value.mpt_issuance_id).toBytes(); | ||
return new Amount((0, utils_1.concat)([leadingByte, amount, mptIssuanceID])); | ||
} | ||
throw new Error('Invalid type to construct an Amount'); | ||
@@ -116,4 +137,8 @@ } | ||
static fromParser(parser) { | ||
const isXRP = parser.peek() & 0x80; | ||
const numBytes = isXRP ? 48 : 8; | ||
const isIOU = parser.peek() & 0x80; | ||
if (isIOU) | ||
return new Amount(parser.read(48)); | ||
// the amount can be either MPT or XRP at this point | ||
const isMPT = parser.peek() & 0x20; | ||
const numBytes = isMPT ? 33 : 8; | ||
return new Amount(parser.read(numBytes)); | ||
@@ -137,3 +162,3 @@ } | ||
} | ||
else { | ||
if (this.isIOU()) { | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
@@ -158,2 +183,18 @@ const mantissa = parser.read(8); | ||
} | ||
if (this.isMPT()) { | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
const leadingByte = parser.read(1); | ||
const amount = parser.read(8); | ||
const mptID = hash_192_1.Hash192.fromParser(parser); | ||
const isPositive = leadingByte[0] & 0x40; | ||
const sign = isPositive ? '' : '-'; | ||
const msb = BigInt((0, utils_2.readUInt32BE)(amount.slice(0, 4), 0)); | ||
const lsb = BigInt((0, utils_2.readUInt32BE)(amount.slice(4), 0)); | ||
const num = (msb << BigInt(32)) | lsb; | ||
return { | ||
value: `${sign}${num.toString()}`, | ||
mpt_issuance_id: mptID.toString(), | ||
}; | ||
} | ||
throw new Error('Invalid amount to construct JSON'); | ||
} | ||
@@ -196,2 +237,22 @@ /** | ||
/** | ||
* Validate MPT.value amount | ||
* | ||
* @param decimal BigNumber object representing MPT.value | ||
* @returns void, but will throw if invalid amount | ||
*/ | ||
static assertMptIsValid(amount) { | ||
if (amount.indexOf('.') !== -1) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
const decimal = new bignumber_js_1.default(amount); | ||
if (!decimal.isZero()) { | ||
if (decimal < (0, bignumber_js_1.default)(0)) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
if (Number(BigInt(amount) & BigInt(mptMask)) != 0) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
} | ||
} | ||
/** | ||
* Ensure that the value after being multiplied by the exponent does not | ||
@@ -218,4 +279,20 @@ * contain a decimal. | ||
isNative() { | ||
return (this.bytes[0] & 0x80) === 0; | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) === 0; | ||
} | ||
/** | ||
* Test if this amount is in units of MPT | ||
* | ||
* @returns true if MPT | ||
*/ | ||
isMPT() { | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) !== 0; | ||
} | ||
/** | ||
* Test if this amount is in units of IOU | ||
* | ||
* @returns true if IOU | ||
*/ | ||
isIOU() { | ||
return (this.bytes[0] & 0x80) !== 0; | ||
} | ||
} | ||
@@ -222,0 +299,0 @@ exports.Amount = Amount; |
@@ -7,2 +7,3 @@ import { AccountID } from './account-id'; | ||
import { Hash160 } from './hash-160'; | ||
import { Hash192 } from './hash-192'; | ||
import { Hash256 } from './hash-256'; | ||
@@ -19,2 +20,2 @@ import { PathSet } from './path-set'; | ||
declare const coreTypes: Record<string, typeof SerializedType>; | ||
export { coreTypes, AccountID, Amount, Blob, Currency, Hash128, Hash160, Hash256, PathSet, STArray, STObject, UInt8, UInt16, UInt32, UInt64, Vector256, }; | ||
export { coreTypes, AccountID, Amount, Blob, Currency, Hash128, Hash160, Hash192, Hash256, PathSet, STArray, STObject, UInt8, UInt16, UInt32, UInt64, Vector256, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Vector256 = exports.UInt64 = exports.UInt32 = exports.UInt16 = exports.UInt8 = exports.STObject = exports.STArray = exports.PathSet = exports.Hash256 = exports.Hash160 = exports.Hash128 = exports.Currency = exports.Blob = exports.Amount = exports.AccountID = exports.coreTypes = void 0; | ||
exports.Vector256 = exports.UInt64 = exports.UInt32 = exports.UInt16 = exports.UInt8 = exports.STObject = exports.STArray = exports.PathSet = exports.Hash256 = exports.Hash192 = exports.Hash160 = exports.Hash128 = exports.Currency = exports.Blob = exports.Amount = exports.AccountID = exports.coreTypes = void 0; | ||
const account_id_1 = require("./account-id"); | ||
@@ -16,2 +16,4 @@ Object.defineProperty(exports, "AccountID", { enumerable: true, get: function () { return account_id_1.AccountID; } }); | ||
Object.defineProperty(exports, "Hash160", { enumerable: true, get: function () { return hash_160_1.Hash160; } }); | ||
const hash_192_1 = require("./hash-192"); | ||
Object.defineProperty(exports, "Hash192", { enumerable: true, get: function () { return hash_192_1.Hash192; } }); | ||
const hash_256_1 = require("./hash-256"); | ||
@@ -45,2 +47,3 @@ Object.defineProperty(exports, "Hash256", { enumerable: true, get: function () { return hash_256_1.Hash256; } }); | ||
Hash160: hash_160_1.Hash160, | ||
Hash192: hash_192_1.Hash192, | ||
Hash256: hash_256_1.Hash256, | ||
@@ -47,0 +50,0 @@ Issue: issue_1.Issue, |
import { BinaryParser } from '../serdes/binary-parser'; | ||
import { JsonObject, SerializedType } from './serialized-type'; | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface AmountObject extends JsonObject { | ||
interface AmountObjectIOU extends JsonObject { | ||
value: string; | ||
@@ -11,3 +8,11 @@ currency: string; | ||
} | ||
interface AmountObjectMPT extends JsonObject { | ||
value: string; | ||
mpt_issuance_id: string; | ||
} | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
type AmountObject = AmountObjectIOU | AmountObjectMPT; | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -19,3 +24,3 @@ */ | ||
/** | ||
* Construct an amount from an IOU or string amount | ||
* Construct an amount from an IOU, MPT or string amount | ||
* | ||
@@ -55,2 +60,9 @@ * @param value An Amount, object representing an IOU, or a string | ||
/** | ||
* Validate MPT.value amount | ||
* | ||
* @param decimal BigNumber object representing MPT.value | ||
* @returns void, but will throw if invalid amount | ||
*/ | ||
private static assertMptIsValid; | ||
/** | ||
* Ensure that the value after being multiplied by the exponent does not | ||
@@ -69,3 +81,15 @@ * contain a decimal. | ||
private isNative; | ||
/** | ||
* Test if this amount is in units of MPT | ||
* | ||
* @returns true if MPT | ||
*/ | ||
private isMPT; | ||
/** | ||
* Test if this amount is in units of IOU | ||
* | ||
* @returns true if IOU | ||
*/ | ||
private isIOU; | ||
} | ||
export { Amount, AmountObject }; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const utils_2 = require("../utils"); | ||
const hash_192_1 = require("./hash-192"); | ||
/** | ||
@@ -24,2 +25,3 @@ * Constants for validating amounts | ||
const mask = BigInt(0x00000000ffffffff); | ||
const mptMask = BigInt(0x8000000000000000); | ||
/** | ||
@@ -35,5 +37,5 @@ * BigNumber configuration for Amount IOUs | ||
/** | ||
* Type guard for AmountObject | ||
* Type guard for AmountObjectIOU | ||
*/ | ||
function isAmountObject(arg) { | ||
function isAmountObjectIOU(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
@@ -46,2 +48,9 @@ return (keys.length === 3 && | ||
/** | ||
* Type guard for AmountObjectMPT | ||
*/ | ||
function isAmountObjectMPT(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
return (keys.length === 2 && keys[0] === 'mpt_issuance_id' && keys[1] === 'value'); | ||
} | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -54,3 +63,3 @@ */ | ||
/** | ||
* Construct an amount from an IOU or string amount | ||
* Construct an amount from an IOU, MPT or string amount | ||
* | ||
@@ -76,3 +85,3 @@ * @param value An Amount, object representing an IOU, or a string | ||
} | ||
if (isAmountObject(value)) { | ||
if (isAmountObjectIOU(value)) { | ||
const number = new bignumber_js_1.default(value.value); | ||
@@ -106,2 +115,14 @@ Amount.assertIouIsValid(number); | ||
} | ||
if (isAmountObjectMPT(value)) { | ||
Amount.assertMptIsValid(value.value); | ||
let leadingByte = new Uint8Array(1); | ||
leadingByte[0] |= 0x60; | ||
const num = BigInt(value.value); | ||
const intBuf = [new Uint8Array(4), new Uint8Array(4)]; | ||
(0, utils_2.writeUInt32BE)(intBuf[0], Number(num >> BigInt(32)), 0); | ||
(0, utils_2.writeUInt32BE)(intBuf[1], Number(num & BigInt(mask)), 0); | ||
amount = (0, utils_1.concat)(intBuf); | ||
const mptIssuanceID = hash_192_1.Hash192.from(value.mpt_issuance_id).toBytes(); | ||
return new Amount((0, utils_1.concat)([leadingByte, amount, mptIssuanceID])); | ||
} | ||
throw new Error('Invalid type to construct an Amount'); | ||
@@ -116,4 +137,8 @@ } | ||
static fromParser(parser) { | ||
const isXRP = parser.peek() & 0x80; | ||
const numBytes = isXRP ? 48 : 8; | ||
const isIOU = parser.peek() & 0x80; | ||
if (isIOU) | ||
return new Amount(parser.read(48)); | ||
// the amount can be either MPT or XRP at this point | ||
const isMPT = parser.peek() & 0x20; | ||
const numBytes = isMPT ? 33 : 8; | ||
return new Amount(parser.read(numBytes)); | ||
@@ -137,3 +162,3 @@ } | ||
} | ||
else { | ||
if (this.isIOU()) { | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
@@ -158,2 +183,18 @@ const mantissa = parser.read(8); | ||
} | ||
if (this.isMPT()) { | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
const leadingByte = parser.read(1); | ||
const amount = parser.read(8); | ||
const mptID = hash_192_1.Hash192.fromParser(parser); | ||
const isPositive = leadingByte[0] & 0x40; | ||
const sign = isPositive ? '' : '-'; | ||
const msb = BigInt((0, utils_2.readUInt32BE)(amount.slice(0, 4), 0)); | ||
const lsb = BigInt((0, utils_2.readUInt32BE)(amount.slice(4), 0)); | ||
const num = (msb << BigInt(32)) | lsb; | ||
return { | ||
value: `${sign}${num.toString()}`, | ||
mpt_issuance_id: mptID.toString(), | ||
}; | ||
} | ||
throw new Error('Invalid amount to construct JSON'); | ||
} | ||
@@ -196,2 +237,22 @@ /** | ||
/** | ||
* Validate MPT.value amount | ||
* | ||
* @param decimal BigNumber object representing MPT.value | ||
* @returns void, but will throw if invalid amount | ||
*/ | ||
static assertMptIsValid(amount) { | ||
if (amount.indexOf('.') !== -1) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
const decimal = new bignumber_js_1.default(amount); | ||
if (!decimal.isZero()) { | ||
if (decimal < (0, bignumber_js_1.default)(0)) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
if (Number(BigInt(amount) & BigInt(mptMask)) != 0) { | ||
throw new Error(`${amount.toString()} is an illegal amount`); | ||
} | ||
} | ||
} | ||
/** | ||
* Ensure that the value after being multiplied by the exponent does not | ||
@@ -218,4 +279,20 @@ * contain a decimal. | ||
isNative() { | ||
return (this.bytes[0] & 0x80) === 0; | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) === 0; | ||
} | ||
/** | ||
* Test if this amount is in units of MPT | ||
* | ||
* @returns true if MPT | ||
*/ | ||
isMPT() { | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) !== 0; | ||
} | ||
/** | ||
* Test if this amount is in units of IOU | ||
* | ||
* @returns true if IOU | ||
*/ | ||
isIOU() { | ||
return (this.bytes[0] & 0x80) !== 0; | ||
} | ||
} | ||
@@ -222,0 +299,0 @@ exports.Amount = Amount; |
@@ -7,2 +7,3 @@ import { AccountID } from './account-id'; | ||
import { Hash160 } from './hash-160'; | ||
import { Hash192 } from './hash-192'; | ||
import { Hash256 } from './hash-256'; | ||
@@ -19,2 +20,2 @@ import { PathSet } from './path-set'; | ||
declare const coreTypes: Record<string, typeof SerializedType>; | ||
export { coreTypes, AccountID, Amount, Blob, Currency, Hash128, Hash160, Hash256, PathSet, STArray, STObject, UInt8, UInt16, UInt32, UInt64, Vector256, }; | ||
export { coreTypes, AccountID, Amount, Blob, Currency, Hash128, Hash160, Hash192, Hash256, PathSet, STArray, STObject, UInt8, UInt16, UInt32, UInt64, Vector256, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Vector256 = exports.UInt64 = exports.UInt32 = exports.UInt16 = exports.UInt8 = exports.STObject = exports.STArray = exports.PathSet = exports.Hash256 = exports.Hash160 = exports.Hash128 = exports.Currency = exports.Blob = exports.Amount = exports.AccountID = exports.coreTypes = void 0; | ||
exports.Vector256 = exports.UInt64 = exports.UInt32 = exports.UInt16 = exports.UInt8 = exports.STObject = exports.STArray = exports.PathSet = exports.Hash256 = exports.Hash192 = exports.Hash160 = exports.Hash128 = exports.Currency = exports.Blob = exports.Amount = exports.AccountID = exports.coreTypes = void 0; | ||
const account_id_1 = require("./account-id"); | ||
@@ -16,2 +16,4 @@ Object.defineProperty(exports, "AccountID", { enumerable: true, get: function () { return account_id_1.AccountID; } }); | ||
Object.defineProperty(exports, "Hash160", { enumerable: true, get: function () { return hash_160_1.Hash160; } }); | ||
const hash_192_1 = require("./hash-192"); | ||
Object.defineProperty(exports, "Hash192", { enumerable: true, get: function () { return hash_192_1.Hash192; } }); | ||
const hash_256_1 = require("./hash-256"); | ||
@@ -45,2 +47,3 @@ Object.defineProperty(exports, "Hash256", { enumerable: true, get: function () { return hash_256_1.Hash256; } }); | ||
Hash160: hash_160_1.Hash160, | ||
Hash192: hash_192_1.Hash192, | ||
Hash256: hash_256_1.Hash256, | ||
@@ -47,0 +50,0 @@ Issue: issue_1.Issue, |
{ | ||
"name": "ripple-binary-codec", | ||
"version": "2.1.0", | ||
"version": "4.0.0-mpt-beta", | ||
"description": "XRP Ledger binary codec", | ||
@@ -44,5 +44,5 @@ "files": [ | ||
"engines": { | ||
"node": ">= 16" | ||
"node": ">= 18" | ||
}, | ||
"gitHead": "92849e57cef5fdd3744af347f2ad051a7735f16e" | ||
"gitHead": "ef82f86f2540baef46b62aef7d1aa594d8f73591" | ||
} |
@@ -21,3 +21,3 @@ { | ||
"UInt96": 20, | ||
"UInt192": 21, | ||
"Hash192": 21, | ||
"UInt384": 22, | ||
@@ -57,2 +57,4 @@ "UInt512": 23, | ||
"Oracle": 128, | ||
"MPTokenIssuance": 126, | ||
"MPToken": 127, | ||
"Any": -3, | ||
@@ -266,2 +268,12 @@ "Child": -2, | ||
[ | ||
"AssetScale", | ||
{ | ||
"nth": 20, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt8" | ||
} | ||
], | ||
[ | ||
"LedgerEntryType", | ||
@@ -1077,2 +1089,42 @@ { | ||
[ | ||
"MaximumAmount", | ||
{ | ||
"nth": 24, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"OutstandingAmount", | ||
{ | ||
"nth": 25, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"LockedAmount", | ||
{ | ||
"nth": 26, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"MPTAmount", | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "UInt64" | ||
} | ||
], | ||
[ | ||
"EmailHash", | ||
@@ -1128,2 +1180,12 @@ { | ||
[ | ||
"MPTokenIssuanceID", | ||
{ | ||
"nth": 1, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "Hash192" | ||
} | ||
], | ||
[ | ||
"LedgerHash", | ||
@@ -1989,2 +2051,12 @@ { | ||
[ | ||
"MPTokenMetadata", | ||
{ | ||
"nth": 30, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "Blob" | ||
} | ||
], | ||
[ | ||
"Account", | ||
@@ -2080,2 +2152,12 @@ { | ||
[ | ||
"MPTokenHolder", | ||
{ | ||
"nth": 11, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"type": "AccountID" | ||
} | ||
], | ||
[ | ||
"HookAccount", | ||
@@ -2808,12 +2890,13 @@ { | ||
"temBAD_NFTOKEN_TRANSFER_FEE": -262, | ||
"temBAD_AMM_TOKENS": -261, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -260, | ||
"temXCHAIN_BAD_PROOF": -259, | ||
"temXCHAIN_BRIDGE_BAD_ISSUES": -258, | ||
"temXCHAIN_BRIDGE_NONDOOR_OWNER": -257, | ||
"temXCHAIN_BRIDGE_BAD_MIN_ACCOUNT_CREATE_AMOUNT": -256, | ||
"temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT": -255, | ||
"temEMPTY_DID": -254, | ||
"temARRAY_EMPTY": -253, | ||
"temARRAY_TOO_LARGE": -252, | ||
"temBAD_MPTOKEN_TRANSFER_FEE": -261, | ||
"temBAD_AMM_TOKENS": -260, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -259, | ||
"temXCHAIN_BAD_PROOF": -258, | ||
"temXCHAIN_BRIDGE_BAD_ISSUES": -257, | ||
"temXCHAIN_BRIDGE_NONDOOR_OWNER": -256, | ||
"temXCHAIN_BRIDGE_BAD_MIN_ACCOUNT_CREATE_AMOUNT": -255, | ||
"temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT": -254, | ||
"temEMPTY_DID": -253, | ||
"temARRAY_EMPTY": -252, | ||
"temARRAY_TOO_LARGE": -251, | ||
@@ -2934,3 +3017,7 @@ "tefFAILURE": -199, | ||
"tecARRAY_EMPTY": 190, | ||
"tecARRAY_TOO_LARGE": 191 | ||
"tecARRAY_TOO_LARGE": 191, | ||
"tecMPTOKEN_EXISTS": 192, | ||
"tecMPT_MAX_AMOUNT_EXCEEDED": 193, | ||
"tecMPT_LOCKED": 194, | ||
"tecMPT_NOT_SUPPORTED": 195 | ||
}, | ||
@@ -2986,2 +3073,6 @@ "TRANSACTION_TYPES": { | ||
"OracleDelete": 52, | ||
"MPTokenIssuanceCreate": 53, | ||
"MPTokenIssuanceDestroy": 54, | ||
"MPTokenAuthorize": 55, | ||
"MPTokenIssuanceSet": 56, | ||
"EnableAmendment": 100, | ||
@@ -2988,0 +3079,0 @@ "SetFee": 101, |
@@ -9,2 +9,3 @@ import { BinaryParser } from '../serdes/binary-parser' | ||
import { readUInt32BE, writeUInt32BE } from '../utils' | ||
import { Hash192 } from './hash-192' | ||
@@ -20,2 +21,3 @@ /** | ||
const mask = BigInt(0x00000000ffffffff) | ||
const mptMask = BigInt(0x8000000000000000) | ||
@@ -32,6 +34,3 @@ /** | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface AmountObject extends JsonObject { | ||
interface AmountObjectIOU extends JsonObject { | ||
value: string | ||
@@ -42,7 +41,18 @@ currency: string | ||
interface AmountObjectMPT extends JsonObject { | ||
value: string | ||
mpt_issuance_id: string | ||
} | ||
/** | ||
* Type guard for AmountObject | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
function isAmountObject(arg): arg is AmountObject { | ||
type AmountObject = AmountObjectIOU | AmountObjectMPT | ||
/** | ||
* Type guard for AmountObjectIOU | ||
*/ | ||
function isAmountObjectIOU(arg): arg is AmountObjectIOU { | ||
const keys = Object.keys(arg).sort() | ||
return ( | ||
@@ -57,2 +67,13 @@ keys.length === 3 && | ||
/** | ||
* Type guard for AmountObjectMPT | ||
*/ | ||
function isAmountObjectMPT(arg): arg is AmountObjectMPT { | ||
const keys = Object.keys(arg).sort() | ||
return ( | ||
keys.length === 2 && keys[0] === 'mpt_issuance_id' && keys[1] === 'value' | ||
) | ||
} | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -68,3 +89,3 @@ */ | ||
/** | ||
* Construct an amount from an IOU or string amount | ||
* Construct an amount from an IOU, MPT or string amount | ||
* | ||
@@ -97,3 +118,3 @@ * @param value An Amount, object representing an IOU, or a string | ||
if (isAmountObject(value)) { | ||
if (isAmountObjectIOU(value)) { | ||
const number = new BigNumber(value.value) | ||
@@ -134,2 +155,20 @@ Amount.assertIouIsValid(number) | ||
if (isAmountObjectMPT(value)) { | ||
Amount.assertMptIsValid(value.value) | ||
let leadingByte = new Uint8Array(1) | ||
leadingByte[0] |= 0x60 | ||
const num = BigInt(value.value) | ||
const intBuf = [new Uint8Array(4), new Uint8Array(4)] | ||
writeUInt32BE(intBuf[0], Number(num >> BigInt(32)), 0) | ||
writeUInt32BE(intBuf[1], Number(num & BigInt(mask)), 0) | ||
amount = concat(intBuf) | ||
const mptIssuanceID = Hash192.from(value.mpt_issuance_id).toBytes() | ||
return new Amount(concat([leadingByte, amount, mptIssuanceID])) | ||
} | ||
throw new Error('Invalid type to construct an Amount') | ||
@@ -145,4 +184,8 @@ } | ||
static fromParser(parser: BinaryParser): Amount { | ||
const isXRP = parser.peek() & 0x80 | ||
const numBytes = isXRP ? 48 : 8 | ||
const isIOU = parser.peek() & 0x80 | ||
if (isIOU) return new Amount(parser.read(48)) | ||
// the amount can be either MPT or XRP at this point | ||
const isMPT = parser.peek() & 0x20 | ||
const numBytes = isMPT ? 33 : 8 | ||
return new Amount(parser.read(numBytes)) | ||
@@ -168,3 +211,5 @@ } | ||
return `${sign}${num.toString()}` | ||
} else { | ||
} | ||
if (this.isIOU()) { | ||
const parser = new BinaryParser(this.toString()) | ||
@@ -195,2 +240,23 @@ const mantissa = parser.read(8) | ||
} | ||
if (this.isMPT()) { | ||
const parser = new BinaryParser(this.toString()) | ||
const leadingByte = parser.read(1) | ||
const amount = parser.read(8) | ||
const mptID = Hash192.fromParser(parser) as Hash192 | ||
const isPositive = leadingByte[0] & 0x40 | ||
const sign = isPositive ? '' : '-' | ||
const msb = BigInt(readUInt32BE(amount.slice(0, 4), 0)) | ||
const lsb = BigInt(readUInt32BE(amount.slice(4), 0)) | ||
const num = (msb << BigInt(32)) | lsb | ||
return { | ||
value: `${sign}${num.toString()}`, | ||
mpt_issuance_id: mptID.toString(), | ||
} | ||
} | ||
throw new Error('Invalid amount to construct JSON') | ||
} | ||
@@ -239,2 +305,25 @@ | ||
/** | ||
* Validate MPT.value amount | ||
* | ||
* @param decimal BigNumber object representing MPT.value | ||
* @returns void, but will throw if invalid amount | ||
*/ | ||
private static assertMptIsValid(amount: string): void { | ||
if (amount.indexOf('.') !== -1) { | ||
throw new Error(`${amount.toString()} is an illegal amount`) | ||
} | ||
const decimal = new BigNumber(amount) | ||
if (!decimal.isZero()) { | ||
if (decimal < BigNumber(0)) { | ||
throw new Error(`${amount.toString()} is an illegal amount`) | ||
} | ||
if (Number(BigInt(amount) & BigInt(mptMask)) != 0) { | ||
throw new Error(`${amount.toString()} is an illegal amount`) | ||
} | ||
} | ||
} | ||
/** | ||
* Ensure that the value after being multiplied by the exponent does not | ||
@@ -263,6 +352,24 @@ * contain a decimal. | ||
private isNative(): boolean { | ||
return (this.bytes[0] & 0x80) === 0 | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) === 0 | ||
} | ||
/** | ||
* Test if this amount is in units of MPT | ||
* | ||
* @returns true if MPT | ||
*/ | ||
private isMPT(): boolean { | ||
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) !== 0 | ||
} | ||
/** | ||
* Test if this amount is in units of IOU | ||
* | ||
* @returns true if IOU | ||
*/ | ||
private isIOU(): boolean { | ||
return (this.bytes[0] & 0x80) !== 0 | ||
} | ||
} | ||
export { Amount, AmountObject } |
@@ -7,2 +7,3 @@ import { AccountID } from './account-id' | ||
import { Hash160 } from './hash-160' | ||
import { Hash192 } from './hash-192' | ||
import { Hash256 } from './hash-256' | ||
@@ -29,2 +30,3 @@ import { Issue } from './issue' | ||
Hash160, | ||
Hash192, | ||
Hash256, | ||
@@ -56,2 +58,3 @@ Issue, | ||
Hash160, | ||
Hash192, | ||
Hash256, | ||
@@ -58,0 +61,0 @@ PathSet, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
960271
290
25922
1