private void StorageForm_Load(object sender, EventArgs e) { dataGridView1.Rows.Clear(); var storage = _debugger.currentAccount.storage; foreach (var entry in storage.entries) { // TODO : Proper type detection? Emulator.Type hintType = entry.Key.Length == 20 ? Emulator.Type.Integer : Emulator.Type.Integer; dataGridView1.Rows.Add(FormattingUtils.OutputData(entry.Key, false), FormattingUtils.OutputData(entry.Value, false, hintType)); } }
private void AddLine(SyntaxNode node, string name, Emulator.Type type) { var position = node.SpanStart; var text = node.GetText(); var loc = node.GetLocation(); var lineSpan = loc.SourceTree.GetLineSpan(loc.SourceSpan); var line = lineSpan.StartLinePosition.Line + 1; assignements[line] = new Assignment() { name = name, type = type }; }
public static string ConvertType(Emulator.Type type) { switch (type) { case Emulator.Type.Boolean: return("bool"); case Emulator.Type.String: return("string"); case Emulator.Type.Integer: return("BigInteger"); case Emulator.Type.ByteArray: return("byte[]"); case Emulator.Type.Array: return("object[]"); default: throw new ArgumentException("Invalid type: " + type); } }
private void StorageForm_Load(object sender, EventArgs e) { dataGridView1.Rows.Clear(); var storage = _debugger.currentAccount.storage; foreach (var entry in storage.entries) { // TODO : Proper type detection? Emulator.Type hintType = entry.Key.Length == 20 ? Emulator.Type.Integer : Emulator.Type.Integer; string key = null; if (entry.Key.Length == 24) { var prefixData = entry.Key.Take(4).ToArray(); var hash = entry.Key.Skip(4).ToArray(); //https://stackoverflow.com/questions/38199136/check-if-c-sharp-byte-array-contains-a-string var isAscii = prefixData.All(b => b >= 32 && b <= 127); if (isAscii) { var prefix = System.Text.Encoding.ASCII.GetString(prefixData); var signatureHash = new UInt160(hash); key = prefix + "." + CryptoUtils.ToAddress(signatureHash); } } if (key == null) { key = FormattingUtils.OutputData(entry.Key, false); } dataGridView1.Rows.Add(key, FormattingUtils.OutputData(entry.Value, false, hintType)); } }
public byte[] GenerateLoaderScriptFromInputs(DataNode inputs, ABI abi) { var methodName = abi != null && abi.entryPoint != null ? abi.entryPoint.name : null; using (ScriptBuilder sb = new ScriptBuilder()) { var items = new Stack <object>(); if (inputs != null) { AVMFunction method = methodName != null && abi.functions.ContainsKey(methodName) ? abi.functions[methodName] : null; int index = 0; foreach (var item in inputs.Children) { Emulator.Type hint = method != null ? method.inputs[index].type : Emulator.Type.Unknown; var obj = Emulator.ConvertArgument(item, hint); items.Push(obj); index++; } } while (items.Count > 0) { var item = items.Pop(); NeoAPI.EmitObject(sb, item); } var loaderScript = sb.ToArray(); //System.IO.File.WriteAllBytes("loader.avm", loaderScript); return(loaderScript); } }
public static object ConvertArgument(DataNode item, Emulator.Type hintType = Emulator.Type.Unknown) { if (item.HasChildren) { bool isByteArray = true; foreach (var child in item.Children) { byte n; if (string.IsNullOrEmpty(child.Value) || !byte.TryParse(child.Value, out n)) { isByteArray = false; break; } } if (hintType == Type.Array) { isByteArray = false; } if (isByteArray) { var arr = new byte[item.ChildCount]; int index = arr.Length; foreach (var child in item.Children) { index--; arr[index] = byte.Parse(child.Value); } return(arr); } else { var list = new List <object>(); for (int i = 0; i < item.ChildCount; i++) { var child = item.GetNodeByIndex(i); list.Add(ConvertArgument(child)); } return(list); } } BigInteger intVal; if (item.Kind == NodeKind.Numeric) { if (BigInteger.TryParse(item.Value, out intVal)) { return(intVal); } else { return(0); } } else if (item.Kind == NodeKind.Boolean) { return("true".Equals(item.Value.ToLowerInvariant()) ? true : false); } else if (item.Kind == NodeKind.Null) { return(null); } else if (item.Value == null) { return(null); } else if (item.Value.StartsWith("0x")) { return(item.Value.Substring(2).HexToByte()); } else { return(item.Value); } }
public static string OutputData(byte[] data, bool addQuotes, Emulator.Type hintType = Emulator.Type.Unknown) { if (data == null) { return("Null"); } byte[] separator = { Convert.ToByte(';') }; int dataLen = data.Length; if (dataLen > 5 && /* {a:...;} */ (char)data[0] == '{' && (char)data[dataLen - 1] == '}') // Binary NeoStorageKey { byte[][] parts = ByteArraySplit(data.SubArray(1, dataLen - 1), separator); Debug.WriteLine("parts.len " + parts.Length.ToString()); string keyString = "{"; foreach (byte[] part in parts) { Debug.WriteLine("part.len " + part.Length.ToString()); if (part.Length >= 4) { byte fieldCode = part[0]; byte fieldType = part[2]; byte[] fieldValueAsBytes = part.SubArray(4, part.Length - 4); string fieldValueAsString = System.Text.Encoding.ASCII.GetString(fieldValueAsBytes); Debug.WriteLine("fieldCode " + ((char)fieldCode).ToString() + " fieldType " + ((int)fieldType).ToString()); Debug.WriteLine("fieldValue " + fieldValueAsBytes.ByteToHex() + " '" + fieldValueAsString + "'"); switch ((char)fieldCode) { case '#': // signature and flags { keyString += "#:" + ((int)fieldType).ToString() + "=" + fieldValueAsBytes.ByteToHex(); break; } case 'a': // app name { keyString += "a:" + ((int)fieldType).ToString() + "=" + fieldValueAsString; break; } case 'M': // app major version { BigInteger fieldValueAsBigInteger = new BigInteger(fieldValueAsBytes); keyString += "M:" + ((int)fieldType).ToString() + "=" + fieldValueAsBigInteger.ToString(); break; } case 'm': // app minor version { BigInteger fieldValueAsBigInteger = new BigInteger(fieldValueAsBytes); keyString += "m:" + ((int)fieldType).ToString() + "=" + fieldValueAsBigInteger.ToString(); break; } case 'b': // app build number { BigInteger fieldValueAsBigInteger = new BigInteger(fieldValueAsBytes); keyString += "b:" + ((int)fieldType).ToString() + "=" + fieldValueAsBigInteger.ToString(); break; } case 'u': // userScriptHash (integer or binary) { switch (fieldType) { case (int)ContractParameterTypeLocal.Integer: { BigInteger fieldValueAsBigInteger = new BigInteger(fieldValueAsBytes); keyString += "u:" + ((int)fieldType).ToString() + "=" + fieldValueAsBigInteger.ToString(); break; } case (int)ContractParameterTypeLocal.String: { keyString += "u:" + ((int)fieldType).ToString() + "=" + fieldValueAsString; break; } case (int)ContractParameterTypeLocal.ByteArray: { keyString += "u:" + ((int)fieldType).ToString() + "=" + fieldValueAsBytes.ByteToHex(); break; } default: { keyString += "u?:" + ((int)fieldType).ToString() + "=" + fieldValueAsBytes.ByteToHex(); break; } } break; } case 'c': // class name { keyString += "c:" + ((int)fieldType).ToString() + "=" + fieldValueAsString; break; } case 'i': // index { BigInteger fieldValueAsBigInteger = new BigInteger(fieldValueAsBytes); keyString += "i:" + ((int)fieldType).ToString() + "=" + fieldValueAsBigInteger.ToString(); break; } case 'f': // field name { keyString += "c:" + ((int)fieldType).ToString() + "=" + fieldValueAsString; break; } default: { keyString += ((char)fieldCode).ToString() + "?:" + ((int)fieldType).ToString() + "=" + fieldValueAsBytes.ByteToHex(); break; } } keyString += ";"; } } keyString += "}"; return(keyString); } else { if (hintType != Emulator.Type.Unknown) { switch (hintType) { case Emulator.Type.String: { return(System.Text.Encoding.UTF8.GetString(data)); } case Emulator.Type.Boolean: { return((data != null && data.Length > 0 && data[0] != 0) ? "True" : "False"); } case Emulator.Type.Integer: { return(new BigInteger(data).ToString()); } } } for (int i = 0; i < dataLen; i++) { var c = (char)data[i]; var isValidText = char.IsLetterOrDigit(c) || char.IsPunctuation(c) || char.IsWhiteSpace(c) || "!@#$%^&*()-=_+[]{}|;':,./<>?".Contains(c.ToString()); if (!isValidText) { if (data.Length == 20) { var signatureHash = new UInt160(data); return(Crypto.Default.ToAddress(signatureHash)); } return(data.ByteToHex()); } } } var result = System.Text.Encoding.ASCII.GetString(data); if (addQuotes) { result = '"' + result + '"'; } return(result); }
public static string StackItemAsString(StackItem item, bool addQuotes = false, Emulator.Type hintType = Emulator.Type.Unknown) { if (item is ICollection) { var bytes = item.GetByteArray(); if (bytes != null && bytes.Length == 20) { var signatureHash = new UInt160(bytes); return(Crypto.Default.ToAddress(signatureHash)); } var s = new StringBuilder(); var items = (ICollection)item; s.Append('['); int i = 0; foreach (StackItem element in items) { if (i > 0) { s.Append(','); } s.Append(StackItemAsString(element)); i++; } s.Append(']'); return(s.ToString()); } if (item is Neo.VM.Types.Boolean && hintType == Emulator.Type.Unknown) { return(item.GetBoolean().ToString()); } if (item is Neo.VM.Types.Integer && hintType == Emulator.Type.Unknown) { return(item.GetBigInteger().ToString()); } if (item is Neo.VM.Types.InteropInterface) { return("{InteropInterface}"); } byte[] data = null; try { data = item.GetByteArray(); } catch { } if ((data == null || data.Length == 0) && hintType == Emulator.Type.Unknown) { return("Null"); } return(FormattingUtils.OutputData(data, addQuotes, hintType)); }
public static string StackItemAsString(StackItem item, bool addQuotes = false, Emulator.Type hintType = Emulator.Type.Unknown) { if (item is ICollection && !(item is Map)) { var bytes = item.GetByteArray(); if (bytes != null && bytes.Length == 20) { var signatureHash = new UInt160(bytes); return(CryptoUtils.ToAddress(signatureHash)); } var s = new StringBuilder(); var items = (ICollection)item; s.Append('['); int i = 0; foreach (StackItem element in items) { if (i > 0) { s.Append(','); } s.Append(StackItemAsString(element)); i++; } s.Append(']'); return(s.ToString()); } else if (item is Map) { Map mapItem = (Map)item; var builder = new StringBuilder(); builder.Append("{\n"); foreach (var key in mapItem.Keys) { builder.Append(StackItemAsString(key)); builder.Append(" : "); builder.Append(StackItemAsString(mapItem[key])); builder.Append("\n"); } builder.Append("}"); return(builder.ToString()); } if (item is Neo.VM.Types.Boolean && hintType == Emulator.Type.Unknown) { return(item.GetBoolean().ToString()); } if (item is Neo.VM.Types.Integer && hintType == Emulator.Type.Unknown) { return(item.GetBigInteger().ToString()); } if (item is Neo.VM.Types.InteropInterface) { return("{InteropInterface}"); } byte[] data = null; try { data = item.GetByteArray(); } catch { } if ((data == null || data.Length == 0) && hintType == Emulator.Type.Unknown) { return("Null"); } if (hintType == Emulator.Type.Array) { var s = new StringBuilder(); s.Append('['); int count = 0; if (data.Length > 0) { var array = (VM.Types.Array)item; foreach (var entry in array) { if (count > 0) { s.Append(", "); } count++; s.Append(StackItemAsString(entry, addQuotes, Emulator.Type.Unknown)); } } s.Append(']'); return(s.ToString()); } return(FormattingUtils.OutputData(data, addQuotes, hintType)); }
public static string OutputData(byte[] data, bool addQuotes, Emulator.Type hintType = Emulator.Type.Unknown) { if (data == null) { return("Null"); } var dataLen = data.Length; if (hintType != Emulator.Type.Unknown) { switch (hintType) { case Emulator.Type.String: { var val = System.Text.Encoding.UTF8.GetString(data); if (addQuotes) { val = '"' + val + '"'; } return(val); } case Emulator.Type.Boolean: { return((data != null && data.Length > 0 && data[0] != 0) ? "True" : "False"); } case Emulator.Type.Integer: { return(new BigInteger(data).ToString()); } } } for (int i = 0; i < dataLen; i++) { var c = (char)data[i]; var isValidText = char.IsLetterOrDigit(c) || char.IsPunctuation(c) || char.IsWhiteSpace(c) || "!@#$%^&*()-=_+[]{}|;':,./<>?".Contains(c.ToString()); if (!isValidText) { if (data.Length == 20) { var signatureHash = new UInt160(data); return(CryptoUtils.ToAddress(signatureHash)); } return(data.ByteToHex()); } } var result = System.Text.Encoding.ASCII.GetString(data); if (addQuotes) { result = '"' + result + '"'; } return(result); }