Skip to content

Commit b010b06

Browse files
committed
Use AssetId as inner type for confidential::Asset
1 parent 2c50e9e commit b010b06

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/confidential.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::{io, fmt};
2525
use bitcoin::hashes::sha256d;
2626

2727
use encode::{self, Encodable, Decodable};
28+
use issuance::AssetId;
2829

2930
// Helper macro to implement various things for the various confidential
3031
// commitment types
@@ -207,7 +208,7 @@ pub enum Asset {
207208
/// No value
208209
Null,
209210
/// Asset entropy is explicitly encoded
210-
Explicit(sha256d::Hash),
211+
Explicit(AssetId),
211212
/// Asset is committed
212213
Confidential(u8, [u8; 32]),
213214
}
@@ -233,7 +234,7 @@ impl Asset {
233234

234235
/// Returns the explicit asset.
235236
/// Returns [None] if [is_explicit] returns false.
236-
pub fn explicit(&self) -> Option<sha256d::Hash> {
237+
pub fn explicit(&self) -> Option<AssetId> {
237238
match *self {
238239
Asset::Explicit(v) => Some(v),
239240
_ => None,
@@ -286,7 +287,7 @@ impl Nonce {
286287

287288
#[cfg(test)]
288289
mod tests {
289-
use bitcoin::hashes::Hash;
290+
use bitcoin::hashes::{Hash, sha256};
290291
use super::*;
291292

292293
#[test]
@@ -315,7 +316,7 @@ mod tests {
315316

316317
let assets = [
317318
Asset::Null,
318-
Asset::Explicit(sha256d::Hash::from_inner([0; 32])),
319+
Asset::Explicit(AssetId::from_inner(sha256::Midstate::from_inner([0; 32]))),
319320
Asset::Confidential(0x0a, [1; 32]),
320321
];
321322
for v in &assets[..] {

src/encode.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
use std::io::Cursor;
1919
use std::{error, fmt, io, mem};
2020

21-
use ::bitcoin::consensus::encode as btcenc;
21+
use bitcoin::consensus::encode as btcenc;
22+
use bitcoin::hashes::sha256;
2223

2324
use transaction::{Transaction, TxIn, TxOut};
2425

25-
pub use ::bitcoin::consensus::encode::MAX_VEC_SIZE;
26+
pub use bitcoin::consensus::encode::MAX_VEC_SIZE;
2627

2728
/// Encoding error
2829
#[derive(Debug)]
@@ -122,6 +123,18 @@ pub fn deserialize_partial<'a, T: Decodable>(data: &'a [u8]) -> Result<(T, usize
122123
Ok((rv, consumed))
123124
}
124125

126+
impl Encodable for sha256::Midstate {
127+
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, Error> {
128+
self.into_inner().consensus_encode(e)
129+
}
130+
}
131+
132+
impl Decodable for sha256::Midstate {
133+
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, Error> {
134+
Ok(Self::from_inner(<[u8; 32]>::consensus_decode(d)?))
135+
}
136+
}
137+
125138
/// Implement Elements encodable traits for Bitcoin encodable types.
126139
macro_rules! impl_upstream {
127140
($type: ty) => {

src/issuance.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515
//! Asset Issuance
1616
17+
use std::io;
1718
use std::str::FromStr;
1819

1920
use bitcoin::util::hash::BitcoinHash;
2021
use bitcoin::hashes::{hex, sha256, Hash};
22+
23+
use encode::{self, Encodable, Decodable};
2124
use fast_merkle_root::fast_merkle_root;
2225
use transaction::OutPoint;
2326

@@ -119,6 +122,18 @@ impl FromStr for AssetId {
119122
}
120123
}
121124

125+
impl Encodable for AssetId {
126+
fn consensus_encode<W: io::Write>(&self, e: W) -> Result<usize, encode::Error> {
127+
self.0.consensus_encode(e)
128+
}
129+
}
130+
131+
impl Decodable for AssetId {
132+
fn consensus_decode<D: io::Read>(d: D) -> Result<Self, encode::Error> {
133+
Ok(Self::from_inner(sha256::Midstate::consensus_decode(d)?))
134+
}
135+
}
136+
122137
#[cfg(feature = "serde")]
123138
impl ::serde::Serialize for AssetId {
124139
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {

src/transaction.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::collections::HashMap;
2121
use bitcoin::{self, BitcoinHash, Txid, VarInt};
2222
use bitcoin::blockdata::opcodes;
2323
use bitcoin::blockdata::script::{Script, Instruction};
24-
use bitcoin::hashes::{Hash, sha256, sha256d};
24+
use bitcoin::hashes::Hash;
2525

2626
use confidential;
2727
use encode::{self, Encodable, Decodable};
@@ -280,7 +280,7 @@ impl TxIn {
280280
},
281281
value: opt_try!(bitcoin::consensus::deserialize(&self.witness.pegin_witness[0])),
282282
asset: confidential::Asset::Explicit(
283-
opt_try!(bitcoin::consensus::deserialize(&self.witness.pegin_witness[1])),
283+
opt_try!(encode::deserialize(&self.witness.pegin_witness[1])),
284284
),
285285
genesis_hash: opt_try!(bitcoin::consensus::deserialize(&self.witness.pegin_witness[2])),
286286
claim_script: &self.witness.pegin_witness[3],
@@ -617,7 +617,6 @@ impl Transaction {
617617

618618
/// Get the total transaction fee in the given asset.
619619
pub fn fee_in(&self, asset: AssetId) -> u64 {
620-
let asset = sha256d::Hash::from_inner(asset.into_inner().into_inner());
621620
// is_fee checks for explicit asset and value, so we can unwrap them here.
622621
self.output.iter()
623622
.filter(|o| o.is_fee() && o.asset.explicit().expect("is_fee") == asset)
@@ -630,8 +629,7 @@ impl Transaction {
630629
let mut fees = HashMap::new();
631630
for out in self.output.iter().filter(|o| o.is_fee()) {
632631
// is_fee checks for explicit asset and value, so we can unwrap them here.
633-
let asset = out.asset.explicit().expect("is_fee").into_inner();
634-
let asset = AssetId::from_inner(sha256::Midstate::from_inner(asset));
632+
let asset = out.asset.explicit().expect("is_fee");
635633
let entry = fees.entry(asset).or_insert(0);
636634
*entry += out.value.explicit().expect("is_fee");
637635
}
@@ -719,7 +717,6 @@ impl Decodable for Transaction {
719717
mod tests {
720718
use bitcoin::{self, BitcoinHash};
721719
use bitcoin::hashes::hex::FromHex;
722-
use bitcoin::hashes::sha256d;
723720

724721
use encode::serialize;
725722
use confidential;
@@ -1222,7 +1219,7 @@ mod tests {
12221219
})
12231220
);
12241221

1225-
let expected_asset_id = sha256d::Hash::from_hex("630ed6f9b176af03c0cd3f8aa430f9e7b4d988cf2d0b2f204322488f03b00bf8").unwrap();
1222+
let expected_asset_id = AssetId::from_hex("630ed6f9b176af03c0cd3f8aa430f9e7b4d988cf2d0b2f204322488f03b00bf8").unwrap();
12261223
if let confidential::Asset::Explicit(asset_id) = tx.output[0].asset {
12271224
assert_eq!(expected_asset_id, asset_id);
12281225
} else {

0 commit comments

Comments
 (0)