Import transparent private key
This commit is contained in:
parent
e28ca7fd68
commit
880fa1b4cb
|
@ -68,6 +68,7 @@ rand_chacha = "0.3.1"
|
|||
blake2b_simd = "1.0.0"
|
||||
chacha20poly1305 = "0.9.0"
|
||||
base64 = "^0.13"
|
||||
base58check = "0.1.0"
|
||||
raptorq = "1.7.0"
|
||||
|
||||
ledger-apdu = { version = "0.9.0", optional = true }
|
||||
|
|
|
@ -43,6 +43,8 @@ void new_sub_account(char *name, int32_t index, uint32_t count);
|
|||
|
||||
void import_transparent_key(uint8_t coin, uint32_t id_account, char *path);
|
||||
|
||||
void import_transparent_secret_key(uint8_t coin, uint32_t id_account, char *secret_key);
|
||||
|
||||
void cancel_warp(void);
|
||||
|
||||
uint8_t warp(uint8_t coin, bool get_tx, uint32_t anchor_offset, int64_t port);
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use crate::coinconfig::CoinConfig;
|
||||
use crate::key2::decode_key;
|
||||
use crate::taddr::derive_tkeys;
|
||||
use crate::taddr::{derive_taddr, derive_tkeys};
|
||||
use anyhow::anyhow;
|
||||
use bip39::{Language, Mnemonic};
|
||||
use rand::rngs::OsRng;
|
||||
|
@ -64,6 +64,14 @@ pub fn import_transparent_key(coin: u8, id_account: u32, path: &str) -> anyhow::
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn import_transparent_secret_key(coin: u8, id_account: u32, sk: &str) -> anyhow::Result<()> {
|
||||
let c = CoinConfig::get(coin);
|
||||
let db = c.db()?;
|
||||
let (sk, addr) = derive_taddr(c.chain.network(), sk)?;
|
||||
db.store_transparent_key(id_account, &sk, &addr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn new_diversified_address() -> anyhow::Result<String> {
|
||||
let c = CoinConfig::get_active();
|
||||
let db = c.db()?;
|
||||
|
|
|
@ -176,6 +176,17 @@ pub unsafe extern "C" fn import_transparent_key(coin: u8, id_account: u32, path:
|
|||
log_result(res)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn import_transparent_secret_key(
|
||||
coin: u8,
|
||||
id_account: u32,
|
||||
secret_key: *mut c_char,
|
||||
) {
|
||||
from_c_str!(secret_key);
|
||||
let res = crate::api::account::import_transparent_secret_key(coin, id_account, &secret_key);
|
||||
log_result(res)
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref SYNC_LOCK: Semaphore = Semaphore::new(1);
|
||||
static ref SYNC_CANCELED: AtomicBool = AtomicBool::new(false);
|
||||
|
|
19
src/taddr.rs
19
src/taddr.rs
|
@ -1,6 +1,7 @@
|
|||
use crate::coinconfig::CoinConfig;
|
||||
use crate::{AddressList, CompactTxStreamerClient, GetAddressUtxosArg, GetAddressUtxosReply};
|
||||
use anyhow::anyhow;
|
||||
use base58check::FromBase58Check;
|
||||
use bip39::{Language, Mnemonic, Seed};
|
||||
use ripemd::{Digest, Ripemd160};
|
||||
use secp256k1::{All, PublicKey, Secp256k1, SecretKey};
|
||||
|
@ -83,11 +84,23 @@ pub fn derive_tkeys(
|
|||
) -> anyhow::Result<(String, String)> {
|
||||
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)?;
|
||||
let seed = Seed::new(&mnemonic, "");
|
||||
let secp = Secp256k1::<All>::new();
|
||||
let ext = ExtendedPrivKey::derive(seed.as_bytes(), path)
|
||||
.map_err(|_| anyhow!("Invalid derivation path"))?;
|
||||
let secret_key = SecretKey::from_slice(&ext.secret())?;
|
||||
let pub_key = PublicKey::from_secret_key(&secp, &secret_key);
|
||||
derive_from_secretkey(network, &secret_key)
|
||||
}
|
||||
|
||||
pub fn derive_taddr(network: &Network, key: &str) -> anyhow::Result<(String, String)> {
|
||||
let (_, sk) = key.from_base58check().map_err(|_| anyhow!("Invalid key"))?;
|
||||
let sk = &sk[0..sk.len() - 1]; // remove compressed pub key marker
|
||||
log::info!("sk {}", hex::encode(&sk));
|
||||
let secret_key = SecretKey::from_slice(&sk)?;
|
||||
derive_from_secretkey(network, &secret_key)
|
||||
}
|
||||
|
||||
fn derive_from_secretkey(network: &Network, sk: &SecretKey) -> anyhow::Result<(String, String)> {
|
||||
let secp = Secp256k1::<All>::new();
|
||||
let pub_key = PublicKey::from_secret_key(&secp, &sk);
|
||||
let pub_key = pub_key.serialize();
|
||||
let pub_key = Ripemd160::digest(&Sha256::digest(&pub_key));
|
||||
let address = TransparentAddress::PublicKey(pub_key.into());
|
||||
|
@ -96,7 +109,7 @@ pub fn derive_tkeys(
|
|||
&network.b58_script_address_prefix(),
|
||||
&address,
|
||||
);
|
||||
let sk = secret_key.display_secret().to_string();
|
||||
let sk = sk.display_secret().to_string();
|
||||
Ok((sk, address))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue