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

Test Runner for Message Signing Serialization Vectors #548

Merged
merged 12 commits into from
Jul 18, 2020
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/serialization_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ forest_message = { path = "../../vm/message", features = ["json"] }
encoding = { package = "forest_encoding", path = "../../encoding" }
forest_blocks = { path = "../../blockchain/blocks", features = ["json"] }
num-traits = "0.2"
bls-signatures = "0.6.0"
50 changes: 50 additions & 0 deletions tests/serialization_tests/tests/signing_ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

// Doesn't run these unless feature specified
#![cfg(feature = "submodule_tests")]

use bls_signatures::{PrivateKey, Serialize};
use cid::Cid;
use crypto::{signature, Signature};
use encoding::Cbor;
use forest_message::{unsigned_message, SignedMessage, UnsignedMessage};
use serde::Deserialize;
use std::fs::File;
use std::io::prelude::*;
use std::str::FromStr;

#[derive(Deserialize)]
#[serde(rename_all = "PascalCase")]
struct TestVec {
#[serde(with = "unsigned_message::json")]
unsigned: UnsignedMessage,
cid: String,
private_key: String,
#[serde(with = "signature::json")]
signature: Signature,
}

#[test]
fn signing_test() {
let mut file = File::open("../serialization-vectors/message_signing.json").unwrap();
let mut string = String::new();
file.read_to_string(&mut string).unwrap();

let vectors: Vec<TestVec> =
serde_json::from_str(&string).expect("Test vector deserialization failed");
for test_vec in vectors {
let test = base64::decode(test_vec.private_key).unwrap();
// TODO set up a private key based on sig type
let priv_key = PrivateKey::from_bytes(&test).unwrap();
flodesi marked this conversation as resolved.
Show resolved Hide resolved
let cid = test_vec.unsigned.cid().unwrap();
let sig = priv_key.sign(cid.to_bytes().as_slice());
let msg_sig = Signature::new_bls(sig.as_bytes());
let signed_message = SignedMessage::new_from_parts(test_vec.unsigned, msg_sig);
let cid = signed_message.cid().unwrap();
let cid_test = Cid::from_str(&test_vec.cid).unwrap();

assert_eq!(sig.as_bytes().as_slice(), test_vec.signature.bytes());
assert_eq!(cid, cid_test);
}
}
9 changes: 6 additions & 3 deletions vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub mod json {
gas_limit: u64,
#[serde(rename = "Method")]
method_num: u64,
params: String,
params: Option<String>,
}

pub fn serialize<S>(m: &UnsignedMessage, serializer: S) -> Result<S::Ok, S::Error>
Expand All @@ -199,7 +199,7 @@ pub mod json {
gas_price: m.gas_price.to_string(),
gas_limit: m.gas_limit,
method_num: m.method_num,
params: base64::encode(m.params.bytes()),
params: Some(base64::encode(m.params.bytes())),
}
.serialize(serializer)
}
Expand All @@ -218,7 +218,10 @@ pub mod json {
gas_price: m.gas_price.parse().map_err(de::Error::custom)?,
gas_limit: m.gas_limit,
method_num: m.method_num,
params: Serialized::new(base64::decode(&m.params).map_err(de::Error::custom)?),
params: Serialized::new(
base64::decode(&m.params.unwrap_or_else(|| "".to_string()))
.map_err(de::Error::custom)?,
),
})
}

Expand Down