nois/
proxy.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{HexBinary, Timestamp};
3
4/// Max length that the job ID is allowed to have (in bytes)
5///
6/// The value is chosen to be enough for 32 byte hashes (such as sha256)
7/// in hex representation. But the choice of the job ID format is up to the
8/// dapp and can be anything that respects this length limit.
9pub const MAX_JOB_ID_LEN: usize = 64;
10
11#[cw_serde]
12pub enum ProxyExecuteMsg {
13    /// Gets the next randomness.
14    GetNextRandomness {
15        /// A job ID chosen by the caller.
16        ///
17        /// Then length of this must not exceed [`MAX_JOB_ID_LEN`].
18        job_id: String,
19    },
20    /// Gets a randomness that is published after the provided timestamp.
21    ///
22    /// For example you can request a randomness in e.g. 25 hours for a game
23    /// round that runs for the upcoming 24 hours.
24    ///
25    /// Working with this message is only inteded for advanced use cases.
26    /// You need to ensure in the calling app that no action can be performed
27    /// anymore once `after` is reached. You need to consider that the BFT blocktime
28    /// can be behind and add an appriate safety margin.
29    GetRandomnessAfter {
30        /// The publish time of the randomness needs to be > `after`.
31        after: Timestamp,
32        /// A job ID chosen by the caller.
33        ///
34        /// Then length of this must not exceed [`MAX_JOB_ID_LEN`].
35        job_id: String,
36    },
37}
38
39/// This must be accepted in an `NoisReceive { callback: NoisCallback }` enum case
40/// in the ExecuteMsg of the app.
41#[cw_serde]
42pub struct NoisCallback {
43    /// The ID chosen by the caller for this job. Use this field to map responses to requests.
44    pub job_id: String,
45    /// The point in time when the randomness was first published. This information is provided
46    /// by the randomness provider. This is not the time when the randomness was processed on chain.
47    pub published: Timestamp,
48    /// The randomness. This is guaranteed to be 32 bytes long.
49    pub randomness: HexBinary,
50}
51
52/// This is just a helper to properly serialize the above callback.
53/// The actual receiver should include this variant in the larger ExecuteMsg enum.
54#[cw_serde]
55pub enum ReceiverExecuteMsg {
56    /// This is sent as `{"nois_receive": {"callback": {"job_id": "...", "randomness": "aabbddff.."}}}`
57    /// to the contract. We prefix the enum variant with `nois_` in order to avoid
58    /// a collision with other contracts (see https://github.com/noislabs/nois/issues/4).
59    NoisReceive { callback: NoisCallback },
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use cosmwasm_std::to_json_vec;
66
67    #[test]
68    fn receiver_execute_msg_serializes_nicely() {
69        let msg = ReceiverExecuteMsg::NoisReceive {
70            callback: NoisCallback {
71                job_id: "first".to_string(),
72                published: Timestamp::from_seconds(1682086395),
73                randomness: HexBinary::from_hex(
74                    "aabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccdd",
75                )
76                .unwrap(),
77            },
78        };
79        let ser = to_json_vec(&msg).unwrap();
80        assert_eq!(
81            ser,
82            br#"{"nois_receive":{"callback":{"job_id":"first","published":"1682086395000000000","randomness":"aabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccdd"}}}"#
83        );
84    }
85}