Skip to content

Commit 7024637

Browse files
fixup! fix(wallet): base wallet should now use the original CBOR if provided
1 parent 82abc3f commit 7024637

18 files changed

+200
-93
lines changed

packages/core/src/Serialization/Transaction.ts

+20
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ export class Transaction {
4949
this.#auxiliaryData = auxiliaryData;
5050
}
5151

52+
/**
53+
* Creates a transaction object from the given transaction body with an empty witness set and no
54+
* auxiliary data.
55+
*
56+
* @param body Data structure that contains all the elements of the transaction.
57+
*/
58+
static fromBody(body: TransactionBody): Transaction {
59+
return new Transaction(body, TransactionWitnessSet.fromCore({ signatures: new Map() }));
60+
}
61+
62+
/**
63+
* Creates a transaction object from the given transaction core body with an empty witness set and no
64+
* auxiliary data.
65+
*
66+
* @param body Data structure that contains all the elements of the transaction.
67+
*/
68+
static fromCoreBody(body: Cardano.TxBody): Transaction {
69+
return new Transaction(TransactionBody.fromCore(body), TransactionWitnessSet.fromCore({ signatures: new Map() }));
70+
}
71+
5272
/**
5373
* Serializes a transaction into CBOR format.
5474
*

packages/e2e/src/util/handle-util.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,19 @@ export const mint = async (
146146

147147
const unsignedTx = await wallet.initializeTx(txProps);
148148

149+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
150+
151+
const serializableTx = new Serialization.Transaction(
152+
Serialization.TransactionBody.fromCore(unsignedTx.body),
153+
Serialization.TransactionWitnessSet.fromCore(witness),
154+
Serialization.AuxiliaryData.fromCore(auxiliaryData)
155+
);
156+
149157
const finalizeProps: FinalizeTxProps = {
150-
auxiliaryData,
151158
signingOptions: {
152159
extraSigners: [policySigner]
153160
},
154-
tx: unsignedTx,
155-
witness: { scripts: [policyScript] }
161+
tx: serializableTx
156162
};
157163

158164
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/src/util/util.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ export const submitCertificate = async (certificate: Cardano.Certificate, wallet
213213
};
214214

215215
const unsignedTx = await wallet.wallet.initializeTx(txProps);
216-
const signedTx = await wallet.wallet.finalizeTx({ tx: unsignedTx });
216+
217+
const signedTx = await wallet.wallet.finalizeTx({
218+
tx: Serialization.Transaction.fromCoreBody(unsignedTx.body)
219+
});
217220

218221
await submitAndConfirm(wallet.wallet, signedTx);
219222

@@ -287,12 +290,18 @@ export const burnTokens = async ({
287290

288291
const unsignedTx = await wallet.initializeTx(txProps);
289292

293+
const witness = { redeemers: unsignedTx.redeemers, scripts, signatures: new Map() };
294+
295+
const serializableTx = new Serialization.Transaction(
296+
Serialization.TransactionBody.fromCore(unsignedTx.body),
297+
Serialization.TransactionWitnessSet.fromCore(witness)
298+
);
299+
290300
const finalizeProps: FinalizeTxProps = {
291301
signingOptions: {
292302
extraSigners
293303
},
294-
tx: unsignedTx,
295-
witness: { scripts }
304+
tx: serializableTx
296305
};
297306

298307
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/test/wallet_epoch_0/PersonalWallet/mint.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseWallet, FinalizeTxProps } from '@cardano-sdk/wallet';
2-
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
2+
import { Cardano, Serialization, nativeScriptPolicyId } from '@cardano-sdk/core';
33
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
44
import { KeyRole, util } from '@cardano-sdk/key-management';
55
import {
@@ -90,12 +90,16 @@ describe('PersonalWallet/mint', () => {
9090

9191
const unsignedTx = await wallet.initializeTx(txProps);
9292

93+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
94+
9395
const finalizeProps: FinalizeTxProps = {
9496
signingOptions: {
9597
extraSigners: [alicePolicySigner]
9698
},
97-
tx: unsignedTx,
98-
witness: { scripts: [policyScript] }
99+
tx: new Serialization.Transaction(
100+
Serialization.TransactionBody.fromCore(unsignedTx.body),
101+
Serialization.TransactionWitnessSet.fromCore(witness)
102+
)
99103
};
100104

101105
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/test/wallet_epoch_0/PersonalWallet/multisignature.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
22
import { BaseWallet, FinalizeTxProps } from '@cardano-sdk/wallet';
3-
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
3+
import { Cardano, Serialization, nativeScriptPolicyId } from '@cardano-sdk/core';
44
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
55
import { KeyRole, util } from '@cardano-sdk/key-management';
66
import {
@@ -117,12 +117,16 @@ describe('PersonalWallet/multisignature', () => {
117117

118118
const unsignedTx = await wallet.initializeTx(txProps);
119119

120+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
121+
120122
const finalizeProps: FinalizeTxProps = {
121123
signingOptions: {
122124
extraSigners: [alicePolicySigner, bobPolicySigner]
123125
},
124-
tx: unsignedTx,
125-
witness: { scripts: [policyScript] }
126+
tx: new Serialization.Transaction(
127+
Serialization.TransactionBody.fromCore(unsignedTx.body),
128+
Serialization.TransactionWitnessSet.fromCore(witness)
129+
)
126130
};
127131

128132
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/test/wallet_epoch_0/PersonalWallet/nft.test.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable sonarjs/no-duplicate-string */
2-
import { Asset, Cardano, metadatum, nativeScriptPolicyId } from '@cardano-sdk/core';
2+
import { Asset, Cardano, Serialization, metadatum, nativeScriptPolicyId } from '@cardano-sdk/core';
33
import { Assets, BaseWallet, FinalizeTxProps } from '@cardano-sdk/wallet';
44
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
55
import { KeyRole, TransactionSigner, util } from '@cardano-sdk/key-management';
@@ -168,13 +168,17 @@ describe('PersonalWallet.assets/nft', () => {
168168

169169
const unsignedTx = await wallet.initializeTx(txProps);
170170

171+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
172+
171173
const finalizeProps: FinalizeTxProps = {
172-
auxiliaryData,
173174
signingOptions: {
174175
extraSigners: [policySigner]
175176
},
176-
tx: unsignedTx,
177-
witness: { scripts: [policyScript] }
177+
tx: new Serialization.Transaction(
178+
Serialization.TransactionBody.fromCore(unsignedTx.body),
179+
Serialization.TransactionWitnessSet.fromCore(witness),
180+
Serialization.AuxiliaryData.fromCore(auxiliaryData)
181+
)
178182
};
179183

180184
const signedTx = await wallet.finalizeTx(finalizeProps);
@@ -284,12 +288,16 @@ describe('PersonalWallet.assets/nft', () => {
284288

285289
const unsignedTx = await wallet.initializeTx(txProps);
286290

291+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
292+
287293
const finalizeProps: FinalizeTxProps = {
288294
signingOptions: {
289295
extraSigners: [policySigner]
290296
},
291-
tx: unsignedTx,
292-
witness: { scripts: [policyScript] }
297+
tx: new Serialization.Transaction(
298+
Serialization.TransactionBody.fromCore(unsignedTx.body),
299+
Serialization.TransactionWitnessSet.fromCore(witness)
300+
)
293301
};
294302

295303
const signedTx = await wallet.finalizeTx(finalizeProps);
@@ -354,13 +362,17 @@ describe('PersonalWallet.assets/nft', () => {
354362

355363
const unsignedTx = await wallet.initializeTx(txProps);
356364

365+
const witness = { redeemers: unsignedTx.redeemers, scripts: [policyScript], signatures: new Map() };
366+
357367
const finalizeProps: FinalizeTxProps = {
358-
auxiliaryData,
359368
signingOptions: {
360369
extraSigners: [policySigner]
361370
},
362-
tx: unsignedTx,
363-
witness: { scripts: [policyScript] }
371+
tx: new Serialization.Transaction(
372+
Serialization.TransactionBody.fromCore(unsignedTx.body),
373+
Serialization.TransactionWitnessSet.fromCore(witness),
374+
Serialization.AuxiliaryData.fromCore(auxiliaryData)
375+
)
364376
};
365377

366378
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/test/wallet_epoch_0/PersonalWallet/phase2validation.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ describe('PersonalWallet/phase2validation', () => {
140140
};
141141

142142
const unsignedTx = await wallet.initializeTx(txProps);
143+
144+
const witness = { redeemers: [scriptRedeemer], scripts: [alwaysFailScript], signatures: new Map() };
145+
143146
const finalizeProps: FinalizeTxProps = {
144147
isValid: false,
145-
tx: unsignedTx,
146-
witness: { redeemers: [scriptRedeemer], scripts: [alwaysFailScript] }
148+
tx: new Serialization.Transaction(
149+
Serialization.TransactionBody.fromCore(unsignedTx.body),
150+
Serialization.TransactionWitnessSet.fromCore(witness)
151+
)
147152
};
148153

149154
const signedTx = await wallet.finalizeTx(finalizeProps);

packages/e2e/test/wallet_epoch_0/PersonalWallet/txChaining.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ObservableWallet } from '@cardano-sdk/wallet';
2+
import { Serialization } from '@cardano-sdk/core';
23
import { firstValueFrom } from 'rxjs';
34
import { getEnv, getWallet, submitAndConfirm, walletReady, walletVariables } from '../../../src';
45
import { logger } from '@cardano-sdk/util-dev';
@@ -29,7 +30,10 @@ describe('PersonalWallet/txChaining', () => {
2930
outputs: new Set([{ address, value: { coins: moreThanHalfOfTheBalanceCoins } }])
3031
});
3132

32-
const finalizedTx1 = await wallet.finalizeTx({ tx: tx1 });
33+
const finalizedTx1 = await wallet.finalizeTx({
34+
tx: Serialization.Transaction.fromCoreBody(tx1.body)
35+
});
36+
3337
await wallet.submitTx(finalizedTx1);
3438

3539
const tx2 = await wallet.initializeTx({
@@ -40,7 +44,9 @@ describe('PersonalWallet/txChaining', () => {
4044
const usingTx1OutputAsInput = [...tx2.inputSelection.inputs].some(([txIn]) => txIn.txId === finalizedTx1.id);
4145
expect(usingTx1OutputAsInput).toBe(true);
4246

43-
const finalizedTx2 = await wallet.finalizeTx({ tx: tx2 });
47+
const finalizedTx2 = await wallet.finalizeTx({
48+
tx: Serialization.Transaction.fromCoreBody(tx2.body)
49+
});
4450

4551
// 1st tx must also be on-chain because the 2nd one uses output from the 1st one
4652
await submitAndConfirm(wallet, finalizedTx2);

packages/wallet/src/Wallets/BaseWallet.ts

+5-25
Original file line numberDiff line numberDiff line change
@@ -586,38 +586,20 @@ export class BaseWallet implements ObservableWallet {
586586
return initializeTx(props, this.getTxBuilderDependencies());
587587
}
588588

589-
async finalizeTx({
590-
tx,
591-
bodyCbor,
592-
signingOptions,
593-
signingContext,
594-
auxiliaryData,
595-
isValid,
596-
witness
597-
}: FinalizeTxProps): Promise<Cardano.Tx> {
589+
async finalizeTx({ tx, signingOptions, signingContext, isValid }: FinalizeTxProps): Promise<Cardano.Tx> {
598590
const knownAddresses = await firstValueFrom(this.addresses$);
599591
const dRepPublicKey = await this.governance.getPubDRepKey();
600592

601593
const context = {
602594
...signingContext,
603595
dRepPublicKey,
604596
knownAddresses,
605-
txInKeyPathMap: await util.createTxInKeyPathMap(tx.body, knownAddresses, this.util)
597+
txInKeyPathMap: await util.createTxInKeyPathMap(tx.body().toCore(), knownAddresses, this.util)
606598
};
607599

608-
const emptyWitness = { signatures: new Map() };
600+
if (isValid !== undefined) tx.setIsValid(isValid);
609601

610-
// The Witnesser takes a serializable transaction. We cant build that from the hash alone, if
611-
// the bodyCbor is available, use that instead of the coreTx type to build the transaction.
612-
const transaction = new Serialization.Transaction(
613-
bodyCbor ? Serialization.TransactionBody.fromCbor(bodyCbor) : Serialization.TransactionBody.fromCore(tx.body),
614-
Serialization.TransactionWitnessSet.fromCore({ ...emptyWitness, ...witness }),
615-
auxiliaryData ? Serialization.AuxiliaryData.fromCore(auxiliaryData) : undefined
616-
);
617-
618-
if (isValid !== undefined) transaction.setIsValid(isValid);
619-
620-
const result = await this.witnesser.witness(transaction, context, signingOptions);
602+
const result = await this.witnesser.witness(tx, context, signingOptions);
621603

622604
this.#newTransactions.signed$.next(result);
623605

@@ -827,12 +809,10 @@ export class BaseWallet implements ObservableWallet {
827809
/** Update the witness of a transaction with witness provided by this wallet */
828810
async updateWitness({ tx, sender }: UpdateWitnessProps): Promise<Cardano.Tx> {
829811
return this.finalizeTx({
830-
auxiliaryData: tx.auxiliaryData,
831812
signingContext: {
832813
sender
833814
},
834-
tx: { body: tx.body, hash: tx.id },
835-
witness: tx.witness
815+
tx: Serialization.Transaction.fromCore(tx)
836816
});
837817
}
838818
}

packages/wallet/src/cip30.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ const baseCip30WalletApi = (
456456
logger.debug('signTx');
457457
const txDecoded = Serialization.Transaction.fromCbor(TxCBOR(tx));
458458

459-
const hash = txDecoded.getId();
460459
const coreTx = txDecoded.toCore();
461460
const shouldProceed = await confirmationCallback
462461
.signTx({
@@ -479,9 +478,8 @@ const baseCip30WalletApi = (
479478
const {
480479
witness: { signatures }
481480
} = await wallet.finalizeTx({
482-
bodyCbor: txDecoded.body().toCbor(),
483481
signingContext: { sender },
484-
tx: { ...coreTx, hash }
482+
tx: txDecoded
485483
});
486484

487485
// If partialSign is true, the wallet only tries to sign what it can. However, if

packages/wallet/src/types.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import {
55
EraSummary,
66
HandleResolution,
77
NetworkInfoProvider,
8+
Serialization,
89
TxCBOR
910
} from '@cardano-sdk/core';
1011
import { BalanceTracker, DelegationTracker, TransactionsTracker, UtxoTracker } from './services';
1112
import { Cip30DataSignature } from '@cardano-sdk/dapp-connector';
1213
import { Ed25519PublicKeyHex } from '@cardano-sdk/crypto';
1314
import { GroupedAddress, MessageSender, SignTransactionContext, WitnessedTx, cip8 } from '@cardano-sdk/key-management';
14-
import { HexBlob, Shutdown } from '@cardano-sdk/util';
1515
import { InitializeTxProps, InitializeTxResult, TxBuilder, TxContext } from '@cardano-sdk/tx-construction';
1616
import { Observable } from 'rxjs';
1717
import { PubStakeKeyAndStatus } from './services/PublicStakeKeysTracker';
18+
import { Shutdown } from '@cardano-sdk/util';
1819

1920
export type Assets = Map<Cardano.AssetId, Asset.AssetInfo>;
2021

@@ -43,11 +44,11 @@ export interface SyncStatus extends Shutdown {
4344
isSettled$: Observable<boolean>;
4445
}
4546

46-
export type FinalizeTxProps = Omit<TxContext, 'signingContext'> & {
47-
tx: Cardano.TxBodyWithHash;
48-
bodyCbor?: HexBlob;
49-
signingContext?: Partial<SignTransactionContext>;
50-
};
47+
export type FinalizeTxProps = Pick<TxContext, 'signingOptions'> &
48+
Pick<TxContext, 'isValid'> & {
49+
tx: Serialization.Transaction;
50+
signingContext?: Partial<SignTransactionContext>;
51+
};
5152

5253
export type UpdateWitnessProps = {
5354
tx: Cardano.Tx;

packages/wallet/test/PersonalWallet/load.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
HandleProvider,
2626
NetworkInfoProvider,
2727
RewardsProvider,
28+
Serialization,
2829
UtxoProvider,
2930
coalesceValueQuantities
3031
} from '@cardano-sdk/core';
@@ -470,7 +471,7 @@ describe('BaseWallet creates big UTXO', () => {
470471
const unsignedTx = await wallet.initializeTx(txProps);
471472

472473
const finalizeProps = {
473-
tx: unsignedTx
474+
tx: Serialization.Transaction.fromCoreBody(unsignedTx.body)
474475
};
475476

476477
const signedTx = await wallet.finalizeTx(finalizeProps);

0 commit comments

Comments
 (0)