private static Wallet OpenWallet(string path, string password) { if (Path.GetExtension(path) == ".db3") { return(UserWallet.Open(path, password)); } else { NEP6Wallet nep6wallet = new NEP6Wallet(path); nep6wallet.Unlock(password); return(nep6wallet); } }
private bool OnCreateWalletCommand(string[] args) { if (args.Length < 3) { Console.WriteLine("error"); return(true); } string path = args[2]; string password = ReadPassword("password"); if (password.Length == 0) { Console.WriteLine("cancelled"); return(true); } string password2 = ReadPassword("password"); if (password != password2) { Console.WriteLine("error"); return(true); } switch (Path.GetExtension(path)) { case ".db3": { Program.Wallet = UserWallet.Create(path, password); WalletAccount account = Program.Wallet.CreateAccount(); Console.WriteLine($"address: {account.Address}"); Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); } break; case ".json": { NEP6Wallet wallet = new NEP6Wallet(path); wallet.Unlock(password); WalletAccount account = wallet.CreateAccount(); wallet.Save(); Program.Wallet = wallet; Console.WriteLine($"address: {account.Address}"); Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); } break; default: Console.WriteLine("Wallet files in that format are not supported, please use a .json or .db3 file extension."); break; } return(true); }
private static Wallet OpenWalletByData(string data, string password) { Wallet wallet = null; NEP6Wallet nep6wallet = new NEP6Wallet(data); nep6wallet.Unlock(password); wallet = nep6wallet; ZoroChainSystem.Singleton.SetWallet(wallet); return(wallet); }
public void TestMigrate() { string path = GetRandomPath(); UserWallet uw = UserWallet.Create(path, "123", ProtocolSettings.Default); uw.CreateAccount(keyPair.PrivateKey); string npath = CreateWalletFile(); // Scrypt test values NEP6Wallet nw = NEP6Wallet.Migrate(npath, path, "123", ProtocolSettings.Default); bool result = nw.Contains(testScriptHash); Assert.AreEqual(true, result); uw.Delete(); nw.Delete(); }
private Witness PrepareDummyWitness(int pubKeys, int m) { var address = new WalletAccount[pubKeys]; var wallets = new NEP6Wallet[pubKeys]; var walletsUnlocks = new IDisposable[pubKeys]; var snapshot = TestBlockchain.GetTestSnapshot(); for (int x = 0; x < pubKeys; x++) { wallets[x] = TestUtils.GenerateTestWallet(); walletsUnlocks[x] = wallets[x].Unlock("123"); address[x] = wallets[x].CreateAccount(); } // Generate multisignature var multiSignContract = Contract.CreateMultiSigContract(m, address.Select(a => a.GetKey().PublicKey).ToArray()); for (int x = 0; x < pubKeys; x++) { wallets[x].CreateAccount(multiSignContract, address[x].GetKey()); } // Sign var data = new ContractParametersContext(snapshot, new Transaction() { Attributes = Array.Empty <TransactionAttribute>(), Signers = new[] { new Signer() { Account = multiSignContract.ScriptHash, Scopes = WitnessScope.CalledByEntry } }, NetworkFee = 0, Nonce = 0, Script = new byte[0], SystemFee = 0, ValidUntilBlock = 0, Version = 0, Witnesses = new Witness[0] }); for (int x = 0; x < m; x++) { Assert.IsTrue(wallets[x].Sign(data)); } Assert.IsTrue(data.Completed); return(data.GetWitnesses()[0]); }
/// <summary> /// 创建无密码的钱包 /// Add Code /// </summary> /// <param name="args"></param> /// <returns></returns> private bool OnCreateWalletsCommand(string[] args) { if (args.Length < 2) { Console.WriteLine("error"); return(true); } //if (args.Length == 2) //{ // Console.WriteLine("功能正在开发中..."); // return true; //} string path = args[2]; string password = GetDefaultPassword(); switch (Path.GetExtension(path)) { case ".db3": { Program.Wallet = UserWallet.Create(path, password); WalletAccount account = Program.Wallet.CreateAccount(); //Console.WriteLine($"address: {account.Address}"); //Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); PrintAccountInfo(account); } break; case ".json": { NEP6Wallet wallet = new NEP6Wallet(path); wallet.Unlock(password); // 添加锁定密码 WalletAccount account = wallet.CreateAccount(); wallet.Save(); // 保存到 文件中 Program.Wallet = wallet; //Console.WriteLine($"address: {account.Address}"); //Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); //Console.WriteLine($"privkey: {account.GetPrivateKey()}"); PrintAccountInfo(account); } break; default: Console.WriteLine("Wallet files in that format are not supported, please use a .json or .db3 file extension."); break; } return(true); }
//TODO: 目前没有想到其它安全的方法来保存密码 //所以只能暂时手动输入,但如此一来就不能以服务的方式启动了 //未来再想想其它办法,比如采用智能卡之类的 private bool OnOpenWalletCommand(string[] args) { if (args.Length < 3) { Console.WriteLine("error"); return(true); } string path = args[2]; if (!File.Exists(path)) { Console.WriteLine($"File does not exist"); return(true); } string password = ReadPassword("password"); if (password.Length == 0) { Console.WriteLine("cancelled"); return(true); } if (Path.GetExtension(path) == ".db3") { try { Program.Wallet = UserWallet.Open(path, password); } catch (CryptographicException) { Console.WriteLine($"failed to open file \"{path}\""); return(true); } } else { NEP6Wallet nep6wallet = new NEP6Wallet(path); try { nep6wallet.Unlock(password); } catch (CryptographicException) { Console.WriteLine($"failed to open file \"{path}\""); return(true); } Program.Wallet = nep6wallet; } return(true); }
private Wallet OpenWallet(string path, string password) { Wallet wallet = null; if (Path.GetExtension(path) == ".json") { NEP6Wallet nep6wallet = new NEP6Wallet(path, null); nep6wallet.Unlock(password); wallet = nep6wallet; ZoroChainSystem.Singleton.SetWallet(wallet); } return(wallet); }
public void TestConstructorWithJObject() { JObject wallet = new JObject(); wallet["name"] = "test"; wallet["version"] = Version.Parse("1.0").ToString(); wallet["scrypt"] = ScryptParameters.Default.ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); wallet.ToString().Should().Be("{\"name\":\"test\",\"version\":\"1.0\",\"scrypt\":{\"n\":16384,\"r\":8,\"p\":8},\"accounts\":[],\"extra\":{}}"); NEP6Wallet w = new NEP6Wallet(wallet); Assert.AreEqual("test", w.Name); Assert.AreEqual(Version.Parse("1.0").ToString(), w.Version.ToString()); }
/// <summary> /// 公钥打开钱包 /// Add Code /// </summary> /// <param name="args"></param> /// <returns></returns> private bool OnOpenWalletpCommand(string[] args) { if (args.Length < 3) { Console.WriteLine("error"); return(true); } string publickey = args[2]; Console.WriteLine($"walletp: {publickey}"); NEP6Wallet nep6wallet = new NEP6Wallet(publickey, "", ""); Program.Wallet = nep6wallet; return(true); }
private static Wallet OpenWallet(string path, string password) { if (Path.GetExtension(path) == ".db3") { string path_new = Path.ChangeExtension(path, ".json"); NEP6Wallet nep6wallet = NEP6Wallet.Migrate(path_new, path, password); nep6wallet.Save(); return(nep6wallet); } else //.json { NEP6Wallet nep6wallet = new NEP6Wallet(path); nep6wallet.Unlock(password); return(nep6wallet); } }
public void TestMigrate() { string path = GetRandomPath(); UserWallet uw = UserWallet.Create(path, "123"); uw.CreateAccount(keyPair.PrivateKey); string npath = Path.Combine(path, "w.json"); NEP6Wallet nw = NEP6Wallet.Migrate(npath, path, "123"); bool result = nw.Contains(testScriptHash); Assert.AreEqual(true, result); if (File.Exists(path)) { File.Delete(path); } }
private static Transaction SignWithWallet(Transaction tx) { if (tx == null) { throw new ArgumentNullException("tx"); } try { tx.ToJson(); } catch (Exception) { throw new FormatException("交易格式错误"); } var wallet = new NEP6Wallet(new WalletIndexer("D:\\PrivateNet2\\node1\\Index_0001E240"), "1.json"); try { wallet.Unlock("11111111"); } catch (Exception) { Console.WriteLine("password error"); } //Signature var context = new ContractParametersContext(tx); wallet.Sign(context); if (context.Completed) { Console.WriteLine("签名成功"); tx.Witnesses = context.GetWitnesses(); } else { Console.WriteLine("签名失败"); } //Console.WriteLine(tx.ToArray().ToHexString()); Console.WriteLine("交易验证:" + tx.Verify(Blockchain.Singleton.GetSnapshot(), new List <Transaction> { tx })); Console.WriteLine(tx.ToArray().ToHexString()); return(tx); }
private void 创建钱包数据库NToolStripMenuItem_Click(object sender, EventArgs e) { using (CreateWalletDialog dialog = new CreateWalletDialog()) { if (dialog.ShowDialog() != DialogResult.OK) { return; } NEP6Wallet wallet = new NEP6Wallet(GetIndexer(), dialog.WalletPath); wallet.Unlock(dialog.Password); wallet.CreateAccount(); wallet.Save(); ChangeWallet(wallet); Settings.Default.LastWalletPath = dialog.WalletPath; Settings.Default.Save(); } }
public void TestSetup() { JObject wallet = new JObject(); wallet["name"] = "name"; wallet["version"] = new System.Version().ToString(); wallet["scrypt"] = ScryptParameters.Default.ToJson(); // test minimally scryptparameters parsing here ScryptParameters.FromJson(wallet["scrypt"]).Should().NotBeNull(); ScryptParameters.FromJson(wallet["scrypt"]).N.Should().Be(ScryptParameters.Default.N); wallet["accounts"] = new JArray(); //accounts = ((JArray)wallet["accounts"]).Select(p => NEP6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash); wallet["extra"] = new JObject(); // check string json wallet.ToString().Should().Be("{\"name\":\"name\",\"version\":\"0.0\",\"scrypt\":{\"n\":16384,\"r\":8,\"p\":8},\"accounts\":[],\"extra\":{}}"); uut = new NEP6Wallet(wallet); }
public void TestChangePassword() { JObject wallet = new(); wallet["name"] = "name"; wallet["version"] = new Version("1.0").ToString(); wallet["scrypt"] = new ScryptParameters(2, 1, 1).ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); File.WriteAllText(wPath, wallet.ToString()); uut = new NEP6Wallet(wPath, "123", ProtocolSettings.Default); uut.CreateAccount(keyPair.PrivateKey); uut.ChangePassword("456", "123").Should().BeFalse(); uut.ChangePassword("123", "456").Should().BeTrue(); uut.VerifyPassword("456").Should().BeTrue(); uut.ChangePassword("456", "123").Should().BeTrue(); }
private Witness PrepareDummyWitness(int maxAccounts) { var address = new WalletAccount[maxAccounts]; var wallets = new NEP6Wallet[maxAccounts]; var walletsUnlocks = new IDisposable[maxAccounts]; for (int x = 0; x < maxAccounts; x++) { wallets[x] = TestUtils.GenerateTestWallet(); walletsUnlocks[x] = wallets[x].Unlock("123"); address[x] = wallets[x].CreateAccount(); } // Generate multisignature var multiSignContract = Contract.CreateMultiSigContract(maxAccounts, address.Select(a => a.GetKey().PublicKey).ToArray()); for (int x = 0; x < maxAccounts; x++) { wallets[x].CreateAccount(multiSignContract, address[x].GetKey()); } // Sign var data = new ContractParametersContext(new Transaction() { Cosigners = new Cosigner[0], Sender = multiSignContract.ScriptHash, Attributes = new TransactionAttribute[0], NetworkFee = 0, Nonce = 0, Script = new byte[0], SystemFee = 0, ValidUntilBlock = 0, Version = 0, Witnesses = new Witness[0] }); for (int x = 0; x < maxAccounts; x++) { Assert.IsTrue(wallets[x].Sign(data)); } Assert.IsTrue(data.Completed); return(data.GetWitnesses()[0]); }
public void TestAESEncryptAndDecrypt() { NEP6Wallet wallet = new NEP6Wallet("", "1", ProtocolSettings.Default); wallet.CreateAccount(); WalletAccount account = wallet.GetAccounts().ToArray()[0]; KeyPair key = account.GetKey(); Random random = new Random(); byte[] nonce = new byte[12]; random.NextBytes(nonce); var cypher = Neo.Cryptography.Helper.AES256Encrypt(Encoding.UTF8.GetBytes("hello world"), key.PrivateKey, nonce); var m = Neo.Cryptography.Helper.AES256Decrypt(cypher, key.PrivateKey); var message2 = Encoding.UTF8.GetString(m); Assert.AreEqual("hello world", message2); }
private static Wallet OpenWallet(string path, string password) { if (!File.Exists(path)) { throw new FileNotFoundException(); } if (Path.GetExtension(path) == ".db3") { return(UserWallet.Open(path, password)); } else { NEP6Wallet nep6wallet = new NEP6Wallet(path); nep6wallet.Unlock(password); return(nep6wallet); } }
public void OpenWallet(string path, string password) { if (!File.Exists(path)) { throw new FileNotFoundException(); } if (Path.GetExtension(path) == ".db3") { CurrentWallet = UserWallet.Open(path, password); } else { NEP6Wallet nep6wallet = new NEP6Wallet(path); nep6wallet.Unlock(password); CurrentWallet = nep6wallet; } }
public void OpenWallet(string walletPath, string password) { Wallet wallet; IDisposable walletLocker; if (Path.GetExtension(walletPath) == ".db3") { DeprecatedWallet userWallet; try { userWallet = DeprecatedWallet.Open(walletPath, password); walletLocker = null; } catch (CryptographicException) { this.notificationService.ShowErrorNotification(Strings.PasswordIncorrect); return; } wallet = userWallet; } else { var nep6Wallet = new NEP6Wallet(walletPath); try { walletLocker = nep6Wallet.Unlock(password); } catch (CryptographicException) { this.notificationService.ShowErrorNotification(Strings.PasswordIncorrect); return; } wallet = nep6Wallet; } if (wallet == null) { // TODO Localise text this.notificationService.ShowErrorNotification("Could not open wallet! An error occurred while opening"); return; } this.SetCurrentWallet(wallet, walletLocker); }
public void TestSave() { JObject wallet = new(); wallet["name"] = "name"; wallet["version"] = new Version("1.0").ToString(); wallet["scrypt"] = new ScryptParameters(2, 1, 1).ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); File.WriteAllText(wPath, wallet.ToString()); uut = new NEP6Wallet(wPath, "123", ProtocolSettings.Default); uut.CreateAccount(keyPair.PrivateKey); bool result = uut.Contains(testScriptHash); Assert.AreEqual(true, result); uut.Save(); result = uut.Contains(testScriptHash); Assert.AreEqual(true, result); }
public void TestChangePassword() { JObject wallet = new JObject(); wallet["name"] = "name"; wallet["version"] = new System.Version("3.0").ToString(); wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); File.WriteAllText(wPath, wallet.ToString()); uut = new NEP6Wallet(wPath); uut.Unlock("123"); uut.CreateAccount(keyPair.PrivateKey); uut.ChangePassword("456", "123").Should().BeFalse(); uut.ChangePassword("123", "456").Should().BeTrue(); uut.VerifyPassword("456").Should().BeTrue(); uut.ChangePassword("456", "123").Should().BeTrue(); uut.Lock(); }
public void TestIsDefault() { JObject wallet = new(); wallet["name"] = "name"; wallet["version"] = new Version("1.0").ToString(); wallet["scrypt"] = new ScryptParameters(2, 1, 1).ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); var w = new NEP6Wallet(null, "", ProtocolSettings.Default, wallet); var ac = w.CreateAccount(); Assert.AreEqual(ac.Address, w.GetDefaultAccount().Address); var ac2 = w.CreateAccount(); Assert.AreEqual(ac.Address, w.GetDefaultAccount().Address); ac2.IsDefault = true; Assert.AreEqual(ac2.Address, w.GetDefaultAccount().Address); }
public void TestSave() { JObject wallet = new JObject(); wallet["name"] = "name"; wallet["version"] = new System.Version().ToString(); wallet["scrypt"] = new ScryptParameters(0, 0, 0).ToJson(); wallet["accounts"] = new JArray(); wallet["extra"] = new JObject(); File.WriteAllText(wPath, wallet.ToString()); uut = new NEP6Wallet(wPath); uut.Unlock("123"); uut.CreateAccount(keyPair.PrivateKey); bool result = uut.Contains(testScriptHash); Assert.AreEqual(true, result); uut.Save(); result = uut.Contains(testScriptHash); Assert.AreEqual(true, result); }
protected bool OpenWalletCommand(string path, string password) { if (!File.Exists(path)) { Console.WriteLine($"File does not exist"); return(false); } if (string.IsNullOrEmpty(password)) { Console.WriteLine("cancelled"); return(false); } if (Path.GetExtension(path) == ".db3") { try { Program.Wallet = UserWallet.Open(path, password); } catch (CryptographicException) { Console.WriteLine($"failed to open file \"{path}\""); return(false); } } else { NEP6Wallet nep6wallet = new NEP6Wallet(path); try { nep6wallet.Unlock(password); } catch (CryptographicException ex) { Console.WriteLine($"failed to open file \"{path}\". Exception:{ex.Message}"); return(false); } Program.Wallet = nep6wallet; } return(true); }
public void TestCreateAccount() { var uut = new NEP6Wallet(wPath, "123", ProtocolSettings.Default); var acc = uut.CreateAccount("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632549".HexToBytes()); var tx = new Transaction() { Attributes = Array.Empty <TransactionAttribute>(), Script = new byte[1], Signers = new Signer[] { new Signer() { Account = acc.ScriptHash } }, }; var ctx = new ContractParametersContext(TestBlockchain.GetTestSnapshot(), tx, ProtocolSettings.Default.Network); var sig = uut.Sign(ctx); tx.Witnesses = ctx.GetWitnesses(); Assert.IsTrue(tx.VerifyWitnesses(ProtocolSettings.Default, TestBlockchain.GetTestSnapshot(), long.MaxValue)); Assert.ThrowsException <ArgumentNullException>(() => uut.CreateAccount((byte[])null)); Assert.ThrowsException <ArgumentException>(() => uut.CreateAccount("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551".HexToBytes())); }
private Witness PrepareDummyWitness(int maxAccounts) { var store = TestBlockchain.GetStore(); var wallet = TestUtils.GenerateTestWallet(); var snapshot = store.GetSnapshot(); // Prepare var address = new WalletAccount[maxAccounts]; var wallets = new NEP6Wallet[maxAccounts]; var walletsUnlocks = new IDisposable[maxAccounts]; for (int x = 0; x < maxAccounts; x++) { wallets[x] = TestUtils.GenerateTestWallet(); walletsUnlocks[x] = wallets[x].Unlock("123"); address[x] = wallets[x].CreateAccount(); } // Generate multisignature var multiSignContract = Contract.CreateMultiSigContract(maxAccounts, address.Select(a => a.GetKey().PublicKey).ToArray()); for (int x = 0; x < maxAccounts; x++) { wallets[x].CreateAccount(multiSignContract, address[x].GetKey()); } // Sign var data = new ContractParametersContext(new DummyVerificable(multiSignContract.ScriptHash)); for (int x = 0; x < maxAccounts; x++) { Assert.IsTrue(wallets[x].Sign(data)); } Assert.IsTrue(data.Completed); return(data.GetWitnesses()[0]); }
private bool OnCreateWalletCommand(string[] args) { if (args.Length < 3) { Console.WriteLine("error"); return(true); } string path = args[2]; if (Path.GetExtension(path) == ".db3") { Console.WriteLine("The wallet file in the db3 format is not supported."); return(true); } string password = ReadPassword("password"); if (password.Length == 0) { Console.WriteLine("cancelled"); return(true); } string password2 = ReadPassword("password"); if (password != password2) { Console.WriteLine("error"); return(true); } NEP6Wallet wallet = new NEP6Wallet(path); wallet.Unlock(password); WalletAccount account = wallet.CreateAccount(); wallet.Save(); Program.Wallet = wallet; Console.WriteLine($"address: {account.Address}"); Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}"); return(true); }
public void CreateMintTx() { if (!InitWallet()) { return; } UInt160 to = Wallet.GetAccounts().First().ScriptHash; // Import all CN keys var wallet = new NEP6Wallet(""); using var unlock = wallet.Unlock(WALLET_PASS); var cnWallets = Settings.Default.Wifs.Select(wif => wallet.Import(wif)).ToArray(); // Get CN contract using var snapshot = Blockchain.Singleton.GetSnapshot(); var validators = NativeContract.NEO.GetValidators(snapshot); var CNContract = Contract.CreateMultiSigContract(validators.Length - (validators.Length - 1) / 3, validators); wallet.CreateAccount(CNContract); Console.WriteLine($"Generated mint tx: from={CNContract.ScriptHash.ToAddress()} to={to.ToAddress()}"); // Create TX var mintTx = wallet.MakeTransaction ( new TransferOutput[] { new TransferOutput() { AssetId = NEO.AssetId, ScriptHash = to, Value = new BigDecimal(50_000_000, 0), },