public void Serialize(Serializer so, string val, bool noMarker = false) { var bytes = Core.Config.GetB58IdentiferCodecs().DecodeAddress(val); SerializedTypeHelper.SerializeVarint(so, (uint)bytes.Length); so.Append(bytes); }
public void Serialize(Serializer so, object val, bool noMarker = false) { var properties = val.GetType().GetProperties() .Where(p => p.Name != p.Name.ToLower()) .ToArray(); var invalid = properties.FirstOrDefault(p => !SerializedTypeHelper.InverseFieldsMap.ContainsKey(p.Name)); if (invalid != null) { throw new InvalidOperationException("JSON contains unknown field: '" + invalid.Name + "'"); } properties = SerializedTypeHelper.SortProperties(properties); foreach (var property in properties) { var pval = property.GetValue(val); if (pval == null) { continue; } SerializedTypeHelper.Serialize(so, property.Name, pval); } if (!noMarker) { //Object ending marker SerializedTypeHelper.STInt8.Serialize(so, 0xe1); } }
void ISerializedType.Serialize(Serializer so, object val, bool noMarker) { ulong?number = Utils.TryGetNumber <ulong>(val, false); if (number.HasValue) { Serialize(so, number.Value, noMarker); return; } if (val is string str) { if (!Utils.IsHexString(str)) { throw new ArgumentException("Invalid hex string."); } if (str.Length > 16) { throw new ArgumentException("Int64 is too large."); } while (str.Length < 16) { str = "0" + str; } SerializedTypeHelper.SerializeHex(so, str, true); return; } throw new ArgumentException("Invalid type for Int64."); }
public void Serialize(Serializer so, IEnumerable <string> val, bool noMarker = false) { var array = val.ToArray(); SerializedTypeHelper.SerializeVarint(so, (uint)array.Length * 32); foreach (var item in array) { SerializedTypeHelper.STHash256.Serialize(so, item); } }
public void Serialize(Serializer so, string val, bool noMarker = false) { if (val != null && Regex.IsMatch(val, "^[0-9A-F]{0,64}$", RegexOptions.IgnoreCase) && val.Length <= 64) { SerializedTypeHelper.SerializeHex(so, val, true); return; } throw new ArgumentException("Invalid Hash256."); }
public void Serialize(Serializer so, IEnumerable val, bool noMarker = false) { foreach (var item in val) { if (item == null || item.GetType().IsValueType) { throw new ArgumentException("Cannot serialize an array containing null or value type object."); } var properties = item.GetType().GetProperties(); if (properties.Length != 1) { throw new ArgumentException("Cannot serialize an array containing non-single-key objects"); } var property = properties.First(); SerializedTypeHelper.Serialize(so, property.Name, property.GetValue(item)); } //Array ending marker SerializedTypeHelper.STInt8.Serialize(so, 0xf1); }
public void Serialize(Serializer so, string val, bool noMarker = false) { SerializedTypeHelper.SerializeHex(so, val); }
public void Serialize(Serializer so, MemoDataInfo val, bool noMarker = false) { var properties = val.GetType().GetProperties() .Where(p => p.Name != p.Name.ToLower()) .ToArray(); var invalid = properties.FirstOrDefault(p => !SerializedTypeHelper.InverseFieldsMap.ContainsKey(p.Name)); if (invalid != null) { throw new InvalidOperationException("JSON contains unknown field: '" + invalid.Name + "'"); } properties = SerializedTypeHelper.SortProperties(properties); // memo format always be string var isJson = val.MemoFormat == "json"; foreach (var property in properties) { object value = property.GetValue(val); if (value == null) { continue; } switch (property.Name) { case "MemoType": value = SerializedTypeHelper.ConvertStringToHex(val.MemoType); break; case "MemoFormat": value = SerializedTypeHelper.ConvertStringToHex(val.MemoFormat); break; case "MemoData": if (val.MemoData is string str) { value = SerializedTypeHelper.ConvertStringToHex(str); break; } if (isJson) { var json = JsonConvert.SerializeObject(val.MemoData); value = SerializedTypeHelper.ConvertStringToHex(json); break; } throw new InvalidOperationException("MemoData can only be a JSON object with a valid json MemoFormat."); } SerializedTypeHelper.Serialize(so, property.Name, value); } if (!noMarker) { //Object ending marker SerializedTypeHelper.STInt8.Serialize(so, 0xe1); } }
public void Serialize(Serializer so, PathComputed[][] val, bool noMarker = false) { if (val != null) { for (int i = 0, l = val.Length; i < l; i++) { //Boundary, if (i > 0) { SerializedTypeHelper.STInt8.Serialize(so, typeBoundary); } var pathes = val[i]; for (int j = 0, l2 = pathes.Length; j < l2; j++) { var entry = pathes[j]; byte type; if (entry.Type != null) { type = entry.Type.Value; } else { type = 0; if (entry.Account != null) { type |= typeAccount; } if (entry.Currency != null) { type |= typeCurrency; } if (entry.Issuer != null) { type |= typeIssuer; } } SerializedTypeHelper.STInt8.Serialize(so, type); if (entry.Account != null) { so.Append(SerializedTypeHelper.ConvertAddressToBytes(entry.Account)); } if (entry.Currency != null) { var currencyBytes = SerializedCurrency.FromJsonToBytes(entry.Currency); so.Append(currencyBytes); } if (entry.Issuer != null) { so.Append(SerializedTypeHelper.ConvertAddressToBytes(entry.Issuer)); } } } } SerializedTypeHelper.STInt8.Serialize(so, typeEnd); }
public void Serialize(Serializer so, TumAmount val, bool noMarker = false) { if (!val.IsValid) { throw new Exception("Not a valid Amount object."); } //For SWT, offset is 0 //only convert the value if (val.IsNative) { var valueHex = val.Value.ToString(16); // Enforce correct length (64 bits) if (valueHex.Length > 16) { throw new Exception("Amount value out of bounds."); } while (valueHex.Length < 16) { valueHex = '0' + valueHex; } //Convert the HEX value to bytes array var valueBytes = Utils.HexToBytes(valueHex); // Clear most significant two bits - these bits should already be 0 if // Amount enforces the range correctly, but we'll clear them anyway just // so this code can make certain guarantees about the encoded value. valueBytes[0] &= 0x3f; if (!val.IsNegative) { valueBytes[0] |= 0x40; } so.Append(valueBytes); } else { //For other non-native currency //1. Serialize the currency value with offset //Put offset long hi = 0, lo = 0; // First bit: non-native hi |= 1 << 31; if (!val.IsZero) { // Second bit: non-negative? if (!val.IsNegative) { hi |= 1 << 30; } // Next eight bits: offset/exponent hi |= ((97L + val.Offset) & 0xff) << 22; // Remaining 54 bits: mantissa hi |= val.Value.ShiftRight(32).LongValue & 0x3fffff; lo = val.Value.LongValue & 0xffffffff; } // Convert from a bitArray to an array of bytes. var arr = new long[] { hi, lo }; int l = arr.Length; long bl; if (l == 0) { bl = 0; } else { var x = arr[l - 1]; var roundX = x / 0x10000000000; // result as Math.Round(x/0x10000000000) if (roundX == 0) { roundX = 32; } bl = (l - 1) * 32 + roundX; } //Setup a new byte array and filled the byte data in //Results should not longer than 8 bytes as defined earlier var tmparray = new List <byte>(); long tmp = 0; for (var i = 0; i < bl / 8; i++) { if ((i & 3) == 0) { tmp = arr[i / 4]; } tmparray.Add((byte)(tmp >> 24)); tmp <<= 8; } if (tmparray.Count > 8) { throw new Exception("Invalid byte array length in AMOUNT value representation"); } var valueBytes = tmparray.ToArray(); so.Append(valueBytes); //2. Serialize the currency info with currency code // and issuer //console.log("Serial non-native AMOUNT ......"); // Currency (160-bit hash) var tum_bytes = val.TumToBytes(); so.Append(tum_bytes); // Issuer (160-bit hash) //so.append(amount.issuer().to_bytes()); so.Append(SerializedTypeHelper.ConvertAddressToBytes(val.Issuer)); } }