mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-22 05:05:48 +00:00
run prettier on all packages
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
/* eslint-disable func-style */
|
||||
|
||||
import { coreTypes } from "./types";
|
||||
import { BinaryParser } from "./serdes/binary-parser";
|
||||
import { AccountID } from "./types/account-id";
|
||||
import { HashPrefix } from "./hash-prefixes";
|
||||
import { BinarySerializer, BytesList } from "./serdes/binary-serializer";
|
||||
import { sha512Half, transactionID } from "./hashes";
|
||||
import { FieldInstance } from "./enums";
|
||||
import { STObject } from "./types/st-object";
|
||||
import { JsonObject } from "./types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as bigInt from "big-integer";
|
||||
import { coreTypes } from './types'
|
||||
import { BinaryParser } from './serdes/binary-parser'
|
||||
import { AccountID } from './types/account-id'
|
||||
import { HashPrefix } from './hash-prefixes'
|
||||
import { BinarySerializer, BytesList } from './serdes/binary-serializer'
|
||||
import { sha512Half, transactionID } from './hashes'
|
||||
import { FieldInstance } from './enums'
|
||||
import { STObject } from './types/st-object'
|
||||
import { JsonObject } from './types/serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
import * as bigInt from 'big-integer'
|
||||
|
||||
/**
|
||||
* Construct a BinaryParser
|
||||
@@ -18,7 +18,7 @@ import * as bigInt from "big-integer";
|
||||
* @param bytes hex-string to construct BinaryParser from
|
||||
* @returns A BinaryParser
|
||||
*/
|
||||
const makeParser = (bytes: string): BinaryParser => new BinaryParser(bytes);
|
||||
const makeParser = (bytes: string): BinaryParser => new BinaryParser(bytes)
|
||||
|
||||
/**
|
||||
* Parse BinaryParser into JSON
|
||||
@@ -27,7 +27,7 @@ const makeParser = (bytes: string): BinaryParser => new BinaryParser(bytes);
|
||||
* @returns JSON for the bytes in the BinaryParser
|
||||
*/
|
||||
const readJSON = (parser: BinaryParser): JsonObject =>
|
||||
(parser.readType(coreTypes.STObject) as STObject).toJSON();
|
||||
(parser.readType(coreTypes.STObject) as STObject).toJSON()
|
||||
|
||||
/**
|
||||
* Parse a hex-string into its JSON interpretation
|
||||
@@ -35,7 +35,7 @@ const readJSON = (parser: BinaryParser): JsonObject =>
|
||||
* @param bytes hex-string to parse into JSON
|
||||
* @returns JSON
|
||||
*/
|
||||
const binaryToJSON = (bytes: string): JsonObject => readJSON(makeParser(bytes));
|
||||
const binaryToJSON = (bytes: string): JsonObject => readJSON(makeParser(bytes))
|
||||
|
||||
/**
|
||||
* Interface for passing parameters to SerializeObject
|
||||
@@ -43,9 +43,9 @@ const binaryToJSON = (bytes: string): JsonObject => readJSON(makeParser(bytes));
|
||||
* @field set signingFieldOnly to true if you want to serialize only signing fields
|
||||
*/
|
||||
interface OptionObject {
|
||||
prefix?: Buffer;
|
||||
suffix?: Buffer;
|
||||
signingFieldsOnly?: boolean;
|
||||
prefix?: Buffer
|
||||
suffix?: Buffer
|
||||
signingFieldsOnly?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,23 +56,23 @@ interface OptionObject {
|
||||
* @returns A Buffer containing the serialized object
|
||||
*/
|
||||
function serializeObject(object: JsonObject, opts: OptionObject = {}): Buffer {
|
||||
const { prefix, suffix, signingFieldsOnly = false } = opts;
|
||||
const bytesList = new BytesList();
|
||||
const { prefix, suffix, signingFieldsOnly = false } = opts
|
||||
const bytesList = new BytesList()
|
||||
|
||||
if (prefix) {
|
||||
bytesList.put(prefix);
|
||||
bytesList.put(prefix)
|
||||
}
|
||||
|
||||
const filter = signingFieldsOnly
|
||||
? (f: FieldInstance): boolean => f.isSigningField
|
||||
: undefined;
|
||||
coreTypes.STObject.from(object, filter).toBytesSink(bytesList);
|
||||
: undefined
|
||||
coreTypes.STObject.from(object, filter).toBytesSink(bytesList)
|
||||
|
||||
if (suffix) {
|
||||
bytesList.put(suffix);
|
||||
bytesList.put(suffix)
|
||||
}
|
||||
|
||||
return bytesList.toBytes();
|
||||
return bytesList.toBytes()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,17 +84,17 @@ function serializeObject(object: JsonObject, opts: OptionObject = {}): Buffer {
|
||||
*/
|
||||
function signingData(
|
||||
transaction: JsonObject,
|
||||
prefix: Buffer = HashPrefix.transactionSig
|
||||
prefix: Buffer = HashPrefix.transactionSig,
|
||||
): Buffer {
|
||||
return serializeObject(transaction, { prefix, signingFieldsOnly: true });
|
||||
return serializeObject(transaction, { prefix, signingFieldsOnly: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface describing fields required for a Claim
|
||||
*/
|
||||
interface ClaimObject extends JsonObject {
|
||||
channel: string;
|
||||
amount: string | number;
|
||||
channel: string
|
||||
amount: string | number
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,17 +104,17 @@ interface ClaimObject extends JsonObject {
|
||||
* @returns the serialized object with appropriate prefix
|
||||
*/
|
||||
function signingClaimData(claim: ClaimObject): Buffer {
|
||||
const num = bigInt(String(claim.amount));
|
||||
const prefix = HashPrefix.paymentChannelClaim;
|
||||
const channel = coreTypes.Hash256.from(claim.channel).toBytes();
|
||||
const amount = coreTypes.UInt64.from(num).toBytes();
|
||||
const num = bigInt(String(claim.amount))
|
||||
const prefix = HashPrefix.paymentChannelClaim
|
||||
const channel = coreTypes.Hash256.from(claim.channel).toBytes()
|
||||
const amount = coreTypes.UInt64.from(num).toBytes()
|
||||
|
||||
const bytesList = new BytesList();
|
||||
const bytesList = new BytesList()
|
||||
|
||||
bytesList.put(prefix);
|
||||
bytesList.put(channel);
|
||||
bytesList.put(amount);
|
||||
return bytesList.toBytes();
|
||||
bytesList.put(prefix)
|
||||
bytesList.put(channel)
|
||||
bytesList.put(amount)
|
||||
return bytesList.toBytes()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,15 +126,15 @@ function signingClaimData(claim: ClaimObject): Buffer {
|
||||
*/
|
||||
function multiSigningData(
|
||||
transaction: JsonObject,
|
||||
signingAccount: string | AccountID
|
||||
signingAccount: string | AccountID,
|
||||
): Buffer {
|
||||
const prefix = HashPrefix.transactionMultiSig;
|
||||
const suffix = coreTypes.AccountID.from(signingAccount).toBytes();
|
||||
const prefix = HashPrefix.transactionMultiSig
|
||||
const suffix = coreTypes.AccountID.from(signingAccount).toBytes()
|
||||
return serializeObject(transaction, {
|
||||
prefix,
|
||||
suffix,
|
||||
signingFieldsOnly: true,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
export {
|
||||
@@ -151,4 +151,4 @@ export {
|
||||
binaryToJSON,
|
||||
sha512Half,
|
||||
transactionID,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,14 +4,14 @@ import {
|
||||
LedgerEntryType,
|
||||
Type,
|
||||
TransactionResult,
|
||||
} from "./enums";
|
||||
import * as types from "./types";
|
||||
import * as binary from "./binary";
|
||||
import { ShaMap } from "./shamap";
|
||||
import * as ledgerHashes from "./ledger-hashes";
|
||||
import * as hashes from "./hashes";
|
||||
import { quality } from "./quality";
|
||||
import { HashPrefix } from "./hash-prefixes";
|
||||
} from './enums'
|
||||
import * as types from './types'
|
||||
import * as binary from './binary'
|
||||
import { ShaMap } from './shamap'
|
||||
import * as ledgerHashes from './ledger-hashes'
|
||||
import * as hashes from './hashes'
|
||||
import { quality } from './quality'
|
||||
import { HashPrefix } from './hash-prefixes'
|
||||
|
||||
export {
|
||||
hashes,
|
||||
@@ -26,4 +26,4 @@ export {
|
||||
HashPrefix,
|
||||
ShaMap,
|
||||
types,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
import * as enums from "./definitions.json";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as enums from './definitions.json'
|
||||
import { SerializedType } from '../types/serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const TYPE_WIDTH = 2;
|
||||
const LEDGER_ENTRY_WIDTH = 2;
|
||||
const TRANSACTION_TYPE_WIDTH = 2;
|
||||
const TRANSACTION_RESULT_WIDTH = 1;
|
||||
const TYPE_WIDTH = 2
|
||||
const LEDGER_ENTRY_WIDTH = 2
|
||||
const TRANSACTION_TYPE_WIDTH = 2
|
||||
const TRANSACTION_RESULT_WIDTH = 1
|
||||
|
||||
/*
|
||||
* @brief: Serialize a field based on type_code and Field.nth
|
||||
*/
|
||||
function fieldHeader(type: number, nth: number): Buffer {
|
||||
const header: Array<number> = [];
|
||||
const header: Array<number> = []
|
||||
if (type < 16) {
|
||||
if (nth < 16) {
|
||||
header.push((type << 4) | nth);
|
||||
header.push((type << 4) | nth)
|
||||
} else {
|
||||
header.push(type << 4, nth);
|
||||
header.push(type << 4, nth)
|
||||
}
|
||||
} else if (nth < 16) {
|
||||
header.push(nth, type);
|
||||
header.push(nth, type)
|
||||
} else {
|
||||
header.push(0, type, nth);
|
||||
header.push(0, type, nth)
|
||||
}
|
||||
return Buffer.from(header);
|
||||
return Buffer.from(header)
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
|
||||
*/
|
||||
class Bytes {
|
||||
readonly bytes: Uint8Array;
|
||||
readonly bytes: Uint8Array
|
||||
|
||||
constructor(
|
||||
readonly name: string,
|
||||
readonly ordinal: number,
|
||||
readonly ordinalWidth: number
|
||||
readonly ordinalWidth: number,
|
||||
) {
|
||||
this.bytes = Buffer.alloc(ordinalWidth);
|
||||
this.bytes = Buffer.alloc(ordinalWidth)
|
||||
for (let i = 0; i < ordinalWidth; i++) {
|
||||
this.bytes[ordinalWidth - i - 1] = (ordinal >>> (i * 8)) & 0xff;
|
||||
this.bytes[ordinalWidth - i - 1] = (ordinal >>> (i * 8)) & 0xff
|
||||
}
|
||||
}
|
||||
|
||||
toJSON(): string {
|
||||
return this.name;
|
||||
return this.name
|
||||
}
|
||||
|
||||
toBytesSink(sink): void {
|
||||
sink.put(this.bytes);
|
||||
sink.put(this.bytes)
|
||||
}
|
||||
|
||||
toBytes(): Uint8Array {
|
||||
return this.bytes;
|
||||
return this.bytes
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,17 +62,17 @@ class Bytes {
|
||||
class BytesLookup {
|
||||
constructor(types: Record<string, number>, readonly ordinalWidth: number) {
|
||||
Object.entries(types).forEach(([k, v]) => {
|
||||
this[k] = new Bytes(k, v, ordinalWidth);
|
||||
this[v.toString()] = this[k];
|
||||
});
|
||||
this[k] = new Bytes(k, v, ordinalWidth)
|
||||
this[v.toString()] = this[k]
|
||||
})
|
||||
}
|
||||
|
||||
from(value: Bytes | string): Bytes {
|
||||
return value instanceof Bytes ? value : (this[value] as Bytes);
|
||||
return value instanceof Bytes ? value : (this[value] as Bytes)
|
||||
}
|
||||
|
||||
fromParser(parser): Bytes {
|
||||
return this.from(parser.readUIntN(this.ordinalWidth).toString());
|
||||
return this.from(parser.readUIntN(this.ordinalWidth).toString())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,28 +80,28 @@ class BytesLookup {
|
||||
* type FieldInfo is the type of the objects containing information about each field in definitions.json
|
||||
*/
|
||||
interface FieldInfo {
|
||||
nth: number;
|
||||
isVLEncoded: boolean;
|
||||
isSerialized: boolean;
|
||||
isSigningField: boolean;
|
||||
type: string;
|
||||
nth: number
|
||||
isVLEncoded: boolean
|
||||
isSerialized: boolean
|
||||
isSigningField: boolean
|
||||
type: string
|
||||
}
|
||||
|
||||
interface FieldInstance {
|
||||
readonly nth: number;
|
||||
readonly isVariableLengthEncoded: boolean;
|
||||
readonly isSerialized: boolean;
|
||||
readonly isSigningField: boolean;
|
||||
readonly type: Bytes;
|
||||
readonly ordinal: number;
|
||||
readonly name: string;
|
||||
readonly header: Buffer;
|
||||
readonly associatedType: typeof SerializedType;
|
||||
readonly nth: number
|
||||
readonly isVariableLengthEncoded: boolean
|
||||
readonly isSerialized: boolean
|
||||
readonly isSigningField: boolean
|
||||
readonly type: Bytes
|
||||
readonly ordinal: number
|
||||
readonly name: string
|
||||
readonly header: Buffer
|
||||
readonly associatedType: typeof SerializedType
|
||||
}
|
||||
|
||||
function buildField([name, info]: [string, FieldInfo]): FieldInstance {
|
||||
const typeOrdinal = enums.TYPES[info.type];
|
||||
const field = fieldHeader(typeOrdinal, info.nth);
|
||||
const typeOrdinal = enums.TYPES[info.type]
|
||||
const field = fieldHeader(typeOrdinal, info.nth)
|
||||
return {
|
||||
name: name,
|
||||
nth: info.nth,
|
||||
@@ -112,7 +112,7 @@ function buildField([name, info]: [string, FieldInfo]): FieldInstance {
|
||||
type: new Bytes(info.type, typeOrdinal, TYPE_WIDTH),
|
||||
header: field,
|
||||
associatedType: SerializedType, // For later assignment in ./types/index.js
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -121,30 +121,30 @@ function buildField([name, info]: [string, FieldInfo]): FieldInstance {
|
||||
class FieldLookup {
|
||||
constructor(fields: Array<[string, FieldInfo]>) {
|
||||
fields.forEach(([k, v]) => {
|
||||
this[k] = buildField([k, v]);
|
||||
this[this[k].ordinal.toString()] = this[k];
|
||||
});
|
||||
this[k] = buildField([k, v])
|
||||
this[this[k].ordinal.toString()] = this[k]
|
||||
})
|
||||
}
|
||||
|
||||
fromString(value: string): FieldInstance {
|
||||
return this[value] as FieldInstance;
|
||||
return this[value] as FieldInstance
|
||||
}
|
||||
}
|
||||
|
||||
const Type = new BytesLookup(enums.TYPES, TYPE_WIDTH);
|
||||
const Type = new BytesLookup(enums.TYPES, TYPE_WIDTH)
|
||||
const LedgerEntryType = new BytesLookup(
|
||||
enums.LEDGER_ENTRY_TYPES,
|
||||
LEDGER_ENTRY_WIDTH
|
||||
);
|
||||
LEDGER_ENTRY_WIDTH,
|
||||
)
|
||||
const TransactionType = new BytesLookup(
|
||||
enums.TRANSACTION_TYPES,
|
||||
TRANSACTION_TYPE_WIDTH
|
||||
);
|
||||
TRANSACTION_TYPE_WIDTH,
|
||||
)
|
||||
const TransactionResult = new BytesLookup(
|
||||
enums.TRANSACTION_RESULTS,
|
||||
TRANSACTION_RESULT_WIDTH
|
||||
);
|
||||
const Field = new FieldLookup(enums.FIELDS as Array<[string, FieldInfo]>);
|
||||
TRANSACTION_RESULT_WIDTH,
|
||||
)
|
||||
const Field = new FieldLookup(enums.FIELDS as Array<[string, FieldInfo]>)
|
||||
|
||||
export {
|
||||
Field,
|
||||
@@ -153,4 +153,4 @@ export {
|
||||
LedgerEntryType,
|
||||
TransactionResult,
|
||||
TransactionType,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -97,38 +97,38 @@ const input = {
|
||||
tecKILLED: 150,
|
||||
tecHAS_OBLIGATIONS: 151,
|
||||
tecTOO_SOON: 152,
|
||||
};
|
||||
}
|
||||
|
||||
let startingFromTemBADSENDXRPPATHS = -284;
|
||||
let startingFromTemBADSENDXRPPATHS = -284
|
||||
|
||||
let startingFromTefFAILURE = -199;
|
||||
let startingFromTefFAILURE = -199
|
||||
|
||||
let startingFromTerRETRY = -99;
|
||||
let startingFromTerRETRY = -99
|
||||
|
||||
const tesSUCCESS = 0;
|
||||
const tesSUCCESS = 0
|
||||
|
||||
let startingFromTecCLAIM = 100;
|
||||
let startingFromTecCLAIM = 100
|
||||
|
||||
const startingFromTecDIRFULL = 121;
|
||||
const startingFromTecDIRFULL = 121
|
||||
|
||||
let previousKey = "tem";
|
||||
let previousKey = 'tem'
|
||||
Object.keys(input).forEach((key) => {
|
||||
if (key.substring(0, 3) !== previousKey.substring(0, 3)) {
|
||||
console.log();
|
||||
previousKey = key;
|
||||
console.log()
|
||||
previousKey = key
|
||||
}
|
||||
if (key.substring(0, 3) === "tem") {
|
||||
console.log(` "${key}": ${startingFromTemBADSENDXRPPATHS++},`);
|
||||
} else if (key.substring(0, 3) === "tef") {
|
||||
console.log(` "${key}": ${startingFromTefFAILURE++},`);
|
||||
} else if (key.substring(0, 3) === "ter") {
|
||||
console.log(` "${key}": ${startingFromTerRETRY++},`);
|
||||
} else if (key.substring(0, 3) === "tes") {
|
||||
console.log(` "${key}": ${tesSUCCESS},`);
|
||||
} else if (key.substring(0, 3) === "tec") {
|
||||
if (key === "tecDIR_FULL") {
|
||||
startingFromTecCLAIM = startingFromTecDIRFULL;
|
||||
if (key.substring(0, 3) === 'tem') {
|
||||
console.log(` "${key}": ${startingFromTemBADSENDXRPPATHS++},`)
|
||||
} else if (key.substring(0, 3) === 'tef') {
|
||||
console.log(` "${key}": ${startingFromTefFAILURE++},`)
|
||||
} else if (key.substring(0, 3) === 'ter') {
|
||||
console.log(` "${key}": ${startingFromTerRETRY++},`)
|
||||
} else if (key.substring(0, 3) === 'tes') {
|
||||
console.log(` "${key}": ${tesSUCCESS},`)
|
||||
} else if (key.substring(0, 3) === 'tec') {
|
||||
if (key === 'tecDIR_FULL') {
|
||||
startingFromTecCLAIM = startingFromTecDIRFULL
|
||||
}
|
||||
console.log(` "${key}": ${startingFromTecCLAIM++},`);
|
||||
console.log(` "${key}": ${startingFromTecCLAIM++},`)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Buffer } from "buffer/";
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Write a 32 bit integer to a Buffer
|
||||
@@ -7,9 +7,9 @@ import { Buffer } from "buffer/";
|
||||
* @returns a buffer with the bytes representation of uint32
|
||||
*/
|
||||
function bytes(uint32: number): Buffer {
|
||||
const result = Buffer.alloc(4);
|
||||
result.writeUInt32BE(uint32, 0);
|
||||
return result;
|
||||
const result = Buffer.alloc(4)
|
||||
result.writeUInt32BE(uint32, 0)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +35,6 @@ const HashPrefix: Record<string, Buffer> = {
|
||||
proposal: bytes(0x50525000),
|
||||
// payment channel claim
|
||||
paymentChannelClaim: bytes(0x434c4d00),
|
||||
};
|
||||
}
|
||||
|
||||
export { HashPrefix };
|
||||
export { HashPrefix }
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { HashPrefix } from "./hash-prefixes";
|
||||
import * as createHash from "create-hash";
|
||||
import { Hash256 } from "./types/hash-256";
|
||||
import { BytesList } from "./serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { HashPrefix } from './hash-prefixes'
|
||||
import * as createHash from 'create-hash'
|
||||
import { Hash256 } from './types/hash-256'
|
||||
import { BytesList } from './serdes/binary-serializer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Class for hashing with SHA512
|
||||
* @extends BytesList So SerializedTypes can write bytes to a Sha512Half
|
||||
*/
|
||||
class Sha512Half extends BytesList {
|
||||
private hash: createHash = createHash("sha512");
|
||||
private hash: createHash = createHash('sha512')
|
||||
|
||||
/**
|
||||
* Construct a new Sha512Hash and write bytes this.hash
|
||||
@@ -18,7 +18,7 @@ class Sha512Half extends BytesList {
|
||||
* @returns the new Sha512Hash object
|
||||
*/
|
||||
static put(bytes: Buffer): Sha512Half {
|
||||
return new Sha512Half().put(bytes);
|
||||
return new Sha512Half().put(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,8 +28,8 @@ class Sha512Half extends BytesList {
|
||||
* @returns the Sha512 object
|
||||
*/
|
||||
put(bytes: Buffer): Sha512Half {
|
||||
this.hash.update(bytes);
|
||||
return this;
|
||||
this.hash.update(bytes)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,8 +38,8 @@ class Sha512Half extends BytesList {
|
||||
* @returns half of a SHA512 hash
|
||||
*/
|
||||
finish256(): Buffer {
|
||||
const bytes: Buffer = this.hash.digest();
|
||||
return bytes.slice(0, 32);
|
||||
const bytes: Buffer = this.hash.digest()
|
||||
return bytes.slice(0, 32)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +48,7 @@ class Sha512Half extends BytesList {
|
||||
* @returns a Hash256 object
|
||||
*/
|
||||
finish(): Hash256 {
|
||||
return new Hash256(this.finish256());
|
||||
return new Hash256(this.finish256())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ class Sha512Half extends BytesList {
|
||||
* @returns the sha512half hash of the arguments.
|
||||
*/
|
||||
function sha512Half(...args: Buffer[]): Buffer {
|
||||
const hash = new Sha512Half();
|
||||
args.forEach((a) => hash.put(a));
|
||||
return hash.finish256();
|
||||
const hash = new Sha512Half()
|
||||
args.forEach((a) => hash.put(a))
|
||||
return hash.finish256()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +71,7 @@ function sha512Half(...args: Buffer[]): Buffer {
|
||||
* @returns a Hash256 object
|
||||
*/
|
||||
function transactionID(serialized: Buffer): Hash256 {
|
||||
return new Hash256(sha512Half(HashPrefix.transactionID, serialized));
|
||||
return new Hash256(sha512Half(HashPrefix.transactionID, serialized))
|
||||
}
|
||||
|
||||
export { Sha512Half, sha512Half, transactionID };
|
||||
export { Sha512Half, sha512Half, transactionID }
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import * as assert from "assert";
|
||||
import { quality, binary } from "./coretypes";
|
||||
import { decodeLedgerData } from "./ledger-hashes";
|
||||
import { ClaimObject } from "./binary";
|
||||
import { JsonObject } from "./types/serialized-type";
|
||||
import * as assert from 'assert'
|
||||
import { quality, binary } from './coretypes'
|
||||
import { decodeLedgerData } from './ledger-hashes'
|
||||
import { ClaimObject } from './binary'
|
||||
import { JsonObject } from './types/serialized-type'
|
||||
const {
|
||||
signingData,
|
||||
signingClaimData,
|
||||
multiSigningData,
|
||||
binaryToJSON,
|
||||
serializeObject,
|
||||
} = binary;
|
||||
} = binary
|
||||
|
||||
/**
|
||||
* Decode a transaction
|
||||
@@ -18,8 +18,8 @@ const {
|
||||
* @returns the JSON representation of the transaction
|
||||
*/
|
||||
function decode(binary: string): JsonObject {
|
||||
assert.ok(typeof binary === "string", "binary must be a hex string");
|
||||
return binaryToJSON(binary);
|
||||
assert.ok(typeof binary === 'string', 'binary must be a hex string')
|
||||
return binaryToJSON(binary)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,10 +29,10 @@ function decode(binary: string): JsonObject {
|
||||
* @returns A hex-string of the encoded transaction
|
||||
*/
|
||||
function encode(json: object): string {
|
||||
assert.ok(typeof json === "object");
|
||||
assert.ok(typeof json === 'object')
|
||||
return serializeObject(json as JsonObject)
|
||||
.toString("hex")
|
||||
.toUpperCase();
|
||||
.toString('hex')
|
||||
.toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,10 +43,10 @@ function encode(json: object): string {
|
||||
* @returns a hex string of the encoded transaction
|
||||
*/
|
||||
function encodeForSigning(json: object): string {
|
||||
assert.ok(typeof json === "object");
|
||||
assert.ok(typeof json === 'object')
|
||||
return signingData(json as JsonObject)
|
||||
.toString("hex")
|
||||
.toUpperCase();
|
||||
.toString('hex')
|
||||
.toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,10 +57,10 @@ function encodeForSigning(json: object): string {
|
||||
* @returns a hex string of the encoded transaction
|
||||
*/
|
||||
function encodeForSigningClaim(json: object): string {
|
||||
assert.ok(typeof json === "object");
|
||||
assert.ok(typeof json === 'object')
|
||||
return signingClaimData(json as ClaimObject)
|
||||
.toString("hex")
|
||||
.toUpperCase();
|
||||
.toString('hex')
|
||||
.toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,11 +71,11 @@ function encodeForSigningClaim(json: object): string {
|
||||
* @returns a hex string of the encoded transaction
|
||||
*/
|
||||
function encodeForMultisigning(json: object, signer: string): string {
|
||||
assert.ok(typeof json === "object");
|
||||
assert.equal(json["SigningPubKey"], "");
|
||||
assert.ok(typeof json === 'object')
|
||||
assert.equal(json['SigningPubKey'], '')
|
||||
return multiSigningData(json as JsonObject, signer)
|
||||
.toString("hex")
|
||||
.toUpperCase();
|
||||
.toString('hex')
|
||||
.toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,8 +85,8 @@ function encodeForMultisigning(json: object, signer: string): string {
|
||||
* @returns a hex-string representing the quality
|
||||
*/
|
||||
function encodeQuality(value: string): string {
|
||||
assert.ok(typeof value === "string");
|
||||
return quality.encode(value).toString("hex").toUpperCase();
|
||||
assert.ok(typeof value === 'string')
|
||||
return quality.encode(value).toString('hex').toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,8 +96,8 @@ function encodeQuality(value: string): string {
|
||||
* @returns a string representing the quality
|
||||
*/
|
||||
function decodeQuality(value: string): string {
|
||||
assert.ok(typeof value === "string");
|
||||
return quality.decode(value).toString();
|
||||
assert.ok(typeof value === 'string')
|
||||
return quality.decode(value).toString()
|
||||
}
|
||||
|
||||
export = {
|
||||
@@ -109,4 +109,4 @@ export = {
|
||||
encodeQuality,
|
||||
decodeQuality,
|
||||
decodeLedgerData,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import * as assert from "assert";
|
||||
import { ShaMap, ShaMapNode, ShaMapLeaf } from "./shamap";
|
||||
import { HashPrefix } from "./hash-prefixes";
|
||||
import { Sha512Half } from "./hashes";
|
||||
import { BinarySerializer, serializeObject } from "./binary";
|
||||
import { Hash256 } from "./types/hash-256";
|
||||
import { STObject } from "./types/st-object";
|
||||
import { UInt64 } from "./types/uint-64";
|
||||
import { UInt32 } from "./types/uint-32";
|
||||
import { UInt8 } from "./types/uint-8";
|
||||
import { BinaryParser } from "./serdes/binary-parser";
|
||||
import { JsonObject } from "./types/serialized-type";
|
||||
import * as bigInt from "big-integer";
|
||||
import * as assert from 'assert'
|
||||
import { ShaMap, ShaMapNode, ShaMapLeaf } from './shamap'
|
||||
import { HashPrefix } from './hash-prefixes'
|
||||
import { Sha512Half } from './hashes'
|
||||
import { BinarySerializer, serializeObject } from './binary'
|
||||
import { Hash256 } from './types/hash-256'
|
||||
import { STObject } from './types/st-object'
|
||||
import { UInt64 } from './types/uint-64'
|
||||
import { UInt32 } from './types/uint-32'
|
||||
import { UInt8 } from './types/uint-8'
|
||||
import { BinaryParser } from './serdes/binary-parser'
|
||||
import { JsonObject } from './types/serialized-type'
|
||||
import * as bigInt from 'big-integer'
|
||||
|
||||
/**
|
||||
* Computes the hash of a list of objects
|
||||
@@ -21,19 +21,19 @@ import * as bigInt from "big-integer";
|
||||
*/
|
||||
function computeHash(
|
||||
itemizer: (item: JsonObject) => [Hash256?, ShaMapNode?, ShaMapLeaf?],
|
||||
itemsJson: Array<JsonObject>
|
||||
itemsJson: Array<JsonObject>,
|
||||
): Hash256 {
|
||||
const map = new ShaMap();
|
||||
itemsJson.forEach((item) => map.addItem(...itemizer(item)));
|
||||
return map.hash();
|
||||
const map = new ShaMap()
|
||||
itemsJson.forEach((item) => map.addItem(...itemizer(item)))
|
||||
return map.hash()
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface describing a transaction item
|
||||
*/
|
||||
interface transactionItemObject extends JsonObject {
|
||||
hash: string;
|
||||
metaData: JsonObject;
|
||||
hash: string
|
||||
metaData: JsonObject
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,28 +43,28 @@ interface transactionItemObject extends JsonObject {
|
||||
* @returns a tuple of index and item to be added to SHAMap
|
||||
*/
|
||||
function transactionItemizer(
|
||||
json: transactionItemObject
|
||||
json: transactionItemObject,
|
||||
): [Hash256, ShaMapNode, undefined] {
|
||||
assert.ok(json.hash);
|
||||
const index = Hash256.from(json.hash);
|
||||
assert.ok(json.hash)
|
||||
const index = Hash256.from(json.hash)
|
||||
const item = {
|
||||
hashPrefix() {
|
||||
return HashPrefix.transaction;
|
||||
return HashPrefix.transaction
|
||||
},
|
||||
toBytesSink(sink) {
|
||||
const serializer = new BinarySerializer(sink);
|
||||
serializer.writeLengthEncoded(STObject.from(json));
|
||||
serializer.writeLengthEncoded(STObject.from(json.metaData));
|
||||
const serializer = new BinarySerializer(sink)
|
||||
serializer.writeLengthEncoded(STObject.from(json))
|
||||
serializer.writeLengthEncoded(STObject.from(json.metaData))
|
||||
},
|
||||
} as ShaMapNode;
|
||||
return [index, item, undefined];
|
||||
} as ShaMapNode
|
||||
return [index, item, undefined]
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface describing an entry item
|
||||
*/
|
||||
interface entryItemObject extends JsonObject {
|
||||
index: string;
|
||||
index: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,19 +74,19 @@ interface entryItemObject extends JsonObject {
|
||||
* @returns a tuple of index and item to be added to SHAMap
|
||||
*/
|
||||
function entryItemizer(
|
||||
json: entryItemObject
|
||||
json: entryItemObject,
|
||||
): [Hash256, ShaMapNode, undefined] {
|
||||
const index = Hash256.from(json.index);
|
||||
const bytes = serializeObject(json);
|
||||
const index = Hash256.from(json.index)
|
||||
const bytes = serializeObject(json)
|
||||
const item = {
|
||||
hashPrefix() {
|
||||
return HashPrefix.accountStateEntry;
|
||||
return HashPrefix.accountStateEntry
|
||||
},
|
||||
toBytesSink(sink) {
|
||||
sink.put(bytes);
|
||||
sink.put(bytes)
|
||||
},
|
||||
} as ShaMapNode;
|
||||
return [index, item, undefined];
|
||||
} as ShaMapNode
|
||||
return [index, item, undefined]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,9 +97,9 @@ function entryItemizer(
|
||||
*/
|
||||
function transactionTreeHash(param: Array<JsonObject>): Hash256 {
|
||||
const itemizer = transactionItemizer as (
|
||||
json: JsonObject
|
||||
) => [Hash256, ShaMapNode, undefined];
|
||||
return computeHash(itemizer, param);
|
||||
json: JsonObject,
|
||||
) => [Hash256, ShaMapNode, undefined]
|
||||
return computeHash(itemizer, param)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,24 +110,24 @@ function transactionTreeHash(param: Array<JsonObject>): Hash256 {
|
||||
*/
|
||||
function accountStateHash(param: Array<JsonObject>): Hash256 {
|
||||
const itemizer = entryItemizer as (
|
||||
json: JsonObject
|
||||
) => [Hash256, ShaMapNode, undefined];
|
||||
return computeHash(itemizer, param);
|
||||
json: JsonObject,
|
||||
) => [Hash256, ShaMapNode, undefined]
|
||||
return computeHash(itemizer, param)
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface describing a ledger header
|
||||
*/
|
||||
interface ledgerObject {
|
||||
ledger_index: number;
|
||||
total_coins: string | number | bigInt.BigInteger;
|
||||
parent_hash: string;
|
||||
transaction_hash: string;
|
||||
account_hash: string;
|
||||
parent_close_time: number;
|
||||
close_time: number;
|
||||
close_time_resolution: number;
|
||||
close_flags: number;
|
||||
ledger_index: number
|
||||
total_coins: string | number | bigInt.BigInteger
|
||||
parent_hash: string
|
||||
transaction_hash: string
|
||||
account_hash: string
|
||||
parent_close_time: number
|
||||
close_time: number
|
||||
close_time_resolution: number
|
||||
close_flags: number
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,23 +137,23 @@ interface ledgerObject {
|
||||
* @returns the hash of header
|
||||
*/
|
||||
function ledgerHash(header: ledgerObject): Hash256 {
|
||||
const hash = new Sha512Half();
|
||||
hash.put(HashPrefix.ledgerHeader);
|
||||
assert.ok(header.parent_close_time !== undefined);
|
||||
assert.ok(header.close_flags !== undefined);
|
||||
const hash = new Sha512Half()
|
||||
hash.put(HashPrefix.ledgerHeader)
|
||||
assert.ok(header.parent_close_time !== undefined)
|
||||
assert.ok(header.close_flags !== undefined)
|
||||
|
||||
UInt32.from<number>(header.ledger_index).toBytesSink(hash);
|
||||
UInt32.from<number>(header.ledger_index).toBytesSink(hash)
|
||||
UInt64.from<bigInt.BigInteger>(
|
||||
bigInt(String(header.total_coins))
|
||||
).toBytesSink(hash);
|
||||
Hash256.from<string>(header.parent_hash).toBytesSink(hash);
|
||||
Hash256.from<string>(header.transaction_hash).toBytesSink(hash);
|
||||
Hash256.from<string>(header.account_hash).toBytesSink(hash);
|
||||
UInt32.from<number>(header.parent_close_time).toBytesSink(hash);
|
||||
UInt32.from<number>(header.close_time).toBytesSink(hash);
|
||||
UInt8.from<number>(header.close_time_resolution).toBytesSink(hash);
|
||||
UInt8.from<number>(header.close_flags).toBytesSink(hash);
|
||||
return hash.finish();
|
||||
bigInt(String(header.total_coins)),
|
||||
).toBytesSink(hash)
|
||||
Hash256.from<string>(header.parent_hash).toBytesSink(hash)
|
||||
Hash256.from<string>(header.transaction_hash).toBytesSink(hash)
|
||||
Hash256.from<string>(header.account_hash).toBytesSink(hash)
|
||||
UInt32.from<number>(header.parent_close_time).toBytesSink(hash)
|
||||
UInt32.from<number>(header.close_time).toBytesSink(hash)
|
||||
UInt8.from<number>(header.close_time_resolution).toBytesSink(hash)
|
||||
UInt8.from<number>(header.close_flags).toBytesSink(hash)
|
||||
return hash.finish()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,8 +163,8 @@ function ledgerHash(header: ledgerObject): Hash256 {
|
||||
* @returns A JSON object describing a ledger header
|
||||
*/
|
||||
function decodeLedgerData(binary: string): object {
|
||||
assert.ok(typeof binary === "string", "binary must be a hex string");
|
||||
const parser = new BinaryParser(binary);
|
||||
assert.ok(typeof binary === 'string', 'binary must be a hex string')
|
||||
const parser = new BinaryParser(binary)
|
||||
return {
|
||||
ledger_index: parser.readUInt32(),
|
||||
total_coins: parser.readType(UInt64).valueOf().toString(),
|
||||
@@ -175,7 +175,7 @@ function decodeLedgerData(binary: string): object {
|
||||
close_time: parser.readUInt32(),
|
||||
close_time_resolution: parser.readUInt8(),
|
||||
close_flags: parser.readUInt8(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export { accountStateHash, transactionTreeHash, ledgerHash, decodeLedgerData };
|
||||
export { accountStateHash, transactionTreeHash, ledgerHash, decodeLedgerData }
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { coreTypes } from "./types";
|
||||
import { Decimal } from "decimal.js";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { coreTypes } from './types'
|
||||
import { Decimal } from 'decimal.js'
|
||||
import * as bigInt from 'big-integer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* class for encoding and decoding quality
|
||||
@@ -14,12 +14,12 @@ class quality {
|
||||
* @returns Serialized quality
|
||||
*/
|
||||
static encode(quality: string): Buffer {
|
||||
const decimal = new Decimal(quality);
|
||||
const exponent = decimal.e - 15;
|
||||
const qualityString = decimal.times(`1e${-exponent}`).abs().toString();
|
||||
const bytes = coreTypes.UInt64.from(bigInt(qualityString)).toBytes();
|
||||
bytes[0] = exponent + 100;
|
||||
return bytes;
|
||||
const decimal = new Decimal(quality)
|
||||
const exponent = decimal.e - 15
|
||||
const qualityString = decimal.times(`1e${-exponent}`).abs().toString()
|
||||
const bytes = coreTypes.UInt64.from(bigInt(qualityString)).toBytes()
|
||||
bytes[0] = exponent + 100
|
||||
return bytes
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,11 +29,11 @@ class quality {
|
||||
* @returns deserialized quality
|
||||
*/
|
||||
static decode(quality: string): Decimal {
|
||||
const bytes = Buffer.from(quality, "hex").slice(-8);
|
||||
const exponent = bytes[0] - 100;
|
||||
const mantissa = new Decimal(`0x${bytes.slice(1).toString("hex")}`);
|
||||
return mantissa.times(`1e${exponent}`);
|
||||
const bytes = Buffer.from(quality, 'hex').slice(-8)
|
||||
const exponent = bytes[0] - 100
|
||||
const mantissa = new Decimal(`0x${bytes.slice(1).toString('hex')}`)
|
||||
return mantissa.times(`1e${exponent}`)
|
||||
}
|
||||
}
|
||||
|
||||
export { quality };
|
||||
export { quality }
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import * as assert from "assert";
|
||||
import { Field, FieldInstance } from "../enums";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as assert from 'assert'
|
||||
import { Field, FieldInstance } from '../enums'
|
||||
import { SerializedType } from '../types/serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* BinaryParser is used to compute fields and values from a HexString
|
||||
*/
|
||||
class BinaryParser {
|
||||
private bytes: Buffer;
|
||||
private bytes: Buffer
|
||||
|
||||
/**
|
||||
* Initialize bytes to a hex string
|
||||
@@ -15,7 +15,7 @@ class BinaryParser {
|
||||
* @param hexBytes a hex string
|
||||
*/
|
||||
constructor(hexBytes: string) {
|
||||
this.bytes = Buffer.from(hexBytes, "hex");
|
||||
this.bytes = Buffer.from(hexBytes, 'hex')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,8 +24,8 @@ class BinaryParser {
|
||||
* @returns The first byte of the BinaryParser
|
||||
*/
|
||||
peek(): number {
|
||||
assert.ok(this.bytes.byteLength !== 0);
|
||||
return this.bytes[0];
|
||||
assert.ok(this.bytes.byteLength !== 0)
|
||||
return this.bytes[0]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,8 +34,8 @@ class BinaryParser {
|
||||
* @param n the number of bytes to skip
|
||||
*/
|
||||
skip(n: number): void {
|
||||
assert.ok(n <= this.bytes.byteLength);
|
||||
this.bytes = this.bytes.slice(n);
|
||||
assert.ok(n <= this.bytes.byteLength)
|
||||
this.bytes = this.bytes.slice(n)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,11 +45,11 @@ class BinaryParser {
|
||||
* @return The bytes
|
||||
*/
|
||||
read(n: number): Buffer {
|
||||
assert.ok(n <= this.bytes.byteLength);
|
||||
assert.ok(n <= this.bytes.byteLength)
|
||||
|
||||
const slice = this.bytes.slice(0, n);
|
||||
this.skip(n);
|
||||
return slice;
|
||||
const slice = this.bytes.slice(0, n)
|
||||
this.skip(n)
|
||||
return slice
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,29 +59,29 @@ class BinaryParser {
|
||||
* @return The number represented by those bytes
|
||||
*/
|
||||
readUIntN(n: number): number {
|
||||
assert.ok(0 < n && n <= 4, "invalid n");
|
||||
return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0;
|
||||
assert.ok(0 < n && n <= 4, 'invalid n')
|
||||
return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0
|
||||
}
|
||||
|
||||
readUInt8(): number {
|
||||
return this.readUIntN(1);
|
||||
return this.readUIntN(1)
|
||||
}
|
||||
|
||||
readUInt16(): number {
|
||||
return this.readUIntN(2);
|
||||
return this.readUIntN(2)
|
||||
}
|
||||
|
||||
readUInt32(): number {
|
||||
return this.readUIntN(4);
|
||||
return this.readUIntN(4)
|
||||
}
|
||||
|
||||
size(): number {
|
||||
return this.bytes.byteLength;
|
||||
return this.bytes.byteLength
|
||||
}
|
||||
|
||||
end(customEnd?: number): boolean {
|
||||
const length = this.bytes.byteLength;
|
||||
return length === 0 || (customEnd !== undefined && length <= customEnd);
|
||||
const length = this.bytes.byteLength
|
||||
return length === 0 || (customEnd !== undefined && length <= customEnd)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +90,7 @@ class BinaryParser {
|
||||
* @return The variable length bytes
|
||||
*/
|
||||
readVariableLength(): Buffer {
|
||||
return this.read(this.readVariableLengthLength());
|
||||
return this.read(this.readVariableLengthLength())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,18 +99,18 @@ class BinaryParser {
|
||||
* @return The length of the variable length encoded bytes
|
||||
*/
|
||||
readVariableLengthLength(): number {
|
||||
const b1 = this.readUInt8();
|
||||
const b1 = this.readUInt8()
|
||||
if (b1 <= 192) {
|
||||
return b1;
|
||||
return b1
|
||||
} else if (b1 <= 240) {
|
||||
const b2 = this.readUInt8();
|
||||
return 193 + (b1 - 193) * 256 + b2;
|
||||
const b2 = this.readUInt8()
|
||||
return 193 + (b1 - 193) * 256 + b2
|
||||
} else if (b1 <= 254) {
|
||||
const b2 = this.readUInt8();
|
||||
const b3 = this.readUInt8();
|
||||
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
|
||||
const b2 = this.readUInt8()
|
||||
const b3 = this.readUInt8()
|
||||
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3
|
||||
}
|
||||
throw new Error("Invalid variable length indicator");
|
||||
throw new Error('Invalid variable length indicator')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,25 +119,25 @@ class BinaryParser {
|
||||
* @return Field ordinal
|
||||
*/
|
||||
readFieldOrdinal(): number {
|
||||
let type = this.readUInt8();
|
||||
let nth = type & 15;
|
||||
type >>= 4;
|
||||
let type = this.readUInt8()
|
||||
let nth = type & 15
|
||||
type >>= 4
|
||||
|
||||
if (type === 0) {
|
||||
type = this.readUInt8();
|
||||
type = this.readUInt8()
|
||||
if (type === 0 || type < 16) {
|
||||
throw new Error("Cannot read FieldOrdinal, type_code out of range");
|
||||
throw new Error('Cannot read FieldOrdinal, type_code out of range')
|
||||
}
|
||||
}
|
||||
|
||||
if (nth === 0) {
|
||||
nth = this.readUInt8();
|
||||
nth = this.readUInt8()
|
||||
if (nth === 0 || nth < 16) {
|
||||
throw new Error("Cannot read FieldOrdinal, field_code out of range");
|
||||
throw new Error('Cannot read FieldOrdinal, field_code out of range')
|
||||
}
|
||||
}
|
||||
|
||||
return (type << 16) | nth;
|
||||
return (type << 16) | nth
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +146,7 @@ class BinaryParser {
|
||||
* @return The field represented by the bytes at the head of the BinaryParser
|
||||
*/
|
||||
readField(): FieldInstance {
|
||||
return Field.fromString(this.readFieldOrdinal().toString());
|
||||
return Field.fromString(this.readFieldOrdinal().toString())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,7 +156,7 @@ class BinaryParser {
|
||||
* @return The instance of that type read from the BinaryParser
|
||||
*/
|
||||
readType(type: typeof SerializedType): SerializedType {
|
||||
return type.fromParser(this);
|
||||
return type.fromParser(this)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +166,7 @@ class BinaryParser {
|
||||
* @return The type associated with the given field
|
||||
*/
|
||||
typeForField(field: FieldInstance): typeof SerializedType {
|
||||
return field.associatedType;
|
||||
return field.associatedType
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,20 +176,20 @@ class BinaryParser {
|
||||
* @return The value associated with the given field
|
||||
*/
|
||||
readFieldValue(field: FieldInstance): SerializedType {
|
||||
const type = this.typeForField(field);
|
||||
const type = this.typeForField(field)
|
||||
if (!type) {
|
||||
throw new Error(`unsupported: (${field.name}, ${field.type.name})`);
|
||||
throw new Error(`unsupported: (${field.name}, ${field.type.name})`)
|
||||
}
|
||||
const sizeHint = field.isVariableLengthEncoded
|
||||
? this.readVariableLengthLength()
|
||||
: undefined;
|
||||
const value = type.fromParser(this, sizeHint);
|
||||
: undefined
|
||||
const value = type.fromParser(this, sizeHint)
|
||||
if (value === undefined) {
|
||||
throw new Error(
|
||||
`fromParser for (${field.name}, ${field.type.name}) -> undefined `
|
||||
);
|
||||
`fromParser for (${field.name}, ${field.type.name}) -> undefined `,
|
||||
)
|
||||
}
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,9 +198,9 @@ class BinaryParser {
|
||||
* @return The field and value
|
||||
*/
|
||||
readFieldAndValue(): [FieldInstance, SerializedType] {
|
||||
const field = this.readField();
|
||||
return [field, this.readFieldValue(field)];
|
||||
const field = this.readField()
|
||||
return [field, this.readFieldValue(field)]
|
||||
}
|
||||
}
|
||||
|
||||
export { BinaryParser };
|
||||
export { BinaryParser }
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import * as assert from "assert";
|
||||
import { FieldInstance } from "../enums";
|
||||
import { SerializedType } from "../types/serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as assert from 'assert'
|
||||
import { FieldInstance } from '../enums'
|
||||
import { SerializedType } from '../types/serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Bytes list is a collection of buffer objects
|
||||
*/
|
||||
class BytesList {
|
||||
private bytesArray: Array<Buffer> = [];
|
||||
private bytesArray: Array<Buffer> = []
|
||||
|
||||
/**
|
||||
* Get the total number of bytes in the BytesList
|
||||
@@ -15,7 +15,7 @@ class BytesList {
|
||||
* @return the number of bytes
|
||||
*/
|
||||
public getLength(): number {
|
||||
return Buffer.concat(this.bytesArray).byteLength;
|
||||
return Buffer.concat(this.bytesArray).byteLength
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,9 +25,9 @@ class BytesList {
|
||||
* @return this BytesList
|
||||
*/
|
||||
public put(bytesArg: Buffer): BytesList {
|
||||
const bytes = Buffer.from(bytesArg); // Temporary, to catch instances of Uint8Array being passed in
|
||||
this.bytesArray.push(bytes);
|
||||
return this;
|
||||
const bytes = Buffer.from(bytesArg) // Temporary, to catch instances of Uint8Array being passed in
|
||||
this.bytesArray.push(bytes)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,15 +36,15 @@ class BytesList {
|
||||
* @param list The BytesList to write to
|
||||
*/
|
||||
public toBytesSink(list: BytesList): void {
|
||||
list.put(this.toBytes());
|
||||
list.put(this.toBytes())
|
||||
}
|
||||
|
||||
public toBytes(): Buffer {
|
||||
return Buffer.concat(this.bytesArray);
|
||||
return Buffer.concat(this.bytesArray)
|
||||
}
|
||||
|
||||
toHex(): string {
|
||||
return this.toBytes().toString("hex").toUpperCase();
|
||||
return this.toBytes().toString('hex').toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,10 +52,10 @@ class BytesList {
|
||||
* BinarySerializer is used to write fields and values to buffers
|
||||
*/
|
||||
class BinarySerializer {
|
||||
private sink: BytesList = new BytesList();
|
||||
private sink: BytesList = new BytesList()
|
||||
|
||||
constructor(sink: BytesList) {
|
||||
this.sink = sink;
|
||||
this.sink = sink
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,7 +64,7 @@ class BinarySerializer {
|
||||
* @param value a SerializedType value
|
||||
*/
|
||||
write(value: SerializedType): void {
|
||||
value.toBytesSink(this.sink);
|
||||
value.toBytesSink(this.sink)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +73,7 @@ class BinarySerializer {
|
||||
* @param bytes the bytes to write
|
||||
*/
|
||||
put(bytes: Buffer): void {
|
||||
this.sink.put(bytes);
|
||||
this.sink.put(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +83,7 @@ class BinarySerializer {
|
||||
* @param value a value of that type
|
||||
*/
|
||||
writeType(type: typeof SerializedType, value: SerializedType): void {
|
||||
this.write(type.from(value));
|
||||
this.write(type.from(value))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +92,7 @@ class BinarySerializer {
|
||||
* @param bl BytesList to write to BinarySerializer
|
||||
*/
|
||||
writeBytesList(bl: BytesList): void {
|
||||
bl.toBytesSink(this.sink);
|
||||
bl.toBytesSink(this.sink)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,23 +101,23 @@ class BinarySerializer {
|
||||
* @param length the length of the bytes
|
||||
*/
|
||||
private encodeVariableLength(length: number): Buffer {
|
||||
const lenBytes = Buffer.alloc(3);
|
||||
const lenBytes = Buffer.alloc(3)
|
||||
if (length <= 192) {
|
||||
lenBytes[0] = length;
|
||||
return lenBytes.slice(0, 1);
|
||||
lenBytes[0] = length
|
||||
return lenBytes.slice(0, 1)
|
||||
} else if (length <= 12480) {
|
||||
length -= 193;
|
||||
lenBytes[0] = 193 + (length >>> 8);
|
||||
lenBytes[1] = length & 0xff;
|
||||
return lenBytes.slice(0, 2);
|
||||
length -= 193
|
||||
lenBytes[0] = 193 + (length >>> 8)
|
||||
lenBytes[1] = length & 0xff
|
||||
return lenBytes.slice(0, 2)
|
||||
} else if (length <= 918744) {
|
||||
length -= 12481;
|
||||
lenBytes[0] = 241 + (length >>> 16);
|
||||
lenBytes[1] = (length >> 8) & 0xff;
|
||||
lenBytes[2] = length & 0xff;
|
||||
return lenBytes.slice(0, 3);
|
||||
length -= 12481
|
||||
lenBytes[0] = 241 + (length >>> 16)
|
||||
lenBytes[1] = (length >> 8) & 0xff
|
||||
lenBytes[2] = length & 0xff
|
||||
return lenBytes.slice(0, 3)
|
||||
}
|
||||
throw new Error("Overflow error");
|
||||
throw new Error('Overflow error')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,16 +127,16 @@ class BinarySerializer {
|
||||
* @param value value to write to BinarySerializer
|
||||
*/
|
||||
writeFieldAndValue(field: FieldInstance, value: SerializedType): void {
|
||||
const associatedValue = field.associatedType.from(value);
|
||||
assert.ok(associatedValue.toBytesSink !== undefined);
|
||||
assert.ok(field.name !== undefined);
|
||||
const associatedValue = field.associatedType.from(value)
|
||||
assert.ok(associatedValue.toBytesSink !== undefined)
|
||||
assert.ok(field.name !== undefined)
|
||||
|
||||
this.sink.put(field.header);
|
||||
this.sink.put(field.header)
|
||||
|
||||
if (field.isVariableLengthEncoded) {
|
||||
this.writeLengthEncoded(associatedValue);
|
||||
this.writeLengthEncoded(associatedValue)
|
||||
} else {
|
||||
associatedValue.toBytesSink(this.sink);
|
||||
associatedValue.toBytesSink(this.sink)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,11 +146,11 @@ class BinarySerializer {
|
||||
* @param value length encoded value to write to BytesList
|
||||
*/
|
||||
public writeLengthEncoded(value: SerializedType): void {
|
||||
const bytes = new BytesList();
|
||||
value.toBytesSink(bytes);
|
||||
this.put(this.encodeVariableLength(bytes.getLength()));
|
||||
this.writeBytesList(bytes);
|
||||
const bytes = new BytesList()
|
||||
value.toBytesSink(bytes)
|
||||
this.put(this.encodeVariableLength(bytes.getLength()))
|
||||
this.writeBytesList(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
export { BytesList, BinarySerializer };
|
||||
export { BytesList, BinarySerializer }
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { strict as assert } from "assert";
|
||||
import { coreTypes } from "./types";
|
||||
import { HashPrefix } from "./hash-prefixes";
|
||||
import { Sha512Half } from "./hashes";
|
||||
import { Hash256 } from "./types/hash-256";
|
||||
import { BytesList } from "./serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { strict as assert } from 'assert'
|
||||
import { coreTypes } from './types'
|
||||
import { HashPrefix } from './hash-prefixes'
|
||||
import { Sha512Half } from './hashes'
|
||||
import { Hash256 } from './types/hash-256'
|
||||
import { BytesList } from './serdes/binary-serializer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Abstract class describing a SHAMapNode
|
||||
*/
|
||||
abstract class ShaMapNode {
|
||||
abstract hashPrefix(): Buffer;
|
||||
abstract isLeaf(): boolean;
|
||||
abstract isInner(): boolean;
|
||||
abstract toBytesSink(list: BytesList): void;
|
||||
abstract hash(): Hash256;
|
||||
abstract hashPrefix(): Buffer
|
||||
abstract isLeaf(): boolean
|
||||
abstract isInner(): boolean
|
||||
abstract toBytesSink(list: BytesList): void
|
||||
abstract hash(): Hash256
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,21 +22,21 @@ abstract class ShaMapNode {
|
||||
*/
|
||||
class ShaMapLeaf extends ShaMapNode {
|
||||
constructor(public index: Hash256, public item?: ShaMapNode) {
|
||||
super();
|
||||
super()
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns true as ShaMapLeaf is a leaf node
|
||||
*/
|
||||
isLeaf(): boolean {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns false as ShaMapLeaf is not an inner node
|
||||
*/
|
||||
isInner(): boolean {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +45,7 @@ class ShaMapLeaf extends ShaMapNode {
|
||||
* @returns The hash prefix, unless this.item is undefined, then it returns an empty Buffer
|
||||
*/
|
||||
hashPrefix(): Buffer {
|
||||
return this.item === undefined ? Buffer.alloc(0) : this.item.hashPrefix();
|
||||
return this.item === undefined ? Buffer.alloc(0) : this.item.hashPrefix()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,9 +54,9 @@ class ShaMapLeaf extends ShaMapNode {
|
||||
* @returns hash of this.item concatenated with this.index
|
||||
*/
|
||||
hash(): Hash256 {
|
||||
const hash = Sha512Half.put(this.hashPrefix());
|
||||
this.toBytesSink(hash);
|
||||
return hash.finish();
|
||||
const hash = Sha512Half.put(this.hashPrefix())
|
||||
this.toBytesSink(hash)
|
||||
return hash.finish()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,9 +65,9 @@ class ShaMapLeaf extends ShaMapNode {
|
||||
*/
|
||||
toBytesSink(list: BytesList): void {
|
||||
if (this.item !== undefined) {
|
||||
this.item.toBytesSink(list);
|
||||
this.item.toBytesSink(list)
|
||||
}
|
||||
this.index.toBytesSink(list);
|
||||
this.index.toBytesSink(list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,25 +75,25 @@ class ShaMapLeaf extends ShaMapNode {
|
||||
* Class defining an Inner Node of a SHAMap
|
||||
*/
|
||||
class ShaMapInner extends ShaMapNode {
|
||||
private slotBits = 0;
|
||||
private branches: Array<ShaMapNode> = Array(16);
|
||||
private slotBits = 0
|
||||
private branches: Array<ShaMapNode> = Array(16)
|
||||
|
||||
constructor(private depth: number = 0) {
|
||||
super();
|
||||
super()
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns true as ShaMapInner is an inner node
|
||||
*/
|
||||
isInner(): boolean {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns false as ShaMapInner is not a leaf node
|
||||
*/
|
||||
isLeaf(): boolean {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +102,7 @@ class ShaMapInner extends ShaMapNode {
|
||||
* @returns hash prefix describing an inner node
|
||||
*/
|
||||
hashPrefix(): Buffer {
|
||||
return HashPrefix.innerNode;
|
||||
return HashPrefix.innerNode
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,15 +112,15 @@ class ShaMapInner extends ShaMapNode {
|
||||
* @param branch Branch to add
|
||||
*/
|
||||
setBranch(slot: number, branch: ShaMapNode): void {
|
||||
this.slotBits = this.slotBits | (1 << slot);
|
||||
this.branches[slot] = branch;
|
||||
this.slotBits = this.slotBits | (1 << slot)
|
||||
this.branches[slot] = branch
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns true if node is empty
|
||||
*/
|
||||
empty(): boolean {
|
||||
return this.slotBits === 0;
|
||||
return this.slotBits === 0
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,11 +130,11 @@ class ShaMapInner extends ShaMapNode {
|
||||
*/
|
||||
hash(): Hash256 {
|
||||
if (this.empty()) {
|
||||
return coreTypes.Hash256.ZERO_256;
|
||||
return coreTypes.Hash256.ZERO_256
|
||||
}
|
||||
const hash = Sha512Half.put(this.hashPrefix());
|
||||
this.toBytesSink(hash);
|
||||
return hash.finish();
|
||||
const hash = Sha512Half.put(this.hashPrefix())
|
||||
this.toBytesSink(hash)
|
||||
return hash.finish()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,9 +144,9 @@ class ShaMapInner extends ShaMapNode {
|
||||
*/
|
||||
toBytesSink(list: BytesList): void {
|
||||
for (let i = 0; i < this.branches.length; i++) {
|
||||
const branch = this.branches[i];
|
||||
const hash = branch ? branch.hash() : coreTypes.Hash256.ZERO_256;
|
||||
hash.toBytesSink(list);
|
||||
const branch = this.branches[i]
|
||||
const hash = branch ? branch.hash() : coreTypes.Hash256.ZERO_256
|
||||
hash.toBytesSink(list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,25 +158,25 @@ class ShaMapInner extends ShaMapNode {
|
||||
* @param leaf Leaf node to insert when branch doesn't exist
|
||||
*/
|
||||
addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void {
|
||||
assert.ok(index !== undefined);
|
||||
const nibble = index.nibblet(this.depth);
|
||||
const existing = this.branches[nibble];
|
||||
assert.ok(index !== undefined)
|
||||
const nibble = index.nibblet(this.depth)
|
||||
const existing = this.branches[nibble]
|
||||
|
||||
if (existing === undefined) {
|
||||
this.setBranch(nibble, leaf || new ShaMapLeaf(index, item));
|
||||
this.setBranch(nibble, leaf || new ShaMapLeaf(index, item))
|
||||
} else if (existing instanceof ShaMapLeaf) {
|
||||
const newInner = new ShaMapInner(this.depth + 1);
|
||||
newInner.addItem(existing.index, undefined, existing);
|
||||
newInner.addItem(index, item, leaf);
|
||||
this.setBranch(nibble, newInner);
|
||||
const newInner = new ShaMapInner(this.depth + 1)
|
||||
newInner.addItem(existing.index, undefined, existing)
|
||||
newInner.addItem(index, item, leaf)
|
||||
this.setBranch(nibble, newInner)
|
||||
} else if (existing instanceof ShaMapInner) {
|
||||
existing.addItem(index, item, leaf);
|
||||
existing.addItem(index, item, leaf)
|
||||
} else {
|
||||
throw new Error("invalid ShaMap.addItem call");
|
||||
throw new Error('invalid ShaMap.addItem call')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ShaMap extends ShaMapInner {}
|
||||
|
||||
export { ShaMap, ShaMapNode, ShaMapLeaf };
|
||||
export { ShaMap, ShaMapNode, ShaMapLeaf }
|
||||
|
||||
@@ -3,20 +3,20 @@ import {
|
||||
encodeAccountID,
|
||||
isValidXAddress,
|
||||
xAddressToClassicAddress,
|
||||
} from "ripple-address-codec";
|
||||
import { Hash160 } from "./hash-160";
|
||||
import { Buffer } from "buffer/";
|
||||
} from 'ripple-address-codec'
|
||||
import { Hash160 } from './hash-160'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/;
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/
|
||||
|
||||
/**
|
||||
* Class defining how to encode and decode an AccountID
|
||||
*/
|
||||
class AccountID extends Hash160 {
|
||||
static readonly defaultAccountID: AccountID = new AccountID(Buffer.alloc(20));
|
||||
static readonly defaultAccountID: AccountID = new AccountID(Buffer.alloc(20))
|
||||
|
||||
constructor(bytes?: Buffer) {
|
||||
super(bytes ?? AccountID.defaultAccountID.bytes);
|
||||
super(bytes ?? AccountID.defaultAccountID.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,20 +27,20 @@ class AccountID extends Hash160 {
|
||||
*/
|
||||
static from<T extends Hash160 | string>(value: T): AccountID {
|
||||
if (value instanceof AccountID) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
if (value === "") {
|
||||
return new AccountID();
|
||||
if (typeof value === 'string') {
|
||||
if (value === '') {
|
||||
return new AccountID()
|
||||
}
|
||||
|
||||
return HEX_REGEX.test(value)
|
||||
? new AccountID(Buffer.from(value, "hex"))
|
||||
: this.fromBase58(value);
|
||||
? new AccountID(Buffer.from(value, 'hex'))
|
||||
: this.fromBase58(value)
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct AccountID from value given");
|
||||
throw new Error('Cannot construct AccountID from value given')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,15 +51,15 @@ class AccountID extends Hash160 {
|
||||
*/
|
||||
static fromBase58(value: string): AccountID {
|
||||
if (isValidXAddress(value)) {
|
||||
const classic = xAddressToClassicAddress(value);
|
||||
const classic = xAddressToClassicAddress(value)
|
||||
|
||||
if (classic.tag !== false)
|
||||
throw new Error("Only allowed to have tag on Account or Destination");
|
||||
throw new Error('Only allowed to have tag on Account or Destination')
|
||||
|
||||
value = classic.classicAddress;
|
||||
value = classic.classicAddress
|
||||
}
|
||||
|
||||
return new AccountID(Buffer.from(decodeAccountID(value)));
|
||||
return new AccountID(Buffer.from(decodeAccountID(value)))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +68,7 @@ class AccountID extends Hash160 {
|
||||
* @returns the base58 string for this AccountID
|
||||
*/
|
||||
toJSON(): string {
|
||||
return this.toBase58();
|
||||
return this.toBase58()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,9 +78,9 @@ class AccountID extends Hash160 {
|
||||
*/
|
||||
toBase58(): string {
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
return encodeAccountID(this.bytes as any);
|
||||
return encodeAccountID(this.bytes as any)
|
||||
/* eslint-enable @typescript-eslint/no-explicit-any */
|
||||
}
|
||||
}
|
||||
|
||||
export { AccountID };
|
||||
export { AccountID }
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { Decimal } from "decimal.js";
|
||||
import { Decimal } from 'decimal.js'
|
||||
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
|
||||
import { AccountID } from "./account-id";
|
||||
import { Currency } from "./currency";
|
||||
import { JsonObject, SerializedType } from "./serialized-type";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { AccountID } from './account-id'
|
||||
import { Currency } from './currency'
|
||||
import { JsonObject, SerializedType } from './serialized-type'
|
||||
import * as bigInt from 'big-integer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Constants for validating amounts
|
||||
*/
|
||||
const MIN_IOU_EXPONENT = -96;
|
||||
const MAX_IOU_EXPONENT = 80;
|
||||
const MAX_IOU_PRECISION = 16;
|
||||
const MAX_DROPS = new Decimal("1e17");
|
||||
const MIN_XRP = new Decimal("1e-6");
|
||||
const mask = bigInt(0x00000000ffffffff);
|
||||
const MIN_IOU_EXPONENT = -96
|
||||
const MAX_IOU_EXPONENT = 80
|
||||
const MAX_IOU_PRECISION = 16
|
||||
const MAX_DROPS = new Decimal('1e17')
|
||||
const MIN_XRP = new Decimal('1e-6')
|
||||
const mask = bigInt(0x00000000ffffffff)
|
||||
|
||||
/**
|
||||
* decimal.js configuration for Amount IOUs
|
||||
@@ -24,28 +24,28 @@ const mask = bigInt(0x00000000ffffffff);
|
||||
Decimal.config({
|
||||
toExpPos: MAX_IOU_EXPONENT + MAX_IOU_PRECISION,
|
||||
toExpNeg: MIN_IOU_EXPONENT - MAX_IOU_PRECISION,
|
||||
});
|
||||
})
|
||||
|
||||
/**
|
||||
* Interface for JSON objects that represent amounts
|
||||
*/
|
||||
interface AmountObject extends JsonObject {
|
||||
value: string;
|
||||
currency: string;
|
||||
issuer: string;
|
||||
value: string
|
||||
currency: string
|
||||
issuer: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard for AmountObject
|
||||
*/
|
||||
function isAmountObject(arg): arg is AmountObject {
|
||||
const keys = Object.keys(arg).sort();
|
||||
const keys = Object.keys(arg).sort()
|
||||
return (
|
||||
keys.length === 3 &&
|
||||
keys[0] === "currency" &&
|
||||
keys[1] === "issuer" &&
|
||||
keys[2] === "value"
|
||||
);
|
||||
keys[0] === 'currency' &&
|
||||
keys[1] === 'issuer' &&
|
||||
keys[2] === 'value'
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,11 +53,11 @@ function isAmountObject(arg): arg is AmountObject {
|
||||
*/
|
||||
class Amount extends SerializedType {
|
||||
static defaultAmount: Amount = new Amount(
|
||||
Buffer.from("4000000000000000", "hex")
|
||||
);
|
||||
Buffer.from('4000000000000000', 'hex'),
|
||||
)
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? Amount.defaultAmount.bytes);
|
||||
super(bytes ?? Amount.defaultAmount.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,63 +69,63 @@ class Amount extends SerializedType {
|
||||
*/
|
||||
static from<T extends Amount | AmountObject | string>(value: T): Amount {
|
||||
if (value instanceof Amount) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
let amount = Buffer.alloc(8);
|
||||
if (typeof value === "string") {
|
||||
Amount.assertXrpIsValid(value);
|
||||
let amount = Buffer.alloc(8)
|
||||
if (typeof value === 'string') {
|
||||
Amount.assertXrpIsValid(value)
|
||||
|
||||
const number = bigInt(value);
|
||||
const number = bigInt(value)
|
||||
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0);
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0)
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0)
|
||||
|
||||
amount = Buffer.concat(intBuf);
|
||||
amount = Buffer.concat(intBuf)
|
||||
|
||||
amount[0] |= 0x40;
|
||||
amount[0] |= 0x40
|
||||
|
||||
return new Amount(amount);
|
||||
return new Amount(amount)
|
||||
}
|
||||
|
||||
if (isAmountObject(value)) {
|
||||
const number = new Decimal(value.value);
|
||||
Amount.assertIouIsValid(number);
|
||||
const number = new Decimal(value.value)
|
||||
Amount.assertIouIsValid(number)
|
||||
|
||||
if (number.isZero()) {
|
||||
amount[0] |= 0x80;
|
||||
amount[0] |= 0x80
|
||||
} else {
|
||||
const integerNumberString = number
|
||||
.times(`1e${-(number.e - 15)}`)
|
||||
.abs()
|
||||
.toString();
|
||||
.toString()
|
||||
|
||||
const num = bigInt(integerNumberString);
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(num.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(num.and(mask)), 0);
|
||||
const num = bigInt(integerNumberString)
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
|
||||
intBuf[0].writeUInt32BE(Number(num.shiftRight(32)), 0)
|
||||
intBuf[1].writeUInt32BE(Number(num.and(mask)), 0)
|
||||
|
||||
amount = Buffer.concat(intBuf);
|
||||
amount = Buffer.concat(intBuf)
|
||||
|
||||
amount[0] |= 0x80;
|
||||
amount[0] |= 0x80
|
||||
|
||||
if (number.gt(new Decimal(0))) {
|
||||
amount[0] |= 0x40;
|
||||
amount[0] |= 0x40
|
||||
}
|
||||
|
||||
const exponent = number.e - 15;
|
||||
const exponentByte = 97 + exponent;
|
||||
amount[0] |= exponentByte >>> 2;
|
||||
amount[1] |= (exponentByte & 0x03) << 6;
|
||||
const exponent = number.e - 15
|
||||
const exponentByte = 97 + exponent
|
||||
amount[0] |= exponentByte >>> 2
|
||||
amount[1] |= (exponentByte & 0x03) << 6
|
||||
}
|
||||
|
||||
const currency = Currency.from(value.currency).toBytes();
|
||||
const issuer = AccountID.from(value.issuer).toBytes();
|
||||
return new Amount(Buffer.concat([amount, currency, issuer]));
|
||||
const currency = Currency.from(value.currency).toBytes()
|
||||
const issuer = AccountID.from(value.issuer).toBytes()
|
||||
return new Amount(Buffer.concat([amount, currency, issuer]))
|
||||
}
|
||||
|
||||
throw new Error("Invalid type to construct an Amount");
|
||||
throw new Error('Invalid type to construct an Amount')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,9 +135,9 @@ class Amount extends SerializedType {
|
||||
* @returns An Amount object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): Amount {
|
||||
const isXRP = parser.peek() & 0x80;
|
||||
const numBytes = isXRP ? 48 : 8;
|
||||
return new Amount(parser.read(numBytes));
|
||||
const isXRP = parser.peek() & 0x80
|
||||
const numBytes = isXRP ? 48 : 8
|
||||
return new Amount(parser.read(numBytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,41 +147,41 @@ class Amount extends SerializedType {
|
||||
*/
|
||||
toJSON(): AmountObject | string {
|
||||
if (this.isNative()) {
|
||||
const bytes = this.bytes;
|
||||
const isPositive = bytes[0] & 0x40;
|
||||
const sign = isPositive ? "" : "-";
|
||||
bytes[0] &= 0x3f;
|
||||
const bytes = this.bytes
|
||||
const isPositive = bytes[0] & 0x40
|
||||
const sign = isPositive ? '' : '-'
|
||||
bytes[0] &= 0x3f
|
||||
|
||||
const msb = bigInt(bytes.slice(0, 4).readUInt32BE(0));
|
||||
const lsb = bigInt(bytes.slice(4).readUInt32BE(0));
|
||||
const num = msb.shiftLeft(32).or(lsb);
|
||||
const msb = bigInt(bytes.slice(0, 4).readUInt32BE(0))
|
||||
const lsb = bigInt(bytes.slice(4).readUInt32BE(0))
|
||||
const num = msb.shiftLeft(32).or(lsb)
|
||||
|
||||
return `${sign}${num.toString()}`;
|
||||
return `${sign}${num.toString()}`
|
||||
} else {
|
||||
const parser = new BinaryParser(this.toString());
|
||||
const mantissa = parser.read(8);
|
||||
const currency = Currency.fromParser(parser) as Currency;
|
||||
const issuer = AccountID.fromParser(parser) as AccountID;
|
||||
const parser = new BinaryParser(this.toString())
|
||||
const mantissa = parser.read(8)
|
||||
const currency = Currency.fromParser(parser) as Currency
|
||||
const issuer = AccountID.fromParser(parser) as AccountID
|
||||
|
||||
const b1 = mantissa[0];
|
||||
const b2 = mantissa[1];
|
||||
const b1 = mantissa[0]
|
||||
const b2 = mantissa[1]
|
||||
|
||||
const isPositive = b1 & 0x40;
|
||||
const sign = isPositive ? "" : "-";
|
||||
const exponent = ((b1 & 0x3f) << 2) + ((b2 & 0xff) >> 6) - 97;
|
||||
const isPositive = b1 & 0x40
|
||||
const sign = isPositive ? '' : '-'
|
||||
const exponent = ((b1 & 0x3f) << 2) + ((b2 & 0xff) >> 6) - 97
|
||||
|
||||
mantissa[0] = 0;
|
||||
mantissa[1] &= 0x3f;
|
||||
const value = new Decimal(`${sign}0x${mantissa.toString("hex")}`).times(
|
||||
`1e${exponent}`
|
||||
);
|
||||
Amount.assertIouIsValid(value);
|
||||
mantissa[0] = 0
|
||||
mantissa[1] &= 0x3f
|
||||
const value = new Decimal(`${sign}0x${mantissa.toString('hex')}`).times(
|
||||
`1e${exponent}`,
|
||||
)
|
||||
Amount.assertIouIsValid(value)
|
||||
|
||||
return {
|
||||
value: value.toString(),
|
||||
currency: currency.toJSON(),
|
||||
issuer: issuer.toJSON(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,14 +192,14 @@ class Amount extends SerializedType {
|
||||
* @returns void, but will throw if invalid amount
|
||||
*/
|
||||
private static assertXrpIsValid(amount: string): void {
|
||||
if (amount.indexOf(".") !== -1) {
|
||||
throw new Error(`${amount.toString()} is an illegal amount`);
|
||||
if (amount.indexOf('.') !== -1) {
|
||||
throw new Error(`${amount.toString()} is an illegal amount`)
|
||||
}
|
||||
|
||||
const decimal = new Decimal(amount);
|
||||
const decimal = new Decimal(amount)
|
||||
if (!decimal.isZero()) {
|
||||
if (decimal.lt(MIN_XRP) || decimal.gt(MAX_DROPS)) {
|
||||
throw new Error(`${amount.toString()} is an illegal amount`);
|
||||
throw new Error(`${amount.toString()} is an illegal amount`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -212,16 +212,16 @@ class Amount extends SerializedType {
|
||||
*/
|
||||
private static assertIouIsValid(decimal: Decimal): void {
|
||||
if (!decimal.isZero()) {
|
||||
const p = decimal.precision();
|
||||
const e = decimal.e - 15;
|
||||
const p = decimal.precision()
|
||||
const e = decimal.e - 15
|
||||
if (
|
||||
p > MAX_IOU_PRECISION ||
|
||||
e > MAX_IOU_EXPONENT ||
|
||||
e < MIN_IOU_EXPONENT
|
||||
) {
|
||||
throw new Error("Decimal precision out of range");
|
||||
throw new Error('Decimal precision out of range')
|
||||
}
|
||||
this.verifyNoDecimal(decimal);
|
||||
this.verifyNoDecimal(decimal)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,10 +236,10 @@ class Amount extends SerializedType {
|
||||
const integerNumberString = decimal
|
||||
.times(`1e${-(decimal.e - 15)}`)
|
||||
.abs()
|
||||
.toString();
|
||||
.toString()
|
||||
|
||||
if (integerNumberString.indexOf(".") !== -1) {
|
||||
throw new Error("Decimal place found in integerNumberString");
|
||||
if (integerNumberString.indexOf('.') !== -1) {
|
||||
throw new Error('Decimal place found in integerNumberString')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,8 +249,8 @@ class Amount extends SerializedType {
|
||||
* @returns true if Native (XRP)
|
||||
*/
|
||||
private isNative(): boolean {
|
||||
return (this.bytes[0] & 0x80) === 0;
|
||||
return (this.bytes[0] & 0x80) === 0
|
||||
}
|
||||
}
|
||||
|
||||
export { Amount, AmountObject };
|
||||
export { Amount, AmountObject }
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { SerializedType } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { SerializedType } from './serialized-type'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Variable length encoded type
|
||||
*/
|
||||
class Blob extends SerializedType {
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes);
|
||||
super(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -18,7 +18,7 @@ class Blob extends SerializedType {
|
||||
* @returns A Blob object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser, hint: number): Blob {
|
||||
return new Blob(parser.read(hint));
|
||||
return new Blob(parser.read(hint))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,15 +29,15 @@ class Blob extends SerializedType {
|
||||
*/
|
||||
static from<T extends Blob | string>(value: T): Blob {
|
||||
if (value instanceof Blob) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return new Blob(Buffer.from(value, "hex"));
|
||||
if (typeof value === 'string') {
|
||||
return new Blob(Buffer.from(value, 'hex'))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct Blob from value given");
|
||||
throw new Error('Cannot construct Blob from value given')
|
||||
}
|
||||
}
|
||||
|
||||
export { Blob };
|
||||
export { Blob }
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
import { Hash160 } from "./hash-160";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Hash160 } from './hash-160'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const ISO_REGEX = /^[A-Z0-9]{3}$/;
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/;
|
||||
const ISO_REGEX = /^[A-Z0-9]{3}$/
|
||||
const HEX_REGEX = /^[A-F0-9]{40}$/
|
||||
|
||||
/**
|
||||
* Convert an ISO code to a currency bytes representation
|
||||
*/
|
||||
function isoToBytes(iso: string): Buffer {
|
||||
const bytes = Buffer.alloc(20);
|
||||
if (iso !== "XRP") {
|
||||
const isoBytes = iso.split("").map((c) => c.charCodeAt(0));
|
||||
bytes.set(isoBytes, 12);
|
||||
const bytes = Buffer.alloc(20)
|
||||
if (iso !== 'XRP') {
|
||||
const isoBytes = iso.split('').map((c) => c.charCodeAt(0))
|
||||
bytes.set(isoBytes, 12)
|
||||
}
|
||||
return bytes;
|
||||
return bytes
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if ISO is a valid iso code
|
||||
*/
|
||||
function isIsoCode(iso: string): boolean {
|
||||
return ISO_REGEX.test(iso);
|
||||
return ISO_REGEX.test(iso)
|
||||
}
|
||||
|
||||
function isoCodeFromHex(code: Buffer): string | null {
|
||||
const iso = code.toString();
|
||||
if (iso === "XRP") {
|
||||
const iso = code.toString()
|
||||
if (iso === 'XRP') {
|
||||
throw new Error(
|
||||
"Disallowed currency code: to indicate the currency XRP you must use 20 bytes of 0s"
|
||||
);
|
||||
'Disallowed currency code: to indicate the currency XRP you must use 20 bytes of 0s',
|
||||
)
|
||||
}
|
||||
if (isIsoCode(iso)) {
|
||||
return iso;
|
||||
return iso
|
||||
}
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if hex is a valid hex-string
|
||||
*/
|
||||
function isHex(hex: string): boolean {
|
||||
return HEX_REGEX.test(hex);
|
||||
return HEX_REGEX.test(hex)
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a string is a valid representation of a currency
|
||||
*/
|
||||
function isStringRepresentation(input: string): boolean {
|
||||
return input.length === 3 || isHex(input);
|
||||
return input.length === 3 || isHex(input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a Buffer is a valid representation of a currency
|
||||
*/
|
||||
function isBytesArray(bytes: Buffer): boolean {
|
||||
return bytes.byteLength === 20;
|
||||
return bytes.byteLength === 20
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ function isBytesArray(bytes: Buffer): boolean {
|
||||
function isValidRepresentation(input: Buffer | string): boolean {
|
||||
return input instanceof Buffer
|
||||
? isBytesArray(input)
|
||||
: isStringRepresentation(input);
|
||||
: isStringRepresentation(input)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,28 +71,28 @@ function isValidRepresentation(input: Buffer | string): boolean {
|
||||
*/
|
||||
function bytesFromRepresentation(input: string): Buffer {
|
||||
if (!isValidRepresentation(input)) {
|
||||
throw new Error(`Unsupported Currency representation: ${input}`);
|
||||
throw new Error(`Unsupported Currency representation: ${input}`)
|
||||
}
|
||||
return input.length === 3 ? isoToBytes(input) : Buffer.from(input, "hex");
|
||||
return input.length === 3 ? isoToBytes(input) : Buffer.from(input, 'hex')
|
||||
}
|
||||
|
||||
/**
|
||||
* Class defining how to encode and decode Currencies
|
||||
*/
|
||||
class Currency extends Hash160 {
|
||||
static readonly XRP = new Currency(Buffer.alloc(20));
|
||||
private readonly _iso: string | null;
|
||||
static readonly XRP = new Currency(Buffer.alloc(20))
|
||||
private readonly _iso: string | null
|
||||
|
||||
constructor(byteBuf: Buffer) {
|
||||
super(byteBuf ?? Currency.XRP.bytes);
|
||||
const code = this.bytes.slice(12, 15);
|
||||
super(byteBuf ?? Currency.XRP.bytes)
|
||||
const code = this.bytes.slice(12, 15)
|
||||
|
||||
if (this.bytes[0] !== 0) {
|
||||
this._iso = null;
|
||||
} else if (code.toString("hex") === "000000") {
|
||||
this._iso = "XRP";
|
||||
this._iso = null
|
||||
} else if (code.toString('hex') === '000000') {
|
||||
this._iso = 'XRP'
|
||||
} else {
|
||||
this._iso = isoCodeFromHex(code);
|
||||
this._iso = isoCodeFromHex(code)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class Currency extends Hash160 {
|
||||
* @returns ISO code if it exists, else null
|
||||
*/
|
||||
iso(): string | null {
|
||||
return this._iso;
|
||||
return this._iso
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,14 +112,14 @@ class Currency extends Hash160 {
|
||||
*/
|
||||
static from<T extends Hash160 | string>(value: T): Currency {
|
||||
if (value instanceof Currency) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return new Currency(bytesFromRepresentation(value));
|
||||
if (typeof value === 'string') {
|
||||
return new Currency(bytesFromRepresentation(value))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct Currency from value given");
|
||||
throw new Error('Cannot construct Currency from value given')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,12 +128,12 @@ class Currency extends Hash160 {
|
||||
* @returns JSON representation
|
||||
*/
|
||||
toJSON(): string {
|
||||
const iso = this.iso();
|
||||
const iso = this.iso()
|
||||
if (iso !== null) {
|
||||
return iso;
|
||||
return iso
|
||||
}
|
||||
return this.bytes.toString("hex").toUpperCase();
|
||||
return this.bytes.toString('hex').toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
export { Currency };
|
||||
export { Currency }
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Hash } from './hash'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Hash with a width of 128 bits
|
||||
*/
|
||||
class Hash128 extends Hash {
|
||||
static readonly width = 16;
|
||||
static readonly ZERO_128: Hash128 = new Hash128(Buffer.alloc(Hash128.width));
|
||||
static readonly width = 16
|
||||
static readonly ZERO_128: Hash128 = new Hash128(Buffer.alloc(Hash128.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? Hash128.ZERO_128.bytes);
|
||||
super(bytes ?? Hash128.ZERO_128.bytes)
|
||||
}
|
||||
}
|
||||
|
||||
export { Hash128 };
|
||||
export { Hash128 }
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Hash } from './hash'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Hash with a width of 160 bits
|
||||
*/
|
||||
class Hash160 extends Hash {
|
||||
static readonly width = 20;
|
||||
static readonly ZERO_160: Hash160 = new Hash160(Buffer.alloc(Hash160.width));
|
||||
static readonly width = 20
|
||||
static readonly ZERO_160: Hash160 = new Hash160(Buffer.alloc(Hash160.width))
|
||||
|
||||
constructor(bytes?: Buffer) {
|
||||
if (bytes && bytes.byteLength === 0) {
|
||||
bytes = Hash160.ZERO_160.bytes;
|
||||
bytes = Hash160.ZERO_160.bytes
|
||||
}
|
||||
|
||||
super(bytes ?? Hash160.ZERO_160.bytes);
|
||||
super(bytes ?? Hash160.ZERO_160.bytes)
|
||||
}
|
||||
}
|
||||
|
||||
export { Hash160 };
|
||||
export { Hash160 }
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Hash } from "./hash";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Hash } from './hash'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Hash with a width of 256 bits
|
||||
*/
|
||||
class Hash256 extends Hash {
|
||||
static readonly width = 32;
|
||||
static readonly ZERO_256 = new Hash256(Buffer.alloc(Hash256.width));
|
||||
static readonly width = 32
|
||||
static readonly ZERO_256 = new Hash256(Buffer.alloc(Hash256.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? Hash256.ZERO_256.bytes);
|
||||
super(bytes ?? Hash256.ZERO_256.bytes)
|
||||
}
|
||||
}
|
||||
|
||||
export { Hash256 };
|
||||
export { Hash256 }
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { Comparable } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Comparable } from './serialized-type'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Base class defining how to encode and decode hashes
|
||||
*/
|
||||
class Hash extends Comparable {
|
||||
static readonly width: number;
|
||||
static readonly width: number
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes);
|
||||
super(bytes)
|
||||
if (this.bytes.byteLength !== (this.constructor as typeof Hash).width) {
|
||||
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`);
|
||||
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,14 +22,14 @@ class Hash extends Comparable {
|
||||
*/
|
||||
static from<T extends Hash | string>(value: T): Hash {
|
||||
if (value instanceof this) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return new this(Buffer.from(value, "hex"));
|
||||
if (typeof value === 'string') {
|
||||
return new this(Buffer.from(value, 'hex'))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct Hash from given value");
|
||||
throw new Error('Cannot construct Hash from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +39,7 @@ class Hash extends Comparable {
|
||||
* @param hint length of the bytes to read, optional
|
||||
*/
|
||||
static fromParser(parser: BinaryParser, hint?: number): Hash {
|
||||
return new this(parser.read(hint ?? this.width));
|
||||
return new this(parser.read(hint ?? this.width))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,15 +49,15 @@ class Hash extends Comparable {
|
||||
*/
|
||||
compareTo(other: Hash): number {
|
||||
return this.bytes.compare(
|
||||
(this.constructor as typeof Hash).from(other).bytes
|
||||
);
|
||||
(this.constructor as typeof Hash).from(other).bytes,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns the hex-string representation of this Hash
|
||||
*/
|
||||
toString(): string {
|
||||
return this.toHex();
|
||||
return this.toHex()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,15 +67,15 @@ class Hash extends Comparable {
|
||||
* @returns The number represented by the four bits
|
||||
*/
|
||||
nibblet(depth: number): number {
|
||||
const byteIx = depth > 0 ? (depth / 2) | 0 : 0;
|
||||
let b = this.bytes[byteIx];
|
||||
const byteIx = depth > 0 ? (depth / 2) | 0 : 0
|
||||
let b = this.bytes[byteIx]
|
||||
if (depth % 2 === 0) {
|
||||
b = (b & 0xf0) >>> 4;
|
||||
b = (b & 0xf0) >>> 4
|
||||
} else {
|
||||
b = b & 0x0f;
|
||||
b = b & 0x0f
|
||||
}
|
||||
return b;
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
export { Hash };
|
||||
export { Hash }
|
||||
|
||||
@@ -3,22 +3,22 @@ import {
|
||||
TransactionResult,
|
||||
TransactionType,
|
||||
LedgerEntryType,
|
||||
} from "../enums";
|
||||
import { AccountID } from "./account-id";
|
||||
import { Amount } from "./amount";
|
||||
import { Blob } from "./blob";
|
||||
import { Currency } from "./currency";
|
||||
import { Hash128 } from "./hash-128";
|
||||
import { Hash160 } from "./hash-160";
|
||||
import { Hash256 } from "./hash-256";
|
||||
import { PathSet } from "./path-set";
|
||||
import { STArray } from "./st-array";
|
||||
import { STObject } from "./st-object";
|
||||
import { UInt16 } from "./uint-16";
|
||||
import { UInt32 } from "./uint-32";
|
||||
import { UInt64 } from "./uint-64";
|
||||
import { UInt8 } from "./uint-8";
|
||||
import { Vector256 } from "./vector-256";
|
||||
} from '../enums'
|
||||
import { AccountID } from './account-id'
|
||||
import { Amount } from './amount'
|
||||
import { Blob } from './blob'
|
||||
import { Currency } from './currency'
|
||||
import { Hash128 } from './hash-128'
|
||||
import { Hash160 } from './hash-160'
|
||||
import { Hash256 } from './hash-256'
|
||||
import { PathSet } from './path-set'
|
||||
import { STArray } from './st-array'
|
||||
import { STObject } from './st-object'
|
||||
import { UInt16 } from './uint-16'
|
||||
import { UInt32 } from './uint-32'
|
||||
import { UInt64 } from './uint-64'
|
||||
import { UInt8 } from './uint-8'
|
||||
import { Vector256 } from './vector-256'
|
||||
|
||||
const coreTypes = {
|
||||
AccountID,
|
||||
@@ -36,14 +36,14 @@ const coreTypes = {
|
||||
UInt32,
|
||||
UInt64,
|
||||
Vector256,
|
||||
};
|
||||
}
|
||||
|
||||
Object.values(Field).forEach((field) => {
|
||||
field.associatedType = coreTypes[field.type.name];
|
||||
});
|
||||
field.associatedType = coreTypes[field.type.name]
|
||||
})
|
||||
|
||||
Field["TransactionType"].associatedType = TransactionType;
|
||||
Field["TransactionResult"].associatedType = TransactionResult;
|
||||
Field["LedgerEntryType"].associatedType = LedgerEntryType;
|
||||
Field['TransactionType'].associatedType = TransactionType
|
||||
Field['TransactionResult'].associatedType = TransactionResult
|
||||
Field['LedgerEntryType'].associatedType = LedgerEntryType
|
||||
|
||||
export { coreTypes };
|
||||
export { coreTypes }
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { AccountID } from "./account-id";
|
||||
import { Currency } from "./currency";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { SerializedType, JsonObject } from "./serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import { AccountID } from './account-id'
|
||||
import { Currency } from './currency'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { SerializedType, JsonObject } from './serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Constants for separating Paths in a PathSet
|
||||
*/
|
||||
const PATHSET_END_BYTE = 0x00;
|
||||
const PATH_SEPARATOR_BYTE = 0xff;
|
||||
const PATHSET_END_BYTE = 0x00
|
||||
const PATH_SEPARATOR_BYTE = 0xff
|
||||
|
||||
/**
|
||||
* Constant for masking types of a Hop
|
||||
*/
|
||||
const TYPE_ACCOUNT = 0x01;
|
||||
const TYPE_CURRENCY = 0x10;
|
||||
const TYPE_ISSUER = 0x20;
|
||||
const TYPE_ACCOUNT = 0x01
|
||||
const TYPE_CURRENCY = 0x10
|
||||
const TYPE_ISSUER = 0x20
|
||||
|
||||
/**
|
||||
* The object representation of a Hop, an issuer AccountID, an account AccountID, and a Currency
|
||||
*/
|
||||
interface HopObject extends JsonObject {
|
||||
issuer?: string;
|
||||
account?: string;
|
||||
currency?: string;
|
||||
issuer?: string
|
||||
account?: string
|
||||
currency?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,7 @@ function isHopObject(arg): arg is HopObject {
|
||||
arg.issuer !== undefined ||
|
||||
arg.account !== undefined ||
|
||||
arg.currency !== undefined
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +45,7 @@ function isPathSet(arg): arg is Array<Array<HopObject>> {
|
||||
(Array.isArray(arg) && arg.length === 0) ||
|
||||
(Array.isArray(arg) && Array.isArray(arg[0]) && arg[0].length === 0) ||
|
||||
(Array.isArray(arg) && Array.isArray(arg[0]) && isHopObject(arg[0][0]))
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,27 +60,27 @@ class Hop extends SerializedType {
|
||||
*/
|
||||
static from(value: Hop | HopObject): Hop {
|
||||
if (value instanceof Hop) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
const bytes: Array<Buffer> = [Buffer.from([0])];
|
||||
const bytes: Array<Buffer> = [Buffer.from([0])]
|
||||
|
||||
if (value.account) {
|
||||
bytes.push(AccountID.from(value.account).toBytes());
|
||||
bytes[0][0] |= TYPE_ACCOUNT;
|
||||
bytes.push(AccountID.from(value.account).toBytes())
|
||||
bytes[0][0] |= TYPE_ACCOUNT
|
||||
}
|
||||
|
||||
if (value.currency) {
|
||||
bytes.push(Currency.from(value.currency).toBytes());
|
||||
bytes[0][0] |= TYPE_CURRENCY;
|
||||
bytes.push(Currency.from(value.currency).toBytes())
|
||||
bytes[0][0] |= TYPE_CURRENCY
|
||||
}
|
||||
|
||||
if (value.issuer) {
|
||||
bytes.push(AccountID.from(value.issuer).toBytes());
|
||||
bytes[0][0] |= TYPE_ISSUER;
|
||||
bytes.push(AccountID.from(value.issuer).toBytes())
|
||||
bytes[0][0] |= TYPE_ISSUER
|
||||
}
|
||||
|
||||
return new Hop(Buffer.concat(bytes));
|
||||
return new Hop(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,22 +90,22 @@ class Hop extends SerializedType {
|
||||
* @returns a Hop
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): Hop {
|
||||
const type = parser.readUInt8();
|
||||
const bytes: Array<Buffer> = [Buffer.from([type])];
|
||||
const type = parser.readUInt8()
|
||||
const bytes: Array<Buffer> = [Buffer.from([type])]
|
||||
|
||||
if (type & TYPE_ACCOUNT) {
|
||||
bytes.push(parser.read(AccountID.width));
|
||||
bytes.push(parser.read(AccountID.width))
|
||||
}
|
||||
|
||||
if (type & TYPE_CURRENCY) {
|
||||
bytes.push(parser.read(Currency.width));
|
||||
bytes.push(parser.read(Currency.width))
|
||||
}
|
||||
|
||||
if (type & TYPE_ISSUER) {
|
||||
bytes.push(parser.read(AccountID.width));
|
||||
bytes.push(parser.read(AccountID.width))
|
||||
}
|
||||
|
||||
return new Hop(Buffer.concat(bytes));
|
||||
return new Hop(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,36 +114,36 @@ class Hop extends SerializedType {
|
||||
* @returns a HopObject, an JS object with optional account, issuer, and currency
|
||||
*/
|
||||
toJSON(): HopObject {
|
||||
const hopParser = new BinaryParser(this.bytes.toString("hex"));
|
||||
const type = hopParser.readUInt8();
|
||||
const hopParser = new BinaryParser(this.bytes.toString('hex'))
|
||||
const type = hopParser.readUInt8()
|
||||
|
||||
let account, currency, issuer;
|
||||
let account, currency, issuer
|
||||
if (type & TYPE_ACCOUNT) {
|
||||
account = (AccountID.fromParser(hopParser) as AccountID).toJSON();
|
||||
account = (AccountID.fromParser(hopParser) as AccountID).toJSON()
|
||||
}
|
||||
|
||||
if (type & TYPE_CURRENCY) {
|
||||
currency = (Currency.fromParser(hopParser) as Currency).toJSON();
|
||||
currency = (Currency.fromParser(hopParser) as Currency).toJSON()
|
||||
}
|
||||
|
||||
if (type & TYPE_ISSUER) {
|
||||
issuer = (AccountID.fromParser(hopParser) as AccountID).toJSON();
|
||||
issuer = (AccountID.fromParser(hopParser) as AccountID).toJSON()
|
||||
}
|
||||
|
||||
const result: HopObject = {};
|
||||
const result: HopObject = {}
|
||||
if (account) {
|
||||
result.account = account;
|
||||
result.account = account
|
||||
}
|
||||
|
||||
if (issuer) {
|
||||
result.issuer = issuer;
|
||||
result.issuer = issuer
|
||||
}
|
||||
|
||||
if (currency) {
|
||||
result.currency = currency;
|
||||
result.currency = currency
|
||||
}
|
||||
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,7 +152,7 @@ class Hop extends SerializedType {
|
||||
* @returns a number to be bitwise and-ed with TYPE_ constants to describe the types in the hop
|
||||
*/
|
||||
type(): number {
|
||||
return this.bytes[0];
|
||||
return this.bytes[0]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,15 +168,15 @@ class Path extends SerializedType {
|
||||
*/
|
||||
static from(value: Path | Array<HopObject>): Path {
|
||||
if (value instanceof Path) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
value.forEach((hop: HopObject) => {
|
||||
bytes.push(Hop.from(hop).toBytes());
|
||||
});
|
||||
bytes.push(Hop.from(hop).toBytes())
|
||||
})
|
||||
|
||||
return new Path(Buffer.concat(bytes));
|
||||
return new Path(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,18 +186,18 @@ class Path extends SerializedType {
|
||||
* @returns the Path represented by the bytes read from the BinaryParser
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): Path {
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
while (!parser.end()) {
|
||||
bytes.push(Hop.fromParser(parser).toBytes());
|
||||
bytes.push(Hop.fromParser(parser).toBytes())
|
||||
|
||||
if (
|
||||
parser.peek() === PATHSET_END_BYTE ||
|
||||
parser.peek() === PATH_SEPARATOR_BYTE
|
||||
) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
return new Path(Buffer.concat(bytes));
|
||||
return new Path(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,14 +206,14 @@ class Path extends SerializedType {
|
||||
* @returns an Array of HopObject constructed from this.bytes
|
||||
*/
|
||||
toJSON(): Array<HopObject> {
|
||||
const json: Array<HopObject> = [];
|
||||
const pathParser = new BinaryParser(this.toString());
|
||||
const json: Array<HopObject> = []
|
||||
const pathParser = new BinaryParser(this.toString())
|
||||
|
||||
while (!pathParser.end()) {
|
||||
json.push(Hop.fromParser(pathParser).toJSON());
|
||||
json.push(Hop.fromParser(pathParser).toJSON())
|
||||
}
|
||||
|
||||
return json;
|
||||
return json
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,23 +229,23 @@ class PathSet extends SerializedType {
|
||||
*/
|
||||
static from<T extends PathSet | Array<Array<HopObject>>>(value: T): PathSet {
|
||||
if (value instanceof PathSet) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (isPathSet(value)) {
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
|
||||
value.forEach((path: Array<HopObject>) => {
|
||||
bytes.push(Path.from(path).toBytes());
|
||||
bytes.push(Buffer.from([PATH_SEPARATOR_BYTE]));
|
||||
});
|
||||
bytes.push(Path.from(path).toBytes())
|
||||
bytes.push(Buffer.from([PATH_SEPARATOR_BYTE]))
|
||||
})
|
||||
|
||||
bytes[bytes.length - 1] = Buffer.from([PATHSET_END_BYTE]);
|
||||
bytes[bytes.length - 1] = Buffer.from([PATHSET_END_BYTE])
|
||||
|
||||
return new PathSet(Buffer.concat(bytes));
|
||||
return new PathSet(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct PathSet from given value");
|
||||
throw new Error('Cannot construct PathSet from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,18 +255,18 @@ class PathSet extends SerializedType {
|
||||
* @returns the PathSet read from parser
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): PathSet {
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
|
||||
while (!parser.end()) {
|
||||
bytes.push(Path.fromParser(parser).toBytes());
|
||||
bytes.push(parser.read(1));
|
||||
bytes.push(Path.fromParser(parser).toBytes())
|
||||
bytes.push(parser.read(1))
|
||||
|
||||
if (bytes[bytes.length - 1][0] == PATHSET_END_BYTE) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return new PathSet(Buffer.concat(bytes));
|
||||
return new PathSet(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,16 +275,16 @@ class PathSet extends SerializedType {
|
||||
* @returns an Array of Array of HopObjects, representing this PathSet
|
||||
*/
|
||||
toJSON(): Array<Array<HopObject>> {
|
||||
const json: Array<Array<HopObject>> = [];
|
||||
const pathParser = new BinaryParser(this.toString());
|
||||
const json: Array<Array<HopObject>> = []
|
||||
const pathParser = new BinaryParser(this.toString())
|
||||
|
||||
while (!pathParser.end()) {
|
||||
json.push(Path.fromParser(pathParser).toJSON());
|
||||
pathParser.skip(1);
|
||||
json.push(Path.fromParser(pathParser).toJSON())
|
||||
pathParser.skip(1)
|
||||
}
|
||||
|
||||
return json;
|
||||
return json
|
||||
}
|
||||
}
|
||||
|
||||
export { PathSet };
|
||||
export { PathSet }
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
import { BytesList } from "../serdes/binary-serializer";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import * as bigInt from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { BytesList } from '../serdes/binary-serializer'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import * as bigInt from 'big-integer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject;
|
||||
type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
|
||||
|
||||
type JsonObject = { [key: string]: JSON };
|
||||
type JsonObject = { [key: string]: JSON }
|
||||
|
||||
/**
|
||||
* The base class for all binary-codec types
|
||||
*/
|
||||
class SerializedType {
|
||||
protected readonly bytes: Buffer = Buffer.alloc(0);
|
||||
protected readonly bytes: Buffer = Buffer.alloc(0)
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
this.bytes = bytes ?? Buffer.alloc(0);
|
||||
this.bytes = bytes ?? Buffer.alloc(0)
|
||||
}
|
||||
|
||||
static fromParser(parser: BinaryParser, hint?: number): SerializedType {
|
||||
throw new Error("fromParser not implemented");
|
||||
return this.fromParser(parser, hint);
|
||||
throw new Error('fromParser not implemented')
|
||||
return this.fromParser(parser, hint)
|
||||
}
|
||||
|
||||
static from(
|
||||
value: SerializedType | JSON | bigInt.BigInteger
|
||||
value: SerializedType | JSON | bigInt.BigInteger,
|
||||
): SerializedType {
|
||||
throw new Error("from not implemented");
|
||||
return this.from(value);
|
||||
throw new Error('from not implemented')
|
||||
return this.from(value)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ class SerializedType {
|
||||
* @param list The BytesList to write SerializedType bytes to
|
||||
*/
|
||||
toBytesSink(list: BytesList): void {
|
||||
list.put(this.bytes);
|
||||
list.put(this.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ class SerializedType {
|
||||
* @returns hex String of this.bytes
|
||||
*/
|
||||
toHex(): string {
|
||||
return this.toBytes().toString("hex").toUpperCase();
|
||||
return this.toBytes().toString('hex').toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,11 +54,11 @@ class SerializedType {
|
||||
*/
|
||||
toBytes(): Buffer {
|
||||
if (this.bytes) {
|
||||
return this.bytes;
|
||||
return this.bytes
|
||||
}
|
||||
const bytes = new BytesList();
|
||||
this.toBytesSink(bytes);
|
||||
return bytes.toBytes();
|
||||
const bytes = new BytesList()
|
||||
this.toBytesSink(bytes)
|
||||
return bytes.toBytes()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,14 +67,14 @@ class SerializedType {
|
||||
* @returns any type, if not overloaded returns hexString representation of bytes
|
||||
*/
|
||||
toJSON(): JSON {
|
||||
return this.toHex();
|
||||
return this.toHex()
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns hexString representation of this.bytes
|
||||
*/
|
||||
toString(): string {
|
||||
return this.toHex();
|
||||
return this.toHex()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,23 +83,23 @@ class SerializedType {
|
||||
*/
|
||||
class Comparable extends SerializedType {
|
||||
lt(other: Comparable): boolean {
|
||||
return this.compareTo(other) < 0;
|
||||
return this.compareTo(other) < 0
|
||||
}
|
||||
|
||||
eq(other: Comparable): boolean {
|
||||
return this.compareTo(other) === 0;
|
||||
return this.compareTo(other) === 0
|
||||
}
|
||||
|
||||
gt(other: Comparable): boolean {
|
||||
return this.compareTo(other) > 0;
|
||||
return this.compareTo(other) > 0
|
||||
}
|
||||
|
||||
gte(other: Comparable): boolean {
|
||||
return this.compareTo(other) > -1;
|
||||
return this.compareTo(other) > -1
|
||||
}
|
||||
|
||||
lte(other: Comparable): boolean {
|
||||
return this.compareTo(other) < 1;
|
||||
return this.compareTo(other) < 1
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,10 +109,8 @@ class Comparable extends SerializedType {
|
||||
* @returns A number denoting the relationship of this and other
|
||||
*/
|
||||
compareTo(other: Comparable): number {
|
||||
throw new Error(
|
||||
`cannot compare ${this.toString()} and ${other.toString()}`
|
||||
);
|
||||
throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
|
||||
}
|
||||
}
|
||||
|
||||
export { SerializedType, Comparable, JSON, JsonObject };
|
||||
export { SerializedType, Comparable, JSON, JsonObject }
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { SerializedType, JsonObject } from "./serialized-type";
|
||||
import { STObject } from "./st-object";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { SerializedType, JsonObject } from './serialized-type'
|
||||
import { STObject } from './st-object'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const ARRAY_END_MARKER = Buffer.from([0xf1]);
|
||||
const ARRAY_END_MARKER_NAME = "ArrayEndMarker";
|
||||
const ARRAY_END_MARKER = Buffer.from([0xf1])
|
||||
const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
|
||||
|
||||
const OBJECT_END_MARKER = Buffer.from([0xe1]);
|
||||
const OBJECT_END_MARKER = Buffer.from([0xe1])
|
||||
|
||||
/**
|
||||
* TypeGuard for Array<JsonObject>
|
||||
*/
|
||||
function isObjects(args): args is Array<JsonObject> {
|
||||
return (
|
||||
Array.isArray(args) && (args.length === 0 || typeof args[0] === "object")
|
||||
);
|
||||
Array.isArray(args) && (args.length === 0 || typeof args[0] === 'object')
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,23 +28,23 @@ class STArray extends SerializedType {
|
||||
* @returns An STArray Object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): STArray {
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
|
||||
while (!parser.end()) {
|
||||
const field = parser.readField();
|
||||
const field = parser.readField()
|
||||
if (field.name === ARRAY_END_MARKER_NAME) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
bytes.push(
|
||||
field.header,
|
||||
parser.readFieldValue(field).toBytes(),
|
||||
OBJECT_END_MARKER
|
||||
);
|
||||
OBJECT_END_MARKER,
|
||||
)
|
||||
}
|
||||
|
||||
bytes.push(ARRAY_END_MARKER);
|
||||
return new STArray(Buffer.concat(bytes));
|
||||
bytes.push(ARRAY_END_MARKER)
|
||||
return new STArray(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,20 +55,20 @@ class STArray extends SerializedType {
|
||||
*/
|
||||
static from<T extends STArray | Array<JsonObject>>(value: T): STArray {
|
||||
if (value instanceof STArray) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (isObjects(value)) {
|
||||
const bytes: Array<Buffer> = [];
|
||||
const bytes: Array<Buffer> = []
|
||||
value.forEach((obj) => {
|
||||
bytes.push(STObject.from(obj).toBytes());
|
||||
});
|
||||
bytes.push(STObject.from(obj).toBytes())
|
||||
})
|
||||
|
||||
bytes.push(ARRAY_END_MARKER);
|
||||
return new STArray(Buffer.concat(bytes));
|
||||
bytes.push(ARRAY_END_MARKER)
|
||||
return new STArray(Buffer.concat(bytes))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct STArray from value given");
|
||||
throw new Error('Cannot construct STArray from value given')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,23 +77,23 @@ class STArray extends SerializedType {
|
||||
* @returns An Array of JSON objects
|
||||
*/
|
||||
toJSON(): Array<JsonObject> {
|
||||
const result: Array<JsonObject> = [];
|
||||
const result: Array<JsonObject> = []
|
||||
|
||||
const arrayParser = new BinaryParser(this.toString());
|
||||
const arrayParser = new BinaryParser(this.toString())
|
||||
|
||||
while (!arrayParser.end()) {
|
||||
const field = arrayParser.readField();
|
||||
const field = arrayParser.readField()
|
||||
if (field.name === ARRAY_END_MARKER_NAME) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
const outer = {};
|
||||
outer[field.name] = STObject.fromParser(arrayParser).toJSON();
|
||||
result.push(outer);
|
||||
const outer = {}
|
||||
outer[field.name] = STObject.fromParser(arrayParser).toJSON()
|
||||
result.push(outer)
|
||||
}
|
||||
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export { STArray };
|
||||
export { STArray }
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
import { Field, FieldInstance } from "../enums";
|
||||
import { SerializedType, JsonObject } from "./serialized-type";
|
||||
import {
|
||||
xAddressToClassicAddress,
|
||||
isValidXAddress,
|
||||
} from "ripple-address-codec";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { BinarySerializer, BytesList } from "../serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { Field, FieldInstance } from '../enums'
|
||||
import { SerializedType, JsonObject } from './serialized-type'
|
||||
import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { BinarySerializer, BytesList } from '../serdes/binary-serializer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1]);
|
||||
const OBJECT_END_MARKER = "ObjectEndMarker";
|
||||
const ST_OBJECT = "STObject";
|
||||
const DESTINATION = "Destination";
|
||||
const ACCOUNT = "Account";
|
||||
const SOURCE_TAG = "SourceTag";
|
||||
const DEST_TAG = "DestinationTag";
|
||||
const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1])
|
||||
const OBJECT_END_MARKER = 'ObjectEndMarker'
|
||||
const ST_OBJECT = 'STObject'
|
||||
const DESTINATION = 'Destination'
|
||||
const ACCOUNT = 'Account'
|
||||
const SOURCE_TAG = 'SourceTag'
|
||||
const DEST_TAG = 'DestinationTag'
|
||||
|
||||
/**
|
||||
* Break down an X-Address into an account and a tag
|
||||
@@ -23,17 +20,17 @@ const DEST_TAG = "DestinationTag";
|
||||
* @param xAddress X-Address corresponding to the field
|
||||
*/
|
||||
function handleXAddress(field: string, xAddress: string): JsonObject {
|
||||
const decoded = xAddressToClassicAddress(xAddress);
|
||||
const decoded = xAddressToClassicAddress(xAddress)
|
||||
|
||||
let tagName;
|
||||
if (field === DESTINATION) tagName = DEST_TAG;
|
||||
else if (field === ACCOUNT) tagName = SOURCE_TAG;
|
||||
let tagName
|
||||
if (field === DESTINATION) tagName = DEST_TAG
|
||||
else if (field === ACCOUNT) tagName = SOURCE_TAG
|
||||
else if (decoded.tag !== false)
|
||||
throw new Error(`${field} cannot have an associated tag`);
|
||||
throw new Error(`${field} cannot have an associated tag`)
|
||||
|
||||
return decoded.tag !== false
|
||||
? { [field]: decoded.classicAddress, [tagName]: decoded.tag }
|
||||
: { [field]: decoded.classicAddress };
|
||||
: { [field]: decoded.classicAddress }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,9 +42,9 @@ function handleXAddress(field: string, xAddress: string): JsonObject {
|
||||
*/
|
||||
function checkForDuplicateTags(obj1: JsonObject, obj2: JsonObject): void {
|
||||
if (!(obj1[SOURCE_TAG] === undefined || obj2[SOURCE_TAG] === undefined))
|
||||
throw new Error("Cannot have Account X-Address and SourceTag");
|
||||
throw new Error('Cannot have Account X-Address and SourceTag')
|
||||
if (!(obj1[DEST_TAG] === undefined || obj2[DEST_TAG] === undefined))
|
||||
throw new Error("Cannot have Destination X-Address and DestinationTag");
|
||||
throw new Error('Cannot have Destination X-Address and DestinationTag')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,24 +58,24 @@ class STObject extends SerializedType {
|
||||
* @returns A STObject object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser): STObject {
|
||||
const list: BytesList = new BytesList();
|
||||
const bytes: BinarySerializer = new BinarySerializer(list);
|
||||
const list: BytesList = new BytesList()
|
||||
const bytes: BinarySerializer = new BinarySerializer(list)
|
||||
|
||||
while (!parser.end()) {
|
||||
const field = parser.readField();
|
||||
const field = parser.readField()
|
||||
if (field.name === OBJECT_END_MARKER) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
const associatedValue = parser.readFieldValue(field);
|
||||
const associatedValue = parser.readFieldValue(field)
|
||||
|
||||
bytes.writeFieldAndValue(field, associatedValue);
|
||||
bytes.writeFieldAndValue(field, associatedValue)
|
||||
if (field.type.name === ST_OBJECT) {
|
||||
bytes.put(OBJECT_END_MARKER_BYTE);
|
||||
bytes.put(OBJECT_END_MARKER_BYTE)
|
||||
}
|
||||
}
|
||||
|
||||
return new STObject(list.toBytes());
|
||||
return new STObject(list.toBytes())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,23 +87,23 @@ class STObject extends SerializedType {
|
||||
*/
|
||||
static from<T extends STObject | JsonObject>(
|
||||
value: T,
|
||||
filter?: (...any) => boolean
|
||||
filter?: (...any) => boolean,
|
||||
): STObject {
|
||||
if (value instanceof STObject) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
const list: BytesList = new BytesList();
|
||||
const bytes: BinarySerializer = new BinarySerializer(list);
|
||||
const list: BytesList = new BytesList()
|
||||
const bytes: BinarySerializer = new BinarySerializer(list)
|
||||
|
||||
const xAddressDecoded = Object.entries(value).reduce((acc, [key, val]) => {
|
||||
let handled: JsonObject | undefined = undefined;
|
||||
let handled: JsonObject | undefined = undefined
|
||||
if (val && isValidXAddress(val.toString())) {
|
||||
handled = handleXAddress(key, val.toString());
|
||||
checkForDuplicateTags(handled, value);
|
||||
handled = handleXAddress(key, val.toString())
|
||||
checkForDuplicateTags(handled, value)
|
||||
}
|
||||
return Object.assign(acc, handled ?? { [key]: val });
|
||||
}, {});
|
||||
return Object.assign(acc, handled ?? { [key]: val })
|
||||
}, {})
|
||||
|
||||
let sorted = Object.keys(xAddressDecoded)
|
||||
.map((f: string): FieldInstance => Field[f] as FieldInstance)
|
||||
@@ -114,28 +111,28 @@ class STObject extends SerializedType {
|
||||
(f: FieldInstance): boolean =>
|
||||
f !== undefined &&
|
||||
xAddressDecoded[f.name] !== undefined &&
|
||||
f.isSerialized
|
||||
f.isSerialized,
|
||||
)
|
||||
.sort((a, b) => {
|
||||
return a.ordinal - b.ordinal;
|
||||
});
|
||||
return a.ordinal - b.ordinal
|
||||
})
|
||||
|
||||
if (filter !== undefined) {
|
||||
sorted = sorted.filter(filter);
|
||||
sorted = sorted.filter(filter)
|
||||
}
|
||||
|
||||
sorted.forEach((field) => {
|
||||
const associatedValue = field.associatedType.from(
|
||||
xAddressDecoded[field.name]
|
||||
);
|
||||
xAddressDecoded[field.name],
|
||||
)
|
||||
|
||||
bytes.writeFieldAndValue(field, associatedValue);
|
||||
bytes.writeFieldAndValue(field, associatedValue)
|
||||
if (field.type.name === ST_OBJECT) {
|
||||
bytes.put(OBJECT_END_MARKER_BYTE);
|
||||
bytes.put(OBJECT_END_MARKER_BYTE)
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
return new STObject(list.toBytes());
|
||||
return new STObject(list.toBytes())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,19 +141,19 @@ class STObject extends SerializedType {
|
||||
* @returns a JSON object
|
||||
*/
|
||||
toJSON(): JsonObject {
|
||||
const objectParser = new BinaryParser(this.toString());
|
||||
const accumulator = {};
|
||||
const objectParser = new BinaryParser(this.toString())
|
||||
const accumulator = {}
|
||||
|
||||
while (!objectParser.end()) {
|
||||
const field = objectParser.readField();
|
||||
const field = objectParser.readField()
|
||||
if (field.name === OBJECT_END_MARKER) {
|
||||
break;
|
||||
break
|
||||
}
|
||||
accumulator[field.name] = objectParser.readFieldValue(field).toJSON();
|
||||
accumulator[field.name] = objectParser.readFieldValue(field).toJSON()
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
return accumulator
|
||||
}
|
||||
}
|
||||
|
||||
export { STObject };
|
||||
export { STObject }
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { UInt } from './uint'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 16 bit UInt
|
||||
*/
|
||||
class UInt16 extends UInt {
|
||||
protected static readonly width: number = 16 / 8; // 2
|
||||
static readonly defaultUInt16: UInt16 = new UInt16(
|
||||
Buffer.alloc(UInt16.width)
|
||||
);
|
||||
protected static readonly width: number = 16 / 8 // 2
|
||||
static readonly defaultUInt16: UInt16 = new UInt16(Buffer.alloc(UInt16.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? UInt16.defaultUInt16.bytes);
|
||||
super(bytes ?? UInt16.defaultUInt16.bytes)
|
||||
}
|
||||
|
||||
static fromParser(parser: BinaryParser): UInt {
|
||||
return new UInt16(parser.read(UInt16.width));
|
||||
return new UInt16(parser.read(UInt16.width))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,16 +24,16 @@ class UInt16 extends UInt {
|
||||
*/
|
||||
static from<T extends UInt16 | number>(val: T): UInt16 {
|
||||
if (val instanceof UInt16) {
|
||||
return val;
|
||||
return val
|
||||
}
|
||||
|
||||
if (typeof val === "number") {
|
||||
const buf = Buffer.alloc(UInt16.width);
|
||||
buf.writeUInt16BE(val, 0);
|
||||
return new UInt16(buf);
|
||||
if (typeof val === 'number') {
|
||||
const buf = Buffer.alloc(UInt16.width)
|
||||
buf.writeUInt16BE(val, 0)
|
||||
return new UInt16(buf)
|
||||
}
|
||||
|
||||
throw new Error("Can not construct UInt16 with given value");
|
||||
throw new Error('Can not construct UInt16 with given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,8 +42,8 @@ class UInt16 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt16BE(0);
|
||||
return this.bytes.readUInt16BE(0)
|
||||
}
|
||||
}
|
||||
|
||||
export { UInt16 };
|
||||
export { UInt16 }
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { UInt } from './uint'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 32 bit UInt
|
||||
*/
|
||||
class UInt32 extends UInt {
|
||||
protected static readonly width: number = 32 / 8; // 4
|
||||
static readonly defaultUInt32: UInt32 = new UInt32(
|
||||
Buffer.alloc(UInt32.width)
|
||||
);
|
||||
protected static readonly width: number = 32 / 8 // 4
|
||||
static readonly defaultUInt32: UInt32 = new UInt32(Buffer.alloc(UInt32.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? UInt32.defaultUInt32.bytes);
|
||||
super(bytes ?? UInt32.defaultUInt32.bytes)
|
||||
}
|
||||
|
||||
static fromParser(parser: BinaryParser): UInt {
|
||||
return new UInt32(parser.read(UInt32.width));
|
||||
return new UInt32(parser.read(UInt32.width))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,23 +24,23 @@ class UInt32 extends UInt {
|
||||
*/
|
||||
static from<T extends UInt32 | number | string>(val: T): UInt32 {
|
||||
if (val instanceof UInt32) {
|
||||
return val;
|
||||
return val
|
||||
}
|
||||
|
||||
const buf = Buffer.alloc(UInt32.width);
|
||||
const buf = Buffer.alloc(UInt32.width)
|
||||
|
||||
if (typeof val === "string") {
|
||||
const num = Number.parseInt(val);
|
||||
buf.writeUInt32BE(num, 0);
|
||||
return new UInt32(buf);
|
||||
if (typeof val === 'string') {
|
||||
const num = Number.parseInt(val)
|
||||
buf.writeUInt32BE(num, 0)
|
||||
return new UInt32(buf)
|
||||
}
|
||||
|
||||
if (typeof val === "number") {
|
||||
buf.writeUInt32BE(val, 0);
|
||||
return new UInt32(buf);
|
||||
if (typeof val === 'number') {
|
||||
buf.writeUInt32BE(val, 0)
|
||||
return new UInt32(buf)
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct UInt32 from given value");
|
||||
throw new Error('Cannot construct UInt32 from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,8 +49,8 @@ class UInt32 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt32BE(0);
|
||||
return this.bytes.readUInt32BE(0)
|
||||
}
|
||||
}
|
||||
|
||||
export { UInt32 };
|
||||
export { UInt32 }
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import * as bigInt from "big-integer";
|
||||
import { isInstance } from "big-integer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { UInt } from './uint'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import * as bigInt from 'big-integer'
|
||||
import { isInstance } from 'big-integer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/;
|
||||
const mask = bigInt(0x00000000ffffffff);
|
||||
const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
|
||||
const mask = bigInt(0x00000000ffffffff)
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 64 bit UInt
|
||||
*/
|
||||
class UInt64 extends UInt {
|
||||
protected static readonly width: number = 64 / 8; // 8
|
||||
static readonly defaultUInt64: UInt64 = new UInt64(
|
||||
Buffer.alloc(UInt64.width)
|
||||
);
|
||||
protected static readonly width: number = 64 / 8 // 8
|
||||
static readonly defaultUInt64: UInt64 = new UInt64(Buffer.alloc(UInt64.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? UInt64.defaultUInt64.bytes);
|
||||
super(bytes ?? UInt64.defaultUInt64.bytes)
|
||||
}
|
||||
|
||||
static fromParser(parser: BinaryParser): UInt {
|
||||
return new UInt64(parser.read(UInt64.width));
|
||||
return new UInt64(parser.read(UInt64.width))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,47 +29,47 @@ class UInt64 extends UInt {
|
||||
* @returns A UInt64 object
|
||||
*/
|
||||
static from<T extends UInt64 | string | bigInt.BigInteger | number>(
|
||||
val: T
|
||||
val: T,
|
||||
): UInt64 {
|
||||
if (val instanceof UInt64) {
|
||||
return val;
|
||||
return val
|
||||
}
|
||||
|
||||
let buf = Buffer.alloc(UInt64.width);
|
||||
let buf = Buffer.alloc(UInt64.width)
|
||||
|
||||
if (typeof val === "number") {
|
||||
if (typeof val === 'number') {
|
||||
if (val < 0) {
|
||||
throw new Error("value must be an unsigned integer");
|
||||
throw new Error('value must be an unsigned integer')
|
||||
}
|
||||
|
||||
const number = bigInt(val);
|
||||
const number = bigInt(val)
|
||||
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0);
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0);
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
|
||||
intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0)
|
||||
intBuf[1].writeUInt32BE(Number(number.and(mask)), 0)
|
||||
|
||||
return new UInt64(Buffer.concat(intBuf));
|
||||
return new UInt64(Buffer.concat(intBuf))
|
||||
}
|
||||
|
||||
if (typeof val === "string") {
|
||||
if (typeof val === 'string') {
|
||||
if (!HEX_REGEX.test(val)) {
|
||||
throw new Error(`${val} is not a valid hex-string`);
|
||||
throw new Error(`${val} is not a valid hex-string`)
|
||||
}
|
||||
|
||||
const strBuf = val.padStart(16, "0");
|
||||
buf = Buffer.from(strBuf, "hex");
|
||||
return new UInt64(buf);
|
||||
const strBuf = val.padStart(16, '0')
|
||||
buf = Buffer.from(strBuf, 'hex')
|
||||
return new UInt64(buf)
|
||||
}
|
||||
|
||||
if (isInstance(val)) {
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)];
|
||||
intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))), 0);
|
||||
intBuf[1].writeUInt32BE(Number(val.and(mask)), 0);
|
||||
const intBuf = [Buffer.alloc(4), Buffer.alloc(4)]
|
||||
intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))), 0)
|
||||
intBuf[1].writeUInt32BE(Number(val.and(mask)), 0)
|
||||
|
||||
return new UInt64(Buffer.concat(intBuf));
|
||||
return new UInt64(Buffer.concat(intBuf))
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct UInt64 from given value");
|
||||
throw new Error('Cannot construct UInt64 from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +78,7 @@ class UInt64 extends UInt {
|
||||
* @returns a hex-string
|
||||
*/
|
||||
toJSON(): string {
|
||||
return this.bytes.toString("hex").toUpperCase();
|
||||
return this.bytes.toString('hex').toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,9 +87,9 @@ class UInt64 extends UInt {
|
||||
* @returns the number represented buy this.bytes
|
||||
*/
|
||||
valueOf(): bigInt.BigInteger {
|
||||
const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE(0));
|
||||
const lsb = bigInt(this.bytes.slice(4).readUInt32BE(0));
|
||||
return msb.shiftLeft(bigInt(32)).or(lsb);
|
||||
const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE(0))
|
||||
const lsb = bigInt(this.bytes.slice(4).readUInt32BE(0))
|
||||
return msb.shiftLeft(bigInt(32)).or(lsb)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,8 +98,8 @@ class UInt64 extends UInt {
|
||||
* @returns 8 bytes representing the UInt64
|
||||
*/
|
||||
toBytes(): Buffer {
|
||||
return this.bytes;
|
||||
return this.bytes
|
||||
}
|
||||
}
|
||||
|
||||
export { UInt64 };
|
||||
export { UInt64 }
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { UInt } from "./uint";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Buffer } from "buffer/";
|
||||
import { UInt } from './uint'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Derived UInt class for serializing/deserializing 8 bit UInt
|
||||
*/
|
||||
class UInt8 extends UInt {
|
||||
protected static readonly width: number = 8 / 8; // 1
|
||||
static readonly defaultUInt8: UInt8 = new UInt8(Buffer.alloc(UInt8.width));
|
||||
protected static readonly width: number = 8 / 8 // 1
|
||||
static readonly defaultUInt8: UInt8 = new UInt8(Buffer.alloc(UInt8.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes ?? UInt8.defaultUInt8.bytes);
|
||||
super(bytes ?? UInt8.defaultUInt8.bytes)
|
||||
}
|
||||
|
||||
static fromParser(parser: BinaryParser): UInt {
|
||||
return new UInt8(parser.read(UInt8.width));
|
||||
return new UInt8(parser.read(UInt8.width))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,16 +24,16 @@ class UInt8 extends UInt {
|
||||
*/
|
||||
static from<T extends UInt8 | number>(val: T): UInt8 {
|
||||
if (val instanceof UInt8) {
|
||||
return val;
|
||||
return val
|
||||
}
|
||||
|
||||
if (typeof val === "number") {
|
||||
const buf = Buffer.alloc(UInt8.width);
|
||||
buf.writeUInt8(val, 0);
|
||||
return new UInt8(buf);
|
||||
if (typeof val === 'number') {
|
||||
const buf = Buffer.alloc(UInt8.width)
|
||||
buf.writeUInt8(val, 0)
|
||||
return new UInt8(buf)
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct UInt8 from given value");
|
||||
throw new Error('Cannot construct UInt8 from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,8 +42,8 @@ class UInt8 extends UInt {
|
||||
* @returns the number represented by this.bytes
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.bytes.readUInt8(0);
|
||||
return this.bytes.readUInt8(0)
|
||||
}
|
||||
}
|
||||
|
||||
export { UInt8 };
|
||||
export { UInt8 }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as bigInt from "big-integer";
|
||||
import { Comparable } from "./serialized-type";
|
||||
import { Buffer } from "buffer/";
|
||||
import * as bigInt from 'big-integer'
|
||||
import { Comparable } from './serialized-type'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* Compare numbers and bigInts n1 and n2
|
||||
@@ -11,19 +11,19 @@ import { Buffer } from "buffer/";
|
||||
*/
|
||||
function compare(
|
||||
n1: number | bigInt.BigInteger,
|
||||
n2: number | bigInt.BigInteger
|
||||
n2: number | bigInt.BigInteger,
|
||||
): number {
|
||||
return n1 < n2 ? -1 : n1 == n2 ? 0 : 1;
|
||||
return n1 < n2 ? -1 : n1 == n2 ? 0 : 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for serializing and deserializing unsigned integers.
|
||||
*/
|
||||
abstract class UInt extends Comparable {
|
||||
protected static width: number;
|
||||
protected static width: number
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes);
|
||||
super(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,7 +33,7 @@ abstract class UInt extends Comparable {
|
||||
* @returns -1, 0, or 1 depending on how the objects relate to each other
|
||||
*/
|
||||
compareTo(other: UInt): number {
|
||||
return compare(this.valueOf(), other.valueOf());
|
||||
return compare(this.valueOf(), other.valueOf())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,8 +42,8 @@ abstract class UInt extends Comparable {
|
||||
* @returns number or string represented by this.bytes
|
||||
*/
|
||||
toJSON(): number | string {
|
||||
const val = this.valueOf();
|
||||
return typeof val === "number" ? val : val.toString();
|
||||
const val = this.valueOf()
|
||||
return typeof val === 'number' ? val : val.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +51,7 @@ abstract class UInt extends Comparable {
|
||||
*
|
||||
* @returns the value
|
||||
*/
|
||||
abstract valueOf(): number | bigInt.BigInteger;
|
||||
abstract valueOf(): number | bigInt.BigInteger
|
||||
}
|
||||
|
||||
export { UInt };
|
||||
export { UInt }
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { SerializedType } from "./serialized-type";
|
||||
import { BinaryParser } from "../serdes/binary-parser";
|
||||
import { Hash256 } from "./hash-256";
|
||||
import { BytesList } from "../serdes/binary-serializer";
|
||||
import { Buffer } from "buffer/";
|
||||
import { SerializedType } from './serialized-type'
|
||||
import { BinaryParser } from '../serdes/binary-parser'
|
||||
import { Hash256 } from './hash-256'
|
||||
import { BytesList } from '../serdes/binary-serializer'
|
||||
import { Buffer } from 'buffer/'
|
||||
|
||||
/**
|
||||
* TypeGuard for Array<string>
|
||||
*/
|
||||
function isStrings(arg): arg is Array<string> {
|
||||
return Array.isArray(arg) && (arg.length === 0 || typeof arg[0] === "string");
|
||||
return Array.isArray(arg) && (arg.length === 0 || typeof arg[0] === 'string')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -16,7 +16,7 @@ function isStrings(arg): arg is Array<string> {
|
||||
*/
|
||||
class Vector256 extends SerializedType {
|
||||
constructor(bytes: Buffer) {
|
||||
super(bytes);
|
||||
super(bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,13 +27,13 @@ class Vector256 extends SerializedType {
|
||||
* @returns a Vector256 object
|
||||
*/
|
||||
static fromParser(parser: BinaryParser, hint?: number): Vector256 {
|
||||
const bytesList = new BytesList();
|
||||
const bytes = hint ?? parser.size();
|
||||
const hashes = bytes / 32;
|
||||
const bytesList = new BytesList()
|
||||
const bytes = hint ?? parser.size()
|
||||
const hashes = bytes / 32
|
||||
for (let i = 0; i < hashes; i++) {
|
||||
Hash256.fromParser(parser).toBytesSink(bytesList);
|
||||
Hash256.fromParser(parser).toBytesSink(bytesList)
|
||||
}
|
||||
return new Vector256(bytesList.toBytes());
|
||||
return new Vector256(bytesList.toBytes())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,18 +44,18 @@ class Vector256 extends SerializedType {
|
||||
*/
|
||||
static from<T extends Vector256 | Array<string>>(value: T): Vector256 {
|
||||
if (value instanceof Vector256) {
|
||||
return value;
|
||||
return value
|
||||
}
|
||||
|
||||
if (isStrings(value)) {
|
||||
const bytesList = new BytesList();
|
||||
const bytesList = new BytesList()
|
||||
value.forEach((hash) => {
|
||||
Hash256.from(hash).toBytesSink(bytesList);
|
||||
});
|
||||
return new Vector256(bytesList.toBytes());
|
||||
Hash256.from(hash).toBytesSink(bytesList)
|
||||
})
|
||||
return new Vector256(bytesList.toBytes())
|
||||
}
|
||||
|
||||
throw new Error("Cannot construct Vector256 from given value");
|
||||
throw new Error('Cannot construct Vector256 from given value')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,20 +65,20 @@ class Vector256 extends SerializedType {
|
||||
*/
|
||||
toJSON(): Array<string> {
|
||||
if (this.bytes.byteLength % 32 !== 0) {
|
||||
throw new Error("Invalid bytes for Vector256");
|
||||
throw new Error('Invalid bytes for Vector256')
|
||||
}
|
||||
|
||||
const result: Array<string> = [];
|
||||
const result: Array<string> = []
|
||||
for (let i = 0; i < this.bytes.byteLength; i += 32) {
|
||||
result.push(
|
||||
this.bytes
|
||||
.slice(i, i + 32)
|
||||
.toString("hex")
|
||||
.toUpperCase()
|
||||
);
|
||||
.toString('hex')
|
||||
.toUpperCase(),
|
||||
)
|
||||
}
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export { Vector256 };
|
||||
export { Vector256 }
|
||||
|
||||
Reference in New Issue
Block a user