hiveio-beekeeper
Rust bindings for Beekeeper, the
Hive wallet component: encrypted key storage, wallet sessions with auto-lock,
key import/removal, digest signing and memo encryption/decryption.
Installation
cargo add hiveio-beekeeper
The library target is named beekeeper, so imports read
use beekeeper::prelude::*;.
Example usage
Runnable versions of all examples below live in examples.rs;
run them in order with cargo run --example readme. The persistent examples
share ./storage_root under the current directory and build on each other's
state.
Importing and removing keys from a wallet
Note: wallet names must be unique within one beekeeper instance — creating
a wallet that already exists there is an error.
Remember to always use strong passwords for wallets! If you pass None as
the password when creating a wallet, a strong random password is generated
for you and returned in CreatedWallet::password.
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(BeekeeperOptions::new("./storage_root"));
let session = beekeeper.create_session()?;
let mut wallet = session.create_wallet("wallet0", "password")?.wallet;
let public_key1 = wallet
.import_key("5JkFnXrLM2ap9t3AmAxBJvQHF7xSKtnTrCTginQCkhzU5S7ecPT")?;
let public_key2 = wallet
.import_key("5KGKYWMXReJewfj5M29APNMqGEu173DzvHv5TeJAg9SkjUeQV78")?;
wallet.remove_key(&public_key1)?;
println!("{}", wallet.has_matching_private_key(&public_key1)?);
assert_eq!(wallet.get_public_keys()?, vec![public_key2]);
Ok(())
}
Listing all wallets
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(BeekeeperOptions::new("./storage_root"));
let session = beekeeper.create_session()?;
for wallet in session.list_created_wallets() {
println!("Available wallet: {}", wallet.name); }
Ok(())
}
Retrieving session information
use std::time::SystemTime;
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(
BeekeeperOptions::default()
.unlock_timeout(10) .in_memory(true),
);
let session = beekeeper.create_session()?;
let info = session.get_info();
let locks_in = info.timeout_time.duration_since(SystemTime::now()).unwrap();
println!(
"All wallets will be automatically locked in {} ms if not used.",
locks_in.as_millis()
);
Ok(())
}
If you still want to use the wallet after the timeout, lock() the handle
and unlock() it again.
Every time you use the wallet, the timeout is reset.
Use beekeeper in-memory
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper =
BeekeeperApi::new(BeekeeperOptions::default().in_memory(true));
let session = beekeeper.create_session()?.is_temporary(true);
let wallet = session.create_wallet("wallet1", "password")?.wallet;
println!("{}", wallet.is_temporary());
Ok(())
}
Open wallet if exists
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(BeekeeperOptions::new("./storage_root"));
let session = beekeeper.create_session()?;
let mut unlocked_wallet = if session.has_wallet("wallet0")? {
session.open_wallet("wallet0")?.unlock("password")?
} else {
session.create_wallet("wallet0", "password")?.wallet
};
println!("{:?}", unlocked_wallet.get_public_keys()?);
Ok(())
}
Sign transaction digest
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(BeekeeperOptions::new("./storage_root"));
let session = beekeeper.create_session()?;
let mut unlocked_wallet =
session.open_wallet("wallet0")?.unlock("password")?;
let sig_digest =
"f1d3ff8443297732862df21dc4e57262a2b0b6f8c5f9f1d3ff8443297732862d";
let my_public_key = "STM6oR6ckA4TejTWTjatUdbcS98AKETc3rcnQ9dWxmeNiKDzfhBZa";
let signature = unlocked_wallet.sign_digest(my_public_key, sig_digest)?;
println!("Signature: {signature}");
Ok(())
}
Encrypt and decrypt message
use beekeeper::prelude::*;
fn main() -> Result<(), BeekeeperError> {
let beekeeper = BeekeeperApi::new(BeekeeperOptions::new("./storage_root"));
let session = beekeeper.create_session()?;
let mut wallet = session.create_wallet("wallet2", "password")?.wallet;
let public_key1 = wallet
.import_key("5JkFnXrLM2ap9t3AmAxBJvQHF7xSKtnTrCTginQCkhzU5S7ecPT")?;
let public_key2 = wallet
.import_key("5KGKYWMXReJewfj5M29APNMqGEu173DzvHv5TeJAg9SkjUeQV78")?;
let message = "Hello, Hive!";
let encrypted =
wallet.encrypt_data(&public_key1, public_key2.as_str(), message, 0)?;
println!("Encrypted message: {encrypted}");
let decrypted =
wallet.decrypt_data(&public_key1, public_key2.as_str(), &encrypted)?;
println!("Decrypted message: {decrypted}");
Ok(())
}
Platform support
Linux x86_64. The bundle is built per target triple; other platforms need a
regenerated bundle and are not published yet.
License
MIT — see the beekeeper repository.