Skip to content

Commit 8b76b97

Browse files
chore(sdk-core): preprocess build params before submitting
In the SDK we handle OP_RETURN outputs by setting them in the address field of the output with the format `scriptPubkey:HEX_ENCODED_SCRIPT_PUBKEY`. When we submit to wallet platform, we want to convert that output to the format { script: string, amount: string | number }. Before submitting the buildParams to the API, we want to convert it. In every other instance for other coins we just want to return the same parameters. TICKET: BTC-1582
1 parent a50bed8 commit 8b76b97

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,21 @@ export abstract class AbstractUtxoCoin extends BaseCoin {
448448
}
449449
}
450450

451+
preprocessBuildParams(params: Record<string, any>): Record<string, any> {
452+
params.recipients =
453+
params.recipients && params.recipients instanceof Array
454+
? params?.recipients?.map((recipient) => {
455+
if (recipient.address.startsWith(ScriptRecipientPrefix)) {
456+
const { address, ...rest } = recipient;
457+
return { ...rest, script: address.replace(ScriptRecipientPrefix, '') };
458+
}
459+
return recipient;
460+
})
461+
: params.recipients;
462+
463+
return params;
464+
}
465+
451466
/**
452467
* Get the latest block height
453468
* @param reqId

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ export abstract class BaseCoin implements IBaseCoin {
249249
return bigNumber.toFixed(0);
250250
}
251251

252+
/**
253+
* Preprocess the build parameters before sending to the API
254+
* @param buildParams
255+
*/
256+
preprocessBuildParams(buildParams: Record<string, any>): Record<string, any> {
257+
return buildParams;
258+
}
259+
252260
/**
253261
* Sign message with private key
254262
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ export interface IBaseCoin {
485485
getBaseFactor(): number | string;
486486
baseUnitsToBigUnits(baseUnits: string | number): string;
487487
bigUnitsToBaseUnits(bigUnits: string | number): string;
488+
preprocessBuildParams(params: Record<string, any>): Record<string, any>;
488489
signMessage(key: { prv: string }, message: string): Promise<Buffer>;
489490
createKeySignatures(
490491
prv: string,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ export class Wallet implements IWallet {
17471747
}
17481748

17491749
// Whitelist params to build tx
1750-
const whitelistedParams = _.pick(params, this.prebuildWhitelistedParams());
1750+
const whitelistedParams = this.baseCoin.preprocessBuildParams(_.pick(params, this.prebuildWhitelistedParams()));
17511751
debug('prebuilding transaction: %O', whitelistedParams);
17521752

17531753
if (params.reqId) {
@@ -3655,7 +3655,7 @@ export class Wallet implements IWallet {
36553655
// extract the whitelisted params from the top level, in case
36563656
// other invalid params are present that would fail encoding
36573657
// and fall back to the body params
3658-
const whitelistedParams = _.pick(params, whitelistedSendParams);
3658+
const whitelistedParams = this.baseCoin.preprocessBuildParams(_.pick(params, whitelistedSendParams));
36593659
const reqTracer = reqId || new RequestTracer();
36603660
this.bitgo.setRequestTracer(reqTracer);
36613661
return postWithCodec(

0 commit comments

Comments
 (0)