-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmod.rs
More file actions
142 lines (114 loc) · 4.05 KB
/
mod.rs
File metadata and controls
142 lines (114 loc) · 4.05 KB
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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the Aleo SDK library.
// The Aleo SDK library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Aleo SDK library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Aleo SDK library. If not, see <https://www.gnu.org/licenses/>.
mod address;
pub use address::Address;
mod compute_key;
pub use compute_key::ComputeKey;
mod private_key;
pub use private_key::PrivateKey;
mod record;
pub use record::{RecordCiphertext, RecordPlaintext};
mod signature;
pub use signature::Signature;
mod text;
pub use text::{Ciphertext, Plaintext};
mod view_key;
pub use view_key::ViewKey;
use pyo3::prelude::*;
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
/// Key management class. Enables the creation of a new Aleo Account,
/// importation of an existing account from an existing private key,
/// and message signing and verification functionality.
#[pyclass(frozen)]
pub struct Account {
private_key: PrivateKey,
view_key: ViewKey,
address: Address,
}
#[pymethods]
impl Account {
/// Generates a new account using a cryptographically secure random number generator
#[new]
fn new() -> Self {
Self::from(PrivateKey::new())
}
/// Creates a new account from the given private key.
#[staticmethod]
fn from_private_key(private_key: PrivateKey) -> Self {
let view_key = private_key.view_key();
let address = private_key.address().unwrap();
Self {
private_key,
view_key,
address,
}
}
/// Returns an account private key.
fn private_key(&self) -> PrivateKey {
self.private_key
}
/// Returns an account view key.
fn view_key(&self) -> ViewKey {
self.view_key.clone()
}
/// Returns an account address.
fn address(&self) -> Address {
self.address.clone()
}
/// Returns a signature for the given message (as bytes)
fn sign(&self, message: &[u8]) -> anyhow::Result<Signature> {
self.private_key.sign(message)
}
/// Returns a signature for the given message (as an aleo value)
fn sign_value(&self, value: &str) -> anyhow::Result<Signature> {
self.private_key.sign_value(value)
}
/// Verifies the signature of the given message.
fn verify(&self, signature: &Signature, message: &[u8]) -> bool {
signature.verify(&self.address, message)
}
/// Verifies the signature of the given value.
fn verify_value(&self, signature: &Signature, value: &str) -> bool {
signature.verify_value(&self.address, value)
}
/// Decrypts a record ciphertext with a view key
fn decrypt(&self, record_ciphertext: &RecordCiphertext) -> anyhow::Result<RecordPlaintext> {
record_ciphertext.decrypt(&self.view_key)
}
/// Determines whether the record belongs to the account.
pub fn is_owner(&self, record_ciphertext: &RecordCiphertext) -> bool {
record_ciphertext.is_owner(&self.view_key)
}
fn __str__(&self) -> String {
self.address.to_string()
}
fn __eq__(&self, other: &Self) -> bool {
self.private_key == other.private_key
}
fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
// because it's enough to hash the private key only we add a dummy string so that:
// hash(Account) != hash(PrivateKey)
"account".hash(&mut hasher);
self.private_key.hash(&mut hasher);
hasher.finish()
}
}
impl From<PrivateKey> for Account {
fn from(private_key: PrivateKey) -> Self {
Self::from_private_key(private_key)
}
}