Skip to content

Commit bdfd091

Browse files
fix(wallet): cip30 signTx function now throws if not enough signatures before calling confirmationCallback
1 parent 7314269 commit bdfd091

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

packages/wallet/src/cip30.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,19 @@ const baseCip30WalletApi = (
458458
const scope = new ManagedFreeableScope();
459459
logger.debug('signTx');
460460
const txDecoded = Serialization.Transaction.fromCbor(Serialization.TxCBOR(tx));
461-
461+
const wallet = await firstValueFrom(wallet$);
462462
const hash = txDecoded.getId();
463463
const coreTx = txDecoded.toCore();
464+
465+
const needsForeignSignature = await requiresForeignSignatures(coreTx, wallet);
466+
467+
// If partialSign is false and the wallet could not sign the entire transaction
468+
if (!partialSign && needsForeignSignature)
469+
throw new DataSignError(
470+
DataSignErrorCode.ProofGeneration,
471+
'The wallet does not have the secret key associated with some of the inputs or certificates.'
472+
);
473+
464474
const shouldProceed = await confirmationCallback
465475
.signTx({
466476
data: coreTx,
@@ -469,16 +479,7 @@ const baseCip30WalletApi = (
469479
})
470480
.catch((error) => mapCallbackFailure(error, logger));
471481
if (shouldProceed) {
472-
const wallet = await firstValueFrom(wallet$);
473482
try {
474-
const needsForeignSignature = await requiresForeignSignatures(coreTx, wallet);
475-
476-
// If partialSign is false and the wallet could not sign the entire transaction
477-
if (!partialSign && needsForeignSignature)
478-
throw new DataSignError(
479-
DataSignErrorCode.ProofGeneration,
480-
'The wallet does not have the secret key associated with some of the inputs or certificates.'
481-
);
482483
const {
483484
witness: { signatures }
484485
} = await wallet.finalizeTx({

packages/wallet/test/integration/cip30mapping.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const {
5050
type TestProviders = Required<Pick<Providers, 'txSubmitProvider' | 'networkInfoProvider'>>;
5151
const mockCollateralCallback = jest.fn().mockResolvedValue([mockUtxo[3]]);
5252
const createMockGenericCallback = () => jest.fn().mockResolvedValue(true);
53+
const foreignTx = Serialization.TxCBOR(
54+
'84a70081825820dce442e983f3f5cd5b2644bc57f749075390f1fbae9ab55bf454342959c885db00018182583900d161d64eef0eeb59f9124f520f8c8f3b717ed04198d54c8b17e604aea63c153fb3ea8a4ea4f165574ea91173756de0bf30222ca0e95a649a1a0082607b021a0016360509a1581cb77934706fa311b6568d1070c2d23f092324b35ad623aa571a0e3726a14e4d6573685f476966745f43617264200b5820d8175f3b1276a48939a6ccee220a7f81b6422167317ba3ff6325cba1fb6ccbe70d818258208d68748457cd0f1a8596f41fd2125a415315897d2da4a4b94335829cee7198ae001281825820dce442e983f3f5cd5b2644bc57f749075390f1fbae9ab55bf454342959c885db00a2068259016b590168010000333232323232323223223222253330083232533300d3010002132533300b3370e6eb4c034009200113371e0020122940dd718058008b180700099299980499b8748008c028dd50008a5eb7bdb1804dd5980718059baa001323300100132330010013756601e602060206020602060186ea8c03cc030dd50019129998070008a5eb7bdb1804c8c8c8c94ccc03ccdc8a45000021533300f3371e91010000210031005133013337606ea4008dd3000998030030019bab3010003375c601c0046024004602000244a66601a002298103d87a8000132323232533300e337220140042a66601c66e3c0280084cdd2a4000660246e980052f5c02980103d87a80001330060060033756601e0066eb8c034008c044008c03c00452613656375c0026eb80055cd2ab9d5573caae7d5d02ba157449810f4e4d6573685f476966745f43617264004c011e581cb77934706fa311b6568d1070c2d23f092324b35ad623aa571a0e3726000159023c59023901000033323232323232322322232323225333009323232533300c3007300d3754002264646464a666026602c00426464a666024601a60266ea803854ccc048c034c04cdd5191980080080311299980b8008a60103d87a80001323253330163375e603660306ea800804c4cdd2a40006603400497ae0133004004001301b002301900115333012300c00113371e00402029405854ccc048cdc3800a4002266e3c0080405281bad3013002375c60220022c602800264a66601e601260206ea800452f5bded8c026eacc050c044dd500099191980080099198008009bab3016301730173017301700522533301500114bd6f7b630099191919299980b19b91488100002153330163371e9101000021003100513301a337606ea4008dd3000998030030019bab3017003375c602a0046032004602e00244a666028002298103d87a800013232323253330153372200e0042a66602a66e3c01c0084cdd2a4000660326e980052f5c02980103d87a80001330060060033756602c0066eb8c050008c060008c058004dd7180998081baa00337586024002601c6ea800858c040c044008c03c004c02cdd50008a4c26cac64a66601060060022a66601660146ea8010526161533300830020011533300b300a37540082930b0b18041baa003370e90011b8748000dd7000ab9a5573aaae7955cfaba05742ae8930010f4e4d6573685f476966745f43617264004c012bd8799fd8799f58203159a6f2ae24c5bfbed947fe0ecfe936f088c8d265484e6979cacb607d33c811ff05ff0001058284000040821a006acfc01ab2d05e00840100d87a80821a006acfc01ab2d05e00f5f6'
55+
);
5356

5457
const createWalletAndApiWithStores = async (
5558
unspendableUtxos: Cardano.Utxo[],
@@ -503,7 +506,8 @@ describe('cip30', () => {
503506
});
504507

505508
it('passes through sender from dapp connector context', async () => {
506-
const finalizeTxSpy = jest.spyOn(wallet, 'finalizeTx');
509+
const finalizeTxSpy = jest.spyOn(wallet, 'finalizeTx').mockClear();
510+
507511
await api.signTx(context, hexTx);
508512
expect(finalizeTxSpy).toBeCalledWith(
509513
expect.objectContaining({
@@ -516,6 +520,15 @@ describe('cip30', () => {
516520
);
517521
expect(confirmationCallback.signTx).toBeCalledWith(expect.objectContaining({ sender: context.sender }));
518522
});
523+
524+
it('doesnt invoke confirmationCallback.signTx if an error occurs', async () => {
525+
const finalizeTxSpy = jest.spyOn(wallet, 'finalizeTx').mockClear();
526+
confirmationCallback.signTx = jest.fn().mockResolvedValueOnce(true).mockClear();
527+
528+
await expect(api.signTx(context, foreignTx, false)).rejects.toThrowError();
529+
expect(finalizeTxSpy).not.toHaveBeenCalled();
530+
expect(confirmationCallback.signTx).not.toHaveBeenCalled();
531+
});
519532
});
520533

521534
describe('api.signData', () => {

0 commit comments

Comments
 (0)