1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

/// These tests use the `serialization-vectors` submodule at the root of this repo
use crate::blocks::CachingBlockHeader;
use crate::message::signed_message::SignedMessage;
use crate::shim::{crypto::Signature, message::Message};
use bls_signatures::{PrivateKey, Serialize as _};
use cid::Cid;
use serde::Deserialize;

#[test]
fn header_cbor_vectors() {
    #[derive(Deserialize)]
    struct Case {
        #[serde(with = "crate::lotus_json")]
        block: CachingBlockHeader,
        #[serde(with = "hex")]
        cbor_hex: Vec<u8>,
        #[serde(with = "crate::lotus_json::stringify")] // yes this isn't CidLotusJson...
        cid: Cid,
    }

    let s = include_str!("serialization-vectors/block_headers.json");

    let cases: Vec<Case> = serde_json::from_str(s).expect("Test vector deserialization failed");

    for Case {
        block,
        cbor_hex,
        cid,
    } in cases
    {
        assert_eq!(cbor_hex, fvm_ipld_encoding::to_vec(&block).unwrap());
        assert_eq!(*block.cid(), cid);
    }
}

#[test]
fn signing_test() {
    #[derive(Deserialize)]
    #[serde(rename_all = "PascalCase")]
    struct Case {
        #[serde(with = "crate::lotus_json")]
        unsigned: Message,
        #[serde(with = "crate::lotus_json::stringify")] // yes this isn't CidLotusJson...
        cid: Cid,
        #[serde(with = "crate::lotus_json::base64_standard")]
        private_key: Vec<u8>,
        #[serde(with = "crate::lotus_json")]
        signature: Signature,
    }

    let s = include_str!("serialization-vectors/message_signing.json");

    let cases: Vec<Case> = serde_json::from_str(s).expect("Test vector deserialization failed");

    for Case {
        unsigned,
        cid: expected_cid,
        private_key,
        signature,
    } in cases
    {
        let priv_key = PrivateKey::from_bytes(&private_key).unwrap();
        let msg_sign_bz = unsigned.cid().to_bytes();
        let bls_sig = priv_key.sign(&msg_sign_bz);
        let sig = Signature::new_bls(bls_sig.as_bytes());
        assert_eq!(sig, signature);

        let smsg = SignedMessage::new_from_parts(unsigned, sig).unwrap();
        let actual_cid = smsg.cid();

        assert_eq!(actual_cid, expected_cid);
    }
}

#[test]
fn unsigned_message_cbor_vectors() {
    #[derive(Deserialize)]
    struct Case {
        #[serde(with = "crate::lotus_json")]
        message: Message,
        #[serde(with = "hex")]
        hex_cbor: Vec<u8>,
    }

    let s = include_str!("serialization-vectors/unsigned_messages.json");

    let vectors: Vec<Case> = serde_json::from_str(s).expect("Test vector deserialization failed");
    for Case {
        message,
        hex_cbor: expected_cbor,
    } in vectors
    {
        let actual_cbor: Vec<u8> = fvm_ipld_encoding::to_vec(&message).unwrap();
        assert_eq!(expected_cbor, actual_cbor);
    }
}