get testcase passing

This commit is contained in:
Mayukha Vadari
2022-05-25 17:38:01 -04:00
parent 158cff2be2
commit f717990922
8 changed files with 310 additions and 10 deletions

View File

@@ -2049,12 +2049,12 @@
"NFTokenCreateOffer": 27,
"NFTokenCancelOffer": 28,
"NFTokenAcceptOffer": 29,
"XchainDoorCreate": 30,
"XchainSeqnumCreate": 31,
"XchainTransfer": 32,
"XchainClaim": 33,
"XchainAccountCreate": 34,
"XchainAccountClaim": 35,
"XChainDoorCreate": 30,
"XChainSeqnumCreate": 31,
"XChainTransfer": 32,
"XChainClaim": 33,
"XChainAccountCreate": 34,
"XChainAccountClaim": 35,
"EnableAmendment": 100,
"SetFee": 101,

View File

@@ -7,7 +7,7 @@ import { Buffer } from 'buffer/'
* BinaryParser is used to compute fields and values from a HexString
*/
class BinaryParser {
private bytes: Buffer
public bytes: Buffer
/**
* Initialize bytes to a hex string

View File

@@ -14,7 +14,7 @@ class Blob extends SerializedType {
* Defines how to read a Blob from a BinaryParser
*
* @param parser The binary parser to read the Blob from
* @param hint The length of the blob, computed by readVariableLengthLength() and passed in
* @param hint The length of the blob, computed by readVariableLength() and passed in
* @returns A Blob object
*/
static fromParser(parser: BinaryParser, hint: number): Blob {

View File

@@ -11,7 +11,9 @@ import { Currency } from './currency'
import { Hash128 } from './hash-128'
import { Hash160 } from './hash-160'
import { Hash256 } from './hash-256'
import { IssuedCurrency } from './issued-currency'
import { PathSet } from './path-set'
import { Sidechain } from './sidechain'
import { STArray } from './st-array'
import { STObject } from './st-object'
import { UInt16 } from './uint-16'
@@ -28,7 +30,9 @@ const coreTypes = {
Hash128,
Hash160,
Hash256,
IssuedCurrency,
PathSet,
Sidechain,
STArray,
STObject,
UInt8,

View File

@@ -0,0 +1,114 @@
import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { Currency } from './currency'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'
/**
* Interface for JSON objects that represent amounts
*/
interface IssuedCurrencyObject extends JsonObject {
currency: string
issuer: string
}
/**
* Type guard for AmountObject
*/
function isIssuedCurrencyObject(arg): arg is IssuedCurrencyObject {
const keys = Object.keys(arg).sort()
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
}
/**
* Class for serializing/Deserializing Amounts
*/
class IssuedCurrency extends SerializedType {
static readonly ZERO_ISSUED_CURRENCY: IssuedCurrency = new IssuedCurrency(
Buffer.alloc(20),
)
constructor(bytes: Buffer) {
super(bytes ?? IssuedCurrency.ZERO_ISSUED_CURRENCY.bytes)
}
/**
* Construct an amount from an IOU or string amount
*
* @param value An Amount, object representing an IOU, or a string
* representing an integer amount
* @returns An Amount object
*/
static from<T extends IssuedCurrency | IssuedCurrencyObject | string>(
value: T,
): IssuedCurrency {
if (value instanceof IssuedCurrency) {
return value
}
if (typeof value === 'string') {
IssuedCurrency.assertXrpIsValid(value)
const currency = Currency.from(value).toBytes()
return new IssuedCurrency(currency)
}
if (isIssuedCurrencyObject(value)) {
const currency = Currency.from(value.currency).toBytes()
const issuer = AccountID.from(value.issuer).toBytes()
return new IssuedCurrency(Buffer.concat([currency, issuer]))
}
throw new Error('Invalid type to construct an Amount')
}
/**
* Read an amount from a BinaryParser
*
* @param parser BinaryParser to read the Amount from
* @returns An Amount object
*/
static fromParser(parser: BinaryParser): IssuedCurrency {
const currency = parser.read(20)
if (new Currency(currency).toJSON() === 'XRP') {
return new IssuedCurrency(currency)
}
const currencyAndIssuer = [currency, parser.read(20)]
return new IssuedCurrency(Buffer.concat(currencyAndIssuer))
}
/**
* Get the JSON representation of this Amount
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): IssuedCurrencyObject | string {
const parser = new BinaryParser(this.toString())
const currency = Currency.fromParser(parser) as Currency
if (currency.toJSON() === 'XRP') {
return currency.toJSON()
}
const issuer = AccountID.fromParser(parser) as AccountID
return {
currency: currency.toJSON(),
issuer: issuer.toJSON(),
}
}
/**
* Validate XRP amount
*
* @param value String representing XRP amount
* @returns void, but will throw if invalid amount
*/
private static assertXrpIsValid(value: string): void {
if (value !== 'XRP') {
throw new Error(`${value} is an illegal amount`)
}
}
}
export { IssuedCurrency, IssuedCurrencyObject }

View File

@@ -0,0 +1,114 @@
import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'
import { IssuedCurrency, IssuedCurrencyObject } from './issued-currency'
/**
* Interface for JSON objects that represent amounts
*/
interface SidechainObject extends JsonObject {
dst_chain_door: string
dst_chain_issue: IssuedCurrencyObject | string
src_chain_door: string
src_chain_issue: IssuedCurrencyObject | string
}
/**
* Type guard for AmountObject
*/
function isSidechainObject(arg): arg is SidechainObject {
const keys = Object.keys(arg).sort()
return (
keys.length === 4 &&
keys[0] === 'dst_chain_door' &&
keys[1] === 'dst_chain_issue' &&
keys[2] === 'src_chain_door' &&
keys[3] === 'src_chain_issue'
)
}
/**
* Class for serializing/Deserializing Amounts
*/
class Sidechain extends SerializedType {
static readonly ZERO_SIDECHAIN: Sidechain = new Sidechain(Buffer.alloc(80))
constructor(bytes: Buffer) {
super(bytes ?? Sidechain.ZERO_SIDECHAIN.bytes)
}
/**
* Construct an amount from an IOU or string amount
*
* @param value An Amount, object representing an IOU, or a string
* representing an integer amount
* @returns An Amount object
*/
static from<T extends Sidechain | SidechainObject>(value: T): Sidechain {
if (value instanceof Sidechain) {
return value
}
if (isSidechainObject(value)) {
const dst_chain_door = AccountID.from(value.dst_chain_door).toBytes()
const dst_chain_issue = IssuedCurrency.from(
value.dst_chain_issue,
).toBytes()
const src_chain_door = AccountID.from(value.src_chain_door).toBytes()
const src_chain_issue = IssuedCurrency.from(
value.src_chain_issue,
).toBytes()
return new Sidechain(
Buffer.concat([
dst_chain_door,
dst_chain_issue,
src_chain_door,
src_chain_issue,
]),
)
}
throw new Error('Invalid type to construct a Sidechain')
}
/**
* Read an amount from a BinaryParser
*
* @param parser BinaryParser to read the Amount from
* @returns An Amount object
*/
static fromParser(parser: BinaryParser): Sidechain {
const bytes: Array<Buffer> = []
bytes.push(parser.read(AccountID.width))
bytes.push(IssuedCurrency.fromParser(parser).toBytes())
bytes.push(parser.read(AccountID.width))
bytes.push(IssuedCurrency.fromParser(parser).toBytes())
return new Sidechain(Buffer.concat(bytes))
}
/**
* Get the JSON representation of this Amount
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): SidechainObject {
const parser = new BinaryParser(this.toString())
const dst_chain_door = AccountID.fromParser(parser) as AccountID
const dst_chain_issue = IssuedCurrency.fromParser(parser)
const src_chain_door = AccountID.fromParser(parser) as AccountID
const src_chain_issue = IssuedCurrency.fromParser(parser)
return {
dst_chain_door: dst_chain_door.toJSON(),
dst_chain_issue: dst_chain_issue.toJSON(),
src_chain_door: src_chain_door.toJSON(),
src_chain_issue: src_chain_issue.toJSON(),
}
}
}
export { Sidechain, SidechainObject }