Skip to content

Commit

Permalink
Add validateOutput call to TransactionBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Nov 1, 2023
1 parent d5687f5 commit 996a8d7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/cashscript/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
getTxSizeWithoutInputs,
getPreimageSize,
buildError,
validateRecipient,
validateOutput,
utxoComparator,
calculateDust,
getOutputSize,
Expand Down Expand Up @@ -100,7 +100,7 @@ export class Transaction {
}

if (Array.isArray(toOrOutputs) && amount === undefined) {
toOrOutputs.forEach(validateRecipient);
toOrOutputs.forEach(validateOutput);
this.outputs = this.outputs.concat(toOrOutputs);
return this;
}
Expand Down
11 changes: 8 additions & 3 deletions packages/cashscript/src/TransactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
isUnlockableUtxo,
} from './interfaces.js';
import { NetworkProvider } from './network/index.js';
import { buildError, cashScriptOutputToLibauthOutput, createOpReturnOutput } from './utils.js';
import {
buildError,
cashScriptOutputToLibauthOutput,
createOpReturnOutput,
validateOutput,
} from './utils.js';

export interface TransactionBuilderOptions {
provider: NetworkProvider;
Expand Down Expand Up @@ -60,11 +65,11 @@ export class TransactionBuilder {
}

addOutput(output: Output): this {
this.outputs.push(output);
return this;
return this.addOutputs([output]);
}

addOutputs(outputs: Output[]): this {
outputs.forEach(validateOutput);
this.outputs = this.outputs.concat(outputs);
return this;
}
Expand Down
21 changes: 11 additions & 10 deletions packages/cashscript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
Utxo,
Output,
Network,
Recipient,
LibauthOutput,
} from './interfaces.js';
import { VERSION_SIZE, LOCKTIME_SIZE } from './constants.js';
Expand All @@ -41,21 +40,23 @@ import {
} from './Errors.js';

// ////////// PARAMETER VALIDATION ////////////////////////////////////////////
export function validateRecipient(recipient: Recipient): void {
const minimumAmount = calculateDust(recipient);
if (recipient.amount < minimumAmount) {
throw new OutputSatoshisTooSmallError(recipient.amount, BigInt(minimumAmount));
export function validateOutput(output: Output): void {
if (typeof output.to !== 'string') return;

const minimumAmount = calculateDust(output);
if (output.amount < minimumAmount) {
throw new OutputSatoshisTooSmallError(output.amount, BigInt(minimumAmount));
}

if (recipient.token) {
if (!isTokenAddress(recipient.to)) {
throw new TokensToNonTokenAddressError(recipient.to);
if (output.token) {
if (!isTokenAddress(output.to)) {
throw new TokensToNonTokenAddressError(output.to);
}
}
}

export function calculateDust(recipient: Recipient): number {
const outputSize = getOutputSize(recipient);
export function calculateDust(output: Output): number {
const outputSize = getOutputSize(output);
// Formula used to calculate the minimum allowed output
const dustAmount = 444 + outputSize * 3;
return dustAmount;
Expand Down

1 comment on commit 996a8d7

@vercel
Copy link

@vercel vercel bot commented on 996a8d7 Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.