@qevm/hash 5.7.1 → 5.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/ens-normalize/decoder.d.ts +4 -4
- package/lib/ens-normalize/decoder.d.ts.map +1 -1
- package/lib/ens-normalize/decoder.js +86 -87
- package/lib/ens-normalize/decoder.js.map +1 -1
- package/lib/ens-normalize/include.js +3 -4
- package/lib/ens-normalize/include.js.map +1 -1
- package/lib/ens-normalize/lib.js +40 -48
- package/lib/ens-normalize/lib.js.map +1 -1
- package/lib/id.js +3 -4
- package/lib/id.js.map +1 -1
- package/lib/index.js +5 -5
- package/lib/index.js.map +1 -1
- package/lib/message.js +5 -5
- package/lib/message.js.map +1 -1
- package/lib/namehash.js +22 -23
- package/lib/namehash.js.map +1 -1
- package/lib/typed-data.js +178 -245
- package/lib/typed-data.js.map +1 -1
- package/package.json +5 -5
- package/src.ts/ens-normalize/include.ts +1 -1
- package/src.ts/ens-normalize/lib.ts +1 -1
- package/src.ts/id.ts +1 -1
- package/src.ts/message.ts +1 -1
- package/src.ts/namehash.ts +1 -1
- package/src.ts/typed-data.ts +1 -1
package/lib/typed-data.js
CHANGED
|
@@ -1,86 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.TypedDataEncoder = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
4
|
+
const address_1 = require("@qevm/address");
|
|
5
|
+
const bignumber_1 = require("@qevm/bignumber");
|
|
6
|
+
const bytes_1 = require("@qevm/bytes");
|
|
7
|
+
const keccak256_1 = require("@ethersproject/keccak256");
|
|
8
|
+
const properties_1 = require("@ethersproject/properties");
|
|
9
|
+
const logger_1 = require("@ethersproject/logger");
|
|
10
|
+
const _version_1 = require("./_version");
|
|
11
|
+
const logger = new logger_1.Logger(_version_1.version);
|
|
12
|
+
const id_1 = require("./id");
|
|
13
|
+
const padding = new Uint8Array(32);
|
|
50
14
|
padding.fill(0);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
15
|
+
const NegativeOne = bignumber_1.BigNumber.from(-1);
|
|
16
|
+
const Zero = bignumber_1.BigNumber.from(0);
|
|
17
|
+
const One = bignumber_1.BigNumber.from(1);
|
|
18
|
+
const MaxUint256 = bignumber_1.BigNumber.from("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
55
19
|
function hexPadRight(value) {
|
|
56
|
-
|
|
57
|
-
|
|
20
|
+
const bytes = (0, bytes_1.arrayify)(value);
|
|
21
|
+
const padOffset = bytes.length % 32;
|
|
58
22
|
if (padOffset) {
|
|
59
23
|
return (0, bytes_1.hexConcat)([bytes, padding.slice(padOffset)]);
|
|
60
24
|
}
|
|
61
25
|
return (0, bytes_1.hexlify)(bytes);
|
|
62
26
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
27
|
+
const hexTrue = (0, bytes_1.hexZeroPad)(One.toHexString(), 32);
|
|
28
|
+
const hexFalse = (0, bytes_1.hexZeroPad)(Zero.toHexString(), 32);
|
|
29
|
+
const domainFieldTypes = {
|
|
66
30
|
name: "string",
|
|
67
31
|
version: "string",
|
|
68
32
|
chainId: "uint256",
|
|
69
33
|
verifyingContract: "address",
|
|
70
34
|
salt: "bytes32"
|
|
71
35
|
};
|
|
72
|
-
|
|
36
|
+
const domainFieldNames = [
|
|
73
37
|
"name", "version", "chainId", "verifyingContract", "salt"
|
|
74
38
|
];
|
|
75
39
|
function checkString(key) {
|
|
76
40
|
return function (value) {
|
|
77
41
|
if (typeof (value) !== "string") {
|
|
78
|
-
logger.throwArgumentError(
|
|
42
|
+
logger.throwArgumentError(`invalid domain value for ${JSON.stringify(key)}`, `domain.${key}`, value);
|
|
79
43
|
}
|
|
80
44
|
return value;
|
|
81
45
|
};
|
|
82
46
|
}
|
|
83
|
-
|
|
47
|
+
const domainChecks = {
|
|
84
48
|
name: checkString("name"),
|
|
85
49
|
version: checkString("version"),
|
|
86
50
|
chainId: function (value) {
|
|
@@ -88,43 +52,43 @@ var domainChecks = {
|
|
|
88
52
|
return bignumber_1.BigNumber.from(value).toString();
|
|
89
53
|
}
|
|
90
54
|
catch (error) { }
|
|
91
|
-
return logger.throwArgumentError(
|
|
55
|
+
return logger.throwArgumentError(`invalid domain value for "chainId"`, "domain.chainId", value);
|
|
92
56
|
},
|
|
93
57
|
verifyingContract: function (value) {
|
|
94
58
|
try {
|
|
95
59
|
return (0, address_1.getAddress)(value).toLowerCase();
|
|
96
60
|
}
|
|
97
61
|
catch (error) { }
|
|
98
|
-
return logger.throwArgumentError(
|
|
62
|
+
return logger.throwArgumentError(`invalid domain value "verifyingContract"`, "domain.verifyingContract", value);
|
|
99
63
|
},
|
|
100
64
|
salt: function (value) {
|
|
101
65
|
try {
|
|
102
|
-
|
|
66
|
+
const bytes = (0, bytes_1.arrayify)(value);
|
|
103
67
|
if (bytes.length !== 32) {
|
|
104
68
|
throw new Error("bad length");
|
|
105
69
|
}
|
|
106
70
|
return (0, bytes_1.hexlify)(bytes);
|
|
107
71
|
}
|
|
108
72
|
catch (error) { }
|
|
109
|
-
return logger.throwArgumentError(
|
|
73
|
+
return logger.throwArgumentError(`invalid domain value "salt"`, "domain.salt", value);
|
|
110
74
|
}
|
|
111
75
|
};
|
|
112
76
|
function getBaseEncoder(type) {
|
|
113
77
|
// intXX and uintXX
|
|
114
78
|
{
|
|
115
|
-
|
|
79
|
+
const match = type.match(/^(u?)int(\d*)$/);
|
|
116
80
|
if (match) {
|
|
117
|
-
|
|
118
|
-
|
|
81
|
+
const signed = (match[1] === "");
|
|
82
|
+
const width = parseInt(match[2] || "256");
|
|
119
83
|
if (width % 8 !== 0 || width > 256 || (match[2] && match[2] !== String(width))) {
|
|
120
84
|
logger.throwArgumentError("invalid numeric width", "type", type);
|
|
121
85
|
}
|
|
122
|
-
|
|
123
|
-
|
|
86
|
+
const boundsUpper = MaxUint256.mask(signed ? (width - 1) : width);
|
|
87
|
+
const boundsLower = signed ? boundsUpper.add(One).mul(NegativeOne) : Zero;
|
|
124
88
|
return function (value) {
|
|
125
|
-
|
|
126
|
-
if (v.lt(
|
|
127
|
-
logger.throwArgumentError(
|
|
89
|
+
const v = bignumber_1.BigNumber.from(value);
|
|
90
|
+
if (v.lt(boundsLower) || v.gt(boundsUpper)) {
|
|
91
|
+
logger.throwArgumentError(`value out-of-bounds for ${type}`, "value", value);
|
|
128
92
|
}
|
|
129
93
|
return (0, bytes_1.hexZeroPad)(v.toTwos(256).toHexString(), 32);
|
|
130
94
|
};
|
|
@@ -132,16 +96,16 @@ function getBaseEncoder(type) {
|
|
|
132
96
|
}
|
|
133
97
|
// bytesXX
|
|
134
98
|
{
|
|
135
|
-
|
|
99
|
+
const match = type.match(/^bytes(\d+)$/);
|
|
136
100
|
if (match) {
|
|
137
|
-
|
|
138
|
-
if (
|
|
101
|
+
const width = parseInt(match[1]);
|
|
102
|
+
if (width === 0 || width > 32 || match[1] !== String(width)) {
|
|
139
103
|
logger.throwArgumentError("invalid bytes width", "type", type);
|
|
140
104
|
}
|
|
141
105
|
return function (value) {
|
|
142
|
-
|
|
143
|
-
if (bytes.length !==
|
|
144
|
-
logger.throwArgumentError(
|
|
106
|
+
const bytes = (0, bytes_1.arrayify)(value);
|
|
107
|
+
if (bytes.length !== width) {
|
|
108
|
+
logger.throwArgumentError(`invalid length for ${type}`, "value", value);
|
|
145
109
|
}
|
|
146
110
|
return hexPadRight(value);
|
|
147
111
|
};
|
|
@@ -164,79 +128,73 @@ function getBaseEncoder(type) {
|
|
|
164
128
|
return null;
|
|
165
129
|
}
|
|
166
130
|
function encodeType(name, fields) {
|
|
167
|
-
return name + "
|
|
168
|
-
var name = _a.name, type = _a.type;
|
|
169
|
-
return (type + " " + name);
|
|
170
|
-
}).join(",") + ")";
|
|
131
|
+
return `${name}(${fields.map(({ name, type }) => (type + " " + name)).join(",")})`;
|
|
171
132
|
}
|
|
172
|
-
|
|
173
|
-
|
|
133
|
+
class TypedDataEncoder {
|
|
134
|
+
constructor(types) {
|
|
174
135
|
(0, properties_1.defineReadOnly)(this, "types", Object.freeze((0, properties_1.deepCopy)(types)));
|
|
175
136
|
(0, properties_1.defineReadOnly)(this, "_encoderCache", {});
|
|
176
137
|
(0, properties_1.defineReadOnly)(this, "_types", {});
|
|
177
138
|
// Link struct types to their direct child structs
|
|
178
|
-
|
|
139
|
+
const links = {};
|
|
179
140
|
// Link structs to structs which contain them as a child
|
|
180
|
-
|
|
141
|
+
const parents = {};
|
|
181
142
|
// Link all subtypes within a given struct
|
|
182
|
-
|
|
183
|
-
Object.keys(types).forEach(
|
|
143
|
+
const subtypes = {};
|
|
144
|
+
Object.keys(types).forEach((type) => {
|
|
184
145
|
links[type] = {};
|
|
185
146
|
parents[type] = [];
|
|
186
147
|
subtypes[type] = {};
|
|
187
148
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
types[
|
|
149
|
+
for (const name in types) {
|
|
150
|
+
const uniqueNames = {};
|
|
151
|
+
types[name].forEach((field) => {
|
|
191
152
|
// Check each field has a unique name
|
|
192
153
|
if (uniqueNames[field.name]) {
|
|
193
|
-
logger.throwArgumentError(
|
|
154
|
+
logger.throwArgumentError(`duplicate variable name ${JSON.stringify(field.name)} in ${JSON.stringify(name)}`, "types", types);
|
|
194
155
|
}
|
|
195
156
|
uniqueNames[field.name] = true;
|
|
196
157
|
// Get the base type (drop any array specifiers)
|
|
197
|
-
|
|
198
|
-
if (baseType ===
|
|
199
|
-
logger.throwArgumentError(
|
|
158
|
+
const baseType = field.type.match(/^([^\x5b]*)(\x5b|$)/)[1];
|
|
159
|
+
if (baseType === name) {
|
|
160
|
+
logger.throwArgumentError(`circular type reference to ${JSON.stringify(baseType)}`, "types", types);
|
|
200
161
|
}
|
|
201
162
|
// Is this a base encoding type?
|
|
202
|
-
|
|
163
|
+
const encoder = getBaseEncoder(baseType);
|
|
203
164
|
if (encoder) {
|
|
204
165
|
return;
|
|
205
166
|
}
|
|
206
167
|
if (!parents[baseType]) {
|
|
207
|
-
logger.throwArgumentError(
|
|
168
|
+
logger.throwArgumentError(`unknown type ${JSON.stringify(baseType)}`, "types", types);
|
|
208
169
|
}
|
|
209
170
|
// Add linkage
|
|
210
|
-
parents[baseType].push(
|
|
211
|
-
links[
|
|
171
|
+
parents[baseType].push(name);
|
|
172
|
+
links[name][baseType] = true;
|
|
212
173
|
});
|
|
213
|
-
};
|
|
214
|
-
for (var name_1 in types) {
|
|
215
|
-
_loop_1(name_1);
|
|
216
174
|
}
|
|
217
175
|
// Deduce the primary type
|
|
218
|
-
|
|
176
|
+
const primaryTypes = Object.keys(parents).filter((n) => (parents[n].length === 0));
|
|
219
177
|
if (primaryTypes.length === 0) {
|
|
220
178
|
logger.throwArgumentError("missing primary type", "types", types);
|
|
221
179
|
}
|
|
222
180
|
else if (primaryTypes.length > 1) {
|
|
223
|
-
logger.throwArgumentError(
|
|
181
|
+
logger.throwArgumentError(`ambiguous primary types or unused types: ${primaryTypes.map((t) => (JSON.stringify(t))).join(", ")}`, "types", types);
|
|
224
182
|
}
|
|
225
183
|
(0, properties_1.defineReadOnly)(this, "primaryType", primaryTypes[0]);
|
|
226
184
|
// Check for circular type references
|
|
227
185
|
function checkCircular(type, found) {
|
|
228
186
|
if (found[type]) {
|
|
229
|
-
logger.throwArgumentError(
|
|
187
|
+
logger.throwArgumentError(`circular type reference to ${JSON.stringify(type)}`, "types", types);
|
|
230
188
|
}
|
|
231
189
|
found[type] = true;
|
|
232
|
-
Object.keys(links[type]).forEach(
|
|
190
|
+
Object.keys(links[type]).forEach((child) => {
|
|
233
191
|
if (!parents[child]) {
|
|
234
192
|
return;
|
|
235
193
|
}
|
|
236
194
|
// Recursively check children
|
|
237
195
|
checkCircular(child, found);
|
|
238
196
|
// Mark all ancestors as having this decendant
|
|
239
|
-
Object.keys(found).forEach(
|
|
197
|
+
Object.keys(found).forEach((subtype) => {
|
|
240
198
|
subtypes[subtype][child] = true;
|
|
241
199
|
});
|
|
242
200
|
});
|
|
@@ -244,221 +202,197 @@ var TypedDataEncoder = /** @class */ (function () {
|
|
|
244
202
|
}
|
|
245
203
|
checkCircular(this.primaryType, {});
|
|
246
204
|
// Compute each fully describe type
|
|
247
|
-
for (
|
|
248
|
-
|
|
205
|
+
for (const name in subtypes) {
|
|
206
|
+
const st = Object.keys(subtypes[name]);
|
|
249
207
|
st.sort();
|
|
250
|
-
this._types[
|
|
208
|
+
this._types[name] = encodeType(name, types[name]) + st.map((t) => encodeType(t, types[t])).join("");
|
|
251
209
|
}
|
|
252
210
|
}
|
|
253
|
-
|
|
254
|
-
|
|
211
|
+
getEncoder(type) {
|
|
212
|
+
let encoder = this._encoderCache[type];
|
|
255
213
|
if (!encoder) {
|
|
256
214
|
encoder = this._encoderCache[type] = this._getEncoder(type);
|
|
257
215
|
}
|
|
258
216
|
return encoder;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
var _this = this;
|
|
217
|
+
}
|
|
218
|
+
_getEncoder(type) {
|
|
262
219
|
// Basic encoder type (address, bool, uint256, etc)
|
|
263
220
|
{
|
|
264
|
-
|
|
221
|
+
const encoder = getBaseEncoder(type);
|
|
265
222
|
if (encoder) {
|
|
266
223
|
return encoder;
|
|
267
224
|
}
|
|
268
225
|
}
|
|
269
226
|
// Array
|
|
270
|
-
|
|
227
|
+
const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
|
|
271
228
|
if (match) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return
|
|
276
|
-
if (
|
|
229
|
+
const subtype = match[1];
|
|
230
|
+
const subEncoder = this.getEncoder(subtype);
|
|
231
|
+
const length = parseInt(match[3]);
|
|
232
|
+
return (value) => {
|
|
233
|
+
if (length >= 0 && value.length !== length) {
|
|
277
234
|
logger.throwArgumentError("array length mismatch; expected length ${ arrayLength }", "value", value);
|
|
278
235
|
}
|
|
279
|
-
|
|
280
|
-
if (
|
|
236
|
+
let result = value.map(subEncoder);
|
|
237
|
+
if (this._types[subtype]) {
|
|
281
238
|
result = result.map(keccak256_1.keccak256);
|
|
282
239
|
}
|
|
283
240
|
return (0, keccak256_1.keccak256)((0, bytes_1.hexConcat)(result));
|
|
284
241
|
};
|
|
285
242
|
}
|
|
286
243
|
// Struct
|
|
287
|
-
|
|
244
|
+
const fields = this.types[type];
|
|
288
245
|
if (fields) {
|
|
289
|
-
|
|
290
|
-
return
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
if (_this._types[type]) {
|
|
246
|
+
const encodedType = (0, id_1.id)(this._types[type]);
|
|
247
|
+
return (value) => {
|
|
248
|
+
const values = fields.map(({ name, type }) => {
|
|
249
|
+
const result = this.getEncoder(type)(value[name]);
|
|
250
|
+
if (this._types[type]) {
|
|
295
251
|
return (0, keccak256_1.keccak256)(result);
|
|
296
252
|
}
|
|
297
253
|
return result;
|
|
298
254
|
});
|
|
299
|
-
values.unshift(
|
|
255
|
+
values.unshift(encodedType);
|
|
300
256
|
return (0, bytes_1.hexConcat)(values);
|
|
301
257
|
};
|
|
302
258
|
}
|
|
303
|
-
return logger.throwArgumentError(
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
259
|
+
return logger.throwArgumentError(`unknown type: ${type}`, "type", type);
|
|
260
|
+
}
|
|
261
|
+
encodeType(name) {
|
|
262
|
+
const result = this._types[name];
|
|
307
263
|
if (!result) {
|
|
308
|
-
logger.throwArgumentError(
|
|
264
|
+
logger.throwArgumentError(`unknown type: ${JSON.stringify(name)}`, "name", name);
|
|
309
265
|
}
|
|
310
266
|
return result;
|
|
311
|
-
}
|
|
312
|
-
|
|
267
|
+
}
|
|
268
|
+
encodeData(type, value) {
|
|
313
269
|
return this.getEncoder(type)(value);
|
|
314
|
-
}
|
|
315
|
-
|
|
270
|
+
}
|
|
271
|
+
hashStruct(name, value) {
|
|
316
272
|
return (0, keccak256_1.keccak256)(this.encodeData(name, value));
|
|
317
|
-
}
|
|
318
|
-
|
|
273
|
+
}
|
|
274
|
+
encode(value) {
|
|
319
275
|
return this.encodeData(this.primaryType, value);
|
|
320
|
-
}
|
|
321
|
-
|
|
276
|
+
}
|
|
277
|
+
hash(value) {
|
|
322
278
|
return this.hashStruct(this.primaryType, value);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
var _this = this;
|
|
279
|
+
}
|
|
280
|
+
_visit(type, value, callback) {
|
|
326
281
|
// Basic encoder type (address, bool, uint256, etc)
|
|
327
282
|
{
|
|
328
|
-
|
|
283
|
+
const encoder = getBaseEncoder(type);
|
|
329
284
|
if (encoder) {
|
|
330
285
|
return callback(type, value);
|
|
331
286
|
}
|
|
332
287
|
}
|
|
333
288
|
// Array
|
|
334
|
-
|
|
289
|
+
const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
|
|
335
290
|
if (match) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if (
|
|
291
|
+
const subtype = match[1];
|
|
292
|
+
const length = parseInt(match[3]);
|
|
293
|
+
if (length >= 0 && value.length !== length) {
|
|
339
294
|
logger.throwArgumentError("array length mismatch; expected length ${ arrayLength }", "value", value);
|
|
340
295
|
}
|
|
341
|
-
return value.map(
|
|
296
|
+
return value.map((v) => this._visit(subtype, v, callback));
|
|
342
297
|
}
|
|
343
298
|
// Struct
|
|
344
|
-
|
|
299
|
+
const fields = this.types[type];
|
|
345
300
|
if (fields) {
|
|
346
|
-
return fields.reduce(
|
|
347
|
-
|
|
348
|
-
accum[name] = _this._visit(type, value[name], callback);
|
|
301
|
+
return fields.reduce((accum, { name, type }) => {
|
|
302
|
+
accum[name] = this._visit(type, value[name], callback);
|
|
349
303
|
return accum;
|
|
350
304
|
}, {});
|
|
351
305
|
}
|
|
352
|
-
return logger.throwArgumentError(
|
|
353
|
-
}
|
|
354
|
-
|
|
306
|
+
return logger.throwArgumentError(`unknown type: ${type}`, "type", type);
|
|
307
|
+
}
|
|
308
|
+
visit(value, callback) {
|
|
355
309
|
return this._visit(this.primaryType, value, callback);
|
|
356
|
-
}
|
|
357
|
-
|
|
310
|
+
}
|
|
311
|
+
static from(types) {
|
|
358
312
|
return new TypedDataEncoder(types);
|
|
359
|
-
}
|
|
360
|
-
|
|
313
|
+
}
|
|
314
|
+
static getPrimaryType(types) {
|
|
361
315
|
return TypedDataEncoder.from(types).primaryType;
|
|
362
|
-
}
|
|
363
|
-
|
|
316
|
+
}
|
|
317
|
+
static hashStruct(name, types, value) {
|
|
364
318
|
return TypedDataEncoder.from(types).hashStruct(name, value);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
for (
|
|
369
|
-
|
|
319
|
+
}
|
|
320
|
+
static hashDomain(domain) {
|
|
321
|
+
const domainFields = [];
|
|
322
|
+
for (const name in domain) {
|
|
323
|
+
const type = domainFieldTypes[name];
|
|
370
324
|
if (!type) {
|
|
371
|
-
logger.throwArgumentError(
|
|
325
|
+
logger.throwArgumentError(`invalid typed-data domain key: ${JSON.stringify(name)}`, "domain", domain);
|
|
372
326
|
}
|
|
373
|
-
domainFields.push({ name
|
|
327
|
+
domainFields.push({ name, type });
|
|
374
328
|
}
|
|
375
|
-
domainFields.sort(
|
|
329
|
+
domainFields.sort((a, b) => {
|
|
376
330
|
return domainFieldNames.indexOf(a.name) - domainFieldNames.indexOf(b.name);
|
|
377
331
|
});
|
|
378
332
|
return TypedDataEncoder.hashStruct("EIP712Domain", { EIP712Domain: domainFields }, domain);
|
|
379
|
-
}
|
|
380
|
-
|
|
333
|
+
}
|
|
334
|
+
static encode(domain, types, value) {
|
|
381
335
|
return (0, bytes_1.hexConcat)([
|
|
382
336
|
"0x1901",
|
|
383
337
|
TypedDataEncoder.hashDomain(domain),
|
|
384
338
|
TypedDataEncoder.from(types).hash(value)
|
|
385
339
|
]);
|
|
386
|
-
}
|
|
387
|
-
|
|
340
|
+
}
|
|
341
|
+
static hash(domain, types, value) {
|
|
388
342
|
return (0, keccak256_1.keccak256)(TypedDataEncoder.encode(domain, types, value));
|
|
389
|
-
}
|
|
343
|
+
}
|
|
390
344
|
// Replaces all address types with ENS names with their looked up address
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
ensCache[value] = "0x";
|
|
409
|
-
}
|
|
410
|
-
return value;
|
|
411
|
-
});
|
|
412
|
-
_a = [];
|
|
413
|
-
for (_b in ensCache)
|
|
414
|
-
_a.push(_b);
|
|
415
|
-
_i = 0;
|
|
416
|
-
_e.label = 1;
|
|
417
|
-
case 1:
|
|
418
|
-
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
419
|
-
name_4 = _a[_i];
|
|
420
|
-
_c = ensCache;
|
|
421
|
-
_d = name_4;
|
|
422
|
-
return [4 /*yield*/, resolveName(name_4)];
|
|
423
|
-
case 2:
|
|
424
|
-
_c[_d] = _e.sent();
|
|
425
|
-
_e.label = 3;
|
|
426
|
-
case 3:
|
|
427
|
-
_i++;
|
|
428
|
-
return [3 /*break*/, 1];
|
|
429
|
-
case 4:
|
|
430
|
-
// Replace the domain verifyingContract if needed
|
|
431
|
-
if (domain.verifyingContract && ensCache[domain.verifyingContract]) {
|
|
432
|
-
domain.verifyingContract = ensCache[domain.verifyingContract];
|
|
433
|
-
}
|
|
434
|
-
// Replace all ENS names with their address
|
|
435
|
-
value = encoder.visit(value, function (type, value) {
|
|
436
|
-
if (type === "address" && ensCache[value]) {
|
|
437
|
-
return ensCache[value];
|
|
438
|
-
}
|
|
439
|
-
return value;
|
|
440
|
-
});
|
|
441
|
-
return [2 /*return*/, { domain: domain, value: value }];
|
|
442
|
-
}
|
|
443
|
-
});
|
|
345
|
+
static async resolveNames(domain, types, value, resolveName) {
|
|
346
|
+
// Make a copy to isolate it from the object passed in
|
|
347
|
+
domain = (0, properties_1.shallowCopy)(domain);
|
|
348
|
+
// Look up all ENS names
|
|
349
|
+
const ensCache = {};
|
|
350
|
+
// Do we need to look up the domain's verifyingContract?
|
|
351
|
+
if (domain.verifyingContract && !(0, bytes_1.isHexString)(domain.verifyingContract, 20)) {
|
|
352
|
+
ensCache[domain.verifyingContract] = "0x";
|
|
353
|
+
}
|
|
354
|
+
// We are going to use the encoder to visit all the base values
|
|
355
|
+
const encoder = TypedDataEncoder.from(types);
|
|
356
|
+
// Get a list of all the addresses
|
|
357
|
+
encoder.visit(value, (type, value) => {
|
|
358
|
+
if (type === "address" && !(0, bytes_1.isHexString)(value, 20)) {
|
|
359
|
+
ensCache[value] = "0x";
|
|
360
|
+
}
|
|
361
|
+
return value;
|
|
444
362
|
});
|
|
445
|
-
|
|
446
|
-
|
|
363
|
+
// Lookup each name
|
|
364
|
+
for (const name in ensCache) {
|
|
365
|
+
ensCache[name] = await resolveName(name);
|
|
366
|
+
}
|
|
367
|
+
// Replace the domain verifyingContract if needed
|
|
368
|
+
if (domain.verifyingContract && ensCache[domain.verifyingContract]) {
|
|
369
|
+
domain.verifyingContract = ensCache[domain.verifyingContract];
|
|
370
|
+
}
|
|
371
|
+
// Replace all ENS names with their address
|
|
372
|
+
value = encoder.visit(value, (type, value) => {
|
|
373
|
+
if (type === "address" && ensCache[value]) {
|
|
374
|
+
return ensCache[value];
|
|
375
|
+
}
|
|
376
|
+
return value;
|
|
377
|
+
});
|
|
378
|
+
return { domain, value };
|
|
379
|
+
}
|
|
380
|
+
static getPayload(domain, types, value) {
|
|
447
381
|
// Validate the domain fields
|
|
448
382
|
TypedDataEncoder.hashDomain(domain);
|
|
449
383
|
// Derive the EIP712Domain Struct reference type
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
domainFieldNames.forEach(
|
|
453
|
-
|
|
384
|
+
const domainValues = {};
|
|
385
|
+
const domainTypes = [];
|
|
386
|
+
domainFieldNames.forEach((name) => {
|
|
387
|
+
const value = domain[name];
|
|
454
388
|
if (value == null) {
|
|
455
389
|
return;
|
|
456
390
|
}
|
|
457
391
|
domainValues[name] = domainChecks[name](value);
|
|
458
|
-
domainTypes.push({ name
|
|
392
|
+
domainTypes.push({ name, type: domainFieldTypes[name] });
|
|
459
393
|
});
|
|
460
|
-
|
|
461
|
-
|
|
394
|
+
const encoder = TypedDataEncoder.from(types);
|
|
395
|
+
const typesWithDomain = (0, properties_1.shallowCopy)(types);
|
|
462
396
|
if (typesWithDomain.EIP712Domain) {
|
|
463
397
|
logger.throwArgumentError("types must not contain EIP712Domain type", "types.EIP712Domain", types);
|
|
464
398
|
}
|
|
@@ -471,7 +405,7 @@ var TypedDataEncoder = /** @class */ (function () {
|
|
|
471
405
|
types: typesWithDomain,
|
|
472
406
|
domain: domainValues,
|
|
473
407
|
primaryType: encoder.primaryType,
|
|
474
|
-
message: encoder.visit(value,
|
|
408
|
+
message: encoder.visit(value, (type, value) => {
|
|
475
409
|
// bytes
|
|
476
410
|
if (type.match(/^bytes(\d*)/)) {
|
|
477
411
|
return (0, bytes_1.hexlify)((0, bytes_1.arrayify)(value));
|
|
@@ -487,15 +421,14 @@ var TypedDataEncoder = /** @class */ (function () {
|
|
|
487
421
|
return !!value;
|
|
488
422
|
case "string":
|
|
489
423
|
if (typeof (value) !== "string") {
|
|
490
|
-
logger.throwArgumentError(
|
|
424
|
+
logger.throwArgumentError(`invalid string`, "value", value);
|
|
491
425
|
}
|
|
492
426
|
return value;
|
|
493
427
|
}
|
|
494
428
|
return logger.throwArgumentError("unsupported type", "type", type);
|
|
495
429
|
})
|
|
496
430
|
};
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
}());
|
|
431
|
+
}
|
|
432
|
+
}
|
|
500
433
|
exports.TypedDataEncoder = TypedDataEncoder;
|
|
501
434
|
//# sourceMappingURL=typed-data.js.map
|