-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathutils.ts
More file actions
303 lines (240 loc) · 13.1 KB
/
utils.ts
File metadata and controls
303 lines (240 loc) · 13.1 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import should from 'should';
import {
flushERC721TokensData,
flushERC1155TokensData,
decodeFlushERC721TokensData,
decodeFlushERC1155TokensData,
isHtsEvmAddress,
} from '../../src/lib/utils';
import { ERC721TransferBuilder } from '../../src/lib/transferBuilders/transferBuilderERC721';
import { ERC721TransferFromMethodId, ERC721SafeTransferTypeMethodId } from '../../src/lib/walletUtil';
describe('Abstract ETH Utils', () => {
describe('ERC721 Flush Functions', () => {
it('should encode flush ERC721 data correctly for v0-v3', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '12345';
const forwarderVersion = 0;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.exist(encoded);
encoded.should.startWith('0x5a953d0a'); // flushERC721ForwarderTokens method ID
encoded.should.be.a.String();
encoded.length.should.be.greaterThan(10); // method ID + parameters
});
it('should encode flush ERC721 data correctly for v4+', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '12345';
const forwarderVersion = 4;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.exist(encoded);
encoded.should.startWith('0x159e44d7'); // flushERC721Token method ID (v5)
encoded.should.be.a.String();
});
it('should decode flush ERC721 data correctly for v0-v3', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '12345';
const forwarderVersion = 2;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC721TokensData(encoded);
should.exist(decoded);
decoded.forwarderAddress.toLowerCase().should.equal(forwarderAddress.toLowerCase());
decoded.tokenAddress.toLowerCase().should.equal(tokenAddress.toLowerCase());
decoded.tokenId.should.equal(tokenId);
should.not.exist(decoded.forwarderVersion);
});
it('should decode flush ERC721 data correctly for v4+', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '12345';
const forwarderVersion = 4;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC721TokensData(encoded, forwarderAddress);
should.exist(decoded);
decoded.forwarderAddress.toLowerCase().should.equal(forwarderAddress.toLowerCase());
decoded.tokenAddress.toLowerCase().should.equal(tokenAddress.toLowerCase());
decoded.tokenId.should.equal(tokenId);
should.exist(decoded.forwarderVersion);
should.equal(decoded.forwarderVersion, 4);
});
it('should handle large token IDs for ERC721', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '115792089237316195423570985008687907853269984665640564039457584007913129639935'; // max uint256
const forwarderVersion = 0;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC721TokensData(encoded);
decoded.tokenId.should.equal(tokenId);
});
it('should throw error for invalid flush ERC721 data', () => {
const invalidData = '0x12345678'; // Wrong method ID
should.throws(() => {
decodeFlushERC721TokensData(invalidData);
}, /Invalid flush ERC721 bytecode/);
});
it('should throw error for missing to address in v4+ decode', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '12345';
const forwarderVersion = 4;
const encoded = flushERC721TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.throws(() => {
decodeFlushERC721TokensData(encoded); // Missing 'to' parameter
}, /Missing to address/);
});
});
describe('ERC1155 Flush Functions', () => {
it('should encode flush ERC1155 data correctly for v0-v3', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '99999';
const forwarderVersion = 0;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.exist(encoded);
encoded.should.startWith('0xe6bd0aa4'); // flushERC1155ForwarderTokens method ID
encoded.should.be.a.String();
encoded.length.should.be.greaterThan(10); // method ID + parameters
});
it('should encode flush ERC1155 data correctly for v4+', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '99999';
const forwarderVersion = 4;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.exist(encoded);
encoded.should.startWith('0x8972c17c'); // flushERC1155Tokens method ID (v5)
encoded.should.be.a.String();
});
it('should decode flush ERC1155 data correctly for v0-v3', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '99999';
const forwarderVersion = 2;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC1155TokensData(encoded);
should.exist(decoded);
decoded.forwarderAddress.toLowerCase().should.equal(forwarderAddress.toLowerCase());
decoded.tokenAddress.toLowerCase().should.equal(tokenAddress.toLowerCase());
decoded.tokenId.should.equal(tokenId);
should.not.exist(decoded.forwarderVersion);
});
it('should decode flush ERC1155 data correctly for v4+', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '99999';
const forwarderVersion = 4;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC1155TokensData(encoded, forwarderAddress);
should.exist(decoded);
decoded.forwarderAddress.toLowerCase().should.equal(forwarderAddress.toLowerCase());
decoded.tokenAddress.toLowerCase().should.equal(tokenAddress.toLowerCase());
decoded.tokenId.should.equal(tokenId);
should.exist(decoded.forwarderVersion);
should.equal(decoded.forwarderVersion, 4);
});
it('should handle token ID 0 for ERC1155', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '0';
const forwarderVersion = 0;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC1155TokensData(encoded);
decoded.tokenId.should.equal(tokenId);
});
it('should handle large token IDs for ERC1155', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '115792089237316195423570985008687907853269984665640564039457584007913129639935'; // max uint256
const forwarderVersion = 0;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
const decoded = decodeFlushERC1155TokensData(encoded);
decoded.tokenId.should.equal(tokenId);
});
it('should throw error for invalid flush ERC1155 data', () => {
const invalidData = '0x87654321'; // Wrong method ID
should.throws(() => {
decodeFlushERC1155TokensData(invalidData);
}, /Invalid flush ERC1155 bytecode/);
});
it('should throw error for missing to address in v4+ decode', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddress = '0xdf7decb1baa8f529f0c8982cbb4be50357195299';
const tokenId = '99999';
const forwarderVersion = 4;
const encoded = flushERC1155TokensData(forwarderAddress, tokenAddress, tokenId, forwarderVersion);
should.throws(() => {
decodeFlushERC1155TokensData(encoded); // Missing 'to' parameter
}, /Missing to address/);
});
});
describe('isHtsEvmAddress', () => {
it('should return true for HTS native token addresses (long-zero format)', () => {
// Real HTS NFT addresses from statics
isHtsEvmAddress('0x00000000000000000000000000000000007ac203').should.be.true();
isHtsEvmAddress('0x00000000000000000000000000000000007103a5').should.be.true();
isHtsEvmAddress('0x0000000000000000000000000000000000728a62').should.be.true();
isHtsEvmAddress('0x00000000000000000000000000000000007ac19c').should.be.true();
});
it('should return false for standard Solidity contract addresses', () => {
isHtsEvmAddress('0x5df4076613e714a4cc4284abac87caa927b918a8').should.be.false();
isHtsEvmAddress('0xcee79325714727016c125f80ef1a5d1f47b3d8d2').should.be.false();
isHtsEvmAddress('0xc795c4faae7f16a69bec13c5dfd9e8a156a68625').should.be.false();
isHtsEvmAddress('0x8f977e912ef500548a0c3be6ddde9899f1199b81').should.be.false();
});
it('should handle uppercase hex characters', () => {
isHtsEvmAddress('0x00000000000000000000000000000000007AC203').should.be.true();
isHtsEvmAddress('0x5DF4076613E714A4CC4284ABAC87CAA927B918A8').should.be.false();
});
it('should return false for zero address', () => {
isHtsEvmAddress('0x0000000000000000000000000000000000000000').should.be.true();
});
it('should return false for invalid format', () => {
isHtsEvmAddress('0x1234').should.be.false();
isHtsEvmAddress('not-an-address').should.be.false();
});
});
describe('ERC721TransferBuilder.buildTransferFrom', () => {
const owner = '0x19645032c7f1533395d44a629462e751084d3e4d';
const recipient = '0x19645032c7f1533395d44a629462e751084d3e4c';
const htsNftAddress = '0x00000000000000000000000000000000007ac203';
it('should encode transferFrom with selector 0x23b872dd', () => {
const builder = new ERC721TransferBuilder();
builder.tokenContractAddress(htsNftAddress).to(recipient).from(owner).tokenId('12');
const data = builder.buildTransferFrom();
should.exist(data);
data.should.startWith(ERC721TransferFromMethodId); // 0x23b872dd
});
it('should encode safeTransferFrom with selector 0xb88d4fde via build()', () => {
const builder = new ERC721TransferBuilder();
builder.tokenContractAddress(htsNftAddress).to(recipient).from(owner).tokenId('12');
const data = builder.build();
should.exist(data);
data.should.startWith(ERC721SafeTransferTypeMethodId); // 0xb88d4fde
});
it('should produce different encodings for build() vs buildTransferFrom()', () => {
const builder = new ERC721TransferBuilder();
builder.tokenContractAddress(htsNftAddress).to(recipient).from(owner).tokenId('12');
const safeTransferData = builder.build();
const transferFromData = builder.buildTransferFrom();
safeTransferData.should.not.equal(transferFromData);
// transferFrom encoding should be shorter (no bytes param)
transferFromData.length.should.be.lessThan(safeTransferData.length);
});
});
describe('Token Address Validation', () => {
it('should preserve address format in encoding/decoding', () => {
const forwarderAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81';
const tokenAddressChecksum = '0xDf7DECb1bAa8F529F0C8982cBB4Be50357195299'; // With checksum
const tokenId = '12345';
const forwarderVersion = 0;
const encoded721 = flushERC721TokensData(forwarderAddress, tokenAddressChecksum, tokenId, forwarderVersion);
const decoded721 = decodeFlushERC721TokensData(encoded721);
// Should preserve the address (though may lowercase it)
decoded721.tokenAddress.toLowerCase().should.equal(tokenAddressChecksum.toLowerCase());
const encoded1155 = flushERC1155TokensData(forwarderAddress, tokenAddressChecksum, tokenId, forwarderVersion);
const decoded1155 = decodeFlushERC1155TokensData(encoded1155);
decoded1155.tokenAddress.toLowerCase().should.equal(tokenAddressChecksum.toLowerCase());
});
});
});