Пример #1
0
        private static string AddKeyBag(string randomseed, Dictionary <string, WalletKey> keyBag)
        {
            WalletKey walletKey = new WalletKey();

            ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, randomseed.HexToBytes());
            string address = Wallet.ToAddress(walletKey.publickey);

            walletKey.random = randomseed.HexToBytes();
            if (Program.keyBag.Count > 0)
            {
                if (keyBag.ContainsKey(address))
                {
                    return("The address already exists!");
                }
            }
            else
            {
                if (!Program.keyBag.ContainsKey(address))
                {
                    keyBag.Add(address, walletKey);
                }
            }


            return(address);
        }
Пример #2
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());
            }
        }
Пример #3
0
        static public void Import(string privatekey, string walletFile = "./Data/walletImport.json")
        {
            var walletKey = new WalletKey();

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

            var keys = new List <WalletKey>();

            keys.Add(walletKey);

            var walletJson = new WalletJson();

            walletJson.curIndex = 0;
            walletJson.accounts = new List <WalletJsonAddress>();

            var aes256 = new AesEverywhere.AES256();

            for (int i = 0; i < keys.Count; i++)
            {
                var walletJsonAddress = new WalletJsonAddress();

                walletJsonAddress.address   = keys[i].ToAddress();
                walletJsonAddress.encrypted = aes256.Encrypt(keys[i].random.ToHexString(), "smartx123");

                walletJson.accounts.Add(walletJsonAddress);
            }

            File.WriteAllText(walletFile, JsonHelper.ToJson(walletJson), System.Text.Encoding.UTF8);
        }
Пример #4
0
        public void MinerReward()
        {
            if (httpPool != null)
            {
                Dictionary <string, MinerTask> miners = httpPool.GetMinerReward(out long miningHeight);
                if (miners != null && miningHeight + 3 < height)
                {
                    string ownerAddress = Wallet.GetWallet().GetCurWallet().ToAddress();

                    var mcblk = BlockChainHelper.GetMcBlock(miningHeight);
                    if (mcblk != null && mcblk.Address == ownerAddress)
                    {
                        var       miner     = miners.Values.First(c => c.random == mcblk.random);
                        WalletKey walletKey = Wallet.GetWallet().GetCurWallet();

                        // 出块奖励
                        if (miner != null)
                        {
                            Transfer transfer = new Transfer();
                            transfer.addressIn  = ownerAddress;
                            transfer.addressOut = miner.address;
                            transfer.amount     = Consensus.GetReward(miningHeight).ToString();
                            transfer.nonce      = TimeHelper.NowSeconds();
                            transfer.type       = "tranfer";
                            transfer.hash       = transfer.ToHash();
                            transfer.sign       = transfer.ToSign(walletKey);
                            minerTransfer.Add(mcblk.hash, transfer);
                        }

                        // 参与奖励
                    }
                    httpPool.DelMiner(miningHeight);
                }
            }
        }
Пример #5
0
        static public void Test2()
        {
            WalletKey walletKey = new WalletKey();

            byte[] byteArray = "aa306f7fad8f12dad3e7b90ee15af0b39e9eccd1aad2e757de2d5ad74b42b67a".HexToBytes();
            byte[] seed32    = new byte[32];
            Buffer.BlockCopy(byteArray, 0, seed32, 0, seed32.Length);
            ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, seed32);


            string address = Wallet.ToAddress(walletKey.publickey);

            Log.Info("publickey  \n" + walletKey.publickey.ToHexString());
            Log.Info("privatekey \n" + walletKey.privatekey.ToHexString());
            Log.Info("Address    \n" + address);


            byte[] data = "e33b68cd7ad3dc29e623e399a46956d54c1861c5cd1e5039b875811d2ca4447d".HexToBytes();
            byte[] sign = Wallet.Sign(data, walletKey);

            Log.Info("sign \n" + sign.ToHexString());

            if (Wallet.Verify(sign, data, address))
            {
                Log.Info("Verify ok ");
            }
        }
Пример #6
0
 static public byte[] Sign(byte[] data, WalletKey key)
 {
     byte[] signature = new byte[64];
     byte[] sign      = new byte[64 + 32];
     ed25519.ed25519_sign(signature, data, data.Length, key.publickey, key.privatekey);
     Buffer.BlockCopy(signature, 0, sign, 0, signature.Length);
     Buffer.BlockCopy(key.publickey, 0, sign, signature.Length, key.publickey.Length);
     return(sign);
 }
Пример #7
0
        public WalletKey Create()
        {
            WalletKey walletKey = new WalletKey();
            string    input     = Wallet.Inst.Input("Please enter random word: ");

            walletKey.random = CryptoHelper.Sha256(Seek().ToHexString() + "#" + input).HexToBytes();
            ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, walletKey.random);
            this.keys.Add(walletKey);
            return(walletKey);
        }
Пример #8
0
        public WalletKey Create()
        {
            WalletKey walletKey = new WalletKey();

            walletKey.random = Seek();
            string seed = CryptoHelper.Sha256(walletKey.random.ToHexString() + "#" + passwords);

            ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, seed.HexToBytes());
            this.keys.Add(walletKey);
            return(walletKey);
        }
Пример #9
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)
                {
                }
            }
        }
Пример #10
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}";
        }
Пример #11
0
        static public void Test()
        {
            string info = "";

            for (int i = 0; i < 100; i++)
            {
                WalletKey walletKey = new WalletKey();
                walletKey.random = Seek();
                string seed = CryptoHelper.Sha256(walletKey.random.ToHexString() + "#" + "123");
                ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, seed.HexToBytes());

                string address = Wallet.ToAddress(walletKey.publickey);
                info += address + "\n";
            }
            Log.Info("Address    \n" + info);
        }
Пример #12
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));
            }
        }
Пример #13
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}";
        }
Пример #14
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);
        }
Пример #15
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);
        }
Пример #16
0
 static public byte[] ToSign(this Block This, WalletKey key)
 {
     return(Wallet.Sign(This.hash.HexToBytes(), key));
 }
Пример #17
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";
            }
        }
Пример #18
0
 public byte[] ToSign(WalletKey key)
 {
     return(Wallet.Sign(hash.HexToBytes(), key));
 }
Пример #19
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);
                }
            }
        }
Пример #20
0
        // Miner reward, only after confirming that it cannot be rolled back
        public Dictionary <string, BlockSub> MinerReward_PPLNS(string today, long minHeight, long maxHeight)
        {
            Dictionary <string, BlockSub> minerTransfer = new Dictionary <string, BlockSub>();

            if (httpPool != null)
            {
                WalletKey walletKey = Wallet.GetWallet().GetCurWallet();
                for (long rewardheight = minHeight; rewardheight < maxHeight; rewardheight++)
                {
                    Dictionary <string, MinerTask> miners = null;
                    using (DbSnapshot snapshot = PoolDBStore.GetSnapshot())
                    {
                        string json = snapshot.Get("Pool_H_" + rewardheight);
                        if (!string.IsNullOrEmpty(json))
                        {
                            miners = JsonHelper.FromJson <Dictionary <string, MinerTask> >(json);
                        }
                    }

                    //var miners = httpPool.GetMiner(rewardheight);
                    if (miners != null)
                    {
                        string ownerAddress = Wallet.GetWallet().GetCurWallet().ToAddress();

                        var mcblk = BlockChainHelper.GetMcBlock(rewardheight);
                        if (mcblk != null && mcblk.Address == ownerAddress)
                        {
                            BigFloat reward = new BigFloat(Consensus.GetReward(rewardheight));
                            reward = reward * (1.0f - serviceFee);

                            var miner = miners.Values.FirstOrDefault(c => c.random == mcblk.random);
                            if (miner == null)
                            {
                                continue;
                            }

                            // Total power
                            BigFloat diffsum = new BigFloat(0);
                            foreach (var dic in miners.Values)
                            {
                                if (string.IsNullOrEmpty(dic.address))
                                {
                                    continue;
                                }
                                if (dic.diff < 0.99999f)
                                {
                                    continue;
                                }
                                diffsum += new BigFloat(dic.diff);
                            }

                            // Reward for participation
                            foreach (var dic in miners.Values)
                            {
                                if (string.IsNullOrEmpty(dic.address))
                                {
                                    continue;
                                }
                                if (dic.diff < 0.99999f)
                                {
                                    continue;
                                }

                                var    v   = new BigFloat(dic.diff);
                                string pay = BigHelper.Round8((v * reward / diffsum).ToString());

                                if (minerTransfer.TryGetValue(dic.address, out BlockSub transfer))
                                {
                                    transfer.amount = BigHelper.Add(transfer.amount, pay);
                                }
                                else
                                if (BigHelper.Greater(pay, "0.002", false))
                                {
                                    transfer            = new BlockSub();
                                    transfer.addressIn  = ownerAddress;
                                    transfer.addressOut = dic.address;
                                    transfer.amount     = BigHelper.Sub(pay, "0.002"); // 扣除交易手续费
                                    transfer.type       = "transfer";
                                    transfer.data       = CryptoHelper.Sha256($"{today}_{maxHeight}_{ownerAddress}_{dic.address}_MinerReward");
                                    minerTransfer.Add(transfer.addressOut, transfer);
                                }
                            }
                        }
                    }
                }
            }
            return(minerTransfer);
        }