@qevm/hash 5.7.2 → 5.7.3

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