public void TestEcdhEncryptAndDecrypt() { NEP6Wallet wallet = new NEP6Wallet("", ProtocolSettings.Default); wallet.Unlock("1"); wallet.CreateAccount(); wallet.CreateAccount(); WalletAccount account1 = wallet.GetAccounts().ToArray()[0]; KeyPair key1 = account1.GetKey(); WalletAccount account2 = wallet.GetAccounts().ToArray()[1]; KeyPair key2 = account2.GetKey(); Console.WriteLine($"Account:{1},privatekey:{key1.PrivateKey.ToHexString()},publicKey:{key1.PublicKey.ToArray().ToHexString()}"); Console.WriteLine($"Account:{2},privatekey:{key2.PrivateKey.ToHexString()},publicKey:{key2.PublicKey.ToArray().ToHexString()}"); var secret1 = Neo.Cryptography.Helper.ECDHDeriveKey(key1, key2.PublicKey); var secret2 = Neo.Cryptography.Helper.ECDHDeriveKey(key2, key1.PublicKey); Assert.AreEqual(secret1.ToHexString(), secret2.ToHexString()); var message = Encoding.ASCII.GetBytes("hello world"); Random random = new Random(); byte[] nonce = new byte[12]; random.NextBytes(nonce); var cypher = message.AES256Encrypt(secret1, nonce); cypher.AES256Decrypt(secret2); Assert.AreEqual("hello world", Encoding.ASCII.GetString(cypher.AES256Decrypt(secret2))); }
public void TestGetAccounts() { Dictionary <string, KeyPair> keys = new Dictionary <string, KeyPair>(); uut.Unlock("123"); byte[] privateKey = new byte[32]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(privateKey); } KeyPair key = new KeyPair(privateKey); Neo.SmartContract.Contract contract = Neo.SmartContract.Contract.CreateSignatureContract(key.PublicKey); keys.Add(contract.Address, key); keys.Add(Neo.SmartContract.Contract.CreateSignatureContract(keyPair.PublicKey).Address, keyPair); uut.CreateAccount(key.PrivateKey); uut.CreateAccount(keyPair.PrivateKey); foreach (var account in uut.GetAccounts()) { if (!keys.TryGetValue(account.Address, out KeyPair k)) { Assert.Fail(); } } }
public void Init(string walletpath, string password, string wif) { Walletpath = walletpath; switch (Path.GetExtension(walletpath)) { case ".db3": { Wallet = UserWallet.Create(walletpath, password); WalletAccount account = Wallet.CreateAccount(Wallet.GetPrivateKeyFromWIF(wif)); } break; case ".json": { NEP6Wallet wallet = new NEP6Wallet(walletpath); wallet.Unlock(password); WalletAccount account = wallet.CreateAccount(Wallet.GetPrivateKeyFromWIF(wif)); wallet.Save(); Wallet = wallet; } break; default: Console.WriteLine("未知的钱包文件类型"); break; } foreach (WalletAccount account in wallet.GetAccounts()) { Address = account.Address; } Password = password; }
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); }
public void TestGetAccounts() { Dictionary <UInt160, KeyPair> keys = new(); byte[] privateKey = new byte[32]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(privateKey); } KeyPair key = new(privateKey); keys.Add(Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash(), key); keys.Add(Contract.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash(), keyPair); uut.CreateAccount(key.PrivateKey); uut.CreateAccount(keyPair.PrivateKey); foreach (var account in uut.GetAccounts()) { if (!keys.ContainsKey(account.ScriptHash)) { Assert.Fail(); } } }
public static string ProcessMessage(JObject data) { var message = data["text"].Value <string>(); switch (message) { case "test": return("testing!"); case "exit": return("exit"); case "openWallet": var walletJson = data["wallet"].Value <string>(); walletJson = walletJson.Split(',')[1]; walletJson = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(walletJson)); string fp = Environment.CurrentDirectory + @"\wallet.json"; File.WriteAllText(fp, walletJson); var PWS = data["PSW"].Value <string>(); NEP6Wallet nep6wallet = new NEP6Wallet(fp); try { nep6wallet.Unlock(PWS); } catch (CryptographicException e) { //MessageBox.Show(Strings.PasswordIncorrect); return(e.Message); } wallet = nep6wallet; return(wallet.GetAccounts().ToArray()[0].Address); case "namehash": try { var nns = data["data"].Value <string>(); System.IO.File.WriteAllLines(@"log.txt", new string[] { "echo:", nns }); var Key = NameHash(nns); return(Key.ToHexString()); } catch (Exception e) { System.IO.File.WriteAllLines(@"err.txt", new string[] { "error:", e.Message }); return("请输入符合规则的域名"); } default: try { message = httpHelper.Get("https://api.otcgo.cn/testnet/address/" + message, new Dictionary <string, string>()); JObject j = JObject.Parse(message); message = JsonConvert.SerializeObject(j["balances"]); } catch { } return(message); //return "echo: " + message; } }