Skip to content

Commit

Permalink
Rename SignedArbitraryMessage to ArbitraryMessageSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
TheQuantumPhysicist committed Feb 7, 2024
1 parent bf96b95 commit d71a65a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum SignArbitraryMessageError {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignedArbitraryMessage {
pub struct ArbitraryMessageSignature {
raw_signature: Vec<u8>,
}

Expand All @@ -67,7 +67,7 @@ pub fn produce_message_challenge(message: &[u8]) -> H256 {
default_hash(wrapped_message)
}

impl SignedArbitraryMessage {
impl ArbitraryMessageSignature {
pub fn from_data(raw_signature: Vec<u8>) -> Self {
Self { raw_signature }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ fn sign_verify_supported_destinations(#[case] seed: Seed) {
let destination_pub_key = Destination::PublicKey(public_key);

for dest in [&destination_addr, &destination_pub_key] {
let sig = SignedArbitraryMessage::produce_uniparty_signature(&private_key, dest, &message)
.unwrap();
let sig =
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, dest, &message)
.unwrap();
let ver_result = sig.verify_signature(&chain_config, dest, &message_challenge);
assert_eq!(ver_result, Ok(()));
}
Expand All @@ -79,14 +80,14 @@ fn sign_verify_unsupported_destination(#[case] seed: Seed) {
let message_challenge = produce_message_challenge(&message);

let random_raw_sig: Vec<u8> = (1..100).map(|_| rng.gen()).collect();
let random_sig = SignedArbitraryMessage {
let random_sig = ArbitraryMessageSignature {
raw_signature: random_raw_sig,
};

// Destination::ClassicMultisig can't be used by produce_uniparty_signature.
let destination = Destination::ClassicMultisig(PublicKeyHash::from(&public_key));
let sig_err =
SignedArbitraryMessage::produce_uniparty_signature(&private_key, &destination, &message)
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, &destination, &message)
.unwrap_err();
assert_eq!(
sig_err,
Expand All @@ -96,7 +97,7 @@ fn sign_verify_unsupported_destination(#[case] seed: Seed) {
// Destination::ScriptHash is unsupported
let destination = Destination::ScriptHash(Id::<_>::new(H256::random_using(&mut rng)));
let sig_err =
SignedArbitraryMessage::produce_uniparty_signature(&private_key, &destination, &message)
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, &destination, &message)
.unwrap_err();
assert_eq!(sig_err, SignArbitraryMessageError::Unsupported);
// Verifying a random signature should also produce an "Unsupported" error.
Expand All @@ -108,7 +109,7 @@ fn sign_verify_unsupported_destination(#[case] seed: Seed) {
// Destination::AnyoneCanSpend makes no sense for this functionality.
let destination = Destination::AnyoneCanSpend;
let sig_err =
SignedArbitraryMessage::produce_uniparty_signature(&private_key, &destination, &message)
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, &destination, &message)
.unwrap_err();
assert_eq!(
sig_err,
Expand Down Expand Up @@ -208,9 +209,12 @@ fn verify_wrong_destination(#[case] seed: Seed) {
];

for (sign_dest, verify_dest, check_func) in test_data {
let sig =
SignedArbitraryMessage::produce_uniparty_signature(&private_key, sign_dest, &message)
.unwrap();
let sig = ArbitraryMessageSignature::produce_uniparty_signature(
&private_key,
sign_dest,
&message,
)
.unwrap();
let ver_result = sig.verify_signature(&chain_config, verify_dest, &message_challenge);
check_func(ver_result);
}
Expand All @@ -236,8 +240,9 @@ fn verify_corrupted_message(#[case] seed: Seed) {
let destination_pub_key = Destination::PublicKey(public_key);

for dest in [&destination_addr, &destination_pub_key] {
let sig = SignedArbitraryMessage::produce_uniparty_signature(&private_key, dest, &message)
.unwrap();
let sig =
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, dest, &message)
.unwrap();
let ver_result = sig.verify_signature(&chain_config, dest, &corrupted_message_challenge);
assert_eq!(
ver_result,
Expand Down Expand Up @@ -265,7 +270,7 @@ fn verify_corrupted_signature(#[case] seed: Seed) {

for dest in [&destination_addr, &destination_pub_key] {
let mut sig =
SignedArbitraryMessage::produce_uniparty_signature(&private_key, dest, &message)
ArbitraryMessageSignature::produce_uniparty_signature(&private_key, dest, &message)
.unwrap();
flip_random_bit(&mut sig.raw_signature, &mut rng);

Expand Down Expand Up @@ -344,7 +349,7 @@ fn signing_transactions_shouldnt_work(#[case] seed: Seed) {
}

// Now try the "arbitrary message" signature.
let msg_sig = SignedArbitraryMessage::produce_uniparty_signature(
let msg_sig = ArbitraryMessageSignature::produce_uniparty_signature(
&private_key,
&destination,
&unhashed_tx_data_to_sign,
Expand Down Expand Up @@ -395,42 +400,42 @@ fn signature_with_chosen_text() {
////////////////////////////////////////////////////////////
// Public key hash verification
////////////////////////////////////////////////////////////
let signature_pubkeyhash = SignedArbitraryMessage::produce_uniparty_signature(
let signature_pubkeyhash = ArbitraryMessageSignature::produce_uniparty_signature(
&private_key,
&destination_pubkeyhash,
message,
)
.unwrap();

SignedArbitraryMessage::from_data(signature_pubkeyhash.raw_signature)
ArbitraryMessageSignature::from_data(signature_pubkeyhash.raw_signature)
.verify_signature(&chain_config, &destination_pubkeyhash, &message_challenge)
.unwrap();

// Ensure the stored signature will always verify correctly
let signature_pubkeyhash_hex = "00030b84796d1e4f528dc7469c03beda6d9158126818ecf0df28e86354246d3de84900fa947e4e502cfa7d608fca02e826606b3c59e20dd14e2694f14152b2947d683cf2ab8df603c57f9706d87fe81fded47f73727ce316ec33cac01da96791f10dfc";
let signature_pubkeyhash_bytes = hex::decode(signature_pubkeyhash_hex).unwrap();
SignedArbitraryMessage::from_data(signature_pubkeyhash_bytes)
ArbitraryMessageSignature::from_data(signature_pubkeyhash_bytes)
.verify_signature(&chain_config, &destination_pubkeyhash, &message_challenge)
.unwrap();
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Public key verification
////////////////////////////////////////////////////////////
let signature_pub_key = SignedArbitraryMessage::produce_uniparty_signature(
let signature_pub_key = ArbitraryMessageSignature::produce_uniparty_signature(
&private_key,
&destination_pub_key,
message,
)
.unwrap();
SignedArbitraryMessage::from_data(signature_pub_key.raw_signature)
ArbitraryMessageSignature::from_data(signature_pub_key.raw_signature)
.verify_signature(&chain_config, &destination_pub_key, &message_challenge)
.unwrap();

// Ensure the stored signature will always verify correctly
let signature_pubkey_hex = "004cf0b83576b35b6684eebcad34b1900d4176d844753665fdf7c042e8cc71d6cfe8a4f9f5c24adfbe8a16e3dead56ea07e2deca4b7bffb1376f04205d6dedbc6e";
let signature_pubkey_bytes = hex::decode(signature_pubkey_hex).unwrap();
SignedArbitraryMessage::from_data(signature_pubkey_bytes)
ArbitraryMessageSignature::from_data(signature_pubkey_bytes)
.verify_signature(&chain_config, &destination_pub_key, &message_challenge)
.unwrap();
////////////////////////////////////////////////////////////
Expand Down
6 changes: 3 additions & 3 deletions wallet/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod utxo_selector;

use common::address::pubkeyhash::PublicKeyHash;
use common::chain::block::timestamp::BlockTimestamp;
use common::chain::signature::inputsig::arbitrary_message::SignedArbitraryMessage;
use common::chain::signature::inputsig::arbitrary_message::ArbitraryMessageSignature;
use common::chain::{AccountCommand, AccountOutPoint, AccountSpending, TransactionCreationError};
use common::primitives::id::WithId;
use common::primitives::{Idable, H256};
Expand Down Expand Up @@ -1240,14 +1240,14 @@ impl Account {
message: Vec<u8>,
destination: Destination,
db_tx: &impl WalletStorageReadUnlocked,
) -> WalletResult<SignedArbitraryMessage> {
) -> WalletResult<ArbitraryMessageSignature> {
let private_key = self
.key_chain
.get_private_key_for_destination(&destination, db_tx)?
.ok_or(WalletError::DestinationNotFromThisWallet)?
.private_key();

let sig = SignedArbitraryMessage::produce_uniparty_signature(
let sig = ArbitraryMessageSignature::produce_uniparty_signature(
&private_key,
&destination,
&message,
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use bip39::{Language, Mnemonic};
use common::address::{Address, AddressError};
use common::chain::block::timestamp::BlockTimestamp;
use common::chain::signature::inputsig::arbitrary_message::{
SignArbitraryMessageError, SignedArbitraryMessage,
ArbitraryMessageSignature, SignArbitraryMessageError,
};
use common::chain::signature::DestinationSigError;
use common::chain::tokens::{
Expand Down Expand Up @@ -1397,7 +1397,7 @@ impl<B: storage::Backend> Wallet<B> {
account_index: U31,
challenge: Vec<u8>,
destination: Destination,
) -> WalletResult<SignedArbitraryMessage> {
) -> WalletResult<ArbitraryMessageSignature> {
self.for_account_rw_unlocked(account_index, |account, db_tx, _| {
account.sign_challenge(challenge, destination, db_tx)
})
Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet-controller/src/synced_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::collections::BTreeSet;
use common::{
address::Address,
chain::{
signature::inputsig::arbitrary_message::SignedArbitraryMessage,
signature::inputsig::arbitrary_message::ArbitraryMessageSignature,
tokens::{
IsTokenFreezable, IsTokenUnfreezable, Metadata, RPCTokenInfo, TokenId, TokenIssuance,
TokenIssuanceV1, TokenTotalSupply,
Expand Down Expand Up @@ -645,7 +645,7 @@ impl<'a, T: NodeInterface, W: WalletEvents> SyncedController<'a, T, W> {
&mut self,
challenge: Vec<u8>,
destination: Destination,
) -> Result<SignedArbitraryMessage, ControllerError<T>> {
) -> Result<ArbitraryMessageSignature, ControllerError<T>> {
self.wallet
.sign_challenge(self.account_index, challenge, destination)
.map_err(ControllerError::WalletError)
Expand Down
6 changes: 3 additions & 3 deletions wallet/wallet-rpc-lib/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use common::{
address::Address,
chain::{
signature::inputsig::arbitrary_message::{
produce_message_challenge, SignedArbitraryMessage,
produce_message_challenge, ArbitraryMessageSignature,
},
tokens::{IsTokenFreezable, IsTokenUnfreezable, Metadata, TokenTotalSupply},
Block, ChainConfig, DelegationId, Destination, GenBlock, PoolId, SignedTransaction,
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<N: NodeInterface + Clone + Send + Sync + 'static> WalletRpc<N> {
account_index: U31,
challenge: Vec<u8>,
address: String,
) -> WRpcResult<SignedArbitraryMessage, N> {
) -> WRpcResult<ArbitraryMessageSignature, N> {
let config = ControllerConfig { in_top_x_mb: 5 }; // irrelevant
let destination = Address::from_str(&self.chain_config, &address)
.and_then(|addr| addr.decode_object(&self.chain_config))
Expand Down Expand Up @@ -460,7 +460,7 @@ impl<N: NodeInterface + Clone + Send + Sync + 'static> WalletRpc<N> {
.map_err(|_| RpcError::InvalidAddress)?;

let message_challenge = produce_message_challenge(&message);
let sig = SignedArbitraryMessage::from_data(signed_challenge);
let sig = ArbitraryMessageSignature::from_data(signed_challenge);
sig.verify_signature(&self.chain_config, &destination, &message_challenge)?;

Ok(())
Expand Down

0 comments on commit d71a65a

Please sign in to comment.