-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathmain.nr
97 lines (87 loc) · 3.61 KB
/
main.nr
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
// A contract used along with `StaticParent` contract to test static calls.
contract StaticChild {
use dep::aztec::prelude::{AztecAddress, FunctionSelector, PublicMutable, PrivateSet, PrivateContext, Deserialize};
use dep::aztec::{
context::{PublicContext, gas::GasOpts}, protocol_types::{abis::{call_context::CallContext}},
note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader},
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note
};
use dep::value_note::value_note::ValueNote;
#[aztec(storage)]
struct Storage {
current_value: PublicMutable<Field>,
a_private_value: PrivateSet<ValueNote>,
}
// Returns base_value + chain_id + version + block_number + timestamp statically
#[aztec(public)]
#[aztec(view)]
fn pub_get_value(base_value: Field) -> Field {
let return_value = base_value
+ context.chain_id()
+ context.version()
+ context.block_number()
+ context.timestamp() as Field;
return_value
}
// Sets `current_value` to `new_value`
#[aztec(public)]
fn pub_set_value(new_value: Field) -> Field {
storage.current_value.write(new_value);
context.emit_unencrypted_log(new_value);
new_value
}
// View function that attempts to modify state. Should always fail regardless how it's called.
#[aztec(private)]
#[aztec(view)]
fn private_illegal_set_value(new_value: Field, owner: AztecAddress) -> Field {
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner);
let mut note = ValueNote::new(new_value, owner_npk_m_hash);
storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner));
new_value
}
// Modify a note
#[aztec(private)]
fn private_set_value(
new_value: Field,
owner: AztecAddress,
outgoing_viewer: AztecAddress
) -> Field {
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner);
let mut note = ValueNote::new(new_value, owner_npk_m_hash);
storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner));
new_value
}
// Retrieve note value statically
#[aztec(private)]
#[aztec(view)]
fn private_get_value(amount: Field, owner: AztecAddress) -> Field {
let owner_npk_m_hash = context.get_header().get_npk_m_hash(&mut context, owner);
let mut options = NoteGetterOptions::new();
options = options.select(ValueNote::properties().value, amount, Option::none()).select(
ValueNote::properties().npk_m_hash,
owner_npk_m_hash,
Option::none()
).set_limit(1);
let notes = storage.a_private_value.get_notes(options);
notes.get(0).value
}
// Increments `current_value` by `new_value`
#[aztec(public)]
fn pub_inc_value(new_value: Field) -> Field {
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
context.emit_unencrypted_log(new_value);
new_value
}
// View function that attempts to modify state. Should always fail regardless how it's called.
#[aztec(public)]
#[aztec(view)]
fn pub_illegal_inc_value(new_value: Field) -> Field {
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
context.emit_unencrypted_log(new_value);
new_value
}
}