Skip to content

Commit e772b49

Browse files
committed
Add ContractHash type to ensure reverse hex serialization
Which is what Elements Core expects.
1 parent 15762c8 commit e772b49

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ documentation = "https://docs.rs/elements/"
1717

1818
[dependencies]
1919
bitcoin = "0.23"
20+
# While this dependency is included in bitcoin, we need this to use the macros.
21+
# We should probably try keep this one in sync with the bitcoin version,
22+
# to avoid requiring two version of bitcoin_hashes.
23+
bitcoin_hashes = "0.7.6"
2024

2125
[dependencies.serde]
2226
version = "1.0"

src/issuance.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::io;
1818
use std::str::FromStr;
1919

2020
use bitcoin::util::hash::BitcoinHash;
21-
use bitcoin::hashes::{hex, sha256, Hash};
21+
use bitcoin::hashes::{hex, sha256, sha256d, Hash};
2222

2323
use encode::{self, Encodable, Decodable};
2424
use fast_merkle_root::fast_merkle_root;
@@ -37,6 +37,8 @@ const TWO32: [u8; 32] = [
3737
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3838
];
3939

40+
hash_newtype!(ContractHash, sha256d::Hash, 32, doc="The hash of an asset contract.", true);
41+
4042
/// An issued asset ID.
4143
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
4244
pub struct AssetId(sha256::Midstate);
@@ -55,7 +57,7 @@ impl AssetId {
5557
/// Generate the asset entropy from the issuance prevout and the contract hash.
5658
pub fn generate_asset_entropy(
5759
prevout: OutPoint,
58-
contract_hash: sha256::Hash,
60+
contract_hash: ContractHash,
5961
) -> sha256::Midstate {
6062
// E : entropy
6163
// I : prevout
@@ -226,7 +228,7 @@ mod test {
226228
let asset_id_hex = "dcd60818d863b5c026c40b2bc3ba6fdaf5018bcc8606c18adf7db4da0bcd8533";
227229
let token_id_hex = "c1adb114f4f87d33bf9ce90dd4f9ca523dd414d6cd010a7917903e2009689530";
228230

229-
let contract_hash = sha256::Hash::from_inner(ZERO32);
231+
let contract_hash = ContractHash::from_inner(ZERO32);
230232
let prevout = OutPoint::from_str(prevout_str).unwrap();
231233
let entropy = sha256::Midstate::from_hex(entropy_hex).unwrap();
232234
assert_eq!(AssetId::generate_asset_entropy(prevout, contract_hash), entropy);

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#![deny(missing_docs)]
2727

2828
pub extern crate bitcoin;
29+
#[macro_use]
30+
pub extern crate bitcoin_hashes;
2931
#[cfg(feature = "serde")] extern crate serde;
3032

3133
#[cfg(test)] extern crate rand;

0 commit comments

Comments
 (0)