Add strings example
This commit is contained in:
parent
ec9d2ea284
commit
49126a8943
34
main.c
34
main.c
@ -1,13 +1,35 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
67
src/lib.rs
67
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<repr_c::Box<Point>>, b: Option<repr_c::Box<Point>>) -> repr_c::Box<Point> {
|
||||
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<repr_c::Box<Point>>) {
|
||||
fn print_point(point: Point) {
|
||||
println!("{:?}", point);
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
fn new_point(x: f64, y: f64) -> repr_c::Box<Point> {
|
||||
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<repr_c::Box<Point>>) {
|
||||
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<WalletPtr>) {
|
||||
println!("before sync");
|
||||
wallet.raw.sync(log_progress(), Some(100));
|
||||
println!("after sync");
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
fn new_address( wallet: repr_c::Box<WalletPtr>) -> 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]
|
||||
|
Loading…
x
Reference in New Issue
Block a user