/// <summary> /// Creates a serialized string used in script signature. /// </summary> /// <remarks> /// </remarks> /// <example> /// python: http://runnable.com/U3Mya-5oZntF5Ira/bitcoin-coinbase-serialize-string-python /// nodejs: https://github.com/zone117x/node-stratum-pool/blob/dfad9e58c661174894d4ab625455bb5b7428881c/lib/util.js#L153 /// </example> /// <param name="input"></param> /// <returns></returns> public static byte[] SerializeString(string input) { if (input.Length < 253) { return(ArrayHelpers.Combine(new[] { (byte)input.Length }, Encoding.UTF8.GetBytes(input))); } // if input string is >=253, we need need a special format. byte[] result; using (var stream = new MemoryStream()) { if (input.Length < 0x10000) { stream.WriteValueU8(253); stream.WriteValueU16(((UInt16)input.Length).LittleEndian()); // write packed length. } else if ((long)input.Length < 0x100000000) { stream.WriteValueU8(254); stream.WriteValueU32(((UInt32)input.Length).LittleEndian()); // write packed length. } else { stream.WriteValueU8(255); stream.WriteValueU16(((UInt16)input.Length).LittleEndian()); // write packed length. } stream.WriteString(input); result = stream.ToArray(); } return(result); }