예제 #1
0
        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(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 == Emulator.Type.Unknown)
            {
                return(item.GetBoolean().ToString());
            }

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

            if (item is 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));
        }
예제 #2
0
        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);
        }