Skip to main content

forest/lotus_json/
signed_message.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::message::SignedMessage;
5use crate::shim::{crypto::Signature, message::Message};
6use ::cid::Cid;
7
8use super::*;
9
10#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, JsonSchema)]
11#[serde(rename_all = "PascalCase")]
12#[schemars(rename = "SignedMessage")]
13pub struct SignedMessageLotusJson {
14    #[schemars(with = "LotusJson<Message>")]
15    #[serde(with = "crate::lotus_json")]
16    message: Message,
17    #[schemars(with = "LotusJson<Signature>")]
18    #[serde(with = "crate::lotus_json")]
19    signature: Signature,
20    #[schemars(with = "LotusJson<Option<Cid>>")]
21    #[serde(
22        with = "crate::lotus_json",
23        rename = "CID",
24        skip_serializing_if = "Option::is_none",
25        default
26    )]
27    cid: Option<Cid>,
28}
29
30impl SignedMessageLotusJson {
31    pub fn with_cid(mut self, cid: Cid) -> Self {
32        self.cid = Some(cid);
33        self
34    }
35}
36
37impl HasLotusJson for SignedMessage {
38    type LotusJson = SignedMessageLotusJson;
39
40    #[cfg(test)]
41    fn snapshots() -> Vec<(serde_json::Value, Self)> {
42        vec![(
43            json!({
44                "Message": {
45                    "From": "f00",
46                    "GasFeeCap": "0",
47                    "GasLimit": 0,
48                    "GasPremium": "0",
49                    "Method": 0,
50                    "Nonce": 0,
51                    "Params": null,
52                    "To": "f00",
53                    "Value": "0",
54                    "Version": 0,
55                },
56                "Signature": {"Type": 2, "Data": "aGVsbG8gd29ybGQh"},
57                "CID": {
58                    "/": "bafy2bzaced3xdk2uf6azekyxgcttujvy3fzyeqmibtpjf2fxcpfdx2zcx4s3g"
59                },
60            }),
61            SignedMessage {
62                message: Message::default(),
63                signature: Signature {
64                    sig_type: crate::shim::crypto::SignatureType::Bls,
65                    bytes: Vec::from_iter(*b"hello world!"),
66                },
67            },
68        )]
69    }
70
71    fn into_lotus_json(self) -> Self::LotusJson {
72        let cid = Some(self.cid());
73        let Self { message, signature } = self;
74        Self::LotusJson {
75            message,
76            signature,
77            cid,
78        }
79    }
80
81    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
82        let Self::LotusJson {
83            message,
84            signature,
85            cid: _ignored, // See notes on Message
86        } = lotus_json;
87        Self { message, signature }
88    }
89}