This repository was archived by the owner on Jul 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmultitoken.test.ts
More file actions
369 lines (306 loc) · 15.8 KB
/
Copy pathmultitoken.test.ts
File metadata and controls
369 lines (306 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { Fr } from '@aztec/aztec.js/fields';
import { AztecAddress } from '@aztec/aztec.js/addresses';
import { type EmbeddedWallet } from '@aztec/wallets/embedded';
import { SetPublicAuthwitContractInteraction, lookupValidity } from '@aztec/aztec.js/authorization';
import { type ContractFunctionInteractionCallIntent } from '@aztec/aztec.js/authorization';
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest';
import {
setupTestSuite,
AMOUNT,
PRIVATE_ADDRESS,
MULTITOKEN_NAME,
MULTITOKEN_SYMBOL,
ID_A,
fieldFromShortString,
compressedStringToBigInt,
deployMultiTokenWithMinter,
initializeMultiTokenTransferCommitment,
expectMultiTokenTransferEvents,
} from './utils.js';
import { MultiTokenContract } from '../../../src/artifacts/MultiToken.js';
const TEST_TIMEOUT = 300_000;
// Scope of this suite: ONE end-to-end happy-path per main (state-mutating) function, exercised through
// the real client stack (deploy -> call -> read via SDK, with real authwit + additionalScopes), asserting
// the resulting balances and the decoded event. It answers "does each function work end-to-end?".
// Correctness details — reverts, overflow/underflow, per-id isolation, id=0, note lifecycle, the ARC-403
// authorization hook, boundaries — are covered by the Noir/TXE suite (src/multitoken_contract/src/test/*).
describe('MultiToken', () => {
let cleanup: () => Promise<void>;
let wallet: EmbeddedWallet;
let accounts: AztecAddress[];
let alice: AztecAddress;
let bob: AztecAddress;
let carl: AztecAddress;
let multitoken: MultiTokenContract;
beforeAll(async () => {
({ cleanup, wallet, accounts } = await setupTestSuite());
[alice, bob, carl] = accounts;
});
beforeEach(async () => {
// Default deploy: minter = alice, auth_contract = ZERO (ARC-403 hook disabled). The hook's behavior
// is covered exhaustively by the Noir suite (authorization.nr); here every path runs with it off.
multitoken = await deployMultiTokenWithMinter(wallet, alice, alice, AztecAddress.ZERO);
});
afterAll(async () => {
await cleanup();
});
// --- deploy / views ---
it(
'deploys with a minter and reads back name/symbol/minter/auth_contract',
async () => {
const nameField = fieldFromShortString(MULTITOKEN_NAME);
const symbolField = fieldFromShortString(MULTITOKEN_SYMBOL);
const mt = await deployMultiTokenWithMinter(wallet, alice, alice, AztecAddress.ZERO);
const nameResult = (await mt.methods.name().simulate({ from: alice })).result;
const symbolResult = (await mt.methods.symbol().simulate({ from: alice })).result;
expect(compressedStringToBigInt(nameResult)).toBe(nameField.toBigInt());
expect(compressedStringToBigInt(symbolResult)).toBe(symbolField.toBigInt());
const minter = (await mt.methods.get_minter().simulate({ from: alice })).result;
expect(minter.equals(alice)).toBe(true);
const authContract = (await mt.methods.get_auth_contract().simulate({ from: alice })).result;
expect(authContract.equals(AztecAddress.ZERO)).toBe(true);
},
TEST_TIMEOUT,
);
// --- mint_to_private ---
it(
'mints to a private recipient balance and leaks nothing publicly',
async () => {
const id = ID_A;
const { receipt: mintTx } = await multitoken.methods.mint_to_private(bob, id, AMOUNT).send({ from: alice });
// mint_to_private: (no public events)
await expectMultiTokenTransferEvents(mintTx.txHash, multitoken.address, []);
// recipient sees its private balance; no public balance was written.
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(AMOUNT);
expect((await multitoken.methods.balance_of_public(bob, id).simulate({ from: bob })).result).toBe(0n);
// an uninvolved third party never observes another account's private notes.
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: carl })).result).toBe(0n);
},
TEST_TIMEOUT,
);
// --- mint_to_public ---
it(
'mints to public and emits a 4-field TransferSingle event',
async () => {
const id = ID_A;
const { receipt: mintTx } = await multitoken.methods.mint_to_public(alice, id, AMOUNT).send({ from: alice });
// mint_to_public: TransferSingle(0x0, alice, id, AMOUNT)
await expectMultiTokenTransferEvents(mintTx.txHash, multitoken.address, [
{ from: AztecAddress.ZERO, to: alice, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: alice })).result).toBe(AMOUNT);
// No private note written for the public mint.
expect((await multitoken.methods.balance_of_private(alice, id).simulate({ from: alice })).result).toBe(0n);
},
TEST_TIMEOUT,
);
// --- mint_to_commitment ---
it(
'mints to a recipient-prepared commitment and credits the recipient privately',
async () => {
const id = ID_A;
// Recipient (bob) prepares the commitment for himself in his own settled tx, bound to the minter (alice) as completer.
const commitment = await initializeMultiTokenTransferCommitment(multitoken, bob, bob, alice);
// Minter (alice = msg_sender) completes the commitment by minting into it.
const { receipt: mintTx } = await multitoken.methods
.mint_to_commitment(id, commitment, AMOUNT)
.send({ from: alice });
// mint_to_commitment: TransferSingle(0x0, PRIVATE, id, AMOUNT)
await expectMultiTokenTransferEvents(mintTx.txHash, multitoken.address, [
{ from: AztecAddress.ZERO, to: PRIVATE_ADDRESS, id, amount: AMOUNT },
]);
// bob holds the tokens privately for this id.
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- transfer_public_to_public (happy-path carries the PUBLIC authwit demo) ---
it(
'transfers public to public with a public authwit',
async () => {
const id = ID_A;
const { receipt: mintTx } = await multitoken
.withWallet(wallet)
.methods.mint_to_public(alice, id, AMOUNT)
.send({ from: alice });
// mint_to_public: TransferSingle(0x0, alice, id, AMOUNT)
await expectMultiTokenTransferEvents(mintTx.txHash, multitoken.address, [
{ from: AztecAddress.ZERO, to: alice, id, amount: AMOUNT },
]);
// Build the transfer; carl will submit it under alice's public authwit.
const nonce = Fr.random();
const action = multitoken.withWallet(wallet).methods.transfer_public_to_public(alice, bob, id, AMOUNT, nonce);
const intent: ContractFunctionInteractionCallIntent = {
caller: carl,
action,
};
const authWitness = await wallet.createAuthWit(alice, {
caller: intent.caller,
call: await intent.action.getFunctionCall(),
});
const setPublicAuthwitInteraction = await SetPublicAuthwitContractInteraction.create(wallet, alice, intent, true);
await setPublicAuthwitInteraction.send();
const validity = await lookupValidity(wallet, alice, intent, authWitness);
expect(validity.isValidInPrivate).toBeTruthy();
expect(validity.isValidInPublic).toBeTruthy();
const { receipt: transferTx } = await action.send({ from: carl, authWitnesses: [authWitness] });
// transfer_public_to_public: TransferSingle(alice, bob, id, AMOUNT)
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, [
{ from: alice, to: bob, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: carl })).result).toBe(0n);
expect((await multitoken.methods.balance_of_public(bob, id).simulate({ from: carl })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- transfer_public_to_private (happy-path carries the PRIVATE authwit + additionalScopes demo) ---
it(
'transfers public to private under a third-party private authwit with additionalScopes',
async () => {
const id = ID_A;
await multitoken.methods.mint_to_public(alice, id, AMOUNT).send({ from: alice });
// Third party (bob) submits alice -> carl spending alice's PUBLIC balance; gate is a private authwit by alice.
const action = multitoken.methods.transfer_public_to_private(alice, carl, id, AMOUNT, 1n);
const intent: ContractFunctionInteractionCallIntent = {
caller: bob,
action,
};
const witness = await wallet.createAuthWit(alice, {
caller: intent.caller,
call: await intent.action.getFunctionCall(),
});
// additionalScopes includes alice so bob's PXE can read alice's account-contract notes
// (signing key) needed for private authwit verification.
const { receipt: transferTx } = await action.send({
from: bob,
authWitnesses: [witness],
additionalScopes: [alice],
});
// transfer_public_to_private: TransferSingle(alice, PRIVATE, id, AMOUNT) (hidden recipient side is the sentinel)
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, [
{ from: alice, to: PRIVATE_ADDRESS, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: bob })).result).toBe(0n);
expect((await multitoken.methods.balance_of_private(carl, id).simulate({ from: carl })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- transfer_public_to_commitment ---
it(
'transfers public to a recipient commitment as a self-spend',
async () => {
const id = ID_A;
// bob prepares the commitment for himself in his own settled tx, bound to alice (the payer) as completer.
const commitment = await initializeMultiTokenTransferCommitment(multitoken, bob, bob, alice);
await multitoken.methods.mint_to_public(alice, id, AMOUNT).send({ from: alice });
const { receipt: transferTx } = await multitoken.methods
.transfer_public_to_commitment(alice, id, commitment, AMOUNT, 0)
.send({ from: alice });
// transfer_public_to_commitment: TransferSingle(alice, PRIVATE, id, AMOUNT)
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, [
{ from: alice, to: PRIVATE_ADDRESS, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- transfer_private_to_private ---
it(
'transfers private to private as a self-spend and leaks nothing publicly',
async () => {
const id = ID_A;
await multitoken.methods.mint_to_private(alice, id, AMOUNT).send({ from: alice });
const { receipt: transferTx } = await multitoken.methods
.transfer_private_to_private(alice, bob, id, AMOUNT, 0)
.send({ from: alice });
// transfer_private_to_private: (no public events)
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, []);
expect((await multitoken.methods.balance_of_private(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(AMOUNT);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_public(bob, id).simulate({ from: alice })).result).toBe(0n);
// Third party never observes bob's new private notes.
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: carl })).result).toBe(0n);
},
TEST_TIMEOUT,
);
// --- transfer_private_to_public ---
it(
'transfers private to public as a self-spend and credits the recipient publicly',
async () => {
const id = ID_A;
await multitoken.methods.mint_to_private(alice, id, AMOUNT).send({ from: alice });
const { receipt: transferTx } = await multitoken.methods
.transfer_private_to_public(alice, bob, id, AMOUNT, 0)
.send({ from: alice });
// transfer_private_to_public: TransferSingle(PRIVATE, bob, id, AMOUNT)
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, [
{ from: PRIVATE_ADDRESS, to: bob, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_private(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_public(bob, id).simulate({ from: alice })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- transfer_private_to_commitment ---
it(
'transfers private to a settled commitment as a self-spend with no public event',
async () => {
const id = ID_A;
// bob prepares the commitment for himself in a PRIOR settled tx, bound to alice (payer) as completer.
// complete_from_private requires the validity commitment to be from a prior settled tx (the helper settles it).
const commitment = await initializeMultiTokenTransferCommitment(multitoken, bob, bob, alice);
await multitoken.methods.mint_to_private(alice, id, AMOUNT).send({ from: alice });
const { receipt: transferTx } = await multitoken.methods
.transfer_private_to_commitment(alice, id, commitment, AMOUNT, 0)
.send({ from: alice });
// transfer_private_to_commitment: (no public events) — private completion uses a private log, not a public write.
await expectMultiTokenTransferEvents(transferTx.txHash, multitoken.address, []);
expect((await multitoken.methods.balance_of_private(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(AMOUNT);
},
TEST_TIMEOUT,
);
// --- burn_private ---
it(
'burns from a private balance as a self-spend',
async () => {
const id = ID_A;
await multitoken.methods.mint_to_private(alice, id, AMOUNT).send({ from: alice });
await multitoken.methods.burn_private(alice, id, AMOUNT, 0).send({ from: alice });
expect((await multitoken.methods.balance_of_private(alice, id).simulate({ from: alice })).result).toBe(0n);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: alice })).result).toBe(0n);
},
TEST_TIMEOUT,
);
// --- burn_public ---
it(
'burns from a public balance and emits a burn event',
async () => {
const id = ID_A;
await multitoken.methods.mint_to_public(alice, id, AMOUNT).send({ from: alice });
const { receipt: burnTx } = await multitoken.methods.burn_public(alice, id, AMOUNT, 0).send({ from: alice });
// burn_public: TransferSingle(alice, 0x0, id, AMOUNT)
await expectMultiTokenTransferEvents(burnTx.txHash, multitoken.address, [
{ from: alice, to: AztecAddress.ZERO, id, amount: AMOUNT },
]);
expect((await multitoken.methods.balance_of_public(alice, id).simulate({ from: alice })).result).toBe(0n);
},
TEST_TIMEOUT,
);
// --- initialize_transfer_commitment ---
it(
'initializes a transfer commitment (non-zero, no authwit, no balance change)',
async () => {
const id = ID_A;
// Uses the sanctioned wallet-internals commitment helper (the ONLY sanctioned PXE/wallet-internals escape hatch).
const commitment = await initializeMultiTokenTransferCommitment(multitoken, alice, bob, alice);
expect(commitment).not.toBe(0n);
// No id is bound at initialization; no balance is credited to the recipient or completer.
expect((await multitoken.methods.balance_of_private(bob, id).simulate({ from: bob })).result).toBe(0n);
expect((await multitoken.methods.balance_of_public(bob, id).simulate({ from: bob })).result).toBe(0n);
},
TEST_TIMEOUT,
);
});