Skip to content

Commit

Permalink
fix(iota-keys): add alias to AccountKeystore::import_from_mnemonic (
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Aug 30, 2024
1 parent 1859059 commit 18acd20
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 13 deletions.
3 changes: 2 additions & 1 deletion crates/iota-keys/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ pub trait AccountKeystore: Send + Sync {
&mut self,
phrase: &str,
key_scheme: SignatureScheme,
alias: Option<String>,
derivation_path: Option<DerivationPath>,
) -> Result<IotaAddress, anyhow::Error> {
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)
.map_err(|e| anyhow::anyhow!("Invalid mnemonic phrase: {:?}", e))?;
let seed = Seed::new(&mnemonic, "");
match derive_key_pair_from_path(seed.as_bytes(), derivation_path, &key_scheme) {
Ok((address, kp)) => {
self.add_key(None, kp)?;
self.add_key(alias, kp)?;
Ok(address)
}
Err(e) => Err(anyhow!("error getting keypair {:?}", e)),
Expand Down
4 changes: 2 additions & 2 deletions crates/iota-keys/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn mnemonic_test() {
let keystore_path_2 = temp_dir.path().join("iota2.keystore");
let mut keystore2 = Keystore::from(FileBasedKeystore::new(&keystore_path_2).unwrap());
let imported_address = keystore2
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None, None)
.unwrap();
assert_eq!(scheme.flag(), Ed25519IotaSignature::SCHEME.flag());
assert_eq!(address, imported_address);
Expand All @@ -224,7 +224,7 @@ fn iota_wallet_address_mnemonic_test() -> Result<(), anyhow::Error> {
let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path).unwrap());

keystore
.import_from_mnemonic(phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(phrase, SignatureScheme::ED25519, None, None)
.unwrap();

let pubkey = keystore.keys()[0].clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/iota-sdk/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn mnemonic_test() {
let keystore_path_2 = temp_dir.path().join("iota2.keystore");
let mut keystore2 = Keystore::from(FileBasedKeystore::new(&keystore_path_2).unwrap());
let imported_address = keystore2
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None, None)
.unwrap();
assert_eq!(scheme.flag(), Ed25519IotaSignature::SCHEME.flag());
assert_eq!(address, imported_address);
Expand Down
1 change: 1 addition & 0 deletions crates/iota/src/keytool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ impl KeyToolCommand {
let iota_address = keystore.import_from_mnemonic(
&input_string,
key_scheme,
None,
derivation_path,
)?;
let ikp = keystore.get_key(&iota_address)?;
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn fund_address(
recipient: IotaAddress,
) -> Result<(), anyhow::Error> {
// Derive the address of the sponsor.
let sponsor = keystore.import_from_mnemonic(SPONSOR_ADDRESS_MNEMONIC, ED25519, None)?;
let sponsor = keystore.import_from_mnemonic(SPONSOR_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("Sponsor address: {sponsor:?}");

Expand Down
8 changes: 6 additions & 2 deletions docs/examples/rust/stardust/address-unlock-condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ async fn main() -> Result<(), anyhow::Error> {
println!("{derivation_path:?}");

// Derive the address of the first account and set it as default
let sender =
keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, Some(derivation_path))?;
let sender = keystore.import_from_mnemonic(
MAIN_ADDRESS_MNEMONIC,
ED25519,
None,
Some(derivation_path),
)?;

println!("Sender address - {sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/alias-migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("{sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/alias-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default.
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("Sender address: {sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/basic-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("Sender address: {sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/foundry-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default.
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("Sender address: {sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/nft-migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("{sender:?}");

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/rust/stardust/nft-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> Result<(), anyhow::Error> {
let mut keystore = setup_keystore()?;

// Derive the address of the first account and set it as default
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None)?;
let sender = keystore.import_from_mnemonic(MAIN_ADDRESS_MNEMONIC, ED25519, None, None)?;

println!("{sender:?}");

Expand Down
2 changes: 2 additions & 0 deletions docs/examples/rust/stardust/shimmer-self-sponsor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async fn main() -> Result<(), anyhow::Error> {
let sponsor = keystore.import_from_mnemonic(
MAIN_ADDRESS_MNEMONIC,
ED25519,
None,
Some(sponsor_derivation_path),
)?;
println!("Sponsor address: {sponsor:?}");
Expand All @@ -61,6 +62,7 @@ async fn main() -> Result<(), anyhow::Error> {
let sender = keystore.import_from_mnemonic(
MAIN_ADDRESS_MNEMONIC,
ED25519,
None,
Some(sender_derivation_path),
)?;
println!("Sender address: {sender:?}");
Expand Down

0 comments on commit 18acd20

Please sign in to comment.