Skip to content

Commit

Permalink
Add chain-id to account storage key (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Jan 8, 2025
1 parent 9294abd commit da06ad3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/account_sdk/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl Controller {
.storage
.set_controller(
app_id.as_str(),
&chain_id,
address,
ControllerMetadata::from(&controller),
)
Expand Down
14 changes: 11 additions & 3 deletions packages/account_sdk/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub struct Credentials {
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct ActiveMetadata {
address: Felt,
chain_id: Felt,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -256,7 +257,10 @@ pub trait StorageBackend: Send + Sync {
self.get(&selectors::Selectors::active(app_id))
.and_then(|value| match value {
Some(StorageValue::Active(metadata)) => self
.get(&selectors::Selectors::account(&metadata.address))
.get(&selectors::Selectors::account(
&metadata.address,
&metadata.chain_id,
))
.and_then(|value| match value {
Some(StorageValue::Controller(metadata)) => Ok(Some(metadata)),
Some(_) => Err(StorageError::TypeMismatch),
Expand All @@ -270,15 +274,19 @@ pub trait StorageBackend: Send + Sync {
fn set_controller(
&mut self,
app_id: &str,
chain_id: &Felt,
address: Felt,
metadata: ControllerMetadata,
) -> Result<(), StorageError> {
self.set(
&selectors::Selectors::active(app_id),
&StorageValue::Active(ActiveMetadata { address }),
&StorageValue::Active(ActiveMetadata {
address,
chain_id: *chain_id,
}),
)?;
self.set(
&selectors::Selectors::account(&address),
&selectors::Selectors::account(&address, chain_id),
&StorageValue::Controller(metadata),
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/account_sdk/src/storage/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ impl Selectors {
format!("@cartridge/{}/active", app_id)
}

pub fn account(address: &Felt) -> String {
format!("@cartridge/account/0x{:x}", address)
pub fn account(address: &Felt, chain_id: &Felt) -> String {
format!("@cartridge/account/0x{:x}/0x{:x}", address, chain_id)
}

pub fn deployment(address: &Felt, chain_id: &Felt) -> String {
Expand Down

0 comments on commit da06ad3

Please sign in to comment.