@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/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
- var address_1 = require("@qevm/address");
41
- var bignumber_1 = require("@ethersproject/bignumber");
42
- var bytes_1 = require("@qevm/bytes");
43
- var keccak256_1 = require("@ethersproject/keccak256");
44
- var properties_1 = require("@ethersproject/properties");
45
- var logger_1 = require("@ethersproject/logger");
46
- var _version_1 = require("./_version");
47
- var logger = new logger_1.Logger(_version_1.version);
48
- var id_1 = require("./id");
49
- var padding = new Uint8Array(32);
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
- var NegativeOne = bignumber_1.BigNumber.from(-1);
52
- var Zero = bignumber_1.BigNumber.from(0);
53
- var One = bignumber_1.BigNumber.from(1);
54
- var MaxUint256 = bignumber_1.BigNumber.from("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
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
- var bytes = (0, bytes_1.arrayify)(value);
57
- var padOffset = bytes.length % 32;
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
- var hexTrue = (0, bytes_1.hexZeroPad)(One.toHexString(), 32);
64
- var hexFalse = (0, bytes_1.hexZeroPad)(Zero.toHexString(), 32);
65
- var domainFieldTypes = {
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
- var domainFieldNames = [
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("invalid domain value for " + JSON.stringify(key), "domain." + key, value);
42
+ logger.throwArgumentError(`invalid domain value for ${JSON.stringify(key)}`, `domain.${key}`, value);
79
43
  }
80
44
  return value;
81
45
  };
82
46
  }
83
- var domainChecks = {
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("invalid domain value for \"chainId\"", "domain.chainId", value);
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("invalid domain value \"verifyingContract\"", "domain.verifyingContract", value);
62
+ return logger.throwArgumentError(`invalid domain value "verifyingContract"`, "domain.verifyingContract", value);
99
63
  },
100
64
  salt: function (value) {
101
65
  try {
102
- var bytes = (0, bytes_1.arrayify)(value);
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("invalid domain value \"salt\"", "domain.salt", value);
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
- var match = type.match(/^(u?)int(\d*)$/);
79
+ const match = type.match(/^(u?)int(\d*)$/);
116
80
  if (match) {
117
- var signed = (match[1] === "");
118
- var width = parseInt(match[2] || "256");
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
- var boundsUpper_1 = MaxUint256.mask(signed ? (width - 1) : width);
123
- var boundsLower_1 = signed ? boundsUpper_1.add(One).mul(NegativeOne) : Zero;
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
- var v = bignumber_1.BigNumber.from(value);
126
- if (v.lt(boundsLower_1) || v.gt(boundsUpper_1)) {
127
- logger.throwArgumentError("value out-of-bounds for " + type, "value", value);
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
- var match = type.match(/^bytes(\d+)$/);
99
+ const match = type.match(/^bytes(\d+)$/);
136
100
  if (match) {
137
- var width_1 = parseInt(match[1]);
138
- if (width_1 === 0 || width_1 > 32 || match[1] !== String(width_1)) {
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
- var bytes = (0, bytes_1.arrayify)(value);
143
- if (bytes.length !== width_1) {
144
- logger.throwArgumentError("invalid length for " + type, "value", value);
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 + "(" + fields.map(function (_a) {
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
- var TypedDataEncoder = /** @class */ (function () {
173
- function TypedDataEncoder(types) {
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
- var links = {};
139
+ const links = {};
179
140
  // Link structs to structs which contain them as a child
180
- var parents = {};
141
+ const parents = {};
181
142
  // Link all subtypes within a given struct
182
- var subtypes = {};
183
- Object.keys(types).forEach(function (type) {
143
+ const subtypes = {};
144
+ Object.keys(types).forEach((type) => {
184
145
  links[type] = {};
185
146
  parents[type] = [];
186
147
  subtypes[type] = {};
187
148
  });
188
- var _loop_1 = function (name_1) {
189
- var uniqueNames = {};
190
- types[name_1].forEach(function (field) {
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("duplicate variable name " + JSON.stringify(field.name) + " in " + JSON.stringify(name_1), "types", types);
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
- var baseType = field.type.match(/^([^\x5b]*)(\x5b|$)/)[1];
198
- if (baseType === name_1) {
199
- logger.throwArgumentError("circular type reference to " + JSON.stringify(baseType), "types", types);
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
- var encoder = getBaseEncoder(baseType);
163
+ const encoder = getBaseEncoder(baseType);
203
164
  if (encoder) {
204
165
  return;
205
166
  }
206
167
  if (!parents[baseType]) {
207
- logger.throwArgumentError("unknown type " + JSON.stringify(baseType), "types", types);
168
+ logger.throwArgumentError(`unknown type ${JSON.stringify(baseType)}`, "types", types);
208
169
  }
209
170
  // Add linkage
210
- parents[baseType].push(name_1);
211
- links[name_1][baseType] = true;
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
- var primaryTypes = Object.keys(parents).filter(function (n) { return (parents[n].length === 0); });
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("ambiguous primary types or unused types: " + primaryTypes.map(function (t) { return (JSON.stringify(t)); }).join(", "), "types", types);
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("circular type reference to " + JSON.stringify(type), "types", types);
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(function (child) {
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(function (subtype) {
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 (var name_2 in subtypes) {
248
- var st = Object.keys(subtypes[name_2]);
205
+ for (const name in subtypes) {
206
+ const st = Object.keys(subtypes[name]);
249
207
  st.sort();
250
- this._types[name_2] = encodeType(name_2, types[name_2]) + st.map(function (t) { return encodeType(t, types[t]); }).join("");
208
+ this._types[name] = encodeType(name, types[name]) + st.map((t) => encodeType(t, types[t])).join("");
251
209
  }
252
210
  }
253
- TypedDataEncoder.prototype.getEncoder = function (type) {
254
- var encoder = this._encoderCache[type];
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
- TypedDataEncoder.prototype._getEncoder = function (type) {
261
- var _this = this;
217
+ }
218
+ _getEncoder(type) {
262
219
  // Basic encoder type (address, bool, uint256, etc)
263
220
  {
264
- var encoder = getBaseEncoder(type);
221
+ const encoder = getBaseEncoder(type);
265
222
  if (encoder) {
266
223
  return encoder;
267
224
  }
268
225
  }
269
226
  // Array
270
- var match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
227
+ const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
271
228
  if (match) {
272
- var subtype_1 = match[1];
273
- var subEncoder_1 = this.getEncoder(subtype_1);
274
- var length_1 = parseInt(match[3]);
275
- return function (value) {
276
- if (length_1 >= 0 && value.length !== length_1) {
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
- var result = value.map(subEncoder_1);
280
- if (_this._types[subtype_1]) {
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
- var fields = this.types[type];
244
+ const fields = this.types[type];
288
245
  if (fields) {
289
- var encodedType_1 = (0, id_1.id)(this._types[type]);
290
- return function (value) {
291
- var values = fields.map(function (_a) {
292
- var name = _a.name, type = _a.type;
293
- var result = _this.getEncoder(type)(value[name]);
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(encodedType_1);
255
+ values.unshift(encodedType);
300
256
  return (0, bytes_1.hexConcat)(values);
301
257
  };
302
258
  }
303
- return logger.throwArgumentError("unknown type: " + type, "type", type);
304
- };
305
- TypedDataEncoder.prototype.encodeType = function (name) {
306
- var result = this._types[name];
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("unknown type: " + JSON.stringify(name), "name", name);
264
+ logger.throwArgumentError(`unknown type: ${JSON.stringify(name)}`, "name", name);
309
265
  }
310
266
  return result;
311
- };
312
- TypedDataEncoder.prototype.encodeData = function (type, value) {
267
+ }
268
+ encodeData(type, value) {
313
269
  return this.getEncoder(type)(value);
314
- };
315
- TypedDataEncoder.prototype.hashStruct = function (name, value) {
270
+ }
271
+ hashStruct(name, value) {
316
272
  return (0, keccak256_1.keccak256)(this.encodeData(name, value));
317
- };
318
- TypedDataEncoder.prototype.encode = function (value) {
273
+ }
274
+ encode(value) {
319
275
  return this.encodeData(this.primaryType, value);
320
- };
321
- TypedDataEncoder.prototype.hash = function (value) {
276
+ }
277
+ hash(value) {
322
278
  return this.hashStruct(this.primaryType, value);
323
- };
324
- TypedDataEncoder.prototype._visit = function (type, value, callback) {
325
- var _this = this;
279
+ }
280
+ _visit(type, value, callback) {
326
281
  // Basic encoder type (address, bool, uint256, etc)
327
282
  {
328
- var encoder = getBaseEncoder(type);
283
+ const encoder = getBaseEncoder(type);
329
284
  if (encoder) {
330
285
  return callback(type, value);
331
286
  }
332
287
  }
333
288
  // Array
334
- var match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
289
+ const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
335
290
  if (match) {
336
- var subtype_2 = match[1];
337
- var length_2 = parseInt(match[3]);
338
- if (length_2 >= 0 && value.length !== length_2) {
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(function (v) { return _this._visit(subtype_2, v, callback); });
296
+ return value.map((v) => this._visit(subtype, v, callback));
342
297
  }
343
298
  // Struct
344
- var fields = this.types[type];
299
+ const fields = this.types[type];
345
300
  if (fields) {
346
- return fields.reduce(function (accum, _a) {
347
- var name = _a.name, type = _a.type;
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("unknown type: " + type, "type", type);
353
- };
354
- TypedDataEncoder.prototype.visit = function (value, callback) {
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
- TypedDataEncoder.from = function (types) {
310
+ }
311
+ static from(types) {
358
312
  return new TypedDataEncoder(types);
359
- };
360
- TypedDataEncoder.getPrimaryType = function (types) {
313
+ }
314
+ static getPrimaryType(types) {
361
315
  return TypedDataEncoder.from(types).primaryType;
362
- };
363
- TypedDataEncoder.hashStruct = function (name, types, value) {
316
+ }
317
+ static hashStruct(name, types, value) {
364
318
  return TypedDataEncoder.from(types).hashStruct(name, value);
365
- };
366
- TypedDataEncoder.hashDomain = function (domain) {
367
- var domainFields = [];
368
- for (var name_3 in domain) {
369
- var type = domainFieldTypes[name_3];
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("invalid typed-data domain key: " + JSON.stringify(name_3), "domain", domain);
325
+ logger.throwArgumentError(`invalid typed-data domain key: ${JSON.stringify(name)}`, "domain", domain);
372
326
  }
373
- domainFields.push({ name: name_3, type: type });
327
+ domainFields.push({ name, type });
374
328
  }
375
- domainFields.sort(function (a, b) {
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
- TypedDataEncoder.encode = function (domain, types, value) {
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
- TypedDataEncoder.hash = function (domain, types, value) {
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
- TypedDataEncoder.resolveNames = function (domain, types, value, resolveName) {
392
- return __awaiter(this, void 0, void 0, function () {
393
- var ensCache, encoder, _a, _b, _i, name_4, _c, _d;
394
- return __generator(this, function (_e) {
395
- switch (_e.label) {
396
- case 0:
397
- // Make a copy to isolate it from the object passed in
398
- domain = (0, properties_1.shallowCopy)(domain);
399
- ensCache = {};
400
- // Do we need to look up the domain's verifyingContract?
401
- if (domain.verifyingContract && !(0, bytes_1.isHexString)(domain.verifyingContract, 20)) {
402
- ensCache[domain.verifyingContract] = "0x";
403
- }
404
- encoder = TypedDataEncoder.from(types);
405
- // Get a list of all the addresses
406
- encoder.visit(value, function (type, value) {
407
- if (type === "address" && !(0, bytes_1.isHexString)(value, 20)) {
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
- TypedDataEncoder.getPayload = function (domain, types, value) {
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
- var domainValues = {};
451
- var domainTypes = [];
452
- domainFieldNames.forEach(function (name) {
453
- var value = domain[name];
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: name, type: domainFieldTypes[name] });
392
+ domainTypes.push({ name, type: domainFieldTypes[name] });
459
393
  });
460
- var encoder = TypedDataEncoder.from(types);
461
- var typesWithDomain = (0, properties_1.shallowCopy)(types);
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, function (type, 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("invalid string", "value", value);
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
- return TypedDataEncoder;
499
- }());
431
+ }
432
+ }
500
433
  exports.TypedDataEncoder = TypedDataEncoder;
501
434
  //# sourceMappingURL=typed-data.js.map