Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

[stacks-core] Stacks transactions #56

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sbtc-core/src/operations/construction/withdrawal_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitcoin::{
secp256k1::{Message, Secp256k1},
Address as BitcoinAddress, Network, PrivateKey, Transaction,
};
use stacks_core::crypto::{sha256::Sha256Hasher, Hashing};
use stacks_core::crypto::{sha256::Sha256Hash, Hashing};

use crate::{
operations::construction::utils::{
Expand Down Expand Up @@ -107,7 +107,7 @@ fn withdrawal_data(
let mut msg = amount.to_be_bytes().to_vec();
msg.extend_from_slice(recipient.script_pubkey().as_bytes());

let msg_hash = Sha256Hasher::new(msg);
let msg_hash = Sha256Hash::new(msg);
let msg_ecdsa = Message::from_slice(msg_hash.as_ref()).unwrap();

let (recovery_id, signature) = Secp256k1::new()
Expand Down
44 changes: 22 additions & 22 deletions stacks-core/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
c32::{decode_address, encode_address},
codec::Codec,
crypto::{
hash160::{Hash160Hasher, HASH160_LENGTH},
sha256::Sha256Hasher,
hash160::{Hash160Hash, HASH160_LENGTH},
sha256::Sha256Hash,
Hashing, PublicKey,
},
StacksError, StacksResult,
Expand Down Expand Up @@ -43,12 +43,12 @@ impl TryFrom<u8> for AddressVersion {
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct StacksAddress {
version: AddressVersion,
hash: Hash160Hasher,
hash: Hash160Hash,
}

impl StacksAddress {
/// Create a new Stacks address from the given version and hash
pub fn new(version: AddressVersion, hash: Hash160Hasher) -> Self {
pub fn new(version: AddressVersion, hash: Hash160Hash) -> Self {
Self { version, hash }
}

Expand All @@ -58,7 +58,7 @@ impl StacksAddress {
}

/// Get the address hash
pub fn hash(&self) -> &Hash160Hasher {
pub fn hash(&self) -> &Hash160Hash {
&self.hash
}

Expand Down Expand Up @@ -106,7 +106,7 @@ impl Codec for StacksAddress {
let mut hash_buffer = [0; HASH160_LENGTH];
data.read_exact(&mut hash_buffer)?;

let hash = Hash160Hasher::from_bytes(&hash_buffer).unwrap();
let hash = Hash160Hash::from_bytes(&hash_buffer).unwrap();

Ok(Self { version, hash })
}
Expand Down Expand Up @@ -144,14 +144,14 @@ impl fmt::Display for StacksAddress {
}
}

fn hash_p2pkh(key: &PublicKey) -> Hash160Hasher {
Hash160Hasher::new(key.serialize())
fn hash_p2pkh(key: &PublicKey) -> Hash160Hash {
Hash160Hash::new(key.serialize())
}

fn hash_p2sh<'a>(
pub_keys: impl IntoIterator<Item = &'a PublicKey>,
signature_threshold: usize,
) -> Hash160Hasher {
) -> Hash160Hash {
let mut builder = Builder::new();
let mut key_counter = 0;

Expand All @@ -166,13 +166,13 @@ fn hash_p2sh<'a>(
builder = builder.push_opcode(OP_CHECKMULTISIG);

let script = builder.into_script();
let script_hash = Hash160Hasher::new(script.as_bytes());
let script_hash = Hash160Hash::new(script.as_bytes());

script_hash
}

fn hash_p2wpkh(key: &PublicKey) -> Hash160Hasher {
let key_hash_hasher = Hash160Hasher::new(key.serialize());
fn hash_p2wpkh(key: &PublicKey) -> Hash160Hash {
let key_hash_hasher = Hash160Hash::new(key.serialize());
let key_hash = key_hash_hasher.as_ref();
let key_hash_len = key_hash.len();

Expand All @@ -181,13 +181,13 @@ fn hash_p2wpkh(key: &PublicKey) -> Hash160Hasher {
buff.push(key_hash_len as u8);
buff.extend_from_slice(key_hash);

Hash160Hasher::new(&buff)
Hash160Hash::new(&buff)
}

fn hash_p2wsh<'a>(
pub_keys: impl IntoIterator<Item = &'a PublicKey>,
signature_threshold: usize,
) -> Hash160Hasher {
) -> Hash160Hash {
let mut script = vec![];
let mut key_count = 0;

Expand All @@ -204,20 +204,20 @@ fn hash_p2wsh<'a>(
script.push(key_count + 80);
script.push(174);

let digest = Sha256Hasher::new(&script);
let digest = Sha256Hash::new(&script);
let digest_bytes = digest.as_ref();

let mut buff = vec![];
buff.push(0);
buff.push(digest_bytes.len() as u8);
buff.extend_from_slice(digest_bytes);

Hash160Hasher::new(&buff)
Hash160Hash::new(&buff)
}

#[cfg(test)]
mod tests {
use crate::crypto::hash160::Hash160Hasher;
use crate::crypto::hash160::Hash160Hash;

use super::*;

Expand Down Expand Up @@ -252,7 +252,7 @@ mod tests {
let addr_hash = "fc1058076c56333d7d2d9fbb936aefa632c0e7a8";

let pk = PublicKey::from_slice(&hex::decode(pk_hex).unwrap()).unwrap();
let expected_hash: Hash160Hasher = hex::decode(addr_hash)
let expected_hash: Hash160Hash = hex::decode(addr_hash)
.unwrap()
.as_slice()
.try_into()
Expand All @@ -270,7 +270,7 @@ mod tests {

let pk1 = PublicKey::from_slice(&hex::decode(pk1_hex).unwrap()).unwrap();
let pk2 = PublicKey::from_slice(&hex::decode(pk2_hex).unwrap()).unwrap();
let expected_hash: Hash160Hasher = hex::decode(addr_hash)
let expected_hash: Hash160Hash = hex::decode(addr_hash)
.unwrap()
.as_slice()
.try_into()
Expand All @@ -286,7 +286,7 @@ mod tests {
let addr_hash = "599623097df78a0e962108bfb0f1f78ef1d15f57";

let pk = PublicKey::from_slice(&hex::decode(pk_hex).unwrap()).unwrap();
let expected_hash: Hash160Hasher = hex::decode(addr_hash)
let expected_hash: Hash160Hash = hex::decode(addr_hash)
.unwrap()
.as_slice()
.try_into()
Expand All @@ -304,7 +304,7 @@ mod tests {

let pk1 = PublicKey::from_slice(&hex::decode(pk1_hex).unwrap()).unwrap();
let pk2 = PublicKey::from_slice(&hex::decode(pk2_hex).unwrap()).unwrap();
let expected_hash: Hash160Hasher = hex::decode(addr_hash)
let expected_hash: Hash160Hash = hex::decode(addr_hash)
.unwrap()
.as_slice()
.try_into()
Expand All @@ -320,7 +320,7 @@ mod tests {
let addr_hash = "3bb7c80b72757b4bc94bd3cb09171500fb72b4ac";

let pk = PublicKey::from_slice(&hex::decode(pk_hex).unwrap()).unwrap();
let expected_hash: Hash160Hasher = hex::decode(addr_hash)
let expected_hash: Hash160Hash = hex::decode(addr_hash)
.unwrap()
.as_slice()
.try_into()
Expand Down
6 changes: 3 additions & 3 deletions stacks-core/src/c32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use once_cell::sync::Lazy;

use crate::{
address::AddressVersion,
crypto::{sha256::DoubleSha256Hasher, Hashing},
crypto::{sha256::DoubleSha256Hash, Hashing},
};

const C32_ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn version_check_encode(version: AddressVersion, data: impl AsRef<[u8]>) ->
let mut buffer = vec![version as u8];
buffer.extend_from_slice(data);

let checksum = DoubleSha256Hasher::new(&buffer).checksum();
let checksum = DoubleSha256Hash::new(&buffer).checksum();
buffer.extend_from_slice(&checksum);

let mut encoded = encode(&buffer[1..]);
Expand Down Expand Up @@ -212,7 +212,7 @@ pub fn version_check_decode(input: impl AsRef<str>) -> Result<(AddressVersion, V
let mut buffer_to_check = vec![decoded_version_byte];
buffer_to_check.extend_from_slice(data_bytes);

let computed_checksum = DoubleSha256Hasher::new(buffer_to_check).checksum();
let computed_checksum = DoubleSha256Hash::new(buffer_to_check).checksum();

if computed_checksum != expected_checksum {
return Err(C32Error::InvalidChecksum(
Expand Down
136 changes: 0 additions & 136 deletions stacks-core/src/contract_name.rs

This file was deleted.

8 changes: 4 additions & 4 deletions stacks-core/src/crypto/hash160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
StacksError, StacksResult,
};

use super::sha256::Sha256Hasher;
use super::sha256::Sha256Hash;

pub(crate) const HASH160_LENGTH: usize = 20;

Expand All @@ -18,7 +18,7 @@ pub struct Hash160Hashing([u8; HASH160_LENGTH]);

impl Hashing<HASH160_LENGTH> for Hash160Hashing {
fn hash(data: &[u8]) -> Self {
Self(Ripemd160::digest(Sha256Hasher::new(data)).into())
Self(Ripemd160::digest(Sha256Hash::new(data)).into())
}

fn as_bytes(&self) -> &[u8] {
Expand Down Expand Up @@ -47,7 +47,7 @@ impl TryFrom<Hex> for Hash160Hashing {
}

/// Hash160 hasher type
pub type Hash160Hasher = Hasher<Hash160Hashing, HASH160_LENGTH>;
pub type Hash160Hash = Hasher<Hash160Hashing, HASH160_LENGTH>;

#[cfg(test)]
mod tests {
Expand All @@ -59,7 +59,7 @@ mod tests {
let expected_hash_hex = "f5e95668dadf6fdef8521f7e1aa8a5e650c9f849";

assert_eq!(
hex::encode(Hash160Hasher::hash(plaintext.as_bytes())),
hex::encode(Hash160Hash::hash(plaintext.as_bytes())),
expected_hash_hex
);
}
Expand Down
Loading