Skip to content

Commit 28d0e67

Browse files
committed
feat(wallet): add support for building tx with metadata
1 parent 8686610 commit 28d0e67

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

packages/wallet/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"lint:fix": "eslint --fix --ignore-path ../../.eslintignore \"**/*.ts\"",
2323
"prepack": "yarn build",
2424
"test": "jest -c ./jest.config.js",
25-
"test:e2e": "jest -c ./e2e.jest.config.js",
25+
"test:e2e": "jest -c ./e2e.jest.config.js --runInBand",
2626
"test:debug": "DEBUG=true yarn test"
2727
},
2828
"devDependencies": {

packages/wallet/src/SingleAddressWallet.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class SingleAddressWallet implements Wallet {
194194
utxo: new Set(utxo)
195195
});
196196
const { body, hash } = await createTransactionInternals({
197+
auxiliaryData: props.auxiliaryData,
197198
certificates: props.certificates,
198199
changeAddress,
199200
inputSelection,
@@ -256,14 +257,14 @@ export class SingleAddressWallet implements Wallet {
256257
buildTx: async (inputSelection) => {
257258
this.#logger.debug('Building TX for selection constraints', inputSelection);
258259
const txInternals = await createTransactionInternals({
260+
auxiliaryData: props.auxiliaryData,
259261
certificates: props.certificates,
260262
changeAddress,
261263
inputSelection,
262264
validityInterval,
263265
withdrawals: props.withdrawals
264266
});
265-
// TODO: add auxiliaryData support
266-
return coreToCsl.tx(await this.finalizeTx(txInternals, undefined, true));
267+
return coreToCsl.tx(await this.finalizeTx(txInternals, props.auxiliaryData, true));
267268
},
268269
protocolParameters
269270
});

packages/wallet/src/Transaction/createTransactionInternals.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ export type CreateTxInternalsProps = {
1212
validityInterval: Cardano.ValidityInterval;
1313
certificates?: Cardano.Certificate[];
1414
withdrawals?: Cardano.Withdrawal[];
15+
auxiliaryData?: Cardano.AuxiliaryData;
1516
};
1617

1718
export const createTransactionInternals = async ({
19+
auxiliaryData,
1820
changeAddress,
1921
withdrawals,
2022
certificates,
@@ -38,8 +40,10 @@ export const createTransactionInternals = async ({
3840
validityInterval,
3941
withdrawals
4042
};
43+
const cslBody = coreToCsl.txBody(body, auxiliaryData);
44+
4145
return {
4246
body,
43-
hash: Cardano.TransactionId(Buffer.from(CSL.hash_transaction(coreToCsl.txBody(body)).to_bytes()).toString('hex'))
47+
hash: Cardano.TransactionId(Buffer.from(CSL.hash_transaction(cslBody).to_bytes()).toString('hex'))
4448
};
4549
};

packages/wallet/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type InitializeTxProps = {
88
outputs?: Set<Cardano.TxOut>;
99
certificates?: Cardano.Certificate[];
1010
withdrawals?: Cardano.Withdrawal[];
11+
auxiliaryData?: Cardano.AuxiliaryData;
1112
options?: {
1213
validityInterval?: Cardano.ValidityInterval;
1314
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Cardano, util } from '@cardano-sdk/core';
2+
import { SingleAddressWallet } from '../../../src';
3+
import { assetProvider, keyAgentReady, stakePoolSearchProvider, timeSettingsProvider, walletProvider } from '../config';
4+
import { filter, firstValueFrom, map } from 'rxjs';
5+
6+
describe('SingleAddressWallet/metadata', () => {
7+
let wallet: SingleAddressWallet;
8+
let ownAddress: Cardano.Address;
9+
10+
beforeAll(async () => {
11+
wallet = new SingleAddressWallet(
12+
{ name: 'Test Wallet' },
13+
{
14+
assetProvider,
15+
keyAgent: await keyAgentReady,
16+
stakePoolSearchProvider,
17+
timeSettingsProvider,
18+
walletProvider
19+
}
20+
);
21+
ownAddress = (await firstValueFrom(wallet.addresses$))[0].address;
22+
});
23+
24+
afterAll(() => wallet.shutdown());
25+
26+
test('can submit tx with metadata and then query it', async () => {
27+
const auxiliaryData: Cardano.AuxiliaryData = {
28+
body: {
29+
blob: new Map([[123n, '1234']])
30+
}
31+
};
32+
const txInternals = await wallet.initializeTx({
33+
auxiliaryData,
34+
outputs: new Set([{ address: ownAddress, value: { coins: 1_000_000n } }])
35+
});
36+
const outgoingTx = await wallet.finalizeTx(txInternals, auxiliaryData);
37+
await wallet.submitTx(outgoingTx);
38+
const loadedTx = await firstValueFrom(
39+
wallet.transactions.history.outgoing$.pipe(
40+
map((txs) => txs.find((tx) => tx.id === outgoingTx.id)),
41+
filter(util.isNotNil)
42+
)
43+
);
44+
expect(loadedTx.auxiliaryData).toEqual(auxiliaryData);
45+
});
46+
});

0 commit comments

Comments
 (0)