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"
42 changes: 42 additions & 0 deletions tests/serialization_tests/tests/signing_ser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

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

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

#[derive(Deserialize)]
struct TestVec {
#[serde(with = "unsigned_message::json")]
Unsigned: UnsignedMessage,
Cid: String,
CidHexBytes: String,
PrivateKey: String,
#[serde(with = "signature::json")]
Signature: Signature,
}
flodesi marked this conversation as resolved.
Show resolved Hide resolved

#[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.PrivateKey).unwrap();
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());
assert_eq!(sig.as_bytes().as_slice(), test_vec.Signature.bytes());
flodesi marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 24 additions & 0 deletions vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ impl Message for UnsignedMessage {

impl Cbor for UnsignedMessage {}

mod params_serde {
use super::*;
use serde::Serializer;

pub fn serialize<S>(param: &str, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_str(param)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<String> = Option::deserialize(deserializer)?;
if let Some(s) = s {
return Ok(s);
}
Ok("".to_string())
}
}

robfusco marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "json")]
pub mod json {
use super::*;
Expand Down Expand Up @@ -183,6 +206,7 @@ pub mod json {
gas_limit: u64,
#[serde(rename = "Method")]
method_num: u64,
#[serde(with = "params_serde")]
params: String,
}

Expand Down