Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions modules/bitgo/test/v2/unit/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
70 changes: 65 additions & 5 deletions modules/sdk-coin-sol/src/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
);
Expand Down Expand Up @@ -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']));

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -908,15 +922,61 @@ 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' &&
txParams !== null &&
'feePayer' in txParams &&
typeof txParams.feePayer === 'string'
? txParams.feePayer
: undefined);
if (consolidateId === undefined && !isCloseAssociatedTokenAccountTx) {
: txRequestFeePayer);
const hasTokenRecipient = txParams.recipients?.some((recipient) => recipient.tokenName !== undefined) ?? false;
const shouldInferConsolidation =
!isConsolidationTx &&
consolidateId === undefined &&
!isCloseAssociatedTokenAccountTx &&
!declaredFeePayer &&
transactionJson.feePayer !== walletRootAddress &&
hasTokenRecipient;
const inferredConsolidation =
shouldInferConsolidation &&
explainedTx.outputs.length > 0 &&
(
await Promise.all(
explainedTx.outputs.map(async (output) => {
if (!output.tokenName) {
return output.address === walletRootAddress;
}

try {
const token = getSolTokenFromTokenName(output.tokenName);
const associatedTokenAccount = await getAssociatedTokenAccountAddress(
token?.tokenAddress ?? output.tokenName,
walletRootAddress as string,
true,
token?.programId
);
return associatedTokenAccount === output.address;
} catch {
return false;
}
})
)
).every(Boolean);
if (
!isConsolidationTx &&
!inferredConsolidation &&
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
Expand Down
32 changes: 32 additions & 0 deletions modules/sdk-coin-sol/test/unit/sol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ export interface TransactionPrebuild extends BaseSignable {
txBase64?: string;
txHex?: string;
txInfo?: unknown;
coinSpecific?: Record<string, unknown>;
}

export interface XpubWithDerivationPath {
Expand Down
12 changes: 11 additions & 1 deletion modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5146,12 +5146,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,
Expand Down
Loading