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)); } }