@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/README.md +4 -5
- package/lib/_version.d.ts +1 -1
- package/lib/_version.js +1 -1
- package/lib/ens-normalize/decoder.js +78 -78
- package/lib/ens-normalize/decoder.js.map +1 -1
- package/lib/ens-normalize/include.js +2 -2
- package/lib/ens-normalize/include.js.map +1 -1
- package/lib/ens-normalize/lib.js +38 -45
- package/lib/ens-normalize/lib.js.map +1 -1
- package/lib/id.js +2 -2
- package/lib/id.js.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -5
- package/lib/index.js.map +1 -1
- package/lib/message.d.ts +1 -1
- package/lib/message.d.ts.map +1 -1
- package/lib/message.js +6 -6
- package/lib/message.js.map +1 -1
- package/lib/namehash.d.ts.map +1 -1
- package/lib/namehash.js +23 -21
- package/lib/namehash.js.map +1 -1
- package/lib/typed-data.d.ts.map +1 -1
- package/lib/typed-data.js +215 -269
- package/lib/typed-data.js.map +1 -1
- package/package.json +34 -30
- package/src.ts/_version.ts +1 -1
- package/src.ts/index.ts +1 -5
- package/src.ts/message.ts +11 -8
- package/src.ts/namehash.ts +41 -22
- package/src.ts/typed-data.ts +269 -110
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
|
-
|
|
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("@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
|
-
|
|
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
|
-
salt: "bytes32"
|
|
34
|
+
salt: "bytes32",
|
|
71
35
|
};
|
|
72
|
-
|
|
73
|
-
"name",
|
|
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
|
|
78
|
-
logger.throwArgumentError(
|
|
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
|
-
|
|
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(
|
|
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(
|
|
66
|
+
return logger.throwArgumentError(`invalid domain value "verifyingContract"`, "domain.verifyingContract", value);
|
|
99
67
|
},
|
|
100
68
|
salt: function (value) {
|
|
101
69
|
try {
|
|
102
|
-
|
|
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(
|
|
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
|
-
|
|
83
|
+
const match = type.match(/^(u?)int(\d*)$/);
|
|
116
84
|
if (match) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (width % 8 !== 0 ||
|
|
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
|
-
|
|
123
|
-
|
|
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
|
-
|
|
126
|
-
if (v.lt(
|
|
127
|
-
logger.throwArgumentError(
|
|
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
|
-
|
|
107
|
+
const match = type.match(/^bytes(\d+)$/);
|
|
136
108
|
if (match) {
|
|
137
|
-
|
|
138
|
-
if (
|
|
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
|
-
|
|
143
|
-
if (bytes.length !==
|
|
144
|
-
logger.throwArgumentError(
|
|
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":
|
|
152
|
-
return
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
|
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
|
-
|
|
173
|
-
|
|
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
|
-
|
|
151
|
+
const links = {};
|
|
179
152
|
// Link structs to structs which contain them as a child
|
|
180
|
-
|
|
153
|
+
const parents = {};
|
|
181
154
|
// Link all subtypes within a given struct
|
|
182
|
-
|
|
183
|
-
Object.keys(types).forEach(
|
|
155
|
+
const subtypes = {};
|
|
156
|
+
Object.keys(types).forEach((type) => {
|
|
184
157
|
links[type] = {};
|
|
185
158
|
parents[type] = [];
|
|
186
159
|
subtypes[type] = {};
|
|
187
160
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
types[
|
|
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(
|
|
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
|
-
|
|
198
|
-
if (baseType ===
|
|
199
|
-
logger.throwArgumentError(
|
|
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
|
-
|
|
175
|
+
const encoder = getBaseEncoder(baseType);
|
|
203
176
|
if (encoder) {
|
|
204
177
|
return;
|
|
205
178
|
}
|
|
206
179
|
if (!parents[baseType]) {
|
|
207
|
-
logger.throwArgumentError(
|
|
180
|
+
logger.throwArgumentError(`unknown type ${JSON.stringify(baseType)}`, "types", types);
|
|
208
181
|
}
|
|
209
182
|
// Add linkage
|
|
210
|
-
parents[baseType].push(
|
|
211
|
-
links[
|
|
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
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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 (
|
|
248
|
-
|
|
217
|
+
for (const name in subtypes) {
|
|
218
|
+
const st = Object.keys(subtypes[name]);
|
|
249
219
|
st.sort();
|
|
250
|
-
this._types[
|
|
220
|
+
this._types[name] =
|
|
221
|
+
encodeType(name, types[name]) +
|
|
222
|
+
st.map((t) => encodeType(t, types[t])).join("");
|
|
251
223
|
}
|
|
252
224
|
}
|
|
253
|
-
|
|
254
|
-
|
|
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
|
-
|
|
261
|
-
var _this = this;
|
|
231
|
+
}
|
|
232
|
+
_getEncoder(type) {
|
|
262
233
|
// Basic encoder type (address, bool, uint256, etc)
|
|
263
234
|
{
|
|
264
|
-
|
|
235
|
+
const encoder = getBaseEncoder(type);
|
|
265
236
|
if (encoder) {
|
|
266
237
|
return encoder;
|
|
267
238
|
}
|
|
268
239
|
}
|
|
269
240
|
// Array
|
|
270
|
-
|
|
241
|
+
const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
|
|
271
242
|
if (match) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return
|
|
276
|
-
if (
|
|
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
|
-
|
|
280
|
-
if (
|
|
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
|
-
|
|
258
|
+
const fields = this.types[type];
|
|
288
259
|
if (fields) {
|
|
289
|
-
|
|
290
|
-
return
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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(
|
|
269
|
+
values.unshift(encodedType);
|
|
300
270
|
return (0, bytes_1.hexConcat)(values);
|
|
301
271
|
};
|
|
302
272
|
}
|
|
303
|
-
return logger.throwArgumentError(
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
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(
|
|
278
|
+
logger.throwArgumentError(`unknown type: ${JSON.stringify(name)}`, "name", name);
|
|
309
279
|
}
|
|
310
280
|
return result;
|
|
311
|
-
}
|
|
312
|
-
|
|
281
|
+
}
|
|
282
|
+
encodeData(type, value) {
|
|
313
283
|
return this.getEncoder(type)(value);
|
|
314
|
-
}
|
|
315
|
-
|
|
284
|
+
}
|
|
285
|
+
hashStruct(name, value) {
|
|
316
286
|
return (0, keccak256_1.keccak256)(this.encodeData(name, value));
|
|
317
|
-
}
|
|
318
|
-
|
|
287
|
+
}
|
|
288
|
+
encode(value) {
|
|
319
289
|
return this.encodeData(this.primaryType, value);
|
|
320
|
-
}
|
|
321
|
-
|
|
290
|
+
}
|
|
291
|
+
hash(value) {
|
|
322
292
|
return this.hashStruct(this.primaryType, value);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
var _this = this;
|
|
293
|
+
}
|
|
294
|
+
_visit(type, value, callback) {
|
|
326
295
|
// Basic encoder type (address, bool, uint256, etc)
|
|
327
296
|
{
|
|
328
|
-
|
|
297
|
+
const encoder = getBaseEncoder(type);
|
|
329
298
|
if (encoder) {
|
|
330
299
|
return callback(type, value);
|
|
331
300
|
}
|
|
332
301
|
}
|
|
333
302
|
// Array
|
|
334
|
-
|
|
303
|
+
const match = type.match(/^(.*)(\x5b(\d*)\x5d)$/);
|
|
335
304
|
if (match) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if (
|
|
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(
|
|
310
|
+
return value.map((v) => this._visit(subtype, v, callback));
|
|
342
311
|
}
|
|
343
312
|
// Struct
|
|
344
|
-
|
|
313
|
+
const fields = this.types[type];
|
|
345
314
|
if (fields) {
|
|
346
|
-
return fields.reduce(
|
|
347
|
-
|
|
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(
|
|
353
|
-
}
|
|
354
|
-
|
|
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
|
-
|
|
324
|
+
}
|
|
325
|
+
static from(types) {
|
|
358
326
|
return new TypedDataEncoder(types);
|
|
359
|
-
}
|
|
360
|
-
|
|
327
|
+
}
|
|
328
|
+
static getPrimaryType(types) {
|
|
361
329
|
return TypedDataEncoder.from(types).primaryType;
|
|
362
|
-
}
|
|
363
|
-
|
|
330
|
+
}
|
|
331
|
+
static hashStruct(name, types, value) {
|
|
364
332
|
return TypedDataEncoder.from(types).hashStruct(name, value);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
for (
|
|
369
|
-
|
|
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(
|
|
339
|
+
logger.throwArgumentError(`invalid typed-data domain key: ${JSON.stringify(name)}`, "domain", domain);
|
|
372
340
|
}
|
|
373
|
-
domainFields.push({ name
|
|
341
|
+
domainFields.push({ name, type });
|
|
374
342
|
}
|
|
375
|
-
domainFields.sort(
|
|
376
|
-
return domainFieldNames.indexOf(a.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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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
|
-
|
|
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
|
-
|
|
454
|
-
|
|
455
|
-
domainFieldNames.forEach(
|
|
456
|
-
|
|
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
|
|
408
|
+
domainTypes.push({ name, type: domainFieldTypes[name] });
|
|
462
409
|
});
|
|
463
|
-
|
|
464
|
-
|
|
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,
|
|
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
|
|
493
|
-
logger.throwArgumentError(
|
|
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
|
-
|
|
502
|
-
}());
|
|
447
|
+
}
|
|
448
|
+
}
|
|
503
449
|
exports.TypedDataEncoder = TypedDataEncoder;
|
|
504
450
|
//# sourceMappingURL=typed-data.js.map
|