Ejemplo n.º 1
0
        private void OnGetNonce(HttpMessage httpMessage)
        {
            string        address = httpMessage.map["address"];
            List <string> addr    = new List <string>();

            addr.Add(address);
            List <string> list = new List <string>();

            list.Add(Base58.Encode(System.Text.Encoding.UTF8.GetBytes("" + JsonHelper.ToJson(addr))));
            httpMessage.map.Clear();
            httpMessage.map.Add("List", list[0]);//JsonHelper.ToJson(list);

            var buffer = Base58.Decode(httpMessage.map["List"]).ToStr();
            var list2  = JsonHelper.FromJson <List <string> >(buffer);

            using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                Account account = dbSnapshot.Accounts.Get(list2[0]);
                if (account != null)
                {
                    httpMessage.result = $"{{\"success\":true,\"message\":\"successful operation\",\"nonce\":{account.nonce}}}";
                }
                else
                {
                    httpMessage.result = $"{{\"success\":true,\"message\":\"successful operation\",\"nonce\":{0}}}";
                }
            }
        }
Ejemplo n.º 2
0
        static Account GetAccount(string url, string address)
        {
            try
            {
                if (httpPoolRelay == null)
                {
                    using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
                    {
                        return(dbSnapshot.Accounts.Get(address));
                    }
                }

                var list = new List <string>();
                list.Add(address);

                HttpMessage quest = new HttpMessage();
                quest.map = new Dictionary <string, string>();
                quest.map.Add("cmd", "getaccounts");
                quest.map.Add("List", Base58.Encode(JsonHelper.ToJson(list).ToByteArray()));

                var result   = ComponentNetworkHttp.QueryStringSync($"http://{url}", quest);
                var accounts = JsonHelper.FromJson <Dictionary <string, Account> >(result);
                accounts.TryGetValue(address, out Account acc);

                return(acc);
            }
            catch (Exception)
            {
            }
            return(null);
        }
Ejemplo n.º 3
0
        public static string Base58CheckEncodeOld(this byte[] data)
        {
            byte[] checksum = data.Sha256().Sha256();
            byte[] buffer   = new byte[data.Length + 4];
            Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
            Buffer.BlockCopy(checksum, 0, buffer, data.Length, 4);
            var ret = Base58.Encode(buffer);

            Array.Clear(buffer, 0, buffer.Length);
            return(ret);
        }
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 Add(string key, TValue value)
        {
            Type type = value.GetType();

            // 数据回退
            string old = db.Get($"{prefix}___{key}", options);

            if (undos != null)
            {
                batch?.Put($"{prefix}___{key}_undo_{undos.height}", old ?? "");
                undos.keys.Add($"{prefix}___{key}");
            }
            currDic.Remove(key);
            currDic.Add(key, value);

            batch?.Put($"{prefix}___{key}", Base58.Encode(Serialize(value)));
        }
Ejemplo n.º 6
0
        public static string Base58CheckEncode(this byte[] data)
        {
            string hash1 = CryptoHelper.Sha256(data.ToHexString());

            byte[] checksum = CryptoHelper.Sha256(hash1).HexToBytes();
            byte[] buffer   = new byte[data.Length + 4];
            Buffer.BlockCopy(data, 0, buffer, 0, data.Length);
            Buffer.BlockCopy(checksum, 0, buffer, data.Length, 4);
            var ret = Base58.Encode(buffer);

            //Log.Info("Base58CheckEncodedata " + data.ToHexString() );
            //Log.Info("Base58CheckEncode1 " + hash1);
            //Log.Info("Base58CheckEncode2 " + CryptoHelper.Sha256(hash1));
            //Log.Info("Base58CheckEncode3 " + buffer.ToHexString() );
            //Log.Info("Base58CheckEncoderet " + ret );

            Array.Clear(buffer, 0, buffer.Length);
            return(ret);
        }
Ejemplo n.º 7
0
        public override void Start()
        {
            rule = Entity.Root.GetComponent <Rule>();
            ComponentNetMsg componentNetMsg = Entity.Root.GetComponent <ComponentNetMsg>();

            componentNetMsg.registerMsg(NetOpcode.P2P_NewBlock, P2P_NewBlock_Handle);

            string genesisText = File.ReadAllText("./Data/genesisBlock.dat");
            Block  blk         = JsonHelper.FromJson <Block>(genesisText);

            superAddress = blk.Address;

            long.TryParse(Entity.Root.GetComponent <LevelDBStore>().Get("UndoHeight"), out long UndoHeight);
            if (UndoHeight == 0)
            {
                if (true)
                {
                    blockMgr.AddBlock(blk);
                    ApplyGenesis(blk);
                }
            }

            string consData = Base58.Encode(FileHelper.GetFileData("./Data/Contract/RuleContract_v1.0.lua").ToByteArray());

            consAddress = Wallet.ToAddress(CryptoHelper.Sha256(Encoding.UTF8.GetBytes(consData)));

            using (DbSnapshot snapshot = levelDBStore.GetSnapshot())
            {
                LuaVMScript luaVMScript = new LuaVMScript()
                {
                    script = FileHelper.GetFileData("./Data/Contract/RuleContract_curr.lua").ToByteArray()
                };
                snapshot.Contracts.Add(consAddress, luaVMScript);
                snapshot.Commit();
            }
            ruleInfos = luaVMEnv.GetRules(consAddress, UndoHeight);

            if (bRun)
            {
                Run();
            }
        }