public Task <IWalletManagerResult> CreateWalletAsync(string password) { return(Task.Run <IWalletManagerResult>(() => { KeyInformation key = CryptoUtility.GenerateKey(password, this.api.Hrp); Wallet wallet = new Wallet { IsOpen = true, PrivateKey = key.PrivateKey, Hrp = this.api.Hrp, Address = key.Address, MnemonicWords = key.Mnemonic }; wallet.EcKey = GetEcKey(wallet.PrivateKey); wallet.AddressBytes = wallet.EcKey.PubKey.Hash.ToBytes(); wallet.PubKeyForSign = GetPubKeyForSign(wallet.EcKey.PubKey.ToBytes()); return new WalletManagerResult() { IsSuccessful = true, Wallet = wallet }; })); }
public Task <IWalletManagerResult> OpenWalletAsync(string mnemonicWords, string password) { return(Task.Run <IWalletManagerResult>(() => { KeyInformation key = CryptoUtility.GetKeyFromMnemonic(mnemonicWords, password, this.api.Hrp); Wallet wallet = new Wallet { IsOpen = true, PrivateKey = key.PrivateKey, Hrp = this.api.Hrp, EcKey = GetEcKey(key.PrivateKey) }; wallet.Address = key.Address; wallet.PubKeyForSign = GetPubKeyForSign(wallet.EcKey.PubKey.ToBytes()); wallet.MnemonicWords = mnemonicWords; // var api = BinanceApiFactory.CreateApiClient(this.Environment); //Account account = await this.api.GetAccountAsync(wallet.Address); //if (account != null) //{ // wallet.AccountNumber = account.AccountNumber; // wallet.Sequence = account.Sequence; //} //else //{ // throw new NullReferenceException("Cannot get account information for address " + wallet.Address); //} //Node nodeInfo = await this.api.GetNodeInfoAsync(); //if (nodeInfo != null) //{ // wallet.ChainId = nodeInfo.NodeInfo.Network; //} //else //{ // throw new NullReferenceException("Cannot get chain ID"); //} return new WalletManagerResult { Wallet = wallet, IsSuccessful = true }; })); }
public static KeyInformation GenerateKey(string password, string hrp) { Mnemonic mnemonic = new Mnemonic(Wordlist.English, WordCount.TwentyFour); ExtKey extKey = mnemonic.DeriveExtKey(password); Key privateKey = extKey.PrivateKey; PubKey publicKey = privateKey.PubKey; string address = GetAddress(publicKey.Hash.ToBytes(), hrp); KeyInformation keyInformation = new KeyInformation { Address = address, Mnemonic = mnemonic.ToString(), PrivateKey = privateKey.ToBytes().ToHexString(), PublicKey = publicKey.ToString() }; return(keyInformation); }
public static KeyInformation GetKeyFromMnemonic(string mnenonicWords, string password, string hrp) { Mnemonic mnemonic = new Mnemonic(mnenonicWords, Wordlist.English); ExtKey key = new ExtKey(mnemonic.DeriveSeed(password)); ExtKey extKey = key.Derive(new KeyPath("m/44'/714'/0'/0/0")); Key privateKey = extKey.PrivateKey; PubKey publicKey = privateKey.PubKey; string address = GetAddress(publicKey.Hash.ToBytes(), hrp); KeyInformation keyInformation = new KeyInformation { Address = address, Mnemonic = mnemonic.ToString(), PrivateKey = privateKey.ToBytes().ToHexString(), PublicKey = publicKey.ToString() }; return(keyInformation); }