Skip to content

Commit a50bed8

Browse files
chore(sdk-core): refactor recipient checking to coin
TICKET: BTC-1582
1 parent 3750e2d commit a50bed8

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type UtxoCustomSigningFunction<TNumber extends number | bigint> = {
104104
}): Promise<SignedTransaction>;
105105
};
106106

107+
export const ScriptRecipientPrefix = 'scriptPubkey:';
107108
const { getExternalChainCode, isChainCode, scriptTypeForChain, outputScripts } = bitgo;
108109
type Unspent<TNumber extends number | bigint = number> = bitgo.Unspent<TNumber>;
109110

@@ -459,6 +460,17 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
459460
return (chainhead as any).height;
460461
}
461462

463+
checkRecipient(recipient: { address: string; amount: number | string }): void {
464+
if (recipient.address.startsWith(ScriptRecipientPrefix)) {
465+
const amount = BigInt(recipient.amount);
466+
if (amount !== BigInt(0)) {
467+
throw new Error('Only zero amounts allowed for non-encodeable scriptPubkeys');
468+
}
469+
} else {
470+
super.checkRecipient(recipient);
471+
}
472+
}
473+
462474
/**
463475
* Run custom coin logic after a transaction prebuild has been received from BitGo
464476
* @param prebuild

modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ export abstract class BaseCoin implements IBaseCoin {
225225
return bigNumber.toFormat(null as any, null as any, { groupSeparator: '', decimalSeparator: '.' });
226226
}
227227

228+
checkRecipient(recipient: { address: string; amount: string | number }): void {
229+
const amount = new BigNumber(recipient.amount);
230+
if (amount.isNegative()) {
231+
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
232+
}
233+
if (!this.valuelessTransferAllowed() && amount.isZero()) {
234+
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
235+
}
236+
}
237+
228238
/**
229239
* Convert a currency amount represented in big units (btc, eth, xrp, xlm)
230240
* to base units (satoshi, wei, atoms, drops, stroops)

modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ export interface IBaseCoin {
473473
getFamily(): string;
474474
getFullName(): string;
475475
valuelessTransferAllowed(): boolean;
476+
checkRecipient(recipient: { address: string; amount: string | number }): void;
476477
sweepWithSendMany(): boolean;
477478
transactionDataAllowed(): boolean;
478479
allowsAccountConsolidations(): boolean;

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,13 +2484,7 @@ export class Wallet implements IWallet {
24842484
const coin = this.baseCoin;
24852485
if (_.isObject(params.recipients)) {
24862486
params.recipients.map(function (recipient) {
2487-
const amount = new BigNumber(recipient.amount);
2488-
if (amount.isNegative()) {
2489-
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
2490-
}
2491-
if (!coin.valuelessTransferAllowed() && amount.isZero()) {
2492-
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
2493-
}
2487+
coin.checkRecipient(recipient);
24942488
});
24952489
}
24962490

0 commit comments

Comments
 (0)