@fastnear/wallet-adapter 0.9.13 → 0.10.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/README.md +24 -0
- package/dist/cjs/actions.cjs +2 -2
- package/dist/cjs/errors.cjs +2 -2
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/meteor.cjs +3 -3
- package/dist/cjs/meteor.cjs.map +1 -1
- package/dist/cjs/near-mobile.cjs +11 -3
- package/dist/cjs/near-mobile.cjs.map +1 -1
- package/dist/cjs/polling.cjs +2 -2
- package/dist/cjs/rpc.cjs +2 -2
- package/dist/cjs/storage.cjs +2 -2
- package/dist/cjs/types.cjs +2 -2
- package/dist/esm/actions.js +2 -2
- package/dist/esm/errors.js +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/meteor.js +3 -3
- package/dist/esm/meteor.js.map +1 -1
- package/dist/esm/near-mobile.js +12 -3
- package/dist/esm/near-mobile.js.map +1 -1
- package/dist/esm/polling.js +2 -2
- package/dist/esm/rpc.js +2 -2
- package/dist/esm/storage.js +2 -2
- package/dist/esm/types.js +2 -2
- package/dist/umd/browser.global.js +1370 -1063
- package/dist/umd/browser.global.js.map +1 -1
- package/package.json +7 -7
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/* ⋈ 🏃🏻💨 FastNear Wallet Adapters - IIFE/UMD (@fastnear/wallet-adapter version 0.
|
|
2
|
-
/* https://www.npmjs.com/package/@fastnear/wallet-adapter/v/0.
|
|
1
|
+
/* ⋈ 🏃🏻💨 FastNear Wallet Adapters - IIFE/UMD (@fastnear/wallet-adapter version 0.10.1) */
|
|
2
|
+
/* https://www.npmjs.com/package/@fastnear/wallet-adapter/v/0.10.1 */
|
|
3
3
|
"use strict";
|
|
4
4
|
var nearWalletAdapters = (() => {
|
|
5
5
|
var __defProp = Object.defineProperty;
|
|
@@ -32,523 +32,147 @@ var nearWalletAdapters = (() => {
|
|
|
32
32
|
isUserRejectedError: () => isUserRejectedError
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
//
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
}()
|
|
76
|
-
);
|
|
77
|
-
var DecodeBuffer = (
|
|
78
|
-
/** @class */
|
|
79
|
-
function() {
|
|
80
|
-
function DecodeBuffer2(buf) {
|
|
81
|
-
this.offset = 0;
|
|
82
|
-
this.buffer_size = buf.length;
|
|
83
|
-
this.buffer = new ArrayBuffer(buf.length);
|
|
84
|
-
new Uint8Array(this.buffer).set(buf);
|
|
85
|
-
this.view = new DataView(this.buffer);
|
|
86
|
-
}
|
|
87
|
-
__name(DecodeBuffer2, "DecodeBuffer");
|
|
88
|
-
DecodeBuffer2.prototype.assert_enough_buffer = function(size) {
|
|
89
|
-
if (this.offset + size > this.buffer.byteLength) {
|
|
90
|
-
throw new Error("Error in schema, the buffer is smaller than expected");
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
DecodeBuffer2.prototype.consume_value = function(type) {
|
|
94
|
-
var bSize = type.substring(1);
|
|
95
|
-
var size = parseInt(bSize) / 8;
|
|
96
|
-
this.assert_enough_buffer(size);
|
|
97
|
-
var toCall = type[0] === "f" ? "getFloat".concat(bSize) : type[0] === "i" ? "getInt".concat(bSize) : "getUint".concat(bSize);
|
|
98
|
-
var ret = this.view[toCall](this.offset, true);
|
|
99
|
-
this.offset += size;
|
|
100
|
-
return ret;
|
|
101
|
-
};
|
|
102
|
-
DecodeBuffer2.prototype.consume_bytes = function(size) {
|
|
103
|
-
this.assert_enough_buffer(size);
|
|
104
|
-
var ret = this.buffer.slice(this.offset, this.offset + size);
|
|
105
|
-
this.offset += size;
|
|
106
|
-
return ret;
|
|
107
|
-
};
|
|
108
|
-
return DecodeBuffer2;
|
|
109
|
-
}()
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
// ../../node_modules/borsh/lib/esm/utils.js
|
|
113
|
-
var __extends = /* @__PURE__ */ function() {
|
|
114
|
-
var extendStatics = /* @__PURE__ */ __name(function(d, b) {
|
|
115
|
-
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(d2, b2) {
|
|
116
|
-
d2.__proto__ = b2;
|
|
117
|
-
} || function(d2, b2) {
|
|
118
|
-
for (var p in b2) if (Object.prototype.hasOwnProperty.call(b2, p)) d2[p] = b2[p];
|
|
119
|
-
};
|
|
120
|
-
return extendStatics(d, b);
|
|
121
|
-
}, "extendStatics");
|
|
122
|
-
return function(d, b) {
|
|
123
|
-
if (typeof b !== "function" && b !== null)
|
|
124
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
125
|
-
extendStatics(d, b);
|
|
126
|
-
function __() {
|
|
127
|
-
this.constructor = d;
|
|
128
|
-
}
|
|
129
|
-
__name(__, "__");
|
|
130
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
131
|
-
};
|
|
132
|
-
}();
|
|
133
|
-
function isArrayLike(value) {
|
|
134
|
-
return Array.isArray(value) || !!value && typeof value === "object" && "length" in value && typeof value.length === "number" && (value.length === 0 || value.length > 0 && value.length - 1 in value);
|
|
135
|
-
}
|
|
136
|
-
__name(isArrayLike, "isArrayLike");
|
|
137
|
-
function expect_type(value, type, fieldPath) {
|
|
138
|
-
if (typeof value !== type) {
|
|
139
|
-
throw new Error("Expected ".concat(type, " not ").concat(typeof value, "(").concat(value, ") at ").concat(fieldPath.join(".")));
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
__name(expect_type, "expect_type");
|
|
143
|
-
function expect_bigint(value, fieldPath) {
|
|
144
|
-
var basicType = ["number", "string", "bigint", "boolean"].includes(typeof value);
|
|
145
|
-
var strObject = typeof value === "object" && value !== null && "toString" in value;
|
|
146
|
-
if (!basicType && !strObject) {
|
|
147
|
-
throw new Error("Expected bigint, number, boolean or string not ".concat(typeof value, "(").concat(value, ") at ").concat(fieldPath.join(".")));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
__name(expect_bigint, "expect_bigint");
|
|
151
|
-
function expect_same_size(length, expected, fieldPath) {
|
|
152
|
-
if (length !== expected) {
|
|
153
|
-
throw new Error("Array length ".concat(length, " does not match schema length ").concat(expected, " at ").concat(fieldPath.join(".")));
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
__name(expect_same_size, "expect_same_size");
|
|
157
|
-
function expect_enum(value, fieldPath) {
|
|
158
|
-
if (typeof value !== "object" || value === null) {
|
|
159
|
-
throw new Error("Expected object not ".concat(typeof value, "(").concat(value, ") at ").concat(fieldPath.join(".")));
|
|
35
|
+
// ../borsh/src/index.ts
|
|
36
|
+
var EncodeBuffer = class {
|
|
37
|
+
static {
|
|
38
|
+
__name(this, "EncodeBuffer");
|
|
39
|
+
}
|
|
40
|
+
offset = 0;
|
|
41
|
+
bufferSize = 256;
|
|
42
|
+
buffer = new ArrayBuffer(this.bufferSize);
|
|
43
|
+
view = new DataView(this.buffer);
|
|
44
|
+
resize(needed) {
|
|
45
|
+
if (this.bufferSize - this.offset < needed) {
|
|
46
|
+
this.bufferSize = Math.max(this.bufferSize * 2, this.bufferSize + needed);
|
|
47
|
+
const next = new ArrayBuffer(this.bufferSize);
|
|
48
|
+
new Uint8Array(next).set(new Uint8Array(this.buffer));
|
|
49
|
+
this.buffer = next;
|
|
50
|
+
this.view = new DataView(next);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
storeU8(v) {
|
|
54
|
+
this.resize(1);
|
|
55
|
+
this.view.setUint8(this.offset, v);
|
|
56
|
+
this.offset += 1;
|
|
57
|
+
}
|
|
58
|
+
storeU16(v) {
|
|
59
|
+
this.resize(2);
|
|
60
|
+
this.view.setUint16(this.offset, v, true);
|
|
61
|
+
this.offset += 2;
|
|
62
|
+
}
|
|
63
|
+
storeU32(v) {
|
|
64
|
+
this.resize(4);
|
|
65
|
+
this.view.setUint32(this.offset, v, true);
|
|
66
|
+
this.offset += 4;
|
|
67
|
+
}
|
|
68
|
+
storeBytes(from) {
|
|
69
|
+
this.resize(from.length);
|
|
70
|
+
new Uint8Array(this.buffer).set(from, this.offset);
|
|
71
|
+
this.offset += from.length;
|
|
72
|
+
}
|
|
73
|
+
result() {
|
|
74
|
+
return new Uint8Array(this.buffer).slice(0, this.offset);
|
|
160
75
|
}
|
|
76
|
+
};
|
|
77
|
+
function encodeBigint(buf, value, byteLen) {
|
|
78
|
+
const out = new Uint8Array(byteLen);
|
|
79
|
+
let v = value;
|
|
80
|
+
for (let i = 0; i < byteLen; i++) {
|
|
81
|
+
out[i] = Number(v & 0xffn);
|
|
82
|
+
v >>= 8n;
|
|
83
|
+
}
|
|
84
|
+
buf.storeBytes(out);
|
|
161
85
|
}
|
|
162
|
-
__name(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
function validate_schema(schema) {
|
|
178
|
-
if (typeof schema === "string" && VALID_STRING_TYPES.includes(schema)) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
if (schema && typeof schema === "object") {
|
|
182
|
-
var keys = Object.keys(schema);
|
|
183
|
-
if (keys.length === 1 && VALID_OBJECT_KEYS.includes(keys[0])) {
|
|
184
|
-
var key = keys[0];
|
|
185
|
-
if (key === "option")
|
|
186
|
-
return validate_schema(schema[key]);
|
|
187
|
-
if (key === "enum")
|
|
188
|
-
return validate_enum_schema(schema[key]);
|
|
189
|
-
if (key === "array")
|
|
190
|
-
return validate_array_schema(schema[key]);
|
|
191
|
-
if (key === "set")
|
|
192
|
-
return validate_schema(schema[key]);
|
|
193
|
-
if (key === "map")
|
|
194
|
-
return validate_map_schema(schema[key]);
|
|
195
|
-
if (key === "struct")
|
|
196
|
-
return validate_struct_schema(schema[key]);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
throw new ErrorSchema(schema, VALID_OBJECT_KEYS.join(", ") + " or " + VALID_STRING_TYPES.join(", "));
|
|
200
|
-
}
|
|
201
|
-
__name(validate_schema, "validate_schema");
|
|
202
|
-
function validate_enum_schema(schema) {
|
|
203
|
-
if (!Array.isArray(schema))
|
|
204
|
-
throw new ErrorSchema(schema, "Array");
|
|
205
|
-
for (var _i = 0, schema_1 = schema; _i < schema_1.length; _i++) {
|
|
206
|
-
var sch = schema_1[_i];
|
|
207
|
-
if (typeof sch !== "object" || !("struct" in sch)) {
|
|
208
|
-
throw new Error('Missing "struct" key in enum schema');
|
|
209
|
-
}
|
|
210
|
-
if (typeof sch.struct !== "object" || Object.keys(sch.struct).length !== 1) {
|
|
211
|
-
throw new Error('The "struct" in each enum must have a single key');
|
|
86
|
+
__name(encodeBigint, "encodeBigint");
|
|
87
|
+
function utf8Encode(str) {
|
|
88
|
+
const bytes = [];
|
|
89
|
+
for (let i = 0; i < str.length; i++) {
|
|
90
|
+
let c = str.charCodeAt(i);
|
|
91
|
+
if (c < 128) {
|
|
92
|
+
bytes.push(c);
|
|
93
|
+
} else if (c < 2048) {
|
|
94
|
+
bytes.push(192 | c >> 6, 128 | c & 63);
|
|
95
|
+
} else if (c < 55296 || c >= 57344) {
|
|
96
|
+
bytes.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
|
|
97
|
+
} else {
|
|
98
|
+
i++;
|
|
99
|
+
c = 65536 + ((c & 1023) << 10 | str.charCodeAt(i) & 1023);
|
|
100
|
+
bytes.push(240 | c >> 18, 128 | c >> 12 & 63, 128 | c >> 6 & 63, 128 | c & 63);
|
|
212
101
|
}
|
|
213
|
-
validate_schema({ struct: sch.struct });
|
|
214
102
|
}
|
|
103
|
+
return new Uint8Array(bytes);
|
|
215
104
|
}
|
|
216
|
-
__name(
|
|
217
|
-
function
|
|
218
|
-
if (typeof schema
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
__name(validate_map_schema, "validate_map_schema");
|
|
237
|
-
function validate_struct_schema(schema) {
|
|
238
|
-
if (typeof schema !== "object")
|
|
239
|
-
throw new ErrorSchema(schema, "object");
|
|
240
|
-
for (var key in schema) {
|
|
241
|
-
validate_schema(schema[key]);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
__name(validate_struct_schema, "validate_struct_schema");
|
|
245
|
-
|
|
246
|
-
// ../../node_modules/borsh/lib/esm/serialize.js
|
|
247
|
-
var BorshSerializer = (
|
|
248
|
-
/** @class */
|
|
249
|
-
function() {
|
|
250
|
-
function BorshSerializer2(checkTypes) {
|
|
251
|
-
this.encoded = new EncodeBuffer();
|
|
252
|
-
this.fieldPath = ["value"];
|
|
253
|
-
this.checkTypes = checkTypes;
|
|
254
|
-
}
|
|
255
|
-
__name(BorshSerializer2, "BorshSerializer");
|
|
256
|
-
BorshSerializer2.prototype.encode = function(value, schema) {
|
|
257
|
-
this.encode_value(value, schema);
|
|
258
|
-
return this.encoded.get_used_buffer();
|
|
259
|
-
};
|
|
260
|
-
BorshSerializer2.prototype.encode_value = function(value, schema) {
|
|
261
|
-
if (typeof schema === "string") {
|
|
262
|
-
if (integers.includes(schema))
|
|
263
|
-
return this.encode_integer(value, schema);
|
|
264
|
-
if (schema === "string")
|
|
265
|
-
return this.encode_string(value);
|
|
266
|
-
if (schema === "bool")
|
|
267
|
-
return this.encode_boolean(value);
|
|
268
|
-
}
|
|
269
|
-
if (typeof schema === "object") {
|
|
270
|
-
if ("option" in schema)
|
|
271
|
-
return this.encode_option(value, schema);
|
|
272
|
-
if ("enum" in schema)
|
|
273
|
-
return this.encode_enum(value, schema);
|
|
274
|
-
if ("array" in schema)
|
|
275
|
-
return this.encode_array(value, schema);
|
|
276
|
-
if ("set" in schema)
|
|
277
|
-
return this.encode_set(value, schema);
|
|
278
|
-
if ("map" in schema)
|
|
279
|
-
return this.encode_map(value, schema);
|
|
280
|
-
if ("struct" in schema)
|
|
281
|
-
return this.encode_struct(value, schema);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
BorshSerializer2.prototype.encode_integer = function(value, schema) {
|
|
285
|
-
var size = parseInt(schema.substring(1));
|
|
286
|
-
if (size <= 32 || schema == "f64") {
|
|
287
|
-
this.checkTypes && expect_type(value, "number", this.fieldPath);
|
|
288
|
-
this.encoded.store_value(value, schema);
|
|
289
|
-
} else {
|
|
290
|
-
this.checkTypes && expect_bigint(value, this.fieldPath);
|
|
291
|
-
this.encode_bigint(BigInt(value), size);
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
BorshSerializer2.prototype.encode_bigint = function(value, size) {
|
|
295
|
-
var buffer_len = size / 8;
|
|
296
|
-
var buffer = new Uint8Array(buffer_len);
|
|
297
|
-
for (var i = 0; i < buffer_len; i++) {
|
|
298
|
-
buffer[i] = Number(value & BigInt(255));
|
|
299
|
-
value = value >> BigInt(8);
|
|
105
|
+
__name(utf8Encode, "utf8Encode");
|
|
106
|
+
function encodeValue(buf, value, schema) {
|
|
107
|
+
if (typeof schema === "string") {
|
|
108
|
+
switch (schema) {
|
|
109
|
+
case "u8":
|
|
110
|
+
return buf.storeU8(value);
|
|
111
|
+
case "u16":
|
|
112
|
+
return buf.storeU16(value);
|
|
113
|
+
case "u32":
|
|
114
|
+
return buf.storeU32(value);
|
|
115
|
+
case "u64":
|
|
116
|
+
return encodeBigint(buf, BigInt(value), 8);
|
|
117
|
+
case "u128":
|
|
118
|
+
return encodeBigint(buf, BigInt(value), 16);
|
|
119
|
+
case "string": {
|
|
120
|
+
const encoded = utf8Encode(value);
|
|
121
|
+
buf.storeU32(encoded.length);
|
|
122
|
+
buf.storeBytes(encoded);
|
|
123
|
+
return;
|
|
300
124
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
var _value = value;
|
|
306
|
-
var utf8Bytes = [];
|
|
307
|
-
for (var i = 0; i < _value.length; i++) {
|
|
308
|
-
var charCode = _value.charCodeAt(i);
|
|
309
|
-
if (charCode < 128) {
|
|
310
|
-
utf8Bytes.push(charCode);
|
|
311
|
-
} else if (charCode < 2048) {
|
|
312
|
-
utf8Bytes.push(192 | charCode >> 6, 128 | charCode & 63);
|
|
313
|
-
} else if (charCode < 55296 || charCode >= 57344) {
|
|
314
|
-
utf8Bytes.push(224 | charCode >> 12, 128 | charCode >> 6 & 63, 128 | charCode & 63);
|
|
315
|
-
} else {
|
|
316
|
-
i++;
|
|
317
|
-
charCode = 65536 + ((charCode & 1023) << 10 | _value.charCodeAt(i) & 1023);
|
|
318
|
-
utf8Bytes.push(240 | charCode >> 18, 128 | charCode >> 12 & 63, 128 | charCode >> 6 & 63, 128 | charCode & 63);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
this.encoded.store_value(utf8Bytes.length, "u32");
|
|
322
|
-
this.encoded.store_bytes(new Uint8Array(utf8Bytes));
|
|
323
|
-
};
|
|
324
|
-
BorshSerializer2.prototype.encode_boolean = function(value) {
|
|
325
|
-
this.checkTypes && expect_type(value, "boolean", this.fieldPath);
|
|
326
|
-
this.encoded.store_value(value ? 1 : 0, "u8");
|
|
327
|
-
};
|
|
328
|
-
BorshSerializer2.prototype.encode_option = function(value, schema) {
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typeof schema === "object") {
|
|
128
|
+
if ("option" in schema) {
|
|
329
129
|
if (value === null || value === void 0) {
|
|
330
|
-
|
|
331
|
-
} else {
|
|
332
|
-
this.encoded.store_value(1, "u8");
|
|
333
|
-
this.encode_value(value, schema.option);
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
BorshSerializer2.prototype.encode_enum = function(value, schema) {
|
|
337
|
-
this.checkTypes && expect_enum(value, this.fieldPath);
|
|
338
|
-
var valueKey = Object.keys(value)[0];
|
|
339
|
-
for (var i = 0; i < schema["enum"].length; i++) {
|
|
340
|
-
var valueSchema = schema["enum"][i];
|
|
341
|
-
if (valueKey === Object.keys(valueSchema.struct)[0]) {
|
|
342
|
-
this.encoded.store_value(i, "u8");
|
|
343
|
-
return this.encode_struct(value, valueSchema);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
throw new Error("Enum key (".concat(valueKey, ") not found in enum schema: ").concat(JSON.stringify(schema), " at ").concat(this.fieldPath.join(".")));
|
|
347
|
-
};
|
|
348
|
-
BorshSerializer2.prototype.encode_array = function(value, schema) {
|
|
349
|
-
if (isArrayLike(value))
|
|
350
|
-
return this.encode_arraylike(value, schema);
|
|
351
|
-
if (value instanceof ArrayBuffer)
|
|
352
|
-
return this.encode_buffer(value, schema);
|
|
353
|
-
throw new Error("Expected Array-like not ".concat(typeof value, "(").concat(value, ") at ").concat(this.fieldPath.join(".")));
|
|
354
|
-
};
|
|
355
|
-
BorshSerializer2.prototype.encode_arraylike = function(value, schema) {
|
|
356
|
-
if (schema.array.len) {
|
|
357
|
-
expect_same_size(value.length, schema.array.len, this.fieldPath);
|
|
130
|
+
buf.storeU8(0);
|
|
358
131
|
} else {
|
|
359
|
-
|
|
132
|
+
buf.storeU8(1);
|
|
133
|
+
encodeValue(buf, value, schema.option);
|
|
360
134
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
};
|
|
373
|
-
BorshSerializer2.prototype.encode_set = function(value, schema) {
|
|
374
|
-
this.checkTypes && expect_type(value, "object", this.fieldPath);
|
|
375
|
-
var isSet = value instanceof Set;
|
|
376
|
-
var values = isSet ? Array.from(value.values()) : Object.values(value);
|
|
377
|
-
this.encoded.store_value(values.length, "u32");
|
|
378
|
-
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
|
|
379
|
-
var value_1 = values_1[_i];
|
|
380
|
-
this.encode_value(value_1, schema.set);
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
BorshSerializer2.prototype.encode_map = function(value, schema) {
|
|
384
|
-
this.checkTypes && expect_type(value, "object", this.fieldPath);
|
|
385
|
-
var isMap = value instanceof Map;
|
|
386
|
-
var keys = isMap ? Array.from(value.keys()) : Object.keys(value);
|
|
387
|
-
this.encoded.store_value(keys.length, "u32");
|
|
388
|
-
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
389
|
-
var key = keys_1[_i];
|
|
390
|
-
this.encode_value(key, schema.map.key);
|
|
391
|
-
this.encode_value(isMap ? value.get(key) : value[key], schema.map.value);
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
BorshSerializer2.prototype.encode_struct = function(value, schema) {
|
|
395
|
-
this.checkTypes && expect_type(value, "object", this.fieldPath);
|
|
396
|
-
for (var _i = 0, _a = Object.keys(schema.struct); _i < _a.length; _i++) {
|
|
397
|
-
var key = _a[_i];
|
|
398
|
-
this.fieldPath.push(key);
|
|
399
|
-
this.encode_value(value[key], schema.struct[key]);
|
|
400
|
-
this.fieldPath.pop();
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
return BorshSerializer2;
|
|
404
|
-
}()
|
|
405
|
-
);
|
|
406
|
-
|
|
407
|
-
// ../../node_modules/borsh/lib/esm/deserialize.js
|
|
408
|
-
var BorshDeserializer = (
|
|
409
|
-
/** @class */
|
|
410
|
-
function() {
|
|
411
|
-
function BorshDeserializer2(bufferArray) {
|
|
412
|
-
this.buffer = new DecodeBuffer(bufferArray);
|
|
413
|
-
}
|
|
414
|
-
__name(BorshDeserializer2, "BorshDeserializer");
|
|
415
|
-
BorshDeserializer2.prototype.decode = function(schema) {
|
|
416
|
-
return this.decode_value(schema);
|
|
417
|
-
};
|
|
418
|
-
BorshDeserializer2.prototype.decode_value = function(schema) {
|
|
419
|
-
if (typeof schema === "string") {
|
|
420
|
-
if (integers.includes(schema))
|
|
421
|
-
return this.decode_integer(schema);
|
|
422
|
-
if (schema === "string")
|
|
423
|
-
return this.decode_string();
|
|
424
|
-
if (schema === "bool")
|
|
425
|
-
return this.decode_boolean();
|
|
426
|
-
}
|
|
427
|
-
if (typeof schema === "object") {
|
|
428
|
-
if ("option" in schema)
|
|
429
|
-
return this.decode_option(schema);
|
|
430
|
-
if ("enum" in schema)
|
|
431
|
-
return this.decode_enum(schema);
|
|
432
|
-
if ("array" in schema)
|
|
433
|
-
return this.decode_array(schema);
|
|
434
|
-
if ("set" in schema)
|
|
435
|
-
return this.decode_set(schema);
|
|
436
|
-
if ("map" in schema)
|
|
437
|
-
return this.decode_map(schema);
|
|
438
|
-
if ("struct" in schema)
|
|
439
|
-
return this.decode_struct(schema);
|
|
440
|
-
}
|
|
441
|
-
throw new Error("Unsupported type: ".concat(schema));
|
|
442
|
-
};
|
|
443
|
-
BorshDeserializer2.prototype.decode_integer = function(schema) {
|
|
444
|
-
var size = parseInt(schema.substring(1));
|
|
445
|
-
if (size <= 32 || schema == "f64") {
|
|
446
|
-
return this.buffer.consume_value(schema);
|
|
447
|
-
}
|
|
448
|
-
return this.decode_bigint(size, schema.startsWith("i"));
|
|
449
|
-
};
|
|
450
|
-
BorshDeserializer2.prototype.decode_bigint = function(size, signed) {
|
|
451
|
-
if (signed === void 0) {
|
|
452
|
-
signed = false;
|
|
453
|
-
}
|
|
454
|
-
var buffer_len = size / 8;
|
|
455
|
-
var buffer = new Uint8Array(this.buffer.consume_bytes(buffer_len));
|
|
456
|
-
var bits = buffer.reduceRight(function(r, x) {
|
|
457
|
-
return r + x.toString(16).padStart(2, "0");
|
|
458
|
-
}, "");
|
|
459
|
-
if (signed && buffer[buffer_len - 1]) {
|
|
460
|
-
return BigInt.asIntN(size, BigInt("0x".concat(bits)));
|
|
461
|
-
}
|
|
462
|
-
return BigInt("0x".concat(bits));
|
|
463
|
-
};
|
|
464
|
-
BorshDeserializer2.prototype.decode_string = function() {
|
|
465
|
-
var len = this.decode_integer("u32");
|
|
466
|
-
var buffer = new Uint8Array(this.buffer.consume_bytes(len));
|
|
467
|
-
var codePoints = [];
|
|
468
|
-
for (var i = 0; i < len; ++i) {
|
|
469
|
-
var byte = buffer[i];
|
|
470
|
-
if (byte < 128) {
|
|
471
|
-
codePoints.push(byte);
|
|
472
|
-
} else if (byte < 224) {
|
|
473
|
-
codePoints.push((byte & 31) << 6 | buffer[++i] & 63);
|
|
474
|
-
} else if (byte < 240) {
|
|
475
|
-
codePoints.push((byte & 15) << 12 | (buffer[++i] & 63) << 6 | buffer[++i] & 63);
|
|
476
|
-
} else {
|
|
477
|
-
var codePoint = (byte & 7) << 18 | (buffer[++i] & 63) << 12 | (buffer[++i] & 63) << 6 | buffer[++i] & 63;
|
|
478
|
-
codePoints.push(codePoint);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if ("enum" in schema) {
|
|
138
|
+
const valueKey = Object.keys(value)[0];
|
|
139
|
+
const variants = schema.enum;
|
|
140
|
+
for (let i = 0; i < variants.length; i++) {
|
|
141
|
+
const variantKey = Object.keys(variants[i].struct)[0];
|
|
142
|
+
if (valueKey === variantKey) {
|
|
143
|
+
buf.storeU8(i);
|
|
144
|
+
encodeStruct(buf, value, variants[i]);
|
|
145
|
+
return;
|
|
479
146
|
}
|
|
480
147
|
}
|
|
481
|
-
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
BorshDeserializer2.prototype.decode_option = function(schema) {
|
|
487
|
-
var option = this.buffer.consume_value("u8");
|
|
488
|
-
if (option === 1) {
|
|
489
|
-
return this.decode_value(schema.option);
|
|
490
|
-
}
|
|
491
|
-
if (option !== 0) {
|
|
492
|
-
throw new Error("Invalid option ".concat(option));
|
|
493
|
-
}
|
|
494
|
-
return null;
|
|
495
|
-
};
|
|
496
|
-
BorshDeserializer2.prototype.decode_enum = function(schema) {
|
|
497
|
-
var _a;
|
|
498
|
-
var valueIndex = this.buffer.consume_value("u8");
|
|
499
|
-
if (valueIndex > schema["enum"].length) {
|
|
500
|
-
throw new Error("Enum option ".concat(valueIndex, " is not available"));
|
|
501
|
-
}
|
|
502
|
-
var struct = schema["enum"][valueIndex].struct;
|
|
503
|
-
var key = Object.keys(struct)[0];
|
|
504
|
-
return _a = {}, _a[key] = this.decode_value(struct[key]), _a;
|
|
505
|
-
};
|
|
506
|
-
BorshDeserializer2.prototype.decode_array = function(schema) {
|
|
507
|
-
var result = [];
|
|
508
|
-
var len = schema.array.len ? schema.array.len : this.decode_integer("u32");
|
|
509
|
-
for (var i = 0; i < len; ++i) {
|
|
510
|
-
result.push(this.decode_value(schema.array.type));
|
|
511
|
-
}
|
|
512
|
-
return result;
|
|
513
|
-
};
|
|
514
|
-
BorshDeserializer2.prototype.decode_set = function(schema) {
|
|
515
|
-
var len = this.decode_integer("u32");
|
|
516
|
-
var result = /* @__PURE__ */ new Set();
|
|
517
|
-
for (var i = 0; i < len; ++i) {
|
|
518
|
-
result.add(this.decode_value(schema.set));
|
|
519
|
-
}
|
|
520
|
-
return result;
|
|
521
|
-
};
|
|
522
|
-
BorshDeserializer2.prototype.decode_map = function(schema) {
|
|
523
|
-
var len = this.decode_integer("u32");
|
|
524
|
-
var result = /* @__PURE__ */ new Map();
|
|
525
|
-
for (var i = 0; i < len; ++i) {
|
|
526
|
-
var key = this.decode_value(schema.map.key);
|
|
527
|
-
var value = this.decode_value(schema.map.value);
|
|
528
|
-
result.set(key, value);
|
|
148
|
+
throw new Error(`Borsh: enum key "${valueKey}" not found in schema`);
|
|
149
|
+
}
|
|
150
|
+
if ("array" in schema) {
|
|
151
|
+
if (schema.array.len == null) {
|
|
152
|
+
buf.storeU32(value.length);
|
|
529
153
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
BorshDeserializer2.prototype.decode_struct = function(schema) {
|
|
533
|
-
var result = {};
|
|
534
|
-
for (var key in schema.struct) {
|
|
535
|
-
result[key] = this.decode_value(schema.struct[key]);
|
|
154
|
+
for (let i = 0; i < value.length; i++) {
|
|
155
|
+
encodeValue(buf, value[i], schema.array.type);
|
|
536
156
|
}
|
|
537
|
-
return
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if ("struct" in schema) {
|
|
160
|
+
encodeStruct(buf, value, schema);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
__name(encodeValue, "encodeValue");
|
|
166
|
+
function encodeStruct(buf, value, schema) {
|
|
167
|
+
for (const key of Object.keys(schema.struct)) {
|
|
168
|
+
encodeValue(buf, value[key], schema.struct[key]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
__name(encodeStruct, "encodeStruct");
|
|
172
|
+
function serialize(schema, value) {
|
|
173
|
+
const buf = new EncodeBuffer();
|
|
174
|
+
encodeValue(buf, value, schema);
|
|
175
|
+
return buf.result();
|
|
552
176
|
}
|
|
553
177
|
__name(serialize, "serialize");
|
|
554
178
|
|
|
@@ -577,6 +201,13 @@ var nearWalletAdapters = (() => {
|
|
|
577
201
|
return value;
|
|
578
202
|
}
|
|
579
203
|
__name(abytes, "abytes");
|
|
204
|
+
function ahash(h) {
|
|
205
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
206
|
+
throw new Error("Hash must wrapped by utils.createHasher");
|
|
207
|
+
anumber(h.outputLen);
|
|
208
|
+
anumber(h.blockLen);
|
|
209
|
+
}
|
|
210
|
+
__name(ahash, "ahash");
|
|
580
211
|
function aexists(instance, checkFinished = true) {
|
|
581
212
|
if (instance.destroyed)
|
|
582
213
|
throw new Error("Hash instance has been destroyed");
|
|
@@ -1254,6 +885,11 @@ var nearWalletAdapters = (() => {
|
|
|
1254
885
|
return n;
|
|
1255
886
|
}
|
|
1256
887
|
__name(abignumber, "abignumber");
|
|
888
|
+
function numberToHexUnpadded(num) {
|
|
889
|
+
const hex = abignumber(num).toString(16);
|
|
890
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
891
|
+
}
|
|
892
|
+
__name(numberToHexUnpadded, "numberToHexUnpadded");
|
|
1257
893
|
function hexToNumber(hex) {
|
|
1258
894
|
if (typeof hex !== "string")
|
|
1259
895
|
throw new Error("hex string expected, got " + typeof hex);
|
|
@@ -1295,7 +931,66 @@ var nearWalletAdapters = (() => {
|
|
|
1295
931
|
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
1296
932
|
}
|
|
1297
933
|
__name(aInRange, "aInRange");
|
|
934
|
+
function bitLen(n) {
|
|
935
|
+
let len;
|
|
936
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
937
|
+
;
|
|
938
|
+
return len;
|
|
939
|
+
}
|
|
940
|
+
__name(bitLen, "bitLen");
|
|
1298
941
|
var bitMask = /* @__PURE__ */ __name((n) => (_1n << BigInt(n)) - _1n, "bitMask");
|
|
942
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
943
|
+
anumber(hashLen, "hashLen");
|
|
944
|
+
anumber(qByteLen, "qByteLen");
|
|
945
|
+
if (typeof hmacFn !== "function")
|
|
946
|
+
throw new Error("hmacFn must be a function");
|
|
947
|
+
const u8n = /* @__PURE__ */ __name((len) => new Uint8Array(len), "u8n");
|
|
948
|
+
const NULL = Uint8Array.of();
|
|
949
|
+
const byte0 = Uint8Array.of(0);
|
|
950
|
+
const byte1 = Uint8Array.of(1);
|
|
951
|
+
const _maxDrbgIters = 1e3;
|
|
952
|
+
let v = u8n(hashLen);
|
|
953
|
+
let k = u8n(hashLen);
|
|
954
|
+
let i = 0;
|
|
955
|
+
const reset = /* @__PURE__ */ __name(() => {
|
|
956
|
+
v.fill(1);
|
|
957
|
+
k.fill(0);
|
|
958
|
+
i = 0;
|
|
959
|
+
}, "reset");
|
|
960
|
+
const h = /* @__PURE__ */ __name((...msgs) => hmacFn(k, concatBytes(v, ...msgs)), "h");
|
|
961
|
+
const reseed = /* @__PURE__ */ __name((seed = NULL) => {
|
|
962
|
+
k = h(byte0, seed);
|
|
963
|
+
v = h();
|
|
964
|
+
if (seed.length === 0)
|
|
965
|
+
return;
|
|
966
|
+
k = h(byte1, seed);
|
|
967
|
+
v = h();
|
|
968
|
+
}, "reseed");
|
|
969
|
+
const gen = /* @__PURE__ */ __name(() => {
|
|
970
|
+
if (i++ >= _maxDrbgIters)
|
|
971
|
+
throw new Error("drbg: tried max amount of iterations");
|
|
972
|
+
let len = 0;
|
|
973
|
+
const out = [];
|
|
974
|
+
while (len < qByteLen) {
|
|
975
|
+
v = h();
|
|
976
|
+
const sl = v.slice();
|
|
977
|
+
out.push(sl);
|
|
978
|
+
len += v.length;
|
|
979
|
+
}
|
|
980
|
+
return concatBytes(...out);
|
|
981
|
+
}, "gen");
|
|
982
|
+
const genUntil = /* @__PURE__ */ __name((seed, pred) => {
|
|
983
|
+
reset();
|
|
984
|
+
reseed(seed);
|
|
985
|
+
let res = void 0;
|
|
986
|
+
while (!(res = pred(gen())))
|
|
987
|
+
reseed();
|
|
988
|
+
reset();
|
|
989
|
+
return res;
|
|
990
|
+
}, "genUntil");
|
|
991
|
+
return genUntil;
|
|
992
|
+
}
|
|
993
|
+
__name(createHmacDrbg, "createHmacDrbg");
|
|
1299
994
|
function validateObject(object, fields = {}, optFields = {}) {
|
|
1300
995
|
if (!object || typeof object !== "object")
|
|
1301
996
|
throw new Error("expected valid options object");
|
|
@@ -1395,13 +1090,13 @@ var nearWalletAdapters = (() => {
|
|
|
1395
1090
|
return root;
|
|
1396
1091
|
}
|
|
1397
1092
|
__name(sqrt5mod8, "sqrt5mod8");
|
|
1398
|
-
function sqrt9mod16(
|
|
1399
|
-
const Fp_ = Field(
|
|
1400
|
-
const tn = tonelliShanks(
|
|
1093
|
+
function sqrt9mod16(P) {
|
|
1094
|
+
const Fp_ = Field(P);
|
|
1095
|
+
const tn = tonelliShanks(P);
|
|
1401
1096
|
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
1402
1097
|
const c2 = tn(Fp_, c1);
|
|
1403
1098
|
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
1404
|
-
const c4 = (
|
|
1099
|
+
const c4 = (P + _7n) / _16n;
|
|
1405
1100
|
return (Fp, n) => {
|
|
1406
1101
|
let tv1 = Fp.pow(n, c4);
|
|
1407
1102
|
let tv2 = Fp.mul(tv1, c1);
|
|
@@ -1418,17 +1113,17 @@ var nearWalletAdapters = (() => {
|
|
|
1418
1113
|
};
|
|
1419
1114
|
}
|
|
1420
1115
|
__name(sqrt9mod16, "sqrt9mod16");
|
|
1421
|
-
function tonelliShanks(
|
|
1422
|
-
if (
|
|
1116
|
+
function tonelliShanks(P) {
|
|
1117
|
+
if (P < _3n)
|
|
1423
1118
|
throw new Error("sqrt is not defined for small field");
|
|
1424
|
-
let Q =
|
|
1119
|
+
let Q = P - _1n2;
|
|
1425
1120
|
let S = 0;
|
|
1426
1121
|
while (Q % _2n === _0n2) {
|
|
1427
1122
|
Q /= _2n;
|
|
1428
1123
|
S++;
|
|
1429
1124
|
}
|
|
1430
1125
|
let Z = _2n;
|
|
1431
|
-
const _Fp = Field(
|
|
1126
|
+
const _Fp = Field(P);
|
|
1432
1127
|
while (FpLegendre(_Fp, Z) === 1) {
|
|
1433
1128
|
if (Z++ > 1e3)
|
|
1434
1129
|
throw new Error("Cannot find square root: probably non-prime P");
|
|
@@ -1468,14 +1163,14 @@ var nearWalletAdapters = (() => {
|
|
|
1468
1163
|
}, "tonelliSlow");
|
|
1469
1164
|
}
|
|
1470
1165
|
__name(tonelliShanks, "tonelliShanks");
|
|
1471
|
-
function FpSqrt(
|
|
1472
|
-
if (
|
|
1166
|
+
function FpSqrt(P) {
|
|
1167
|
+
if (P % _4n === _3n)
|
|
1473
1168
|
return sqrt3mod4;
|
|
1474
|
-
if (
|
|
1169
|
+
if (P % _8n === _5n)
|
|
1475
1170
|
return sqrt5mod8;
|
|
1476
|
-
if (
|
|
1477
|
-
return sqrt9mod16(
|
|
1478
|
-
return tonelliShanks(
|
|
1171
|
+
if (P % _16n === _9n)
|
|
1172
|
+
return sqrt9mod16(P);
|
|
1173
|
+
return tonelliShanks(P);
|
|
1479
1174
|
}
|
|
1480
1175
|
__name(FpSqrt, "FpSqrt");
|
|
1481
1176
|
var isNegativeLE = /* @__PURE__ */ __name((num, modulo) => (mod(num, modulo) & _1n2) === _1n2, "isNegativeLE");
|
|
@@ -1709,6 +1404,30 @@ var nearWalletAdapters = (() => {
|
|
|
1709
1404
|
return new _Field(ORDER, opts);
|
|
1710
1405
|
}
|
|
1711
1406
|
__name(Field, "Field");
|
|
1407
|
+
function getFieldBytesLength(fieldOrder) {
|
|
1408
|
+
if (typeof fieldOrder !== "bigint")
|
|
1409
|
+
throw new Error("field order must be bigint");
|
|
1410
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
1411
|
+
return Math.ceil(bitLength / 8);
|
|
1412
|
+
}
|
|
1413
|
+
__name(getFieldBytesLength, "getFieldBytesLength");
|
|
1414
|
+
function getMinHashLength(fieldOrder) {
|
|
1415
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
1416
|
+
return length + Math.ceil(length / 2);
|
|
1417
|
+
}
|
|
1418
|
+
__name(getMinHashLength, "getMinHashLength");
|
|
1419
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
1420
|
+
abytes(key);
|
|
1421
|
+
const len = key.length;
|
|
1422
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
1423
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
1424
|
+
if (len < 16 || len < minLen || len > 1024)
|
|
1425
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
1426
|
+
const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
1427
|
+
const reduced = mod(num, fieldOrder - _1n2) + _1n2;
|
|
1428
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
1429
|
+
}
|
|
1430
|
+
__name(mapHashToField, "mapHashToField");
|
|
1712
1431
|
|
|
1713
1432
|
// ../../node_modules/@noble/curves/abstract/curve.js
|
|
1714
1433
|
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
@@ -1757,8 +1476,8 @@ var nearWalletAdapters = (() => {
|
|
|
1757
1476
|
__name(calcOffsets, "calcOffsets");
|
|
1758
1477
|
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
1759
1478
|
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
1760
|
-
function getW(
|
|
1761
|
-
return pointWindowSizes.get(
|
|
1479
|
+
function getW(P) {
|
|
1480
|
+
return pointWindowSizes.get(P) || 1;
|
|
1762
1481
|
}
|
|
1763
1482
|
__name(getW, "getW");
|
|
1764
1483
|
function assert0(n) {
|
|
@@ -1891,15 +1610,31 @@ var nearWalletAdapters = (() => {
|
|
|
1891
1610
|
// We calculate precomputes for elliptic curve point multiplication
|
|
1892
1611
|
// using windowed method. This specifies window size and
|
|
1893
1612
|
// stores precomputed values. Usually only base point would be precomputed.
|
|
1894
|
-
createCache(
|
|
1613
|
+
createCache(P, W) {
|
|
1895
1614
|
validateW(W, this.bits);
|
|
1896
|
-
pointWindowSizes.set(
|
|
1897
|
-
pointPrecomputes.delete(
|
|
1615
|
+
pointWindowSizes.set(P, W);
|
|
1616
|
+
pointPrecomputes.delete(P);
|
|
1898
1617
|
}
|
|
1899
1618
|
hasCache(elm) {
|
|
1900
1619
|
return getW(elm) !== 1;
|
|
1901
1620
|
}
|
|
1902
1621
|
};
|
|
1622
|
+
function mulEndoUnsafe(Point, point, k1, k2) {
|
|
1623
|
+
let acc = point;
|
|
1624
|
+
let p1 = Point.ZERO;
|
|
1625
|
+
let p2 = Point.ZERO;
|
|
1626
|
+
while (k1 > _0n3 || k2 > _0n3) {
|
|
1627
|
+
if (k1 & _1n3)
|
|
1628
|
+
p1 = p1.add(acc);
|
|
1629
|
+
if (k2 & _1n3)
|
|
1630
|
+
p2 = p2.add(acc);
|
|
1631
|
+
acc = acc.double();
|
|
1632
|
+
k1 >>= _1n3;
|
|
1633
|
+
k2 >>= _1n3;
|
|
1634
|
+
}
|
|
1635
|
+
return { p1, p2 };
|
|
1636
|
+
}
|
|
1637
|
+
__name(mulEndoUnsafe, "mulEndoUnsafe");
|
|
1903
1638
|
function createField(order, field, isLE) {
|
|
1904
1639
|
if (field) {
|
|
1905
1640
|
if (field.ORDER !== order)
|
|
@@ -2396,19 +2131,19 @@ var nearWalletAdapters = (() => {
|
|
|
2396
2131
|
}))();
|
|
2397
2132
|
function ed25519_pow_2_252_3(x) {
|
|
2398
2133
|
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
2399
|
-
const
|
|
2400
|
-
const x2 = x * x %
|
|
2401
|
-
const b2 = x2 * x %
|
|
2402
|
-
const b4 = pow2(b2, _2n3,
|
|
2403
|
-
const b5 = pow2(b4, _1n5,
|
|
2404
|
-
const b10 = pow2(b5, _5n2,
|
|
2405
|
-
const b20 = pow2(b10, _10n,
|
|
2406
|
-
const b40 = pow2(b20, _20n,
|
|
2407
|
-
const b80 = pow2(b40, _40n,
|
|
2408
|
-
const b160 = pow2(b80, _80n,
|
|
2409
|
-
const b240 = pow2(b160, _80n,
|
|
2410
|
-
const b250 = pow2(b240, _10n,
|
|
2411
|
-
const pow_p_5_8 = pow2(b250, _2n3,
|
|
2134
|
+
const P = ed25519_CURVE_p;
|
|
2135
|
+
const x2 = x * x % P;
|
|
2136
|
+
const b2 = x2 * x % P;
|
|
2137
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
2138
|
+
const b5 = pow2(b4, _1n5, P) * x % P;
|
|
2139
|
+
const b10 = pow2(b5, _5n2, P) * b5 % P;
|
|
2140
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
2141
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
2142
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
2143
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
2144
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
2145
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
2146
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
2412
2147
|
return { pow_p_5_8, b2 };
|
|
2413
2148
|
}
|
|
2414
2149
|
__name(ed25519_pow_2_252_3, "ed25519_pow_2_252_3");
|
|
@@ -2421,23 +2156,23 @@ var nearWalletAdapters = (() => {
|
|
|
2421
2156
|
__name(adjustScalarBytes, "adjustScalarBytes");
|
|
2422
2157
|
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
2423
2158
|
function uvRatio(u, v) {
|
|
2424
|
-
const
|
|
2425
|
-
const v3 = mod(v * v * v,
|
|
2426
|
-
const v7 = mod(v3 * v3 * v,
|
|
2159
|
+
const P = ed25519_CURVE_p;
|
|
2160
|
+
const v3 = mod(v * v * v, P);
|
|
2161
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
2427
2162
|
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
2428
|
-
let x = mod(u * v3 * pow,
|
|
2429
|
-
const vx2 = mod(v * x * x,
|
|
2163
|
+
let x = mod(u * v3 * pow, P);
|
|
2164
|
+
const vx2 = mod(v * x * x, P);
|
|
2430
2165
|
const root1 = x;
|
|
2431
|
-
const root2 = mod(x * ED25519_SQRT_M1,
|
|
2166
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
2432
2167
|
const useRoot1 = vx2 === u;
|
|
2433
|
-
const useRoot2 = vx2 === mod(-u,
|
|
2434
|
-
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1,
|
|
2168
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
2169
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
2435
2170
|
if (useRoot1)
|
|
2436
2171
|
x = root1;
|
|
2437
2172
|
if (useRoot2 || noRoot)
|
|
2438
2173
|
x = root2;
|
|
2439
|
-
if (isNegativeLE(x,
|
|
2440
|
-
x = mod(-x,
|
|
2174
|
+
if (isNegativeLE(x, P))
|
|
2175
|
+
x = mod(-x, P);
|
|
2441
2176
|
return { isValid: useRoot1 || useRoot2, value: x };
|
|
2442
2177
|
}
|
|
2443
2178
|
__name(uvRatio, "uvRatio");
|
|
@@ -2448,6 +2183,1043 @@ var nearWalletAdapters = (() => {
|
|
|
2448
2183
|
__name(ed, "ed");
|
|
2449
2184
|
var ed25519 = /* @__PURE__ */ ed({});
|
|
2450
2185
|
|
|
2186
|
+
// ../../node_modules/@noble/hashes/hmac.js
|
|
2187
|
+
var _HMAC = class {
|
|
2188
|
+
static {
|
|
2189
|
+
__name(this, "_HMAC");
|
|
2190
|
+
}
|
|
2191
|
+
oHash;
|
|
2192
|
+
iHash;
|
|
2193
|
+
blockLen;
|
|
2194
|
+
outputLen;
|
|
2195
|
+
finished = false;
|
|
2196
|
+
destroyed = false;
|
|
2197
|
+
constructor(hash, key) {
|
|
2198
|
+
ahash(hash);
|
|
2199
|
+
abytes(key, void 0, "key");
|
|
2200
|
+
this.iHash = hash.create();
|
|
2201
|
+
if (typeof this.iHash.update !== "function")
|
|
2202
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
2203
|
+
this.blockLen = this.iHash.blockLen;
|
|
2204
|
+
this.outputLen = this.iHash.outputLen;
|
|
2205
|
+
const blockLen = this.blockLen;
|
|
2206
|
+
const pad = new Uint8Array(blockLen);
|
|
2207
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
2208
|
+
for (let i = 0; i < pad.length; i++)
|
|
2209
|
+
pad[i] ^= 54;
|
|
2210
|
+
this.iHash.update(pad);
|
|
2211
|
+
this.oHash = hash.create();
|
|
2212
|
+
for (let i = 0; i < pad.length; i++)
|
|
2213
|
+
pad[i] ^= 54 ^ 92;
|
|
2214
|
+
this.oHash.update(pad);
|
|
2215
|
+
clean(pad);
|
|
2216
|
+
}
|
|
2217
|
+
update(buf) {
|
|
2218
|
+
aexists(this);
|
|
2219
|
+
this.iHash.update(buf);
|
|
2220
|
+
return this;
|
|
2221
|
+
}
|
|
2222
|
+
digestInto(out) {
|
|
2223
|
+
aexists(this);
|
|
2224
|
+
abytes(out, this.outputLen, "output");
|
|
2225
|
+
this.finished = true;
|
|
2226
|
+
this.iHash.digestInto(out);
|
|
2227
|
+
this.oHash.update(out);
|
|
2228
|
+
this.oHash.digestInto(out);
|
|
2229
|
+
this.destroy();
|
|
2230
|
+
}
|
|
2231
|
+
digest() {
|
|
2232
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
2233
|
+
this.digestInto(out);
|
|
2234
|
+
return out;
|
|
2235
|
+
}
|
|
2236
|
+
_cloneInto(to) {
|
|
2237
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
2238
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
2239
|
+
to = to;
|
|
2240
|
+
to.finished = finished;
|
|
2241
|
+
to.destroyed = destroyed;
|
|
2242
|
+
to.blockLen = blockLen;
|
|
2243
|
+
to.outputLen = outputLen;
|
|
2244
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
2245
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
2246
|
+
return to;
|
|
2247
|
+
}
|
|
2248
|
+
clone() {
|
|
2249
|
+
return this._cloneInto();
|
|
2250
|
+
}
|
|
2251
|
+
destroy() {
|
|
2252
|
+
this.destroyed = true;
|
|
2253
|
+
this.oHash.destroy();
|
|
2254
|
+
this.iHash.destroy();
|
|
2255
|
+
}
|
|
2256
|
+
};
|
|
2257
|
+
var hmac = /* @__PURE__ */ __name((hash, key, message) => new _HMAC(hash, key).update(message).digest(), "hmac");
|
|
2258
|
+
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
2259
|
+
|
|
2260
|
+
// ../../node_modules/@noble/curves/abstract/weierstrass.js
|
|
2261
|
+
var divNearest = /* @__PURE__ */ __name((num, den) => (num + (num >= 0 ? den : -den) / _2n4) / den, "divNearest");
|
|
2262
|
+
function _splitEndoScalar(k, basis, n) {
|
|
2263
|
+
const [[a1, b1], [a2, b2]] = basis;
|
|
2264
|
+
const c1 = divNearest(b2 * k, n);
|
|
2265
|
+
const c2 = divNearest(-b1 * k, n);
|
|
2266
|
+
let k1 = k - c1 * a1 - c2 * a2;
|
|
2267
|
+
let k2 = -c1 * b1 - c2 * b2;
|
|
2268
|
+
const k1neg = k1 < _0n5;
|
|
2269
|
+
const k2neg = k2 < _0n5;
|
|
2270
|
+
if (k1neg)
|
|
2271
|
+
k1 = -k1;
|
|
2272
|
+
if (k2neg)
|
|
2273
|
+
k2 = -k2;
|
|
2274
|
+
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n6;
|
|
2275
|
+
if (k1 < _0n5 || k1 >= MAX_NUM || k2 < _0n5 || k2 >= MAX_NUM) {
|
|
2276
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + k);
|
|
2277
|
+
}
|
|
2278
|
+
return { k1neg, k1, k2neg, k2 };
|
|
2279
|
+
}
|
|
2280
|
+
__name(_splitEndoScalar, "_splitEndoScalar");
|
|
2281
|
+
function validateSigFormat(format) {
|
|
2282
|
+
if (!["compact", "recovered", "der"].includes(format))
|
|
2283
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
2284
|
+
return format;
|
|
2285
|
+
}
|
|
2286
|
+
__name(validateSigFormat, "validateSigFormat");
|
|
2287
|
+
function validateSigOpts(opts, def) {
|
|
2288
|
+
const optsn = {};
|
|
2289
|
+
for (let optName of Object.keys(def)) {
|
|
2290
|
+
optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
2291
|
+
}
|
|
2292
|
+
abool(optsn.lowS, "lowS");
|
|
2293
|
+
abool(optsn.prehash, "prehash");
|
|
2294
|
+
if (optsn.format !== void 0)
|
|
2295
|
+
validateSigFormat(optsn.format);
|
|
2296
|
+
return optsn;
|
|
2297
|
+
}
|
|
2298
|
+
__name(validateSigOpts, "validateSigOpts");
|
|
2299
|
+
var DERErr = class extends Error {
|
|
2300
|
+
static {
|
|
2301
|
+
__name(this, "DERErr");
|
|
2302
|
+
}
|
|
2303
|
+
constructor(m = "") {
|
|
2304
|
+
super(m);
|
|
2305
|
+
}
|
|
2306
|
+
};
|
|
2307
|
+
var DER = {
|
|
2308
|
+
// asn.1 DER encoding utils
|
|
2309
|
+
Err: DERErr,
|
|
2310
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
2311
|
+
_tlv: {
|
|
2312
|
+
encode: /* @__PURE__ */ __name((tag, data) => {
|
|
2313
|
+
const { Err: E } = DER;
|
|
2314
|
+
if (tag < 0 || tag > 256)
|
|
2315
|
+
throw new E("tlv.encode: wrong tag");
|
|
2316
|
+
if (data.length & 1)
|
|
2317
|
+
throw new E("tlv.encode: unpadded data");
|
|
2318
|
+
const dataLen = data.length / 2;
|
|
2319
|
+
const len = numberToHexUnpadded(dataLen);
|
|
2320
|
+
if (len.length / 2 & 128)
|
|
2321
|
+
throw new E("tlv.encode: long form length too big");
|
|
2322
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
2323
|
+
const t = numberToHexUnpadded(tag);
|
|
2324
|
+
return t + lenLen + len + data;
|
|
2325
|
+
}, "encode"),
|
|
2326
|
+
// v - value, l - left bytes (unparsed)
|
|
2327
|
+
decode(tag, data) {
|
|
2328
|
+
const { Err: E } = DER;
|
|
2329
|
+
let pos = 0;
|
|
2330
|
+
if (tag < 0 || tag > 256)
|
|
2331
|
+
throw new E("tlv.encode: wrong tag");
|
|
2332
|
+
if (data.length < 2 || data[pos++] !== tag)
|
|
2333
|
+
throw new E("tlv.decode: wrong tlv");
|
|
2334
|
+
const first = data[pos++];
|
|
2335
|
+
const isLong = !!(first & 128);
|
|
2336
|
+
let length = 0;
|
|
2337
|
+
if (!isLong)
|
|
2338
|
+
length = first;
|
|
2339
|
+
else {
|
|
2340
|
+
const lenLen = first & 127;
|
|
2341
|
+
if (!lenLen)
|
|
2342
|
+
throw new E("tlv.decode(long): indefinite length not supported");
|
|
2343
|
+
if (lenLen > 4)
|
|
2344
|
+
throw new E("tlv.decode(long): byte length is too big");
|
|
2345
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
2346
|
+
if (lengthBytes.length !== lenLen)
|
|
2347
|
+
throw new E("tlv.decode: length bytes not complete");
|
|
2348
|
+
if (lengthBytes[0] === 0)
|
|
2349
|
+
throw new E("tlv.decode(long): zero leftmost byte");
|
|
2350
|
+
for (const b of lengthBytes)
|
|
2351
|
+
length = length << 8 | b;
|
|
2352
|
+
pos += lenLen;
|
|
2353
|
+
if (length < 128)
|
|
2354
|
+
throw new E("tlv.decode(long): not minimal encoding");
|
|
2355
|
+
}
|
|
2356
|
+
const v = data.subarray(pos, pos + length);
|
|
2357
|
+
if (v.length !== length)
|
|
2358
|
+
throw new E("tlv.decode: wrong value length");
|
|
2359
|
+
return { v, l: data.subarray(pos + length) };
|
|
2360
|
+
}
|
|
2361
|
+
},
|
|
2362
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
2363
|
+
// since we always use positive integers here. It must always be empty:
|
|
2364
|
+
// - add zero byte if exists
|
|
2365
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2366
|
+
_int: {
|
|
2367
|
+
encode(num) {
|
|
2368
|
+
const { Err: E } = DER;
|
|
2369
|
+
if (num < _0n5)
|
|
2370
|
+
throw new E("integer: negative integers are not allowed");
|
|
2371
|
+
let hex = numberToHexUnpadded(num);
|
|
2372
|
+
if (Number.parseInt(hex[0], 16) & 8)
|
|
2373
|
+
hex = "00" + hex;
|
|
2374
|
+
if (hex.length & 1)
|
|
2375
|
+
throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
2376
|
+
return hex;
|
|
2377
|
+
},
|
|
2378
|
+
decode(data) {
|
|
2379
|
+
const { Err: E } = DER;
|
|
2380
|
+
if (data[0] & 128)
|
|
2381
|
+
throw new E("invalid signature integer: negative");
|
|
2382
|
+
if (data[0] === 0 && !(data[1] & 128))
|
|
2383
|
+
throw new E("invalid signature integer: unnecessary leading zero");
|
|
2384
|
+
return bytesToNumberBE(data);
|
|
2385
|
+
}
|
|
2386
|
+
},
|
|
2387
|
+
toSig(bytes) {
|
|
2388
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
2389
|
+
const data = abytes(bytes, void 0, "signature");
|
|
2390
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
2391
|
+
if (seqLeftBytes.length)
|
|
2392
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2393
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
2394
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
2395
|
+
if (sLeftBytes.length)
|
|
2396
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2397
|
+
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
2398
|
+
},
|
|
2399
|
+
hexFromSig(sig) {
|
|
2400
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
2401
|
+
const rs = tlv.encode(2, int.encode(sig.r));
|
|
2402
|
+
const ss = tlv.encode(2, int.encode(sig.s));
|
|
2403
|
+
const seq = rs + ss;
|
|
2404
|
+
return tlv.encode(48, seq);
|
|
2405
|
+
}
|
|
2406
|
+
};
|
|
2407
|
+
var _0n5 = BigInt(0);
|
|
2408
|
+
var _1n6 = BigInt(1);
|
|
2409
|
+
var _2n4 = BigInt(2);
|
|
2410
|
+
var _3n2 = BigInt(3);
|
|
2411
|
+
var _4n2 = BigInt(4);
|
|
2412
|
+
function weierstrass(params, extraOpts = {}) {
|
|
2413
|
+
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
2414
|
+
const { Fp, Fn } = validated;
|
|
2415
|
+
let CURVE = validated.CURVE;
|
|
2416
|
+
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
2417
|
+
validateObject(extraOpts, {}, {
|
|
2418
|
+
allowInfinityPoint: "boolean",
|
|
2419
|
+
clearCofactor: "function",
|
|
2420
|
+
isTorsionFree: "function",
|
|
2421
|
+
fromBytes: "function",
|
|
2422
|
+
toBytes: "function",
|
|
2423
|
+
endo: "object"
|
|
2424
|
+
});
|
|
2425
|
+
const { endo } = extraOpts;
|
|
2426
|
+
if (endo) {
|
|
2427
|
+
if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
|
|
2428
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
const lengths = getWLengths(Fp, Fn);
|
|
2432
|
+
function assertCompressionIsSupported() {
|
|
2433
|
+
if (!Fp.isOdd)
|
|
2434
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
2435
|
+
}
|
|
2436
|
+
__name(assertCompressionIsSupported, "assertCompressionIsSupported");
|
|
2437
|
+
function pointToBytes(_c, point, isCompressed) {
|
|
2438
|
+
const { x, y } = point.toAffine();
|
|
2439
|
+
const bx = Fp.toBytes(x);
|
|
2440
|
+
abool(isCompressed, "isCompressed");
|
|
2441
|
+
if (isCompressed) {
|
|
2442
|
+
assertCompressionIsSupported();
|
|
2443
|
+
const hasEvenY = !Fp.isOdd(y);
|
|
2444
|
+
return concatBytes(pprefix(hasEvenY), bx);
|
|
2445
|
+
} else {
|
|
2446
|
+
return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
__name(pointToBytes, "pointToBytes");
|
|
2450
|
+
function pointFromBytes(bytes) {
|
|
2451
|
+
abytes(bytes, void 0, "Point");
|
|
2452
|
+
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
2453
|
+
const length = bytes.length;
|
|
2454
|
+
const head = bytes[0];
|
|
2455
|
+
const tail = bytes.subarray(1);
|
|
2456
|
+
if (length === comp && (head === 2 || head === 3)) {
|
|
2457
|
+
const x = Fp.fromBytes(tail);
|
|
2458
|
+
if (!Fp.isValid(x))
|
|
2459
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
2460
|
+
const y2 = weierstrassEquation(x);
|
|
2461
|
+
let y;
|
|
2462
|
+
try {
|
|
2463
|
+
y = Fp.sqrt(y2);
|
|
2464
|
+
} catch (sqrtError) {
|
|
2465
|
+
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
2466
|
+
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
2467
|
+
}
|
|
2468
|
+
assertCompressionIsSupported();
|
|
2469
|
+
const evenY = Fp.isOdd(y);
|
|
2470
|
+
const evenH = (head & 1) === 1;
|
|
2471
|
+
if (evenH !== evenY)
|
|
2472
|
+
y = Fp.neg(y);
|
|
2473
|
+
return { x, y };
|
|
2474
|
+
} else if (length === uncomp && head === 4) {
|
|
2475
|
+
const L = Fp.BYTES;
|
|
2476
|
+
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
2477
|
+
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
2478
|
+
if (!isValidXY(x, y))
|
|
2479
|
+
throw new Error("bad point: is not on curve");
|
|
2480
|
+
return { x, y };
|
|
2481
|
+
} else {
|
|
2482
|
+
throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
__name(pointFromBytes, "pointFromBytes");
|
|
2486
|
+
const encodePoint = extraOpts.toBytes || pointToBytes;
|
|
2487
|
+
const decodePoint = extraOpts.fromBytes || pointFromBytes;
|
|
2488
|
+
function weierstrassEquation(x) {
|
|
2489
|
+
const x2 = Fp.sqr(x);
|
|
2490
|
+
const x3 = Fp.mul(x2, x);
|
|
2491
|
+
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
2492
|
+
}
|
|
2493
|
+
__name(weierstrassEquation, "weierstrassEquation");
|
|
2494
|
+
function isValidXY(x, y) {
|
|
2495
|
+
const left = Fp.sqr(y);
|
|
2496
|
+
const right = weierstrassEquation(x);
|
|
2497
|
+
return Fp.eql(left, right);
|
|
2498
|
+
}
|
|
2499
|
+
__name(isValidXY, "isValidXY");
|
|
2500
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
|
2501
|
+
throw new Error("bad curve params: generator point");
|
|
2502
|
+
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n2), _4n2);
|
|
2503
|
+
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
2504
|
+
if (Fp.is0(Fp.add(_4a3, _27b2)))
|
|
2505
|
+
throw new Error("bad curve params: a or b");
|
|
2506
|
+
function acoord(title, n, banZero = false) {
|
|
2507
|
+
if (!Fp.isValid(n) || banZero && Fp.is0(n))
|
|
2508
|
+
throw new Error(`bad point coordinate ${title}`);
|
|
2509
|
+
return n;
|
|
2510
|
+
}
|
|
2511
|
+
__name(acoord, "acoord");
|
|
2512
|
+
function aprjpoint(other) {
|
|
2513
|
+
if (!(other instanceof Point))
|
|
2514
|
+
throw new Error("Weierstrass Point expected");
|
|
2515
|
+
}
|
|
2516
|
+
__name(aprjpoint, "aprjpoint");
|
|
2517
|
+
function splitEndoScalarN(k) {
|
|
2518
|
+
if (!endo || !endo.basises)
|
|
2519
|
+
throw new Error("no endo");
|
|
2520
|
+
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
2521
|
+
}
|
|
2522
|
+
__name(splitEndoScalarN, "splitEndoScalarN");
|
|
2523
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
2524
|
+
const { X, Y, Z } = p;
|
|
2525
|
+
if (Fp.eql(Z, Fp.ONE))
|
|
2526
|
+
return { x: X, y: Y };
|
|
2527
|
+
const is0 = p.is0();
|
|
2528
|
+
if (iz == null)
|
|
2529
|
+
iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
2530
|
+
const x = Fp.mul(X, iz);
|
|
2531
|
+
const y = Fp.mul(Y, iz);
|
|
2532
|
+
const zz = Fp.mul(Z, iz);
|
|
2533
|
+
if (is0)
|
|
2534
|
+
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
2535
|
+
if (!Fp.eql(zz, Fp.ONE))
|
|
2536
|
+
throw new Error("invZ was invalid");
|
|
2537
|
+
return { x, y };
|
|
2538
|
+
});
|
|
2539
|
+
const assertValidMemo = memoized((p) => {
|
|
2540
|
+
if (p.is0()) {
|
|
2541
|
+
if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
|
|
2542
|
+
return;
|
|
2543
|
+
throw new Error("bad point: ZERO");
|
|
2544
|
+
}
|
|
2545
|
+
const { x, y } = p.toAffine();
|
|
2546
|
+
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
2547
|
+
throw new Error("bad point: x or y not field elements");
|
|
2548
|
+
if (!isValidXY(x, y))
|
|
2549
|
+
throw new Error("bad point: equation left != right");
|
|
2550
|
+
if (!p.isTorsionFree())
|
|
2551
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
2552
|
+
return true;
|
|
2553
|
+
});
|
|
2554
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
2555
|
+
k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
2556
|
+
k1p = negateCt(k1neg, k1p);
|
|
2557
|
+
k2p = negateCt(k2neg, k2p);
|
|
2558
|
+
return k1p.add(k2p);
|
|
2559
|
+
}
|
|
2560
|
+
__name(finishEndo, "finishEndo");
|
|
2561
|
+
class Point {
|
|
2562
|
+
static {
|
|
2563
|
+
__name(this, "Point");
|
|
2564
|
+
}
|
|
2565
|
+
// base / generator point
|
|
2566
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
2567
|
+
// zero / infinity / identity point
|
|
2568
|
+
static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
2569
|
+
// 0, 1, 0
|
|
2570
|
+
// math field
|
|
2571
|
+
static Fp = Fp;
|
|
2572
|
+
// scalar field
|
|
2573
|
+
static Fn = Fn;
|
|
2574
|
+
X;
|
|
2575
|
+
Y;
|
|
2576
|
+
Z;
|
|
2577
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2578
|
+
constructor(X, Y, Z) {
|
|
2579
|
+
this.X = acoord("x", X);
|
|
2580
|
+
this.Y = acoord("y", Y, true);
|
|
2581
|
+
this.Z = acoord("z", Z);
|
|
2582
|
+
Object.freeze(this);
|
|
2583
|
+
}
|
|
2584
|
+
static CURVE() {
|
|
2585
|
+
return CURVE;
|
|
2586
|
+
}
|
|
2587
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2588
|
+
static fromAffine(p) {
|
|
2589
|
+
const { x, y } = p || {};
|
|
2590
|
+
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
2591
|
+
throw new Error("invalid affine point");
|
|
2592
|
+
if (p instanceof Point)
|
|
2593
|
+
throw new Error("projective point not allowed");
|
|
2594
|
+
if (Fp.is0(x) && Fp.is0(y))
|
|
2595
|
+
return Point.ZERO;
|
|
2596
|
+
return new Point(x, y, Fp.ONE);
|
|
2597
|
+
}
|
|
2598
|
+
static fromBytes(bytes) {
|
|
2599
|
+
const P = Point.fromAffine(decodePoint(abytes(bytes, void 0, "point")));
|
|
2600
|
+
P.assertValidity();
|
|
2601
|
+
return P;
|
|
2602
|
+
}
|
|
2603
|
+
static fromHex(hex) {
|
|
2604
|
+
return Point.fromBytes(hexToBytes(hex));
|
|
2605
|
+
}
|
|
2606
|
+
get x() {
|
|
2607
|
+
return this.toAffine().x;
|
|
2608
|
+
}
|
|
2609
|
+
get y() {
|
|
2610
|
+
return this.toAffine().y;
|
|
2611
|
+
}
|
|
2612
|
+
/**
|
|
2613
|
+
*
|
|
2614
|
+
* @param windowSize
|
|
2615
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
2616
|
+
* @returns
|
|
2617
|
+
*/
|
|
2618
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
2619
|
+
wnaf.createCache(this, windowSize);
|
|
2620
|
+
if (!isLazy)
|
|
2621
|
+
this.multiply(_3n2);
|
|
2622
|
+
return this;
|
|
2623
|
+
}
|
|
2624
|
+
// TODO: return `this`
|
|
2625
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
2626
|
+
assertValidity() {
|
|
2627
|
+
assertValidMemo(this);
|
|
2628
|
+
}
|
|
2629
|
+
hasEvenY() {
|
|
2630
|
+
const { y } = this.toAffine();
|
|
2631
|
+
if (!Fp.isOdd)
|
|
2632
|
+
throw new Error("Field doesn't support isOdd");
|
|
2633
|
+
return !Fp.isOdd(y);
|
|
2634
|
+
}
|
|
2635
|
+
/** Compare one point to another. */
|
|
2636
|
+
equals(other) {
|
|
2637
|
+
aprjpoint(other);
|
|
2638
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2639
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2640
|
+
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
2641
|
+
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
2642
|
+
return U1 && U2;
|
|
2643
|
+
}
|
|
2644
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
2645
|
+
negate() {
|
|
2646
|
+
return new Point(this.X, Fp.neg(this.Y), this.Z);
|
|
2647
|
+
}
|
|
2648
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
2649
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2650
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2651
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2652
|
+
double() {
|
|
2653
|
+
const { a, b } = CURVE;
|
|
2654
|
+
const b3 = Fp.mul(b, _3n2);
|
|
2655
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2656
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2657
|
+
let t0 = Fp.mul(X1, X1);
|
|
2658
|
+
let t1 = Fp.mul(Y1, Y1);
|
|
2659
|
+
let t2 = Fp.mul(Z1, Z1);
|
|
2660
|
+
let t3 = Fp.mul(X1, Y1);
|
|
2661
|
+
t3 = Fp.add(t3, t3);
|
|
2662
|
+
Z3 = Fp.mul(X1, Z1);
|
|
2663
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2664
|
+
X3 = Fp.mul(a, Z3);
|
|
2665
|
+
Y3 = Fp.mul(b3, t2);
|
|
2666
|
+
Y3 = Fp.add(X3, Y3);
|
|
2667
|
+
X3 = Fp.sub(t1, Y3);
|
|
2668
|
+
Y3 = Fp.add(t1, Y3);
|
|
2669
|
+
Y3 = Fp.mul(X3, Y3);
|
|
2670
|
+
X3 = Fp.mul(t3, X3);
|
|
2671
|
+
Z3 = Fp.mul(b3, Z3);
|
|
2672
|
+
t2 = Fp.mul(a, t2);
|
|
2673
|
+
t3 = Fp.sub(t0, t2);
|
|
2674
|
+
t3 = Fp.mul(a, t3);
|
|
2675
|
+
t3 = Fp.add(t3, Z3);
|
|
2676
|
+
Z3 = Fp.add(t0, t0);
|
|
2677
|
+
t0 = Fp.add(Z3, t0);
|
|
2678
|
+
t0 = Fp.add(t0, t2);
|
|
2679
|
+
t0 = Fp.mul(t0, t3);
|
|
2680
|
+
Y3 = Fp.add(Y3, t0);
|
|
2681
|
+
t2 = Fp.mul(Y1, Z1);
|
|
2682
|
+
t2 = Fp.add(t2, t2);
|
|
2683
|
+
t0 = Fp.mul(t2, t3);
|
|
2684
|
+
X3 = Fp.sub(X3, t0);
|
|
2685
|
+
Z3 = Fp.mul(t2, t1);
|
|
2686
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2687
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2688
|
+
return new Point(X3, Y3, Z3);
|
|
2689
|
+
}
|
|
2690
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
2691
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2692
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
2693
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2694
|
+
add(other) {
|
|
2695
|
+
aprjpoint(other);
|
|
2696
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2697
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2698
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2699
|
+
const a = CURVE.a;
|
|
2700
|
+
const b3 = Fp.mul(CURVE.b, _3n2);
|
|
2701
|
+
let t0 = Fp.mul(X1, X2);
|
|
2702
|
+
let t1 = Fp.mul(Y1, Y2);
|
|
2703
|
+
let t2 = Fp.mul(Z1, Z2);
|
|
2704
|
+
let t3 = Fp.add(X1, Y1);
|
|
2705
|
+
let t4 = Fp.add(X2, Y2);
|
|
2706
|
+
t3 = Fp.mul(t3, t4);
|
|
2707
|
+
t4 = Fp.add(t0, t1);
|
|
2708
|
+
t3 = Fp.sub(t3, t4);
|
|
2709
|
+
t4 = Fp.add(X1, Z1);
|
|
2710
|
+
let t5 = Fp.add(X2, Z2);
|
|
2711
|
+
t4 = Fp.mul(t4, t5);
|
|
2712
|
+
t5 = Fp.add(t0, t2);
|
|
2713
|
+
t4 = Fp.sub(t4, t5);
|
|
2714
|
+
t5 = Fp.add(Y1, Z1);
|
|
2715
|
+
X3 = Fp.add(Y2, Z2);
|
|
2716
|
+
t5 = Fp.mul(t5, X3);
|
|
2717
|
+
X3 = Fp.add(t1, t2);
|
|
2718
|
+
t5 = Fp.sub(t5, X3);
|
|
2719
|
+
Z3 = Fp.mul(a, t4);
|
|
2720
|
+
X3 = Fp.mul(b3, t2);
|
|
2721
|
+
Z3 = Fp.add(X3, Z3);
|
|
2722
|
+
X3 = Fp.sub(t1, Z3);
|
|
2723
|
+
Z3 = Fp.add(t1, Z3);
|
|
2724
|
+
Y3 = Fp.mul(X3, Z3);
|
|
2725
|
+
t1 = Fp.add(t0, t0);
|
|
2726
|
+
t1 = Fp.add(t1, t0);
|
|
2727
|
+
t2 = Fp.mul(a, t2);
|
|
2728
|
+
t4 = Fp.mul(b3, t4);
|
|
2729
|
+
t1 = Fp.add(t1, t2);
|
|
2730
|
+
t2 = Fp.sub(t0, t2);
|
|
2731
|
+
t2 = Fp.mul(a, t2);
|
|
2732
|
+
t4 = Fp.add(t4, t2);
|
|
2733
|
+
t0 = Fp.mul(t1, t4);
|
|
2734
|
+
Y3 = Fp.add(Y3, t0);
|
|
2735
|
+
t0 = Fp.mul(t5, t4);
|
|
2736
|
+
X3 = Fp.mul(t3, X3);
|
|
2737
|
+
X3 = Fp.sub(X3, t0);
|
|
2738
|
+
t0 = Fp.mul(t3, t1);
|
|
2739
|
+
Z3 = Fp.mul(t5, Z3);
|
|
2740
|
+
Z3 = Fp.add(Z3, t0);
|
|
2741
|
+
return new Point(X3, Y3, Z3);
|
|
2742
|
+
}
|
|
2743
|
+
subtract(other) {
|
|
2744
|
+
return this.add(other.negate());
|
|
2745
|
+
}
|
|
2746
|
+
is0() {
|
|
2747
|
+
return this.equals(Point.ZERO);
|
|
2748
|
+
}
|
|
2749
|
+
/**
|
|
2750
|
+
* Constant time multiplication.
|
|
2751
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
2752
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
2753
|
+
* Uses precomputes when available.
|
|
2754
|
+
* Uses endomorphism for Koblitz curves.
|
|
2755
|
+
* @param scalar by which the point would be multiplied
|
|
2756
|
+
* @returns New point
|
|
2757
|
+
*/
|
|
2758
|
+
multiply(scalar) {
|
|
2759
|
+
const { endo: endo2 } = extraOpts;
|
|
2760
|
+
if (!Fn.isValidNot0(scalar))
|
|
2761
|
+
throw new Error("invalid scalar: out of range");
|
|
2762
|
+
let point, fake;
|
|
2763
|
+
const mul = /* @__PURE__ */ __name((n) => wnaf.cached(this, n, (p) => normalizeZ(Point, p)), "mul");
|
|
2764
|
+
if (endo2) {
|
|
2765
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
|
|
2766
|
+
const { p: k1p, f: k1f } = mul(k1);
|
|
2767
|
+
const { p: k2p, f: k2f } = mul(k2);
|
|
2768
|
+
fake = k1f.add(k2f);
|
|
2769
|
+
point = finishEndo(endo2.beta, k1p, k2p, k1neg, k2neg);
|
|
2770
|
+
} else {
|
|
2771
|
+
const { p, f } = mul(scalar);
|
|
2772
|
+
point = p;
|
|
2773
|
+
fake = f;
|
|
2774
|
+
}
|
|
2775
|
+
return normalizeZ(Point, [point, fake])[0];
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
2779
|
+
* It's faster, but should only be used when you don't care about
|
|
2780
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
2781
|
+
*/
|
|
2782
|
+
multiplyUnsafe(sc) {
|
|
2783
|
+
const { endo: endo2 } = extraOpts;
|
|
2784
|
+
const p = this;
|
|
2785
|
+
if (!Fn.isValid(sc))
|
|
2786
|
+
throw new Error("invalid scalar: out of range");
|
|
2787
|
+
if (sc === _0n5 || p.is0())
|
|
2788
|
+
return Point.ZERO;
|
|
2789
|
+
if (sc === _1n6)
|
|
2790
|
+
return p;
|
|
2791
|
+
if (wnaf.hasCache(this))
|
|
2792
|
+
return this.multiply(sc);
|
|
2793
|
+
if (endo2) {
|
|
2794
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
|
|
2795
|
+
const { p1, p2 } = mulEndoUnsafe(Point, p, k1, k2);
|
|
2796
|
+
return finishEndo(endo2.beta, p1, p2, k1neg, k2neg);
|
|
2797
|
+
} else {
|
|
2798
|
+
return wnaf.unsafe(p, sc);
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2801
|
+
/**
|
|
2802
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
2803
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
2804
|
+
*/
|
|
2805
|
+
toAffine(invertedZ) {
|
|
2806
|
+
return toAffineMemo(this, invertedZ);
|
|
2807
|
+
}
|
|
2808
|
+
/**
|
|
2809
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
2810
|
+
* Always torsion-free for cofactor=1 curves.
|
|
2811
|
+
*/
|
|
2812
|
+
isTorsionFree() {
|
|
2813
|
+
const { isTorsionFree } = extraOpts;
|
|
2814
|
+
if (cofactor === _1n6)
|
|
2815
|
+
return true;
|
|
2816
|
+
if (isTorsionFree)
|
|
2817
|
+
return isTorsionFree(Point, this);
|
|
2818
|
+
return wnaf.unsafe(this, CURVE_ORDER).is0();
|
|
2819
|
+
}
|
|
2820
|
+
clearCofactor() {
|
|
2821
|
+
const { clearCofactor } = extraOpts;
|
|
2822
|
+
if (cofactor === _1n6)
|
|
2823
|
+
return this;
|
|
2824
|
+
if (clearCofactor)
|
|
2825
|
+
return clearCofactor(Point, this);
|
|
2826
|
+
return this.multiplyUnsafe(cofactor);
|
|
2827
|
+
}
|
|
2828
|
+
isSmallOrder() {
|
|
2829
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
2830
|
+
}
|
|
2831
|
+
toBytes(isCompressed = true) {
|
|
2832
|
+
abool(isCompressed, "isCompressed");
|
|
2833
|
+
this.assertValidity();
|
|
2834
|
+
return encodePoint(Point, this, isCompressed);
|
|
2835
|
+
}
|
|
2836
|
+
toHex(isCompressed = true) {
|
|
2837
|
+
return bytesToHex(this.toBytes(isCompressed));
|
|
2838
|
+
}
|
|
2839
|
+
toString() {
|
|
2840
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
const bits = Fn.BITS;
|
|
2844
|
+
const wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
|
|
2845
|
+
Point.BASE.precompute(8);
|
|
2846
|
+
return Point;
|
|
2847
|
+
}
|
|
2848
|
+
__name(weierstrass, "weierstrass");
|
|
2849
|
+
function pprefix(hasEvenY) {
|
|
2850
|
+
return Uint8Array.of(hasEvenY ? 2 : 3);
|
|
2851
|
+
}
|
|
2852
|
+
__name(pprefix, "pprefix");
|
|
2853
|
+
function getWLengths(Fp, Fn) {
|
|
2854
|
+
return {
|
|
2855
|
+
secretKey: Fn.BYTES,
|
|
2856
|
+
publicKey: 1 + Fp.BYTES,
|
|
2857
|
+
publicKeyUncompressed: 1 + 2 * Fp.BYTES,
|
|
2858
|
+
publicKeyHasPrefix: true,
|
|
2859
|
+
signature: 2 * Fn.BYTES
|
|
2860
|
+
};
|
|
2861
|
+
}
|
|
2862
|
+
__name(getWLengths, "getWLengths");
|
|
2863
|
+
function ecdh(Point, ecdhOpts = {}) {
|
|
2864
|
+
const { Fn } = Point;
|
|
2865
|
+
const randomBytes_ = ecdhOpts.randomBytes || randomBytes;
|
|
2866
|
+
const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: getMinHashLength(Fn.ORDER) });
|
|
2867
|
+
function isValidSecretKey(secretKey) {
|
|
2868
|
+
try {
|
|
2869
|
+
const num = Fn.fromBytes(secretKey);
|
|
2870
|
+
return Fn.isValidNot0(num);
|
|
2871
|
+
} catch (error) {
|
|
2872
|
+
return false;
|
|
2873
|
+
}
|
|
2874
|
+
}
|
|
2875
|
+
__name(isValidSecretKey, "isValidSecretKey");
|
|
2876
|
+
function isValidPublicKey(publicKey, isCompressed) {
|
|
2877
|
+
const { publicKey: comp, publicKeyUncompressed } = lengths;
|
|
2878
|
+
try {
|
|
2879
|
+
const l = publicKey.length;
|
|
2880
|
+
if (isCompressed === true && l !== comp)
|
|
2881
|
+
return false;
|
|
2882
|
+
if (isCompressed === false && l !== publicKeyUncompressed)
|
|
2883
|
+
return false;
|
|
2884
|
+
return !!Point.fromBytes(publicKey);
|
|
2885
|
+
} catch (error) {
|
|
2886
|
+
return false;
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
__name(isValidPublicKey, "isValidPublicKey");
|
|
2890
|
+
function randomSecretKey(seed = randomBytes_(lengths.seed)) {
|
|
2891
|
+
return mapHashToField(abytes(seed, lengths.seed, "seed"), Fn.ORDER);
|
|
2892
|
+
}
|
|
2893
|
+
__name(randomSecretKey, "randomSecretKey");
|
|
2894
|
+
function getPublicKey(secretKey, isCompressed = true) {
|
|
2895
|
+
return Point.BASE.multiply(Fn.fromBytes(secretKey)).toBytes(isCompressed);
|
|
2896
|
+
}
|
|
2897
|
+
__name(getPublicKey, "getPublicKey");
|
|
2898
|
+
function isProbPub(item) {
|
|
2899
|
+
const { secretKey, publicKey, publicKeyUncompressed } = lengths;
|
|
2900
|
+
if (!isBytes(item))
|
|
2901
|
+
return void 0;
|
|
2902
|
+
if ("_lengths" in Fn && Fn._lengths || secretKey === publicKey)
|
|
2903
|
+
return void 0;
|
|
2904
|
+
const l = abytes(item, void 0, "key").length;
|
|
2905
|
+
return l === publicKey || l === publicKeyUncompressed;
|
|
2906
|
+
}
|
|
2907
|
+
__name(isProbPub, "isProbPub");
|
|
2908
|
+
function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
|
|
2909
|
+
if (isProbPub(secretKeyA) === true)
|
|
2910
|
+
throw new Error("first arg must be private key");
|
|
2911
|
+
if (isProbPub(publicKeyB) === false)
|
|
2912
|
+
throw new Error("second arg must be public key");
|
|
2913
|
+
const s = Fn.fromBytes(secretKeyA);
|
|
2914
|
+
const b = Point.fromBytes(publicKeyB);
|
|
2915
|
+
return b.multiply(s).toBytes(isCompressed);
|
|
2916
|
+
}
|
|
2917
|
+
__name(getSharedSecret, "getSharedSecret");
|
|
2918
|
+
const utils = {
|
|
2919
|
+
isValidSecretKey,
|
|
2920
|
+
isValidPublicKey,
|
|
2921
|
+
randomSecretKey
|
|
2922
|
+
};
|
|
2923
|
+
const keygen = createKeygen(randomSecretKey, getPublicKey);
|
|
2924
|
+
return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
|
|
2925
|
+
}
|
|
2926
|
+
__name(ecdh, "ecdh");
|
|
2927
|
+
function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
2928
|
+
ahash(hash);
|
|
2929
|
+
validateObject(ecdsaOpts, {}, {
|
|
2930
|
+
hmac: "function",
|
|
2931
|
+
lowS: "boolean",
|
|
2932
|
+
randomBytes: "function",
|
|
2933
|
+
bits2int: "function",
|
|
2934
|
+
bits2int_modN: "function"
|
|
2935
|
+
});
|
|
2936
|
+
ecdsaOpts = Object.assign({}, ecdsaOpts);
|
|
2937
|
+
const randomBytes2 = ecdsaOpts.randomBytes || randomBytes;
|
|
2938
|
+
const hmac2 = ecdsaOpts.hmac || ((key, msg) => hmac(hash, key, msg));
|
|
2939
|
+
const { Fp, Fn } = Point;
|
|
2940
|
+
const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
|
|
2941
|
+
const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
|
|
2942
|
+
const defaultSigOpts = {
|
|
2943
|
+
prehash: true,
|
|
2944
|
+
lowS: typeof ecdsaOpts.lowS === "boolean" ? ecdsaOpts.lowS : true,
|
|
2945
|
+
format: "compact",
|
|
2946
|
+
extraEntropy: false
|
|
2947
|
+
};
|
|
2948
|
+
const hasLargeCofactor = CURVE_ORDER * _2n4 < Fp.ORDER;
|
|
2949
|
+
function isBiggerThanHalfOrder(number) {
|
|
2950
|
+
const HALF = CURVE_ORDER >> _1n6;
|
|
2951
|
+
return number > HALF;
|
|
2952
|
+
}
|
|
2953
|
+
__name(isBiggerThanHalfOrder, "isBiggerThanHalfOrder");
|
|
2954
|
+
function validateRS(title, num) {
|
|
2955
|
+
if (!Fn.isValidNot0(num))
|
|
2956
|
+
throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
|
|
2957
|
+
return num;
|
|
2958
|
+
}
|
|
2959
|
+
__name(validateRS, "validateRS");
|
|
2960
|
+
function assertSmallCofactor() {
|
|
2961
|
+
if (hasLargeCofactor)
|
|
2962
|
+
throw new Error('"recovered" sig type is not supported for cofactor >2 curves');
|
|
2963
|
+
}
|
|
2964
|
+
__name(assertSmallCofactor, "assertSmallCofactor");
|
|
2965
|
+
function validateSigLength(bytes, format) {
|
|
2966
|
+
validateSigFormat(format);
|
|
2967
|
+
const size = lengths.signature;
|
|
2968
|
+
const sizer = format === "compact" ? size : format === "recovered" ? size + 1 : void 0;
|
|
2969
|
+
return abytes(bytes, sizer);
|
|
2970
|
+
}
|
|
2971
|
+
__name(validateSigLength, "validateSigLength");
|
|
2972
|
+
class Signature {
|
|
2973
|
+
static {
|
|
2974
|
+
__name(this, "Signature");
|
|
2975
|
+
}
|
|
2976
|
+
r;
|
|
2977
|
+
s;
|
|
2978
|
+
recovery;
|
|
2979
|
+
constructor(r, s, recovery) {
|
|
2980
|
+
this.r = validateRS("r", r);
|
|
2981
|
+
this.s = validateRS("s", s);
|
|
2982
|
+
if (recovery != null) {
|
|
2983
|
+
assertSmallCofactor();
|
|
2984
|
+
if (![0, 1, 2, 3].includes(recovery))
|
|
2985
|
+
throw new Error("invalid recovery id");
|
|
2986
|
+
this.recovery = recovery;
|
|
2987
|
+
}
|
|
2988
|
+
Object.freeze(this);
|
|
2989
|
+
}
|
|
2990
|
+
static fromBytes(bytes, format = defaultSigOpts.format) {
|
|
2991
|
+
validateSigLength(bytes, format);
|
|
2992
|
+
let recid;
|
|
2993
|
+
if (format === "der") {
|
|
2994
|
+
const { r: r2, s: s2 } = DER.toSig(abytes(bytes));
|
|
2995
|
+
return new Signature(r2, s2);
|
|
2996
|
+
}
|
|
2997
|
+
if (format === "recovered") {
|
|
2998
|
+
recid = bytes[0];
|
|
2999
|
+
format = "compact";
|
|
3000
|
+
bytes = bytes.subarray(1);
|
|
3001
|
+
}
|
|
3002
|
+
const L = lengths.signature / 2;
|
|
3003
|
+
const r = bytes.subarray(0, L);
|
|
3004
|
+
const s = bytes.subarray(L, L * 2);
|
|
3005
|
+
return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
|
|
3006
|
+
}
|
|
3007
|
+
static fromHex(hex, format) {
|
|
3008
|
+
return this.fromBytes(hexToBytes(hex), format);
|
|
3009
|
+
}
|
|
3010
|
+
assertRecovery() {
|
|
3011
|
+
const { recovery } = this;
|
|
3012
|
+
if (recovery == null)
|
|
3013
|
+
throw new Error("invalid recovery id: must be present");
|
|
3014
|
+
return recovery;
|
|
3015
|
+
}
|
|
3016
|
+
addRecoveryBit(recovery) {
|
|
3017
|
+
return new Signature(this.r, this.s, recovery);
|
|
3018
|
+
}
|
|
3019
|
+
recoverPublicKey(messageHash) {
|
|
3020
|
+
const { r, s } = this;
|
|
3021
|
+
const recovery = this.assertRecovery();
|
|
3022
|
+
const radj = recovery === 2 || recovery === 3 ? r + CURVE_ORDER : r;
|
|
3023
|
+
if (!Fp.isValid(radj))
|
|
3024
|
+
throw new Error("invalid recovery id: sig.r+curve.n != R.x");
|
|
3025
|
+
const x = Fp.toBytes(radj);
|
|
3026
|
+
const R = Point.fromBytes(concatBytes(pprefix((recovery & 1) === 0), x));
|
|
3027
|
+
const ir = Fn.inv(radj);
|
|
3028
|
+
const h = bits2int_modN(abytes(messageHash, void 0, "msgHash"));
|
|
3029
|
+
const u1 = Fn.create(-h * ir);
|
|
3030
|
+
const u2 = Fn.create(s * ir);
|
|
3031
|
+
const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
|
|
3032
|
+
if (Q.is0())
|
|
3033
|
+
throw new Error("invalid recovery: point at infinify");
|
|
3034
|
+
Q.assertValidity();
|
|
3035
|
+
return Q;
|
|
3036
|
+
}
|
|
3037
|
+
// Signatures should be low-s, to prevent malleability.
|
|
3038
|
+
hasHighS() {
|
|
3039
|
+
return isBiggerThanHalfOrder(this.s);
|
|
3040
|
+
}
|
|
3041
|
+
toBytes(format = defaultSigOpts.format) {
|
|
3042
|
+
validateSigFormat(format);
|
|
3043
|
+
if (format === "der")
|
|
3044
|
+
return hexToBytes(DER.hexFromSig(this));
|
|
3045
|
+
const { r, s } = this;
|
|
3046
|
+
const rb = Fn.toBytes(r);
|
|
3047
|
+
const sb = Fn.toBytes(s);
|
|
3048
|
+
if (format === "recovered") {
|
|
3049
|
+
assertSmallCofactor();
|
|
3050
|
+
return concatBytes(Uint8Array.of(this.assertRecovery()), rb, sb);
|
|
3051
|
+
}
|
|
3052
|
+
return concatBytes(rb, sb);
|
|
3053
|
+
}
|
|
3054
|
+
toHex(format) {
|
|
3055
|
+
return bytesToHex(this.toBytes(format));
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
const bits2int = ecdsaOpts.bits2int || /* @__PURE__ */ __name(function bits2int_def(bytes) {
|
|
3059
|
+
if (bytes.length > 8192)
|
|
3060
|
+
throw new Error("input is too large");
|
|
3061
|
+
const num = bytesToNumberBE(bytes);
|
|
3062
|
+
const delta = bytes.length * 8 - fnBits;
|
|
3063
|
+
return delta > 0 ? num >> BigInt(delta) : num;
|
|
3064
|
+
}, "bits2int_def");
|
|
3065
|
+
const bits2int_modN = ecdsaOpts.bits2int_modN || /* @__PURE__ */ __name(function bits2int_modN_def(bytes) {
|
|
3066
|
+
return Fn.create(bits2int(bytes));
|
|
3067
|
+
}, "bits2int_modN_def");
|
|
3068
|
+
const ORDER_MASK = bitMask(fnBits);
|
|
3069
|
+
function int2octets(num) {
|
|
3070
|
+
aInRange("num < 2^" + fnBits, num, _0n5, ORDER_MASK);
|
|
3071
|
+
return Fn.toBytes(num);
|
|
3072
|
+
}
|
|
3073
|
+
__name(int2octets, "int2octets");
|
|
3074
|
+
function validateMsgAndHash(message, prehash) {
|
|
3075
|
+
abytes(message, void 0, "message");
|
|
3076
|
+
return prehash ? abytes(hash(message), void 0, "prehashed message") : message;
|
|
3077
|
+
}
|
|
3078
|
+
__name(validateMsgAndHash, "validateMsgAndHash");
|
|
3079
|
+
function prepSig(message, secretKey, opts) {
|
|
3080
|
+
const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
|
|
3081
|
+
message = validateMsgAndHash(message, prehash);
|
|
3082
|
+
const h1int = bits2int_modN(message);
|
|
3083
|
+
const d = Fn.fromBytes(secretKey);
|
|
3084
|
+
if (!Fn.isValidNot0(d))
|
|
3085
|
+
throw new Error("invalid private key");
|
|
3086
|
+
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
3087
|
+
if (extraEntropy != null && extraEntropy !== false) {
|
|
3088
|
+
const e = extraEntropy === true ? randomBytes2(lengths.secretKey) : extraEntropy;
|
|
3089
|
+
seedArgs.push(abytes(e, void 0, "extraEntropy"));
|
|
3090
|
+
}
|
|
3091
|
+
const seed = concatBytes(...seedArgs);
|
|
3092
|
+
const m = h1int;
|
|
3093
|
+
function k2sig(kBytes) {
|
|
3094
|
+
const k = bits2int(kBytes);
|
|
3095
|
+
if (!Fn.isValidNot0(k))
|
|
3096
|
+
return;
|
|
3097
|
+
const ik = Fn.inv(k);
|
|
3098
|
+
const q = Point.BASE.multiply(k).toAffine();
|
|
3099
|
+
const r = Fn.create(q.x);
|
|
3100
|
+
if (r === _0n5)
|
|
3101
|
+
return;
|
|
3102
|
+
const s = Fn.create(ik * Fn.create(m + r * d));
|
|
3103
|
+
if (s === _0n5)
|
|
3104
|
+
return;
|
|
3105
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n6);
|
|
3106
|
+
let normS = s;
|
|
3107
|
+
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
3108
|
+
normS = Fn.neg(s);
|
|
3109
|
+
recovery ^= 1;
|
|
3110
|
+
}
|
|
3111
|
+
return new Signature(r, normS, hasLargeCofactor ? void 0 : recovery);
|
|
3112
|
+
}
|
|
3113
|
+
__name(k2sig, "k2sig");
|
|
3114
|
+
return { seed, k2sig };
|
|
3115
|
+
}
|
|
3116
|
+
__name(prepSig, "prepSig");
|
|
3117
|
+
function sign(message, secretKey, opts = {}) {
|
|
3118
|
+
const { seed, k2sig } = prepSig(message, secretKey, opts);
|
|
3119
|
+
const drbg = createHmacDrbg(hash.outputLen, Fn.BYTES, hmac2);
|
|
3120
|
+
const sig = drbg(seed, k2sig);
|
|
3121
|
+
return sig.toBytes(opts.format);
|
|
3122
|
+
}
|
|
3123
|
+
__name(sign, "sign");
|
|
3124
|
+
function verify(signature, message, publicKey, opts = {}) {
|
|
3125
|
+
const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
|
|
3126
|
+
publicKey = abytes(publicKey, void 0, "publicKey");
|
|
3127
|
+
message = validateMsgAndHash(message, prehash);
|
|
3128
|
+
if (!isBytes(signature)) {
|
|
3129
|
+
const end = signature instanceof Signature ? ", use sig.toBytes()" : "";
|
|
3130
|
+
throw new Error("verify expects Uint8Array signature" + end);
|
|
3131
|
+
}
|
|
3132
|
+
validateSigLength(signature, format);
|
|
3133
|
+
try {
|
|
3134
|
+
const sig = Signature.fromBytes(signature, format);
|
|
3135
|
+
const P = Point.fromBytes(publicKey);
|
|
3136
|
+
if (lowS && sig.hasHighS())
|
|
3137
|
+
return false;
|
|
3138
|
+
const { r, s } = sig;
|
|
3139
|
+
const h = bits2int_modN(message);
|
|
3140
|
+
const is = Fn.inv(s);
|
|
3141
|
+
const u1 = Fn.create(h * is);
|
|
3142
|
+
const u2 = Fn.create(r * is);
|
|
3143
|
+
const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2));
|
|
3144
|
+
if (R.is0())
|
|
3145
|
+
return false;
|
|
3146
|
+
const v = Fn.create(R.x);
|
|
3147
|
+
return v === r;
|
|
3148
|
+
} catch (e) {
|
|
3149
|
+
return false;
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
__name(verify, "verify");
|
|
3153
|
+
function recoverPublicKey(signature, message, opts = {}) {
|
|
3154
|
+
const { prehash } = validateSigOpts(opts, defaultSigOpts);
|
|
3155
|
+
message = validateMsgAndHash(message, prehash);
|
|
3156
|
+
return Signature.fromBytes(signature, "recovered").recoverPublicKey(message).toBytes();
|
|
3157
|
+
}
|
|
3158
|
+
__name(recoverPublicKey, "recoverPublicKey");
|
|
3159
|
+
return Object.freeze({
|
|
3160
|
+
keygen,
|
|
3161
|
+
getPublicKey,
|
|
3162
|
+
getSharedSecret,
|
|
3163
|
+
utils,
|
|
3164
|
+
lengths,
|
|
3165
|
+
Point,
|
|
3166
|
+
sign,
|
|
3167
|
+
verify,
|
|
3168
|
+
recoverPublicKey,
|
|
3169
|
+
Signature,
|
|
3170
|
+
hash
|
|
3171
|
+
});
|
|
3172
|
+
}
|
|
3173
|
+
__name(ecdsa, "ecdsa");
|
|
3174
|
+
|
|
3175
|
+
// ../../node_modules/@noble/curves/secp256k1.js
|
|
3176
|
+
var secp256k1_CURVE = {
|
|
3177
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
3178
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
3179
|
+
h: BigInt(1),
|
|
3180
|
+
a: BigInt(0),
|
|
3181
|
+
b: BigInt(7),
|
|
3182
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
3183
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
3184
|
+
};
|
|
3185
|
+
var secp256k1_ENDO = {
|
|
3186
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
3187
|
+
basises: [
|
|
3188
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
3189
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
3190
|
+
]
|
|
3191
|
+
};
|
|
3192
|
+
var _2n5 = /* @__PURE__ */ BigInt(2);
|
|
3193
|
+
function sqrtMod(y) {
|
|
3194
|
+
const P = secp256k1_CURVE.p;
|
|
3195
|
+
const _3n3 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
3196
|
+
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
3197
|
+
const b2 = y * y * y % P;
|
|
3198
|
+
const b3 = b2 * b2 * y % P;
|
|
3199
|
+
const b6 = pow2(b3, _3n3, P) * b3 % P;
|
|
3200
|
+
const b9 = pow2(b6, _3n3, P) * b3 % P;
|
|
3201
|
+
const b11 = pow2(b9, _2n5, P) * b2 % P;
|
|
3202
|
+
const b22 = pow2(b11, _11n, P) * b11 % P;
|
|
3203
|
+
const b44 = pow2(b22, _22n, P) * b22 % P;
|
|
3204
|
+
const b88 = pow2(b44, _44n, P) * b44 % P;
|
|
3205
|
+
const b176 = pow2(b88, _88n, P) * b88 % P;
|
|
3206
|
+
const b220 = pow2(b176, _44n, P) * b44 % P;
|
|
3207
|
+
const b223 = pow2(b220, _3n3, P) * b3 % P;
|
|
3208
|
+
const t1 = pow2(b223, _23n, P) * b22 % P;
|
|
3209
|
+
const t2 = pow2(t1, _6n, P) * b2 % P;
|
|
3210
|
+
const root = pow2(t2, _2n5, P);
|
|
3211
|
+
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
|
3212
|
+
throw new Error("Cannot find square root");
|
|
3213
|
+
return root;
|
|
3214
|
+
}
|
|
3215
|
+
__name(sqrtMod, "sqrtMod");
|
|
3216
|
+
var Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
|
|
3217
|
+
var Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
|
|
3218
|
+
Fp: Fpk1,
|
|
3219
|
+
endo: secp256k1_ENDO
|
|
3220
|
+
});
|
|
3221
|
+
var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha256);
|
|
3222
|
+
|
|
2451
3223
|
// ../utils/node_modules/base58-js/base58_chars.js
|
|
2452
3224
|
var base58_chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
2453
3225
|
var base58_chars_default = base58_chars;
|
|
@@ -2517,493 +3289,6 @@ var nearWalletAdapters = (() => {
|
|
|
2517
3289
|
__name(binary_to_base58, "binary_to_base58");
|
|
2518
3290
|
var binary_to_base58_default = binary_to_base58;
|
|
2519
3291
|
|
|
2520
|
-
// ../../node_modules/big.js/big.mjs
|
|
2521
|
-
var DP = 20;
|
|
2522
|
-
var RM = 1;
|
|
2523
|
-
var MAX_DP = 1e6;
|
|
2524
|
-
var MAX_POWER = 1e6;
|
|
2525
|
-
var NE = -7;
|
|
2526
|
-
var PE = 21;
|
|
2527
|
-
var STRICT = false;
|
|
2528
|
-
var NAME = "[big.js] ";
|
|
2529
|
-
var INVALID = NAME + "Invalid ";
|
|
2530
|
-
var INVALID_DP = INVALID + "decimal places";
|
|
2531
|
-
var INVALID_RM = INVALID + "rounding mode";
|
|
2532
|
-
var DIV_BY_ZERO = NAME + "Division by zero";
|
|
2533
|
-
var P = {};
|
|
2534
|
-
var UNDEFINED = void 0;
|
|
2535
|
-
var NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
2536
|
-
function _Big_() {
|
|
2537
|
-
function Big2(n) {
|
|
2538
|
-
var x = this;
|
|
2539
|
-
if (!(x instanceof Big2)) return n === UNDEFINED ? _Big_() : new Big2(n);
|
|
2540
|
-
if (n instanceof Big2) {
|
|
2541
|
-
x.s = n.s;
|
|
2542
|
-
x.e = n.e;
|
|
2543
|
-
x.c = n.c.slice();
|
|
2544
|
-
} else {
|
|
2545
|
-
if (typeof n !== "string") {
|
|
2546
|
-
if (Big2.strict === true && typeof n !== "bigint") {
|
|
2547
|
-
throw TypeError(INVALID + "value");
|
|
2548
|
-
}
|
|
2549
|
-
n = n === 0 && 1 / n < 0 ? "-0" : String(n);
|
|
2550
|
-
}
|
|
2551
|
-
parse(x, n);
|
|
2552
|
-
}
|
|
2553
|
-
x.constructor = Big2;
|
|
2554
|
-
}
|
|
2555
|
-
__name(Big2, "Big");
|
|
2556
|
-
Big2.prototype = P;
|
|
2557
|
-
Big2.DP = DP;
|
|
2558
|
-
Big2.RM = RM;
|
|
2559
|
-
Big2.NE = NE;
|
|
2560
|
-
Big2.PE = PE;
|
|
2561
|
-
Big2.strict = STRICT;
|
|
2562
|
-
Big2.roundDown = 0;
|
|
2563
|
-
Big2.roundHalfUp = 1;
|
|
2564
|
-
Big2.roundHalfEven = 2;
|
|
2565
|
-
Big2.roundUp = 3;
|
|
2566
|
-
return Big2;
|
|
2567
|
-
}
|
|
2568
|
-
__name(_Big_, "_Big_");
|
|
2569
|
-
function parse(x, n) {
|
|
2570
|
-
var e, i, nl;
|
|
2571
|
-
if (!NUMERIC.test(n)) {
|
|
2572
|
-
throw Error(INVALID + "number");
|
|
2573
|
-
}
|
|
2574
|
-
x.s = n.charAt(0) == "-" ? (n = n.slice(1), -1) : 1;
|
|
2575
|
-
if ((e = n.indexOf(".")) > -1) n = n.replace(".", "");
|
|
2576
|
-
if ((i = n.search(/e/i)) > 0) {
|
|
2577
|
-
if (e < 0) e = i;
|
|
2578
|
-
e += +n.slice(i + 1);
|
|
2579
|
-
n = n.substring(0, i);
|
|
2580
|
-
} else if (e < 0) {
|
|
2581
|
-
e = n.length;
|
|
2582
|
-
}
|
|
2583
|
-
nl = n.length;
|
|
2584
|
-
for (i = 0; i < nl && n.charAt(i) == "0"; ) ++i;
|
|
2585
|
-
if (i == nl) {
|
|
2586
|
-
x.c = [x.e = 0];
|
|
2587
|
-
} else {
|
|
2588
|
-
for (; nl > 0 && n.charAt(--nl) == "0"; ) ;
|
|
2589
|
-
x.e = e - i - 1;
|
|
2590
|
-
x.c = [];
|
|
2591
|
-
for (e = 0; i <= nl; ) x.c[e++] = +n.charAt(i++);
|
|
2592
|
-
}
|
|
2593
|
-
return x;
|
|
2594
|
-
}
|
|
2595
|
-
__name(parse, "parse");
|
|
2596
|
-
function round(x, sd, rm, more) {
|
|
2597
|
-
var xc = x.c;
|
|
2598
|
-
if (rm === UNDEFINED) rm = x.constructor.RM;
|
|
2599
|
-
if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) {
|
|
2600
|
-
throw Error(INVALID_RM);
|
|
2601
|
-
}
|
|
2602
|
-
if (sd < 1) {
|
|
2603
|
-
more = rm === 3 && (more || !!xc[0]) || sd === 0 && (rm === 1 && xc[0] >= 5 || rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED)));
|
|
2604
|
-
xc.length = 1;
|
|
2605
|
-
if (more) {
|
|
2606
|
-
x.e = x.e - sd + 1;
|
|
2607
|
-
xc[0] = 1;
|
|
2608
|
-
} else {
|
|
2609
|
-
xc[0] = x.e = 0;
|
|
2610
|
-
}
|
|
2611
|
-
} else if (sd < xc.length) {
|
|
2612
|
-
more = rm === 1 && xc[sd] >= 5 || rm === 2 && (xc[sd] > 5 || xc[sd] === 5 && (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) || rm === 3 && (more || !!xc[0]);
|
|
2613
|
-
xc.length = sd;
|
|
2614
|
-
if (more) {
|
|
2615
|
-
for (; ++xc[--sd] > 9; ) {
|
|
2616
|
-
xc[sd] = 0;
|
|
2617
|
-
if (sd === 0) {
|
|
2618
|
-
++x.e;
|
|
2619
|
-
xc.unshift(1);
|
|
2620
|
-
break;
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
}
|
|
2624
|
-
for (sd = xc.length; !xc[--sd]; ) xc.pop();
|
|
2625
|
-
}
|
|
2626
|
-
return x;
|
|
2627
|
-
}
|
|
2628
|
-
__name(round, "round");
|
|
2629
|
-
function stringify(x, doExponential, isNonzero) {
|
|
2630
|
-
var e = x.e, s = x.c.join(""), n = s.length;
|
|
2631
|
-
if (doExponential) {
|
|
2632
|
-
s = s.charAt(0) + (n > 1 ? "." + s.slice(1) : "") + (e < 0 ? "e" : "e+") + e;
|
|
2633
|
-
} else if (e < 0) {
|
|
2634
|
-
for (; ++e; ) s = "0" + s;
|
|
2635
|
-
s = "0." + s;
|
|
2636
|
-
} else if (e > 0) {
|
|
2637
|
-
if (++e > n) {
|
|
2638
|
-
for (e -= n; e--; ) s += "0";
|
|
2639
|
-
} else if (e < n) {
|
|
2640
|
-
s = s.slice(0, e) + "." + s.slice(e);
|
|
2641
|
-
}
|
|
2642
|
-
} else if (n > 1) {
|
|
2643
|
-
s = s.charAt(0) + "." + s.slice(1);
|
|
2644
|
-
}
|
|
2645
|
-
return x.s < 0 && isNonzero ? "-" + s : s;
|
|
2646
|
-
}
|
|
2647
|
-
__name(stringify, "stringify");
|
|
2648
|
-
P.abs = function() {
|
|
2649
|
-
var x = new this.constructor(this);
|
|
2650
|
-
x.s = 1;
|
|
2651
|
-
return x;
|
|
2652
|
-
};
|
|
2653
|
-
P.cmp = function(y) {
|
|
2654
|
-
var isneg, x = this, xc = x.c, yc = (y = new x.constructor(y)).c, i = x.s, j = y.s, k = x.e, l = y.e;
|
|
2655
|
-
if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
|
|
2656
|
-
if (i != j) return i;
|
|
2657
|
-
isneg = i < 0;
|
|
2658
|
-
if (k != l) return k > l ^ isneg ? 1 : -1;
|
|
2659
|
-
j = (k = xc.length) < (l = yc.length) ? k : l;
|
|
2660
|
-
for (i = -1; ++i < j; ) {
|
|
2661
|
-
if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
|
|
2662
|
-
}
|
|
2663
|
-
return k == l ? 0 : k > l ^ isneg ? 1 : -1;
|
|
2664
|
-
};
|
|
2665
|
-
P.div = function(y) {
|
|
2666
|
-
var x = this, Big2 = x.constructor, a = x.c, b = (y = new Big2(y)).c, k = x.s == y.s ? 1 : -1, dp = Big2.DP;
|
|
2667
|
-
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
|
|
2668
|
-
throw Error(INVALID_DP);
|
|
2669
|
-
}
|
|
2670
|
-
if (!b[0]) {
|
|
2671
|
-
throw Error(DIV_BY_ZERO);
|
|
2672
|
-
}
|
|
2673
|
-
if (!a[0]) {
|
|
2674
|
-
y.s = k;
|
|
2675
|
-
y.c = [y.e = 0];
|
|
2676
|
-
return y;
|
|
2677
|
-
}
|
|
2678
|
-
var bl, bt, n, cmp, ri, bz = b.slice(), ai = bl = b.length, al = a.length, r = a.slice(0, bl), rl = r.length, q = y, qc = q.c = [], qi = 0, p = dp + (q.e = x.e - y.e) + 1;
|
|
2679
|
-
q.s = k;
|
|
2680
|
-
k = p < 0 ? 0 : p;
|
|
2681
|
-
bz.unshift(0);
|
|
2682
|
-
for (; rl++ < bl; ) r.push(0);
|
|
2683
|
-
do {
|
|
2684
|
-
for (n = 0; n < 10; n++) {
|
|
2685
|
-
if (bl != (rl = r.length)) {
|
|
2686
|
-
cmp = bl > rl ? 1 : -1;
|
|
2687
|
-
} else {
|
|
2688
|
-
for (ri = -1, cmp = 0; ++ri < bl; ) {
|
|
2689
|
-
if (b[ri] != r[ri]) {
|
|
2690
|
-
cmp = b[ri] > r[ri] ? 1 : -1;
|
|
2691
|
-
break;
|
|
2692
|
-
}
|
|
2693
|
-
}
|
|
2694
|
-
}
|
|
2695
|
-
if (cmp < 0) {
|
|
2696
|
-
for (bt = rl == bl ? b : bz; rl; ) {
|
|
2697
|
-
if (r[--rl] < bt[rl]) {
|
|
2698
|
-
ri = rl;
|
|
2699
|
-
for (; ri && !r[--ri]; ) r[ri] = 9;
|
|
2700
|
-
--r[ri];
|
|
2701
|
-
r[rl] += 10;
|
|
2702
|
-
}
|
|
2703
|
-
r[rl] -= bt[rl];
|
|
2704
|
-
}
|
|
2705
|
-
for (; !r[0]; ) r.shift();
|
|
2706
|
-
} else {
|
|
2707
|
-
break;
|
|
2708
|
-
}
|
|
2709
|
-
}
|
|
2710
|
-
qc[qi++] = cmp ? n : ++n;
|
|
2711
|
-
if (r[0] && cmp) r[rl] = a[ai] || 0;
|
|
2712
|
-
else r = [a[ai]];
|
|
2713
|
-
} while ((ai++ < al || r[0] !== UNDEFINED) && k--);
|
|
2714
|
-
if (!qc[0] && qi != 1) {
|
|
2715
|
-
qc.shift();
|
|
2716
|
-
q.e--;
|
|
2717
|
-
p--;
|
|
2718
|
-
}
|
|
2719
|
-
if (qi > p) round(q, p, Big2.RM, r[0] !== UNDEFINED);
|
|
2720
|
-
return q;
|
|
2721
|
-
};
|
|
2722
|
-
P.eq = function(y) {
|
|
2723
|
-
return this.cmp(y) === 0;
|
|
2724
|
-
};
|
|
2725
|
-
P.gt = function(y) {
|
|
2726
|
-
return this.cmp(y) > 0;
|
|
2727
|
-
};
|
|
2728
|
-
P.gte = function(y) {
|
|
2729
|
-
return this.cmp(y) > -1;
|
|
2730
|
-
};
|
|
2731
|
-
P.lt = function(y) {
|
|
2732
|
-
return this.cmp(y) < 0;
|
|
2733
|
-
};
|
|
2734
|
-
P.lte = function(y) {
|
|
2735
|
-
return this.cmp(y) < 1;
|
|
2736
|
-
};
|
|
2737
|
-
P.minus = P.sub = function(y) {
|
|
2738
|
-
var i, j, t, xlty, x = this, Big2 = x.constructor, a = x.s, b = (y = new Big2(y)).s;
|
|
2739
|
-
if (a != b) {
|
|
2740
|
-
y.s = -b;
|
|
2741
|
-
return x.plus(y);
|
|
2742
|
-
}
|
|
2743
|
-
var xc = x.c.slice(), xe = x.e, yc = y.c, ye = y.e;
|
|
2744
|
-
if (!xc[0] || !yc[0]) {
|
|
2745
|
-
if (yc[0]) {
|
|
2746
|
-
y.s = -b;
|
|
2747
|
-
} else if (xc[0]) {
|
|
2748
|
-
y = new Big2(x);
|
|
2749
|
-
} else {
|
|
2750
|
-
y.s = 1;
|
|
2751
|
-
}
|
|
2752
|
-
return y;
|
|
2753
|
-
}
|
|
2754
|
-
if (a = xe - ye) {
|
|
2755
|
-
if (xlty = a < 0) {
|
|
2756
|
-
a = -a;
|
|
2757
|
-
t = xc;
|
|
2758
|
-
} else {
|
|
2759
|
-
ye = xe;
|
|
2760
|
-
t = yc;
|
|
2761
|
-
}
|
|
2762
|
-
t.reverse();
|
|
2763
|
-
for (b = a; b--; ) t.push(0);
|
|
2764
|
-
t.reverse();
|
|
2765
|
-
} else {
|
|
2766
|
-
j = ((xlty = xc.length < yc.length) ? xc : yc).length;
|
|
2767
|
-
for (a = b = 0; b < j; b++) {
|
|
2768
|
-
if (xc[b] != yc[b]) {
|
|
2769
|
-
xlty = xc[b] < yc[b];
|
|
2770
|
-
break;
|
|
2771
|
-
}
|
|
2772
|
-
}
|
|
2773
|
-
}
|
|
2774
|
-
if (xlty) {
|
|
2775
|
-
t = xc;
|
|
2776
|
-
xc = yc;
|
|
2777
|
-
yc = t;
|
|
2778
|
-
y.s = -y.s;
|
|
2779
|
-
}
|
|
2780
|
-
if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--; ) xc[i++] = 0;
|
|
2781
|
-
for (b = i; j > a; ) {
|
|
2782
|
-
if (xc[--j] < yc[j]) {
|
|
2783
|
-
for (i = j; i && !xc[--i]; ) xc[i] = 9;
|
|
2784
|
-
--xc[i];
|
|
2785
|
-
xc[j] += 10;
|
|
2786
|
-
}
|
|
2787
|
-
xc[j] -= yc[j];
|
|
2788
|
-
}
|
|
2789
|
-
for (; xc[--b] === 0; ) xc.pop();
|
|
2790
|
-
for (; xc[0] === 0; ) {
|
|
2791
|
-
xc.shift();
|
|
2792
|
-
--ye;
|
|
2793
|
-
}
|
|
2794
|
-
if (!xc[0]) {
|
|
2795
|
-
y.s = 1;
|
|
2796
|
-
xc = [ye = 0];
|
|
2797
|
-
}
|
|
2798
|
-
y.c = xc;
|
|
2799
|
-
y.e = ye;
|
|
2800
|
-
return y;
|
|
2801
|
-
};
|
|
2802
|
-
P.mod = function(y) {
|
|
2803
|
-
var ygtx, x = this, Big2 = x.constructor, a = x.s, b = (y = new Big2(y)).s;
|
|
2804
|
-
if (!y.c[0]) {
|
|
2805
|
-
throw Error(DIV_BY_ZERO);
|
|
2806
|
-
}
|
|
2807
|
-
x.s = y.s = 1;
|
|
2808
|
-
ygtx = y.cmp(x) == 1;
|
|
2809
|
-
x.s = a;
|
|
2810
|
-
y.s = b;
|
|
2811
|
-
if (ygtx) return new Big2(x);
|
|
2812
|
-
a = Big2.DP;
|
|
2813
|
-
b = Big2.RM;
|
|
2814
|
-
Big2.DP = Big2.RM = 0;
|
|
2815
|
-
x = x.div(y);
|
|
2816
|
-
Big2.DP = a;
|
|
2817
|
-
Big2.RM = b;
|
|
2818
|
-
return this.minus(x.times(y));
|
|
2819
|
-
};
|
|
2820
|
-
P.neg = function() {
|
|
2821
|
-
var x = new this.constructor(this);
|
|
2822
|
-
x.s = -x.s;
|
|
2823
|
-
return x;
|
|
2824
|
-
};
|
|
2825
|
-
P.plus = P.add = function(y) {
|
|
2826
|
-
var e, k, t, x = this, Big2 = x.constructor;
|
|
2827
|
-
y = new Big2(y);
|
|
2828
|
-
if (x.s != y.s) {
|
|
2829
|
-
y.s = -y.s;
|
|
2830
|
-
return x.minus(y);
|
|
2831
|
-
}
|
|
2832
|
-
var xe = x.e, xc = x.c, ye = y.e, yc = y.c;
|
|
2833
|
-
if (!xc[0] || !yc[0]) {
|
|
2834
|
-
if (!yc[0]) {
|
|
2835
|
-
if (xc[0]) {
|
|
2836
|
-
y = new Big2(x);
|
|
2837
|
-
} else {
|
|
2838
|
-
y.s = x.s;
|
|
2839
|
-
}
|
|
2840
|
-
}
|
|
2841
|
-
return y;
|
|
2842
|
-
}
|
|
2843
|
-
xc = xc.slice();
|
|
2844
|
-
if (e = xe - ye) {
|
|
2845
|
-
if (e > 0) {
|
|
2846
|
-
ye = xe;
|
|
2847
|
-
t = yc;
|
|
2848
|
-
} else {
|
|
2849
|
-
e = -e;
|
|
2850
|
-
t = xc;
|
|
2851
|
-
}
|
|
2852
|
-
t.reverse();
|
|
2853
|
-
for (; e--; ) t.push(0);
|
|
2854
|
-
t.reverse();
|
|
2855
|
-
}
|
|
2856
|
-
if (xc.length - yc.length < 0) {
|
|
2857
|
-
t = yc;
|
|
2858
|
-
yc = xc;
|
|
2859
|
-
xc = t;
|
|
2860
|
-
}
|
|
2861
|
-
e = yc.length;
|
|
2862
|
-
for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0;
|
|
2863
|
-
if (k) {
|
|
2864
|
-
xc.unshift(k);
|
|
2865
|
-
++ye;
|
|
2866
|
-
}
|
|
2867
|
-
for (e = xc.length; xc[--e] === 0; ) xc.pop();
|
|
2868
|
-
y.c = xc;
|
|
2869
|
-
y.e = ye;
|
|
2870
|
-
return y;
|
|
2871
|
-
};
|
|
2872
|
-
P.pow = function(n) {
|
|
2873
|
-
var x = this, one = new x.constructor("1"), y = one, isneg = n < 0;
|
|
2874
|
-
if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) {
|
|
2875
|
-
throw Error(INVALID + "exponent");
|
|
2876
|
-
}
|
|
2877
|
-
if (isneg) n = -n;
|
|
2878
|
-
for (; ; ) {
|
|
2879
|
-
if (n & 1) y = y.times(x);
|
|
2880
|
-
n >>= 1;
|
|
2881
|
-
if (!n) break;
|
|
2882
|
-
x = x.times(x);
|
|
2883
|
-
}
|
|
2884
|
-
return isneg ? one.div(y) : y;
|
|
2885
|
-
};
|
|
2886
|
-
P.prec = function(sd, rm) {
|
|
2887
|
-
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
|
|
2888
|
-
throw Error(INVALID + "precision");
|
|
2889
|
-
}
|
|
2890
|
-
return round(new this.constructor(this), sd, rm);
|
|
2891
|
-
};
|
|
2892
|
-
P.round = function(dp, rm) {
|
|
2893
|
-
if (dp === UNDEFINED) dp = 0;
|
|
2894
|
-
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) {
|
|
2895
|
-
throw Error(INVALID_DP);
|
|
2896
|
-
}
|
|
2897
|
-
return round(new this.constructor(this), dp + this.e + 1, rm);
|
|
2898
|
-
};
|
|
2899
|
-
P.sqrt = function() {
|
|
2900
|
-
var r, c, t, x = this, Big2 = x.constructor, s = x.s, e = x.e, half = new Big2("0.5");
|
|
2901
|
-
if (!x.c[0]) return new Big2(x);
|
|
2902
|
-
if (s < 0) {
|
|
2903
|
-
throw Error(NAME + "No square root");
|
|
2904
|
-
}
|
|
2905
|
-
s = Math.sqrt(+stringify(x, true, true));
|
|
2906
|
-
if (s === 0 || s === 1 / 0) {
|
|
2907
|
-
c = x.c.join("");
|
|
2908
|
-
if (!(c.length + e & 1)) c += "0";
|
|
2909
|
-
s = Math.sqrt(c);
|
|
2910
|
-
e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
|
|
2911
|
-
r = new Big2((s == 1 / 0 ? "5e" : (s = s.toExponential()).slice(0, s.indexOf("e") + 1)) + e);
|
|
2912
|
-
} else {
|
|
2913
|
-
r = new Big2(s + "");
|
|
2914
|
-
}
|
|
2915
|
-
e = r.e + (Big2.DP += 4);
|
|
2916
|
-
do {
|
|
2917
|
-
t = r;
|
|
2918
|
-
r = half.times(t.plus(x.div(t)));
|
|
2919
|
-
} while (t.c.slice(0, e).join("") !== r.c.slice(0, e).join(""));
|
|
2920
|
-
return round(r, (Big2.DP -= 4) + r.e + 1, Big2.RM);
|
|
2921
|
-
};
|
|
2922
|
-
P.times = P.mul = function(y) {
|
|
2923
|
-
var c, x = this, Big2 = x.constructor, xc = x.c, yc = (y = new Big2(y)).c, a = xc.length, b = yc.length, i = x.e, j = y.e;
|
|
2924
|
-
y.s = x.s == y.s ? 1 : -1;
|
|
2925
|
-
if (!xc[0] || !yc[0]) {
|
|
2926
|
-
y.c = [y.e = 0];
|
|
2927
|
-
return y;
|
|
2928
|
-
}
|
|
2929
|
-
y.e = i + j;
|
|
2930
|
-
if (a < b) {
|
|
2931
|
-
c = xc;
|
|
2932
|
-
xc = yc;
|
|
2933
|
-
yc = c;
|
|
2934
|
-
j = a;
|
|
2935
|
-
a = b;
|
|
2936
|
-
b = j;
|
|
2937
|
-
}
|
|
2938
|
-
for (c = new Array(j = a + b); j--; ) c[j] = 0;
|
|
2939
|
-
for (i = b; i--; ) {
|
|
2940
|
-
b = 0;
|
|
2941
|
-
for (j = a + i; j > i; ) {
|
|
2942
|
-
b = c[j] + yc[i] * xc[j - i - 1] + b;
|
|
2943
|
-
c[j--] = b % 10;
|
|
2944
|
-
b = b / 10 | 0;
|
|
2945
|
-
}
|
|
2946
|
-
c[j] = b;
|
|
2947
|
-
}
|
|
2948
|
-
if (b) ++y.e;
|
|
2949
|
-
else c.shift();
|
|
2950
|
-
for (i = c.length; !c[--i]; ) c.pop();
|
|
2951
|
-
y.c = c;
|
|
2952
|
-
return y;
|
|
2953
|
-
};
|
|
2954
|
-
P.toExponential = function(dp, rm) {
|
|
2955
|
-
var x = this, n = x.c[0];
|
|
2956
|
-
if (dp !== UNDEFINED) {
|
|
2957
|
-
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
|
|
2958
|
-
throw Error(INVALID_DP);
|
|
2959
|
-
}
|
|
2960
|
-
x = round(new x.constructor(x), ++dp, rm);
|
|
2961
|
-
for (; x.c.length < dp; ) x.c.push(0);
|
|
2962
|
-
}
|
|
2963
|
-
return stringify(x, true, !!n);
|
|
2964
|
-
};
|
|
2965
|
-
P.toFixed = function(dp, rm) {
|
|
2966
|
-
var x = this, n = x.c[0];
|
|
2967
|
-
if (dp !== UNDEFINED) {
|
|
2968
|
-
if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
|
|
2969
|
-
throw Error(INVALID_DP);
|
|
2970
|
-
}
|
|
2971
|
-
x = round(new x.constructor(x), dp + x.e + 1, rm);
|
|
2972
|
-
for (dp = dp + x.e + 1; x.c.length < dp; ) x.c.push(0);
|
|
2973
|
-
}
|
|
2974
|
-
return stringify(x, false, !!n);
|
|
2975
|
-
};
|
|
2976
|
-
P[Symbol.for("nodejs.util.inspect.custom")] = P.toJSON = P.toString = function() {
|
|
2977
|
-
var x = this, Big2 = x.constructor;
|
|
2978
|
-
return stringify(x, x.e <= Big2.NE || x.e >= Big2.PE, !!x.c[0]);
|
|
2979
|
-
};
|
|
2980
|
-
P.toNumber = function() {
|
|
2981
|
-
var n = +stringify(this, true, true);
|
|
2982
|
-
if (this.constructor.strict === true && !this.eq(n.toString())) {
|
|
2983
|
-
throw Error(NAME + "Imprecise conversion");
|
|
2984
|
-
}
|
|
2985
|
-
return n;
|
|
2986
|
-
};
|
|
2987
|
-
P.toPrecision = function(sd, rm) {
|
|
2988
|
-
var x = this, Big2 = x.constructor, n = x.c[0];
|
|
2989
|
-
if (sd !== UNDEFINED) {
|
|
2990
|
-
if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
|
|
2991
|
-
throw Error(INVALID + "precision");
|
|
2992
|
-
}
|
|
2993
|
-
x = round(new Big2(x), sd, rm);
|
|
2994
|
-
for (; x.c.length < sd; ) x.c.push(0);
|
|
2995
|
-
}
|
|
2996
|
-
return stringify(x, sd <= x.e || x.e <= Big2.NE || x.e >= Big2.PE, !!n);
|
|
2997
|
-
};
|
|
2998
|
-
P.valueOf = function() {
|
|
2999
|
-
var x = this, Big2 = x.constructor;
|
|
3000
|
-
if (Big2.strict === true) {
|
|
3001
|
-
throw Error(NAME + "valueOf disallowed");
|
|
3002
|
-
}
|
|
3003
|
-
return stringify(x, x.e <= Big2.NE || x.e >= Big2.PE, true);
|
|
3004
|
-
};
|
|
3005
|
-
var Big = _Big_();
|
|
3006
|
-
|
|
3007
3292
|
// ../../node_modules/js-base64/base64.mjs
|
|
3008
3293
|
var _hasBuffer = typeof Buffer === "function";
|
|
3009
3294
|
var _TD = typeof TextDecoder === "function" ? new TextDecoder() : void 0;
|
|
@@ -3079,25 +3364,40 @@ var nearWalletAdapters = (() => {
|
|
|
3079
3364
|
__name(bytesToBase64, "bytesToBase64");
|
|
3080
3365
|
|
|
3081
3366
|
// ../utils/src/crypto.ts
|
|
3367
|
+
function curveFromKey(key) {
|
|
3368
|
+
if (!key.includes(":")) return "ed25519";
|
|
3369
|
+
const curve = key.split(":")[0];
|
|
3370
|
+
if (curve === "ed25519" || curve === "secp256k1") return curve;
|
|
3371
|
+
throw new Error(`Unsupported curve: ${curve}`);
|
|
3372
|
+
}
|
|
3373
|
+
__name(curveFromKey, "curveFromKey");
|
|
3082
3374
|
var keyFromString = /* @__PURE__ */ __name((key) => base58_to_binary_default(
|
|
3083
3375
|
key.includes(":") ? (() => {
|
|
3084
3376
|
const [curve, keyPart] = key.split(":");
|
|
3085
|
-
if (curve !== "ed25519") {
|
|
3377
|
+
if (curve !== "ed25519" && curve !== "secp256k1") {
|
|
3086
3378
|
throw new Error(`Unsupported curve: ${curve}`);
|
|
3087
3379
|
}
|
|
3088
3380
|
return keyPart;
|
|
3089
3381
|
})() : key
|
|
3090
3382
|
), "keyFromString");
|
|
3091
|
-
var keyToString = /* @__PURE__ */ __name((key) =>
|
|
3383
|
+
var keyToString = /* @__PURE__ */ __name((key, curve = "ed25519") => `${curve}:${binary_to_base58_default(key)}`, "keyToString");
|
|
3092
3384
|
function publicKeyFromPrivate(privateKey) {
|
|
3385
|
+
const curve = curveFromKey(privateKey);
|
|
3386
|
+
if (curve === "secp256k1") {
|
|
3387
|
+
const secret2 = keyFromString(privateKey);
|
|
3388
|
+
const fullPk = secp256k1.getPublicKey(secret2, false);
|
|
3389
|
+
const publicKey2 = fullPk.slice(1);
|
|
3390
|
+
return keyToString(publicKey2, "secp256k1");
|
|
3391
|
+
}
|
|
3093
3392
|
const secret = keyFromString(privateKey).slice(0, 32);
|
|
3094
3393
|
const publicKey = ed25519.getPublicKey(secret);
|
|
3095
3394
|
return keyToString(publicKey);
|
|
3096
3395
|
}
|
|
3097
3396
|
__name(publicKeyFromPrivate, "publicKeyFromPrivate");
|
|
3098
|
-
function privateKeyFromRandom() {
|
|
3099
|
-
const
|
|
3100
|
-
|
|
3397
|
+
function privateKeyFromRandom(curve = "ed25519") {
|
|
3398
|
+
const size = curve === "secp256k1" ? 32 : 64;
|
|
3399
|
+
const privateKey = crypto.getRandomValues(new Uint8Array(size));
|
|
3400
|
+
return keyToString(privateKey, curve);
|
|
3101
3401
|
}
|
|
3102
3402
|
__name(privateKeyFromRandom, "privateKeyFromRandom");
|
|
3103
3403
|
|
|
@@ -3264,14 +3564,22 @@ var nearWalletAdapters = (() => {
|
|
|
3264
3564
|
var getBorshSchema = /* @__PURE__ */ __name(() => nearChainSchema, "getBorshSchema");
|
|
3265
3565
|
|
|
3266
3566
|
// ../utils/src/transaction.ts
|
|
3567
|
+
function mapPublicKey(keyString) {
|
|
3568
|
+
const curve = curveFromKey(keyString);
|
|
3569
|
+
const data = keyFromString(keyString);
|
|
3570
|
+
return curve === "secp256k1" ? { secp256k1Key: { data } } : { ed25519Key: { data } };
|
|
3571
|
+
}
|
|
3572
|
+
__name(mapPublicKey, "mapPublicKey");
|
|
3573
|
+
function mapSignature(sigBase58, signerKeyString) {
|
|
3574
|
+
const curve = curveFromKey(signerKeyString);
|
|
3575
|
+
const data = base58_to_binary_default(sigBase58);
|
|
3576
|
+
return curve === "secp256k1" ? { secp256k1Signature: { data } } : { ed25519Signature: { data } };
|
|
3577
|
+
}
|
|
3578
|
+
__name(mapSignature, "mapSignature");
|
|
3267
3579
|
function mapTransaction(jsonTransaction) {
|
|
3268
3580
|
return {
|
|
3269
3581
|
signerId: jsonTransaction.signerId,
|
|
3270
|
-
publicKey:
|
|
3271
|
-
ed25519Key: {
|
|
3272
|
-
data: keyFromString(jsonTransaction.publicKey)
|
|
3273
|
-
}
|
|
3274
|
-
},
|
|
3582
|
+
publicKey: mapPublicKey(jsonTransaction.publicKey),
|
|
3275
3583
|
nonce: BigInt(jsonTransaction.nonce),
|
|
3276
3584
|
receiverId: jsonTransaction.receiverId,
|
|
3277
3585
|
blockHash: base58_to_binary_default(jsonTransaction.blockHash),
|
|
@@ -3314,22 +3622,14 @@ var nearWalletAdapters = (() => {
|
|
|
3314
3622
|
return {
|
|
3315
3623
|
stake: {
|
|
3316
3624
|
stake: BigInt(action.stake),
|
|
3317
|
-
publicKey:
|
|
3318
|
-
ed25519Key: {
|
|
3319
|
-
data: keyFromString(action.publicKey)
|
|
3320
|
-
}
|
|
3321
|
-
}
|
|
3625
|
+
publicKey: mapPublicKey(action.publicKey)
|
|
3322
3626
|
}
|
|
3323
3627
|
};
|
|
3324
3628
|
}
|
|
3325
3629
|
case "AddKey": {
|
|
3326
3630
|
return {
|
|
3327
3631
|
addKey: {
|
|
3328
|
-
publicKey:
|
|
3329
|
-
ed25519Key: {
|
|
3330
|
-
data: keyFromString(action.publicKey)
|
|
3331
|
-
}
|
|
3332
|
-
},
|
|
3632
|
+
publicKey: mapPublicKey(action.publicKey),
|
|
3333
3633
|
accessKey: {
|
|
3334
3634
|
nonce: BigInt(action.accessKey.nonce),
|
|
3335
3635
|
permission: action.accessKey.permission === "FullAccess" ? { fullAccess: {} } : {
|
|
@@ -3346,11 +3646,7 @@ var nearWalletAdapters = (() => {
|
|
|
3346
3646
|
case "DeleteKey": {
|
|
3347
3647
|
return {
|
|
3348
3648
|
deleteKey: {
|
|
3349
|
-
publicKey:
|
|
3350
|
-
ed25519Key: {
|
|
3351
|
-
data: keyFromString(action.publicKey)
|
|
3352
|
-
}
|
|
3353
|
-
}
|
|
3649
|
+
publicKey: mapPublicKey(action.publicKey)
|
|
3354
3650
|
}
|
|
3355
3651
|
};
|
|
3356
3652
|
}
|
|
@@ -3365,9 +3661,7 @@ var nearWalletAdapters = (() => {
|
|
|
3365
3661
|
return {
|
|
3366
3662
|
signedDelegate: {
|
|
3367
3663
|
delegateAction: mapAction(action.delegateAction),
|
|
3368
|
-
signature:
|
|
3369
|
-
ed25519Signature: base58_to_binary_default(action.signature)
|
|
3370
|
-
}
|
|
3664
|
+
signature: mapSignature(action.signature, action.publicKey)
|
|
3371
3665
|
}
|
|
3372
3666
|
};
|
|
3373
3667
|
}
|
|
@@ -4123,6 +4417,13 @@ var nearWalletAdapters = (() => {
|
|
|
4123
4417
|
const hash = sha256(new Uint8Array(borshPayload));
|
|
4124
4418
|
const pk = keyFromString(publicKey);
|
|
4125
4419
|
const sig = base64ToBytes(signature);
|
|
4420
|
+
if (curveFromKey(publicKey) === "secp256k1") {
|
|
4421
|
+
const compactSig = sig.slice(0, 64);
|
|
4422
|
+
const fullPk = new Uint8Array(65);
|
|
4423
|
+
fullPk[0] = 4;
|
|
4424
|
+
fullPk.set(pk, 1);
|
|
4425
|
+
return secp256k1.verify(compactSig, hash, fullPk, { prehash: false });
|
|
4426
|
+
}
|
|
4126
4427
|
return ed25519.verify(sig, hash, pk);
|
|
4127
4428
|
}, "verifyNep413Signature");
|
|
4128
4429
|
var SessionRepository = class {
|
|
@@ -4585,6 +4886,12 @@ var nearWalletAdapters = (() => {
|
|
|
4585
4886
|
|
|
4586
4887
|
@noble/curves/ed25519.js:
|
|
4587
4888
|
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4889
|
+
|
|
4890
|
+
@noble/curves/abstract/weierstrass.js:
|
|
4891
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4892
|
+
|
|
4893
|
+
@noble/curves/secp256k1.js:
|
|
4894
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
4588
4895
|
*/
|
|
4589
4896
|
|
|
4590
4897
|
Object.defineProperty(globalThis, 'nearWalletAdapters', {
|