refactor: define TrustSet transaction model (#1549)

- Defines a TypeScript type for TrustSet
- Provides an optional function to users for verifying a TrustSet instance at runtime: verifyTrustSet()
- Adds tests for verifyTrustSet()
This commit is contained in:
Omar Khan
2021-08-19 11:47:01 -04:00
committed by Mayukha Vadari
parent f7b93c54ff
commit 9e16327676
4 changed files with 123 additions and 1 deletions

View File

@@ -7,5 +7,6 @@ export * from './checkCash'
export * from './checkCancel'
export * from './accountDelete'
export * from './signerListSet'
export * from './trustSet'
export * from './depositPreauth'
export * from './paymentTransaction'

View File

@@ -9,6 +9,7 @@ import { OfferCancel } from "./offerCancel"
import { OfferCreate } from "./offerCreate"
import { PaymentTransaction } from "./paymentTransaction"
import { SignerListSet } from "./signerListSet"
import { TrustSet } from "./trustSet"
export type Transaction =
AccountDelete
@@ -30,7 +31,7 @@ export type Transaction =
// | SetRegularKey
| SignerListSet
// | TicketCreate
// | TrustSet
| TrustSet
export interface TransactionAndMetadata {
transaction: Transaction

View File

@@ -0,0 +1,54 @@
import { ValidationError } from '../../common/errors'
import { Amount } from '../common'
import { BaseTransaction, GlobalFlags, isAmount, verifyBaseTransaction } from './common'
export enum TrustSetFlagsEnum {
tfSetfAuth = 0x00010000,
tfSetNoRipple = 0x00020000,
tfClearNoRipple = 0x00040000,
tfSetFreeze = 0x00100000,
tfClearFreeze = 0x00200000,
}
export interface TrustSetFlags extends GlobalFlags {
tfSetfAuth?: boolean
tfSetNoRipple?: boolean
tfClearNoRipple?: boolean
tfSetFreeze?: boolean
tfClearFreeze?: boolean
}
export interface TrustSet extends BaseTransaction {
TransactionType: 'TrustSet'
LimitAmount: Amount
QualityIn?: number
QualityOut?: number
Flags?: number | TrustSetFlags
}
/**
*
* @param tx A TrustSet Transaction.
* @returns {void}
* @throws {ValidationError} When the TrustSet is malformed.
*/
export function verifyTrustSet(tx: TrustSet): void {
verifyBaseTransaction(tx)
const { LimitAmount, QualityIn, QualityOut } = tx
if (LimitAmount === undefined) {
throw new ValidationError('TrustSet: missing field LimitAmount')
}
if (!isAmount(LimitAmount)) {
throw new ValidationError('TrustSet: invalid LimitAmount')
}
if (QualityIn !== undefined && typeof QualityIn !== 'number') {
throw new ValidationError('TrustSet: QualityIn must be a number')
}
if (QualityOut !== undefined && typeof QualityOut !== 'number') {
throw new ValidationError('TrustSet: QualityOut must be a number')
}
}