algokit_transact/transactions/
state_proof.rs

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! State proof transaction module for AlgoKit Core.
//!
//! This module provides functionality for decoding state proof transactions.

use crate::Transaction;
use crate::transactions::common::TransactionHeader;
use crate::utils::{is_zero, is_zero_opt};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use serde_with::{Bytes, serde_as};
use std::collections::BTreeMap;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Builder)]
#[builder(name = "HashFactoryBuilder")]
pub struct HashFactory {
    #[serde(rename = "t")]
    pub hash_type: u64,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct MerkleArrayProof {
    #[serde(rename = "pth")]
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[serde_as(as = "Vec<Bytes>")]
    pub path: Vec<Vec<u8>>,

    #[serde(rename = "hsh")]
    pub hash_factory: HashFactory,

    #[serde(rename = "td")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub tree_depth: u64,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct MerkleSignatureVerifier {
    #[serde(rename = "cmt")]
    #[serde_as(as = "Bytes")]
    pub commitment: [u8; 64],

    #[serde(rename = "lf")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub key_lifetime: u64,
}

/// A Participant corresponds to an account whose AccountData.Status is Online, and for which the
/// expected sigRound satisfies AccountData.VoteFirstValid <= sigRound <= AccountData.VoteLastValid.
///
/// In the Algorand ledger, it is possible for multiple accounts to have the same PK. Thus, the PK is
/// not necessarily unique among Participants. However, each account will produce a unique Participant
/// struct, to avoid potential DoS attacks where one account claims to have the same VoteID PK as
/// another account.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Participant {
    #[serde(rename = "p")]
    pub verifier: MerkleSignatureVerifier,

    #[serde(rename = "w")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub weight: u64,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct FalconVerifier {
    #[serde(rename = "k")]
    #[serde_as(as = "Bytes")]
    pub public_key: Vec<u8>,
}

/// Represents a signature in the merkle signature scheme using falcon signatures
/// as an underlying crypto scheme. It consists of an ephemeral public key, a signature, a merkle
/// verification path and an index. The merkle signature considered valid only if the Signature is
/// verified under the ephemeral public key and the Merkle verification path verifies that the
/// ephemeral public key is located at the given index of the tree (for the root given in the
/// long-term public key). More details can be found on Algorand's spec
#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct FalconSignatureStruct {
    #[serde(rename = "sig")]
    #[serde_as(as = "Bytes")]
    pub signature: Vec<u8>,

    #[serde(rename = "idx")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub vector_commitment_index: u64,

    #[serde(rename = "prf")]
    pub proof: MerkleArrayProof,

    #[serde(rename = "vkey")]
    pub verifying_key: FalconVerifier,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct SigslotCommit {
    #[serde(rename = "s")]
    pub sig: FalconSignatureStruct,

    #[serde(rename = "l")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub lower_sig_weight: u64,
}

/// A single array position revealed as part of a state proof. It reveals an element of the
/// signature array and the corresponding element of the participants array.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct Reveal {
    #[serde(rename = "s")]
    pub sigslot: SigslotCommit,

    #[serde(rename = "p")]
    pub participant: Participant,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct StateProof {
    #[serde(rename = "c")]
    #[serde_as(as = "Bytes")]
    pub sig_commit: Vec<u8>,

    #[serde(rename = "w")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub signed_weight: u64,

    #[serde(rename = "S")]
    pub sig_proofs: MerkleArrayProof,

    #[serde(rename = "P")]
    pub part_proofs: MerkleArrayProof,

    #[serde(rename = "v")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub merkle_signature_salt_version: u64,

    /// A sparse map from the position being revealed to the corresponding elements from the
    /// sigs and participants arrays.
    #[serde(rename = "r")]
    #[serde(default)]
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub reveals: BTreeMap<u64, Reveal>,

    #[serde(rename = "pr")]
    #[serde(default)]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub positions_to_reveal: Vec<u64>,
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct StateProofMessage {
    #[serde(rename = "b")]
    #[serde_as(as = "Bytes")]
    pub block_headers_commitment: Vec<u8>,

    #[serde(rename = "v")]
    #[serde_as(as = "Bytes")]
    pub voters_commitment: Vec<u8>,

    #[serde(rename = "P")]
    pub ln_proven_weight: u64,

    #[serde(rename = "f")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub first_attested_round: u64,

    #[serde(rename = "l")]
    #[serde(default)]
    #[serde(skip_serializing_if = "is_zero")]
    pub last_attested_round: u64,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Builder)]
#[builder(
    name = "StateProofTransactionBuilder",
    setter(strip_option),
    build_fn(name = "build_fields")
)]
pub struct StateProofTransactionFields {
    #[serde(flatten)]
    pub header: TransactionHeader,

    #[serde(rename = "sptype")]
    #[serde(skip_serializing_if = "is_zero_opt")]
    #[serde(default)]
    #[builder(default)]
    pub state_proof_type: Option<u64>,

    #[serde(rename = "sp")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    #[builder(default)]
    pub state_proof: Option<StateProof>,

    #[serde(rename = "spmsg")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    #[builder(default)]
    pub message: Option<StateProofMessage>,
}

impl StateProofTransactionBuilder {
    pub fn build(&self) -> Result<Transaction, StateProofTransactionBuilderError> {
        self.build_fields().map(Transaction::StateProof)
    }
}

#[cfg(test)]
mod tests {
    use crate::test_utils::TestDataMother;

    #[test]
    fn test_state_proof_snapshot() {
        let data = TestDataMother::state_proof();
        assert_eq!(
            data.id,
            String::from("6D3MLKOASKUXHFTTWYUG563UBKZ5RW3FFKN6ZUUWBCY47RZT3HIA")
        );
    }
}