Browse Source

auto-generate ETH wallet when account init

pull/24/head
Sun 3 years ago
parent
commit
2d9654385b
  1. 25
      src/account.rs
  2. 2
      src/apps/wallet/mod.rs
  3. 15
      src/group.rs

25
src/account.rs

@ -5,11 +5,13 @@ use tdn::types::{ @@ -5,11 +5,13 @@ use tdn::types::{
group::EventId,
primitives::{PeerId, PeerKey, Result},
};
use tdn_did::{generate_peer, Language};
use tdn_did::{generate_eth_account, generate_peer, secp256k1::SecretKey, Language};
use tdn_storage::local::{DStorage, DsValue};
use web3::signing::Key;
use esse_primitives::{id_from_str, id_to_str};
use crate::apps::wallet::models::{Address, ChainToken};
use crate::utils::crypto::{
check_pin, decrypt, decrypt_key, encrypt_key, encrypt_multiple, hash_pin,
};
@ -78,6 +80,7 @@ impl Account { @@ -78,6 +80,7 @@ impl Account {
secret: Vec<u8>,
encrypt: Vec<u8>,
plainkey: Vec<u8>,
wallet: String,
) -> Self {
let start = SystemTime::now();
let datetime = start
@ -89,7 +92,6 @@ impl Account { @@ -89,7 +92,6 @@ impl Account {
id: 0,
pub_height: 1,
own_height: 0,
wallet: String::new(),
event: EventId::default(),
pid,
index,
@ -100,6 +102,7 @@ impl Account { @@ -100,6 +102,7 @@ impl Account {
mnemonic,
secret,
encrypt,
wallet,
plainkey,
avatar,
datetime,
@ -113,21 +116,29 @@ impl Account { @@ -113,21 +116,29 @@ impl Account {
pub fn generate(
index: u32,
salt: &[u8], // &[u8; 32]
lang: i64,
rlang: i64,
mnemonic: &str,
pass: &str,
name: &str,
lock: &str,
avatar: Vec<u8>,
) -> Result<(Account, PeerKey)> {
) -> Result<(Account, PeerKey, Address)> {
let lang = lang_from_i64(rlang);
let sk = generate_peer(
lang_from_i64(lang),
lang,
mnemonic,
index,
0, // account default multiple address index is 0.
if pass.len() > 0 { Some(pass) } else { None },
)?;
// Default ETH wallet account.
let wallet_pass = if pass.len() > 0 { Some(pass) } else { None };
let wallet_sk = generate_eth_account(lang, mnemonic, index, 0, wallet_pass)?;
let wallet_address = format!("{:?}", (&wallet_sk).address());
let wallet = ChainToken::ETH.update_main(&wallet_address, "");
let w = Address::new(ChainToken::ETH, 0, wallet_address, true);
let key = rand::thread_rng().gen::<[u8; 32]>();
let ckey = encrypt_key(salt, lock, &key)?;
let mut ebytes = encrypt_multiple(
@ -144,7 +155,7 @@ impl Account { @@ -144,7 +155,7 @@ impl Account {
Account::new(
sk.peer_id(),
index,
lang,
rlang,
pass.to_string(),
name.to_string(),
hash_pin(lock)?,
@ -153,8 +164,10 @@ impl Account { @@ -153,8 +164,10 @@ impl Account {
secret,
ckey,
key.to_vec(),
wallet,
),
sk,
w,
))
}

2
src/apps/wallet/mod.rs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
mod models;
pub mod models;
mod rpc;
pub(crate) use rpc::new_rpc_handler;

15
src/group.rs

@ -20,7 +20,7 @@ use crate::apps::device::Device; @@ -20,7 +20,7 @@ use crate::apps::device::Device;
//use crate::event::{InnerEvent, StatusEvent, SyncEvent};
//use crate::layer::Layer;
//use crate::rpc;
use crate::storage::{account_db, account_init, consensus_db, write_avatar};
use crate::storage::{account_db, account_init, consensus_db, wallet_db, write_avatar};
use crate::utils::crypto::{decrypt, encrypt};
use crate::utils::device_status::{device_info, device_status as local_device_status};
@ -463,7 +463,7 @@ impl Group { @@ -463,7 +463,7 @@ impl Group {
secret: &[u8],
) -> Result<(i64, PeerId)> {
let account_index = self.accounts.len() as u32;
let (mut account, sk) = Account::generate(
let (mut account, sk, mut wallet) = Account::generate(
account_index,
secret,
lang,
@ -489,11 +489,16 @@ impl Group { @@ -489,11 +489,16 @@ impl Group {
let _ = write_avatar(base, &account_id, &account_id, &account.avatar).await;
self.accounts.insert(account.pid, account);
let db_key = self.db_key(&account_id)?;
let wallet_db = wallet_db(base, &account_id, &db_key)?;
wallet.insert(&wallet_db)?;
wallet_db.close()?;
let (device_name, device_info) = device_info();
let mut device = Device::new(device_name, device_info, Peer::peer(account_id));
let db = consensus_db(base, &account_id, &self.db_key(&account_id)?)?;
device.insert(&db)?;
db.close()?;
let device_db = consensus_db(base, &account_id, &db_key)?;
device.insert(&device_db)?;
device_db.close()?;
Ok((account_did, account_id))
}

Loading…
Cancel
Save