Skip to content

Commit

Permalink
feat: cip30 signTx should send CBOR in sign transactionWitnesserReq
Browse files Browse the repository at this point in the history
Instead of the only the transaction body.
The witnesser request was missing the auxiliaryData and witness fields.
Plus, it's more reliable to use the CBOR instead of reconstructing it.
  • Loading branch information
mirceahasegan committed Oct 6, 2024
1 parent ed57710 commit c26c773
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions packages/wallet/test/PersonalWallet/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ describe('BaseWallet methods', () => {
});

describe('finalizeTx', () => {
let witnessSpy: jest.SpyInstance;

beforeEach(() => {
witnessSpy = jest.spyOn(witnesser, 'witness');
});

afterEach(() => {
witnessSpy.mockClear();
witnessSpy.mockReset();
witnessSpy.mockRestore();
});

it('resolves with TransactionWitnessSet', async () => {
const txInternals = await wallet.initializeTx(props);
const unhydratedTxBody = Serialization.TransactionBody.fromCore(txInternals.body).toCore();
Expand All @@ -241,24 +253,50 @@ describe('BaseWallet methods', () => {

it('passes through sender to witnesser', async () => {
const sender = { url: 'https://lace.io' };
const witnessSpy = jest.spyOn(witnesser, 'witness');
const txInternals = await wallet.initializeTx(props);
await wallet.finalizeTx({ signingContext: { sender }, tx: txInternals });

// Reset witness calls from wallet.initializeTx
witnessSpy.mockClear();

await wallet.finalizeTx({ signingContext: { sender }, tx: txInternals });
expect(witnessSpy).toHaveBeenCalledTimes(1);
expect(witnessSpy).toBeCalledWith(expect.anything(), expect.objectContaining({ sender }), void 0);
});

it('uses the original CBOR to create the serializable transaction if given', async () => {
const sender = { url: 'https://lace.io' };
const witnessSpy = jest.spyOn(witnesser, 'witness');
const txInternals = await wallet.initializeTx(props);

// Reset witness calls from wallet.initializeTx
witnessSpy.mockClear();

await wallet.finalizeTx({
auxiliaryData: geniusYieldTx.auxiliaryData()?.toCore(),
bodyCbor: geniusYieldTx.body().toCbor(),
signingContext: { sender },
tx: txInternals
tx: txInternals,
witness: geniusYieldTx.witnessSet()?.toCore()
});

expect(witnessSpy).toHaveBeenCalledTimes(1);
const tx: Serialization.Transaction = witnessSpy.mock.calls[0][0];
expect(tx.body().toCbor()).toEqual(geniusYieldTx.body().toCbor());
// The transaction CBOR will not match due to reencoding witnessSet and auxiliaryData
// expect(tx.toCbor()).toEqual(geniusYieldTx.toCbor());
});

it('uses the complete transaction CBOR, ignoring auxiliaryData, witness and isValid', async () => {
const sender = { url: 'https://lace.io' };
await wallet.finalizeTx({
auxiliaryData: 'ignored auxiliary data' as Cardano.AuxiliaryData,
signingContext: { sender },
tx: geniusYieldTx.toCbor(),
witness: 'ignored witness set' as Partial<Cardano.Witness>
});

expect(witnessSpy).toBeCalledWith(geniusYieldTx, expect.objectContaining({ sender }), void 0);
expect(witnessSpy).toHaveBeenCalledTimes(1);
const tx: Serialization.Transaction = witnessSpy.mock.calls[0][0];
expect(tx.toCbor()).toEqual(geniusYieldTx.toCbor());
});
});

Expand Down

0 comments on commit c26c773

Please sign in to comment.