Skip to content

Commit

Permalink
[SDK] Add a new SDK method to create account via private key literal (a…
Browse files Browse the repository at this point in the history
…ptos-labs#12528)

* Add a new SDK method to create account via private key literal

Signed-off-by: JmPotato <[email protected]>

* Adopt the changes on LocalAccount

Signed-off-by: JmPotato <[email protected]>

---------

Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato authored Jun 21, 2024
1 parent 791ad68 commit 6e5304e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ aptos-types = { workspace = true }
base64 = { workspace = true }
bcs = { workspace = true }
ed25519-dalek-bip32 = { workspace = true }
hex = { workspace = true }
move-core-types = { workspace = true }
rand_core = { workspace = true }
serde_json = { workspace = true }
Expand Down
31 changes: 31 additions & 0 deletions sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ impl LocalAccount {
Ok(Self::new(address, key, sequence_number))
}

/// Create a new account from the given private key in hex literal.
pub fn from_private_key(private_key: &str, sequence_number: u64) -> Result<Self> {
let key = AccountKey::from_private_key(Ed25519PrivateKey::try_from(
hex::decode(private_key.trim_start_matches("0x"))?.as_ref(),
)?);
let address = key.authentication_key().account_address();

Ok(Self::new(address, key, sequence_number))
}

/// Generate a new account locally. Note: This function does not actually
/// create an account on the Aptos blockchain, it just generates a new
/// account locally.
Expand Down Expand Up @@ -575,4 +585,25 @@ mod tests {
// Return an error for empty mnemonic phrase.
assert!(LocalAccount::from_derive_path(derive_path, "", 0).is_err());
}

#[test]
fn test_create_account_from_private_key() {
let key = AccountKey::generate(&mut rand::rngs::OsRng);
let (private_key_hex_literal, public_key_hex_literal) = (
hex::encode(key.private_key().to_bytes().as_ref()),
key.authentication_key().account_address().to_hex_literal(),
);

// Test private key hex literal without `0x` prefix.
let account = LocalAccount::from_private_key(&private_key_hex_literal, 0).unwrap();
assert_eq!(account.address().to_hex_literal(), public_key_hex_literal);

// Test private key hex literal with `0x` prefix.
let account =
LocalAccount::from_private_key(&format!("0x{}", private_key_hex_literal), 0).unwrap();
assert_eq!(account.address().to_hex_literal(), public_key_hex_literal);

// Test invalid private key hex literal.
assert!(LocalAccount::from_private_key("invalid_private_key", 0).is_err());
}
}

0 comments on commit 6e5304e

Please sign in to comment.