Ejemplo n.º 1
0
        public static void TestCoroutine(string[] args)
        {
            try
            {
                Wallet    wallet    = Wallet.GetWallet();
                WalletKey walletKey = wallet.GetCurWallet();

                Log.Info($"\npublic_key  {walletKey.publickey.ToHexString()}   {walletKey.publickey.ToHexString().Length}");
                Log.Info($"\nprivate_key {walletKey.privatekey.ToHexString()}   {walletKey.privatekey.ToHexString().Length}");

                byte[] message = "e92e086a818ee476dea6f46d15ededb441bbe7926e806efe45dbad14a4b627f0".HexToBytes();
                string aa      = message.ToHexString();
                Log.Info($"\n aa {aa}   {aa.Length}");

                Log.Info($"\nmessage {message.ToHexString()}   {message.ToHexString().Length}");

                byte[] signature = Wallet.Sign(message, walletKey);

                Log.Info($"\nsignature {signature.ToHexString()}  {signature.Length}");

                if (Wallet.Verify(signature, message, Wallet.ToAddress(walletKey.publickey)))
                {
                    Log.Info($"\nverify Success");
                }

                Log.Info($"\nAddress {walletKey.ToAddress()}  {walletKey.ToAddress().Length}");
            }
            catch (Exception e)
            {
                Log.Debug(e.ToString());
            }
        }
Ejemplo n.º 2
0
        public void callFun(HttpMessage httpMessage)
        {
            httpMessage.result = "command error! \nexample: contract consAddress callFun";
            GetParam(httpMessage, "1", "consAddress", out string consAddress);
            if (!GetParam(httpMessage, "2", "data", out string data))
            {
                return;
            }
            data = System.Web.HttpUtility.UrlDecode(data);

            WalletKey key    = Wallet.GetWallet().GetCurWallet();
            var       sender = key.ToAddress();

            using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot())
            {
                var consensus = Entity.Root.GetComponent <Consensus>();
                var luaVMEnv  = Entity.Root.GetComponent <LuaVMEnv>();

                var blockSub = new BlockSub();
                blockSub.addressIn  = sender;
                blockSub.addressOut = consAddress;
                blockSub.data       = data;

                try
                {
                    if (luaVMEnv.IsERC(dbSnapshot, consAddress, ""))
                    {
                        bool rel = luaVMEnv.Execute(dbSnapshot, blockSub, consensus.transferHeight, out object[] result);
                        if (rel)
                        {
                            if (result != null)
                            {
                                if (result.Length == 1)
                                {
                                    httpMessage.result = JsonHelper.ToJson(result[0]);
                                }
                                else
                                {
                                    httpMessage.result = JsonHelper.ToJson(result);
                                }
                            }
                        }
                        else
                        {
                            httpMessage.result = "error";
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Ejemplo n.º 3
0
        public void OnBeRuler(HttpMessage httpMessage)
        {
            var consensus = Entity.Root.GetComponent <Consensus>();
            var blockMgr  = Entity.Root.GetComponent <BlockMgr>();

            // 判断当前块高度是否接近主线
            if (blockMgr.newBlockHeight - consensus.transferHeight > 1000)
            {
                httpMessage.result = $"{consensus.transferHeight}:{blockMgr.newBlockHeight} The current block height is too low. command BeRuler have been ignore.";
                return;
            }

            WalletKey key = Wallet.GetWallet().GetCurWallet();

            var  address = key.ToAddress();
            long notice  = 1;

            using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                var account = dbSnapshot.Accounts.Get(address);
                if (account != null)
                {
                    notice = account.nonce + 1;
                }
            }

            BlockSub transfer = new BlockSub();

            transfer.addressIn  = address;
            transfer.addressOut = consensus.consAddress;
            transfer.amount     = "0";
            transfer.nonce      = notice;
            transfer.type       = "contract";

            LuaVMCall luaVMCall = new LuaVMCall();

            luaVMCall.fnName   = "add";
            luaVMCall.args     = new FieldParam[0];
            transfer.data      = luaVMCall.Encode();
            transfer.timestamp = TimeHelper.Now();
            transfer.hash      = transfer.ToHash();
            transfer.sign      = transfer.ToSign(key);

            var rel = Entity.Root.GetComponent <Rule>().AddTransfer(transfer);

            if (rel == -1)
            {
                OnTransferAsync(transfer);
            }
            httpMessage.result = $"accepted transfer:{transfer.hash}";
        }
Ejemplo n.º 4
0
        public static void MakeGenesis()
        {
            if (true)
            {
                WalletKey key = Wallet.GetWallet().GetCurWallet();

                // Genesis
                Block blk = new Block();
                blk.Address   = key.ToAddress();
                blk.prehash   = "";
                blk.height    = 1;
                blk.timestamp = TimeHelper.NowSeconds();
                blk.random    = RandomHelper.RandUInt64().ToString("x");

                // Transfer
                {
                    Transfer transfer = new Transfer();
                    transfer.addressIn  = "";
                    transfer.addressOut = blk.Address;
                    transfer.amount     = (3L * 100L * 10000L * 10000L).ToString();
                    transfer.nonce      = 1;
                    transfer.type       = "tranfer";
                    transfer.hash       = transfer.ToHash();
                    transfer.sign       = transfer.ToSign(key);
                    blk.AddTransfer(0, transfer);
                }

                // rule Consensus
                {
                    Transfer transfer = new Transfer();
                    transfer.addressIn  = blk.Address;
                    transfer.addressOut = "";
                    transfer.amount     = "0";
                    transfer.nonce      = 1;
                    transfer.type       = "contract";
                    transfer.data       = Base58.Encode(FileHelper.GetFileData("./Data/Contract/RuleContract_v1.0.lua").ToByteArray());
                    transfer.hash       = transfer.ToHash();
                    transfer.sign       = transfer.ToSign(key);
                    blk.AddTransfer(1, transfer);
                }

                blk.hash = blk.ToHash();
                blk.sign = blk.ToSign(key);
                File.WriteAllText("./Data/genesisBlock.dat", JsonHelper.ToJson(blk));
            }
        }
Ejemplo n.º 5
0
        public void OnBeRuler(HttpMessage httpMessage)
        {
            Consensus consensus = Entity.Root.GetComponent <Consensus>();
            WalletKey key       = Wallet.GetWallet().GetCurWallet();

            var  address = key.ToAddress();
            long notice  = 1;

            using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                var account = dbSnapshot.Accounts.Get(address);
                if (account != null)
                {
                    notice = account.notice + 1;
                }
            }

            Transfer transfer = new Transfer();

            transfer.addressIn  = address;
            transfer.addressOut = consensus.consAddress;
            transfer.amount     = "0";
            transfer.notice     = notice;
            transfer.type       = "contract";

            LuaVMCall luaVMCall = new LuaVMCall();

            luaVMCall.fnName = "Add";
            luaVMCall.args   = new FieldParam[0];
            //luaVMCall.args[0] = new FieldParam();
            //luaVMCall.args[0].type = "Int64";
            //luaVMCall.args[0].value = "999";
            //long aaa = (long)luaVMCall.args[0].GetValue();
            transfer.data = luaVMCall.Encode();

            transfer.hash = transfer.ToHash();
            transfer.sign = transfer.ToSign(key);

            var rel = Entity.Root.GetComponent <Rule>().AddTransfer(transfer);

            if (rel == -1)
            {
                OnTransferAsync(transfer);
            }
            httpMessage.result = $"accepted transfer:{transfer.hash}";
        }
Ejemplo n.º 6
0
        int OpenWallet(string password)
        {
            passwords = password;
            try
            {
                if (!File.Exists(walletFile))
                {
                    return(-1);
                }

                string allText    = File.ReadAllText(walletFile, System.Text.Encoding.UTF8);
                var    walletJson = JsonHelper.FromJson <WalletJson>(allText);
                if (walletJson == null || walletJson.version < 101)
                {
                    return(-3);
                }
                var aes256 = new AesEverywhere.AES256();

                for (int i = 0; i < walletJson.accounts.Count; i++)
                {
                    WalletKey walletKey = new WalletKey();
                    string    base64    = aes256.Decrypt(walletJson.accounts[i].encrypted, passwords);
                    walletKey.random = base64.HexToBytes();

                    ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, walletKey.random);

                    if (walletKey.ToAddress() != walletJson.accounts[i].address)
                    {
                        return(-2);
                    }

                    keys.Add(walletKey);
                }

                curIndex = walletJson.curIndex;
            }
            catch (Exception)
            {
                return(-2);
            }
            return(1);
        }
Ejemplo n.º 7
0
        int OpenWallet(string password)
        {
            passwords = password;
            try
            {
                string[] lines = File.ReadAllLines(walletFile, System.Text.Encoding.Default);
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] array = lines[i].Split(',');
                    if (array.Length == 2)
                    {
                        string    seed      = CryptoHelper.Sha256(array[0] + "#" + password);
                        WalletKey walletKey = new WalletKey();
                        ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, seed.HexToBytes());

                        if (walletKey.ToAddress() != array[1])
                        {
                            return(-2);
                        }

                        walletKey.random = array[0].HexToBytes();
                        keys.Add(walletKey);
                    }
                    else
                    if (array.Length == 1)
                    {
                        curIndex = int.Parse(array[0]);
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
            return(1);
        }
Ejemplo n.º 8
0
        public static async System.Threading.Tasks.Task OrderAsync(HttpMessage httpMessage)
        {
            Wallet.Inst = null;
            if (httpMessage.map["0"] == "importkey")
            {
                if (!GetParam(httpMessage, "1", "1", out string privatekey))
                {
                    httpMessage.result = "command error! \nexample: import key privatekey";
                    return;
                }
                string address = AddKeyBag(privatekey, Program.keyBag);
                httpMessage.result = $" {address}";
            }
            else if (httpMessage.map["0"] == "showip")
            {
                httpMessage.result = JsonHelper.ToJson(Program.ruleIP);
            }
            else if (httpMessage.map["0"] == "openwallet")
            {
                Wallet wallet = new Wallet();
                if (httpMessage.map.Count == 2)
                {
                    httpMessage.map.Add("1", "./smartx-wallet.json");
                }
                if (Program.wallet.keys.Count > 0)
                {
                    httpMessage.result = "please close wallet first!";
                    return;
                }
                try
                {
                    string path    = httpMessage.map["1"];
                    Wallet wallet1 = Wallet.GetWallet(path);
                    wallet = wallet1;
                }
                catch (Exception e)
                {
                    httpMessage.result = "Please check your path";
                    return;
                }


                if (wallet == null)
                {
                    httpMessage.result = "Password error";
                    return;
                }
                if (Program.keyBag.Count > 0)
                {
                    if (!Program.keyBag.ContainsKey(wallet.keys[0].ToAddress()))
                    {
                        Program.keyBag.Add(Wallet.ToAddress(wallet.keys[0].publickey), wallet.keys[0]);
                    }
                }
                else
                {
                    Program.keyBag.Add(Wallet.ToAddress(wallet.keys[0].publickey), wallet.keys[0]);
                }
                Program.wallet = wallet;
                //string random = Program.wallet.keys[0].random.ToHex();
                //string nonce="0";
                string nonce = await GetNonceAsync(Wallet.ToAddress(Program.wallet.keys[0].publickey), httpMessage, Program.ruleIP);//0

                if (nonce == "INVALID URL, set the correct url" || nonce == "false")
                {
                    httpMessage.result = nonce;
                    return;
                }
                Program.nonce = (int.Parse(nonce) + 1).ToString();
                //Console.WriteLine(Program.nonce);
                httpMessage.result = Wallet.ToAddress(Program.wallet.keys[0].publickey) + " open";
            }
            else if (httpMessage.map["0"] == "setwallet")
            {
                if (!GetParam(httpMessage, "1", "address", out string address))
                {
                    httpMessage.result = "command error! \nexample: setwallet address";
                    return;
                }
                WalletKey key = new WalletKey();
                if (Program.keyBag.Count > 0)
                {
                    if (Program.keyBag.ContainsKey(address))
                    {
                        key = Program.keyBag[address];
                    }
                    else
                    {
                        httpMessage.result = "The address isn't in the key bag";
                        return;
                    }
                }
                else
                {
                    httpMessage.result = "The address isn't in the key bag";
                    return;
                }

                if (Program.wallet.keys.Count != 0)
                {
                    httpMessage.result = "please close wallet first!";
                    return;
                }
                Program.wallet.keys.Add(key);
                string nonce = await GetNonceAsync(address, httpMessage, Program.ruleIP);//0

                if (nonce == "INVALID URL, set the correct url" || nonce == "false")
                {
                    httpMessage.result = nonce;
                    return;
                }
                Program.nonce      = (int.Parse(nonce) + 1).ToString();
                httpMessage.result = $"{address} set";
            }
            else if (httpMessage.map["0"] == "list")
            {
                List <string> list = new List <string>();
                foreach (KeyValuePair <string, WalletKey> k in Program.keyBag)
                {
                    list.Add(k.Key);
                }
                httpMessage.result = JsonHelper.ToJson(list);
            }
            else if (httpMessage.map["0"] == "closewallet")
            {
                Wallet.Inst        = null;
                Program.wallet     = new Wallet();
                Program.nonce      = "";
                httpMessage.result = "wallet closed";
            }
            else if (httpMessage.map["0"] == "getprivatekey")
            {
                if (Program.wallet.keys.Count != 0)
                {
                    httpMessage.result = JsonHelper.ToJson(Program.wallet.keys[0].random.ToHexString());
                }
                else
                {
                    httpMessage.result = "please set wallet";
                }
            }
            else if (httpMessage.map["0"] == "exportkey")
            {
                if (Program.wallet.keys.Count == 0)
                {
                    httpMessage.result = "please set wallet";
                    return;
                }
                if (httpMessage.map.Count <= 2)
                {
                    File.WriteAllText("./private.json", Program.wallet.keys[0].random.ToHexString());
                    httpMessage.result = $"export key  successful";
                    return;
                }
                else if (Program.wallet.keys.Count > 0)
                {
                    try
                    {
                        File.WriteAllText(httpMessage.map["1"] + "/private.json", Program.wallet.keys[0].random.ToHexString());
                        httpMessage.result = $"export key  successful";
                    }
                    catch (Exception)
                    {
                        httpMessage.result = "Please check the path";
                        return;
                    }
                }
                else
                {
                    httpMessage.result = "Please set the wallet first";
                }
            }
            else if (httpMessage.map["0"] == "clear")
            {
                Console.Clear();
            }
            else if (httpMessage.map["0"] == "transfer")
            {
                if (httpMessage.map.Count <= 3)
                {
                    Console.WriteLine("transfer error example transfer addressOut amount fee");
                    return;
                }
                if (Program.wallet.keys.Count == 0)
                {
                    httpMessage.result = "Please set the wallet first";
                    return;
                }
                if (!Wallet.CheckAddress(httpMessage.map["1"]))
                {
                    httpMessage.result = "Please check addressOut";
                    return;
                }
                TransferInfo(httpMessage, Program.ruleIP);
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                if (result.result.Contains("true"))
                {
                    Program.nonce      = (int.Parse(Program.nonce) + 1).ToString();
                    httpMessage.result = result.result + " " + httpMessage.map["hash"];
                }
                else
                {
                    httpMessage.result = "false";
                }
            }
            else if (httpMessage.map["0"] == "createkey")
            {
                Console.WriteLine("Please enter random word: ");
                string    input     = Console.ReadLine();
                WalletKey walletKey = new WalletKey();
                walletKey.random = CryptoHelper.Sha256(Seek().ToHexString() + "#" + input).HexToBytes();
                ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, walletKey.random);
                if (walletKey.random != null)
                {
                    Dictionary <string, string> walletinfo = new Dictionary <string, string>();
                    walletinfo.Add("address", walletKey.ToAddress());
                    walletinfo.Add("privatekey", walletKey.random.ToHexString());
                    httpMessage.result = JsonHelper.ToJson(walletinfo);
                    return;
                }
                httpMessage.result = "createkey error";
            }
            else if (httpMessage.map["0"] == "createwallet")
            {
                OnCreat(httpMessage);
            }
            else if (httpMessage.map["0"] == "mempool")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "getlastblock")
            {
                try
                {
                    if (!GetParam(httpMessage, "1", "index", out string index))
                    {
                        httpMessage.result = "command error! \nexample: getlastblock index";
                        return;
                    }
                    string height = await GetHeightAsync();

                    httpMessage.map["1"] = (long.Parse(height) - 19 * long.Parse(httpMessage.map["1"])) != 0 ? (long.Parse(height) - 19 * long.Parse(httpMessage.map["1"])).ToString() : "0";
                    HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                    httpMessage.result = result.result;
                } catch (Exception e)
                {
                    httpMessage.result = "command error! \nexample: getlastblock index";
                }
            }
            else if (httpMessage.map["0"] == "transfercount")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "search")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "miner")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "node")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "beruler")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "rules")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "stats")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "account")
            {
                HttpMessage result = await ComponentNetworkHttp.QueryCommand(Program.ruleIP + $"/{httpMessage.map["cmd"]}", httpMessage);

                httpMessage.result = result.result;
            }
            else if (httpMessage.map["0"] == "help")
            {
                OnHelp(httpMessage);
            }
            else
            {
                httpMessage.result = "command error";
            }
        }
Ejemplo n.º 9
0
        public void OnContract(HttpMessage httpMessage)
        {
            httpMessage.result = "command error! \nexample: contract consAddress callFun";
            GetParam(httpMessage, "1", "consAddress", out string consAddress);
            GetParam(httpMessage, "1", "depend", out string depend);
            if (!GetParam(httpMessage, "2", "callFun", out string callFun))
            {
                return;
            }

            WalletKey key    = Wallet.GetWallet().GetCurWallet();
            var       sender = key.ToAddress();

            if (callFun.IndexOf("create(") != -1)
            {
                var consensus = Entity.Root.GetComponent <Consensus>();
                var blockMgr  = Entity.Root.GetComponent <BlockMgr>();

                long notice = 1;
                using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
                {
                    var account = dbSnapshot.Accounts.Get(sender);
                    if (account != null)
                    {
                        notice = account.nonce + 1;
                    }
                }

                BlockSub transfer = new BlockSub();
                transfer.addressIn  = sender;
                transfer.addressOut = null;
                transfer.amount     = "0";
                transfer.nonce      = notice;
                transfer.type       = "contract";
                transfer.depend     = depend;
                transfer.data       = callFun;
                var luaVMCall = LuaVMCall.Decode(transfer.data);
                Log.Info(JsonHelper.ToJson(luaVMCall));

                //transfer.timestamp = TimeHelper.Now();
                //transfer.hash = transfer.ToHash();
                //transfer.sign = transfer.ToSign(key);

                //var rel = Entity.Root.GetComponent<Rule>().AddTransfer(transfer);
                //if (rel == -1)
                //{
                //    OnTransferAsync(transfer);
                //}
                //httpMessage.result = $"accepted transfer:{transfer.hash}";
            }
            else
            {
                using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot())
                {
                    var consensus = Entity.Root.GetComponent <Consensus>();
                    var luaVMEnv  = Entity.Root.GetComponent <LuaVMEnv>();

                    var blockSub = new BlockSub();
                    blockSub.addressIn  = sender;
                    blockSub.addressOut = consAddress;
                    blockSub.data       = callFun;
                    bool rel = luaVMEnv.Execute(dbSnapshot, blockSub, consensus.transferHeight, out object[] result);
                    httpMessage.result = JsonHelper.ToJson(result);
                }
            }
        }