Skip to content

Instantly share code, notes, and snippets.

@greenhat
Last active April 6, 2025 11:07
Show Gist options
  • Select an option

  • Save greenhat/3c46e4b8ca850639392c1c03b2ae0c7a to your computer and use it in GitHub Desktop.

Select an option

Save greenhat/3c46e4b8ca850639392c1c03b2ae0c7a to your computer and use it in GitHub Desktop.
how to define a map in account storage? example for storing quantity for pub key
Goose is running! Enter your instructions, or try asking what goose can do.
( O)> how to define a map in account storage? example for storing quantity for pub key.
# Defining a Map in Miden Account Storage
You want to create a map in a Miden account's storage to track quantities associated with public keys. Let me help you understand how to implement this in Miden.
Let me search for relevant documentation first.
─── search_dev_docs | miden_mcp ──────────────────────────
prompt: miden account storage map public key
Based on the documentation, I can explain how to define a map in your Miden account storage for storing quantities associated with public keys.
## Basic Map Implementation in Miden Account Storage
In Miden, you can define a map in your account storage using the `StorageMap` type. This is specifically designed for key-value mappings, which is perfect for your use case of storing quantities for public keys.
Here's how to implement it:
### 1. Define Your Account Structure with a Map
```rust
#[component]
struct MyAccount {
// This defines a map in slot 0 of your storage
#[storage(slot(0), description = "Public key to quantity mapping")]
pub_key_qty_map: StorageMap,
// Other fields as needed
}
```
### 2. Implement Methods to Interact with the Map
```rust
impl foo::Guest for MyAccount {
// Method to set a quantity for a public key
fn set_quantity(pub_key: Word, qty: Felt) {
let my_account = MyAccount::default();
my_account.pub_key_qty_map.set(pub_key, qty);
}
// Method to get a quantity for a public key
fn get_quantity(pub_key: Word) -> Felt {
let my_account = MyAccount::default();
my_account.pub_key_qty_map.get(&pub_key)
}
}
```
### 3. Complete Example
Here's a complete example that includes authentication to ensure only the owner can modify quantities:
```rust
#[component]
struct MyAccount {
#[storage(
slot(0),
description = "Owner public key",
type = "auth::rpo_falcon512::pub_key"
)]
owner_public_key: Value,
#[storage(slot(1), description = "Public key to quantity mapping")]
pub_key_qty_map: StorageMap,
}
impl foo::Guest for MyAccount {
// Set quantity for a public key (only if caller is the owner)
fn set_quantity(caller_key: Word, pub_key: Word, qty: Felt) {
let my_account = MyAccount::default();
let owner_key: Word = my_account.owner_public_key.read();
// Ensure only the owner can set quantities
if caller_key == owner_key {
my_account.pub_key_qty_map.set(pub_key, qty);
}
}
// Get quantity for a public key (anyone can read)
fn get_quantity(pub_key: Word) -> Felt {
let my_account = MyAccount::default();
my_account.pub_key_qty_map.get(&pub_key)
}
}
```
## Key Points to Remember
1. **Storage Slots**: Each `StorageMap` is assigned to a specific slot in your account storage (you can have up to 255 slots).
2. **Key and Value Types**:
- Both keys and values in a `StorageMap` are 32 bytes
- Public keys are typically represented as `Word` type
- Quantities can be represented as `Felt` type
3. **Methods**:
- `StorageMap` provides two main methods:
- `set(key, value)`: Store a value for a key
- `get(key)`: Retrieve a value for a key
4. **Access Control**: Consider implementing access control to prevent unauthorized modifications to your map.
This pattern allows you to efficiently store and retrieve quantities associated with public keys in your Miden account storage.
( O)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment