-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapi.rs
140 lines (121 loc) · 5.38 KB
/
api.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
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// Copyright 2020 Kodebox, Inc.
// This file is part of Parity.
// Parity 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.
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
extern crate codechain_keystore as ckeystore;
mod util;
use ckey::{verify, Ed25519KeyPair as KeyPair, Ed25519Private as Private, Ed25519Public as Public, KeyPairTrait};
use ckeystore::accounts_dir::RootDiskDirectory;
use ckeystore::{KeyStore, SimpleSecretStore};
use primitives::H256;
use std::str::FromStr;
use util::TransientDir;
#[test]
fn secret_store_create() {
let dir = TransientDir::create().unwrap();
let _ = KeyStore::open(Box::new(dir)).unwrap();
}
#[test]
#[should_panic]
fn secret_store_open_not_existing() {
let dir = TransientDir::open();
let _ = KeyStore::open(Box::new(dir)).unwrap();
}
fn random_secret() -> Private {
Private::random()
}
#[test]
fn secret_store_create_account() {
let dir = TransientDir::create().unwrap();
let store = KeyStore::open(Box::new(dir)).unwrap();
assert_eq!(store.accounts().unwrap().len(), 0);
assert!(store.insert_account(random_secret(), &"".into()).is_ok());
assert_eq!(store.accounts().unwrap().len(), 1);
assert!(store.insert_account(random_secret(), &"".into()).is_ok());
assert_eq!(store.accounts().unwrap().len(), 2);
}
#[test]
fn secret_store_sign() {
let dir = TransientDir::create().unwrap();
let store = KeyStore::open(Box::new(dir)).unwrap();
assert!(store.insert_account(random_secret(), &"".into()).is_ok());
let accounts = store.accounts().unwrap();
assert_eq!(accounts.len(), 1);
assert!(store.decrypt_account(&accounts[0], &"".into()).is_ok());
assert!(store.decrypt_account(&accounts[0], &"1".into()).is_err());
}
#[test]
fn secret_store_change_password() {
let dir = TransientDir::create().unwrap();
let store = KeyStore::open(Box::new(dir)).unwrap();
assert!(store.insert_account(random_secret(), &"".into()).is_ok());
let accounts = store.accounts().unwrap();
assert_eq!(accounts.len(), 1);
assert!(store.decrypt_account(&accounts[0], &"".into()).is_ok());
assert!(store.change_password(&accounts[0], &"".into(), &"1".into()).is_ok());
assert!(store.decrypt_account(&accounts[0], &"".into()).is_err());
assert!(store.decrypt_account(&accounts[0], &"1".into()).is_ok());
}
#[test]
fn secret_store_remove_account() {
let dir = TransientDir::create().unwrap();
let store = KeyStore::open(Box::new(dir)).unwrap();
assert!(store.insert_account(random_secret(), &"".into()).is_ok());
let accounts = store.accounts().unwrap();
assert_eq!(accounts.len(), 1);
assert!(store.remove_account(&accounts[0]).is_ok());
assert_eq!(store.accounts().unwrap().len(), 0);
assert!(store.remove_account(&accounts[0]).is_err());
}
fn pat_path() -> &'static str {
match ::std::fs::metadata("keystore") {
Ok(_) => "keystore/tests/res/pat",
Err(_) => "tests/res/pat",
}
}
fn ciphertext_path() -> &'static str {
match ::std::fs::metadata("keystore") {
Ok(_) => "keystore/tests/res/ciphertext",
Err(_) => "tests/res/ciphertext",
}
}
#[test]
fn secret_store_load_pat_files() {
let dir = RootDiskDirectory::at(pat_path());
let store = KeyStore::open(Box::new(dir)).unwrap();
assert_eq!(store.accounts().unwrap(), vec![
Public::from_str("800a29dbeab141ada7923517e945bf4594917473809547bc0bb2e47cd39ac94b").unwrap(),
Public::from_str("9f3f180b63b95559a95735385e35cd973c3d4e9f81bbf0faa61cf6159841feb5").unwrap()
]);
}
#[test]
fn decrypting_files_with_short_ciphertext() {
// 800a29dbeab141ada7923517e945bf4594917473809547bc0bb2e47cd39ac94b
let kp1: KeyPair =
KeyPair::from_private("f52e5b0b80c5e7b6fcd64869dd5f4dc84763395b05620209fcc11e7436f0ac05800a29dbeab141ada7923517e945bf4594917473809547bc0bb2e47cd39ac94b".parse().unwrap())
;
// 9f3f180b63b95559a95735385e35cd973c3d4e9f81bbf0faa61cf6159841feb5
let kp2: KeyPair =
KeyPair::from_private("44795b6f434fde613af66cb01fe14ecf51f0f610b7db38ce3b369e15a49016709f3f180b63b95559a95735385e35cd973c3d4e9f81bbf0faa61cf6159841feb5".parse().unwrap());
let dir = RootDiskDirectory::at(ciphertext_path());
let store = KeyStore::open(Box::new(dir)).unwrap();
let accounts = store.accounts().unwrap();
assert_eq!(accounts, vec![
Public::from_str("800a29dbeab141ada7923517e945bf4594917473809547bc0bb2e47cd39ac94b").unwrap(),
Public::from_str("9f3f180b63b95559a95735385e35cd973c3d4e9f81bbf0faa61cf6159841feb5").unwrap()
]);
let message = H256::random();
let s1 = store.decrypt_account(&accounts[0], &"password".into()).unwrap().sign(&message).unwrap();
let s2 = store.decrypt_account(&accounts[1], &"password".into()).unwrap().sign(&message).unwrap();
assert!(verify(&s1, message.as_ref(), kp1.public()));
assert!(verify(&s2, message.as_ref(), kp2.public()));
}