Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bytes for Ed25519Signature public key #1818

Merged
merged 8 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
Response::Ok
}
UtilsMethod::VerifyEd25519Signature { signature, message } => {
use iota_sdk::types::block::Error;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
let signature = Ed25519Signature::try_from(signature)?;
let message: Vec<u8> = prefix_hex::decode(message)?;
Response::Bool(signature.verify(&message))
Response::Bool(signature.verify(&message).map_err(Error::from)?)
}
UtilsMethod::VerifySecp256k1EcdsaSignature {
public_key,
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/sign_and_verify_ed25519/sign_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ async fn main() -> Result<()> {
.await?;
println!(
"Public key: {}\nSignature: {}",
prefix_hex::encode(signature.public_key().as_ref()),
prefix_hex::encode(signature.public_key()),
prefix_hex::encode(signature.signature().to_bytes()),
);

// Hash the public key to get the address
let bech32_address = hex_public_key_to_bech32_address(&prefix_hex::encode(signature.public_key().as_ref()), "rms")?;
let bech32_address = hex_public_key_to_bech32_address(&prefix_hex::encode(signature.public_key()), "rms")?;
println!("Address: {bech32_address}");

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/types/block/payload/milestone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ impl MilestonePayload {
)));
}

if !signature.verify(&essence_hash) {
if !signature
.verify(&essence_hash)
.map_err(|e| MilestoneValidationError::Crypto(e))?
{
return Err(MilestoneValidationError::InvalidSignature(
index,
prefix_hex::encode(signature.public_key().as_ref()),
Expand Down
45 changes: 24 additions & 21 deletions sdk/src/types/block/signature/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::types::block::{address::Ed25519Address, Error};
/// An Ed25519 signature.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Ed25519Signature {
public_key: PublicKey,
public_key: [u8; Self::PUBLIC_KEY_LENGTH],
signature: Signature,
}

Expand All @@ -33,22 +33,27 @@ impl Ed25519Signature {

/// Creates a new [`Ed25519Signature`].
pub fn new(public_key: PublicKey, signature: Signature) -> Self {
Self { public_key, signature }
Self {
public_key: public_key.to_bytes(),
signature,
}
}

/// Creates a new [`Ed25519Signature`] from bytes.
pub fn try_from_bytes(
public_key: [u8; Self::PUBLIC_KEY_LENGTH],
signature: [u8; Self::SIGNATURE_LENGTH],
) -> Result<Self, Error> {
Ok(Self::new(
PublicKey::try_from_bytes(public_key)?,
Signature::from_bytes(signature),
))
pub fn from_bytes(public_key: [u8; Self::PUBLIC_KEY_LENGTH], signature: [u8; Self::SIGNATURE_LENGTH]) -> Self {
Self {
public_key,
signature: Signature::from_bytes(signature),
}
}

/// Converts the public key bytes to a [`PublicKey`], if valid.
pub fn to_public_key(&self) -> Result<PublicKey, crypto::Error> {
Ok(PublicKey::try_from_bytes(self.public_key)?)
}

/// Returns the public key of an [`Ed25519Signature`].
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
pub fn public_key(&self) -> &PublicKey {
pub fn public_key(&self) -> &[u8; Self::PUBLIC_KEY_LENGTH] {
&self.public_key
}

Expand All @@ -57,8 +62,8 @@ impl Ed25519Signature {
&self.signature
}

pub fn verify(&self, message: &[u8]) -> bool {
self.public_key.verify(&self.signature, message)
pub fn verify(&self, message: &[u8]) -> Result<bool, crypto::Error> {
Ok(self.to_public_key()?.verify(&self.signature, message))
}

/// Verifies the [`Ed25519Signature`] for a message against an [`Ed25519Address`].
Expand All @@ -72,7 +77,7 @@ impl Ed25519Signature {
});
}

if !self.verify(message) {
if !self.verify(message)? {
return Err(Error::InvalidSignature);
}

Expand Down Expand Up @@ -109,7 +114,7 @@ impl Packable for Ed25519Signature {
type UnpackVisitor = ();

fn pack<P: Packer>(&self, packer: &mut P) -> Result<(), P::Error> {
self.public_key.to_bytes().pack(packer)?;
self.public_key.pack(packer)?;
self.signature.to_bytes().pack(packer)?;

Ok(())
Expand All @@ -121,9 +126,7 @@ impl Packable for Ed25519Signature {
) -> Result<Self, UnpackError<Self::UnpackError, U::Error>> {
let public_key = <[u8; Self::PUBLIC_KEY_LENGTH]>::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
let signature = <[u8; Self::SIGNATURE_LENGTH]>::unpack::<_, VERIFY>(unpacker, visitor).coerce()?;
Self::try_from_bytes(public_key, signature)
.map_err(UnpackError::Packable)
.coerce()
Ok(Self::from_bytes(public_key, signature))
}
}

Expand All @@ -150,7 +153,7 @@ pub(crate) mod dto {
fn from(value: &Ed25519Signature) -> Self {
Self {
kind: Ed25519Signature::KIND,
public_key: prefix_hex::encode(value.public_key.as_slice()),
public_key: prefix_hex::encode(&value.public_key),
signature: prefix_hex::encode(value.signature.to_bytes()),
}
}
Expand All @@ -160,10 +163,10 @@ pub(crate) mod dto {
type Error = Error;

fn try_from(value: Ed25519SignatureDto) -> Result<Self, Self::Error> {
Self::try_from_bytes(
Ok(Self::from_bytes(
prefix_hex::decode(&value.public_key).map_err(|_| Error::InvalidField("publicKey"))?,
prefix_hex::decode(&value.signature).map_err(|_| Error::InvalidField("signature"))?,
)
))
}
}
}
2 changes: 1 addition & 1 deletion sdk/src/wallet/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ mod test {

let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let ref_unlock = Unlock::Reference(ReferenceUnlock::new(0).unwrap());
let unlocks = Unlocks::new([sig_unlock, ref_unlock]).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/types/ed25519_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn kind() {
fn packed_len() {
let pub_key_bytes: [u8; 32] = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes: [u8; 64] = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let sig = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let sig = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);

assert_eq!(sig.packed_len(), 32 + 64);
assert_eq!(sig.pack_to_vec().len(), 32 + 64);
Expand All @@ -27,7 +27,7 @@ fn packed_len() {
fn pack_unpack_valid() {
let pub_key_bytes: [u8; 32] = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes: [u8; 64] = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let sig = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let sig = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_packed = sig.pack_to_vec();

assert_eq!(sig, PackableExt::unpack_verified(sig_packed.as_slice(), &()).unwrap());
Expand Down
6 changes: 2 additions & 4 deletions sdk/tests/types/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn transaction() {

let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let ref_unlock = Unlock::Reference(ReferenceUnlock::new(0).unwrap());
let unlocks = Unlocks::new(vec![sig_unlock, ref_unlock]).unwrap();
Expand Down Expand Up @@ -93,9 +93,7 @@ fn milestone() {
MilestoneOptions::from_vec(vec![]).unwrap(),
)
.unwrap(),
vec![Signature::from(
Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap(),
)],
vec![Signature::from(Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes))],
)
.unwrap()
.into();
Expand Down
8 changes: 4 additions & 4 deletions sdk/tests/types/transaction_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn builder_no_essence_too_few_unlocks() {
// Construct a list with a single unlock, whereas we have 2 tx inputs.
let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let unlocks = Unlocks::new([sig_unlock]).unwrap();

Expand Down Expand Up @@ -90,7 +90,7 @@ fn builder_no_essence_too_many_unlocks() {
// Construct a list of two unlocks, whereas we only have 1 tx input.
let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let ref_unlock = Unlock::Reference(ReferenceUnlock::new(0).unwrap());

Expand Down Expand Up @@ -130,7 +130,7 @@ fn pack_unpack_valid() {
// Construct a list of two unlocks, whereas we only have 1 tx input.
let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let ref_unlock = Unlock::Reference(ReferenceUnlock::new(0).unwrap());
let unlocks = Unlocks::new([sig_unlock, ref_unlock]).unwrap();
Expand Down Expand Up @@ -172,7 +172,7 @@ fn getters() {
// Construct a list of two unlocks, whereas we only have 1 tx input.
let pub_key_bytes = prefix_hex::decode(ED25519_PUBLIC_KEY).unwrap();
let sig_bytes = prefix_hex::decode(ED25519_SIGNATURE).unwrap();
let signature = Ed25519Signature::try_from_bytes(pub_key_bytes, sig_bytes).unwrap();
let signature = Ed25519Signature::from_bytes(pub_key_bytes, sig_bytes);
let sig_unlock = Unlock::Signature(SignatureUnlock::from(Signature::from(signature)));
let ref_unlock = Unlock::Reference(ReferenceUnlock::new(0).unwrap());
let unlocks = Unlocks::new([sig_unlock, ref_unlock]).unwrap();
Expand Down
Loading