deserialize.move 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. module pyth::deserialize {
  2. use wormhole::bytes::{Self};
  3. use wormhole::cursor::{Cursor};
  4. use pyth::i64::{Self, I64};
  5. #[test_only]
  6. use wormhole::cursor::{take_rest};
  7. #[test_only]
  8. use wormhole::cursor::{Self};
  9. public fun deserialize_vector(cur: &mut Cursor<u8>, n: u64): vector<u8> {
  10. bytes::take_bytes(cur, n)
  11. }
  12. public fun deserialize_u8(cur: &mut Cursor<u8>): u8 {
  13. bytes::take_u8(cur)
  14. }
  15. public fun deserialize_u16(cur: &mut Cursor<u8>): u16 {
  16. bytes::take_u16_be(cur)
  17. }
  18. public fun deserialize_u32(cur: &mut Cursor<u8>): u32 {
  19. bytes::take_u32_be(cur)
  20. }
  21. public fun deserialize_i32(cur: &mut Cursor<u8>): I64 {
  22. let deserialized = deserialize_u32(cur);
  23. // If negative, pad the value
  24. let negative = (deserialized >> 31) == 1;
  25. if (negative) {
  26. let padded = (0xFFFFFFFF << 32) + (deserialized as u64);
  27. i64::from_u64((padded as u64))
  28. } else {
  29. i64::from_u64((deserialized as u64))
  30. }
  31. }
  32. public fun deserialize_u64(cur: &mut Cursor<u8>): u64 {
  33. bytes::take_u64_be(cur)
  34. }
  35. public fun deserialize_i64(cur: &mut Cursor<u8>): I64 {
  36. i64::from_u64(deserialize_u64(cur))
  37. }
  38. #[test]
  39. fun test_deserialize_u8() {
  40. let input = x"48258963";
  41. let cursor = cursor::new(input);
  42. let result = deserialize_u8(&mut cursor);
  43. assert!(result == 0x48, 1);
  44. let rest = take_rest(cursor);
  45. assert!(rest == x"258963", 1);
  46. }
  47. #[test]
  48. fun test_deserialize_u16() {
  49. let input = x"48258963";
  50. let cursor = cursor::new(input);
  51. let result = deserialize_u16(&mut cursor);
  52. assert!(result == 0x4825, 1);
  53. let rest = take_rest(cursor);
  54. assert!(rest == x"8963", 1);
  55. }
  56. #[test]
  57. fun test_deserialize_u32() {
  58. let input = x"4825896349741695";
  59. let cursor = cursor::new(input);
  60. let result = deserialize_u32(&mut cursor);
  61. assert!(result == 0x48258963, 1);
  62. let rest = take_rest(cursor);
  63. assert!(rest == x"49741695", 1);
  64. }
  65. #[test]
  66. fun test_deserialize_i32_positive() {
  67. let input = x"4825896349741695";
  68. let cursor = cursor::new(input);
  69. let result = deserialize_i32(&mut cursor);
  70. assert!(result == i64::from_u64(0x48258963), 1);
  71. let rest = take_rest(cursor);
  72. assert!(rest == x"49741695", 1);
  73. }
  74. #[test]
  75. fun test_deserialize_i32_negative() {
  76. let input = x"FFFFFDC349741695";
  77. let cursor = cursor::new(input);
  78. let result = deserialize_i32(&mut cursor);
  79. assert!(result == i64::from_u64(0xFFFFFFFFFFFFFDC3), 1);
  80. let rest = take_rest(cursor);
  81. assert!(rest == x"49741695", 1);
  82. }
  83. #[test]
  84. fun test_deserialize_u64() {
  85. let input = x"48258963497416957497253486";
  86. let cursor = cursor::new(input);
  87. let result = deserialize_u64(&mut cursor);
  88. assert!(result == 0x4825896349741695, 1);
  89. let rest = take_rest(cursor);
  90. assert!(rest == x"7497253486", 1);
  91. }
  92. #[test]
  93. fun test_deserialize_i64_positive() {
  94. let input = x"48258963497416957497253486";
  95. let cursor = cursor::new(input);
  96. let result = deserialize_i64(&mut cursor);
  97. assert!(result == i64::from_u64(0x4825896349741695), 1);
  98. let rest = take_rest(cursor);
  99. assert!(rest == x"7497253486", 1);
  100. }
  101. #[test]
  102. fun test_deserialize_i64_negative() {
  103. let input = x"FFFFFFFFFFFFFDC37497253486";
  104. let cursor = cursor::new(input);
  105. let result = deserialize_i64(&mut cursor);
  106. assert!(result == i64::from_u64(0xFFFFFFFFFFFFFDC3), 1);
  107. let rest = take_rest(cursor);
  108. assert!(rest == x"7497253486", 1);
  109. }
  110. }