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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use crate::{
account::{Address, PrivateKey},
dpc::{EmptyLedger, Record},
errors::DPCError,
};
use snarkvm_algorithms::CRH;
use snarkvm_dpc::{
testnet1::{
instantiated::{CommitmentMerkleParameters, Components, InstantiatedDPC, Tx},
parameters::{NoopProgramSNARKParameters, SystemParameters},
TransactionKernel as TransactionKernelNative,
},
DPCComponents,
DPCScheme,
RecordScheme,
*,
};
use snarkvm_utilities::{to_bytes, FromBytes, ToBytes};
use rand::Rng;
use std::{fmt, str::FromStr};
pub type MerkleTreeLedger = EmptyLedger<Tx, CommitmentMerkleParameters>;
#[derive(Clone, Debug)]
pub struct TransactionInput {
pub(crate) private_key: PrivateKey,
pub(crate) record: Record,
}
#[derive(Clone, Debug)]
pub struct TransactionOutput {
pub(crate) recipient: Address,
pub(crate) amount: u64,
}
pub struct TransactionKernel {
pub(crate) transaction_kernel: TransactionKernelNative<Components>,
}
#[derive(Clone, Debug, Default)]
pub struct TransactionKernelBuilder {
pub(crate) inputs: Vec<TransactionInput>,
pub(crate) outputs: Vec<TransactionOutput>,
pub(crate) network_id: u8,
pub(crate) memo: Option<[u8; 32]>,
}
impl TransactionKernelBuilder {
pub fn new() -> Self {
Self {
inputs: vec![],
outputs: vec![],
network_id: 1,
memo: None,
}
}
pub fn add_input(self, private_key: PrivateKey, record: Record) -> Result<Self, DPCError> {
if self.inputs.len() > Components::NUM_INPUT_RECORDS {
return Err(DPCError::InvalidNumberOfInputs(
self.inputs.len() + 1,
Components::NUM_INPUT_RECORDS,
));
}
let input = TransactionInput { private_key, record };
let mut builder = self;
builder.inputs.push(input);
Ok(builder)
}
pub fn add_output(self, recipient: Address, amount: u64) -> Result<Self, DPCError> {
if self.outputs.len() > Components::NUM_OUTPUT_RECORDS {
return Err(DPCError::InvalidNumberOfOutputs(
self.outputs.len() + 1,
Components::NUM_OUTPUT_RECORDS,
));
}
let output = TransactionOutput { recipient, amount };
let mut builder = self;
builder.outputs.push(output);
Ok(builder)
}
pub fn network_id(self, network_id: u8) -> Self {
let mut builder = self;
builder.network_id = network_id;
builder
}
pub fn memo(self, memo: [u8; 32]) -> Self {
let mut builder = self;
builder.memo = Some(memo);
builder
}
pub fn build<R: Rng>(&self, rng: &mut R) -> Result<TransactionKernel, DPCError> {
match self.inputs.len() {
1 | 2 => {}
num_inputs => {
return Err(DPCError::InvalidNumberOfInputs(
num_inputs,
Components::NUM_INPUT_RECORDS,
));
}
}
match self.outputs.len() {
0 => return Err(DPCError::MissingOutputs),
1 | 2 => {}
num_inputs => {
return Err(DPCError::InvalidNumberOfInputs(
num_inputs,
Components::NUM_INPUT_RECORDS,
));
}
}
let mut spenders = vec![];
let mut records_to_spend = vec![];
for input in &self.inputs {
spenders.push(input.private_key.clone());
records_to_spend.push(input.record.clone());
}
let mut recipients = vec![];
let mut recipient_amounts = vec![];
for output in &self.outputs {
recipients.push(output.recipient.clone());
recipient_amounts.push(output.amount);
}
TransactionKernel::new(
spenders,
records_to_spend,
recipients,
recipient_amounts,
self.network_id,
rng,
)
}
}
impl TransactionKernel {
pub(crate) fn new<R: Rng>(
spenders: Vec<PrivateKey>,
records_to_spend: Vec<Record>,
recipients: Vec<Address>,
recipient_amounts: Vec<u64>,
network_id: u8,
rng: &mut R,
) -> Result<Self, DPCError> {
let parameters = SystemParameters::<Components>::load().unwrap();
let noop_program_snark_parameters = NoopProgramSNARKParameters::<Components>::load().unwrap();
assert!(!spenders.is_empty());
assert_eq!(spenders.len(), records_to_spend.len());
assert!(!recipients.is_empty());
assert_eq!(recipients.len(), recipient_amounts.len());
let noop_program_id = to_bytes![
parameters
.program_verification_key_crh
.hash(&to_bytes![noop_program_snark_parameters.verification_key]?)?
]?;
let mut old_records = vec![];
for record in records_to_spend {
old_records.push(record.record);
}
let mut old_account_private_keys = vec![];
for private_key in spenders {
old_account_private_keys.push(private_key.private_key);
}
while old_records.len() < Components::NUM_INPUT_RECORDS {
let sn_randomness: [u8; 32] = rng.gen();
let old_sn_nonce = parameters.serial_number_nonce.hash(&sn_randomness)?;
let private_key = old_account_private_keys[0].clone();
let address = AccountAddress::<Components>::from_private_key(
¶meters.account_signature,
¶meters.account_commitment,
¶meters.account_encryption,
&private_key,
)?;
let dummy_record = InstantiatedDPC::generate_record(
¶meters,
old_sn_nonce,
address,
true,
0,
Default::default(),
noop_program_id.clone(),
noop_program_id.clone(),
rng,
)?;
old_records.push(dummy_record);
old_account_private_keys.push(private_key);
}
assert_eq!(old_records.len(), Components::NUM_INPUT_RECORDS);
for (private_key, record) in old_account_private_keys.iter().zip(&old_records) {
let address = AccountAddress::<Components>::from_private_key(
¶meters.account_signature,
¶meters.account_commitment,
¶meters.account_encryption,
private_key,
)?;
assert_eq!(&address, record.owner());
}
assert_eq!(old_records.len(), Components::NUM_INPUT_RECORDS);
assert_eq!(old_account_private_keys.len(), Components::NUM_INPUT_RECORDS);
let mut new_record_owners = vec![];
let mut new_is_dummy_flags = vec![];
let mut new_values = vec![];
for (recipient, amount) in recipients.iter().zip(recipient_amounts) {
new_record_owners.push(recipient.address.clone());
new_is_dummy_flags.push(false);
new_values.push(amount);
}
while new_record_owners.len() < Components::NUM_OUTPUT_RECORDS {
new_record_owners.push(new_record_owners[0].clone());
new_is_dummy_flags.push(true);
new_values.push(0);
}
assert_eq!(new_record_owners.len(), Components::NUM_OUTPUT_RECORDS);
assert_eq!(new_is_dummy_flags.len(), Components::NUM_OUTPUT_RECORDS);
assert_eq!(new_values.len(), Components::NUM_OUTPUT_RECORDS);
let new_birth_program_ids = vec![noop_program_id.clone(); Components::NUM_OUTPUT_RECORDS];
let new_death_program_ids = vec![noop_program_id; Components::NUM_OUTPUT_RECORDS];
let new_payloads = vec![Default::default(); Components::NUM_OUTPUT_RECORDS];
let memo = rng.gen();
let transaction_kernel = <InstantiatedDPC as DPCScheme<MerkleTreeLedger>>::execute_offline(
parameters,
old_records,
old_account_private_keys,
new_record_owners,
&new_is_dummy_flags,
&new_values,
new_payloads,
new_birth_program_ids,
new_death_program_ids,
memo,
network_id,
rng,
)?;
Ok(Self { transaction_kernel })
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut output = vec![];
self.transaction_kernel
.write(&mut output)
.expect("serialization to bytes failed");
output
}
}
impl FromStr for TransactionKernel {
type Err = DPCError;
fn from_str(transaction_kernel: &str) -> Result<Self, Self::Err> {
Ok(Self {
transaction_kernel: TransactionKernelNative::<Components>::read(&hex::decode(transaction_kernel)?[..])?,
})
}
}
impl fmt::Display for TransactionKernel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
hex::encode(to_bytes![self.transaction_kernel].expect("couldn't serialize to bytes"))
)
}
}