Skip to content

Commit

Permalink
refactor(crypto): throw error if multi payment has less than two paym…
Browse files Browse the repository at this point in the history
…ents (#3345)
  • Loading branch information
dated authored and spkjp committed Dec 18, 2019
1 parent 443d34e commit 427b1e7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import "jest-extended";

import { TransactionType } from "../../../../../../packages/crypto/src/enums";
import { MaximumPaymentCountExceededError } from "../../../../../../packages/crypto/src/errors";
import {
MaximumPaymentCountExceededError,
MinimumPaymentCountSubceededError,
} from "../../../../../../packages/crypto/src/errors";
import { BuilderFactory, MultiPaymentTransaction } from "../../../../../../packages/crypto/src/transactions";
import { MultiPaymentBuilder } from "../../../../../../packages/crypto/src/transactions/builders/transactions/multi-payment";
import { BigNumber } from "../../../../../../packages/crypto/src/utils";
Expand Down Expand Up @@ -59,4 +62,18 @@ describe("Multi Payment Transaction", () => {
expect(() => builder.addPayment("address", "2")).toThrow(MaximumPaymentCountExceededError);
});
});

describe("getStruct", () => {
it("should throw if payments is undefined", () => {
builder.data.asset.payments = undefined;

expect(() => builder.getStruct()).toThrow(MinimumPaymentCountSubceededError);
});

it("should throw if payments count is less than min required", () => {
builder.addPayment("address", "1");

expect(() => builder.getStruct()).toThrow(MinimumPaymentCountSubceededError);
});
});
});
11 changes: 0 additions & 11 deletions __tests__/unit/crypto/transactions/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,17 +748,6 @@ describe("Multi Payment Transaction", () => {
expect(error).toBeUndefined();
});

it("should be invalid with 0 or 1 payment", () => {
multiPayment.sign("passphrase");
const { error: errorZeroPayment } = Ajv.validate(transactionSchema.$id, multiPayment.getStruct());
expect(errorZeroPayment).not.toBeUndefined();

multiPayment.addPayment(address, "100").sign("passphrase");

const { error: errorOnePayment } = Ajv.validate(transactionSchema.$id, multiPayment.getStruct());
expect(errorOnePayment).not.toBeUndefined();
});

it("should not accept more than `multiPaymentLimit` payments", () => {
const limit = configManager.getMilestone().multiPaymentLimit;

Expand Down
6 changes: 6 additions & 0 deletions packages/crypto/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export class MaximumPaymentCountExceededError extends CryptoError {
}
}

export class MinimumPaymentCountSubceededError extends CryptoError {
constructor() {
super(`Number of payments subceeded the required minimum of 2.`);
}
}

export class VendorFieldLengthExceededError extends CryptoError {
constructor(limit: number) {
super(`Length of vendor field exceeded the allowed maximum ${limit}.`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaximumPaymentCountExceededError } from "../../../errors";
import { MaximumPaymentCountExceededError, MinimumPaymentCountSubceededError } from "../../../errors";
import { ITransactionData } from "../../../interfaces";
import { configManager } from "../../../managers";
import { BigNumber } from "../../../utils";
Expand Down Expand Up @@ -35,6 +35,14 @@ export class MultiPaymentBuilder extends TransactionBuilder<MultiPaymentBuilder>
}

public getStruct(): ITransactionData {
if (
!this.data.asset.payments ||
!Array.isArray(this.data.asset.payments) ||
this.data.asset.payments.length <= 1
) {
throw new MinimumPaymentCountSubceededError();
}

const struct: ITransactionData = super.getStruct();
struct.senderPublicKey = this.data.senderPublicKey;
struct.vendorField = this.data.vendorField;
Expand Down

0 comments on commit 427b1e7

Please sign in to comment.