Skip to content

Commit 3677ca0

Browse files
committed
refactor(api-keys): simplify secret resolution
Pass the resolved wallet through the API-key helper instead of threading its ID and key type separately, and clarify the supported-chains docs for mnemonic versus private-key wallets.
1 parent 81f7f19 commit 3677ca0

3 files changed

Lines changed: 12 additions & 33 deletions

File tree

docs/07-supported-chains.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Master Seed (512 bits via PBKDF2)
117117
└── m/44'/461'/0'/0/0 → Filecoin Account 0
118118
```
119119

120-
A single mnemonic derives accounts across all supported chains. The wallet file stores the encrypted mnemonic; the signer derives the appropriate private key using each chain's coin type and derivation path.
120+
For mnemonic-based wallets, a single mnemonic derives accounts across all supported chains. Those wallet files store the encrypted mnemonic, and the signer derives the appropriate private key using each chain's coin type and derivation path. Wallets imported from raw private keys instead store encrypted curve-key material directly.
121121

122122
## Adding a New Chain
123123

ows/crates/ows-lib/src/key_ops.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::path::Path;
33

4-
use ows_core::{ApiKeyFile, KeyType, OwsError};
4+
use ows_core::{ApiKeyFile, EncryptedWallet, OwsError};
55
use ows_signer::{decrypt, encrypt_with_hkdf, signer_for_chain, CryptoEnvelope, SecretBytes};
66

77
use crate::error::OwsLibError;
@@ -132,14 +132,7 @@ pub fn sign_with_api_key(
132132
}
133133

134134
// 6. Decrypt wallet secret from key file using HKDF(token)
135-
let key = decrypt_key_from_api_key(
136-
&key_file,
137-
&wallet.id,
138-
wallet.key_type.clone(),
139-
token,
140-
chain.chain_type,
141-
index,
142-
)?;
135+
let key = decrypt_key_from_api_key(&key_file, &wallet, token, chain.chain_type, index)?;
143136

144137
// 7. Sign (extract signable portion first — e.g. strips Solana sig-slot headers)
145138
let signer = signer_for_chain(chain.chain_type);
@@ -200,14 +193,7 @@ pub fn sign_message_with_api_key(
200193
}));
201194
}
202195

203-
let key = decrypt_key_from_api_key(
204-
&key_file,
205-
&wallet.id,
206-
wallet.key_type.clone(),
207-
token,
208-
chain.chain_type,
209-
index,
210-
)?;
196+
let key = decrypt_key_from_api_key(&key_file, &wallet, token, chain.chain_type, index)?;
211197
let signer = signer_for_chain(chain.chain_type);
212198
let output = signer.sign_message(key.expose(), msg_bytes)?;
213199

@@ -267,14 +253,7 @@ pub fn enforce_policy_and_decrypt_key(
267253
}));
268254
}
269255

270-
let key = decrypt_key_from_api_key(
271-
&key_file,
272-
&wallet.id,
273-
wallet.key_type.clone(),
274-
token,
275-
chain.chain_type,
276-
index,
277-
)?;
256+
let key = decrypt_key_from_api_key(&key_file, &wallet, token, chain.chain_type, index)?;
278257

279258
Ok((key, key_file))
280259
}
@@ -315,21 +294,21 @@ fn load_policies_for_key(
315294

316295
fn decrypt_key_from_api_key(
317296
key_file: &ApiKeyFile,
318-
wallet_id: &str,
319-
key_type: KeyType,
297+
wallet: &EncryptedWallet,
320298
token: &str,
321299
chain_type: ows_core::ChainType,
322300
index: Option<u32>,
323301
) -> Result<SecretBytes, OwsLibError> {
324-
let envelope_value = key_file.wallet_secrets.get(wallet_id).ok_or_else(|| {
302+
let envelope_value = key_file.wallet_secrets.get(&wallet.id).ok_or_else(|| {
325303
OwsLibError::InvalidInput(format!(
326-
"API key has no encrypted secret for wallet {wallet_id}"
304+
"API key has no encrypted secret for wallet {}",
305+
wallet.id
327306
))
328307
})?;
329308

330309
let envelope: CryptoEnvelope = serde_json::from_value(envelope_value.clone())?;
331310
let secret = decrypt(&envelope, token)?;
332-
crate::ops::secret_to_signing_key(&secret, key_type, chain_type, index)
311+
crate::ops::secret_to_signing_key(&secret, &wallet.key_type, chain_type, index)
333312
}
334313

335314
#[cfg(test)]

ows/crates/ows-lib/src/ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn derive_all_accounts_from_keys(keys: &KeyPair) -> Result<Vec<WalletAccount>, O
130130

131131
pub(crate) fn secret_to_signing_key(
132132
secret: &SecretBytes,
133-
key_type: KeyType,
133+
key_type: &KeyType,
134134
chain_type: ChainType,
135135
index: Option<u32>,
136136
) -> Result<SecretBytes, OwsLibError> {
@@ -639,7 +639,7 @@ pub fn decrypt_signing_key(
639639
let wallet = vault::load_wallet_by_name_or_id(wallet_name_or_id, vault_path)?;
640640
let envelope: CryptoEnvelope = serde_json::from_value(wallet.crypto.clone())?;
641641
let secret = decrypt(&envelope, passphrase)?;
642-
secret_to_signing_key(&secret, wallet.key_type, chain_type, index)
642+
secret_to_signing_key(&secret, &wallet.key_type, chain_type, index)
643643
}
644644

645645
/// Resolve the RPC URL: explicit > config override (exact chain_id) > config (namespace) > built-in default.

0 commit comments

Comments
 (0)