Exemplo n.º 1
0
        public void StringEvenLengthRoundTripTest(
            [RandomStrings(Count = 10, Pattern = "([0-9a-fA-F][0-9a-fA-F]){1,16}")] string byteString,
            [EnumData(typeof(Endianness))] Endianness endianness)
        {
            var bytes     = BufferOperations.FromByteString(byteString, endianness);
            var roundTrip = BufferOperations.ToByteString(bytes, endianness);

            Assert.AreEqual(byteString, roundTrip, StringComparison.OrdinalIgnoreCase);
        }
Exemplo n.º 2
0
        public static Hash256 Parse(string hashString)
        {
            ContractsCommon.NotNull(hashString, "hashString");
            Contract.Requires <FormatException>(hashString.Trim().Length == HASH_LEN * 2, "Hash string not of expected length.");
            ContractsCommon.ResultIsNonNull <Hash256>();

            var bytes = BufferOperations.FromByteString(hashString.Trim(), Endianness.LittleEndian);

            return(new Hash256(bytes));
        }
Exemplo n.º 3
0
 public static byte[] ConvertStringToByteArray(string s)
 {
     if (s.StartsWith("0x"))
     {
         return(BufferOperations.FromByteString(s.Substring(2), Endianness.BigEndian));
     }
     else
     {
         return(BufferOperations.FromByteString(s, Endianness.LittleEndian));
     }
 }
Exemplo n.º 4
0
        public static Script Parse(string scriptString)
        {
            var           atoms    = scriptString.Split(' ');
            var           script   = new Script();
            StringBuilder valueStr = null;

            foreach (var atom in atoms)
            {
                IScriptAtom newAtom = null;
                if (valueStr != null)
                {
                    valueStr.Append(" ");
                    if (atom.EndsWith("'"))
                    {
                        if (atom.EndsWith(@"\''"))
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 3));
                            valueStr.Append("'");
                            var valueBytes = Encoding.ASCII.GetBytes(valueStr.ToString());
                            newAtom = new ValueAtom(valueBytes);
                            script.Atoms.Add(newAtom);
                            valueStr = null;
                        }
                        else if (atom.EndsWith(@"\'"))
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 2));
                            valueStr.Append("'");
                        }
                        else
                        {
                            valueStr.Append(atom.Substring(0, atom.Length - 1));
                            var valueBytes = Encoding.ASCII.GetBytes(valueStr.ToString());
                            newAtom = new ValueAtom(valueBytes);
                            script.Atoms.Add(newAtom);
                            valueStr = null;
                        }
                    }
                    else
                    {
                        valueStr.Append(atom);
                    }
                    continue;
                }
                if (atom.StartsWith("OP_"))
                {
                    var opcode = (ScriptOpCode)Enum.Parse(typeof(ScriptOpCode), atom, false);
                    newAtom = ScriptAtomFactory.GetOpAtom(opcode);
                }
                else if (atom.StartsWith("'"))
                {
                    if (atom.EndsWith("'"))
                    {
                        if (atom.EndsWith(@"\''"))
                        {
                            var valueBytes = Encoding.ASCII.GetBytes(atom.Substring(1, atom.Length - 4) + "'");
                            newAtom = new ValueAtom(valueBytes);
                        }
                        else if (atom.EndsWith(@"\'"))
                        {
                            valueStr = new StringBuilder();
                            valueStr.Append(atom.Substring(1, atom.Length - 3));
                            valueStr.Append("'");
                        }
                        else if (atom.Length > 1)
                        {
                            if (atom.Length > 2)
                            {
                                var valueBytes = Encoding.ASCII.GetBytes(atom.Substring(1, atom.Length - 2));
                                newAtom = new ValueAtom(valueBytes);
                            }
                        }
                        else
                        {
                            valueStr = new StringBuilder();
                        }
                    }
                    else
                    {
                        valueStr = new StringBuilder();
                        valueStr.Append(atom.Substring(1, atom.Length - 1));
                    }
                }
                else if (atom.Length > 0)
                {
                    var valueBytes = BufferOperations.FromByteString(atom, Endianness.BigEndian);
                    newAtom = new ValueAtom(valueBytes);
                }
                if (newAtom != null)
                {
                    script.Atoms.Add(newAtom);
                }
            }
            if (valueStr != null)
            {
                throw new FormatException("Unclosed script string found");
            }
            script.Freeze();
            return(script);
        }