Skip to content

Commit 9c2672b

Browse files
authored
Merge pull request #133 from 0xMiden/0.12.0
feat: update rust examples to 0.12.0
2 parents 3b4c028 + 9e0cddb commit 9c2672b

30 files changed

+3056
-2471
lines changed

docs/src/rust-client/counter_contract_tutorial.md

Lines changed: 112 additions & 125 deletions
Large diffs are not rendered by default.

docs/src/rust-client/create_deploy_tutorial.md

Lines changed: 95 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ Add the following dependencies to your `Cargo.toml` file:
4848

4949
```toml
5050
[dependencies]
51-
miden-client = { version = "0.11", features = ["testing", "tonic", "sqlite"] }
52-
miden-lib = { version = "0.11", default-features = false }
53-
miden-objects = { version = "0.11", default-features = false, features = ["testing"] }
54-
miden-crypto = { version = "0.15.9", features = ["executable"] }
55-
miden-assembly = "0.17.0"
51+
miden-client = { version = "0.12", features = ["testing", "tonic"] }
52+
miden-client-sqlite-store = { version = "0.12", package = "miden-client-sqlite-store" }
53+
miden-lib = { version = "0.12", default-features = false }
54+
miden-objects = { version = "0.12", default-features = false, features = ["testing"] }
55+
miden-crypto = { version = "0.17.1", features = ["executable"] }
56+
miden-assembly = "0.18.3"
5657
rand = { version = "0.9" }
5758
serde = { version = "1", features = ["derive"] }
5859
serde_json = { version = "1.0", features = ["raw_value"] }
@@ -73,41 +74,48 @@ Copy and paste the following code into your `src/main.rs` file.
7374

7475
```rust no_run
7576
use miden_lib::account::auth::AuthRpoFalcon512;
76-
use rand::RngCore;
77+
use rand::{rngs::StdRng, RngCore};
7778
use std::sync::Arc;
7879
use tokio::time::Duration;
7980

8081
use miden_client::{
8182
account::{
8283
component::{BasicFungibleFaucet, BasicWallet},
83-
AccountBuilder, AccountId, AccountIdAddress, AccountStorageMode, AccountType, Address,
84-
AddressInterface,
84+
AccountId,
8585
},
86-
asset::{FungibleAsset, TokenSymbol},
86+
address::NetworkId,
8787
auth::AuthSecretKey,
8888
builder::ClientBuilder,
89-
crypto::SecretKey,
9089
keystore::FilesystemKeyStore,
9190
note::{create_p2id_note, NoteType},
92-
rpc::{Endpoint, TonicRpcClient},
93-
transaction::{OutputNote, PaymentNoteDescription, TransactionRequestBuilder},
94-
ClientError, Felt,
91+
rpc::{Endpoint, GrpcClient},
92+
transaction::{OutputNote, TransactionRequestBuilder},
93+
ClientError,
94+
};
95+
use miden_client_sqlite_store::ClientBuilderSqliteExt;
96+
use miden_objects::{
97+
account::{AccountBuilder, AccountIdVersion, AccountStorageMode, AccountType},
98+
asset::{FungibleAsset, TokenSymbol},
99+
Felt,
95100
};
96-
97-
use miden_objects::account::{AccountIdVersion, NetworkId};
98-
99101

100102
#[tokio::main]
101103
async fn main() -> Result<(), ClientError> {
102-
// Initialize client & keystore
104+
// Initialize client
103105
let endpoint = Endpoint::testnet();
104106
let timeout_ms = 10_000;
105-
let rpc_api = Arc::new(TonicRpcClient::new(&endpoint, timeout_ms));
106-
let keystore: FilesystemKeyStore<rand::prelude::StdRng> = FilesystemKeyStore::new("./keystore".into()).unwrap().into();
107+
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));
108+
109+
// Initialize keystore
110+
let keystore_path = std::path::PathBuf::from("./keystore");
111+
let keystore = Arc::new(FilesystemKeyStore::<StdRng>::new(keystore_path).unwrap());
112+
113+
let store_path = std::path::PathBuf::from("./store.sqlite3");
107114

108115
let mut client = ClientBuilder::new()
109-
.rpc(rpc_api)
110-
.authenticator(keystore.clone().into())
116+
.rpc(rpc_client)
117+
.sqlite_store(store_path)
118+
.authenticator(keystore.clone())
111119
.in_debug_mode(true.into())
112120
.build()
113121
.await?;
@@ -155,36 +163,25 @@ println!("\n[STEP 1] Creating a new account for Alice");
155163
let mut init_seed = [0_u8; 32];
156164
client.rng().fill_bytes(&mut init_seed);
157165

158-
let key_pair = SecretKey::with_rng(client.rng());
166+
let key_pair = AuthSecretKey::new_rpo_falcon512();
159167

160168
// Build the account
161-
let builder = AccountBuilder::new(init_seed)
169+
let alice_account = AccountBuilder::new(init_seed)
162170
.account_type(AccountType::RegularAccountUpdatableCode)
163171
.storage_mode(AccountStorageMode::Public)
164-
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key()))
165-
.with_component(BasicWallet);
166-
167-
let (alice_account, seed) = builder.build().unwrap();
172+
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key().to_commitment()))
173+
.with_component(BasicWallet)
174+
.build()
175+
.unwrap();
168176

169177
// Add the account to the client
170-
client
171-
.add_account(&alice_account, Some(seed), false)
172-
.await?;
178+
client.add_account(&alice_account, false).await?;
173179

174180
// Add the key pair to the keystore
175-
keystore
176-
.add_key(&AuthSecretKey::RpoFalcon512(key_pair))
177-
.unwrap();
178-
179-
println!(
180-
"Alice's account ID: {:?}",
181-
Address::from(AccountIdAddress::new(
182-
alice_account.id(),
183-
AddressInterface::Unspecified
184-
))
185-
.to_bech32(NetworkId::Testnet)
186-
);
181+
keystore.add_key(&key_pair).unwrap();
187182

183+
let alice_account_id_bech32 = alice_account.id().to_bech32(NetworkId::Testnet);
184+
println!("Alice's account ID: {:?}", alice_account_id_bech32);
188185
```
189186

190187
## Step 4: Deploying a fungible faucet
@@ -211,35 +208,25 @@ let decimals = 8;
211208
let max_supply = Felt::new(1_000_000);
212209

213210
// Generate key pair
214-
let key_pair = SecretKey::with_rng(client.rng());
211+
let key_pair = AuthSecretKey::new_rpo_falcon512();
215212

216-
// Build the account
217-
let builder = AccountBuilder::new(init_seed)
213+
// Build the faucet account
214+
let faucet_account = AccountBuilder::new(init_seed)
218215
.account_type(AccountType::FungibleFaucet)
219216
.storage_mode(AccountStorageMode::Public)
220-
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key()))
221-
.with_component(BasicFungibleFaucet::new(symbol, decimals, max_supply).unwrap());
222-
223-
let (faucet_account, seed) = builder.build().unwrap();
217+
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key().to_commitment()))
218+
.with_component(BasicFungibleFaucet::new(symbol, decimals, max_supply).unwrap())
219+
.build()
220+
.unwrap();
224221

225222
// Add the faucet to the client
226-
client
227-
.add_account(&faucet_account, Some(seed), false)
228-
.await?;
223+
client.add_account(&faucet_account, false).await?;
229224

230225
// Add the key pair to the keystore
231-
keystore
232-
.add_key(&AuthSecretKey::RpoFalcon512(key_pair))
233-
.unwrap();
226+
keystore.add_key(&key_pair).unwrap();
234227

235-
println!(
236-
"Faucet account ID: {:?}",
237-
Address::from(AccountIdAddress::new(
238-
faucet_account.id(),
239-
AddressInterface::Unspecified
240-
))
241-
.to_bech32(NetworkId::Testnet)
242-
);
228+
let faucet_account_id_bech32 = faucet_account.id().to_bech32(NetworkId::Testnet);
229+
println!("Faucet account ID: {:?}", faucet_account_id_bech32);
243230

244231
// Resync to show newly deployed faucet
245232
client.sync_state().await?;
@@ -253,41 +240,49 @@ _When tokens are minted from this faucet, each token batch is represented as a "
253240
Your updated `main()` function in `src/main.rs` should look like this:
254241

255242
```rust
256-
use rand::RngCore;
243+
use miden_lib::account::auth::AuthRpoFalcon512;
244+
use rand::{rngs::StdRng, RngCore};
257245
use std::sync::Arc;
258246
use tokio::time::Duration;
259247

260248
use miden_client::{
261249
account::{
262250
component::{BasicFungibleFaucet, BasicWallet},
263-
AccountBuilder, AccountId, AccountIdAddress, AccountStorageMode, AccountType, Address,
264-
AddressInterface,
251+
AccountId,
265252
},
266-
asset::{FungibleAsset, TokenSymbol},
253+
address::NetworkId,
267254
auth::AuthSecretKey,
268255
builder::ClientBuilder,
269-
crypto::SecretKey,
270256
keystore::FilesystemKeyStore,
271257
note::{create_p2id_note, NoteType},
272-
rpc::{Endpoint, TonicRpcClient},
273-
transaction::{OutputNote, PaymentNoteDescription, TransactionRequestBuilder},
274-
ClientError, Felt,
258+
rpc::{Endpoint, GrpcClient},
259+
transaction::{OutputNote, TransactionRequestBuilder},
260+
ClientError,
261+
};
262+
use miden_client_sqlite_store::ClientBuilderSqliteExt;
263+
use miden_objects::{
264+
account::{AccountBuilder, AccountIdVersion, AccountStorageMode, AccountType},
265+
asset::{FungibleAsset, TokenSymbol},
266+
Felt,
275267
};
276-
use miden_lib::account::auth::AuthRpoFalcon512;
277-
use miden_objects::account::{AccountIdVersion, NetworkId};
278268

279269
#[tokio::main]
280270
async fn main() -> Result<(), ClientError> {
281-
// Initialize client & keystore
271+
// Initialize client
282272
let endpoint = Endpoint::testnet();
283273
let timeout_ms = 10_000;
284-
let rpc_api = Arc::new(TonicRpcClient::new(&endpoint, timeout_ms));
285-
let keystore: FilesystemKeyStore<rand::prelude::StdRng> =
286-
FilesystemKeyStore::new("./keystore".into()).unwrap();
274+
let rpc_client = Arc::new(GrpcClient::new(&endpoint, timeout_ms));
275+
276+
// Initialize keystore
277+
let keystore_path = std::path::PathBuf::from("./keystore");
278+
let keystore = Arc::new(FilesystemKeyStore::<StdRng>::new(keystore_path).unwrap());
279+
280+
let store_path = std::path::PathBuf::from("./store.sqlite3");
287281

288282
let mut client = ClientBuilder::new()
289-
.rpc(rpc_api)
290-
.authenticator(keystore.clone().into())
283+
.rpc(rpc_client)
284+
.sqlite_store(store_path)
285+
.authenticator(keystore.clone())
291286
.in_debug_mode(true.into())
292287
.build()
293288
.await?;
@@ -304,35 +299,25 @@ async fn main() -> Result<(), ClientError> {
304299
let mut init_seed = [0_u8; 32];
305300
client.rng().fill_bytes(&mut init_seed);
306301

307-
let key_pair = SecretKey::with_rng(client.rng());
302+
let key_pair = AuthSecretKey::new_rpo_falcon512();
308303

309304
// Build the account
310-
let builder = AccountBuilder::new(init_seed)
305+
let alice_account = AccountBuilder::new(init_seed)
311306
.account_type(AccountType::RegularAccountUpdatableCode)
312307
.storage_mode(AccountStorageMode::Public)
313-
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key()))
314-
.with_component(BasicWallet);
315-
316-
let (alice_account, seed) = builder.build().unwrap();
308+
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key().to_commitment()))
309+
.with_component(BasicWallet)
310+
.build()
311+
.unwrap();
317312

318313
// Add the account to the client
319-
client
320-
.add_account(&alice_account, Some(seed), false)
321-
.await?;
314+
client.add_account(&alice_account, false).await?;
322315

323316
// Add the key pair to the keystore
324-
keystore
325-
.add_key(&AuthSecretKey::RpoFalcon512(key_pair))
326-
.unwrap();
317+
keystore.add_key(&key_pair).unwrap();
327318

328-
println!(
329-
"Alice's account ID: {:?}",
330-
Address::from(AccountIdAddress::new(
331-
alice_account.id(),
332-
AddressInterface::Unspecified
333-
))
334-
.to_bech32(NetworkId::Testnet)
335-
);
319+
let alice_account_id_bech32 = alice_account.id().to_bech32(NetworkId::Testnet);
320+
println!("Alice's account ID: {:?}", alice_account_id_bech32);
336321

337322
//------------------------------------------------------------
338323
// STEP 2: Deploy a fungible faucet
@@ -349,35 +334,25 @@ async fn main() -> Result<(), ClientError> {
349334
let max_supply = Felt::new(1_000_000);
350335

351336
// Generate key pair
352-
let key_pair = SecretKey::with_rng(client.rng());
337+
let key_pair = AuthSecretKey::new_rpo_falcon512();
353338

354-
// Build the account
355-
let builder = AccountBuilder::new(init_seed)
339+
// Build the faucet account
340+
let faucet_account = AccountBuilder::new(init_seed)
356341
.account_type(AccountType::FungibleFaucet)
357342
.storage_mode(AccountStorageMode::Public)
358-
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key()))
359-
.with_component(BasicFungibleFaucet::new(symbol, decimals, max_supply).unwrap());
360-
361-
let (faucet_account, seed) = builder.build().unwrap();
343+
.with_auth_component(AuthRpoFalcon512::new(key_pair.public_key().to_commitment()))
344+
.with_component(BasicFungibleFaucet::new(symbol, decimals, max_supply).unwrap())
345+
.build()
346+
.unwrap();
362347

363348
// Add the faucet to the client
364-
client
365-
.add_account(&faucet_account, Some(seed), false)
366-
.await?;
349+
client.add_account(&faucet_account, false).await?;
367350

368351
// Add the key pair to the keystore
369-
keystore
370-
.add_key(&AuthSecretKey::RpoFalcon512(key_pair))
371-
.unwrap();
352+
keystore.add_key(&key_pair).unwrap();
372353

373-
println!(
374-
"Faucet account ID: {:?}",
375-
Address::from(AccountIdAddress::new(
376-
faucet_account.id(),
377-
AddressInterface::Unspecified
378-
))
379-
.to_bech32(NetworkId::Testnet)
380-
);
354+
let faucet_account_id_bech32 = faucet_account.id().to_bech32(NetworkId::Testnet);
355+
println!("Faucet account ID: {:?}", faucet_account_id_bech32);
381356

382357
// Resync to show newly deployed faucet
383358
client.sync_state().await?;

0 commit comments

Comments
 (0)