Add get_height and get_block_hash methods on blockchain

This commit is contained in:
thunderbiscuit 2022-08-17 16:43:17 -04:00
parent d38737669d
commit 25963ec982
No known key found for this signature in database
GPG Key ID: 88253696EB836462
3 changed files with 22 additions and 0 deletions

View File

@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- Add `get_height()` and `get_block_hash()` methods on blockchain [#184]
[#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184
## [v0.8.0]
- Update BDK to version 0.20.0 [#169]

View File

@ -143,6 +143,12 @@ interface Blockchain {
[Throws=BdkError]
void broadcast([ByRef] PartiallySignedBitcoinTransaction psbt);
[Throws=BdkError]
u32 get_height();
[Throws=BdkError]
string get_block_hash(u32 height);
};
callback interface Progress {

View File

@ -3,6 +3,8 @@ use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{Address, Network, OutPoint as BdkOutPoint, Script, Txid};
use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig};
use bdk::blockchain::GetBlockHash;
use bdk::blockchain::GetHeight;
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
};
@ -169,6 +171,16 @@ impl Blockchain {
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
self.get_blockchain().broadcast(&tx)
}
fn get_height(&self) -> Result<u32, Error> {
self.get_blockchain().get_height()
}
fn get_block_hash(&self, height: u32) -> Result<String, Error> {
self.get_blockchain()
.get_block_hash(u64::from(height))
.map(|hash| hash.to_string())
}
}
struct Wallet {