예제 #1
0
        public JObject OpenWallet(string path, string password)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            switch (GetExtension(path))
            {
            case ".db3":
            {
                wallet = UserWallet.Open(path, password);
                break;
            }

            case ".json":
            {
                NEP6Wallet nep6wallet = new NEP6Wallet(path);
                nep6wallet.Unlock(password);
                wallet = nep6wallet;
                break;
            }

            default:
                throw new NotSupportedException();
            }
            return(true);
        }
예제 #2
0
        private void OnStart()
        {
            if (started)
            {
                return;
            }
            string password = GetService <ConsoleServiceBase>().ReadUserInput("password", true);

            if (password.Length == 0)
            {
                Console.WriteLine("Cancelled");
                return;
            }

            wallet = new NEP6Wallet(Settings.Default.Wallet);
            try
            {
                wallet.Unlock(password);
            }
            catch (System.Security.Cryptography.CryptographicException)
            {
                Console.WriteLine($"Failed to open wallet");
                return;
            }

            using (var snapshot = Blockchain.Singleton.GetSnapshot())
            {
                if (!CheckOracleAvaiblable(snapshot, out ECPoint[] oracles))
예제 #3
0
        private static void openWallet(JObject data)
        {
            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);

            string PWS = string.Empty;

            using (allowUseWallet dialog = new allowUseWallet((string)data["addrIn"], (string)data["addrOut"], (string)data["assetID"], (string)data["amounts"]))
            {
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                PWS = dialog.PSW;

                wallet = new NEP6Wallet(fp);
                try
                {
                    wallet.Unlock(PWS);
                }
                catch (CryptographicException e)
                {
                    //MessageBox.Show(Strings.PasswordIncorrect);
                    //return e.Message;
                }
            }
        }
        public IActionResult OpenWallet(string path, string password)
        {
            if (!SystemFile.Exists(path))
            {
                return(NotFound());
            }
            switch (GetExtension(path))
            {
            case ".db3":
            {
                wallet = UserWallet.Open(path, password);
                break;
            }

            case ".json":
            {
                NEP6Wallet nep6wallet = new NEP6Wallet(path);
                nep6wallet.Unlock(password);
                wallet = nep6wallet;
                break;
            }

            default:
                throw new NotSupportedException();
            }
            return(FormatJson("Success"));
        }
예제 #5
0
파일: WalletVC.cs 프로젝트: RALEx147/NEOSx
        partial void walletOpen(NSObject sender)
        {
            //start loading bar

            var t = Task.Run(() =>
            {
                NEP6Wallet w = new NEP6Wallet("testnet.json");
                w.Unlock("qwerasdfqwer");
                return(w);
            });

            t.ContinueWith((w) => {
                var v = w.Result.GetAccounts();

                //current wallet is w
                //setup ui with w address
                //stop loading bar
                //close poopup
                BeginInvokeOnMainThread(() =>
                {
                    foreach (WalletAccount s in v)
                    {
                        DataSource.addresses.Add(new AddressTable(s.Address));
                    }
                    table.ReloadData();
                });
            }
                           );
        }
예제 #6
0
 public void TestGetKey()
 {
     _account.GetKey().Should().BeNull();
     _wallet.Unlock("Satoshi");
     _account = new NEP6Account(_wallet, _hash, _nep2);
     _account.GetKey().Should().Be(_keyPair);
 }
예제 #7
0
        public void CreateWallet(string path, string password)
        {
            switch (Path.GetExtension(path))
            {
            case ".db3":
            {
                UserWallet    wallet  = UserWallet.Create(path, password);
                WalletAccount account = wallet.CreateAccount();
                Console.WriteLine($"   Address: {account.Address}");
                Console.WriteLine($"    Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
                Console.WriteLine($"ScriptHash: {account.ScriptHash}");
                CurrentWallet = wallet;
            }
            break;

            case ".json":
            {
                NEP6Wallet wallet = new NEP6Wallet(path);
                wallet.Unlock(password);
                WalletAccount account = wallet.CreateAccount();
                wallet.Save();
                Console.WriteLine($"   Address: {account.Address}");
                Console.WriteLine($"    Pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
                Console.WriteLine($"ScriptHash: {account.ScriptHash}");
                CurrentWallet = wallet;
            }
            break;

            default:
                Console.WriteLine("Wallet files in that format are not supported, please use a .json or .db3 file extension.");
                break;
            }
        }
예제 #8
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");
            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);
        }
예제 #9
0
        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;
        }
예제 #10
0
        /// <summary>
        /// 初始化钱包
        /// Add Code
        /// </summary>
        protected internal override void InitWallet()
        {
            #region 测试代码
            // 测试钱包索引更新
            //string[] publickeys =  { "0286036b123ada4ee19f08fb068825f203e17d0078472c126cc4317e3fc6f87135",
            //    "02726e970bd85975ed07ad2c9f22123ea92e427537d57799f497e26a09949e3515",
            //"0303c8f06b27689280127a60cabcbfb6a62ee7e06fe5c3cf903472c7a5afd8d502",
            //"03ea1e4929619f27398951e2bdf17ca964066a3c3d4339364cf99ed44b4a3c8f30"};
            //NEP6Wallet wallets = new NEP6Wallet();
            //foreach (string publickey in publickeys)
            //{
            //    wallets.RegisterLocalWallet(publickey);
            //}
            #endregion

            #region 通过文件加载钱包
            string path = "wallet/wallet.json"; // 钱包文件
            if (!File.Exists(path))
            {
                FileInfo file = new FileInfo(path);
                Directory.CreateDirectory(file.DirectoryName);
                // 创建钱包 保存
                NEP6Wallet wallet = new NEP6Wallet(path);
                wallet.Unlock(GetDefaultPassword()); // 添加锁定密码
                WalletAccount account = wallet.CreateAccount();
                wallet.save();                       // 保存到 文件中
                //Program.Wallet = wallet; // 打开钱包
                PrintAccountInfo(account);
            }
            // 重新打开钱包文件   需要把私钥从内存中去除
            NEP6Wallet nep6wallet = new NEP6Wallet(path, GetDefaultPassword(), 1);
            Program.Wallet = nep6wallet;
            #endregion
        }
예제 #11
0
        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)));
        }
예제 #12
0
        private void 打开钱包数据库OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenWalletDialog dialog = new OpenWalletDialog())
            {
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string path = dialog.WalletPath;

                LoadingFrm loadingFrm = new LoadingFrm("Opening...");
                SplashScreen.Show(loadingFrm);

                NEP6Wallet nep6wallet = new NEP6Wallet(dialog.WalletPath, null);
                try
                {
                    nep6wallet.Unlock(dialog.Password);
                }
                catch (CryptographicException)
                {
                    SplashScreen.Close();
                    MessageBox.Show(Strings.PasswordIncorrect);
                    return;
                }

                Settings.Default.LastWalletPath = path;
                Settings.Default.Save();

                ChangeWallet(nep6wallet);

                Program.Wallet = nep6wallet;

                SplashScreen.Close();
            }
        }
예제 #13
0
        private void 创建钱包数据库NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (CreateWalletDialog dialog = new CreateWalletDialog())
            {
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                LoadingFrm loadingFrm = new LoadingFrm("Creating...");
                SplashScreen.Show(loadingFrm);

                NEP6Wallet wallet = new NEP6Wallet(dialog.WalletPath, null);
                wallet.Unlock(dialog.Password);
                WalletAccount account = wallet.CreateAccount();
                wallet.Save();
                Settings.Default.LastWalletPath = dialog.WalletPath;
                Settings.Default.Save();

                Program.Wallet = wallet;
                //ZoroChainSystem.Singleton.SetWallet(Program.Wallet);

                SplashScreen.Close();
            }
        }
예제 #14
0
        public void OpenWallet(string path, string password)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }

            switch (Path.GetExtension(path).ToLowerInvariant())
            {
            case ".db3":
            {
                CurrentWallet = UserWallet.Open(path, password);
                break;
            }

            case ".json":
            {
                NEP6Wallet nep6wallet = new NEP6Wallet(path);
                nep6wallet.Unlock(password);
                CurrentWallet = nep6wallet;
                break;
            }

            default: throw new NotSupportedException();
            }
        }
예제 #15
0
 public void TestGetKey()
 {
     account.GetKey().Should().BeNull();
     wallet.Unlock("Satoshi");
     account = new NEP6Account(wallet, hash, nep2);
     account.GetKey().Should().Be(keyPair);
 }
예제 #16
0
        private JObject OpenWallet(JArray _params)
        {
            string path     = _params[0].AsString();
            string password = _params[1].AsString();

            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            switch (GetExtension(path))
            {
            case ".db3":
            {
                wallet = UserWallet.Open(path, password);
                break;
            }

            case ".json":
            {
                NEP6Wallet nep6wallet = new NEP6Wallet(path);
                nep6wallet.Unlock(password);
                wallet = nep6wallet;
                break;
            }

            default:
                throw new NotSupportedException();
            }
            return(true);
        }
예제 #17
0
        private bool OnCreateWalletCommand(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("error");
                return(true);
            }
            string path     = args[2];
            string password = ReadUserInput("password", true);

            if (password.Length == 0)
            {
                Console.WriteLine("cancelled");
                return(true);
            }
            string password2 = ReadUserInput("password", true);

            if (password != password2)
            {
                Console.WriteLine("error");
                return(true);
            }
            switch (Path.GetExtension(path))
            {
            case ".db3":
            {
                Program.Wallet = UserWallet.Create(GetIndexer(), path, password);
                WalletAccount account = Program.Wallet.CreateAccount();
                Console.WriteLine($"address: {account.Address}");
                Console.WriteLine($" pubkey: {account.GetKey().PublicKey.EncodePoint(true).ToHexString()}");
                if (system.RpcServer != null)
                {
                    system.RpcServer.Wallet = Program.Wallet;
                }
            }
            break;

            case ".json":
            {
                NEP6Wallet wallet = new NEP6Wallet(GetIndexer(), 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()}");
                if (system.RpcServer != null)
                {
                    system.RpcServer.Wallet = Program.Wallet;
                }
            }
            break;

            default:
                Console.WriteLine("Wallet files in that format are not supported, please use a .json or .db3 file extension.");
                break;
            }
            return(true);
        }
예제 #18
0
        public void CreateWallet(string walletPath, string password)
        {
            var newWallet = new NEP6Wallet(walletPath);

            newWallet.Unlock(password);

            this.SetCurrentWallet(newWallet);
        }
예제 #19
0
 public void TestChangePassword()
 {
     _account = new NEP6Account(_wallet, _hash, _nep2);
     _account.ChangePasswordPrepare("b", "Satoshi").Should().BeTrue();
     _account.ChangePasswordCommit();
     _account.Contract = new Contract();
     _wallet.Unlock("Satoshi");
     _account.ChangePasswordPrepare("b", "Satoshi").Should().BeFalse();
     _account.ChangePasswordPrepare("Satoshi", "b").Should().BeTrue();
     _account.ChangePasswordCommit();
     _account.VerifyPassword("b").Should().BeTrue();
     _account.ChangePasswordPrepare("b", "Satoshi").Should().BeTrue();
     _account.ChangePasswordCommit();
     _account.ChangePasswordPrepare("Satoshi", "b").Should().BeTrue();
     _account.ChangePasswordRoolback();
     _account.VerifyPassword("Satoshi").Should().BeTrue();
     _wallet.Lock();
 }
예제 #20
0
 private JObject OnUnLock(JArray _params)
 {
     if (wallet == null)
     {
         return(false);
     }
     this.locker = wallet.Unlock(_params[0].AsString());
     return(true);
 }
예제 #21
0
        public void Export(string filename, string password)
        {
            var nep6Wallet = new NEP6Wallet(filename, ProtocolSettings, Name);

            nep6Wallet.Unlock(password);
            foreach (var account in GetAccounts())
            {
                nep6Wallet.CreateAccount(account.Contract, account.GetKey());
            }
            nep6Wallet.Save();
        }
예제 #22
0
        /// <summary>
        /// create new wallet
        /// </summary>
        /// <param name="path"></param>
        /// <param name="password"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public async Task <object> CreateWallet(string path, string password, string privateKey = null)
        {
            var result        = new WalletModel();
            var hexPrivateKey = privateKey.TryGetPrivateKey();

            try
            {
                switch (Path.GetExtension(path))
                {
                case ".db3":
                {
                    UserWallet wallet  = UserWallet.Create(path, password, CliSettings.Default.Protocol);
                    var        account = hexPrivateKey.NotEmpty() ? wallet.CreateAccount(hexPrivateKey) : wallet.CreateAccount();
                    result.Accounts.Add(new AccountModel()
                        {
                            AccountType = AccountType.Standard,
                            Address     = account.Address,
                            ScriptHash  = account.ScriptHash,
                        });
                    Program.Starter.CurrentWallet = wallet;
                }
                break;

                case ".json":
                {
                    NEP6Wallet wallet = new NEP6Wallet(path, CliSettings.Default.Protocol);
                    wallet.Unlock(password);
                    var account = hexPrivateKey.NotEmpty() ? wallet.CreateAccount(hexPrivateKey) : wallet.CreateAccount();
                    wallet.Save();
                    result.Accounts.Add(new AccountModel()
                        {
                            AccountType = AccountType.Standard,
                            ScriptHash  = account.ScriptHash,
                            Address     = account.Address,
                        });
                    Program.Starter.CurrentWallet = wallet;
                }
                break;

                default:
                    throw new Exception("Wallet files in that format are not supported, please use a .json or .db3 file extension.");
                }
            }
            catch (CryptographicException e)
            {
                return(Error(ErrorCode.WrongPassword));
            }


            GetNeoAndGas(result.Accounts);
            return(result);
        }
예제 #23
0
        public void CreateWallet(string walletPath, string password, bool createWithAccount = true)
        {
            var newWallet = new NEP6Wallet(walletPath);

            var walletLocker = newWallet.Unlock(password);

            this.SetCurrentWallet(newWallet, walletLocker);

            if (createWithAccount)
            {
                this.CreateNewAccount();
            }
        }
예제 #24
0
 private static Wallet OpenWallet(WalletIndexer indexer, string path, string password)
 {
     if (Path.GetExtension(path) == ".db3")
     {
         return(UserWallet.Open(indexer, path, password));
     }
     else
     {
         NEP6Wallet nep6wallet = new NEP6Wallet(indexer, path);
         nep6wallet.Unlock(password);
         return(nep6wallet);
     }
 }
예제 #25
0
        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);
        }
예제 #26
0
        //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);
        }
예제 #27
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);
        }
예제 #28
0
        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);
        }
예제 #29
0
 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);
     }
 }
예제 #30
0
        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);
        }