Skip to content

Commit 996a8d7

Browse files
committed
Add validateOutput call to TransactionBuilder
1 parent d5687f5 commit 996a8d7

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

packages/cashscript/src/Transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
getTxSizeWithoutInputs,
3131
getPreimageSize,
3232
buildError,
33-
validateRecipient,
33+
validateOutput,
3434
utxoComparator,
3535
calculateDust,
3636
getOutputSize,
@@ -100,7 +100,7 @@ export class Transaction {
100100
}
101101

102102
if (Array.isArray(toOrOutputs) && amount === undefined) {
103-
toOrOutputs.forEach(validateRecipient);
103+
toOrOutputs.forEach(validateOutput);
104104
this.outputs = this.outputs.concat(toOrOutputs);
105105
return this;
106106
}

packages/cashscript/src/TransactionBuilder.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import {
1212
isUnlockableUtxo,
1313
} from './interfaces.js';
1414
import { NetworkProvider } from './network/index.js';
15-
import { buildError, cashScriptOutputToLibauthOutput, createOpReturnOutput } from './utils.js';
15+
import {
16+
buildError,
17+
cashScriptOutputToLibauthOutput,
18+
createOpReturnOutput,
19+
validateOutput,
20+
} from './utils.js';
1621

1722
export interface TransactionBuilderOptions {
1823
provider: NetworkProvider;
@@ -60,11 +65,11 @@ export class TransactionBuilder {
6065
}
6166

6267
addOutput(output: Output): this {
63-
this.outputs.push(output);
64-
return this;
68+
return this.addOutputs([output]);
6569
}
6670

6771
addOutputs(outputs: Output[]): this {
72+
outputs.forEach(validateOutput);
6873
this.outputs = this.outputs.concat(outputs);
6974
return this;
7075
}

packages/cashscript/src/utils.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
Utxo,
2727
Output,
2828
Network,
29-
Recipient,
3029
LibauthOutput,
3130
} from './interfaces.js';
3231
import { VERSION_SIZE, LOCKTIME_SIZE } from './constants.js';
@@ -41,21 +40,23 @@ import {
4140
} from './Errors.js';
4241

4342
// ////////// PARAMETER VALIDATION ////////////////////////////////////////////
44-
export function validateRecipient(recipient: Recipient): void {
45-
const minimumAmount = calculateDust(recipient);
46-
if (recipient.amount < minimumAmount) {
47-
throw new OutputSatoshisTooSmallError(recipient.amount, BigInt(minimumAmount));
43+
export function validateOutput(output: Output): void {
44+
if (typeof output.to !== 'string') return;
45+
46+
const minimumAmount = calculateDust(output);
47+
if (output.amount < minimumAmount) {
48+
throw new OutputSatoshisTooSmallError(output.amount, BigInt(minimumAmount));
4849
}
4950

50-
if (recipient.token) {
51-
if (!isTokenAddress(recipient.to)) {
52-
throw new TokensToNonTokenAddressError(recipient.to);
51+
if (output.token) {
52+
if (!isTokenAddress(output.to)) {
53+
throw new TokensToNonTokenAddressError(output.to);
5354
}
5455
}
5556
}
5657

57-
export function calculateDust(recipient: Recipient): number {
58-
const outputSize = getOutputSize(recipient);
58+
export function calculateDust(output: Output): number {
59+
const outputSize = getOutputSize(output);
5960
// Formula used to calculate the minimum allowed output
6061
const dustAmount = 444 + outputSize * 3;
6162
return dustAmount;

0 commit comments

Comments
 (0)