From 4469d1cbf8dcfb0a4c25a1cede69890cde9b225c Mon Sep 17 00:00:00 2001 From: Nathan Nichols Date: Thu, 19 Aug 2021 11:08:45 -0500 Subject: [PATCH] refactor: Define PaymentChannelClaim transaction model (#1536) refactor: Define PaymentChannelClaim transaction model (#1536) --- src/models/transactions/index.ts | 2 +- .../transactions/paymentChannelClaim.ts | 46 +++++++++ src/models/transactions/transaction.ts | 3 +- test/models/paymentChannelClaim.ts | 98 +++++++++++++++++++ 4 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/models/transactions/paymentChannelClaim.ts create mode 100644 test/models/paymentChannelClaim.ts diff --git a/src/models/transactions/index.ts b/src/models/transactions/index.ts index dedfdd87..c8225f8e 100644 --- a/src/models/transactions/index.ts +++ b/src/models/transactions/index.ts @@ -12,7 +12,7 @@ export * from './escrowFinish' export * from './offerCancel' export * from './offerCreate' export * from './paymentTransaction' +export * from './paymentChannelClaim' export * from './paymentChannelCreate' export * from './signerListSet' export * from './trustSet' - diff --git a/src/models/transactions/paymentChannelClaim.ts b/src/models/transactions/paymentChannelClaim.ts new file mode 100644 index 00000000..1d27d148 --- /dev/null +++ b/src/models/transactions/paymentChannelClaim.ts @@ -0,0 +1,46 @@ +import { ValidationError } from "../../common/errors"; +import { BaseTransaction, GlobalFlags, verifyBaseTransaction } from "./common"; + +export interface PaymentChannelClaimFlags extends GlobalFlags { + tfRenew?: boolean; + tfClose?: boolean; +} + +export interface PaymentChannelClaim extends BaseTransaction { + TransactionType: "PaymentChannelClaim"; + Flags?: number | PaymentChannelClaimFlags + Channel: string; + Balance?: string; + Amount?: string; + Signature?: string; + PublicKey?: string; +} + +/** + * Verify the form and type of an PaymentChannelClaim at runtime. + * + * @param tx - An PaymentChannelClaim Transaction + * @returns - Void. + * @throws - When the PaymentChannelClaim is Malformed. + */ + export function verifyPaymentChannelClaim(tx: PaymentChannelClaim): void { + verifyBaseTransaction(tx) + + if (tx.Channel === undefined) + throw new ValidationError("PaymentChannelClaim: missing Channel") + + if (typeof tx.Channel !== 'string') + throw new ValidationError("PaymentChannelClaim: Channel must be a string") + + if (tx.Balance !== undefined && typeof tx.Balance !== 'string') + throw new ValidationError("PaymentChannelClaim: Balance must be a string") + + if (tx.Amount !== undefined && typeof tx.Amount !== 'string') + throw new ValidationError("PaymentChannelClaim: Amount must be a string") + + if (tx.Signature !== undefined && typeof tx.Signature !== 'string') + throw new ValidationError("PaymentChannelClaim: Signature must be a string") + + if (tx.PublicKey !== undefined && typeof tx.PublicKey !== 'string') + throw new ValidationError("PaymentChannelClaim: PublicKey must be a string") +} \ No newline at end of file diff --git a/src/models/transactions/transaction.ts b/src/models/transactions/transaction.ts index 05d809e5..69097c58 100644 --- a/src/models/transactions/transaction.ts +++ b/src/models/transactions/transaction.ts @@ -11,6 +11,7 @@ import { OfferCancel } from "./offerCancel" import { OfferCreate } from "./offerCreate" import { PaymentTransaction } from "./paymentTransaction" import { PaymentChannelCreate } from "./paymentChannelCreate" +import { PaymentChannelClaim } from './paymentChannelClaim' import { SignerListSet } from "./signerListSet" import { TicketCreate } from "./ticketCreate" import { TrustSet } from "./trustSet" @@ -28,7 +29,7 @@ export type Transaction = | OfferCancel | OfferCreate | PaymentTransaction -// | PaymentChannelClaim + | PaymentChannelClaim | PaymentChannelCreate // | PaymentChannelFund // | SetRegularKey diff --git a/test/models/paymentChannelClaim.ts b/test/models/paymentChannelClaim.ts new file mode 100644 index 00000000..e3d4e252 --- /dev/null +++ b/test/models/paymentChannelClaim.ts @@ -0,0 +1,98 @@ +import { ValidationError } from 'xrpl-local/common/errors' +import { verifyPaymentChannelClaim } from './../../src/models/transactions/paymentChannelClaim' +import { assert } from 'chai' + + +/** + * PaymentChannelClaim Transaction Verification Testing + * + * Providing runtime verification testing for each specific transaction type + */ +describe('PaymentChannelClaim Transaction Verification', function () { + let channel + + beforeEach(() => { + channel = { + "Account": "r...", + "TransactionType": "PaymentChannelClaim", + "Channel": "C1AE6DDDEEC05CF2978C0BAD6FE302948E9533691DC749DCDD3B9E5992CA6198", + "Balance": "1000000", + "Amount": "1000000", + "Signature": "30440220718D264EF05CAED7C781FF6DE298DCAC68D002562C9BF3A07C1E721B420C0DAB02203A5A4779EF4D2CCC7BC3EF886676D803A9981B928D3B8ACA483B80ECA3CD7B9B", + "PublicKey": "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A" + } + }) + + it (`verifies valid PaymentChannelClaim`, () => { + assert.doesNotThrow(() => verifyPaymentChannelClaim(channel)) + }) + + it (`verifies valid PaymentChannelClaim w/o optional`, () => { + delete channel.Balance + delete channel.Amount + delete channel.Signature + delete channel.PublicKey + + assert.doesNotThrow(() => verifyPaymentChannelClaim(channel)) + }) + + it (`throws w/ missing Channel`, () => { + delete channel.Channel + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: missing Channel" + ) + }) + + it (`throws w/ invalid Channel`, () => { + channel.Channel = 100 + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: Channel must be a string" + ) + }) + + it (`throws w/ invalid Balance`, () => { + channel.Balance = 100 + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: Balance must be a string" + ) + }) + + it (`throws w/ invalid Amount`, () => { + channel.Amount = 1000 + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: Amount must be a string" + ) + }) + + it (`throws w/ invalid Signature`, () => { + channel.Signature = 1000 + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: Signature must be a string" + ) + }) + + it (`throws w/ invalid PublicKey`, () => { + channel.PublicKey = ["100000"] + + assert.throws( + () => verifyPaymentChannelClaim(channel), + ValidationError, + "PaymentChannelClaim: PublicKey must be a string" + ) + }) +}) \ No newline at end of file