Skip to content

Commit ded22f3

Browse files
committed
Update manually-written bindings types to latest rust-bitcoin API
1 parent e6ed034 commit ded22f3

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

lightning-c-bindings/src/bitcoin/network.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A C-mapped version fo bitcoin::network::constants::Network
22
3-
use bitcoin::network::constants::Network as BitcoinNetwork;
3+
use bitcoin::Network as BitcoinNetwork;
44

55
#[repr(C)]
66
/// An enum representing the possible Bitcoin or test networks which we can run on

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ pub mod derived;
66
use bitcoin::Transaction as BitcoinTransaction;
77
use bitcoin::Witness as BitcoinWitness;
88
use bitcoin::address;
9-
use bitcoin::address::WitnessProgram as BitcoinWitnessProgram;
9+
use bitcoin::WitnessProgram as BitcoinWitnessProgram;
10+
use bitcoin::WitnessVersion as BitcoinWitnessVersion;
1011
use bitcoin::key::TweakedPublicKey as BitcoinTweakedPublicKey;
1112
use bitcoin::key::XOnlyPublicKey;
1213
use bitcoin::hashes::Hash;
@@ -18,16 +19,13 @@ use bitcoin::secp256k1::Error as SecpError;
1819
use bitcoin::secp256k1::ecdsa::RecoveryId;
1920
use bitcoin::secp256k1::ecdsa::RecoverableSignature as SecpRecoverableSignature;
2021
use bitcoin::secp256k1::Scalar as SecpScalar;
21-
use bitcoin::bech32;
22+
use bech32;
2223

2324
use core::convert::TryInto; // Bindings need at least rustc 1.34
2425
use alloc::borrow::ToOwned;
2526
use core::ffi::c_void;
2627

27-
#[cfg(feature = "std")]
28-
pub(crate) use std::io::{self, Cursor, Read};
29-
#[cfg(feature = "no-std")]
30-
pub(crate) use core2::io::{self, Cursor, Read};
28+
pub(crate) use bitcoin::io::{self, Cursor, Read};
3129
#[cfg(feature = "no-std")]
3230
use alloc::{boxed::Box, vec::Vec, string::String};
3331

@@ -90,12 +88,12 @@ impl Into<u128> for U128 {
9088
#[repr(C)]
9189
pub struct WitnessVersion(u8);
9290

93-
impl From<address::WitnessVersion> for WitnessVersion {
94-
fn from(o: address::WitnessVersion) -> Self { Self(o.to_num()) }
91+
impl From<BitcoinWitnessVersion> for WitnessVersion {
92+
fn from(o: BitcoinWitnessVersion) -> Self { Self(o.to_num()) }
9593
}
96-
impl Into<address::WitnessVersion> for WitnessVersion {
97-
fn into(self) -> address::WitnessVersion {
98-
address::WitnessVersion::try_from(self.0).expect("WitnessVersion objects must be in the range 0..=16")
94+
impl Into<BitcoinWitnessVersion> for WitnessVersion {
95+
fn into(self) -> BitcoinWitnessVersion {
96+
BitcoinWitnessVersion::try_from(self.0).expect("WitnessVersion objects must be in the range 0..=16")
9997
}
10098
}
10199

@@ -116,7 +114,7 @@ impl WitnessProgram {
116114
pub(crate) fn into_bitcoin(mut self) -> BitcoinWitnessProgram {
117115
BitcoinWitnessProgram::new(
118116
self.version.into(),
119-
self.program.into_rust(),
117+
self.program.as_slice(),
120118
).expect("Program length was previously checked")
121119
}
122120
}
@@ -322,6 +320,8 @@ pub enum Secp256k1Error {
322320
InvalidPublicKeySum,
323321
/// The only valid parity values are 0 or 1.
324322
InvalidParityValue,
323+
/// Invalid Elligator Swift Value
324+
InvalidEllSwift,
325325
}
326326
impl Secp256k1Error {
327327
pub(crate) fn from_rust(err: SecpError) -> Self {
@@ -336,6 +336,7 @@ impl Secp256k1Error {
336336
SecpError::InvalidTweak => Secp256k1Error::InvalidTweak,
337337
SecpError::NotEnoughMemory => Secp256k1Error::NotEnoughMemory,
338338
SecpError::InvalidPublicKeySum => Secp256k1Error::InvalidPublicKeySum,
339+
SecpError::InvalidEllSwift => Secp256k1Error::InvalidEllSwift,
339340
SecpError::InvalidParityValue(_) => Secp256k1Error::InvalidParityValue,
340341
}
341342
}
@@ -352,6 +353,7 @@ impl Secp256k1Error {
352353
Secp256k1Error::InvalidTweak => SecpError::InvalidTweak,
353354
Secp256k1Error::NotEnoughMemory => SecpError::NotEnoughMemory,
354355
Secp256k1Error::InvalidPublicKeySum => SecpError::InvalidPublicKeySum,
356+
Secp256k1Error::InvalidEllSwift => SecpError::InvalidEllSwift,
355357
Secp256k1Error::InvalidParityValue => SecpError::InvalidParityValue(invalid_parity),
356358
}
357359
}
@@ -460,12 +462,15 @@ impl IOError {
460462
io::ErrorKind::Interrupted => IOError::Interrupted,
461463
io::ErrorKind::Other => IOError::Other,
462464
io::ErrorKind::UnexpectedEof => IOError::UnexpectedEof,
463-
_ => IOError::Other,
464465
}
465466
}
466-
pub(crate) fn from_rust(err: io::Error) -> Self {
467+
pub(crate) fn from_bitcoin(err: io::Error) -> Self {
467468
Self::from_rust_kind(err.kind())
468469
}
470+
#[cfg(feature = "std")]
471+
pub(crate) fn from_rust(err: std::io::Error) -> Self {
472+
Self::from_bitcoin(err.into())
473+
}
469474
pub(crate) fn to_rust_kind(&self) -> io::ErrorKind {
470475
match self {
471476
IOError::NotFound => io::ErrorKind::NotFound,
@@ -701,13 +706,13 @@ impl TxOut {
701706
pub(crate) fn into_rust(mut self) -> ::bitcoin::blockdata::transaction::TxOut {
702707
::bitcoin::blockdata::transaction::TxOut {
703708
script_pubkey: self.script_pubkey.into_rust().into(),
704-
value: self.value,
709+
value: bitcoin::Amount::from_sat(self.value),
705710
}
706711
}
707712
pub(crate) fn from_rust(txout: &::bitcoin::blockdata::transaction::TxOut) -> Self {
708713
Self {
709714
script_pubkey: derived::CVec_u8Z::from(txout.script_pubkey.clone().into_bytes()),
710-
value: txout.value
715+
value: txout.value.to_sat()
711716
}
712717
}
713718
}
@@ -764,7 +769,7 @@ impl u8slice {
764769
}
765770
pub(crate) fn reader_to_vec<R: Read>(r: &mut R) -> derived::CVec_u8Z {
766771
let mut res = Vec::new();
767-
r.read_to_end(&mut res).unwrap();
772+
r.read_to_limit(&mut res, u64::MAX).unwrap();
768773
derived::CVec_u8Z::from(res)
769774
}
770775

@@ -873,6 +878,12 @@ impl Str {
873878
pub(crate) fn into_pathbuf(mut self) -> std::path::PathBuf {
874879
std::path::PathBuf::from(self.into_string())
875880
}
881+
pub(crate) fn from_rust(s: &str) -> Self {
882+
s.into()
883+
}
884+
pub(crate) fn is_empty(&self) -> bool {
885+
self.len == 0
886+
}
876887
}
877888
impl Into<Str> for String {
878889
fn into(self) -> Str {

0 commit comments

Comments
 (0)