Skip to content
This repository was archived by the owner on Oct 18, 2022. It is now read-only.

Commit f8db356

Browse files
committed
Update 'bdk' version to 0.7.0 and CHANGELOG.md
1 parent bf74e5c commit f8db356

File tree

3 files changed

+60
-51
lines changed

3 files changed

+60
-51
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- Return Transaction details data list from list_transactions
3030
- Add android unit tests
3131
- Add android tests
32-
- Update `bdk` to rev `0.4`
32+
- Update `bdk` to rev `0.7`
3333
- Add multi-thread coroutine test, fix Lib.call() request string handling
3434
- Add create and restore extended key APIs
3535

36+
#### Fixed
37+
- Fix crashes on errors
38+
3639
[unreleased]: https://github.com/bitcoindevkit/bdk-jni/compare/d08725cc...HEAD

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android_logger = "0.8"
1212
crate-type = ["dylib"]
1313

1414
[dependencies]
15-
bdk = { version = "^0.6", features = ["all-keys"] }
15+
bdk = { version = "^0.7", features = ["all-keys"] }
1616
log = "0.4.8"
1717
serde = { version = "1.0", features = ["derive"] }
1818
serde_json = "1.0"

src/lib.rs

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use serde::{Deserialize, Serialize};
1515
#[allow(unused_imports)]
1616
use log::{debug, error, info, trace};
1717

18-
use bdk::electrum_client;
1918
use bdk::sled;
2019
use bdk::Wallet;
2120
use bdk::{bitcoin, KeychainKind};
21+
use bdk::{electrum_client, SignOptions};
2222

2323
use bdk::bitcoin::secp256k1::Secp256k1;
2424
use bdk::blockchain::{noop_progress, ElectrumBlockchain};
@@ -52,7 +52,7 @@ impl<F: std::fmt::Debug, S: std::fmt::Debug> From<KotlinPair<F, S>> for (F, S) {
5252
#[derive(Debug, Deserialize)]
5353
#[serde(tag = "method", content = "params")]
5454
#[serde(rename_all = "snake_case")]
55-
enum BDKRequest {
55+
enum BdkRequest {
5656
Constructor {
5757
name: String,
5858
network: Network,
@@ -132,7 +132,7 @@ enum BDKRequest {
132132
}
133133

134134
#[derive(Debug)]
135-
enum BDKJNIError {
135+
enum BdkJniError {
136136
WalletError(bdk::Error),
137137
ElectrumClientError(bdk::electrum_client::Error),
138138
Serialization(serde_json::error::Error),
@@ -146,24 +146,24 @@ enum BDKJNIError {
146146
ExtKeyError(bdk::keys::KeyError),
147147
}
148148

149-
impl From<bdk::Error> for BDKJNIError {
149+
impl From<bdk::Error> for BdkJniError {
150150
fn from(other: bdk::Error) -> Self {
151151
match other {
152-
bdk::Error::Electrum(e) => BDKJNIError::ElectrumClientError(e),
153-
e => BDKJNIError::WalletError(e),
152+
bdk::Error::Electrum(e) => BdkJniError::ElectrumClientError(e),
153+
e => BdkJniError::WalletError(e),
154154
}
155155
}
156156
}
157157

158-
impl From<bdk::electrum_client::Error> for BDKJNIError {
158+
impl From<bdk::electrum_client::Error> for BdkJniError {
159159
fn from(other: bdk::electrum_client::Error) -> Self {
160-
BDKJNIError::ElectrumClientError(other)
160+
BdkJniError::ElectrumClientError(other)
161161
}
162162
}
163163

164-
impl From<bdk::keys::KeyError> for BDKJNIError {
164+
impl From<bdk::keys::KeyError> for BdkJniError {
165165
fn from(other: bdk::keys::KeyError) -> Self {
166-
BDKJNIError::ExtKeyError(other)
166+
BdkJniError::ExtKeyError(other)
167167
}
168168
}
169169

@@ -231,8 +231,8 @@ struct IntermediatePtr {
231231
}
232232

233233
#[allow(dead_code)]
234-
fn do_constructor_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError> {
235-
use crate::BDKRequest::*;
234+
fn do_constructor_call(req: BdkRequest) -> Result<serde_json::Value, BdkJniError> {
235+
use crate::BdkRequest::*;
236236

237237
if let Constructor {
238238
name,
@@ -245,10 +245,10 @@ fn do_constructor_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError
245245
} = req
246246
{
247247
let database =
248-
sled::open(path.clone()).map_err(|e| BDKJNIError::CantOpenDb(e, path.clone()))?;
248+
sled::open(path.clone()).map_err(|e| BdkJniError::CantOpenDb(e, path.clone()))?;
249249
let tree = database
250250
.open_tree(name.clone())
251-
.map_err(|e| BDKJNIError::CantOpenTree(e, name.clone()))?;
251+
.map_err(|e| BdkJniError::CantOpenTree(e, name.clone()))?;
252252

253253
debug!(
254254
"Database at {} name {} opened successfully",
@@ -269,9 +269,9 @@ fn do_constructor_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError
269269
)?
270270
.into();
271271

272-
serde_json::to_value(&ptr).map_err(BDKJNIError::Serialization)
272+
serde_json::to_value(&ptr).map_err(BdkJniError::Serialization)
273273
} else {
274-
Err(BDKJNIError::Unsupported(
274+
Err(BdkJniError::Unsupported(
275275
"Called `do_constructor_call` with a non-Constructor request".to_string(),
276276
))
277277
}
@@ -280,37 +280,37 @@ fn do_constructor_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError
280280
#[allow(dead_code)]
281281
fn do_wallet_call<S, D>(
282282
wallet: &Wallet<S, D>,
283-
req: BDKRequest,
284-
) -> Result<serde_json::Value, BDKJNIError>
283+
req: BdkRequest,
284+
) -> Result<serde_json::Value, BdkJniError>
285285
where
286286
S: bdk::blockchain::Blockchain,
287287
D: bdk::database::BatchDatabase,
288288
{
289-
use crate::BDKRequest::*;
289+
use crate::BdkRequest::*;
290290

291291
let resp = match req {
292292
Constructor { .. } => {
293-
return Err(BDKJNIError::Unsupported(
293+
return Err(BdkJniError::Unsupported(
294294
"Called `do_wallet_call` with a Constructor request".to_string(),
295295
))
296296
}
297297
Destructor { .. } => Ok(serde_json::Value::Null),
298298
GetNewAddress { .. } => {
299-
serde_json::to_value(&wallet.get_address(New)?).map_err(BDKJNIError::Serialization)
299+
serde_json::to_value(&wallet.get_address(New)?).map_err(BdkJniError::Serialization)
300300
}
301301
Sync { max_address, .. } => {
302302
serde_json::to_value(&wallet.sync(noop_progress(), max_address)?)
303-
.map_err(BDKJNIError::Serialization)
303+
.map_err(BdkJniError::Serialization)
304304
}
305305
ListUnspent { .. } => {
306-
serde_json::to_value(&wallet.list_unspent()?).map_err(BDKJNIError::Serialization)
306+
serde_json::to_value(&wallet.list_unspent()?).map_err(BdkJniError::Serialization)
307307
}
308308
GetBalance { .. } => {
309-
serde_json::to_value(&wallet.get_balance()?).map_err(BDKJNIError::Serialization)
309+
serde_json::to_value(&wallet.get_balance()?).map_err(BdkJniError::Serialization)
310310
}
311311
ListTransactions { include_raw, .. } => {
312312
serde_json::to_value(&wallet.list_transactions(include_raw.unwrap_or(false))?)
313-
.map_err(BDKJNIError::Serialization)
313+
.map_err(BdkJniError::Serialization)
314314
}
315315
CreateTx {
316316
fee_rate,
@@ -334,7 +334,7 @@ where
334334
Ok((Address::from_str(&a)?.script_pubkey(), v.parse()?))
335335
})
336336
.collect::<Result<Vec<_>, _>>()
337-
.map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
337+
.map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
338338

339339
let mut builder = wallet.build_tx();
340340
builder.fee_rate(FeeRate::from_sat_per_vb(fee_rate));
@@ -354,15 +354,15 @@ where
354354
.collect::<Result<Vec<_>, _>>()
355355
})
356356
.transpose()
357-
.map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
357+
.map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
358358
let unspendable: Option<Vec<OutPoint>> = unspendable
359359
.map(|u| {
360360
u.into_iter()
361361
.map(|s| s.parse())
362362
.collect::<Result<Vec<_>, _>>()
363363
})
364364
.transpose()
365-
.map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
365+
.map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
366366

367367
if let Some(utxos) = utxos {
368368
builder.add_utxos(utxos.as_slice())?;
@@ -380,7 +380,7 @@ where
380380
details,
381381
psbt: base64::encode(&serialize(&psbt)),
382382
})
383-
.map_err(BDKJNIError::Serialization)
383+
.map_err(BdkJniError::Serialization)
384384
}
385385
Sign {
386386
psbt,
@@ -394,32 +394,38 @@ where
394394
}
395395

396396
let psbt =
397-
base64::decode(&psbt).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
398-
let psbt = deserialize(&psbt).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
399-
400-
let (psbt, finalized) = wallet.sign(psbt, assume_height)?;
397+
base64::decode(&psbt).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
398+
let psbt =
399+
&mut deserialize(&psbt).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
400+
let finalized = wallet.sign(
401+
psbt,
402+
SignOptions {
403+
assume_height,
404+
..Default::default()
405+
},
406+
)?;
401407

402408
serde_json::to_value(&SignResponse {
403409
psbt: base64::encode(&serialize(&psbt)),
404410
finalized,
405411
})
406-
.map_err(BDKJNIError::Serialization)
412+
.map_err(BdkJniError::Serialization)
407413
}
408414
ExtractPsbt { psbt, .. } => {
409415
let psbt =
410-
base64::decode(&psbt).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
416+
base64::decode(&psbt).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
411417
let psbt: PartiallySignedTransaction =
412-
deserialize(&psbt).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
418+
deserialize(&psbt).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
413419

414420
Ok(json!({
415421
"transaction": serialize(&psbt.extract_tx()).to_hex(),
416422
}))
417423
}
418424
Broadcast { raw_tx, .. } => {
419425
let raw_tx: Vec<u8> =
420-
FromHex::from_hex(&raw_tx).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
426+
FromHex::from_hex(&raw_tx).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
421427
let raw_tx: Transaction =
422-
deserialize(&raw_tx).map_err(|e| BDKJNIError::Parsing(format!("{:?}", e)))?;
428+
deserialize(&raw_tx).map_err(|e| BdkJniError::Parsing(format!("{:?}", e)))?;
423429

424430
let txid = wallet.broadcast(raw_tx)?;
425431

@@ -443,12 +449,12 @@ where
443449
.map(|d| d.to_string());
444450

445451
serde_json::to_value(&PublicDescriptorsResponse { external, internal })
446-
.map_err(BDKJNIError::Serialization)
452+
.map_err(BdkJniError::Serialization)
447453
}
448-
GenerateExtendedKey { .. } => Err(BDKJNIError::Unsupported(
454+
GenerateExtendedKey { .. } => Err(BdkJniError::Unsupported(
449455
"Called `do_wallet_call` with a GenerateExtendedKey request".to_string(),
450456
)),
451-
RestoreExtendedKey { .. } => Err(BDKJNIError::Unsupported(
457+
RestoreExtendedKey { .. } => Err(BdkJniError::Unsupported(
452458
"Called `do_wallet_call` with a RestoreExtendedKey request".to_string(),
453459
)),
454460
};
@@ -457,8 +463,8 @@ where
457463
}
458464

459465
#[allow(dead_code)]
460-
fn do_key_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError> {
461-
use crate::BDKRequest::*;
466+
fn do_key_call(req: BdkRequest) -> Result<serde_json::Value, BdkJniError> {
467+
use crate::BdkRequest::*;
462468

463469
let secp = Secp256k1::new();
464470

@@ -491,7 +497,7 @@ fn do_key_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError> {
491497
fingerprint: fingerprint.to_string(),
492498
};
493499

494-
serde_json::to_value(resp).map_err(BDKJNIError::Serialization)
500+
serde_json::to_value(resp).map_err(BdkJniError::Serialization)
495501
}
496502
RestoreExtendedKey {
497503
network,
@@ -514,9 +520,9 @@ fn do_key_call(req: BDKRequest) -> Result<serde_json::Value, BDKJNIError> {
514520
fingerprint: fingerprint.to_string(),
515521
};
516522

517-
serde_json::to_value(resp).map_err(BDKJNIError::Serialization)
523+
serde_json::to_value(resp).map_err(BdkJniError::Serialization)
518524
}
519-
_ => Err(BDKJNIError::Unsupported(
525+
_ => Err(BdkJniError::Unsupported(
520526
"Called `do_key_call` with a non-keys request".to_string(),
521527
)),
522528
}
@@ -569,7 +575,7 @@ pub mod android {
569575
_: JClass,
570576
incoming_stringj: JString,
571577
) -> jstring {
572-
use crate::BDKRequest::*;
578+
use crate::BdkRequest::*;
573579

574580
android_logger::init_once(
575581
android_logger::Config::default().with_min_level(log::Level::Debug),
@@ -586,7 +592,7 @@ pub mod android {
586592
}
587593
};
588594

589-
let deser = match serde_json::from_str::<BDKRequest>(incoming_string.as_str()) {
595+
let deser = match serde_json::from_str::<BdkRequest>(incoming_string.as_str()) {
590596
Ok(req) => req,
591597
Err(e) => {
592598
return JNIError {
@@ -622,7 +628,7 @@ pub mod android {
622628

623629
result
624630
} else {
625-
Err(BDKJNIError::Unsupported(
631+
Err(BdkJniError::Unsupported(
626632
"Invalid wallet pointer".to_string(),
627633
))
628634
}

0 commit comments

Comments
 (0)