Skip to content

Commit

Permalink
Update rust-bitcoin, rust-elements and rust-electrum-client
Browse files Browse the repository at this point in the history
- Introduced new BEScript, BETxid and BEBlockHash enums to encapsulate rust-elements's new data types.

- The new data types will BREAK the existing RawCache database, which will be automatically re-populated from the Electrum server. Care has been taken to retain compatibility with the RawStore database (containing the user's settings and tx labels).

- Added support for Signet.
  • Loading branch information
shesek committed Mar 2, 2021
1 parent 7a2fc61 commit 758597e
Show file tree
Hide file tree
Showing 23 changed files with 572 additions and 203 deletions.
6 changes: 3 additions & 3 deletions subprojects/gdk_rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["staticlib"]
android_log = ["android_logger"]

[dependencies]
bitcoin = { version = "0.25.2", features = [ "use-serde" ] }
bitcoin = { version = "0.26.0", features = [ "use-serde" ] }
rand = "0.7.3"
gdk-electrum = { path = "gdk_electrum", features = ["android_log"] }
gdk-common = { path = "gdk_common" }
Expand All @@ -30,8 +30,8 @@ ureq = { version = "1.0.0", features = ["json"] }
[dev-dependencies]
tempdir = "0.3.7"
bitcoincore-rpc = { git="https://github.com/romanz/rust-bitcoincore-rpc", rev="637e895a0b73ffc21c8f62fa2c65b6fd1247785a" }
electrum-client = "0.4.0-beta.1"
elements = { version = "0.13", features = ["serde-feature"] }
electrum-client = "0.7.0"
elements = { version = "0.16", features = ["serde-feature"] }

[profile.release]
lto = true
Expand Down
4 changes: 2 additions & 2 deletions subprojects/gdk_rust/gdk_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ serde_cbor = "0.11.1"
libc = "0.2"
hex = "0.4.0"
rand = "0.7.3"
bitcoin = { version = "0.25", features = ["use-serde"] }
elements = { version = "0.13", features = ["serde-feature"] }
bitcoin = { version = "0.26", features = ["use-serde"] }
elements = { version = "0.16", features = ["serde-feature"] }

8 changes: 4 additions & 4 deletions subprojects/gdk_rust/gdk_common/src/be/address.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::Script;
use super::BEScript;

#[derive(Debug)]
pub enum BEAddress {
Expand All @@ -7,10 +7,10 @@ pub enum BEAddress {
}

impl BEAddress {
pub fn script_pubkey(&self) -> Script {
pub fn script_pubkey(&self) -> BEScript {
match self {
BEAddress::Bitcoin(addr) => addr.script_pubkey(),
BEAddress::Elements(addr) => addr.script_pubkey(),
BEAddress::Bitcoin(addr) => addr.script_pubkey().into(),
BEAddress::Elements(addr) => addr.script_pubkey().into(),
}
}
pub fn blinding_pubkey(&self) -> Option<bitcoin::secp256k1::PublicKey> {
Expand Down
34 changes: 34 additions & 0 deletions subprojects/gdk_rust/gdk_common/src/be/blockhash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use bitcoin::hashes::hex::ToHex;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum BEBlockHash {
Bitcoin(bitcoin::BlockHash),
Elements(elements::BlockHash),
}

impl BEBlockHash {
pub fn to_hex(&self) -> String {
match self {
Self::Bitcoin(blockhash) => blockhash.to_hex(),
Self::Elements(blockhash) => blockhash.to_hex(),
}
}
}

impl ToString for BEBlockHash {
fn to_string(&self) -> String {
match self {
Self::Bitcoin(blockhash) => blockhash.to_string(),
Self::Elements(blockhash) => blockhash.to_string(),
}
}
}

// We must have a default for Store, so we use bitcoin::BlockHash which
// will be replaced with the proper type after the first sync.
impl Default for BEBlockHash {
fn default() -> Self {
Self::Bitcoin(Default::default())
}
}
11 changes: 5 additions & 6 deletions subprojects/gdk_rust/gdk_common/src/be/blockheader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::NetworkId;
use bitcoin::BlockHash;
use serde::{Deserialize, Serialize};

use super::BEBlockHash;

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub enum BEBlockHeader {
Expand Down Expand Up @@ -31,18 +32,16 @@ impl BEBlockHeader {
}
}

pub fn block_hash(&self) -> BlockHash {
pub fn block_hash(&self) -> BEBlockHash {
match self {
Self::Bitcoin(header) => header.block_hash(),
Self::Elements(header) => header.block_hash(),
Self::Bitcoin(header) => BEBlockHash::Bitcoin(header.block_hash()),
Self::Elements(header) => BEBlockHash::Elements(header.block_hash()),
}
}
}

#[cfg(test)]
mod test {
use crate::be::BEBlockHeader;
use elements::bitcoin::Block;
use elements::encode::deserialize;
use elements::BlockHeader;

Expand Down
13 changes: 9 additions & 4 deletions subprojects/gdk_rust/gdk_common/src/be/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use std::convert::TryInto;

mod address;
mod blockhash;
mod blockheader;
mod outpoint;
mod script;
mod transaction;
mod txid;

pub use address::*;
use bitcoin::hashes::core::fmt::Formatter;
use bitcoin::util::bip32::DerivationPath;
use bitcoin::Script;
pub use blockhash::*;
pub use blockheader::*;
pub use outpoint::*;
pub use script::*;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
pub use transaction::*;
pub use txid::*;

pub type AssetId = [u8; 32]; // TODO use elements::issuance::AssetId
pub type Utxos = Vec<(BEOutPoint, UTXOInfo)>;
Expand All @@ -22,7 +27,7 @@ pub type Utxos = Vec<(BEOutPoint, UTXOInfo)>;
pub struct UTXOInfo {
pub asset: String,
pub value: u64,
pub script: Script,
pub script: BEScript,
pub height: Option<u32>,
pub path: DerivationPath,
}
Expand All @@ -31,7 +36,7 @@ impl UTXOInfo {
pub fn new(
asset: String,
value: u64,
script: Script,
script: BEScript,
height: Option<u32>,
path: DerivationPath,
) -> Self {
Expand Down Expand Up @@ -81,7 +86,7 @@ pub fn asset_to_hex(asset: &[u8]) -> String {
#[derive(Default)]
pub struct ScriptBatch {
pub cached: bool,
pub value: Vec<(Script, DerivationPath)>,
pub value: Vec<(BEScript, DerivationPath)>,
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions subprojects/gdk_rust/gdk_common/src/be/outpoint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bitcoin::Txid;
use super::BETxid;

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum BEOutPoint {
Expand All @@ -19,24 +19,24 @@ impl From<elements::OutPoint> for BEOutPoint {
}

impl BEOutPoint {
pub fn new_bitcoin(txid: Txid, vout: u32) -> Self {
pub fn new_bitcoin(txid: bitcoin::Txid, vout: u32) -> Self {
BEOutPoint::Bitcoin(bitcoin::OutPoint {
txid,
vout,
})
}

pub fn new_elements(txid: Txid, vout: u32) -> Self {
pub fn new_elements(txid: elements::Txid, vout: u32) -> Self {
BEOutPoint::Elements(elements::OutPoint {
txid,
vout,
})
}

pub fn txid(&self) -> bitcoin::Txid {
pub fn txid(&self) -> BETxid {
match self {
Self::Bitcoin(outpoint) => outpoint.txid,
Self::Elements(outpoint) => outpoint.txid,
Self::Bitcoin(outpoint) => outpoint.txid.into(),
Self::Elements(outpoint) => outpoint.txid.into(),
}
}

Expand Down
140 changes: 140 additions & 0 deletions subprojects/gdk_rust/gdk_common/src/be/script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use bitcoin::hashes::hex::ToHex;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum BEScript {
Bitcoin(bitcoin::Script),
Elements(elements::Script),
}

impl BEScript {
pub fn to_hex(&self) -> String {
match self {
Self::Bitcoin(script) => script.to_hex(),
Self::Elements(script) => script.to_hex(),
}
}

pub fn is_empty(&self) -> bool {
match self {
Self::Bitcoin(script) => script.is_empty(),
Self::Elements(script) => script.is_empty(),
}
}
pub fn ref_bitcoin(&self) -> Option<&bitcoin::Script> {
match self {
Self::Bitcoin(script) => Some(script),
Self::Elements(_) => None,
}
}
pub fn ref_elements(&self) -> Option<&elements::Script> {
match self {
Self::Bitcoin(_) => None,
Self::Elements(script) => Some(script),
}
}
}

pub trait BEScriptConvert {
fn into_bitcoin(self) -> bitcoin::Script;
fn into_elements(self) -> elements::Script;
fn into_be(self) -> BEScript;
}

impl BEScriptConvert for BEScript {
fn into_bitcoin(self) -> bitcoin::Script {
match self {
Self::Bitcoin(script) => script,
Self::Elements(script) => script.into_bitcoin(),
}
}
fn into_elements(self) -> elements::Script {
match self {
Self::Bitcoin(script) => script.into_elements(),
Self::Elements(script) => script,
}
}
fn into_be(self) -> Self {
self
}
}

impl BEScriptConvert for bitcoin::Script {
fn into_bitcoin(self) -> bitcoin::Script {
self
}
fn into_elements(self) -> elements::Script {
elements::Script::from(self.into_bytes())
}
fn into_be(self) -> BEScript {
self.into()
}
}

impl BEScriptConvert for elements::Script {
fn into_bitcoin(self) -> bitcoin::Script {
bitcoin::Script::from(self.into_bytes())
}
fn into_elements(self) -> elements::Script {
self
}
fn into_be(self) -> BEScript {
self.into()
}
}

impl BEScriptConvert for &elements::Script {
fn into_bitcoin(self) -> bitcoin::Script {
self.clone().into_bitcoin()
}
fn into_elements(self) -> elements::Script {
self.clone().into_elements()
}
fn into_be(self) -> BEScript {
self.clone().into()
}
}

impl BEScriptConvert for &bitcoin::Script {
fn into_bitcoin(self) -> bitcoin::Script {
self.clone().into_bitcoin()
}
fn into_elements(self) -> elements::Script {
self.clone().into_elements()
}
fn into_be(self) -> BEScript {
self.clone().into()
}
}

impl ToString for BEScript {
fn to_string(&self) -> String {
match self {
BEScript::Bitcoin(script) => script.to_string(),
BEScript::Elements(script) => script.to_string(),
}
}
}

impl From<bitcoin::Script> for BEScript {
fn from(script: bitcoin::Script) -> BEScript {
BEScript::Bitcoin(script)
}
}

impl From<elements::Script> for BEScript {
fn from(script: elements::Script) -> BEScript {
BEScript::Elements(script)
}
}
impl From<&bitcoin::Script> for BEScript {
fn from(script: &bitcoin::Script) -> BEScript {
script.clone().into()
}
}

impl From<&elements::Script> for BEScript {
fn from(script: &elements::Script) -> BEScript {
script.clone().into()
}
}
Loading

0 comments on commit 758597e

Please sign in to comment.