moved 'message_type' from 'ContractMessageContent' to 'SignableMessage'

This commit is contained in:
Jędrzej Stuczyński
2023-03-08 13:56:27 +00:00
parent c3d9b8b21b
commit 667e41d4ec
3 changed files with 22 additions and 11 deletions
@@ -98,7 +98,7 @@ pub trait SigningPurpose {
fn message_type() -> MessageType;
}
#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
pub struct MessageType(String);
@@ -137,15 +137,20 @@ impl SigningAlgorithm {
pub struct SignableMessage<T> {
pub nonce: u32,
pub algorithm: SigningAlgorithm,
pub message_type: MessageType,
pub content: T,
}
impl<T> SignableMessage<T> {
impl<T> SignableMessage<T>
where
T: SigningPurpose,
{
pub fn new(nonce: u32, content: T) -> Self {
SignableMessage {
nonce,
algorithm: SigningAlgorithm::Ed25519,
message_type: T::message_type(),
content,
}
}
@@ -162,7 +167,10 @@ impl<T> SignableMessage<T> {
to_vec(self)
}
pub fn to_sha256_plaintext_digest(&self) -> StdResult<Vec<u8>> {
pub fn to_sha256_plaintext_digest(&self) -> StdResult<Vec<u8>>
where
T: Serialize,
{
unimplemented!()
}
@@ -209,20 +217,24 @@ impl<T> SignableMessage<T> {
#[derive(Serialize)]
pub struct ContractMessageContent<T> {
pub message_type: MessageType,
pub sender: Addr,
pub proxy: Option<Addr>,
pub funds: Vec<Coin>,
pub data: T,
}
impl<T> ContractMessageContent<T>
impl<T> SigningPurpose for ContractMessageContent<T>
where
T: SigningPurpose,
{
fn message_type() -> MessageType {
T::message_type()
}
}
impl<T> ContractMessageContent<T> {
pub fn new(sender: Addr, proxy: Option<Addr>, funds: Vec<Coin>, data: T) -> Self {
ContractMessageContent {
message_type: T::message_type(),
sender,
proxy,
funds,
@@ -238,7 +250,6 @@ where
};
ContractMessageContent {
message_type: T::message_type(),
sender: signer,
proxy,
funds: info.funds,
@@ -1,7 +1,7 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::signing::{MessageSignature, SignableMessage, SigningAlgorithm};
use crate::signing::{MessageSignature, SignableMessage, SigningAlgorithm, SigningPurpose};
use cosmwasm_std::{Api, StdError, VerificationError};
use serde::Serialize;
use thiserror::Error;
@@ -9,7 +9,7 @@ use thiserror::Error;
pub trait Verifier {
type Error: From<StdError>;
fn verify_message<T: Serialize>(
fn verify_message<T: Serialize + SigningPurpose>(
&self,
message: SignableMessage<T>,
signature: MessageSignature,
+2 -2
View File
@@ -67,7 +67,7 @@ pub mod test_helpers {
SignableMixNodeBondingMsg,
};
use nym_contracts_common::signing::{
ContractMessageContent, MessageSignature, SignableMessage, SigningAlgorithm,
ContractMessageContent, MessageSignature, SignableMessage, SigningAlgorithm, SigningPurpose,
};
use nym_crypto::asymmetric::identity;
use nym_crypto::asymmetric::identity::KeyPair;
@@ -860,7 +860,7 @@ pub mod test_helpers {
}
}
pub fn ed25519_sign_message<T: Serialize>(
pub fn ed25519_sign_message<T: Serialize + SigningPurpose>(
message: SignableMessage<T>,
private_key: &identity::PrivateKey,
) -> MessageSignature {