Skip to content
Merged
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
20 changes: 20 additions & 0 deletions crates/astria-core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ impl<'a> From<&'a SigningKey> for VerificationKey {
}
}

impl From<Ed25519SigningKey> for SigningKey {
fn from(key: Ed25519SigningKey) -> Self {
Self(key)
}
}

impl TryFrom<&[u8]> for SigningKey {
type Error = ed25519_consensus::Error;

Expand Down Expand Up @@ -210,6 +216,14 @@ impl AsRef<[u8]> for VerificationKey {
}
}

impl From<Ed25519VerificationKey> for VerificationKey {
fn from(key: Ed25519VerificationKey) -> Self {
Self {
key,
}
}
}

impl TryFrom<&[u8]> for VerificationKey {
type Error = Error;

Expand Down Expand Up @@ -262,6 +276,12 @@ impl From<[u8; 64]> for Signature {
}
}

impl From<Ed25519Signature> for Signature {
fn from(signature: Ed25519Signature) -> Self {
Self(signature)
}
}

impl TryFrom<&[u8]> for Signature {
type Error = Error;

Expand Down
17 changes: 17 additions & 0 deletions crates/astria-core/src/primitive/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ impl<TBytes, TPrefix> AddressBuilder<TBytes, TPrefix> {
}
}

/// Use the given verification key for address generation.
///
/// The verification key is hashed with SHA256 and the first 20 bytes are used as the address
/// bytes.
#[allow(clippy::missing_panics_doc)] // allow clippy, as the conversion is infallible
#[must_use = "the builder must be built to construct an address to be useful"]
pub fn verification_key(
self,
key: &crate::crypto::VerificationKey,
) -> AddressBuilder<WithBytes<'static>, TPrefix> {
let hash = Sha256::digest(key.as_bytes());
let array: [u8; ADDRESS_LEN] = hash[0..ADDRESS_LEN]
.try_into()
.expect("hash is 32 bytes long, so must always be able to convert to 20 bytes");
self.array(array)
}

#[must_use = "the builder must be built to construct an address to be useful"]
pub fn prefix<'a, T: Into<std::borrow::Cow<'a, str>>>(
self,
Expand Down