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
mod input_id;
pub use input_id::InputID;
mod bytes;
mod serialize;
mod sign;
mod string;
mod verify;
use crate::{Identifier, Plaintext, ProgramID, Record, Value, ValueType};
use snarkvm_console_account::{Address, ComputeKey, GraphKey, PrivateKey, Signature, ViewKey};
use snarkvm_console_network::Network;
use snarkvm_console_types::prelude::*;
#[derive(Clone, PartialEq, Eq)]
pub struct Request<N: Network> {
caller: Address<N>,
network_id: U16<N>,
program_id: ProgramID<N>,
function_name: Identifier<N>,
input_ids: Vec<InputID<N>>,
inputs: Vec<Value<N>>,
signature: Signature<N>,
sk_tag: Field<N>,
tvk: Field<N>,
tsk: Scalar<N>,
tcm: Field<N>,
}
impl<N: Network>
From<(
Address<N>,
U16<N>,
ProgramID<N>,
Identifier<N>,
Vec<InputID<N>>,
Vec<Value<N>>,
Signature<N>,
Field<N>,
Field<N>,
Scalar<N>,
Field<N>,
)> for Request<N>
{
fn from(
(caller, network_id, program_id, function_name, input_ids, inputs, signature, sk_tag, tvk, tsk, tcm): (
Address<N>,
U16<N>,
ProgramID<N>,
Identifier<N>,
Vec<InputID<N>>,
Vec<Value<N>>,
Signature<N>,
Field<N>,
Field<N>,
Scalar<N>,
Field<N>,
),
) -> Self {
if *network_id != N::ID {
N::halt(format!("Invalid network ID. Expected {}, found {}", N::ID, *network_id))
} else {
Self { caller, network_id, program_id, function_name, input_ids, inputs, signature, sk_tag, tvk, tsk, tcm }
}
}
}
impl<N: Network> Request<N> {
pub const fn caller(&self) -> &Address<N> {
&self.caller
}
pub const fn network_id(&self) -> &U16<N> {
&self.network_id
}
pub const fn program_id(&self) -> &ProgramID<N> {
&self.program_id
}
pub const fn function_name(&self) -> &Identifier<N> {
&self.function_name
}
pub fn input_ids(&self) -> &[InputID<N>] {
&self.input_ids
}
pub fn inputs(&self) -> &[Value<N>] {
&self.inputs
}
pub const fn signature(&self) -> &Signature<N> {
&self.signature
}
pub const fn sk_tag(&self) -> &Field<N> {
&self.sk_tag
}
pub const fn tvk(&self) -> &Field<N> {
&self.tvk
}
pub fn to_tpk(&self) -> Group<N> {
let challenge = self.signature.challenge();
let response = self.signature.response();
let pk_sig = self.signature.compute_key().pk_sig();
(pk_sig * challenge) + N::g_scalar_multiply(&response)
}
pub const fn tsk(&self) -> &Scalar<N> {
&self.tsk
}
pub const fn tcm(&self) -> &Field<N> {
&self.tcm
}
}
#[cfg(test)]
mod test_helpers {
use super::*;
use snarkvm_console_network::Testnet3;
type CurrentNetwork = Testnet3;
const ITERATIONS: u64 = 1000;
pub(super) fn sample_requests(rng: &mut TestRng) -> Vec<Request<CurrentNetwork>> {
(0..ITERATIONS)
.map(|i| {
let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();
let address = Address::try_from(&private_key).unwrap();
let program_id = ProgramID::from_str("token.aleo").unwrap();
let function_name = Identifier::from_str("transfer").unwrap();
let record_string =
format!("{{ owner: {address}.private, gates: {i}u64.private, token_amount: {i}u64.private, _nonce: 2293253577170800572742339369209137467208538700597121244293392265726446806023group.public }}");
let input_constant = Value::from_str(&format!("{{ token_amount: {i}u128 }}")).unwrap();
let input_public = Value::from_str(&format!("{{ token_amount: {i}u128 }}")).unwrap();
let input_private = Value::from_str(&format!("{{ token_amount: {i}u128 }}")).unwrap();
let input_record = Value::from_str(&record_string).unwrap();
let input_external_record = Value::from_str(&record_string).unwrap();
let inputs = vec![input_constant, input_public, input_private, input_record, input_external_record];
let input_types = vec![
ValueType::from_str("amount.constant").unwrap(),
ValueType::from_str("amount.public").unwrap(),
ValueType::from_str("amount.private").unwrap(),
ValueType::from_str("token.record").unwrap(),
ValueType::from_str("token.aleo/token.record").unwrap(),
];
let request =
Request::sign(&private_key, program_id, function_name, &inputs, &input_types, rng).unwrap();
assert!(request.verify(&input_types));
request
})
.collect()
}
}