3
3
extern crate rand;
4
4
extern crate alloc;
5
5
extern crate ark_secp256k1;
6
- use autct :: key_processing:: create_fake_privkeys_values_files;
7
- use autct :: rpcclient;
8
- use autct :: rpcserver;
9
- use autct :: config:: { AutctConfig , get_params_from_config_string} ;
10
- use autct :: utils:: write_file_string;
6
+ use crate :: key_processing:: create_fake_privkeys_values_files;
7
+ use crate :: rpcclient;
8
+ // use crate ::rpcserver;
9
+ use crate :: config:: { AutctConfig , get_params_from_config_string} ;
10
+ use crate :: utils:: write_file_string;
11
11
use std:: io:: Write ;
12
12
use std:: error:: Error ;
13
13
use base64:: prelude:: * ;
14
- use autct:: encryption:: { encrypt, decrypt} ;
15
-
16
- #[ tokio:: main]
17
- async fn main ( ) -> Result < ( ) , Box < dyn Error > > {
18
- let autctcfg = AutctConfig :: build ( ) ?;
19
- match autctcfg. clone ( ) . mode . unwrap ( ) . as_str ( ) {
20
- "prove" => { return request_prove ( autctcfg) . await
21
- } ,
22
- "verify" => { return request_verify ( autctcfg) . await } ,
23
- "serve" => { return rpcserver:: do_serve ( autctcfg) . await
24
- } ,
25
- "newkeys" => { return request_create_keys ( autctcfg) . await } ,
26
- "auditprove" => { return request_audit ( autctcfg) . await } ,
27
- "auditverify" => { return request_audit_verify ( autctcfg) . await } ,
28
- // this extra tool is really just for testing:
29
- "encryptkey" => { return request_encrypt_key ( autctcfg) . await } ,
30
- // extra tool for exporting the key:
31
- "decryptkey" => { return request_decrypted_key ( autctcfg) . await } ,
32
- // extra (undocumented) tool for creating test files:
33
- "audittestgen" => { return create_test_data ( autctcfg) . await } ,
34
- _ => { return Err ( "Invalid mode, must be 'prove', 'auditprove', 'serve', 'newkeys', 'encryptkey', 'decryptkey' or 'verify'" . into ( ) ) } ,
35
-
36
- }
37
- }
14
+ use crate :: encryption:: { encrypt, decrypt} ;
38
15
39
16
/// An undocumented tool for creating test files.
40
17
/// Note that this misuses config vars as follows:
@@ -46,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error>>{
46
23
/// the proof, along with the value for each index, using colon
47
24
/// separated pairs. For example:
48
25
/// ./autct -M audittestgen -W 100 -l 2:5000,14:90000 -k mytestdata
49
- async fn create_test_data ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
26
+ pub async fn create_test_data ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
50
27
let filenameprefix = autctcfg. keysets . clone ( ) . unwrap ( ) ;
51
28
let numprivs = autctcfg. depth . clone ( ) . unwrap ( ) as u64 ;
52
29
let values_indices_str = autctcfg. rpc_host . clone ( ) . unwrap ( ) ;
@@ -62,10 +39,10 @@ async fn create_test_data(autctcfg: AutctConfig) -> Result<(), Box<dyn Error>> {
62
39
/// This is a tool for manual testing; users will always
63
40
/// have privkeys stored in encrypted files. Hence
64
41
/// there is no attempt to handle errors properly.
65
- async fn request_encrypt_key ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
42
+ pub async fn request_encrypt_key ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
66
43
let password = rpassword:: prompt_password ( "Enter a password to encrypt the private key: " ) . unwrap ( ) ;
67
44
let privkey_file_str = autctcfg. privkey_file_str . clone ( ) . unwrap ( ) ;
68
- let plaintext_priv_wif = autct :: utils:: read_file_string ( & privkey_file_str) ?;
45
+ let plaintext_priv_wif = crate :: utils:: read_file_string ( & privkey_file_str) ?;
69
46
let mut buf: Vec < u8 > = Vec :: new ( ) ;
70
47
write ! ( & mut buf, "{}" , plaintext_priv_wif) ?;
71
48
let encrypted_data = encrypt ( & buf, & password. as_bytes ( ) ) ?;
@@ -75,7 +52,7 @@ async fn request_encrypt_key(autctcfg: AutctConfig) -> Result<(), Box<dyn Error>
75
52
}
76
53
77
54
// This tool is helpful to allow importing the key to a Bitcoin wallet
78
- async fn request_decrypted_key ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
55
+ pub async fn request_decrypted_key ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
79
56
let password = rpassword:: prompt_password ( "Enter password to decrypt private key: " ) ?;
80
57
let privkey_file_str = autctcfg. privkey_file_str . clone ( ) . unwrap ( ) ;
81
58
let encrypted_priv_wif = std:: fs:: read ( & privkey_file_str) ?;
@@ -85,7 +62,7 @@ async fn request_decrypted_key(autctcfg: AutctConfig) -> Result<(), Box<dyn Erro
85
62
Ok ( ( ) )
86
63
}
87
64
88
- async fn request_create_keys ( autctcfg : AutctConfig ) ->Result < ( ) , Box < dyn Error > > {
65
+ pub async fn request_create_keys ( autctcfg : AutctConfig ) ->Result < ( ) , Box < dyn Error > > {
89
66
// This requires interaction from user: give a password on the command line:
90
67
let password = rpassword:: prompt_password ( "Enter a password to encrypt the new private key: " ) . unwrap ( ) ;
91
68
let res = rpcclient:: createkeys ( autctcfg, password) . await ;
@@ -109,7 +86,7 @@ async fn request_create_keys(autctcfg: AutctConfig) ->Result<(), Box<dyn Error>>
109
86
Ok ( ( ) )
110
87
}
111
88
112
- async fn request_verify ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
89
+ pub async fn request_verify ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
113
90
let res = rpcclient:: verify ( autctcfg) . await ;
114
91
match res {
115
92
Ok ( rest) => {
@@ -132,27 +109,40 @@ async fn request_verify(autctcfg: AutctConfig) -> Result<(), Box<dyn Error>> {
132
109
Ok ( ( ) )
133
110
}
134
111
135
- async fn request_audit_verify ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
112
+ fn print_and_return ( s : & str ) -> Result < String , Box < dyn Error > > {
113
+ println ! ( "{}" , s) ;
114
+ Ok ( s. to_string ( ) )
115
+ }
116
+
117
+ pub async fn request_audit_verify ( autctcfg : AutctConfig ) -> Result < String , Box < dyn Error > > {
136
118
let res = rpcclient:: auditverify ( autctcfg. clone ( ) ) . await ;
137
119
match res {
138
120
Ok ( rest) => {
139
- let satmin = autctcfg. audit_range_min . unwrap ( ) ;
140
- let satmax = satmin + 2u64 . pow ( autctcfg. audit_range_exponent . unwrap ( ) as u32 ) ;
121
+ // Note that we are guaranteed non-nonsense values
122
+ // for these fields because this is a non-Err response:
123
+ let satmin = rest. audit_range_min ;
124
+ let satmax = satmin + 2u64 . pow ( rest. audit_range_exponent as u32 ) ;
141
125
match rest. accepted {
142
- 1 => println ! ( "Audit is valid! The utxos' total value is between {} and {} satoshis." ,
143
- satmin, satmax) ,
144
- -1 => println ! ( "Invalid encoding of proof, should be base64." ) ,
145
- -2 => println ! ( "Invalid proof serialization" ) ,
146
- -3 => println ! ( "Proof of assets in range is rejected, proof invalid." ) ,
147
- _ => println ! ( "Unrecognized error code from server?" ) ,
126
+ 1 => { let s = format ! ( "Audit is valid! The utxos' total value is between {} and {} satoshis." ,
127
+ satmin, satmax) ;
128
+ println ! ( "{}" , s) ;
129
+ return Ok ( s) } ,
130
+ -1 => { let s = "Invalid encoding of proof, should be base64." ;
131
+ return print_and_return ( s) ; } ,
132
+ -2 => { let s = "Invalid proof serialization" ;
133
+ return print_and_return ( s) ; } ,
134
+ -3 => { let s = "Proof of assets in range is rejected, proof invalid." ;
135
+ return print_and_return ( s) ; } ,
136
+ _ => { let s = "Unrecognized error code from server?" ;
137
+ return print_and_return ( s) ; } ,
148
138
}
149
139
} ,
150
- Err ( _) => return Err ( "Verificatoin request processing failed." . into ( ) ) ,
140
+ Err ( e) => return Err (
141
+ format ! ( "{}" , e) . into ( ) ) ,
151
142
} ;
152
- Ok ( ( ) )
153
-
154
143
}
155
- async fn request_audit ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
144
+
145
+ pub async fn request_audit ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
156
146
let res = rpcclient:: auditprove (
157
147
autctcfg. clone ( ) ) . await ;
158
148
let required_proof_destination = autctcfg. clone ( )
@@ -185,7 +175,7 @@ async fn request_audit(autctcfg: AutctConfig) -> Result<(), Box<dyn Error>>{
185
175
Ok ( ( ) )
186
176
}
187
177
188
- async fn request_prove ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
178
+ pub async fn request_prove ( autctcfg : AutctConfig ) -> Result < ( ) , Box < dyn Error > > {
189
179
// This requires interaction from user: give a password on the command line:
190
180
let password = rpassword:: prompt_password ( "Enter a password to decrypt the private key: " ) . unwrap ( ) ;
191
181
let required_proof_destination = autctcfg. clone ( ) . proof_file_str . unwrap ( ) ;
0 commit comments