webylib 0.2.1

Webcash HD wallet library — bearer e-cash with BIP32-style key derivation, SQLite storage, AES-256-GCM encryption, and full C FFI for cross-platform SDKs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! FFI exports for wallet operations.
//!
//! Every function returns an `i32` error code (0 = success).
//! On failure, call `weby_last_error_message()` for a human-readable description.
//! Strings returned via `out_*` pointers must be freed with `weby_free_string()`.

use std::ffi::c_char;

use super::error::{result_to_code, set_last_error, WebyErrorCode};
use super::types::{cstr_to_str, str_to_cstring};
use crate::wallet::Wallet;

/// Opaque wallet handle.
pub struct WebyWallet {
    wallet: Wallet,
    runtime: tokio::runtime::Runtime,
}

// ── Lifecycle ───────────────────────────────────────────────────────

/// Open or create a wallet at `path`.
///
/// On success, writes the wallet handle to `*out_wallet`.
/// The caller must eventually free it with `weby_wallet_free`.
///
/// # Safety
/// - `path` must be a valid null-terminated UTF-8 string.
/// - `out_wallet` must be a valid, non-null pointer.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_open(
    path: *const c_char,
    out_wallet: *mut *mut WebyWallet,
) -> i32 {
    if out_wallet.is_null() {
        set_last_error("out_wallet is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let path_str = match unsafe { cstr_to_str(path) } {
        Some(s) => s,
        None => {
            set_last_error("path is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let rt = match tokio::runtime::Runtime::new() {
        Ok(rt) => rt,
        Err(e) => {
            set_last_error(&format!("Failed to create async runtime: {}", e));
            return WebyErrorCode::Unknown as i32;
        }
    };
    let result = rt.block_on(Wallet::open(path_str));
    let code = result_to_code(&result);
    if let Ok(wallet) = result {
        let handle = Box::new(WebyWallet { wallet, runtime: rt });
        unsafe { *out_wallet = Box::into_raw(handle) };
    }
    code
}

/// Open or create a wallet with a caller-provided 32-byte seed.
///
/// # Safety
/// - `path` must be a valid null-terminated UTF-8 string.
/// - `seed_ptr` must point to at least 32 bytes.
/// - `out_wallet` must be a valid, non-null pointer.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_open_with_seed(
    path: *const c_char,
    seed_ptr: *const u8,
    seed_len: usize,
    out_wallet: *mut *mut WebyWallet,
) -> i32 {
    if out_wallet.is_null() || seed_ptr.is_null() || seed_len != 32 {
        set_last_error("Invalid arguments: need non-null pointers and seed_len=32");
        return WebyErrorCode::InvalidInput as i32;
    }
    let path_str = match unsafe { cstr_to_str(path) } {
        Some(s) => s,
        None => {
            set_last_error("path is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let mut seed = [0u8; 32];
    unsafe { std::ptr::copy_nonoverlapping(seed_ptr, seed.as_mut_ptr(), 32) };

    let rt = match tokio::runtime::Runtime::new() {
        Ok(rt) => rt,
        Err(e) => {
            set_last_error(&format!("Failed to create async runtime: {}", e));
            return WebyErrorCode::Unknown as i32;
        }
    };
    let result = rt.block_on(Wallet::open_with_seed(path_str, &seed));
    let code = result_to_code(&result);
    if let Ok(wallet) = result {
        let handle = Box::new(WebyWallet { wallet, runtime: rt });
        unsafe { *out_wallet = Box::into_raw(handle) };
    }
    code
}

/// Free a wallet handle.
///
/// # Safety
/// `wallet` must have been returned by `weby_wallet_open*` or be NULL (no-op).
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_free(wallet: *mut WebyWallet) {
    if !wallet.is_null() {
        drop(unsafe { Box::from_raw(wallet) });
    }
}

// ── Operations ──────────────────────────────────────────────────────

/// Get the wallet balance as a decimal string (e.g., "1.50000000").
///
/// On success, writes a C string to `*out_balance`. Free with `weby_free_string`.
///
/// # Safety
/// - `wallet` must be a valid handle from `weby_wallet_open*`.
/// - `out_balance` must be a valid, non-null pointer.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_balance(
    wallet: *const WebyWallet,
    out_balance: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_balance.is_null() {
        set_last_error("wallet or out_balance is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let handle = unsafe { &*wallet };
    let result = handle.runtime.block_on(handle.wallet.balance());
    let code = result_to_code(&result);
    if let Ok(balance) = result {
        unsafe { *out_balance = str_to_cstring(&balance) };
    }
    code
}

/// Insert webcash into the wallet (ownership transfer via server).
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `webcash_str` must be a valid null-terminated webcash string (e.g., "e1:secret:abc...").
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_insert(
    wallet: *const WebyWallet,
    webcash_str: *const c_char,
) -> i32 {
    if wallet.is_null() {
        set_last_error("wallet is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let wc_str = match unsafe { cstr_to_str(webcash_str) } {
        Some(s) => s,
        None => {
            set_last_error("webcash_str is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let webcash = match crate::webcash::SecretWebcash::parse(wc_str) {
        Ok(wc) => wc,
        Err(e) => {
            set_last_error(&e.to_string());
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let handle = unsafe { &*wallet };
    let result = handle.runtime.block_on(handle.wallet.insert(webcash));
    result_to_code(&result)
}

/// Pay amount from the wallet. Returns payment webcash string for recipient.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `amount_str` must be a valid decimal amount string (e.g., "1.5").
/// - `memo` must be a valid null-terminated string (or NULL for empty).
/// - `out_webcash` must be a valid, non-null pointer. Free result with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_pay(
    wallet: *const WebyWallet,
    amount_str: *const c_char,
    memo: *const c_char,
    out_webcash: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_webcash.is_null() {
        set_last_error("wallet or out_webcash is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let amt = match unsafe { cstr_to_str(amount_str) } {
        Some(s) => match s.parse::<crate::Amount>() {
            Ok(a) => a,
            Err(e) => {
                set_last_error(&e.to_string());
                return WebyErrorCode::InvalidInput as i32;
            }
        },
        None => {
            set_last_error("amount_str is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let memo_str = unsafe { cstr_to_str(memo) }.unwrap_or("");
    let handle = unsafe { &*wallet };
    let result = handle.runtime.block_on(handle.wallet.pay(amt, memo_str));
    let code = result_to_code(&result);
    if let Ok(msg) = result {
        unsafe { *out_webcash = str_to_cstring(&msg) };
    }
    code
}

/// Check wallet status against server.
///
/// # Safety
/// `wallet` must be a valid handle.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_check(wallet: *const WebyWallet) -> i32 {
    if wallet.is_null() {
        set_last_error("wallet is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let handle = unsafe { &*wallet };
    let result = handle.runtime.block_on(handle.wallet.check());
    result_to_code(&result)
}

/// Merge small outputs to reduce fragmentation.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `out_result` must be a valid, non-null pointer. Free with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_merge(
    wallet: *const WebyWallet,
    max_outputs: u32,
    out_result: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_result.is_null() {
        set_last_error("wallet or out_result is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let handle = unsafe { &*wallet };
    let result = handle
        .runtime
        .block_on(handle.wallet.merge(max_outputs as usize));
    let code = result_to_code(&result);
    if let Ok(msg) = result {
        unsafe { *out_result = str_to_cstring(&msg) };
    }
    code
}

/// Recover wallet from master secret hex.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `master_secret_hex` must be a 64-character hex string.
/// - `out_result` must be a valid, non-null pointer. Free with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_recover(
    wallet: *const WebyWallet,
    master_secret_hex: *const c_char,
    gap_limit: u32,
    out_result: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_result.is_null() {
        set_last_error("wallet or out_result is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let secret = match unsafe { cstr_to_str(master_secret_hex) } {
        Some(s) => s,
        None => {
            set_last_error("master_secret_hex is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let handle = unsafe { &*wallet };
    let result = handle
        .runtime
        .block_on(handle.wallet.recover(secret, gap_limit as usize));
    let code = result_to_code(&result);
    if let Ok(r) = result {
        unsafe { *out_result = str_to_cstring(&r.to_string()) };
    }
    code
}

/// Get wallet statistics as JSON.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `out_json` must be a valid, non-null pointer. Free with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_stats(
    wallet: *const WebyWallet,
    out_json: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_json.is_null() {
        set_last_error("wallet or out_json is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let handle = unsafe { &*wallet };
    let result = handle.runtime.block_on(handle.wallet.stats());
    let code = result_to_code(&result);
    if let Ok(stats) = result {
        let json = format!(
            r#"{{"total_webcash":{},"unspent_webcash":{},"spent_webcash":{},"total_balance":"{}"}}"#,
            stats.total_webcash, stats.unspent_webcash, stats.spent_webcash, stats.total_balance
        );
        unsafe { *out_json = str_to_cstring(&json) };
    }
    code
}

/// Export wallet snapshot as JSON.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `out_json` must be a valid, non-null pointer. Free with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_export_snapshot(
    wallet: *const WebyWallet,
    out_json: *mut *mut c_char,
) -> i32 {
    if wallet.is_null() || out_json.is_null() {
        set_last_error("wallet or out_json is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let handle = unsafe { &*wallet };
    let result = handle.wallet.export_snapshot();
    let code = result_to_code(&result);
    if let Ok(snapshot) = result {
        match serde_json::to_string(&snapshot) {
            Ok(json) => unsafe { *out_json = str_to_cstring(&json) },
            Err(e) => {
                set_last_error(&e.to_string());
                return WebyErrorCode::Unknown as i32;
            }
        }
    }
    code
}

// ── Encryption ──────────────────────────────────────────────────────

/// Encrypt the wallet seed with a password.
///
/// # Safety
/// - `wallet` must be a valid handle.
/// - `password` must be a valid null-terminated string.
#[no_mangle]
pub unsafe extern "C" fn weby_wallet_encrypt_seed(
    wallet: *const WebyWallet,
    password: *const c_char,
) -> i32 {
    if wallet.is_null() {
        set_last_error("wallet is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let pw = match unsafe { cstr_to_str(password) } {
        Some(s) => s,
        None => {
            set_last_error("password is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    let handle = unsafe { &*wallet };
    let result = handle
        .runtime
        .block_on(handle.wallet.encrypt_database_with_password(pw));
    result_to_code(&result)
}

// ── Utilities ───────────────────────────────────────────────────────

/// Get the library version string.
///
/// The returned pointer is valid for the lifetime of the program — do **not** free it.
#[no_mangle]
pub extern "C" fn weby_version() -> *const c_char {
    // Use a static CStr to avoid per-call allocation
    static VERSION_CSTR: std::sync::LazyLock<std::ffi::CString> =
        std::sync::LazyLock::new(|| {
            std::ffi::CString::new(crate::protocol::VERSION).unwrap()
        });
    VERSION_CSTR.as_ptr()
}

/// Parse an amount string and return the value in wats (integer).
///
/// # Safety
/// - `amount_str` must be a valid null-terminated decimal string.
/// - `out_wats` must be a valid, non-null pointer.
#[no_mangle]
pub unsafe extern "C" fn weby_amount_parse(
    amount_str: *const c_char,
    out_wats: *mut i64,
) -> i32 {
    if out_wats.is_null() {
        set_last_error("out_wats is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let s = match unsafe { cstr_to_str(amount_str) } {
        Some(s) => s,
        None => {
            set_last_error("amount_str is null or invalid UTF-8");
            return WebyErrorCode::InvalidInput as i32;
        }
    };
    match s.parse::<crate::Amount>() {
        Ok(amount) => {
            unsafe { *out_wats = amount.wats };
            WebyErrorCode::Ok as i32
        }
        Err(e) => {
            set_last_error(&e.to_string());
            WebyErrorCode::InvalidInput as i32
        }
    }
}

/// Format wats as a decimal amount string.
///
/// # Safety
/// `out_str` must be a valid, non-null pointer. Free with `weby_free_string`.
#[no_mangle]
pub unsafe extern "C" fn weby_amount_format(
    wats: i64,
    out_str: *mut *mut c_char,
) -> i32 {
    if out_str.is_null() {
        set_last_error("out_str is null");
        return WebyErrorCode::InvalidInput as i32;
    }
    let amount = crate::Amount::from_wats(wats);
    unsafe { *out_str = str_to_cstring(&amount.to_string()) };
    WebyErrorCode::Ok as i32
}