diff --git a/modules/bitgo/test/v2/unit/accountConsolidations.ts b/modules/bitgo/test/v2/unit/accountConsolidations.ts index 0adf64adab..4dd0161e9d 100644 --- a/modules/bitgo/test/v2/unit/accountConsolidations.ts +++ b/modules/bitgo/test/v2/unit/accountConsolidations.ts @@ -77,6 +77,27 @@ describe('Account Consolidations:', function () { accountConsolidationBuild.length.should.equal(2); scope.isDone().should.be.True(); }); + it('should retain consolidation metadata on TSS prebuilds', async function () { + const tssWallet = new Wallet(bitgo, basecoin, { + id: wallet.id(), + coin: coinName, + multisigType: 'tss', + }); + const scope = nock(bgUrl) + .post(`/api/v2/${coinName}/wallet/${tssWallet.id()}/consolidateAccount/build`, { + consolidationMode: 'single-asset', + }) + .reply(200, fixtures.buildAccountConsolidation); + + const accountConsolidationBuild = await tssWallet.buildAccountConsolidations({ + consolidationMode: 'single-asset', + }); + + accountConsolidationBuild.forEach((build) => { + build.buildParams?.type.should.equal('consolidate'); + }); + scope.isDone().should.be.True(); + }); it('should throw if the result is an empty array', async function () { const scope = nock(bgUrl) diff --git a/modules/bitgo/test/v2/unit/wallet.ts b/modules/bitgo/test/v2/unit/wallet.ts index 59b780902a..f015174f87 100644 --- a/modules/bitgo/test/v2/unit/wallet.ts +++ b/modules/bitgo/test/v2/unit/wallet.ts @@ -3186,6 +3186,26 @@ describe('V2 Wallet:', function () { }, }); }); + it('should propagate coin-specific txRequest metadata to the prebuild', async function () { + const feePayerPolicy = { + sponsorshipMode: 'enterprise', + feePayerAddress: '5hr5fisPi6DXNuuRpm5XUbzpiEnmdyxXuBDTwzwZj5Pe', + }; + const prebuildTxWithIntent = sandbox.stub(TssUtils.prototype, 'prebuildTxWithIntent'); + prebuildTxWithIntent.resolves({ + ...txRequest, + unsignedTxs: [{ ...txRequest.unsignedTxs[0], coinSpecific: { solFeePayerPolicy: feePayerPolicy } }], + }); + + const txPrebuild = await tssSolWallet.prebuildTransaction({ + reqId, + recipients: [], + type: 'transfer', + }); + + assert.deepStrictEqual(txPrebuild.coinSpecific, { solFeePayerPolicy: feePayerPolicy }); + assert.strictEqual(txPrebuild.buildParams?.feePayer, feePayerPolicy.feePayerAddress); + }); it('should build a single recipient transfer with pending approval id if transaction is having one', async function () { const recipients = [ diff --git a/modules/sdk-coin-sol/src/sol.ts b/modules/sdk-coin-sol/src/sol.ts index 1aae1f293c..8148efe6b1 100644 --- a/modules/sdk-coin-sol/src/sol.ts +++ b/modules/sdk-coin-sol/src/sol.ts @@ -768,9 +768,23 @@ export class Sol extends BaseCoin { } const isTokenEnablementTx = txParams.type === 'enabletoken'; + let consolidationMode: unknown; + if (typeof txParams === 'object' && txParams !== null && 'consolidationMode' in txParams) { + consolidationMode = txParams.consolidationMode; + } + const hasConsolidationMode = consolidationMode === 'single-asset' || consolidationMode === 'legacy-multi-asset'; + const isConsolidationTx = + txParams.type === 'consolidate' || + hasConsolidationMode || + verificationOptions?.consolidationToBaseAddress === true; // users do not input recipients for consolidation requests as they are generated by the server // Close-ATA txs do not populate explainedTx.outputs; recipients carry ATA addresses for intent only. - if (txParams.recipients !== undefined && !isTokenEnablementTx && !isCloseAssociatedTokenAccountTx) { + if ( + !isConsolidationTx && + txParams.recipients !== undefined && + !isTokenEnablementTx && + !isCloseAssociatedTokenAccountTx + ) { const filteredRecipients = txParams.recipients?.map((recipient) => _.pick(recipient, ['address', 'amount', 'tokenName', 'tokenAddress', 'programId']) ); @@ -835,7 +849,7 @@ export class Sol extends BaseCoin { if (recipientChecks.includes(false)) { throw new Error('Tx outputs does not match with expected txParams recipients'); } - } else if (verificationOptions?.consolidationToBaseAddress) { + } else if (isConsolidationTx) { //verify funds are sent to walletRootAddress for a consolidation const filteredOutputs = explainedTx.outputs.map((output) => _.pick(output, ['address', 'amount', 'tokenName'])); @@ -878,7 +892,7 @@ export class Sol extends BaseCoin { if (memo && memo.value !== explainedTx.memo) { throw new Error('Tx memo does not match with expected txParams recipient memo'); } - if (txParams.recipients && !isTokenEnablementTx && !isCloseAssociatedTokenAccountTx) { + if (!isConsolidationTx && txParams.recipients && !isTokenEnablementTx && !isCloseAssociatedTokenAccountTx) { for (const recipients of txParams.recipients) { // totalAmount based on each token const assetName = recipients.tokenName || this.getChain(); @@ -908,6 +922,14 @@ export class Sol extends BaseCoin { // or — when wallet-platform declares a sponsored fee payer — exactly that address. // The closed match only accepts the specific fee payer declared for this // transaction; every other non-root fee payer still throws. (CHALO-1749) + const solFeePayerPolicy = txPrebuild.coinSpecific?.solFeePayerPolicy; + const txRequestFeePayer = + typeof solFeePayerPolicy === 'object' && + solFeePayerPolicy !== null && + 'feePayerAddress' in solFeePayerPolicy && + typeof solFeePayerPolicy.feePayerAddress === 'string' + ? solFeePayerPolicy.feePayerAddress + : undefined; const declaredFeePayer = params.feePayer ?? (typeof txParams === 'object' && @@ -915,8 +937,8 @@ export class Sol extends BaseCoin { 'feePayer' in txParams && typeof txParams.feePayer === 'string' ? txParams.feePayer - : undefined); - if (consolidateId === undefined && !isCloseAssociatedTokenAccountTx) { + : txRequestFeePayer); + if (!isConsolidationTx && consolidateId === undefined && !isCloseAssociatedTokenAccountTx) { if (declaredFeePayer) { // Sponsored: WP declared the fee payer for this transaction — strict match. // A mismatch means the transaction's fee payer was tampered with or the diff --git a/modules/sdk-coin-sol/test/unit/sol.ts b/modules/sdk-coin-sol/test/unit/sol.ts index 16ed66510b..696715ab0e 100644 --- a/modules/sdk-coin-sol/test/unit/sol.ts +++ b/modules/sdk-coin-sol/test/unit/sol.ts @@ -667,6 +667,38 @@ describe('SOL:', function () { } as any); result.should.equal(true); }); + it('should accept the fee payer from a persisted txRequest coinSpecific policy', async function () { + const txParams = newTxParams(); + const txPrebuild = newTxPrebuild(); + const walletData = { + id: '5b34252f1bf349930e34020a00000000', + coin: 'tsol', + keys: [ + '5b3424f91bf349930e34017500000000', + '5b3424f91bf349930e34017600000000', + '5b3424f91bf349930e34017700000000', + ], + coinSpecific: { + rootAddress: stakeAccount.pub, + }, + multisigType: 'tss', + }; + const walletWithDifferentRoot = new Wallet(bitgo, basecoin, walletData); + txPrebuild.coinSpecific = { + solFeePayerPolicy: { + sponsorshipMode: 'enterprise', + feePayerAddress: '5hr5fisPi6DXNuuRpm5XUbzpiEnmdyxXuBDTwzwZj5Pe', + }, + }; + + const result = await basecoin.verifyTransaction({ + txParams, + txPrebuild, + memo, + wallet: walletWithDifferentRoot, + } as unknown as SolVerifyTransactionOptions); + result.should.equal(true); + }); it('accepts a fee payer declared inside txParams by the wallet verification path', async function () { const txPrebuild = newTxPrebuild(); @@ -862,6 +894,28 @@ describe('SOL:', function () { } as any) .should.be.rejectedWith('Number of tx outputs does not match with number of txParams recipients'); }); + const buildConsolidationRaw = async (feePayer: string) => { + const txBuilder = factory.getTransferBuilder(); + txBuilder.sender(wallet.pub); + txBuilder.feePayer(feePayer); + txBuilder.nonce(blockHash); + txBuilder.send({ address: wallet.pub, amount: '1000' }); + return (await txBuilder.build()).toBroadcastFormat(); + }; + + for (const consolidationMode of ['single-asset', 'legacy-multi-asset'] as const) { + it(`should verify ${consolidationMode} consolidation with a sponsored fee payer`, async function () { + const txBase64 = await buildConsolidationRaw(stakeAccount.pub); + const result = await basecoin.verifyTransaction({ + txParams: { consolidationMode, recipients: [] }, + txPrebuild: { txBase64 }, + wallet: walletObj, + feePayer: stakeAccount.pub, + blockhash: blockHash, + } as unknown as SolVerifyTransactionOptions); + result.should.equal(true); + }); + } it('should reject a Marinade unstake whose transfer is not sent from the wallet root', async function () { const otherSender = stakeAccount.pub; diff --git a/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts b/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts index 5d18c248ad..3720151250 100644 --- a/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts +++ b/modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts @@ -454,6 +454,7 @@ export interface TransactionPrebuild extends BaseSignable { txBase64?: string; txHex?: string; txInfo?: unknown; + coinSpecific?: Record; } export interface XpubWithDerivationPath { diff --git a/modules/sdk-core/src/bitgo/wallet/wallet.ts b/modules/sdk-core/src/bitgo/wallet/wallet.ts index 6b4e129b2a..da51eacac1 100644 --- a/modules/sdk-core/src/bitgo/wallet/wallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/wallet.ts @@ -3968,15 +3968,19 @@ export class Wallet implements IWallet { // we need to step over each prebuild now - should be in an array in the body const consolidations: PrebuildTransactionResult[] = []; for (const consolidateAccountBuild of buildResponse) { + const buildParams = { ...whitelistedParams, type: 'consolidate' as const }; let prebuild: PrebuildTransactionResult = (await this.baseCoin.postProcessPrebuild( Object.assign(consolidateAccountBuild, { wallet: this, - buildParams: whitelistedParams, + buildParams, }) )) as PrebuildTransactionResult; delete prebuild.wallet; delete prebuild.buildParams; + if (this._wallet.multisigType === 'tss') { + prebuild.buildParams = buildParams; + } prebuild = _.extend({}, prebuild, { walletId: this.id() }); debug('final consolidation transaction prebuild: %O', prebuild); @@ -5146,12 +5150,22 @@ export class Wallet implements IWallet { } const whitelistedParams = _.pick(params, this.prebuildWhitelistedParams()); + const solFeePayerPolicy = unsignedTx.coinSpecific?.solFeePayerPolicy; + const feePayer = + typeof solFeePayerPolicy === 'object' && + solFeePayerPolicy !== null && + 'feePayerAddress' in solFeePayerPolicy && + typeof solFeePayerPolicy.feePayerAddress === 'string' + ? solFeePayerPolicy.feePayerAddress + : undefined; + const buildParams = feePayer ? { ...whitelistedParams, feePayer } : whitelistedParams; return { walletId: this.id(), wallet: this, txRequestId: txRequest.txRequestId, txHex: unsignedTx.serializedTxHex, - buildParams: whitelistedParams, + ...(unsignedTx.coinSpecific && { coinSpecific: unsignedTx.coinSpecific }), + buildParams, feeInfo: unsignedTx.feeInfo, ...(txRequest.pendingApprovalId && { pendingApprovalId: txRequest.pendingApprovalId,