Skip to content

Commit 427b1e7

Browse files
datedspkjp
authored andcommitted
refactor(crypto): throw error if multi payment has less than two payments (#3345)
1 parent 443d34e commit 427b1e7

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

__tests__/unit/crypto/transactions/builders/transactions/multi-payment.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import "jest-extended";
22

33
import { TransactionType } from "../../../../../../packages/crypto/src/enums";
4-
import { MaximumPaymentCountExceededError } from "../../../../../../packages/crypto/src/errors";
4+
import {
5+
MaximumPaymentCountExceededError,
6+
MinimumPaymentCountSubceededError,
7+
} from "../../../../../../packages/crypto/src/errors";
58
import { BuilderFactory, MultiPaymentTransaction } from "../../../../../../packages/crypto/src/transactions";
69
import { MultiPaymentBuilder } from "../../../../../../packages/crypto/src/transactions/builders/transactions/multi-payment";
710
import { BigNumber } from "../../../../../../packages/crypto/src/utils";
@@ -59,4 +62,18 @@ describe("Multi Payment Transaction", () => {
5962
expect(() => builder.addPayment("address", "2")).toThrow(MaximumPaymentCountExceededError);
6063
});
6164
});
65+
66+
describe("getStruct", () => {
67+
it("should throw if payments is undefined", () => {
68+
builder.data.asset.payments = undefined;
69+
70+
expect(() => builder.getStruct()).toThrow(MinimumPaymentCountSubceededError);
71+
});
72+
73+
it("should throw if payments count is less than min required", () => {
74+
builder.addPayment("address", "1");
75+
76+
expect(() => builder.getStruct()).toThrow(MinimumPaymentCountSubceededError);
77+
});
78+
});
6279
});

__tests__/unit/crypto/transactions/schemas.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -748,17 +748,6 @@ describe("Multi Payment Transaction", () => {
748748
expect(error).toBeUndefined();
749749
});
750750

751-
it("should be invalid with 0 or 1 payment", () => {
752-
multiPayment.sign("passphrase");
753-
const { error: errorZeroPayment } = Ajv.validate(transactionSchema.$id, multiPayment.getStruct());
754-
expect(errorZeroPayment).not.toBeUndefined();
755-
756-
multiPayment.addPayment(address, "100").sign("passphrase");
757-
758-
const { error: errorOnePayment } = Ajv.validate(transactionSchema.$id, multiPayment.getStruct());
759-
expect(errorOnePayment).not.toBeUndefined();
760-
});
761-
762751
it("should not accept more than `multiPaymentLimit` payments", () => {
763752
const limit = configManager.getMilestone().multiPaymentLimit;
764753

packages/crypto/src/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ export class MaximumPaymentCountExceededError extends CryptoError {
132132
}
133133
}
134134

135+
export class MinimumPaymentCountSubceededError extends CryptoError {
136+
constructor() {
137+
super(`Number of payments subceeded the required minimum of 2.`);
138+
}
139+
}
140+
135141
export class VendorFieldLengthExceededError extends CryptoError {
136142
constructor(limit: number) {
137143
super(`Length of vendor field exceeded the allowed maximum ${limit}.`);

packages/crypto/src/transactions/builders/transactions/multi-payment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MaximumPaymentCountExceededError } from "../../../errors";
1+
import { MaximumPaymentCountExceededError, MinimumPaymentCountSubceededError } from "../../../errors";
22
import { ITransactionData } from "../../../interfaces";
33
import { configManager } from "../../../managers";
44
import { BigNumber } from "../../../utils";
@@ -35,6 +35,14 @@ export class MultiPaymentBuilder extends TransactionBuilder<MultiPaymentBuilder>
3535
}
3636

3737
public getStruct(): ITransactionData {
38+
if (
39+
!this.data.asset.payments ||
40+
!Array.isArray(this.data.asset.payments) ||
41+
this.data.asset.payments.length <= 1
42+
) {
43+
throw new MinimumPaymentCountSubceededError();
44+
}
45+
3846
const struct: ITransactionData = super.getStruct();
3947
struct.senderPublicKey = this.data.senderPublicKey;
4048
struct.vendorField = this.data.vendorField;

0 commit comments

Comments
 (0)