Skip to content

Commit

Permalink
temp 1
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Oct 23, 2023
1 parent 7463fa7 commit 5c543ac
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 37 deletions.
69 changes: 41 additions & 28 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,16 @@ interface Wallet {

[Throws=BdkError]
void apply_update(Update update);
};

interface Update {};

// ------------------------------------------------------------------------
// bdk crate - bitcoin reexports
// ------------------------------------------------------------------------

enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
record<KeychainKind, sequence<IndexedScript>> get_all_spks();
};

enum WordCount {
"Words12",
"Words15",
"Words18",
"Words21",
"Words24",
dictionary IndexedScript {
u32 index;
Script script;
};

interface Address {
[Throws=BdkError]
constructor(string address, Network network);

Network network();

string to_qr_uri();

string as_string();
};
interface Update {};

// ------------------------------------------------------------------------
// bdk crate - descriptor module
Expand Down Expand Up @@ -209,3 +186,39 @@ interface Descriptor {
interface EsploraClient {
constructor(string url);
};

// ------------------------------------------------------------------------
// bdk crate - bitcoin reexports
// ------------------------------------------------------------------------

interface Script {
constructor(sequence<u8> raw_output_script);

sequence<u8> to_bytes();
};

enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
};

enum WordCount {
"Words12",
"Words15",
"Words18",
"Words21",
"Words24",
};

interface Address {
[Throws=BdkError]
constructor(string address, Network network);

Network network();

string to_qr_uri();

string as_string();
};
22 changes: 22 additions & 0 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;

/// A Bitcoin script.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Script(pub(crate) BdkScriptBuf);

impl Script {
pub fn new(raw_output_script: Vec<u8>) -> Self {
let script: BdkScriptBuf = BdkScriptBuf::from(raw_output_script);
Script(script)
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}
}

impl From<BdkScriptBuf> for Script {
fn from(script: BdkScriptBuf) -> Self {
Script(script)
}
}
4 changes: 3 additions & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod esplora;
mod keys;
mod psbt;
mod wallet;
mod bitcoin;

use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk::bitcoin::Address as BdkAddress;
Expand All @@ -24,7 +25,8 @@ use crate::keys::Mnemonic;
use crate::wallet::Update;
use crate::wallet::Wallet;
use bdk::keys::bip39::WordCount;
// use bdk_esplora::EsploraExt;
use crate::bitcoin::Script;
use crate::wallet::IndexedScript;

uniffi::include_scaffolding!("bdk");

Expand Down
55 changes: 53 additions & 2 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;
use crate::descriptor::Descriptor;
use crate::Balance;
use crate::{AddressIndex, AddressInfo, Network};
use bdk::wallet::Update as BdkUpdate;
use bdk::Error as BdkError;
use bdk::{Error as BdkError, KeychainKind};
use bdk::Wallet as BdkWallet;
use std::sync::{Arc, Mutex, MutexGuard};
use crate::bitcoin::Script;

#[derive(Debug)]
pub struct Wallet {
Expand Down Expand Up @@ -68,13 +70,40 @@ impl Wallet {
.map_err(|e| BdkError::Generic(e.to_string()))
}

pub fn get_all_spks(&self) -> HashMap<KeychainKind, Vec<IndexedScript>> {
self
.get_wallet()
.spks_of_all_keychains()
.into_iter()
.fold(
HashMap::new(),
|mut map, (keychain, iter)| {
dbg!();
let vec = iter.map(|(index, scriptbuf)| {
dbg!();
let script: Arc<Script> = Arc::new(scriptbuf.into());
IndexedScript { index, script }
}).collect();
dbg!(&vec);
map.insert(keychain, vec);
map
}
)
}

// pub fn commit(&self) -> Result<(), BdkError> {}

// fn is_mine(&self, script: Arc<Script>) -> bool {
// self.get_wallet().is_mine(&script.inner)
// }
}

#[derive(Debug)]
pub struct IndexedScript {
pub index: u32,
pub script: Arc<Script>
}

pub struct Update(pub(crate) BdkUpdate);

// /// A Bitcoin wallet.
Expand Down Expand Up @@ -639,7 +668,29 @@ pub struct Update(pub(crate) BdkUpdate);
// .map(Arc::new)
// }
// }
//

#[cfg(test)]
mod test {
use std::sync::Arc;
use crate::descriptor::Descriptor;
use crate::Network;
use crate::wallet::Wallet;

#[test]
fn test_get_spks() {
let descriptor = Descriptor::new(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)".to_string(),
Network::Testnet
).unwrap();
let wallet = Wallet::new_no_persist(
Arc::new(descriptor),
None,
Network::Testnet
).unwrap();
let spks = wallet.get_all_spks();
}
}

// // The goal of these tests to to ensure `bdk-ffi` intermediate code correctly calls `bdk` APIs.
// // These tests should not be used to verify `bdk` behavior that is already tested in the `bdk`
// // crate.
Expand Down
17 changes: 11 additions & 6 deletions bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/JvmLibTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package org.bitcoindevkit

import org.junit.Assert.*
import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Files

class WalletTest {
@Test
Expand All @@ -24,7 +19,7 @@ class WalletTest {
fun testUsedWallet() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val (index, address, keychain) = wallet.getAddress(AddressIndex.LastUnused)
val (index, address, keychain) = wallet.getAddress(AddressIndex.LastUnused)
println("Address ${address.asString()} at index $index")
}

Expand All @@ -36,6 +31,16 @@ class WalletTest {
assert(wallet.getBalance().total() == 0uL)
}

@Test
fun testSpks() {
val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
val wallet = Wallet.newNoPersist(descriptor, null, Network.TESTNET)
val (index, address, keychain) = wallet.getAddress(AddressIndex.LastUnused)
//
val spks: Map<KeychainKind, List<IndexedScript>> = wallet.getAllSpks()
// println(spks)
}

// @Test
// fun testSyncedBalance() {
// val descriptor = Descriptor("wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", Network.TESTNET)
Expand Down

0 comments on commit 5c543ac

Please sign in to comment.