-
Notifications
You must be signed in to change notification settings - Fork 0
/
BalloonWallet
50 lines (41 loc) · 1.61 KB
/
BalloonWallet
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
use std::fs::{File, OpenOptions};
use std::io::{Write, BufReader, BufRead};
use rand::rngs::OsRng;
use rand::RngCore;
use sha2::{Sha256, Digest};
use bip39::{Mnemonic, Language, Seed};
use bip39::Entropy;
const WALLET_FILE: &str = "wallet.txt";
fn main() {
// Check if a wallet already exists
if let Ok(file) = File::open(WALLET_FILE) {
let reader = BufReader::new(file);
let lines: Vec<String> = reader.lines().map(|l| l.unwrap()).collect();
if lines.len() == 2 {
println!("Wallet already exists with address: {}", &lines[0]);
return;
}
}
// Generate a new wallet
let mut entropy = [0; 32];
OsRng.fill_bytes(&mut entropy);
let mnemonic = Mnemonic::from_entropy(&Entropy::Raw(&entropy), Language::English)
.expect("Could not generate mnemonic phrase");
let seed = Seed::new(&mnemonic, "");
let mut address = [0; 32];
let mut hasher = Sha256::new();
hasher.update(&seed.as_bytes()[..32]);
let private_key = hasher.finalize();
hasher.update(&private_key);
hasher.update(&address);
address.copy_from_slice(&hasher.finalize());
let mut writer = OpenOptions::new().write(true).create(true).open(WALLET_FILE).unwrap();
writer.write_all(&mnemonic.to_string().as_bytes()).unwrap();
writer.write_all(b"\n").unwrap();
writer.write_all(&address).unwrap();
writer.write_all(b"\n").unwrap();
writer.write_all(&private_key).unwrap();
writer.write_all(b"\n").unwrap();
println!("New wallet generated with address: {}", hex::encode(address));
println!("Mnemonic phrase: {}", mnemonic.to_string());
}