public static byte[] Base58CheckDecode(this string input) { byte[] buffer = Base58.Decode(input); if (buffer.Length < 4) { throw new FormatException(); } byte[] checksum = buffer.Sha256(0, buffer.Length - 4).Sha256(); if (!buffer.Skip(buffer.Length - 4).SequenceEqual(checksum.Take(4))) { throw new FormatException(); } var ret = buffer.Take(buffer.Length - 4).ToArray(); Array.Clear(buffer, 0, buffer.Length); return(ret); }
public TValue Get(string key) { TValue currValue = default(TValue); if (currDic.TryGetValue(key, out currValue)) { return(currValue); } string value = db.Get($"{prefix}___{key}", options); if (value != null) { return(Deserialize(Base58.Decode(value))); } return(default(TValue)); }
public void GetAccounts(HttpMessage httpMessage) { var buffer = Base58.Decode(httpMessage.map["List"]).ToStr(); var list = JsonHelper.FromJson <List <string> >(buffer); using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0)) { var accounts = new Dictionary <string, Account>(); for (int i = 0; i < list.Count; i++) { Account account = dbSnapshot.Accounts.Get(list[i]); if (account == null) { account = new Account() { address = list[i], amount = "0", index = 0, notice = 0 }; } accounts.Remove(account.address); accounts.Add(account.address, account); } httpMessage.result = JsonHelper.ToJson(accounts); } }
public void Execute(DbSnapshot dbSnapshot, Transfer transfer, long height) { LuaVMCall luaVMCall = new LuaVMCall(); LuaVMScript luaVMScript = null; LuaVMContext LuaVMContext = null; try { s_dbSnapshot = dbSnapshot; string address = (transfer.addressOut == null || transfer.addressOut == "") ? Wallet.ToAddress(CryptoHelper.Sha256(Encoding.UTF8.GetBytes(transfer.data))) : transfer.addressOut; LuaEnv luaenv = GetLuaEnv(address); // rapidjson待优化,改为直接在C#层调用 luaenv.Global.Set("contractAddress", address); luaenv.Global.Set("dbSnapshot", dbSnapshot); luaenv.DoString(initScript); if (transfer.addressOut == null || transfer.addressOut == "") { // 当前版本只能由superAddress发布智能合约 if (consensus.superAddress != transfer.addressIn) { return; } luaVMScript = new LuaVMScript() { script = Base58.Decode(transfer.data) }; LuaVMContext = new LuaVMContext() { jsonData = "{}".ToByteArray() }; luaVMCall.fnName = "Create"; luaVMCall.args = new FieldParam[0]; if (dbSnapshot.Contracts.Get(address) == null) { dbSnapshot.Contracts.Add(address, luaVMScript); } luaenv.DoString(luaVMScript.script); } else { luaVMScript = dbSnapshot.Contracts.Get(address); LuaVMContext = dbSnapshot.Storages.Get(address); luaVMCall = LuaVMCall.Decode(transfer.data); luaenv.DoString(luaVMScript.script, transfer.addressOut); luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n"); } object[] args = luaVMCall.args.Select(a => a.GetValue()).ToArray(); LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName); luaenv.DoString($"curHeight = {height}\n"); luaenv.DoString($"curAddress = '{transfer.addressIn}'\n"); luaFun.Call(args); // 待优化,改为直接在C#层调用 luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n"); LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray(); dbSnapshot.Storages.Add(address, LuaVMContext); } catch (Exception) { Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}"); } }