pub struct KeyPair {
pub secret_key: SecretKey,
pub public_key: PublicKey,
}Expand description
A cryptographic key pair (secret key + public key).
This is a convenience type that bundles a SecretKey with its derived
PublicKey for situations where you need both (e.g., creating accounts,
adding access keys).
§Example
use near_kit::KeyPair;
// Generate a random Ed25519 key pair
let keypair = KeyPair::random();
println!("Public key: {}", keypair.public_key);
println!("Secret key: {}", keypair.secret_key);
// Use with account creation
// near.transaction("new.alice.testnet")
// .create_account()
// .add_full_access_key(keypair.public_key)
// .send()
// .await?;Fields§
§secret_key: SecretKeyThe secret (private) key.
public_key: PublicKeyThe public key derived from the secret key.
Implementations§
Source§impl KeyPair
impl KeyPair
Sourcepub fn random() -> Self
pub fn random() -> Self
Generate a random Ed25519 key pair.
This is the most common key type for NEAR.
§Example
use near_kit::KeyPair;
let keypair = KeyPair::random();Sourcepub fn random_ed25519() -> Self
pub fn random_ed25519() -> Self
Generate a random Ed25519 key pair.
§Example
use near_kit::KeyPair;
let keypair = KeyPair::random_ed25519();
assert!(keypair.public_key.to_string().starts_with("ed25519:"));Sourcepub fn from_secret_key(secret_key: SecretKey) -> Self
pub fn from_secret_key(secret_key: SecretKey) -> Self
Create a key pair from an existing secret key.
§Example
use near_kit::{KeyPair, SecretKey};
let secret_key: SecretKey = "ed25519:3D4YudUahN1nawWogh8pAKSj92sUNMdbZGjn7kERKzYoTy8tnFQuwoGUC51DowKqorvkr2pytJSnwuSbsNVfqygr".parse().unwrap();
let keypair = KeyPair::from_secret_key(secret_key);Sourcepub fn from_seed_phrase(phrase: impl AsRef<str>) -> Result<Self, SignerError>
pub fn from_seed_phrase(phrase: impl AsRef<str>) -> Result<Self, SignerError>
Create a key pair from a seed phrase using the default NEAR HD path.
§Example
use near_kit::KeyPair;
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let keypair = KeyPair::from_seed_phrase(phrase).unwrap();Sourcepub fn random_with_seed_phrase() -> Result<(String, Self), SignerError>
pub fn random_with_seed_phrase() -> Result<(String, Self), SignerError>
Generate a new random key pair with a seed phrase for backup.
Returns the seed phrase (for backup) and the key pair.
§Example
use near_kit::KeyPair;
let (phrase, keypair) = KeyPair::random_with_seed_phrase().unwrap();
println!("Backup your seed phrase: {}", phrase);
println!("Public key: {}", keypair.public_key);Trait Implementations§
Auto Trait Implementations§
impl Freeze for KeyPair
impl RefUnwindSafe for KeyPair
impl Send for KeyPair
impl Sync for KeyPair
impl Unpin for KeyPair
impl UnsafeUnpin for KeyPair
impl UnwindSafe for KeyPair
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more