rlp-decode.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //See: https://github.com/ethereum/wiki/wiki/RLP
  2. import { hexlify } from "./data.js";
  3. import { assert, assertArgument } from "./errors.js";
  4. import { getBytes } from "./data.js";
  5. import type { BytesLike, RlpStructuredData } from "./index.js";
  6. function hexlifyByte(value: number): string {
  7. let result = value.toString(16);
  8. while (result.length < 2) { result = "0" + result; }
  9. return "0x" + result;
  10. }
  11. function unarrayifyInteger(data: Uint8Array, offset: number, length: number): number {
  12. let result = 0;
  13. for (let i = 0; i < length; i++) {
  14. result = (result * 256) + data[offset + i];
  15. }
  16. return result;
  17. }
  18. type Decoded = {
  19. result: any;
  20. consumed: number;
  21. };
  22. function _decodeChildren(data: Uint8Array, offset: number, childOffset: number, length: number): Decoded {
  23. const result: Array<any> = [];
  24. while (childOffset < offset + 1 + length) {
  25. const decoded = _decode(data, childOffset);
  26. result.push(decoded.result);
  27. childOffset += decoded.consumed;
  28. assert(childOffset <= offset + 1 + length, "child data too short", "BUFFER_OVERRUN", {
  29. buffer: data, length, offset
  30. });
  31. }
  32. return {consumed: (1 + length), result: result};
  33. }
  34. // returns { consumed: number, result: Object }
  35. function _decode(data: Uint8Array, offset: number): { consumed: number, result: any } {
  36. assert(data.length !== 0, "data too short", "BUFFER_OVERRUN", {
  37. buffer: data, length: 0, offset: 1
  38. });
  39. const checkOffset = (offset: number) => {
  40. assert(offset <= data.length, "data short segment too short", "BUFFER_OVERRUN", {
  41. buffer: data, length: data.length, offset
  42. });
  43. };
  44. // Array with extra length prefix
  45. if (data[offset] >= 0xf8) {
  46. const lengthLength = data[offset] - 0xf7;
  47. checkOffset(offset + 1 + lengthLength);
  48. const length = unarrayifyInteger(data, offset + 1, lengthLength);
  49. checkOffset(offset + 1 + lengthLength + length);
  50. return _decodeChildren(data, offset, offset + 1 + lengthLength, lengthLength + length);
  51. } else if (data[offset] >= 0xc0) {
  52. const length = data[offset] - 0xc0;
  53. checkOffset(offset + 1 + length);
  54. return _decodeChildren(data, offset, offset + 1, length);
  55. } else if (data[offset] >= 0xb8) {
  56. const lengthLength = data[offset] - 0xb7;
  57. checkOffset(offset + 1 + lengthLength);
  58. const length = unarrayifyInteger(data, offset + 1, lengthLength);
  59. checkOffset(offset + 1 + lengthLength + length);
  60. const result = hexlify(data.slice(offset + 1 + lengthLength, offset + 1 + lengthLength + length));
  61. return { consumed: (1 + lengthLength + length), result: result }
  62. } else if (data[offset] >= 0x80) {
  63. const length = data[offset] - 0x80;
  64. checkOffset(offset + 1 + length);
  65. const result = hexlify(data.slice(offset + 1, offset + 1 + length));
  66. return { consumed: (1 + length), result: result }
  67. }
  68. return { consumed: 1, result: hexlifyByte(data[offset]) };
  69. }
  70. /**
  71. * Decodes %%data%% into the structured data it represents.
  72. */
  73. export function decodeRlp(_data: BytesLike): RlpStructuredData {
  74. const data = getBytes(_data, "data");
  75. const decoded = _decode(data, 0);
  76. assertArgument(decoded.consumed === data.length, "unexpected junk after rlp payload", "data", _data);
  77. return decoded.result;
  78. }