From 49126a8943d14bdcf054e7ca77991d4fc9393b50 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Fri, 4 Jun 2021 18:03:34 -0700 Subject: [PATCH] Add strings example --- main.c | 34 ++++++++++++++++++++++----- src/lib.rs | 67 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index 7321133..1a82336 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,35 @@ #include - +#include #include "bdk_ffi.h" int main (int argc, char const * const argv[]) { - Point_t * a = new_point(84,45); - Point_t * b = new_point(0.0,39.0); - Point_t * m = mid_point(a, b); - print_point(m); - print_point(NULL); + + char const * string1 = "string1"; + char const * string2 = "string2"; + char * string3 = concat_string(string1, string2); + + print_string(string3); + print_string(string3); + free_string(string3); + + //Point_t a = new_point(84,45); + //Point_t b = new_point(0,39); + //Point_t m = mid_point(a, b); + //print_point(m); + + //char const * name = "test_wallet"; + //char const * desc = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)"; + //char const * change = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"; + + //printf("wallet name: %s\n", name); + //printf("descriptor: %s\n", desc); + //printf("change descriptor: %s\n", change); + //WalletPtr_t * wallet = new_wallet(name, desc, change); + //sync_wallet(wallet); + + //char const * address = new_address(wallet); + //printf("new address: %s\n", address); + return EXIT_SUCCESS; } diff --git a/src/lib.rs b/src/lib.rs index 537fc6c..37d8e19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,41 @@ -use ::safer_ffi::prelude::*; +#![deny(unsafe_code)] /* No `unsafe` needed! */ + +//use ::safer_ffi::prelude::*; use bdk::bitcoin::network::constants::Network::Testnet; use bdk::blockchain::{ElectrumBlockchain, log_progress}; use bdk::electrum_client::Client; use bdk::sled; use bdk::sled::Tree; use bdk::Wallet; +use bdk::wallet::AddressIndex::New; +use safer_ffi::char_p::{char_p_ref, char_p_boxed}; +use ::safer_ffi::prelude::*; + +/// Concatenate two input UTF-8 (_e.g._, ASCII) strings. +/// +/// \remark The returned string must be freed with `rust_free_string` +#[ffi_export] +fn concat_string<'a>(fst: char_p_ref<'a>, snd: char_p_ref<'a>) + -> char_p_boxed +{ + let fst = fst.to_str(); // : &'_ str + let snd = snd.to_str(); // : &'_ str + let ccat = format!("{}{}", fst, snd).try_into().unwrap(); + ccat +} + +#[ffi_export] +fn print_string (string: char_p_ref) +{ + println!("{}", string); +} + +/// Frees a Rust-allocated string. +#[ffi_export] +fn free_string (string: char_p_boxed) +{ + drop(string) +} /// A `struct` usable from both Rust and C #[derive_ReprC] @@ -18,30 +49,28 @@ pub struct Point { /* Export a Rust function to the C world. */ /// Returns the middle point of `[a, b]`. #[ffi_export] -fn mid_point(a: Option>, b: Option>) -> repr_c::Box { - let a = a.unwrap(); - let b = b.unwrap(); - repr_c::Box::new(Point { +fn mid_point(a: Point, b: Point) -> Point { + Point { x: (a.x + b.x) / 2., y: (a.y + b.y) / 2., - }) + } } /// Pretty-prints a point using Rust's formatting logic. #[ffi_export] -fn print_point(point: Option>) { +fn print_point(point: Point) { println!("{:?}", point); } #[ffi_export] -fn new_point(x: f64, y: f64) -> repr_c::Box { - repr_c::Box::new(Point { x, y }) +fn new_point(x: f64, y: f64) -> Point { + Point { x, y } } -#[ffi_export] -fn free_point(point: Option>) { - drop(point) -} +//#[ffi_export] +//fn free_point(point: Point) { +// drop(point) +//} #[derive_ReprC] #[ReprC::opaque] @@ -90,11 +119,21 @@ fn new_wallet( #[ffi_export] fn sync_wallet( wallet: repr_c::Box) { - println!("before sync"); wallet.raw.sync(log_progress(), Some(100)); println!("after sync"); } +#[ffi_export] +fn new_address( wallet: repr_c::Box) -> char_p::Box { + println!("before new_address"); + let new_address = wallet.raw.get_address(New); + println!("after new_address: {:?}", new_address); + let new_address = new_address.unwrap(); + let new_address = new_address.to_string(); + println!("new address: ${}", new_address); + new_address.try_into().unwrap() +} + /// The following test function is necessary for the header generation. #[::safer_ffi::cfg_headers] #[test]