@qevm/hash 5.7.3 → 5.7.4

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