Skip to content

Commit a280fa9

Browse files
committed
Use more general trait for message
1 parent 418909a commit a280fa9

File tree

6 files changed

+163
-47
lines changed

6 files changed

+163
-47
lines changed

Cargo.lock

Lines changed: 131 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ snafu = { version = "0.8.5", default-features = false, features = ["rust_1_61",
2020
[dev-dependencies]
2121
hex = "0.4.3"
2222
pretty_assertions = "1.4.1"
23-
rand = "0.8.5"
23+
rand = "0.9.0"

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ mod tests {
6666
assert_eq!(
6767
create_to_spend(
6868
&Address::from_str(SEGWIT_ADDRESS).unwrap().assume_checked(),
69-
"".as_bytes()
69+
""
7070
)
7171
.unwrap()
7272
.compute_txid()
@@ -77,7 +77,7 @@ mod tests {
7777
assert_eq!(
7878
create_to_spend(
7979
&Address::from_str(SEGWIT_ADDRESS).unwrap().assume_checked(),
80-
"Hello World".as_bytes()
80+
"Hello World"
8181
)
8282
.unwrap()
8383
.compute_txid()
@@ -90,7 +90,7 @@ mod tests {
9090
fn to_sign_txids_correct() {
9191
let to_spend = create_to_spend(
9292
&Address::from_str(SEGWIT_ADDRESS).unwrap().assume_checked(),
93-
"".as_bytes(),
93+
"",
9494
)
9595
.unwrap();
9696

@@ -103,7 +103,7 @@ mod tests {
103103

104104
let to_spend = create_to_spend(
105105
&Address::from_str(SEGWIT_ADDRESS).unwrap().assume_checked(),
106-
"Hello World".as_bytes(),
106+
"Hello World",
107107
)
108108
.unwrap();
109109

@@ -338,13 +338,13 @@ mod tests {
338338
#[test]
339339
fn adding_aux_randomness_roundtrips() {
340340
let address = Address::from_str(TAPROOT_ADDRESS).unwrap().assume_checked();
341-
let message = "Hello World with aux randomness".as_bytes();
341+
let message = "Hello World with aux randomness";
342342
let to_spend = create_to_spend(&address, message).unwrap();
343343
let to_sign = create_to_sign(&to_spend, None).unwrap();
344344
let private_key = PrivateKey::from_wif(WIF_PRIVATE_KEY).unwrap();
345345

346346
let mut aux_rand = [0u8; 32];
347-
rand::thread_rng().fill_bytes(&mut aux_rand);
347+
rand::rng().fill_bytes(&mut aux_rand);
348348

349349
let witness =
350350
create_message_signature_taproot(&to_spend, &to_sign, private_key, Some(aux_rand));

src/sign.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn sign_simple_encoded(address: &str, message: &str, wif_private_key: &str)
88

99
let private_key = PrivateKey::from_wif(wif_private_key).context(error::PrivateKeyParse)?;
1010

11-
let witness = sign_simple(&address, message.as_bytes(), private_key)?;
11+
let witness = sign_simple(&address, message, private_key)?;
1212

1313
let mut buffer = Vec::new();
1414

@@ -27,7 +27,7 @@ pub fn sign_full_encoded(address: &str, message: &str, wif_private_key: &str) ->
2727

2828
let private_key = PrivateKey::from_wif(wif_private_key).context(error::PrivateKeyParse)?;
2929

30-
let tx = sign_full(&address, message.as_bytes(), private_key)?;
30+
let tx = sign_full(&address, message, private_key)?;
3131

3232
let mut buffer = Vec::new();
3333

@@ -38,7 +38,11 @@ pub fn sign_full_encoded(address: &str, message: &str, wif_private_key: &str) ->
3838
}
3939

4040
/// Signs in the BIP-322 simple format from proper Rust types and returns the witness.
41-
pub fn sign_simple(address: &Address, message: &[u8], private_key: PrivateKey) -> Result<Witness> {
41+
pub fn sign_simple(
42+
address: &Address,
43+
message: impl AsRef<[u8]>,
44+
private_key: PrivateKey,
45+
) -> Result<Witness> {
4246
Ok(
4347
sign_full(address, message, private_key)?.input[0]
4448
.witness
@@ -49,7 +53,7 @@ pub fn sign_simple(address: &Address, message: &[u8], private_key: PrivateKey) -
4953
/// Signs in the BIP-322 full format from proper Rust types and returns the full transaction.
5054
pub fn sign_full(
5155
address: &Address,
52-
message: &[u8],
56+
message: impl AsRef<[u8]>,
5357
private_key: PrivateKey,
5458
) -> Result<Transaction> {
5559
let to_spend = create_to_spend(address, message)?;

src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn tagged_hash(tag: &str, message: impl AsRef<[u8]>) -> [u8; 32] {
1414
}
1515

1616
/// Create the `to_spend` transaction.
17-
pub fn create_to_spend(address: &Address, message: &[u8]) -> Result<Transaction> {
17+
pub fn create_to_spend(address: &Address, message: impl AsRef<[u8]>) -> Result<Transaction> {
1818
Ok(Transaction {
1919
version: Version(0),
2020
lock_time: LockTime::ZERO,

0 commit comments

Comments
 (0)