beekeeper/lib.rs
1//! Rust bindings for the Beekeeper wallet daemon.
2//!
3//! This crate exposes the same wallet-management surface as the
4//! `@hiveio/beekeeper` npm package (`programs/beekeeper/beekeeper_wasm`),
5//! but as a native Rust library. The underlying wallet logic and crypto
6//! primitives live in the shared C++ `core/` library and the FC-backed
7//! `fc_crypto_bridge`, reached through a [`cxx`] bridge
8//! ([`ffi::BeekeeperHolder`]). Storage callbacks are still injected from
9//! Rust via [`RustStorageProtocol`].
10//!
11//! # Quick start
12//!
13//! ```no_run
14//! use beekeeper::prelude::*;
15//!
16//! let bk = BeekeeperApi::new(
17//! BeekeeperOptions::new("./storage_root-rust").unlock_timeout(900),
18//! );
19//! let session = bk.create_session().unwrap();
20//! let mut created = session.create_wallet("my-wallet", "password").unwrap();
21//! let pubkey = created.import_key("5J...wif...").unwrap();
22//! let sig = created.sign_digest(&pubkey, "deadbeef...").unwrap();
23//! // Dropping `session` locks its wallets and closes the session.
24//! ```
25//!
26//! # Design notes
27//!
28//! The API follows the npm package, but where Rust allows a safer shape
29//! than a 1:1 port, the Rust shape wins:
30//!
31//! - **Synchronous API.** All calls are blocking.
32//! - **Sessions close on `Drop`.** A [`session::Session`] is an owned
33//! guard that locks its wallets and closes itself when it goes out of
34//! scope; there is no explicit `close`.
35//! - **Lock state lives in the type system.** `unlock`/`lock`/`close`
36//! consume the wallet handle and return the next state, so stale
37//! handles are unrepresentable.
38//! - **Fixed backends.** Storage is always the bundled filesystem-backed
39//! [`storage::RustStorageProtocol`] and crypto is always the FC-backed
40//! provider; in-memory mode is selected through
41//! [`BeekeeperOptions::in_memory`]. The key prefix is hard-coded to
42//! [`wallet::DEFAULT_KEY_PREFIX`] (`"STM"`).
43
44pub mod api;
45pub mod options;
46pub mod session;
47pub mod wallet;
48
49mod consts;
50mod errors;
51mod storage;
52
53pub use api::BeekeeperApi;
54pub use errors::BeekeeperError;
55pub use options::BeekeeperOptions;
56pub use storage::{RustStorageProtocol, new_rust_storage_protocol};
57
58/// One-line import for everything the README examples use.
59///
60/// ```no_run
61/// use beekeeper::prelude::*;
62/// ```
63pub mod prelude {
64 pub use crate::api::BeekeeperApi;
65 pub use crate::errors::BeekeeperError;
66 pub use crate::options::{BeekeeperOptions, DEFAULT_STORAGE_ROOT};
67 pub use crate::session::{Session, SessionInfo};
68 pub use crate::wallet::{
69 CreatedWallet, DEFAULT_KEY_PREFIX, LockedWallet, UnlockedWallet,
70 WalletInfo,
71 };
72}
73
74/// `cxx`-generated FFI between Rust and the C++ `beekeeper_rs::beekeeper_holder`.
75///
76/// Items in this module are produced by the [`cxx::bridge`] macro and are
77/// considered an implementation detail of the crate. They are public so that
78/// the higher-level wrappers in [`api`], [`session`] and [`wallet`] can hold
79/// `UniquePtr<BeekeeperHolder>` fields, but new code outside of those modules
80/// should prefer the safe wrappers.
81///
82/// All `cpp_*` methods on [`RustStorageProtocol`] are reachable from C++
83/// only — they are listed here purely to satisfy the bridge contract.
84#[cxx::bridge(namespace = "cpp")]
85pub mod ffi {
86 /// Plain-data summary of a wallet returned by C++ `list_wallets`.
87 ///
88 /// The Rust facade widens this to [`wallet::WalletInfo`] with the extra
89 /// `is_temporary` flag tracked by [`api::BeekeeperApi`].
90 pub struct WalletDetails {
91 pub name: String,
92 pub unlocked: bool,
93 }
94
95 extern "Rust" {
96 type RustStorageProtocol;
97
98 fn new_rust_storage_protocol(
99 storage_root: &str,
100 ) -> Box<RustStorageProtocol>;
101
102 fn cpp_load(
103 self: &mut RustStorageProtocol,
104 name: &str,
105 ) -> Result<Vec<u8>>;
106
107 fn cpp_save(
108 self: &mut RustStorageProtocol,
109 name: &str,
110 data: &[u8],
111 ) -> Result<()>;
112
113 fn cpp_scan_dir(self: &mut RustStorageProtocol, name: &str) -> bool;
114 fn cpp_sync(self: &mut RustStorageProtocol);
115 fn cpp_close(self: &mut RustStorageProtocol);
116 }
117
118 unsafe extern "C++" {
119 include!("beekeeper_rs/beekeeper_holder.hpp");
120 include!("beekeeper_rs/rust_wallet_storage.hpp");
121
122 #[namespace = "beekeeper_rs"]
123 #[cxx_name = "beekeeper_holder"]
124 type BeekeeperHolder;
125
126 #[namespace = "beekeeper_rs"]
127 fn new_beekeeper_holder(
128 storage_impl: Box<RustStorageProtocol>,
129 unlock_timeout_sec: u32,
130 ) -> UniquePtr<BeekeeperHolder>;
131
132 #[namespace = "beekeeper_rs"]
133 fn new_beekeeper_holder_in_memory(
134 unlock_timeout_sec: u32,
135 ) -> UniquePtr<BeekeeperHolder>;
136
137 fn create_session(self: Pin<&mut BeekeeperHolder>) -> Result<String>;
138 fn close_session(
139 self: Pin<&mut BeekeeperHolder>,
140 token: &str,
141 ) -> Result<()>;
142 fn has_wallet(self: &BeekeeperHolder, name: &str) -> Result<bool>;
143 fn create_wallet(
144 self: Pin<&mut BeekeeperHolder>,
145 token: &str,
146 name: &str,
147 password: &str,
148 is_temporary: bool,
149 ) -> Result<String>;
150 fn open_wallet(
151 self: Pin<&mut BeekeeperHolder>,
152 token: &str,
153 name: &str,
154 ) -> Result<()>;
155 fn close_wallet(
156 self: Pin<&mut BeekeeperHolder>,
157 name: &str,
158 ) -> Result<()>;
159 fn unlock(
160 self: Pin<&mut BeekeeperHolder>,
161 name: &str,
162 password: &str,
163 ) -> Result<()>;
164 fn lock(self: Pin<&mut BeekeeperHolder>, name: &str) -> Result<()>;
165 fn lock_all(self: Pin<&mut BeekeeperHolder>) -> Result<()>;
166 fn sync_storage(self: Pin<&mut BeekeeperHolder>);
167 fn close_storage(self: Pin<&mut BeekeeperHolder>);
168
169 fn list_wallets(
170 self: &BeekeeperHolder,
171 token: &str,
172 ) -> Result<Vec<WalletDetails>>;
173 fn import_key(
174 self: Pin<&mut BeekeeperHolder>,
175 name: &str,
176 wif_key: &str,
177 prefix: &str,
178 ) -> Result<String>;
179 fn remove_key(
180 self: Pin<&mut BeekeeperHolder>,
181 name: &str,
182 public_key: &str,
183 prefix: &str,
184 ) -> Result<()>;
185 fn has_matching_private_key(
186 self: &BeekeeperHolder,
187 name: &str,
188 public_key: &str,
189 prefix: &str,
190 ) -> Result<bool>;
191 fn sign_digest(
192 self: Pin<&mut BeekeeperHolder>,
193 name: &str,
194 digest_hex: &str,
195 public_key: &str,
196 prefix: &str,
197 ) -> Result<String>;
198 fn get_public_keys(
199 self: &BeekeeperHolder,
200 name: &str,
201 prefix: &str,
202 ) -> Result<Vec<String>>;
203 fn encrypt_data(
204 self: Pin<&mut BeekeeperHolder>,
205 name: &str,
206 from_key: &str,
207 to_key: &str,
208 content: &str,
209 prefix: &str,
210 nonce: u64,
211 ) -> Result<String>;
212 fn decrypt_data(
213 self: Pin<&mut BeekeeperHolder>,
214 name: &str,
215 from_key: &str,
216 to_key: &str,
217 encrypted_content: &str,
218 prefix: &str,
219 ) -> Result<String>;
220 }
221}