Skip to content

Commit

Permalink
Amend
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Feb 2, 2025
1 parent 89d2c30 commit 418909a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 13 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ default = []
[dependencies]
base64 = "0.22.1"
bitcoin = "0.32.5"
sha2 = "0.10.8"
snafu = { version = "0.8.5", default-features = false, features = ["rust_1_61", "std"] }

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use {
base64::{engine::general_purpose, Engine},
bitcoin::hashes::{sha256, Hash},
bitcoin::{
absolute::LockTime,
address::AddressData,
Expand All @@ -18,6 +17,7 @@ use {
Transaction, TxIn, TxOut, Witness,
},
error::Error,
sha2::{Digest, Sha256},
snafu::{ResultExt, Snafu},
std::str::FromStr,
};
Expand Down Expand Up @@ -51,12 +51,12 @@ mod tests {
#[test]
fn message_hashes_are_correct() {
assert_eq!(
hex::encode(message_hash("".as_bytes())),
hex::encode(tagged_hash(BIP322_TAG, "")),
"c90c269c4f8fcbe6880f72a721ddfbf1914268a794cbb21cfafee13770ae19f1"
);

assert_eq!(
hex::encode(message_hash("Hello World".as_bytes())),
hex::encode(tagged_hash(BIP322_TAG, "Hello World")),
"f0eb03b1a75ac6d9847f55c624a99169b5dccba2a31f5b23bea77ba270de0a7a"
);
}
Expand Down
25 changes: 15 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::*;

const TAG: &str = "BIP0322-signed-message";
pub const BIP322_TAG: &str = "BIP0322-signed-message";

/// Create the tagged message hash.
pub fn message_hash(message: &[u8]) -> Vec<u8> {
let mut tag_hash = sha256::Hash::hash(TAG.as_bytes()).to_byte_array().to_vec();
tag_hash.extend(tag_hash.clone());
tag_hash.extend(message);

sha256::Hash::hash(tag_hash.as_slice())
.to_byte_array()
.to_vec()
pub fn tagged_hash(tag: &str, message: impl AsRef<[u8]>) -> [u8; 32] {
let tag_hash = Sha256::new().chain_update(tag).finalize();
Sha256::new()
.chain_update(tag_hash)
.chain_update(tag_hash)
.chain_update(message)
.finalize()
.into()
}

/// Create the `to_spend` transaction.
Expand All @@ -27,7 +27,12 @@ pub fn create_to_spend(address: &Address, message: &[u8]) -> Result<Transaction>
},
script_sig: script::Builder::new()
.push_int(0)
.push_slice::<&PushBytes>(message_hash(message).as_slice().try_into().unwrap())
.push_slice::<&PushBytes>(
tagged_hash(BIP322_TAG, message)
.as_slice()
.try_into()
.unwrap(),
)
.into_script(),
sequence: Sequence(0),
witness: Witness::new(),
Expand Down

0 comments on commit 418909a

Please sign in to comment.