public override bool VerifyPassword(string password) { lock (accounts) { NEP6Account account = accounts.Values.FirstOrDefault(p => !p.Decrypted); if (account == null) { account = accounts.Values.FirstOrDefault(p => p.HasKey); } if (account == null) { return(true); } if (account.Decrypted) { return(account.VerifyPassword(password)); } else { try { account.GetKey(password); return(true); } catch (FormatException) { return(false); } } } }
private void AddAccount(NEP6Account account, bool is_import) { lock (accounts) { if (accounts.TryGetValue(account.ScriptHash, out NEP6Account account_old)) { account.Label = account_old.Label; account.IsDefault = account_old.IsDefault; account.Lock = account_old.Lock; if (account.Contract == null) { account.Contract = account_old.Contract; } else { NEP6Contract contract_old = (NEP6Contract)account_old.Contract; if (contract_old != null) { NEP6Contract contract = (NEP6Contract)account.Contract; contract.ParameterNames = contract_old.ParameterNames; contract.Deployed = contract_old.Deployed; } } account.Extra = account_old.Extra; } else { WalletIndexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Default?.Height ?? 0); } accounts[account.ScriptHash] = account; } }
public NEP6Wallet(string path, string name = null) { this.path = path; if (File.Exists(path)) { JObject wallet; using (StreamReader reader = new StreamReader(path)) { wallet = JObject.Parse(reader); } this.name = wallet["name"]?.AsString(); this.version = Version.Parse(wallet["version"].AsString()); this.Scrypt = ScryptParameters.FromJson(wallet["scrypt"]); this.accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash); this.extra = wallet["extra"]; WalletIndexer.RegisterAccounts(accounts.Keys); } else { this.name = name; this.version = Version.Parse("1.0"); this.Scrypt = ScryptParameters.Default; this.accounts = new Dictionary <UInt160, NEP6Account>(); this.extra = JObject.Null; } WalletIndexer.BalanceChanged += WalletIndexer_BalanceChanged; }
public override WalletAccount CreateAccount(UInt160 scriptHash) { NEP6Account account = new NEP6Account(this, scriptHash); AddAccount(account, true); return(account); }
/// <summary> /// 从公钥导入钱包 使用一个假的私钥 /// Add Code /// </summary> /// <param name="publicKey">公钥</param> /// <returns>钱包账号</returns> public WalletAccount ImportFromPublicKey(string publicKey) { //Console.WriteLine($"ScriptHash1: {publicKey}"); KeyPair key = new KeyPair(publicKey); //Console.WriteLine($"ScriptHash2: {publicKey}"); NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; //Console.WriteLine("Import 3"); //Console.WriteLine($"ScriptHash: {contract.ScriptHash}"); NEP6Account account; if (Scrypt.N == 16384 && Scrypt.R == 8 && Scrypt.P == 8) { account = new NEP6Account(this, contract.ScriptHash); } else { account = new NEP6Account(this, contract.ScriptHash, key, null); } account.Contract = contract; AddAccount(account, true); return(account); }
public override WalletAccount CreateAccount(Contract contract, KeyPair key = null) { NEP6Contract nep6contract = contract as NEP6Contract; if (nep6contract == null) { nep6contract = new NEP6Contract { Script = contract.Script, ParameterList = contract.ParameterList, ParameterNames = contract.ParameterList.Select((p, i) => $"parameter{i}").ToArray(), Deployed = false }; } NEP6Account account; if (key == null) { account = new NEP6Account(this, nep6contract.ScriptHash); } else { account = new NEP6Account(this, nep6contract.ScriptHash, key, password); } account.Contract = nep6contract; AddAccount(account, false); return(account); }
/// <summary> /// 使用钱包文件初始化钱包 /// /// Add Code /// </summary> /// <param name="path">钱包文件路径</param> /// <param name="password">密码</param> /// <param name="type">账号类型</param> public NEP6Wallet(string path, string password, int type = 0) { this.type = type; this.path = path; // 钱包文件是否存在 if (!File.Exists(path)) { throw new FileNotFoundException(); } // 打开钱包 JObject wallet; using (StreamReader reader = new StreamReader(path)) { wallet = JObject.Parse(reader); } //Console.WriteLine($"Wallet Name: {wallet["version"].AsString()}"); this.name = wallet["name"]?.AsString(); this.version = Version.Parse(wallet["version"].AsString()); this.Scrypt = ScryptParameters.FromJson(wallet["scrypt"]); // 把文件中的账号信息转换成 NEP6Account 并加入到 accounts 键名是 ScriptHash this.accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.fromJson(p, this)).ToDictionary(p => p.ScriptHash); //foreach (UInt160 key in this.accounts.Keys) //{ // this.accounts[key].GetKey(); //} this.extra = wallet["extra"]; WalletIndexer.RegisterAccounts(accounts.Keys); }
public override WalletAccount Import(string nep2, string passphrase) { TR.Enter(); KeyPair key = new KeyPair(GetPrivateKeyFromNEP2(nep2, passphrase)); NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; NEP6Account account; if (Scrypt.N == 16384 && Scrypt.R == 8 && Scrypt.P == 8) { account = new NEP6Account(this, contract.ScriptHash, nep2); } else { account = new NEP6Account(this, contract.ScriptHash, key, passphrase); } account.Contract = contract; AddAccount(account, true); return(TR.Exit(account)); }
public override WalletAccount CreateAccount(UInt160 scriptHash) { TR.Enter(); NEP6Account account = new NEP6Account(this, scriptHash); AddAccount(account, true); return(TR.Exit(account)); }
public override WalletAccount Import(string wif) { KeyPair key = new KeyPair(GetPrivateKeyFromWIF(wif)); NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; NEP6Account account = new NEP6Account(this, contract.ScriptHash, key, password) { Contract = contract }; AddAccount(account, true); return(account); }
public override WalletAccount CreateAccount(byte[] privateKey) { KeyPair key = new KeyPair(privateKey); NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; NEP6Account account = new NEP6Account(this, contract.ScriptHash, key, password) { Contract = contract }; AddAccount(account, false); return(account); }
/// <summary> /// 验证密码是否有效 /// </summary> /// <param name="password"></param> /// <returns></returns> public override bool VerifyPassword(string password) { lock (accounts) { NEP6Account account = accounts.Values.FirstOrDefault(p => !p.Decrypted); if (account == null) { account = accounts.Values.FirstOrDefault(p => p.HasKey); } if (account == null) { return(true); } //Console.WriteLine($"NEP6 VerifyPassword password: {password}"); if (account.Decrypted) { //Console.WriteLine($"NEP6 VerifyPassword password1: {password}"); return(account.VerifyPassword(password)); } else { try { //Console.WriteLine($"NEP6Wallet account: {account.ToJson().AsString()}"); //Console.WriteLine($"Account Key: {account.Address}"); //Console.WriteLine($"NEP6 VerifyPassword password2: {password}"); //Console.WriteLine($"WIFKey: {account.GetWIFKey()}"); account.GetKey(password); // 获取私钥 //Console.WriteLine($"WIFKey: {account.GetWIFKey()}"); return(true); } catch (FormatException) { //Console.WriteLine($"NEP6 VerifyPassword password2 Exception: {password}"); return(false); } } } }
/// <summary> /// 使用Nep2key 和 密码 导入钱包 /// </summary> /// <param name="nep2">nep2key</param> /// <param name="passphrase">密码</param> /// <returns>钱包账号</returns> public override WalletAccount Import(string nep2, string passphrase) { //KeyPair key = new KeyPair(GetPrivateKeyFromNEP2(nep2, passphrase)); // 增加无密码分支 AddCode //Console.WriteLine("Import 1"); KeyPair key; if (string.IsNullOrEmpty(passphrase)) { key = new KeyPair(GetPrivateKeyFromNEP2(nep2)); } else { key = new KeyPair(GetPrivateKeyFromNEP2(nep2, passphrase)); } //Console.WriteLine("Import 2"); NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; //Console.WriteLine("Import 3"); //Console.WriteLine($"ScriptHash: {contract.ScriptHash}"); NEP6Account account; if (Scrypt.N == 16384 && Scrypt.R == 8 && Scrypt.P == 8) { account = new NEP6Account(this, contract.ScriptHash, nep2); } else { account = new NEP6Account(this, contract.ScriptHash, key, passphrase); } account.Contract = contract; AddAccount(account, true); return(account); }
public override WalletAccount Import(X509Certificate2 cert) { KeyPair key; using (ECDsa ecdsa = cert.GetECDsaPrivateKey()) { key = new KeyPair(ecdsa.ExportParameters(true).D); } NEP6Contract contract = new NEP6Contract { Script = Contract.CreateSignatureRedeemScript(key.PublicKey), ParameterList = new[] { ContractParameterType.Signature }, ParameterNames = new[] { "signature" }, Deployed = false }; NEP6Account account = new NEP6Account(this, contract.ScriptHash, key, password) { Contract = contract }; AddAccount(account, true); return(account); }
public NEP6Wallet(string path, string name = null) { this.type = 0; // 以文件的形式打开钱包 this.path = path; //Console.WriteLine($"NEP6Wallet Path: {this.path}"); if (File.Exists(path)) // 通过文件加载钱包 { //Console.WriteLine($"NEP6Wallet Path: {this.path}"); JObject wallet; using (StreamReader reader = new StreamReader(path)) { wallet = JObject.Parse(reader); } //Console.WriteLine($"Wallet Name: {wallet["version"].AsString()}"); this.name = wallet["name"]?.AsString(); this.version = Version.Parse(wallet["version"].AsString()); this.Scrypt = ScryptParameters.FromJson(wallet["scrypt"]); // 把文件中的账号信息转换成 NEP6Account 并加入到 accounts 键名是 ScriptHash this.accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash); //foreach (UInt160 key in this.accounts.Keys) //{ // this.accounts[key].GetKey(); //} this.extra = wallet["extra"]; WalletIndexer.RegisterAccounts(accounts.Keys); } else { this.name = name; this.version = Version.Parse("1.0"); this.Scrypt = ScryptParameters.Default; this.accounts = new Dictionary <UInt160, NEP6Account>(); this.extra = JObject.Null; } WalletIndexer.BalanceChanged += WalletIndexer_BalanceChanged; }