public string ToJsonHex() { var data = "['" + Nonce.ToHex() + "','" + GasPrice.ToHex() + "','" + GasLimit.ToHex() + "','" + ReceiveAddress.ToHex() + "','" + Value.ToHex() + "','" + ToHex(Data) + "','" + ChainId.ToHex() + "','" + RHash.ToHex() + "','" + SHash.ToHex() + "'"; if (Signature != null) { data = data + ", '" + Signature.V.ToHex() + "', '" + Signature.R.ToHex() + "', '" + Signature.S.ToHex() + "'"; } return(data + "]"); }
public static SValue Unserialize(byte[] bytes) { if (bytes == null) { return(new SValue()); } using (MemoryStream stream = new MemoryStream(bytes)) using (BinaryReader reader = new BinaryReader(stream)) { try { if (reader.ReadByte() != 'D' || reader.ReadByte() != 'S' || reader.ReadByte() != 'V') { return(new SValue()); } int bytesLength = bytes.Length; int length; length = reader.ReadInt32(); if (length > bytesLength) { return(new SValue()); } string[] texts = new string[length]; length = reader.ReadInt32(); if (length > bytesLength) { return(new SValue()); } SList[] lists = new SList[length]; length = reader.ReadInt32(); if (length > bytesLength) { return(new SValue()); } SHash[] hashes = new SHash[length]; for (int i = 0; i < lists.Length; i++) { lists[i] = new SList(); } for (int i = 0; i < hashes.Length; i++) { hashes[i] = new SHash(); } for (int i = 0; i < texts.Length; i++) { texts[i] = reader.ReadString(); } for (int i = 0; i < lists.Length; i++) { SList list = lists[i]; int count = reader.ReadInt32(); for (int j = 0; j < count; j++) { list.Add(ReadPrimitive(lists, hashes, texts, reader)); } } for (int i = 0; i < hashes.Length; i++) { SHash hash = hashes[i]; int count = reader.ReadInt32(); for (int j = 0; j < count; j++) { string key; if (reader.ReadBoolean()) { key = texts[reader.ReadInt32()]; } else { key = reader.ReadString(); } hash[key] = ReadPrimitive(lists, hashes, texts, reader); } } return(ReadPrimitive(lists, hashes, texts, reader)); } catch { return(new SValue()); } } }
private static void FillComplexValues( Dictionary <SList, int> listSet, Dictionary <SHash, int> hashSet, Dictionary <string, int> textSet, SValue value, ref int listIndex, ref int hashIndex, ref int textIndex) { switch (value.type) { case TypeList: SList list = value.value as SList; if (listSet.ContainsKey(list)) { return; } listSet[list] = listIndex++; for (int i = 0, count = list.Count; i < count; i++) { FillComplexValues(listSet, hashSet, textSet, list[i], ref listIndex, ref hashIndex, ref textIndex); } break; case TypeHash: SHash hash = value.value as SHash; if (hashSet.ContainsKey(hash)) { return; } hashSet[hash] = hashIndex++; foreach (KeyValuePair <string, SValue> pair in hash) { string key = pair.Key; if (IsStoredText(key)) { int textsCount; if (!textSet.TryGetValue(key, out textsCount)) { textSet[key] = -1; } else if (textsCount == -1) { textSet[key] = textIndex++; } } FillComplexValues(listSet, hashSet, textSet, pair.Value, ref listIndex, ref hashIndex, ref textIndex); } break; case TypeString: { string key = (value.value as string) ?? ""; int textsCount; if (!textSet.TryGetValue(key, out textsCount)) { textSet[key] = -1; } else if (textsCount == -1) { textSet[key] = textIndex++; } break; } } }
public static byte[] Serialize(SValue value) { using (MemoryStream stream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { Dictionary <SList, int> listSet = new Dictionary <SList, int>(); Dictionary <SHash, int> hashSet = new Dictionary <SHash, int>(); Dictionary <string, int> textSet = new Dictionary <string, int>(); int listIndex = 0; int hashIndex = 0; int textIndex = 0; FillComplexValues(listSet, hashSet, textSet, value, ref listIndex, ref hashIndex, ref textIndex); SList[] lists = new SList[listIndex]; SHash[] hashes = new SHash[hashIndex]; string[] texts = new string[textIndex]; foreach (KeyValuePair <SList, int> pair in listSet) { lists[pair.Value] = pair.Key; } foreach (KeyValuePair <SHash, int> pair in hashSet) { hashes[pair.Value] = pair.Key; } foreach (KeyValuePair <string, int> pair in textSet) { if (pair.Value != -1) { texts[pair.Value] = pair.Key; } } writer.Write((byte)'D'); writer.Write((byte)'S'); writer.Write((byte)'V'); writer.Write(texts.Length); writer.Write(lists.Length); writer.Write(hashes.Length); for (int i = 0; i < texts.Length; i++) { writer.Write(texts[i]); } for (int i = 0; i < lists.Length; i++) { SList list = lists[i]; writer.Write(list.Count); for (int j = 0, count = list.Count; j < count; j++) { SerializePrimitive(listSet, hashSet, textSet, writer, list[j]); } } for (int i = 0; i < hashes.Length; i++) { SHash hash = hashes[i]; writer.Write(hash.Count); foreach (KeyValuePair <string, SValue> pair in hash) { int textIndexI; if (IsStoredText(pair.Key) && textSet.TryGetValue(pair.Key, out textIndexI) && textIndexI != -1) { writer.Write(true); writer.Write(textIndexI); } else { writer.Write(false); writer.Write(pair.Key); } SerializePrimitive(listSet, hashSet, textSet, writer, pair.Value); } } SerializePrimitive(listSet, hashSet, textSet, writer, value); return(stream.ToArray()); } }