Skip to content

Commit c0679bd

Browse files
authored
Merge pull request #10082 from BitGo/prabhsharansingh/chalo-1957-sdk-empty-recipients
fix(sdk-core): send empty recipients for ERC-7984 builds (CHALO-1957)
2 parents 42039fb + 816eb19 commit c0679bd

5 files changed

Lines changed: 105 additions & 22 deletions

File tree

‎modules/bitgo/test/v2/unit/pendingApproval.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ describe('Pending Approvals:', () => {
481481
}
482482

483483
(['wrap', 'finalizeUnwrap'] as const).forEach((legType) => {
484-
it(`strips stored recipients when approving a ${legType} intent pending approval`, async () => {
484+
it(`sends empty recipients when approving a ${legType} intent pending approval`, async () => {
485485
const { pendingApproval, htethCoin, paData } = makeEthIntentPendingApproval(legType);
486486

487487
const prebuildAndSignStub = sandbox
@@ -502,7 +502,7 @@ describe('Pending Approvals:', () => {
502502
paScope.isDone().should.be.true();
503503
prebuildAndSignStub.calledOnce.should.be.true();
504504
const prebuildParamsArg = prebuildAndSignStub.getCall(0).args[0]!;
505-
should.not.exist(prebuildParamsArg.recipients);
505+
prebuildParamsArg.recipients!.should.deepEqual([]);
506506
prebuildParamsArg.type!.should.equal(legType);
507507
(prebuildParamsArg.pendingApprovalId as string).should.equal(`pa-${legType}`);
508508
});
@@ -633,7 +633,7 @@ describe('Pending Approvals:', () => {
633633
.post(`/api/v2/hteth/wallet/${walletId}/tx/build`, (body) => {
634634
body.type.should.equal(legType);
635635
body.pendingApprovalId.should.equal(paId);
636-
should.not.exist(body.recipients);
636+
body.recipients.should.deepEqual([]);
637637
should.not.exist(body.wrapParams);
638638
if (legType === 'unwrap') {
639639
body.unwrapParams.should.deepEqual(unwrapParams);

‎modules/bitgo/test/v2/unit/wallet.ts‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,21 +654,28 @@ describe('V2 Wallet:', function () {
654654

655655
it('should send wrapParams to /tx/build when prebuilding wrapApprove on an onchain multi-sig wallet', async function () {
656656
const wrapParams = { tokenName: 'hteth:cusdt', amount: '1000000' };
657+
const callerParams: { type: string; wrapParams: { tokenName: string; amount: string }; recipients?: unknown } = {
658+
type: 'wrapApprove',
659+
wrapParams,
660+
};
657661
const buildNock = nock(bgUrl)
658662
.post(`/api/v2/${ethWallet.coin()}/wallet/${ethWallet.id()}/tx/build`, (body) => {
659663
body.type.should.equal('wrapApprove');
660664
body.wrapParams.should.deepEqual(wrapParams);
661-
should.not.exist(body.recipients);
665+
body.recipients.should.deepEqual([]);
662666
return true;
663667
})
664668
.reply(200, { txHex: '0xfaketxhex', feeInfo: { fee: '1000' } });
665669

666670
try {
667-
await ethWallet.prebuildTransaction({ type: 'wrapApprove', wrapParams });
671+
await ethWallet.prebuildTransaction(callerParams);
668672
} catch (e) {
669673
// post-processing of the fake prebuild may fail; the contract under test is the request body
670674
}
671675
buildNock.isDone().should.be.true();
676+
// the caller's params must stay untouched: they flow into prebuildAndSignTransaction
677+
// (prebuildTx + sign flows), where a truthy recipients trips the prebuildTx check
678+
should.not.exist(callerParams.recipients);
672679
});
673680

674681
it('should send unwrapParams to /tx/build when prebuilding unwrap on an onchain multi-sig wallet', async function () {
@@ -677,7 +684,7 @@ describe('V2 Wallet:', function () {
677684
.post(`/api/v2/${ethWallet.coin()}/wallet/${ethWallet.id()}/tx/build`, (body) => {
678685
body.type.should.equal('unwrap');
679686
body.unwrapParams.should.deepEqual(unwrapParams);
680-
should.not.exist(body.recipients);
687+
body.recipients.should.deepEqual([]);
681688
return true;
682689
})
683690
.reply(200, { txHex: '0xfaketxhex', feeInfo: { fee: '1000' } });
@@ -690,6 +697,40 @@ describe('V2 Wallet:', function () {
690697
buildNock.isDone().should.be.true();
691698
});
692699

700+
it('should send empty recipients to /tx/build when prebuilding wrap (request-shape contract)', async function () {
701+
const buildNock = nock(bgUrl)
702+
.post(`/api/v2/${ethWallet.coin()}/wallet/${ethWallet.id()}/tx/build`, (body) => {
703+
body.type.should.equal('wrap');
704+
body.recipients.should.deepEqual([]);
705+
return true;
706+
})
707+
.reply(200, { txHex: '0xfaketxhex', feeInfo: { fee: '1000' } });
708+
709+
try {
710+
await ethWallet.prebuildTransaction({ type: 'wrap' });
711+
} catch (e) {
712+
// the server rejects direct wrap builds; the contract under test is the request body
713+
}
714+
buildNock.isDone().should.be.true();
715+
});
716+
717+
it('should send empty recipients to /tx/build when prebuilding finalizeUnwrap (request-shape contract)', async function () {
718+
const buildNock = nock(bgUrl)
719+
.post(`/api/v2/${ethWallet.coin()}/wallet/${ethWallet.id()}/tx/build`, (body) => {
720+
body.type.should.equal('finalizeUnwrap');
721+
body.recipients.should.deepEqual([]);
722+
return true;
723+
})
724+
.reply(200, { txHex: '0xfaketxhex', feeInfo: { fee: '1000' } });
725+
726+
try {
727+
await ethWallet.prebuildTransaction({ type: 'finalizeUnwrap' });
728+
} catch (e) {
729+
// the server rejects direct finalizeUnwrap builds; the contract under test is the request body
730+
}
731+
buildNock.isDone().should.be.true();
732+
});
733+
693734
it('should not pass recipients in sendMany when transaction type is finalizeUnwrap', async function () {
694735
const recipients = [
695736
{
@@ -724,6 +765,37 @@ describe('V2 Wallet:', function () {
724765
caught!.message.should.equal('cannot provide recipients for transaction type wrapApprove');
725766
caught!.code!.should.equal('recipients_not_allowed_for_fillnonce_and_acceleration_tx_type');
726767
});
768+
769+
it('allows an empty recipients array when prebuilding wrapApprove (server-derived shape)', async function () {
770+
const nockKeychain = nock(bgUrl).get(`/api/v2/${ethWallet.coin()}/key/${ethWallet.keyIds()[0]}`).reply(200, {
771+
id: '598f606cd8fc24710d2ebad89dce86c2',
772+
pub: 'xpub661MyMwAqRbcFXDcWD2vxuebcT1ZpTF4Vke6qmMW8yzddwNYpAPjvYEEL5jLfyYXW2fuxtAxY8TgjPUJLcf1C8qz9N6VgZxArKX4EwB8rH5',
773+
ethAddress: '0x26a163ba9739529720c0914c583865dec0d37278',
774+
source: 'user',
775+
encryptedPrv:
776+
'{"iv":"15FsbDVI1zG9OggD8YX+Hg==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"hHbNH3Sz/aU=","ct":"WoNVKz7afiRxXI2w/YkzMdMyoQg/B15u1Q8aQgi96jJZ9wk6TIaSEc6bXFH3AHzD9MdJCWJQUpRhoQc/rgytcn69scPTjKeeyVMElGCxZdFVS/psQcNE+lue3//2Zlxj+6t1NkvYO+8yAezSMRBK5OdftXEjNQI="}',
777+
coinSpecific: {},
778+
});
779+
780+
// the guard must let the empty-array shape through; the failure below is the passphrase check
781+
await ethWallet
782+
.prebuildAndSignTransaction({ type: 'wrapApprove', recipients: [], walletPassphrase: 'wrongPassphrase' })
783+
.should.be.rejectedWith('unable to decrypt keychain with the given wallet passphrase');
784+
nockKeychain.isDone().should.be.true();
785+
});
786+
787+
it('still rejects an empty recipients array for fillNonce (guard regression)', async function () {
788+
let caught: (Error & { code?: string }) | undefined;
789+
try {
790+
await ethWallet.prebuildAndSignTransaction({ type: 'fillNonce', recipients: [] });
791+
} catch (e) {
792+
caught = e;
793+
}
794+
795+
should.exist(caught);
796+
caught!.message.should.equal('cannot provide recipients for transaction type fillNonce');
797+
caught!.code!.should.equal('recipients_not_allowed_for_fillnonce_and_acceleration_tx_type');
798+
});
727799
});
728800

729801
describe('OFC Create Address', () => {

‎modules/sdk-core/src/bitgo/pendingApproval/pendingApproval.ts‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { RequestTracer, RequestType } from '../utils';
1919
import { IWallet } from '../wallet';
2020
import { isSafeChildPublicOnlyKeychain } from '../wallet/safeKeychain';
21-
import { BuildParams } from '../wallet/BuildParams';
21+
import { BuildParams, ERC7984_INTENT_TX_TYPES } from '../wallet/BuildParams';
2222
import { IRequestTracer } from '../../api';
2323
import BaseTssUtils from '../utils/tss/baseTSSUtils';
2424
import EddsaUtils, { EddsaMPCv2Utils } from '../utils/tss/eddsa';
@@ -40,15 +40,6 @@ type ApprovePendingApprovalRequestBody = {
4040
halfSigned?: string | Omit<PreApproveResult, 'halfSigned'>;
4141
};
4242

43-
// ERC-7984 shield/unshield intent types: calldata is built server-side from the intent, so a
44-
// pending approval's stored recipients are display-only for the approval UI, never build inputs.
45-
const ERC7984_INTENT_TX_TYPES: Record<string, true> = {
46-
wrapApprove: true,
47-
wrap: true,
48-
unwrap: true,
49-
finalizeUnwrap: true,
50-
};
51-
5243
export class PendingApproval implements IPendingApproval {
5344
private readonly bitgo: BitGoBase;
5445
private readonly baseCoin: IBaseCoin;
@@ -313,11 +304,12 @@ export class PendingApproval implements IPendingApproval {
313304
const recipients = transactionRequest.recipients;
314305
let prebuildParams = _.extend({}, params, { recipients: recipients }, transactionRequest.buildParams);
315306

316-
// Stored recipients must not be forwarded for these intents — the prebuild guard rejects
317-
// recipients for them, and the /tx/build PA-rebuild carve-out re-derives the real inputs
318-
// server-side from the pendingApprovalId approve() injects.
319-
if (ERC7984_INTENT_TX_TYPES[prebuildParams.type]) {
320-
delete prebuildParams.recipients;
307+
// The stored recipients merged above are display-only for the approval UI. For the ERC-7984
308+
// intent types the real recipients are derived server-side, so the rebuild forwards an empty
309+
// array (the server-derived /tx/build shape, as with acceleration). Other PA types forward
310+
// their stored recipients unchanged.
311+
if (ERC7984_INTENT_TX_TYPES[prebuildParams.type] === true) {
312+
prebuildParams.recipients = [];
321313
}
322314

323315
if (!_.isUndefined(originalPrebuild.hopTransaction)) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import * as t from 'io-ts';
44
import { unifiedAddressPreference } from '@bitgo/public-types';
55
import { getCodecProperties } from '../utils/codecProps';
66

7+
// ERC-7984 intent tx types (ETH family): calldata is derived server-side from the intent, so
8+
// callers never supply recipients - an empty array is the server-derived build shape.
9+
export const ERC7984_INTENT_TX_TYPES: Record<string, true> = {
10+
wrapApprove: true,
11+
wrap: true,
12+
unwrap: true,
13+
finalizeUnwrap: true,
14+
};
15+
716
export const Bip322Message = t.type({
817
message: t.string,
918
address: t.string,

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { EcdsaMPCv2Utils, EcdsaUtils } from '../utils/tss/ecdsa';
5858
import EddsaUtils, { EddsaMPCv2Utils } from '../utils/tss/eddsa';
5959
import { RedpallasMPCv2Utils } from '../utils/tss/redpallas';
6060
import { getTxRequestApiVersion, validateTxRequestApiVersion } from '../utils/txRequest';
61-
import { buildParamKeys, BuildParams } from './BuildParams';
61+
import { buildParamKeys, BuildParams, ERC7984_INTENT_TX_TYPES } from './BuildParams';
6262
import {
6363
fetchRootKeychainForSafeChild,
6464
isSafeChildPublicOnlyKeychain,
@@ -2262,6 +2262,12 @@ export class Wallet implements IWallet {
22622262
}
22632263
const extraParams = await this.baseCoin.getExtraPrebuildParams(Object.assign(params, { wallet: this }));
22642264
Object.assign(whitelistedParams, extraParams);
2265+
// ERC-7984 intent builds derive recipients server-side; /tx/build requires the key (empty
2266+
// array). Default on the outgoing body only - the caller's params flow into
2267+
// prebuildAndSignTransaction, where a truthy recipients trips the prebuildTx check.
2268+
if (params.type !== undefined && ERC7984_INTENT_TX_TYPES[params.type] === true) {
2269+
whitelistedParams.recipients = whitelistedParams.recipients ?? [];
2270+
}
22652271
const queryParams = {
22662272
offlineVerification: params.offlineVerification ? true : undefined,
22672273
};
@@ -2780,8 +2786,12 @@ export class Wallet implements IWallet {
27802786
throw error;
27812787
}
27822788

2789+
// ERC-7984 intent types derive recipients server-side, so an empty array is a valid
2790+
// server-derived shape here (mirroring acceleration) - only populated lists are rejected.
2791+
const isErc7984IntentType = params.type !== undefined && ERC7984_INTENT_TX_TYPES[params.type] === true;
27832792
if (
27842793
params.recipients &&
2794+
!(isErc7984IntentType && params.recipients.length === 0) &&
27852795
(params.type === 'fillNonce' ||
27862796
params.type === 'acceleration' ||
27872797
params.type === 'wrapApprove' ||

0 commit comments

Comments
 (0)