示例#1
0
        public static string StackItemAsString(StackItem item, bool addQuotes = false, VMType hintType = VMType.Unknown)
        {
            if (item is ICollection)
            {
                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());
            }

            if (item is VM.Types.Boolean && hintType == VMType.Unknown)
            {
                return(item.GetBoolean().ToString());
            }

            if (item is VM.Types.Integer && hintType == VMType.Unknown)
            {
                return(item.GetBigInteger().ToString());
            }

            if (item is VM.Types.InteropInterface)
            {
                var type = ((VM.Types.InteropInterface)item).GetInterfaceType();
                return($"{type.Name}");
            }

            byte[] data = null;

            try
            {
                data = item.GetByteArray();
            }
            catch
            {
            }

            if ((data == null || data.Length == 0) && hintType == VMType.Unknown)
            {
                return("Null");
            }

            if (hintType == VMType.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, VMType.Unknown));
                    }
                }
                s.Append(']');

                return(s.ToString());
            }

            return(FormattingUtils.OutputData(data, addQuotes, hintType));
        }
示例#2
0
        public static string OutputData(byte[] data, bool addQuotes, VMType hintType = VMType.Unknown)
        {
            if (data == null)
            {
                return("Null");
            }

            var dataLen = data.Length;

            if (hintType != VMType.Unknown)
            {
                switch (hintType)
                {
                case VMType.String:
                {
                    var val = System.Text.Encoding.UTF8.GetString(data);
                    if (addQuotes)
                    {
                        val = '"' + val + '"';
                    }
                    return(val);
                }

                case VMType.Boolean:
                {
                    return((data != null && data.Length > 0 && data[0] != 0) ? "True" : "False");
                }

                case VMType.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)
                {
                    string prefix = null;

                    if (data.Length == 23)
                    {
                        prefix = Encoding.ASCII.GetString(data.Take(3).ToArray());
                        data   = data.Skip(3).ToArray();
                    }

                    if (data.Length == 20)
                    {
                        var signatureHash = new UInt160(data);
                        var output        = CryptoUtils.ToAddress(signatureHash);
                        if (prefix != null)
                        {
                            output = prefix + "." + output;
                        }
                        return(output);
                    }

                    return(data.ByteToHex());
                }
            }

            var result = System.Text.Encoding.ASCII.GetString(data);

            if (addQuotes)
            {
                result = '"' + result + '"';
            }

            return(result);
        }