Skip to content

Commit e9a4145

Browse files
committed
Merge #175: Add to and from base64 string to pset
f42203e Add to and from base64 string to pset (Riccardo Casatta) c47e12e fix 1.48.0 build (Riccardo Casatta) Pull request description: via standard trait Display and FromStr like it's done in rust-bitcoin PSBT. Reuse rust-bitcoin base64 re-exported ACKs for top commit: delta1: ACK f42203e sanket1729: ACK f42203e Tree-SHA512: 13d69a3d815c3e937d52465e6a39c260cd388bbc0711c23b1ac2e2a2c1c63ed6759f45ebec537e3b16077b78bed4612451f20cde83a6154bad154c14f0240cc2
2 parents 1821ade + f42203e commit e9a4145

7 files changed

Lines changed: 58 additions & 18 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ json-contract = [ "serde_json" ]
1919
"secp256k1-zkp/serde",
2020
"actual-serde"
2121
]
22+
base64 = ["bitcoin/base64"]
2223

2324
[dependencies]
2425
bitcoin = "0.30.0"
@@ -37,7 +38,6 @@ serde_test = "1.0.19"
3738
serde_json = "1.0"
3839
serde_cbor = "0.8" # older than latest version to support 1.41.1
3940
bincode = "1.3"
40-
base64 = "0.13.0"
4141

4242
[[example]]
4343
name = "pset_blind_coinjoin"

contrib/test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ FEATURES="serde"
44

55
# Pin dependencies as required if we are using MSRV toolchain.
66
if cargo --version | grep "1\.48"; then
7-
cargo update -p once_cell --precise 1.13.1
8-
cargo update -p quote --precise 1.0.28
9-
cargo update -p proc-macro2 --precise 1.0.63
107
cargo update -p serde_json --precise 1.0.99
118
# 1.0.157 uses syn 2.0 which requires edition 2018
129
cargo update -p serde --precise 1.0.156
10+
cargo update -p once_cell --precise 1.13.1
11+
cargo update -p quote --precise 1.0.28
12+
cargo update -p proc-macro2 --precise 1.0.63
1313
cargo update -p serde_test --precise 1.0.156
1414

1515
cargo update -p log --precise 0.4.18

elementsd-tests/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
base64 = "0.13.0"
1110
bitcoin = "0.30.0"
12-
elements = {path = "../"}
11+
elements = {path = "../", features = ["base64"]}
1312
elementsd = "0.8.0"
1413
rand = "0.8"
1514

elementsd-tests/src/pset.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{setup, Call};
88

99
use bitcoin::{self, Address, Amount};
1010
use elements::hex::ToHex;
11-
use elements::encode::{deserialize, serialize};
11+
use elements::encode::serialize;
1212
use elements::hashes::Hash;
1313
use elements::pset::PartiallySignedTransaction;
1414
use elements::{AssetId, ContractHash};
@@ -131,15 +131,16 @@ fn tx_pegin() {
131131
}
132132

133133
fn rtt(base64: &str) -> String {
134-
base64::encode(serialize(&psbt_from_base64(&base64)))
134+
let pset: PartiallySignedTransaction = base64.parse().unwrap();
135+
pset.to_string()
135136
}
136137

137138
fn psbt_rtt(elementsd: &ElementsD, base64: &str) {
138139
let a = elementsd.decode_psbt(&base64).unwrap();
139140

140-
let b_psbt = psbt_from_base64(&base64);
141+
let b_psbt: PartiallySignedTransaction = base64.parse().unwrap();
141142
let mut b_bytes = serialize(&b_psbt);
142-
let b_base64 = base64::encode(&b_bytes);
143+
let b_base64 = bitcoin::base64::encode(&b_bytes);
143144
let b = elementsd.decode_psbt(&b_base64).unwrap();
144145

145146
assert_eq!(a, b);
@@ -151,16 +152,10 @@ fn psbt_rtt(elementsd: &ElementsD, base64: &str) {
151152
// ensuring decode prints all data inside psbt, randomly changing a byte,
152153
// if the results is still decodable it should not be equal to initial value
153154
b_bytes[i] = b_bytes[i].wrapping_add(1);
154-
let base64 = base64::encode(&b_bytes);
155+
let base64 = bitcoin::base64::encode(&b_bytes);
155156
if let Some(decoded) = elementsd.decode_psbt(&base64) {
156157
assert_ne!(a, decoded, "{} with changed byte {}", b_bytes.to_hex(), i);
157158
}
158159
b_bytes[i] = b_bytes[i].wrapping_sub(1);
159160
}
160161
}
161-
162-
fn psbt_from_base64(base64: &str) -> PartiallySignedTransaction {
163-
let bytes = base64::decode(&base64).unwrap();
164-
deserialize(&bytes).unwrap()
165-
}
166-

src/blech32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ mod test {
289289
fn test_checksum() {
290290
let data = vec![7,2,3,4,5,6,7,8,9,234,123,213,16];
291291
let cs = create_checksum(b"lq", &data.to_base32(), Variant::Blech32);
292-
let expected_cs = vec![22,13,13,5,4,4,23,7,28,21,30,12];
292+
let expected_cs = [22,13,13,5,4,4,23,7,28,21,30,12];
293293
for i in 0..expected_cs.len() {
294294
assert_eq!(expected_cs[i], *cs[i].as_ref());
295295
}

src/pset/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ mod map;
3030
pub mod raw;
3131
pub mod serialize;
3232

33+
#[cfg(feature = "base64")]
34+
mod str;
35+
3336
use crate::blind::{BlindAssetProofs, BlindValueProofs};
3437
use crate::confidential;
3538
use crate::encode::{self, Decodable, Encodable};

src/pset/str.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use super::PartiallySignedTransaction;
2+
3+
#[derive(Debug)]
4+
pub enum Error {
5+
Base64(bitcoin::base64::DecodeError),
6+
Deserialize(crate::encode::Error)
7+
}
8+
9+
impl core::fmt::Display for Error {
10+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11+
match self {
12+
Error::Base64(_) => write!(f, "Base64 error"),
13+
Error::Deserialize(_) => write!(f, "Deserialize error"),
14+
}
15+
}
16+
}
17+
18+
impl std::error::Error for Error {
19+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
20+
match self {
21+
Error::Base64(e) => Some(e),
22+
Error::Deserialize(e) => Some(e),
23+
}
24+
}
25+
26+
}
27+
28+
impl std::str::FromStr for PartiallySignedTransaction {
29+
type Err=Error;
30+
31+
fn from_str(s: &str) -> Result<Self, Self::Err> {
32+
let bytes = bitcoin::base64::decode(s).map_err(Error::Base64)?;
33+
crate::encode::deserialize(&bytes).map_err(Error::Deserialize)
34+
}
35+
}
36+
37+
impl std::fmt::Display for PartiallySignedTransaction {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
let bytes = crate::encode::serialize(self);
40+
let base64 = bitcoin::base64::encode(bytes);
41+
write!(f, "{}", base64)
42+
}
43+
}

0 commit comments

Comments
 (0)