-
Notifications
You must be signed in to change notification settings - Fork 24
/
memo.test.js
120 lines (100 loc) · 3.71 KB
/
memo.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var assert = require("assert");
const {
Header,
FieldsV0,
EncodingFormat,
OpCode,
EncodeToBytes,
} = require("./memo");
const { Web3 } = require("web3");
const web3 = new Web3();
// Test data
const receiver = "0xEA9808f0Ac504d1F521B5BbdfC33e6f1953757a7";
const payload = new TextEncoder().encode("a payload");
const revertAddress = "tb1q6rufg6myrxurdn0h57d2qhtm9zfmjw2mzcm05q";
// Test case for memo ABI encoding
const testMemoAbi = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtABI,
OpCode.DepositAndCall
);
// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);
// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);
// Expected output
const expectedHex =
"5a001007" + // header
"000000000000000000000000ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"0000000000000000000000000000000000000000000000000000000000000060" + // payload offset
"00000000000000000000000000000000000000000000000000000000000000a0" + // revertAddress offset
"0000000000000000000000000000000000000000000000000000000000000009" + // payload length
"61207061796c6f61640000000000000000000000000000000000000000000000" + // payload
"000000000000000000000000000000000000000000000000000000000000002a" + // revertAddress length
"746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d30357100000000000000000000000000000000000000000000"; // revertAddress
// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"ABI encoding failed: encoded bytes do not match expected"
);
console.log("Test passed: testMemoAbi");
};
// Test case for memo compact short encoding
const testMemoCompactShort = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtCompactShort,
OpCode.DepositAndCall
);
// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);
// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);
// Expected output
const expectedHex =
"5a011007" + // header
"ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"0961207061796c6f6164" + // payload
"2a746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571"; // revertAddress
// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"Compact short encoding failed: encoded bytes do not match expected"
);
console.log("Test passed: testMemoCompactShort");
};
// Test case for memo compact long encoding
const testMemoCompactLong = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtCompactLong,
OpCode.DepositAndCall
);
// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);
// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);
// Expected output
const expectedHex =
"5a021007" + // header
"ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"090061207061796c6f6164" + // payload
"2a00746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571"; // revertAddress
// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"Compact long encoding failed: encoded bytes do not match expected"
);
console.log("Test passed: testMemoCompactLong");
};
// Run the test cases
testMemoAbi();
testMemoCompactShort();
testMemoCompactLong();