3
3
use parity_multiaddr:: Multiaddr ;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
use std:: fs:: { self , File } ;
6
+ use std:: net:: SocketAddr ;
6
7
use std:: num:: NonZeroU16 ;
7
8
use std:: path:: Path ;
9
+ use std:: str:: FromStr ;
10
+ use structopt:: StructOpt ;
8
11
use thiserror:: Error ;
9
12
10
13
/// Temporary module required to de/ser config files base64'd protobuf rsa private key format.
@@ -13,6 +16,24 @@ mod keys_proto {
13
16
include ! ( concat!( env!( "OUT_DIR" ) , "/keys_proto.rs" ) ) ;
14
17
}
15
18
19
+ #[ derive( Debug , StructOpt ) ]
20
+ pub enum Profile {
21
+ Test ,
22
+ Default ,
23
+ }
24
+
25
+ impl FromStr for Profile {
26
+ type Err = InitializationError ;
27
+
28
+ fn from_str ( profile : & str ) -> Result < Self , Self :: Err > {
29
+ match profile {
30
+ "test" => Ok ( Profile :: Test ) ,
31
+ "default" => Ok ( Profile :: Default ) ,
32
+ _ => Err ( InitializationError :: InvalidProfile ( profile. to_string ( ) ) ) ,
33
+ }
34
+ }
35
+ }
36
+
16
37
/// The way things can go wrong when calling [`initialize`].
17
38
#[ derive( Error , Debug ) ]
18
39
pub enum InitializationError {
@@ -23,7 +44,7 @@ pub enum InitializationError {
23
44
#[ error( "invalid RSA key length given: {0}" ) ]
24
45
InvalidRsaKeyLength ( u16 ) ,
25
46
#[ error( "unsupported profiles selected: {0:?}" ) ]
26
- InvalidProfiles ( Vec < String > ) ,
47
+ InvalidProfile ( String ) ,
27
48
#[ error( "key generation failed: {0}" ) ]
28
49
KeyGeneration ( Box < dyn std:: error:: Error + ' static > ) ,
29
50
#[ error( "key encoding failed: {0}" ) ]
@@ -37,7 +58,7 @@ pub enum InitializationError {
37
58
pub fn initialize (
38
59
ipfs_path : & Path ,
39
60
bits : NonZeroU16 ,
40
- profiles : Vec < String > ,
61
+ profile : Profile ,
41
62
) -> Result < ( ) , InitializationError > {
42
63
let config_path = ipfs_path. join ( "config" ) ;
43
64
@@ -46,14 +67,10 @@ pub fn initialize(
46
67
. and_then ( |_| {
47
68
fs:: File :: create ( & config_path) . map_err ( InitializationError :: ConfigCreationFailed )
48
69
} )
49
- . and_then ( |config_file| create ( config_file, bits, profiles ) )
70
+ . and_then ( |config_file| create ( config_file, bits, profile ) )
50
71
}
51
72
52
- fn create (
53
- config : File ,
54
- bits : NonZeroU16 ,
55
- profiles : Vec < String > ,
56
- ) -> Result < ( ) , InitializationError > {
73
+ fn create ( config : File , bits : NonZeroU16 , profile : Profile ) -> Result < ( ) , InitializationError > {
57
74
use multibase:: Base :: Base64Pad ;
58
75
use prost:: Message ;
59
76
use std:: io:: BufWriter ;
@@ -65,13 +82,18 @@ fn create(
65
82
return Err ( InitializationError :: InvalidRsaKeyLength ( bits) ) ;
66
83
}
67
84
68
- if profiles. len ( ) != 1 || profiles[ 0 ] != "test" {
69
- // profiles are expected to be (comma separated) "test" as there are no bootstrap peer
70
- // handling yet. the conformance test cases seem to init `go-ipfs` in this profile where
71
- // it does not have any bootstrap nodes, and multi node tests later call swarm apis to
72
- // dial the nodes together.
73
- return Err ( InitializationError :: InvalidProfiles ( profiles) ) ;
74
- }
85
+ // if profiles.len() != 1 || profiles[0] != "test" || profiles[0] != "default" {
86
+ // // profiles are expected to be (comma separated) "test" as there are no bootstrap peer
87
+ // // handling yet. the conformance test cases seem to init `go-ipfs` in this profile where
88
+ // // it does not have any bootstrap nodes, and multi node tests later call swarm apis to
89
+ // // dial the nodes together.
90
+ // return Err(InitializationError::InvalidProfile(profiles));
91
+ // }
92
+
93
+ let api_addrs = match profile {
94
+ Profile :: Test => "127.0.0.1:0" ,
95
+ Profile :: Default => "127.0.0.1:4004" ,
96
+ } ;
75
97
76
98
let pk = openssl:: rsa:: Rsa :: generate ( bits as u32 )
77
99
. map_err ( |e| InitializationError :: KeyGeneration ( Box :: new ( e) ) ) ?;
@@ -118,6 +140,7 @@ fn create(
118
140
} ,
119
141
addresses : Addresses {
120
142
swarm : vec ! [ "/ip4/127.0.0.1/tcp/0" . parse( ) . unwrap( ) ] ,
143
+ api : api_addrs. parse ( ) . unwrap ( ) ,
121
144
} ,
122
145
} ;
123
146
@@ -147,7 +170,7 @@ pub enum LoadingError {
147
170
/// Returns only the keypair and listening addresses or [`LoadingError`] but this should be
148
171
/// extended to contain the bootstrap nodes at least later when we need to support those for
149
172
/// testing purposes.
150
- pub fn load ( config : File ) -> Result < ( ipfs:: Keypair , Vec < Multiaddr > ) , LoadingError > {
173
+ pub fn load ( config : File ) -> Result < ( ipfs:: Keypair , Vec < Multiaddr > , SocketAddr ) , LoadingError > {
151
174
use std:: io:: BufReader ;
152
175
153
176
let CompatibleConfigFile {
@@ -167,7 +190,7 @@ pub fn load(config: File) -> Result<(ipfs::Keypair, Vec<Multiaddr>), LoadingErro
167
190
} ) ;
168
191
}
169
192
170
- Ok ( ( kp, addresses. swarm ) )
193
+ Ok ( ( kp, addresses. swarm , addresses . api ) )
171
194
}
172
195
173
196
/// Converts a PEM format to DER where PEM is a container for Base64 data with padding, starting on
@@ -245,6 +268,7 @@ struct CompatibleConfigFile {
245
268
#[ serde( rename_all = "PascalCase" ) ]
246
269
struct Addresses {
247
270
swarm : Vec < Multiaddr > ,
271
+ api : SocketAddr ,
248
272
}
249
273
250
274
#[ derive( Debug , Serialize , Deserialize ) ]
0 commit comments