-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathunauthenticatedNoteTransfer.ts
More file actions
176 lines (152 loc) · 5.81 KB
/
unauthenticatedNoteTransfer.ts
File metadata and controls
176 lines (152 loc) · 5.81 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
/**
* Demonstrates unauthenticated note transfer chain using a delegated prover on the Miden Network
* Creates a chain of P2ID (Pay to ID) notes: Alice → wallet 1 → wallet 2 → wallet 3 → wallet 4
*
* @throws {Error} If the function cannot be executed in a browser environment
*/
export async function unauthenticatedNoteTransfer(): Promise<void> {
// Ensure this runs only in a browser context
if (typeof window === 'undefined') return console.warn('Run in browser');
const {
WebClient,
AccountStorageMode,
NoteType,
TransactionProver,
Note,
NoteAssets,
OutputNoteArray,
Felt,
FungibleAsset,
NoteAndArgsArray,
NoteAndArgs,
TransactionRequestBuilder,
OutputNote,
} = await import('@demox-labs/miden-sdk');
const client = await WebClient.createClient('https://rpc.testnet.miden.io');
const prover = TransactionProver.newRemoteProver(
'https://tx-prover.testnet.miden.io',
);
console.log('Latest block:', (await client.syncState()).blockNum());
// ── Creating new account ──────────────────────────────────────────────────────
console.log('Creating accounts');
console.log('Creating account for Alice…');
const alice = await client.newWallet(AccountStorageMode.public(), true, 0);
console.log('Alice accout ID:', alice.id().toString());
const wallets = [];
for (let i = 0; i < 5; i++) {
const wallet = await client.newWallet(AccountStorageMode.public(), true, 0);
wallets.push(wallet);
console.log('wallet ', i.toString(), wallet.id().toString());
}
// ── Creating new faucet ──────────────────────────────────────────────────────
const faucet = await client.newFaucet(
AccountStorageMode.public(),
false,
'MID',
8,
BigInt(1_000_000),
0,
);
console.log('Faucet ID:', faucet.id().toString());
// ── mint 10 000 MID to Alice ──────────────────────────────────────────────────────
{
const txResult = await client.executeTransaction(
faucet.id(),
client.newMintTransactionRequest(
alice.id(),
faucet.id(),
NoteType.Public,
BigInt(10_000),
),
);
const proven = await client.proveTransaction(txResult, prover);
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
await client.applyTransaction(txResult, submissionHeight);
}
console.log('Waiting for settlement');
await new Promise((r) => setTimeout(r, 7_000));
await client.syncState();
// ── Consume the freshly minted note ──────────────────────────────────────────────
const noteIds = (await client.getConsumableNotes(alice.id())).map((rec) =>
rec.inputNoteRecord().id().toString(),
);
{
const txResult = await client.executeTransaction(
alice.id(),
client.newConsumeTransactionRequest(noteIds),
);
const proven = await client.proveTransaction(txResult, prover);
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
await client.applyTransaction(txResult, submissionHeight);
await client.syncState();
}
// ── Create unauthenticated note transfer chain ─────────────────────────────────────────────
// Alice → wallet 1 → wallet 2 → wallet 3 → wallet 4
for (let i = 0; i < wallets.length; i++) {
console.log(`\nUnauthenticated tx ${i + 1}`);
// Determine sender and receiver for this iteration
const sender = i === 0 ? alice : wallets[i - 1];
const receiver = wallets[i];
console.log('Sender:', sender.id().toString());
console.log('Receiver:', receiver.id().toString());
const assets = new NoteAssets([new FungibleAsset(faucet.id(), BigInt(50))]);
const p2idNote = Note.createP2IDNote(
sender.id(),
receiver.id(),
assets,
NoteType.Public,
new Felt(BigInt(0)), // aux value
);
const outputP2ID = OutputNote.full(p2idNote);
console.log('Creating P2ID note...');
{
const txResult = await client.executeTransaction(
sender.id(),
new TransactionRequestBuilder()
.withOwnOutputNotes(new OutputNoteArray([outputP2ID]))
.build(),
);
const proven = await client.proveTransaction(txResult, prover);
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
await client.applyTransaction(txResult, submissionHeight);
}
console.log('Consuming P2ID note...');
const noteIdAndArgs = new NoteAndArgs(p2idNote, null);
const consumeRequest = new TransactionRequestBuilder()
.withUnauthenticatedInputNotes(new NoteAndArgsArray([noteIdAndArgs]))
.build();
{
const txResult = await client.executeTransaction(
receiver.id(),
consumeRequest,
);
const proven = await client.proveTransaction(txResult, prover);
const submissionHeight = await client.submitProvenTransaction(
proven,
txResult,
);
const txExecutionResult = await client.applyTransaction(
txResult,
submissionHeight,
);
const txId = txExecutionResult
.executedTransaction()
.id()
.toHex()
.toString();
console.log(
`Consumed Note Tx on MidenScan: https://testnet.midenscan.com/tx/${txId}`,
);
}
}
console.log('Asset transfer chain completed ✅');
}