ripple-binary-codec
Advanced tools
Comparing version
@@ -674,2 +674,22 @@ { | ||
[ | ||
"ExtensionComputeLimit", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 52, | ||
"type": "UInt32" | ||
} | ||
], | ||
[ | ||
"ExtensionSizeLimit", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 53, | ||
"type": "UInt32" | ||
} | ||
], | ||
[ | ||
"IndexNext", | ||
@@ -917,6 +937,6 @@ { | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 27, | ||
"type": "UInt64" | ||
@@ -928,6 +948,6 @@ } | ||
{ | ||
"nth": 28, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 28, | ||
"type": "UInt64" | ||
@@ -1259,6 +1279,6 @@ } | ||
{ | ||
"nth": 34, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 34, | ||
"type": "Hash256" | ||
@@ -1850,6 +1870,16 @@ } | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 31, | ||
"isVLEncoded": true, | ||
"type": "Blob" | ||
} | ||
], | ||
[ | ||
"FinishFunction", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 32, | ||
"type": "Blob" | ||
@@ -2031,6 +2061,6 @@ } | ||
{ | ||
"nth": 24, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 24, | ||
"type": "AccountID" | ||
@@ -2040,2 +2070,12 @@ } | ||
[ | ||
"Number", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 1, | ||
"type": "Number" | ||
} | ||
], | ||
[ | ||
"TransactionMetaData", | ||
@@ -2333,6 +2373,6 @@ { | ||
{ | ||
"nth": 33, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 33, | ||
"type": "STObject" | ||
@@ -2534,6 +2574,6 @@ } | ||
{ | ||
"nth": 26, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 26, | ||
"type": "STArray" | ||
@@ -2545,6 +2585,6 @@ } | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 27, | ||
"type": "STArray" | ||
@@ -2554,7 +2594,8 @@ } | ||
[ | ||
"AcceptedCredentials", { | ||
"nth": 28, | ||
"isVLEncoded": false, | ||
"AcceptedCredentials", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 28, | ||
"type": "STArray" | ||
@@ -2746,6 +2787,6 @@ } | ||
{ | ||
"nth": 5, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 5, | ||
"type": "Vector256" | ||
@@ -2881,2 +2922,3 @@ } | ||
"Check": 67, | ||
"Credential": 129, | ||
"DID": 73, | ||
@@ -2896,3 +2938,2 @@ "DepositPreauth": 112, | ||
"Oracle": 128, | ||
"Credential": 129, | ||
"PayChannel": 120, | ||
@@ -2927,2 +2968,3 @@ "PermissionedDomain": 130, | ||
"tecHAS_OBLIGATIONS": 151, | ||
"tecHOOK_REJECTED": 153, | ||
"tecINCOMPLETE": 169, | ||
@@ -2969,2 +3011,3 @@ "tecINSUFFICIENT_FUNDS": 159, | ||
"tecUNFUNDED_PAYMENT": 104, | ||
"tecWASM_REJECTED": 194, | ||
"tecXCHAIN_ACCOUNT_CREATE_PAST": 181, | ||
@@ -2987,2 +3030,3 @@ "tecXCHAIN_ACCOUNT_CREATE_TOO_MANY": 182, | ||
"tecXCHAIN_WRONG_CHAIN": 176, | ||
"tefALREADY": -198, | ||
@@ -3010,2 +3054,3 @@ "tefBAD_ADD_AUTH": -197, | ||
"tefWRONG_PRIOR": -189, | ||
"telBAD_DOMAIN": -398, | ||
@@ -3028,2 +3073,3 @@ "telBAD_PATH_COUNT": -397, | ||
"telWRONG_NETWORK": -386, | ||
"temARRAY_EMPTY": -253, | ||
@@ -3078,2 +3124,3 @@ "temARRAY_TOO_LARGE": -252, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -260, | ||
"terFUNDS_SPENT": -98, | ||
@@ -3092,2 +3139,3 @@ "terINSUF_FEE_B": -97, | ||
"terRETRY": -99, | ||
"tesSUCCESS": 0 | ||
@@ -3109,4 +3157,4 @@ }, | ||
"Clawback": 30, | ||
"CredentialAccept": 59, | ||
"CredentialCreate": 58, | ||
"CredentialAccept": 59, | ||
"CredentialDelete": 60, | ||
@@ -3140,4 +3188,4 @@ "DIDDelete": 50, | ||
"PaymentChannelFund": 14, | ||
"PermissionedDomainDelete": 63, | ||
"PermissionedDomainSet": 62, | ||
"PermissionedDomainDelete": 63, | ||
"SetFee": 101, | ||
@@ -3172,2 +3220,3 @@ "SetRegularKey": 5, | ||
"NotPresent": 0, | ||
"Number": 9, | ||
"PathSet": 18, | ||
@@ -3174,0 +3223,0 @@ "STArray": 15, |
import { BinaryParser } from '../serdes/binary-parser'; | ||
import { JsonObject, SerializedType } from './serialized-type'; | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface IssueObject extends JsonObject { | ||
interface XRPIssue extends JsonObject { | ||
currency: string; | ||
issuer?: string; | ||
} | ||
interface IOUIssue extends JsonObject { | ||
currency: string; | ||
issuer: string; | ||
} | ||
interface MPTIssue extends JsonObject { | ||
mpt_issuance_id: string; | ||
} | ||
/** | ||
* Interface for JSON objects that represent issues | ||
*/ | ||
type IssueObject = XRPIssue | IOUIssue | MPTIssue; | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -19,5 +26,5 @@ */ | ||
* | ||
* @param value An Amount, object representing an IOU, or a string | ||
* @param value An Amount, object representing an IOU, MPTAmount, or a string | ||
* representing an integer amount | ||
* @returns An Amount object | ||
* @returns An Issue object | ||
*/ | ||
@@ -29,5 +36,8 @@ static from<T extends Issue | IssueObject>(value: T): Issue; | ||
* @param parser BinaryParser to read the Amount from | ||
* @returns An Amount object | ||
* @param hint The number of bytes to consume from the parser. | ||
* For an MPT amount, pass 24 (the fixed length for Hash192). | ||
* | ||
* @returns An Issue object | ||
*/ | ||
static fromParser(parser: BinaryParser): Issue; | ||
static fromParser(parser: BinaryParser, hint?: number): Issue; | ||
/** | ||
@@ -34,0 +44,0 @@ * Get the JSON representation of this Amount |
@@ -9,11 +9,12 @@ "use strict"; | ||
const serialized_type_1 = require("./serialized-type"); | ||
const hash_192_1 = require("./hash-192"); | ||
/** | ||
* Type guard for AmountObject | ||
* Type guard for Issue Object | ||
*/ | ||
function isIssueObject(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
if (keys.length === 1) { | ||
return keys[0] === 'currency'; | ||
} | ||
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'; | ||
const isXRP = keys.length === 1 && keys[0] === 'currency'; | ||
const isIOU = keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'; | ||
const isMPT = keys.length === 1 && keys[0] === 'mpt_issuance_id'; | ||
return isXRP || isIOU || isMPT; | ||
} | ||
@@ -30,5 +31,5 @@ /** | ||
* | ||
* @param value An Amount, object representing an IOU, or a string | ||
* @param value An Amount, object representing an IOU, MPTAmount, or a string | ||
* representing an integer amount | ||
* @returns An Amount object | ||
* @returns An Issue object | ||
*/ | ||
@@ -40,8 +41,17 @@ static from(value) { | ||
if (isIssueObject(value)) { | ||
const currency = currency_1.Currency.from(value.currency).toBytes(); | ||
if (value.issuer == null) { | ||
if (value.currency) { | ||
const currency = currency_1.Currency.from(value.currency.toString()).toBytes(); | ||
//IOU case | ||
if (value.issuer) { | ||
const issuer = account_id_1.AccountID.from(value.issuer.toString()).toBytes(); | ||
return new Issue((0, utils_1.concat)([currency, issuer])); | ||
} | ||
//XRP case | ||
return new Issue(currency); | ||
} | ||
const issuer = account_id_1.AccountID.from(value.issuer).toBytes(); | ||
return new Issue((0, utils_1.concat)([currency, issuer])); | ||
// MPT case | ||
if (value.mpt_issuance_id) { | ||
const mptIssuanceIdBytes = hash_192_1.Hash192.from(value.mpt_issuance_id.toString()).toBytes(); | ||
return new Issue(mptIssuanceIdBytes); | ||
} | ||
} | ||
@@ -54,5 +64,12 @@ throw new Error('Invalid type to construct an Amount'); | ||
* @param parser BinaryParser to read the Amount from | ||
* @returns An Amount object | ||
* @param hint The number of bytes to consume from the parser. | ||
* For an MPT amount, pass 24 (the fixed length for Hash192). | ||
* | ||
* @returns An Issue object | ||
*/ | ||
static fromParser(parser) { | ||
static fromParser(parser, hint) { | ||
if (hint === hash_192_1.Hash192.width) { | ||
const mptBytes = parser.read(hash_192_1.Hash192.width); | ||
return new Issue(mptBytes); | ||
} | ||
const currency = parser.read(20); | ||
@@ -71,2 +88,8 @@ if (new currency_1.Currency(currency).toJSON() === 'XRP') { | ||
toJSON() { | ||
// If the buffer is exactly 24 bytes, treat it as an MPT amount. | ||
if (this.toBytes().length === hash_192_1.Hash192.width) { | ||
return { | ||
mpt_issuance_id: this.toHex().toUpperCase(), | ||
}; | ||
} | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
@@ -73,0 +96,0 @@ const currency = currency_1.Currency.fromParser(parser); |
@@ -99,7 +99,17 @@ "use strict"; | ||
}, {}); | ||
function isValidFieldInstance(f) { | ||
return (f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized); | ||
} | ||
let sorted = Object.keys(xAddressDecoded) | ||
.map((f) => definitions.field[f]) | ||
.filter((f) => f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized) | ||
.map((f) => { | ||
if (!(f in definitions.field)) { | ||
if (f[0] === f[0].toLowerCase()) | ||
return undefined; | ||
throw new Error(`Field ${f} is not defined in the definitions`); | ||
} | ||
return definitions.field[f]; | ||
}) | ||
.filter(isValidFieldInstance) | ||
.sort((a, b) => { | ||
@@ -106,0 +116,0 @@ return a.ordinal - b.ordinal; |
import { BinaryParser } from '../serdes/binary-parser'; | ||
import { JsonObject, SerializedType } from './serialized-type'; | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface IssueObject extends JsonObject { | ||
interface XRPIssue extends JsonObject { | ||
currency: string; | ||
issuer?: string; | ||
} | ||
interface IOUIssue extends JsonObject { | ||
currency: string; | ||
issuer: string; | ||
} | ||
interface MPTIssue extends JsonObject { | ||
mpt_issuance_id: string; | ||
} | ||
/** | ||
* Interface for JSON objects that represent issues | ||
*/ | ||
type IssueObject = XRPIssue | IOUIssue | MPTIssue; | ||
/** | ||
* Class for serializing/Deserializing Amounts | ||
@@ -19,5 +26,5 @@ */ | ||
* | ||
* @param value An Amount, object representing an IOU, or a string | ||
* @param value An Amount, object representing an IOU, MPTAmount, or a string | ||
* representing an integer amount | ||
* @returns An Amount object | ||
* @returns An Issue object | ||
*/ | ||
@@ -29,5 +36,8 @@ static from<T extends Issue | IssueObject>(value: T): Issue; | ||
* @param parser BinaryParser to read the Amount from | ||
* @returns An Amount object | ||
* @param hint The number of bytes to consume from the parser. | ||
* For an MPT amount, pass 24 (the fixed length for Hash192). | ||
* | ||
* @returns An Issue object | ||
*/ | ||
static fromParser(parser: BinaryParser): Issue; | ||
static fromParser(parser: BinaryParser, hint?: number): Issue; | ||
/** | ||
@@ -34,0 +44,0 @@ * Get the JSON representation of this Amount |
@@ -9,11 +9,12 @@ "use strict"; | ||
const serialized_type_1 = require("./serialized-type"); | ||
const hash_192_1 = require("./hash-192"); | ||
/** | ||
* Type guard for AmountObject | ||
* Type guard for Issue Object | ||
*/ | ||
function isIssueObject(arg) { | ||
const keys = Object.keys(arg).sort(); | ||
if (keys.length === 1) { | ||
return keys[0] === 'currency'; | ||
} | ||
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'; | ||
const isXRP = keys.length === 1 && keys[0] === 'currency'; | ||
const isIOU = keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'; | ||
const isMPT = keys.length === 1 && keys[0] === 'mpt_issuance_id'; | ||
return isXRP || isIOU || isMPT; | ||
} | ||
@@ -30,5 +31,5 @@ /** | ||
* | ||
* @param value An Amount, object representing an IOU, or a string | ||
* @param value An Amount, object representing an IOU, MPTAmount, or a string | ||
* representing an integer amount | ||
* @returns An Amount object | ||
* @returns An Issue object | ||
*/ | ||
@@ -40,8 +41,17 @@ static from(value) { | ||
if (isIssueObject(value)) { | ||
const currency = currency_1.Currency.from(value.currency).toBytes(); | ||
if (value.issuer == null) { | ||
if (value.currency) { | ||
const currency = currency_1.Currency.from(value.currency.toString()).toBytes(); | ||
//IOU case | ||
if (value.issuer) { | ||
const issuer = account_id_1.AccountID.from(value.issuer.toString()).toBytes(); | ||
return new Issue((0, utils_1.concat)([currency, issuer])); | ||
} | ||
//XRP case | ||
return new Issue(currency); | ||
} | ||
const issuer = account_id_1.AccountID.from(value.issuer).toBytes(); | ||
return new Issue((0, utils_1.concat)([currency, issuer])); | ||
// MPT case | ||
if (value.mpt_issuance_id) { | ||
const mptIssuanceIdBytes = hash_192_1.Hash192.from(value.mpt_issuance_id.toString()).toBytes(); | ||
return new Issue(mptIssuanceIdBytes); | ||
} | ||
} | ||
@@ -54,5 +64,12 @@ throw new Error('Invalid type to construct an Amount'); | ||
* @param parser BinaryParser to read the Amount from | ||
* @returns An Amount object | ||
* @param hint The number of bytes to consume from the parser. | ||
* For an MPT amount, pass 24 (the fixed length for Hash192). | ||
* | ||
* @returns An Issue object | ||
*/ | ||
static fromParser(parser) { | ||
static fromParser(parser, hint) { | ||
if (hint === hash_192_1.Hash192.width) { | ||
const mptBytes = parser.read(hash_192_1.Hash192.width); | ||
return new Issue(mptBytes); | ||
} | ||
const currency = parser.read(20); | ||
@@ -71,2 +88,8 @@ if (new currency_1.Currency(currency).toJSON() === 'XRP') { | ||
toJSON() { | ||
// If the buffer is exactly 24 bytes, treat it as an MPT amount. | ||
if (this.toBytes().length === hash_192_1.Hash192.width) { | ||
return { | ||
mpt_issuance_id: this.toHex().toUpperCase(), | ||
}; | ||
} | ||
const parser = new binary_parser_1.BinaryParser(this.toString()); | ||
@@ -73,0 +96,0 @@ const currency = currency_1.Currency.fromParser(parser); |
@@ -99,7 +99,17 @@ "use strict"; | ||
}, {}); | ||
function isValidFieldInstance(f) { | ||
return (f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized); | ||
} | ||
let sorted = Object.keys(xAddressDecoded) | ||
.map((f) => definitions.field[f]) | ||
.filter((f) => f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized) | ||
.map((f) => { | ||
if (!(f in definitions.field)) { | ||
if (f[0] === f[0].toLowerCase()) | ||
return undefined; | ||
throw new Error(`Field ${f} is not defined in the definitions`); | ||
} | ||
return definitions.field[f]; | ||
}) | ||
.filter(isValidFieldInstance) | ||
.sort((a, b) => { | ||
@@ -106,0 +116,0 @@ return a.ordinal - b.ordinal; |
{ | ||
"name": "ripple-binary-codec", | ||
"version": "2.3.0", | ||
"version": "2.4.0-smartescrow.0", | ||
"description": "XRP Ledger binary codec", | ||
@@ -24,3 +24,4 @@ "files": [ | ||
"test:browser": "npm run build && karma start ./karma.config.js", | ||
"lint": "eslint . --ext .ts --ext .test.js" | ||
"lint": "eslint . --ext .ts --ext .test.js", | ||
"generateDefinitions": "node ./tools/generateDefinitions.js" | ||
}, | ||
@@ -47,3 +48,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "5b16d0cfe347aea1d5fb3c8f8a6e9af8d78ac99e" | ||
"gitHead": "4934ad3686c563c13e7e2832f57e7890dd96fd1a" | ||
} |
@@ -674,2 +674,22 @@ { | ||
[ | ||
"ExtensionComputeLimit", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 52, | ||
"type": "UInt32" | ||
} | ||
], | ||
[ | ||
"ExtensionSizeLimit", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 53, | ||
"type": "UInt32" | ||
} | ||
], | ||
[ | ||
"IndexNext", | ||
@@ -917,6 +937,6 @@ { | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 27, | ||
"type": "UInt64" | ||
@@ -928,6 +948,6 @@ } | ||
{ | ||
"nth": 28, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 28, | ||
"type": "UInt64" | ||
@@ -1259,6 +1279,6 @@ } | ||
{ | ||
"nth": 34, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 34, | ||
"type": "Hash256" | ||
@@ -1850,6 +1870,16 @@ } | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 31, | ||
"isVLEncoded": true, | ||
"type": "Blob" | ||
} | ||
], | ||
[ | ||
"FinishFunction", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 32, | ||
"type": "Blob" | ||
@@ -2031,6 +2061,6 @@ } | ||
{ | ||
"nth": 24, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 24, | ||
"type": "AccountID" | ||
@@ -2040,2 +2070,12 @@ } | ||
[ | ||
"Number", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 1, | ||
"type": "Number" | ||
} | ||
], | ||
[ | ||
"TransactionMetaData", | ||
@@ -2333,6 +2373,6 @@ { | ||
{ | ||
"nth": 33, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 33, | ||
"type": "STObject" | ||
@@ -2534,6 +2574,6 @@ } | ||
{ | ||
"nth": 26, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 26, | ||
"type": "STArray" | ||
@@ -2545,6 +2585,6 @@ } | ||
{ | ||
"nth": 27, | ||
"isVLEncoded": false, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 27, | ||
"type": "STArray" | ||
@@ -2554,7 +2594,8 @@ } | ||
[ | ||
"AcceptedCredentials", { | ||
"nth": 28, | ||
"isVLEncoded": false, | ||
"AcceptedCredentials", | ||
{ | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": false, | ||
"nth": 28, | ||
"type": "STArray" | ||
@@ -2746,6 +2787,6 @@ } | ||
{ | ||
"nth": 5, | ||
"isVLEncoded": true, | ||
"isSerialized": true, | ||
"isSigningField": true, | ||
"isVLEncoded": true, | ||
"nth": 5, | ||
"type": "Vector256" | ||
@@ -2881,2 +2922,3 @@ } | ||
"Check": 67, | ||
"Credential": 129, | ||
"DID": 73, | ||
@@ -2896,3 +2938,2 @@ "DepositPreauth": 112, | ||
"Oracle": 128, | ||
"Credential": 129, | ||
"PayChannel": 120, | ||
@@ -2927,2 +2968,3 @@ "PermissionedDomain": 130, | ||
"tecHAS_OBLIGATIONS": 151, | ||
"tecHOOK_REJECTED": 153, | ||
"tecINCOMPLETE": 169, | ||
@@ -2969,2 +3011,3 @@ "tecINSUFFICIENT_FUNDS": 159, | ||
"tecUNFUNDED_PAYMENT": 104, | ||
"tecWASM_REJECTED": 194, | ||
"tecXCHAIN_ACCOUNT_CREATE_PAST": 181, | ||
@@ -2987,2 +3030,3 @@ "tecXCHAIN_ACCOUNT_CREATE_TOO_MANY": 182, | ||
"tecXCHAIN_WRONG_CHAIN": 176, | ||
"tefALREADY": -198, | ||
@@ -3010,2 +3054,3 @@ "tefBAD_ADD_AUTH": -197, | ||
"tefWRONG_PRIOR": -189, | ||
"telBAD_DOMAIN": -398, | ||
@@ -3028,2 +3073,3 @@ "telBAD_PATH_COUNT": -397, | ||
"telWRONG_NETWORK": -386, | ||
"temARRAY_EMPTY": -253, | ||
@@ -3078,2 +3124,3 @@ "temARRAY_TOO_LARGE": -252, | ||
"temXCHAIN_EQUAL_DOOR_ACCOUNTS": -260, | ||
"terFUNDS_SPENT": -98, | ||
@@ -3092,2 +3139,3 @@ "terINSUF_FEE_B": -97, | ||
"terRETRY": -99, | ||
"tesSUCCESS": 0 | ||
@@ -3109,4 +3157,4 @@ }, | ||
"Clawback": 30, | ||
"CredentialAccept": 59, | ||
"CredentialCreate": 58, | ||
"CredentialAccept": 59, | ||
"CredentialDelete": 60, | ||
@@ -3140,4 +3188,4 @@ "DIDDelete": 50, | ||
"PaymentChannelFund": 14, | ||
"PermissionedDomainDelete": 63, | ||
"PermissionedDomainSet": 62, | ||
"PermissionedDomainDelete": 63, | ||
"SetFee": 101, | ||
@@ -3172,2 +3220,3 @@ "SetRegularKey": 5, | ||
"NotPresent": 0, | ||
"Number": 9, | ||
"PathSet": 18, | ||
@@ -3174,0 +3223,0 @@ "STArray": 15, |
@@ -7,20 +7,31 @@ import { concat } from '@xrplf/isomorphic/utils' | ||
import { JsonObject, SerializedType } from './serialized-type' | ||
import { Hash192 } from './hash-192' | ||
/** | ||
* Interface for JSON objects that represent amounts | ||
*/ | ||
interface IssueObject extends JsonObject { | ||
interface XRPIssue extends JsonObject { | ||
currency: string | ||
issuer?: string | ||
} | ||
interface IOUIssue extends JsonObject { | ||
currency: string | ||
issuer: string | ||
} | ||
interface MPTIssue extends JsonObject { | ||
mpt_issuance_id: string | ||
} | ||
/** | ||
* Type guard for AmountObject | ||
* Interface for JSON objects that represent issues | ||
*/ | ||
type IssueObject = XRPIssue | IOUIssue | MPTIssue | ||
/** | ||
* Type guard for Issue Object | ||
*/ | ||
function isIssueObject(arg): arg is IssueObject { | ||
const keys = Object.keys(arg).sort() | ||
if (keys.length === 1) { | ||
return keys[0] === 'currency' | ||
} | ||
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer' | ||
const isXRP = keys.length === 1 && keys[0] === 'currency' | ||
const isIOU = | ||
keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer' | ||
const isMPT = keys.length === 1 && keys[0] === 'mpt_issuance_id' | ||
return isXRP || isIOU || isMPT | ||
} | ||
@@ -41,5 +52,5 @@ | ||
* | ||
* @param value An Amount, object representing an IOU, or a string | ||
* @param value An Amount, object representing an IOU, MPTAmount, or a string | ||
* representing an integer amount | ||
* @returns An Amount object | ||
* @returns An Issue object | ||
*/ | ||
@@ -52,8 +63,22 @@ static from<T extends Issue | IssueObject>(value: T): Issue { | ||
if (isIssueObject(value)) { | ||
const currency = Currency.from(value.currency).toBytes() | ||
if (value.issuer == null) { | ||
if (value.currency) { | ||
const currency = Currency.from(value.currency.toString()).toBytes() | ||
//IOU case | ||
if (value.issuer) { | ||
const issuer = AccountID.from(value.issuer.toString()).toBytes() | ||
return new Issue(concat([currency, issuer])) | ||
} | ||
//XRP case | ||
return new Issue(currency) | ||
} | ||
const issuer = AccountID.from(value.issuer).toBytes() | ||
return new Issue(concat([currency, issuer])) | ||
// MPT case | ||
if (value.mpt_issuance_id) { | ||
const mptIssuanceIdBytes = Hash192.from( | ||
value.mpt_issuance_id.toString(), | ||
).toBytes() | ||
return new Issue(mptIssuanceIdBytes) | ||
} | ||
} | ||
@@ -68,5 +93,12 @@ | ||
* @param parser BinaryParser to read the Amount from | ||
* @returns An Amount object | ||
* @param hint The number of bytes to consume from the parser. | ||
* For an MPT amount, pass 24 (the fixed length for Hash192). | ||
* | ||
* @returns An Issue object | ||
*/ | ||
static fromParser(parser: BinaryParser): Issue { | ||
static fromParser(parser: BinaryParser, hint?: number): Issue { | ||
if (hint === Hash192.width) { | ||
const mptBytes = parser.read(Hash192.width) | ||
return new Issue(mptBytes) | ||
} | ||
const currency = parser.read(20) | ||
@@ -86,3 +118,11 @@ if (new Currency(currency).toJSON() === 'XRP') { | ||
toJSON(): IssueObject { | ||
// If the buffer is exactly 24 bytes, treat it as an MPT amount. | ||
if (this.toBytes().length === Hash192.width) { | ||
return { | ||
mpt_issuance_id: this.toHex().toUpperCase(), | ||
} | ||
} | ||
const parser = new BinaryParser(this.toString()) | ||
const currency = Currency.fromParser(parser) as Currency | ||
@@ -89,0 +129,0 @@ if (currency.toJSON() === 'XRP') { |
@@ -119,10 +119,21 @@ import { | ||
function isValidFieldInstance( | ||
f: FieldInstance | undefined, | ||
): f is FieldInstance { | ||
return ( | ||
f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized | ||
) | ||
} | ||
let sorted = Object.keys(xAddressDecoded) | ||
.map((f: string): FieldInstance => definitions.field[f] as FieldInstance) | ||
.filter( | ||
(f: FieldInstance): boolean => | ||
f !== undefined && | ||
xAddressDecoded[f.name] !== undefined && | ||
f.isSerialized, | ||
) | ||
.map((f: string): FieldInstance | undefined => { | ||
if (!(f in definitions.field)) { | ||
if (f[0] === f[0].toLowerCase()) return undefined | ||
throw new Error(`Field ${f} is not defined in the definitions`) | ||
} | ||
return definitions.field[f] as FieldInstance | ||
}) | ||
.filter(isValidFieldInstance) | ||
.sort((a, b) => { | ||
@@ -129,0 +140,0 @@ return a.ordinal - b.ordinal |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
995412
1.76%291
0.34%26689
1.16%1
Infinity%