-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathchain_config.rs
More file actions
400 lines (365 loc) · 16.1 KB
/
chain_config.rs
File metadata and controls
400 lines (365 loc) · 16.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use crate::utils::mnemonic;
use bip39::{Language, Mnemonic};
use clarity_repl::clarity::util::hash::bytes_to_hex;
use clarity_repl::clarity::util::secp256k1::Secp256k1PublicKey;
use clarity_repl::clarity::util::StacksAddress;
use libsecp256k1::{PublicKey, SecretKey};
use std::io::{BufReader, Read};
use std::path::PathBuf;
use std::{collections::BTreeMap, fs::File};
use tiny_hderive::bip32::ExtendedPrivKey;
use toml::value::Value;
pub const DEFAULT_DERIVATION_PATH: &str = "m/44'/5757'/0'/0/0";
pub const DEFAULT_BITCOIN_NODE_IMAGE: &str = "quay.io/hirosystems/bitcoind:devnet";
pub const DEFAULT_STACKS_NODE_IMAGE: &str = "quay.io/hirosystems/stacks-node:devnet";
pub const DEFAULT_BITCOIN_EXPLORER_IMAGE: &str = "quay.io/hirosystems/bitcoin-explorer:devnet";
pub const DEFAULT_STACKS_API_IMAGE: &str = "blockstack/stacks-blockchain-api:latest";
pub const DEFAULT_STACKS_EXPLORER_IMAGE: &str = "blockstack/explorer:1.16.1";
pub const DEFAULT_POSTGRES_IMAGE: &str = "postgres:alpine";
#[derive(Serialize, Deserialize, Debug)]
pub struct ChainConfigFile {
network: NetworkConfigFile,
accounts: Option<Value>,
devnet: Option<DevnetConfigFile>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct NetworkConfigFile {
name: String,
node_rpc_address: Option<String>,
deployment_fee_rate: Option<u64>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct DevnetConfigFile {
pub orchestrator_port: Option<u16>,
pub bitcoin_node_p2p_port: Option<u16>,
pub bitcoin_node_rpc_port: Option<u16>,
pub stacks_node_p2p_port: Option<u16>,
pub stacks_node_rpc_port: Option<u16>,
pub stacks_node_events_observers: Option<Vec<String>>,
pub stacks_api_port: Option<u16>,
pub stacks_api_events_port: Option<u16>,
pub bitcoin_explorer_port: Option<u16>,
pub stacks_explorer_port: Option<u16>,
pub bitcoin_controller_port: Option<u16>,
pub bitcoin_node_username: Option<String>,
pub bitcoin_node_password: Option<String>,
pub miner_mnemonic: Option<String>,
pub miner_derivation_path: Option<String>,
pub bitcoin_controller_block_time: Option<u32>,
pub working_dir: Option<String>,
pub postgres_port: Option<u16>,
pub postgres_username: Option<String>,
pub postgres_password: Option<String>,
pub postgres_database: Option<String>,
pub pox_stacking_orders: Option<Vec<PoxStackingOrder>>,
pub execute_script: Option<Vec<ExecuteScript>>,
pub bitcoin_node_image_url: Option<String>,
pub bitcoin_explorer_image_url: Option<String>,
pub stacks_node_image_url: Option<String>,
pub stacks_api_image_url: Option<String>,
pub stacks_explorer_image_url: Option<String>,
pub postgres_image_url: Option<String>,
pub disable_bitcoin_explorer: Option<bool>,
pub disable_stacks_explorer: Option<bool>,
pub disable_stacks_api: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct PoxStackingOrderFile {
pub start_at_cycle: u32,
pub end_at_cycle: u32,
pub amount_locked: u32,
pub wallet_label: String,
pub bitcoin_address: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExecuteScript {
pub script: String,
pub allow_wallets: bool,
pub allow_write: bool,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AccountConfigFile {
mnemonic: Option<String>,
derivation: Option<String>,
balance: Option<u64>,
is_mainnet: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChainConfig {
pub network: NetworkConfig,
pub accounts: BTreeMap<String, AccountConfig>,
pub devnet: Option<DevnetConfig>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NetworkConfig {
name: String,
pub node_rpc_address: Option<String>,
pub deployment_fee_rate: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DevnetConfig {
pub orchestrator_port: u16,
pub bitcoin_node_p2p_port: u16,
pub bitcoin_node_rpc_port: u16,
pub bitcoin_node_username: String,
pub bitcoin_node_password: String,
pub stacks_node_p2p_port: u16,
pub stacks_node_rpc_port: u16,
pub stacks_node_events_observers: Vec<String>,
pub stacks_api_port: u16,
pub stacks_api_events_port: u16,
pub stacks_explorer_port: u16,
pub bitcoin_explorer_port: u16,
pub bitcoin_controller_port: u16,
pub bitcoin_controller_block_time: u32,
pub miner_stx_address: String,
pub miner_secret_key_hex: String,
pub miner_btc_address: String,
pub miner_mnemonic: String,
pub miner_derivation_path: String,
pub working_dir: String,
pub postgres_port: u16,
pub postgres_username: String,
pub postgres_password: String,
pub postgres_database: String,
pub pox_stacking_orders: Vec<PoxStackingOrder>,
pub execute_script: Vec<ExecuteScript>,
pub bitcoin_node_image_url: String,
pub stacks_node_image_url: String,
pub stacks_api_image_url: String,
pub stacks_explorer_image_url: String,
pub postgres_image_url: String,
pub bitcoin_explorer_image_url: String,
pub disable_bitcoin_explorer: bool,
pub disable_stacks_explorer: bool,
pub disable_stacks_api: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PoxStackingOrder {
pub start_at_cycle: u32,
pub duration: u32,
pub wallet: String,
pub slots: u64,
pub btc_address: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AccountConfig {
pub mnemonic: String,
pub derivation: String,
pub balance: u64,
pub address: String,
pub is_mainnet: bool,
}
impl ChainConfig {
#[allow(non_fmt_panics)]
pub fn from_path(path: &PathBuf) -> ChainConfig {
let path = match File::open(path) {
Ok(path) => path,
Err(_) => {
let error = format!("Unable to open file {:?}", path.to_str());
panic!(error)
}
};
let mut chain_config_file_reader = BufReader::new(path);
let mut chain_config_file_buffer = vec![];
chain_config_file_reader
.read_to_end(&mut chain_config_file_buffer)
.unwrap();
let mut chain_config_file: ChainConfigFile =
toml::from_slice(&chain_config_file_buffer[..]).unwrap();
ChainConfig::from_chain_config_file(&mut chain_config_file)
}
pub fn from_chain_config_file(chain_config_file: &mut ChainConfigFile) -> ChainConfig {
let network = NetworkConfig {
name: chain_config_file.network.name.clone(),
node_rpc_address: chain_config_file.network.node_rpc_address.clone(),
deployment_fee_rate: chain_config_file.network.deployment_fee_rate.unwrap_or(10),
};
let mut accounts = BTreeMap::new();
let is_mainnet = network.name == "mainnet".to_string();
match &chain_config_file.accounts {
Some(Value::Table(entries)) => {
for (account_name, account_settings) in entries.iter() {
match account_settings {
Value::Table(account_settings) => {
let balance = match account_settings.get("balance") {
Some(Value::Integer(balance)) => *balance as u64,
_ => 0,
};
let is_mainnet = match account_settings.get("is_mainnet") {
Some(Value::Boolean(is_mainnet)) => *is_mainnet,
_ => false,
};
let mnemonic = match account_settings.get("mnemonic") {
Some(Value::String(words)) => {
Mnemonic::parse_in_normalized(Language::English, words)
.unwrap()
.to_string()
}
_ => {
let entropy = &[
0x33, 0xE4, 0x6B, 0xB1, 0x3A, 0x74, 0x6E, 0xA4, 0x1C, 0xDD,
0xE4, 0x5C, 0x90, 0x84, 0x6A, 0x79,
]; // todo(ludo): rand
Mnemonic::from_entropy(entropy).unwrap().to_string()
}
};
let derivation = match account_settings.get("derivation") {
Some(Value::String(derivation)) => derivation.to_string(),
_ => DEFAULT_DERIVATION_PATH.to_string(),
}; // todo(ludo): use derivation path
let (address, _, _) =
compute_addresses(&mnemonic, &derivation, is_mainnet);
accounts.insert(
account_name.to_string(),
AccountConfig {
mnemonic: mnemonic.to_string(),
derivation,
balance,
address,
is_mainnet,
},
);
}
_ => {}
}
}
}
_ => {}
};
let devnet = if chain_config_file.network.name == "devnet" {
let mut devnet_config = match chain_config_file.devnet.take() {
Some(conf) => conf,
_ => DevnetConfigFile::default(),
};
let now = clarity_repl::clarity::util::get_epoch_time_secs();
let mut dir = std::env::temp_dir();
dir.push(format!("stacks-devnet-{}/", now));
let default_working_dir = dir.display().to_string();
let miner_mnemonic = devnet_config.miner_mnemonic.take().unwrap_or("fragile loan twenty basic net assault jazz absorb diet talk art shock innocent float punch travel gadget embrace caught blossom hockey surround initial reduce".to_string());
let miner_derivation_path = devnet_config
.miner_derivation_path
.take()
.unwrap_or(DEFAULT_DERIVATION_PATH.to_string());
let (miner_stx_address, miner_btc_address, miner_secret_key_hex) =
compute_addresses(&miner_mnemonic, &miner_derivation_path, is_mainnet);
let mut config = DevnetConfig {
orchestrator_port: devnet_config.orchestrator_port.unwrap_or(20445),
bitcoin_node_p2p_port: devnet_config.bitcoin_node_p2p_port.unwrap_or(18444),
bitcoin_node_rpc_port: devnet_config.bitcoin_node_rpc_port.unwrap_or(18443),
bitcoin_node_username: devnet_config
.bitcoin_node_username
.take()
.unwrap_or("devnet".to_string()),
bitcoin_node_password: devnet_config
.bitcoin_node_password
.take()
.unwrap_or("devnet".to_string()),
bitcoin_controller_port: devnet_config.bitcoin_controller_port.unwrap_or(18442),
bitcoin_controller_block_time: devnet_config
.bitcoin_controller_block_time
.unwrap_or(30_000),
stacks_node_p2p_port: devnet_config.stacks_node_p2p_port.unwrap_or(20444),
stacks_node_rpc_port: devnet_config.stacks_node_rpc_port.unwrap_or(20443),
stacks_node_events_observers: devnet_config
.stacks_node_events_observers
.take()
.unwrap_or(vec![]),
stacks_api_port: devnet_config.stacks_api_port.unwrap_or(3999),
stacks_api_events_port: devnet_config.stacks_api_events_port.unwrap_or(3700),
stacks_explorer_port: devnet_config.stacks_explorer_port.unwrap_or(8000),
bitcoin_explorer_port: devnet_config.bitcoin_explorer_port.unwrap_or(8001),
miner_btc_address,
miner_stx_address,
miner_mnemonic,
miner_secret_key_hex,
miner_derivation_path,
working_dir: devnet_config
.working_dir
.take()
.unwrap_or(default_working_dir),
postgres_port: devnet_config.postgres_port.unwrap_or(5432),
postgres_username: devnet_config
.postgres_username
.take()
.unwrap_or("postgres".to_string()),
postgres_password: devnet_config
.postgres_password
.take()
.unwrap_or("postgres".to_string()),
postgres_database: devnet_config
.postgres_database
.take()
.unwrap_or("postgres".to_string()),
execute_script: devnet_config.execute_script.take().unwrap_or(vec![]),
bitcoin_node_image_url: devnet_config
.bitcoin_node_image_url
.take()
.unwrap_or(DEFAULT_BITCOIN_NODE_IMAGE.to_string()),
stacks_node_image_url: devnet_config
.stacks_node_image_url
.take()
.unwrap_or(DEFAULT_STACKS_NODE_IMAGE.to_string()),
stacks_api_image_url: devnet_config
.stacks_api_image_url
.take()
.unwrap_or(DEFAULT_STACKS_API_IMAGE.to_string()),
postgres_image_url: devnet_config
.postgres_image_url
.take()
.unwrap_or(DEFAULT_POSTGRES_IMAGE.to_string()),
stacks_explorer_image_url: devnet_config
.stacks_explorer_image_url
.take()
.unwrap_or(DEFAULT_STACKS_EXPLORER_IMAGE.to_string()),
bitcoin_explorer_image_url: devnet_config
.bitcoin_explorer_image_url
.take()
.unwrap_or(DEFAULT_BITCOIN_EXPLORER_IMAGE.to_string()),
pox_stacking_orders: devnet_config.pox_stacking_orders.take().unwrap_or(vec![]),
disable_bitcoin_explorer: devnet_config.disable_bitcoin_explorer.unwrap_or(false),
disable_stacks_api: devnet_config.disable_stacks_api.unwrap_or(false),
disable_stacks_explorer: devnet_config.disable_stacks_explorer.unwrap_or(false),
};
if !config.disable_stacks_api && config.disable_stacks_api {
config.disable_stacks_api = false;
}
Some(config)
} else {
None
};
let config = ChainConfig {
network,
accounts,
devnet,
};
config
}
}
pub fn compute_addresses(
mnemonic: &str,
derivation_path: &str,
mainnet: bool,
) -> (String, String, String) {
let bip39_seed = match mnemonic::get_bip39_seed_from_mnemonic(&mnemonic, "") {
Ok(bip39_seed) => bip39_seed,
Err(_) => panic!(),
};
let ext = ExtendedPrivKey::derive(&bip39_seed[..], derivation_path).unwrap();
let secret_key = SecretKey::parse_slice(&ext.secret()).unwrap();
// Enforce a 33 bytes secret key format, expected by Stacks
let mut secret_key_bytes = secret_key.serialize().to_vec();
secret_key_bytes.push(1);
let miner_secret_key_hex = bytes_to_hex(&secret_key_bytes);
let public_key = PublicKey::from_secret_key(&secret_key);
let pub_key = Secp256k1PublicKey::from_slice(&public_key.serialize_compressed()).unwrap();
let version = if mainnet {
clarity_repl::clarity::util::C32_ADDRESS_VERSION_MAINNET_SINGLESIG
} else {
clarity_repl::clarity::util::C32_ADDRESS_VERSION_TESTNET_SINGLESIG
};
let stx_address = StacksAddress::from_public_key(version, pub_key).unwrap();
// TODO(ludo): de-hardcode this
let btc_address = "n3GRiDLKWuKLCw1DZmV75W1mE35qmW2tQm".to_string();
(stx_address.to_string(), btc_address, miner_secret_key_hex)
}