Skip to content

Commit c28f530

Browse files
committed
fix(sdk-core): skip auto-sign for message-sign approvals
postApprove() unconditionally ran the TSS recreate-and-sign flow for TRANSACTION_REQUEST_FULL approvals, which reads transactions[0].unsignedTx. Message-only txRequests have an empty transactions array, so this crashed. postApprove() now returns early when the approval's intent is a message-sign intent (signMessage / signTypedStructuredData), leaving the message to be signed separately via signAndSendMessageTxRequest. Also hardens the ecdsa signTxRequest assertions to check length > 0 instead of truthiness (an empty array is truthy), and types the transactionRequestFull info field on PendingApprovalInfo. Refs: DEFI-961 TICKET: DEFI-961
1 parent e29ac14 commit c28f530

4 files changed

Lines changed: 125 additions & 4 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,4 +706,75 @@ describe('Pending Approvals:', () => {
706706
});
707707
});
708708
});
709+
710+
describe('message-sign approvals (transactionRequestFull)', () => {
711+
const buildMessageSignPendingApprovalData = (intentType: string): PendingApprovalData => ({
712+
id: 'pa-msg',
713+
info: {
714+
type: Type.TRANSACTION_REQUEST_FULL,
715+
transactionRequestFull: {
716+
txRequestId: 'txreq-msg-1',
717+
intent: { intentType },
718+
},
719+
} as PendingApprovalInfo,
720+
state: State.PENDING,
721+
creator: 'test',
722+
txRequestId: 'txreq-msg-1',
723+
});
724+
725+
['signMessage', 'signTypedStructuredData'].forEach((intentType) => {
726+
it(`should approve without the post-approve auto-sign for a ${intentType} approval`, async () => {
727+
const ethBaseCoin = bitgo.coin('teth');
728+
const paData = buildMessageSignPendingApprovalData(intentType);
729+
const pendingApproval = new PendingApproval(bitgo, ethBaseCoin, paData, wallet);
730+
731+
const paScope = nock(bgUrl)
732+
.put(`/api/v2/teth/pendingapprovals/pa-msg`, { state: 'approved', otp: undefined })
733+
.reply(200, { ...paData, state: 'approved' });
734+
const recreateTransactionTssStub = sandbox.stub(PendingApproval.prototype, 'recreateAndSignTSSTransaction');
735+
736+
await pendingApproval.approve({ walletPassphrase: 'pass' });
737+
738+
recreateTransactionTssStub.notCalled.should.be.true();
739+
paScope.isDone().should.be.true();
740+
});
741+
});
742+
743+
it('should keep the post-approve auto-sign for transaction approvals', async () => {
744+
const ethBaseCoin = bitgo.coin('teth');
745+
const paData = buildMessageSignPendingApprovalData('payment');
746+
const pendingApproval = new PendingApproval(bitgo, ethBaseCoin, paData, wallet);
747+
748+
const paScope = nock(bgUrl)
749+
.put(`/api/v2/teth/pendingapprovals/pa-msg`, { state: 'approved', otp: undefined })
750+
.reply(200, { ...paData, state: 'approved' });
751+
const recreateTransactionTssStub = sandbox.stub(PendingApproval.prototype, 'recreateAndSignTSSTransaction');
752+
753+
await pendingApproval.approve({ walletPassphrase: 'pass' });
754+
755+
recreateTransactionTssStub.calledOnce.should.be.true();
756+
paScope.isDone().should.be.true();
757+
});
758+
759+
it('should detect a message-sign request from messages when intent is absent', async () => {
760+
const ethBaseCoin = bitgo.coin('teth');
761+
const paData = buildMessageSignPendingApprovalData('payment');
762+
const full = paData.info.transactionRequestFull;
763+
if (full) {
764+
delete full.intent;
765+
full.messages = [{ messageRaw: 'hello' }];
766+
}
767+
const pendingApproval = new PendingApproval(bitgo, ethBaseCoin, paData, wallet);
768+
769+
const paScope = nock(bgUrl)
770+
.put(`/api/v2/teth/pendingapprovals/pa-msg`, { state: 'approved', otp: undefined })
771+
.reply(200, { ...paData, state: 'approved' });
772+
const recreateTransactionTssStub = sandbox.stub(PendingApproval.prototype, 'recreateAndSignTSSTransaction');
773+
774+
await pendingApproval.approve({ walletPassphrase: 'pass' });
775+
776+
recreateTransactionTssStub.notCalled.should.be.true();
777+
paScope.isDone().should.be.true();
778+
});
779+
});
709780
});

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,24 @@ export interface PendingApprovalInfo {
4848
};
4949
sourceWallet?: string;
5050
};
51+
/**
52+
* Present on transactionRequestFull pending approvals. intent.intentType is
53+
* MESSAGE_SIGNING_INTENT_TYPES for message-sign approvals, which have no
54+
* unsigned transaction.
55+
*/
56+
transactionRequestFull?: {
57+
txRequestId?: string;
58+
intent?: {
59+
intentType?: string;
60+
[index: string]: unknown;
61+
};
62+
[index: string]: unknown;
63+
};
5164
}
5265

66+
/** Intent types whose txRequest carries a message to sign rather than a transaction. */
67+
export const MESSAGE_SIGNING_INTENT_TYPES: ReadonlySet<string> = new Set(['signMessage', 'signTypedStructuredData']);
68+
5369
export interface PendingApprovalData {
5470
id: string;
5571
wallet?: string;

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as _ from 'lodash';
55
import * as utxolib from '@bitgo/utxo-lib';
66
import type { AttestationPayload } from '@bitgo/public-types';
7+
import { MESSAGE_SIGNING_INTENT_TYPES } from './iPendingApproval';
78
import { IBaseCoin } from '../baseCoin';
89
import { BitGoBase } from '../bitgoBase';
910
import {
@@ -172,7 +173,10 @@ export class PendingApproval implements IPendingApproval {
172173
}
173174

174175
/**
175-
* Sets this PendingApproval to an approved state
176+
* Sets this PendingApproval to an approved state.
177+
* For transaction approvals on TSS wallets, this also attempts to recreate
178+
* and sign the transaction. For message-sign approvals, signing is deferred:
179+
* the message must be signed separately via signAndSendMessageTxRequest.
176180
*/
177181
async approve(params: ApproveOptions = {}): Promise<any> {
178182
params.previewPendingTxs = true;
@@ -489,12 +493,30 @@ export class PendingApproval implements IPendingApproval {
489493
reqId
490494
);
491495
} else if (this.canRecreateTransaction(params) && this.baseCoin.supportsTss()) {
496+
if (this.isMessageSigningRequest()) {
497+
return;
498+
}
492499
await this.recreateAndSignTSSTransaction(params, reqId);
493500
}
494501
}
495502
}
496503
}
497504

505+
/**
506+
* True when the approval's intent is a message-sign intent.
507+
*/
508+
private isMessageSigningRequest(): boolean {
509+
const info = this.info()?.transactionRequestFull;
510+
if (info?.intent?.intentType && MESSAGE_SIGNING_INTENT_TYPES.has(info.intent.intentType)) {
511+
return true;
512+
}
513+
// Fallback when intent is absent: a messages array means the request is message-only.
514+
if (info && 'messages' in info && Array.isArray(info.messages) && info.messages.length > 0) {
515+
return true;
516+
}
517+
return false;
518+
}
519+
498520
/**
499521
* Helper function to ensure that self.wallet is set
500522
*/

‎modules/sdk-core/src/bitgo/utils/tss/ecdsa/ecdsa.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,13 @@ export class EcdsaUtils extends BaseEcdsaUtils {
590590

591591
if (requestType === RequestType.tx) {
592592
assert(
593-
txRequest.transactions || (txRequest as TxRequest).unsignedTxs,
593+
(txRequest.transactions && txRequest.transactions.length > 0) ||
594+
(txRequest.unsignedTxs && txRequest.unsignedTxs.length > 0),
594595
'Unable to find transactions in txRequest'
595596
);
596597
const unsignedTx =
597598
txRequest.apiVersion === 'full' ? txRequest.transactions![0].unsignedTx : txRequest.unsignedTxs[0];
599+
assert(unsignedTx, 'Missing unsignedTx in txRequest');
598600
derivationPath = unsignedTx.derivationPath;
599601
} else if (requestType === RequestType.message) {
600602
// TODO BG-67299 Message signing with derivation path
@@ -641,9 +643,14 @@ export class EcdsaUtils extends BaseEcdsaUtils {
641643
const txRequest: TxRequest = tssParams.txRequest;
642644
let signablePayload;
643645
if (requestType === RequestType.tx) {
644-
assert(txRequest.transactions || txRequest.unsignedTxs, 'Unable to find transactions in txRequest');
646+
assert(
647+
(txRequest.transactions && txRequest.transactions.length > 0) ||
648+
(txRequest.unsignedTxs && txRequest.unsignedTxs.length > 0),
649+
'Unable to find transactions in txRequest'
650+
);
645651
const unsignedTx =
646652
txRequest.apiVersion === 'full' ? txRequest.transactions![0].unsignedTx : txRequest.unsignedTxs[0];
653+
assert(unsignedTx, 'Missing unsignedTx in txRequest');
647654
signablePayload = Buffer.from(unsignedTx.signableHex, 'hex');
648655
} else if (requestType === RequestType.message) {
649656
signablePayload = (params.tssParams as TSSParamsForMessage).bufferToSign;
@@ -799,9 +806,14 @@ export class EcdsaUtils extends BaseEcdsaUtils {
799806
let derivationPath = '';
800807

801808
if (requestType === RequestType.tx) {
802-
assert(txRequest.transactions || txRequest.unsignedTxs, 'Unable to find transactions in txRequest');
809+
assert(
810+
(txRequest.transactions && txRequest.transactions.length > 0) ||
811+
(txRequest.unsignedTxs && txRequest.unsignedTxs.length > 0),
812+
'Unable to find transactions in txRequest'
813+
);
803814
const unsignedTx =
804815
txRequest.apiVersion === 'full' ? txRequest.transactions![0].unsignedTx : txRequest.unsignedTxs[0];
816+
assert(unsignedTx, 'Missing unsignedTx in txRequest');
805817

806818
if (
807819
'recipientSource' in params &&

0 commit comments

Comments
 (0)