From f68eb37565530293fbc7e43de45144f1d96cfba7 Mon Sep 17 00:00:00 2001 From: Nathan Nichols Date: Thu, 19 Aug 2021 10:58:15 -0500 Subject: [PATCH] refactor: Define EscrowFinish transaction model (#1531) refactor: Define EscrowFinish transaction model (#1531) --- src/models/transactions/escrowFinish.ts | 39 +++++++++++++ src/models/transactions/index.ts | 1 + src/models/transactions/transaction.ts | 4 +- test/models/escrowFinish.ts | 75 +++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/models/transactions/escrowFinish.ts create mode 100644 test/models/escrowFinish.ts diff --git a/src/models/transactions/escrowFinish.ts b/src/models/transactions/escrowFinish.ts new file mode 100644 index 00000000..7d3c329f --- /dev/null +++ b/src/models/transactions/escrowFinish.ts @@ -0,0 +1,39 @@ +import { ValidationError } from "../../common/errors" +import { BaseTransaction, verifyBaseTransaction } from "./common" + +export interface EscrowFinish extends BaseTransaction { + TransactionType: "EscrowFinish" + Owner: string + OfferSequence: number + Condition?: string + Fulfillment?: string +} + +/** + * Verify the form and type of an EscrowFinish at runtime. + * + * @param tx - An EscrowFinish Transaction + * @returns - Void. + * @throws - When the EscrowFinish is Malformed. + */ + export function verifyEscrowFinish(tx: EscrowFinish): void { + verifyBaseTransaction(tx) + + if (tx.Owner === undefined) + throw new ValidationError("EscrowFinish: missing field Owner") + + if (typeof tx.Owner !== 'string') + throw new ValidationError("EscrowFinish: Owner must be a string") + + if (tx.OfferSequence === undefined) + throw new ValidationError("EscrowFinish: missing field OfferSequence") + + if (typeof tx.OfferSequence !== 'number') + throw new ValidationError("EscrowFinish: OfferSequence must be a number") + + if (tx.Condition !== undefined && typeof tx.Condition !== 'string') + throw new ValidationError("EscrowFinish: Condition must be a string") + + if (tx.Fulfillment !== undefined && typeof tx.Fulfillment !== 'string') + throw new ValidationError("EscrowFinish: Fulfillment must be a string") +} \ No newline at end of file diff --git a/src/models/transactions/index.ts b/src/models/transactions/index.ts index 0cccba7b..5808d223 100644 --- a/src/models/transactions/index.ts +++ b/src/models/transactions/index.ts @@ -7,6 +7,7 @@ export * from './checkCreate' export * from './signerListSet' export * from './depositPreauth' export * from './escrowCancel' +export * from './escrowFinish' export * from './offerCancel' export * from './offerCreate' export * from './paymentTransaction' diff --git a/src/models/transactions/transaction.ts b/src/models/transactions/transaction.ts index 6bda6f33..76370bb9 100644 --- a/src/models/transactions/transaction.ts +++ b/src/models/transactions/transaction.ts @@ -6,6 +6,7 @@ import { CheckCash } from "./checkCash"; import { CheckCreate } from "./checkCreate"; import { DepositPreauth } from "./depositPreauth" import { EscrowCancel } from './escrowCancel' +import { EscrowFinish } from "./escrowFinish" import { OfferCancel } from "./offerCancel" import { OfferCreate } from "./offerCreate" import { PaymentTransaction } from "./paymentTransaction" @@ -21,9 +22,8 @@ export type Transaction = | DepositPreauth | EscrowCancel // | EscrowCreate -// | EscrowFinish + | EscrowFinish | OfferCancel -// | OfferCancel | OfferCreate | PaymentTransaction // | PaymentChannelClaim diff --git a/test/models/escrowFinish.ts b/test/models/escrowFinish.ts new file mode 100644 index 00000000..c16faf5e --- /dev/null +++ b/test/models/escrowFinish.ts @@ -0,0 +1,75 @@ +import { ValidationError } from 'xrpl-local/common/errors' +import { verifyEscrowFinish } from './../../src/models/transactions/escrowFinish' +import { assert } from 'chai' + +/** + * EscrowFinish Transaction Verification Testing + * + * Providing runtime verification testing for each specific transaction type + */ +describe('EscrowFinish Transaction Verification', function () { + let escrow + + beforeEach(() => { + escrow = { + Account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + TransactionType: "EscrowFinish", + Owner: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", + OfferSequence: 7, + Condition: "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855810100", + Fulfillment: "A0028000" + } + }) + it (`verifies valid EscrowFinish`, () => { + assert.doesNotThrow(() => verifyEscrowFinish(escrow)) + }) + + it (`verifies valid EscrowFinish w/o optional`, () => { + delete escrow.Condition + delete escrow.Fulfillment + + assert.doesNotThrow(() => verifyEscrowFinish(escrow)) + }) + + + it (`throws w/ invalid Owner`, () => { + escrow.Owner = 0x15415253 + + + assert.throws( + () => verifyEscrowFinish(escrow), + ValidationError, + "EscrowFinish: Owner must be a string" + ) + }) + + it (`throws w/ invalid OfferSequence`, () => { + escrow.OfferSequence = "10" + + assert.throws( + () => verifyEscrowFinish(escrow), + ValidationError, + "EscrowFinish: OfferSequence must be a number" + ) + }) + + it (`throws w/ invalid Condition`, () => { + escrow.Condition = 10 + + assert.throws( + () => verifyEscrowFinish(escrow), + ValidationError, + "EscrowFinish: Condition must be a string" + ) + }) + + it (`throws w/ invalid Fulfillment`, () => { + escrow.Fulfillment = 0x142341 + + assert.throws( + () => verifyEscrowFinish(escrow), + ValidationError, + "EscrowFinish: Fulfillment must be a string" + ) + }) +}) \ No newline at end of file