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

Update message types function signatures #170

Merged
merged 3 commits into from
Jan 17, 2020
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
2 changes: 1 addition & 1 deletion crypto/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ use std::error::Error;

/// Signer is a trait which allows a key implementation to sign data for an address
pub trait Signer {
fn sign_bytes(&self, data: Vec<u8>, address: Address) -> Result<Signature, Box<dyn Error>>;
fn sign_bytes(&self, data: Vec<u8>, address: &Address) -> Result<Signature, Box<dyn Error>>;
}
14 changes: 7 additions & 7 deletions vm/message/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ use vm::{MethodNum, MethodParams, TokenAmount};

pub trait Message {
/// from returns the from address of the message
fn from(&self) -> Address;
fn from(&self) -> &Address;
/// to returns the destination address of the message
fn to(&self) -> Address;
fn to(&self) -> &Address;
/// sequence returns the message sequence or nonce
fn sequence(&self) -> u64;
/// value returns the amount sent in message
fn value(&self) -> TokenAmount;
fn value(&self) -> &TokenAmount;
/// method_num returns the method number to be called
fn method_num(&self) -> MethodNum;
fn method_num(&self) -> &MethodNum;
/// params returns the encoded parameters for the method call
fn params(&self) -> MethodParams;
fn params(&self) -> &MethodParams;
/// gas_price returns gas price for the message
fn gas_price(&self) -> BigUint;
fn gas_price(&self) -> &BigUint;
/// gas_limit returns the gas limit for the message
fn gas_limit(&self) -> BigUint;
fn gas_limit(&self) -> &BigUint;
}
26 changes: 13 additions & 13 deletions vm/message/src/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,55 @@ pub struct SignedMessage {
// https://github.com/ChainSafe/ferret/issues/143

impl SignedMessage {
pub fn new(msg: &UnsignedMessage, s: &impl Signer) -> Result<SignedMessage, CryptoError> {
pub fn new<S: Signer>(msg: &UnsignedMessage, signer: &S) -> Result<Self, CryptoError> {
let bz = msg.marshal_cbor()?;

let sig = s.sign_bytes(bz, msg.from())?;
let sig = signer.sign_bytes(bz, msg.from())?;

Ok(SignedMessage {
message: msg.clone(),
signature: sig,
})
}
pub fn message(&self) -> UnsignedMessage {
self.message.clone()
pub fn message(&self) -> &UnsignedMessage {
&self.message
}
pub fn signature(&self) -> Signature {
self.signature.clone()
pub fn signature(&self) -> &Signature {
&self.signature
}
}

impl Message for SignedMessage {
/// from returns the from address of the message
fn from(&self) -> Address {
fn from(&self) -> &Address {
self.message.from()
}
/// to returns the destination address of the message
fn to(&self) -> Address {
fn to(&self) -> &Address {
self.message.to()
}
/// sequence returns the message sequence or nonce
fn sequence(&self) -> u64 {
self.message.sequence()
}
/// value returns the amount sent in message
fn value(&self) -> TokenAmount {
fn value(&self) -> &TokenAmount {
self.message.value()
}
/// method_num returns the method number to be called
fn method_num(&self) -> MethodNum {
fn method_num(&self) -> &MethodNum {
self.message.method_num()
}
/// params returns the encoded parameters for the method call
fn params(&self) -> MethodParams {
fn params(&self) -> &MethodParams {
self.message.params()
}
/// gas_price returns gas price for the message
fn gas_price(&self) -> BigUint {
fn gas_price(&self) -> &BigUint {
self.message.gas_price()
}
/// gas_limit returns the gas limit for the message
fn gas_limit(&self) -> BigUint {
fn gas_limit(&self) -> &BigUint {
self.message.gas_limit()
}
}
Expand Down
28 changes: 14 additions & 14 deletions vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,36 @@ impl UnsignedMessage {

impl Message for UnsignedMessage {
/// from returns the from address of the message
fn from(&self) -> Address {
self.from.clone()
fn from(&self) -> &Address {
&self.from
}
/// to returns the destination address of the message
fn to(&self) -> Address {
self.to.clone()
fn to(&self) -> &Address {
&self.to
}
/// sequence returns the message sequence or nonce
fn sequence(&self) -> u64 {
self.sequence
}
/// value returns the amount sent in message
fn value(&self) -> TokenAmount {
self.value.clone()
fn value(&self) -> &TokenAmount {
&self.value
}
/// method_num returns the method number to be called
fn method_num(&self) -> MethodNum {
self.method_num.clone()
fn method_num(&self) -> &MethodNum {
&self.method_num
}
/// params returns the encoded parameters for the method call
fn params(&self) -> MethodParams {
self.params.clone()
fn params(&self) -> &MethodParams {
&self.params
}
/// gas_price returns gas price for the message
fn gas_price(&self) -> BigUint {
self.gas_price.clone()
fn gas_price(&self) -> &BigUint {
&self.gas_price
}
/// gas_limit returns the gas limit for the message
fn gas_limit(&self) -> BigUint {
self.gas_limit.clone()
fn gas_limit(&self) -> &BigUint {
&self.gas_limit
}
}

Expand Down
24 changes: 12 additions & 12 deletions vm/message/tests/builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const DUMMY_SIG: [u8; 1] = [0u8];

struct DummySigner;
impl Signer for DummySigner {
fn sign_bytes(&self, _: Vec<u8>, _: Address) -> Result<Signature, Box<dyn Error>> {
fn sign_bytes(&self, _: Vec<u8>, _: &Address) -> Result<Signature, Box<dyn Error>> {
Ok(DUMMY_SIG.to_vec())
}
}
Expand All @@ -33,14 +33,14 @@ fn unsigned_message_builder() {
.gas_price(BigUint::default())
.build()
.unwrap();
assert_eq!(message.from(), from_addr.clone());
assert_eq!(message.to(), to_addr.clone());
assert_eq!(message.from(), &from_addr.clone());
assert_eq!(message.to(), &to_addr.clone());
assert_eq!(message.sequence(), 0);
assert_eq!(message.method_num(), MethodNum::default());
assert_eq!(message.params(), MethodParams::default());
assert_eq!(message.value(), TokenAmount::new(0));
assert_eq!(message.gas_price(), BigUint::default());
assert_eq!(message.gas_limit(), BigUint::default());
assert_eq!(message.method_num(), &MethodNum::default());
assert_eq!(message.params(), &MethodParams::default());
assert_eq!(message.value(), &TokenAmount::new(0));
assert_eq!(message.gas_price(), &BigUint::default());
assert_eq!(message.gas_limit(), &BigUint::default());
let mut mb = UnsignedMessage::builder();
mb.to(to_addr.clone());
mb.from(from_addr.clone());
Expand All @@ -50,8 +50,8 @@ fn unsigned_message_builder() {
}
// test unwrapping
let u_msg = mb.build().unwrap();
assert_eq!(u_msg.from(), from_addr.clone());
assert_eq!(u_msg.to(), to_addr.clone());
assert_eq!(u_msg.from(), &from_addr.clone());
assert_eq!(u_msg.to(), &to_addr.clone());
assert_eq!(u_msg.sequence(), 1);
}

Expand All @@ -66,6 +66,6 @@ fn generate_signed_message() {
let signed_msg = SignedMessage::new(&unsigned_msg, &DummySigner).unwrap();

// Assert message and signature are expected
assert_eq!(signed_msg.message(), unsigned_msg);
assert_eq!(signed_msg.signature(), DUMMY_SIG.to_vec());
assert_eq!(signed_msg.message(), &unsigned_msg);
assert_eq!(signed_msg.signature(), &DUMMY_SIG.to_vec());
}