@xrpay/x402 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +125 -2
- package/dist/browser.js +2 -0
- package/dist/client.cjs +1552 -0
- package/dist/client.d.cts +46 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +1529 -0
- package/dist/commerce.cjs +1611 -0
- package/dist/commerce.d.cts +99 -0
- package/dist/commerce.d.ts +99 -0
- package/dist/commerce.js +1588 -0
- package/dist/mcp-tools.d.ts +56 -0
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +1749 -0
- package/dist/protocol.cjs +1447 -0
- package/dist/protocol.d.cts +64 -0
- package/dist/protocol.d.ts +64 -0
- package/dist/protocol.js +1424 -0
- package/dist/server.cjs +1772 -0
- package/dist/server.d.cts +62 -0
- package/dist/server.d.ts +62 -0
- package/dist/server.js +1723 -0
- package/examples/buy-digital-product.mjs +44 -0
- package/package.json +101 -4
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,1552 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
PAYMENT_HEADERS: () => PAYMENT_HEADERS,
|
|
24
|
+
RLUSD_ASSET: () => RLUSD_ASSET,
|
|
25
|
+
X402Client: () => X402Client,
|
|
26
|
+
amountEquals: () => amountEquals,
|
|
27
|
+
decodePaymentHeader: () => decodePaymentHeader,
|
|
28
|
+
encodePaymentHeader: () => encodePaymentHeader,
|
|
29
|
+
isRecord: () => isRecord,
|
|
30
|
+
positiveAmount: () => positiveAmount,
|
|
31
|
+
requirementsKey: () => requirementsKey,
|
|
32
|
+
validatePayload: () => validatePayload,
|
|
33
|
+
validateRequirements: () => validateRequirements,
|
|
34
|
+
xrpToDrops: () => xrpToDrops
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(client_exports);
|
|
37
|
+
|
|
38
|
+
// ../../node_modules/bignumber.js/bignumber.mjs
|
|
39
|
+
var isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
|
40
|
+
var mathceil = Math.ceil;
|
|
41
|
+
var mathfloor = Math.floor;
|
|
42
|
+
var bignumberError = "[BigNumber Error] ";
|
|
43
|
+
var tooManyDigits = bignumberError + "Number primitive has more than 15 significant digits: ";
|
|
44
|
+
var BASE = 1e14;
|
|
45
|
+
var LOG_BASE = 14;
|
|
46
|
+
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
47
|
+
var POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13];
|
|
48
|
+
var SQRT_BASE = 1e7;
|
|
49
|
+
var MAX = 1e9;
|
|
50
|
+
function clone(configObject) {
|
|
51
|
+
var div, convertBase, parseNumeric, P = BigNumber2.prototype = { constructor: BigNumber2, toString: null, valueOf: null }, ONE = new BigNumber2(1), DECIMAL_PLACES = 20, ROUNDING_MODE = 4, TO_EXP_NEG = -7, TO_EXP_POS = 21, MIN_EXP = -1e7, MAX_EXP = 1e7, CRYPTO = false, MODULO_MODE = 1, POW_PRECISION = 0, FORMAT = {
|
|
52
|
+
prefix: "",
|
|
53
|
+
groupSize: 3,
|
|
54
|
+
secondaryGroupSize: 0,
|
|
55
|
+
groupSeparator: ",",
|
|
56
|
+
decimalSeparator: ".",
|
|
57
|
+
fractionGroupSize: 0,
|
|
58
|
+
fractionGroupSeparator: "\xA0",
|
|
59
|
+
// non-breaking space
|
|
60
|
+
suffix: ""
|
|
61
|
+
}, ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz", alphabetHasNormalDecimalDigits = true;
|
|
62
|
+
function BigNumber2(v, b) {
|
|
63
|
+
var alphabet, c, caseChanged, e, i, isNum, len, str, x = this;
|
|
64
|
+
if (!(x instanceof BigNumber2)) return new BigNumber2(v, b);
|
|
65
|
+
if (b == null) {
|
|
66
|
+
if (v && v._isBigNumber === true) {
|
|
67
|
+
x.s = v.s;
|
|
68
|
+
if (!v.c || v.e > MAX_EXP) {
|
|
69
|
+
x.c = x.e = null;
|
|
70
|
+
} else if (v.e < MIN_EXP) {
|
|
71
|
+
x.c = [x.e = 0];
|
|
72
|
+
} else {
|
|
73
|
+
x.e = v.e;
|
|
74
|
+
x.c = v.c.slice();
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if ((isNum = typeof v == "number") && v * 0 == 0) {
|
|
79
|
+
x.s = 1 / v < 0 ? (v = -v, -1) : 1;
|
|
80
|
+
if (v === ~~v) {
|
|
81
|
+
for (e = 0, i = v; i >= 10; i /= 10, e++) ;
|
|
82
|
+
if (e > MAX_EXP) {
|
|
83
|
+
x.c = x.e = null;
|
|
84
|
+
} else {
|
|
85
|
+
x.e = e;
|
|
86
|
+
x.c = [v];
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
str = String(v);
|
|
91
|
+
} else {
|
|
92
|
+
if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);
|
|
93
|
+
x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
|
|
94
|
+
}
|
|
95
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
96
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
97
|
+
if (e < 0) e = i;
|
|
98
|
+
e += +str.slice(i + 1);
|
|
99
|
+
str = str.substring(0, i);
|
|
100
|
+
} else if (e < 0) {
|
|
101
|
+
e = str.length;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
intCheck(b, 2, ALPHABET.length, "Base");
|
|
105
|
+
if (b == 10 && alphabetHasNormalDecimalDigits) {
|
|
106
|
+
x = new BigNumber2(v);
|
|
107
|
+
return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
|
|
108
|
+
}
|
|
109
|
+
str = String(v);
|
|
110
|
+
if (isNum = typeof v == "number") {
|
|
111
|
+
if (v * 0 != 0) return parseNumeric(x, str, isNum, b);
|
|
112
|
+
x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
|
|
113
|
+
if (BigNumber2.DEBUG && str.replace(/^0\.0*|\./, "").length > 15) {
|
|
114
|
+
throw Error(tooManyDigits + v);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
|
|
118
|
+
}
|
|
119
|
+
alphabet = ALPHABET.slice(0, b);
|
|
120
|
+
e = i = 0;
|
|
121
|
+
for (len = str.length; i < len; i++) {
|
|
122
|
+
if (alphabet.indexOf(c = str.charAt(i)) < 0) {
|
|
123
|
+
if (c == ".") {
|
|
124
|
+
if (i > e) {
|
|
125
|
+
e = len;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
} else if (!caseChanged) {
|
|
129
|
+
if (str == str.toUpperCase() && (str = str.toLowerCase()) || str == str.toLowerCase() && (str = str.toUpperCase())) {
|
|
130
|
+
caseChanged = true;
|
|
131
|
+
i = -1;
|
|
132
|
+
e = 0;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return parseNumeric(x, String(v), isNum, b);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
isNum = false;
|
|
140
|
+
str = convertBase(str, b, 10, x.s);
|
|
141
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
142
|
+
else e = str.length;
|
|
143
|
+
}
|
|
144
|
+
for (i = 0; str.charCodeAt(i) === 48; i++) ;
|
|
145
|
+
for (len = str.length; str.charCodeAt(--len) === 48; ) ;
|
|
146
|
+
if (str = str.slice(i, ++len)) {
|
|
147
|
+
len -= i;
|
|
148
|
+
if (isNum && BigNumber2.DEBUG && len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
|
|
149
|
+
throw Error(tooManyDigits + x.s * v);
|
|
150
|
+
}
|
|
151
|
+
if ((e = e - i - 1) > MAX_EXP) {
|
|
152
|
+
x.c = x.e = null;
|
|
153
|
+
} else if (e < MIN_EXP) {
|
|
154
|
+
x.c = [x.e = 0];
|
|
155
|
+
} else {
|
|
156
|
+
x.e = e;
|
|
157
|
+
x.c = [];
|
|
158
|
+
i = (e + 1) % LOG_BASE;
|
|
159
|
+
if (e < 0) i += LOG_BASE;
|
|
160
|
+
if (i < len) {
|
|
161
|
+
if (i) x.c.push(+str.slice(0, i));
|
|
162
|
+
for (len -= LOG_BASE; i < len; ) {
|
|
163
|
+
x.c.push(+str.slice(i, i += LOG_BASE));
|
|
164
|
+
}
|
|
165
|
+
i = LOG_BASE - (str = str.slice(i)).length;
|
|
166
|
+
} else {
|
|
167
|
+
i -= len;
|
|
168
|
+
}
|
|
169
|
+
for (; i--; str += "0") ;
|
|
170
|
+
x.c.push(+str);
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
x.c = [x.e = 0];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
BigNumber2.clone = clone;
|
|
177
|
+
BigNumber2.ROUND_UP = 0;
|
|
178
|
+
BigNumber2.ROUND_DOWN = 1;
|
|
179
|
+
BigNumber2.ROUND_CEIL = 2;
|
|
180
|
+
BigNumber2.ROUND_FLOOR = 3;
|
|
181
|
+
BigNumber2.ROUND_HALF_UP = 4;
|
|
182
|
+
BigNumber2.ROUND_HALF_DOWN = 5;
|
|
183
|
+
BigNumber2.ROUND_HALF_EVEN = 6;
|
|
184
|
+
BigNumber2.ROUND_HALF_CEIL = 7;
|
|
185
|
+
BigNumber2.ROUND_HALF_FLOOR = 8;
|
|
186
|
+
BigNumber2.EUCLID = 9;
|
|
187
|
+
BigNumber2.config = BigNumber2.set = function(obj) {
|
|
188
|
+
var p, v;
|
|
189
|
+
if (obj != null) {
|
|
190
|
+
if (typeof obj == "object") {
|
|
191
|
+
if (obj.hasOwnProperty(p = "DECIMAL_PLACES")) {
|
|
192
|
+
v = obj[p];
|
|
193
|
+
intCheck(v, 0, MAX, p);
|
|
194
|
+
DECIMAL_PLACES = v;
|
|
195
|
+
}
|
|
196
|
+
if (obj.hasOwnProperty(p = "ROUNDING_MODE")) {
|
|
197
|
+
v = obj[p];
|
|
198
|
+
intCheck(v, 0, 8, p);
|
|
199
|
+
ROUNDING_MODE = v;
|
|
200
|
+
}
|
|
201
|
+
if (obj.hasOwnProperty(p = "EXPONENTIAL_AT")) {
|
|
202
|
+
v = obj[p];
|
|
203
|
+
if (v && v.pop) {
|
|
204
|
+
intCheck(v[0], -MAX, 0, p);
|
|
205
|
+
intCheck(v[1], 0, MAX, p);
|
|
206
|
+
TO_EXP_NEG = v[0];
|
|
207
|
+
TO_EXP_POS = v[1];
|
|
208
|
+
} else {
|
|
209
|
+
intCheck(v, -MAX, MAX, p);
|
|
210
|
+
TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (obj.hasOwnProperty(p = "RANGE")) {
|
|
214
|
+
v = obj[p];
|
|
215
|
+
if (v && v.pop) {
|
|
216
|
+
intCheck(v[0], -MAX, -1, p);
|
|
217
|
+
intCheck(v[1], 1, MAX, p);
|
|
218
|
+
MIN_EXP = v[0];
|
|
219
|
+
MAX_EXP = v[1];
|
|
220
|
+
} else {
|
|
221
|
+
intCheck(v, -MAX, MAX, p);
|
|
222
|
+
if (v) {
|
|
223
|
+
MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
|
|
224
|
+
} else {
|
|
225
|
+
throw Error(bignumberError + p + " cannot be zero: " + v);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (obj.hasOwnProperty(p = "CRYPTO")) {
|
|
230
|
+
v = obj[p];
|
|
231
|
+
if (v === !!v) {
|
|
232
|
+
if (v) {
|
|
233
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
|
|
234
|
+
CRYPTO = v;
|
|
235
|
+
} else {
|
|
236
|
+
CRYPTO = !v;
|
|
237
|
+
throw Error(bignumberError + "crypto unavailable");
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
CRYPTO = v;
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
throw Error(bignumberError + p + " not true or false: " + v);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (obj.hasOwnProperty(p = "MODULO_MODE")) {
|
|
247
|
+
v = obj[p];
|
|
248
|
+
intCheck(v, 0, 9, p);
|
|
249
|
+
MODULO_MODE = v;
|
|
250
|
+
}
|
|
251
|
+
if (obj.hasOwnProperty(p = "POW_PRECISION")) {
|
|
252
|
+
v = obj[p];
|
|
253
|
+
intCheck(v, 0, MAX, p);
|
|
254
|
+
POW_PRECISION = v;
|
|
255
|
+
}
|
|
256
|
+
if (obj.hasOwnProperty(p = "FORMAT")) {
|
|
257
|
+
v = obj[p];
|
|
258
|
+
if (typeof v == "object") FORMAT = v;
|
|
259
|
+
else throw Error(bignumberError + p + " not an object: " + v);
|
|
260
|
+
}
|
|
261
|
+
if (obj.hasOwnProperty(p = "ALPHABET")) {
|
|
262
|
+
v = obj[p];
|
|
263
|
+
if (typeof v == "string" && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
|
|
264
|
+
alphabetHasNormalDecimalDigits = v.slice(0, 10) == "0123456789";
|
|
265
|
+
ALPHABET = v;
|
|
266
|
+
} else {
|
|
267
|
+
throw Error(bignumberError + p + " invalid: " + v);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
throw Error(bignumberError + "Object expected: " + obj);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
DECIMAL_PLACES,
|
|
276
|
+
ROUNDING_MODE,
|
|
277
|
+
EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
|
|
278
|
+
RANGE: [MIN_EXP, MAX_EXP],
|
|
279
|
+
CRYPTO,
|
|
280
|
+
MODULO_MODE,
|
|
281
|
+
POW_PRECISION,
|
|
282
|
+
FORMAT,
|
|
283
|
+
ALPHABET
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
BigNumber2.isBigNumber = function(v) {
|
|
287
|
+
if (!v || v._isBigNumber !== true) return false;
|
|
288
|
+
if (!BigNumber2.DEBUG) return true;
|
|
289
|
+
var i, n, c = v.c, e = v.e, s = v.s;
|
|
290
|
+
out: if ({}.toString.call(c) == "[object Array]") {
|
|
291
|
+
if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
|
|
292
|
+
if (c[0] === 0) {
|
|
293
|
+
if (e === 0 && c.length === 1) return true;
|
|
294
|
+
break out;
|
|
295
|
+
}
|
|
296
|
+
i = (e + 1) % LOG_BASE;
|
|
297
|
+
if (i < 1) i += LOG_BASE;
|
|
298
|
+
if (String(c[0]).length == i) {
|
|
299
|
+
for (i = 0; i < c.length; i++) {
|
|
300
|
+
n = c[i];
|
|
301
|
+
if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
|
|
302
|
+
}
|
|
303
|
+
if (n !== 0) return true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
throw Error(bignumberError + "Invalid BigNumber: " + v);
|
|
310
|
+
};
|
|
311
|
+
BigNumber2.maximum = BigNumber2.max = function() {
|
|
312
|
+
return maxOrMin(arguments, -1);
|
|
313
|
+
};
|
|
314
|
+
BigNumber2.minimum = BigNumber2.min = function() {
|
|
315
|
+
return maxOrMin(arguments, 1);
|
|
316
|
+
};
|
|
317
|
+
BigNumber2.random = (function() {
|
|
318
|
+
var pow2_53 = 9007199254740992;
|
|
319
|
+
var random53bitInt = Math.random() * pow2_53 & 2097151 ? function() {
|
|
320
|
+
return mathfloor(Math.random() * pow2_53);
|
|
321
|
+
} : function() {
|
|
322
|
+
return (Math.random() * 1073741824 | 0) * 8388608 + (Math.random() * 8388608 | 0);
|
|
323
|
+
};
|
|
324
|
+
return function(dp) {
|
|
325
|
+
var a, b, e, k, v, i = 0, c = [], rand = new BigNumber2(ONE);
|
|
326
|
+
if (dp == null) dp = DECIMAL_PLACES;
|
|
327
|
+
else intCheck(dp, 0, MAX);
|
|
328
|
+
k = mathceil(dp / LOG_BASE);
|
|
329
|
+
if (CRYPTO) {
|
|
330
|
+
if (crypto.getRandomValues) {
|
|
331
|
+
a = crypto.getRandomValues(new Uint32Array(k *= 2));
|
|
332
|
+
for (; i < k; ) {
|
|
333
|
+
v = a[i] * 131072 + (a[i + 1] >>> 11);
|
|
334
|
+
if (v >= 9e15) {
|
|
335
|
+
b = crypto.getRandomValues(new Uint32Array(2));
|
|
336
|
+
a[i] = b[0];
|
|
337
|
+
a[i + 1] = b[1];
|
|
338
|
+
} else {
|
|
339
|
+
c.push(v % 1e14);
|
|
340
|
+
i += 2;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
i = k / 2;
|
|
344
|
+
} else if (crypto.randomBytes) {
|
|
345
|
+
a = crypto.randomBytes(k *= 7);
|
|
346
|
+
for (; i < k; ) {
|
|
347
|
+
v = (a[i] & 31) * 281474976710656 + a[i + 1] * 1099511627776 + a[i + 2] * 4294967296 + a[i + 3] * 16777216 + (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
|
|
348
|
+
if (v >= 9e15) {
|
|
349
|
+
crypto.randomBytes(7).copy(a, i);
|
|
350
|
+
} else {
|
|
351
|
+
c.push(v % 1e14);
|
|
352
|
+
i += 7;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
i = k / 7;
|
|
356
|
+
} else {
|
|
357
|
+
CRYPTO = false;
|
|
358
|
+
throw Error(bignumberError + "crypto unavailable");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (!CRYPTO) {
|
|
362
|
+
for (; i < k; ) {
|
|
363
|
+
v = random53bitInt();
|
|
364
|
+
if (v < 9e15) c[i++] = v % 1e14;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
k = c[--i];
|
|
368
|
+
dp %= LOG_BASE;
|
|
369
|
+
if (k && dp) {
|
|
370
|
+
v = POWS_TEN[LOG_BASE - dp];
|
|
371
|
+
c[i] = mathfloor(k / v) * v;
|
|
372
|
+
}
|
|
373
|
+
for (; c[i] === 0; c.pop(), i--) ;
|
|
374
|
+
if (i < 0) {
|
|
375
|
+
c = [e = 0];
|
|
376
|
+
} else {
|
|
377
|
+
for (e = -1; c[0] === 0; c.splice(0, 1), e -= LOG_BASE) ;
|
|
378
|
+
for (i = 1, v = c[0]; v >= 10; v /= 10, i++) ;
|
|
379
|
+
if (i < LOG_BASE) e -= LOG_BASE - i;
|
|
380
|
+
}
|
|
381
|
+
rand.e = e;
|
|
382
|
+
rand.c = c;
|
|
383
|
+
return rand;
|
|
384
|
+
};
|
|
385
|
+
})();
|
|
386
|
+
BigNumber2.sum = function() {
|
|
387
|
+
var i = 1, args = arguments, sum = new BigNumber2(args[0]);
|
|
388
|
+
for (; i < args.length; ) sum = sum.plus(args[i++]);
|
|
389
|
+
return sum;
|
|
390
|
+
};
|
|
391
|
+
convertBase = /* @__PURE__ */ (function() {
|
|
392
|
+
var decimal = "0123456789";
|
|
393
|
+
function toBaseOut(str, baseIn, baseOut, alphabet) {
|
|
394
|
+
var j, arr = [0], arrL, i = 0, len = str.length;
|
|
395
|
+
for (; i < len; ) {
|
|
396
|
+
for (arrL = arr.length; arrL--; arr[arrL] *= baseIn) ;
|
|
397
|
+
arr[0] += alphabet.indexOf(str.charAt(i++));
|
|
398
|
+
for (j = 0; j < arr.length; j++) {
|
|
399
|
+
if (arr[j] > baseOut - 1) {
|
|
400
|
+
if (arr[j + 1] == null) arr[j + 1] = 0;
|
|
401
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
402
|
+
arr[j] %= baseOut;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return arr.reverse();
|
|
407
|
+
}
|
|
408
|
+
return function(str, baseIn, baseOut, sign, callerIsToString) {
|
|
409
|
+
var alphabet, d, e, k, r, x, xc, y, i = str.indexOf("."), dp = DECIMAL_PLACES, rm = ROUNDING_MODE;
|
|
410
|
+
if (i >= 0) {
|
|
411
|
+
k = POW_PRECISION;
|
|
412
|
+
POW_PRECISION = 0;
|
|
413
|
+
str = str.replace(".", "");
|
|
414
|
+
y = new BigNumber2(baseIn);
|
|
415
|
+
x = y.pow(str.length - i);
|
|
416
|
+
POW_PRECISION = k;
|
|
417
|
+
y.c = toBaseOut(
|
|
418
|
+
toFixedPoint(coeffToString(x.c), x.e, "0"),
|
|
419
|
+
10,
|
|
420
|
+
baseOut,
|
|
421
|
+
decimal
|
|
422
|
+
);
|
|
423
|
+
y.e = y.c.length;
|
|
424
|
+
}
|
|
425
|
+
xc = toBaseOut(str, baseIn, baseOut, callerIsToString ? (alphabet = ALPHABET, decimal) : (alphabet = decimal, ALPHABET));
|
|
426
|
+
e = k = xc.length;
|
|
427
|
+
for (; xc[--k] == 0; xc.pop()) ;
|
|
428
|
+
if (!xc[0]) return alphabet.charAt(0);
|
|
429
|
+
if (i < 0) {
|
|
430
|
+
--e;
|
|
431
|
+
} else {
|
|
432
|
+
x.c = xc;
|
|
433
|
+
x.e = e;
|
|
434
|
+
x.s = sign;
|
|
435
|
+
x = div(x, y, dp, rm, baseOut);
|
|
436
|
+
xc = x.c;
|
|
437
|
+
r = x.r;
|
|
438
|
+
e = x.e;
|
|
439
|
+
}
|
|
440
|
+
d = e + dp + 1;
|
|
441
|
+
i = xc[d];
|
|
442
|
+
k = baseOut / 2;
|
|
443
|
+
r = r || d < 0 || xc[d + 1] != null;
|
|
444
|
+
r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : i > k || i == k && (rm == 4 || r || rm == 6 && xc[d - 1] & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
445
|
+
if (d < 1 || !xc[0]) {
|
|
446
|
+
str = r ? toFixedPoint(alphabet.charAt(1), -dp, alphabet.charAt(0)) : alphabet.charAt(0);
|
|
447
|
+
} else {
|
|
448
|
+
xc.length = d;
|
|
449
|
+
if (r) {
|
|
450
|
+
for (--baseOut; ++xc[--d] > baseOut; ) {
|
|
451
|
+
xc[d] = 0;
|
|
452
|
+
if (!d) {
|
|
453
|
+
++e;
|
|
454
|
+
xc = [1].concat(xc);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
for (k = xc.length; !xc[--k]; ) ;
|
|
459
|
+
for (i = 0, str = ""; i <= k; str += alphabet.charAt(xc[i++])) ;
|
|
460
|
+
str = toFixedPoint(str, e, alphabet.charAt(0));
|
|
461
|
+
}
|
|
462
|
+
return str;
|
|
463
|
+
};
|
|
464
|
+
})();
|
|
465
|
+
div = /* @__PURE__ */ (function() {
|
|
466
|
+
function multiply(x, k, base) {
|
|
467
|
+
var m, temp, xlo, xhi, carry = 0, i = x.length, klo = k % SQRT_BASE, khi = k / SQRT_BASE | 0;
|
|
468
|
+
for (x = x.slice(); i--; ) {
|
|
469
|
+
xlo = x[i] % SQRT_BASE;
|
|
470
|
+
xhi = x[i] / SQRT_BASE | 0;
|
|
471
|
+
m = khi * xlo + xhi * klo;
|
|
472
|
+
temp = klo * xlo + m % SQRT_BASE * SQRT_BASE + carry;
|
|
473
|
+
carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
|
|
474
|
+
x[i] = temp % base;
|
|
475
|
+
}
|
|
476
|
+
if (carry) x = [carry].concat(x);
|
|
477
|
+
return x;
|
|
478
|
+
}
|
|
479
|
+
function compare2(a, b, aL, bL) {
|
|
480
|
+
var i, cmp;
|
|
481
|
+
if (aL != bL) {
|
|
482
|
+
cmp = aL > bL ? 1 : -1;
|
|
483
|
+
} else {
|
|
484
|
+
for (i = cmp = 0; i < aL; i++) {
|
|
485
|
+
if (a[i] != b[i]) {
|
|
486
|
+
cmp = a[i] > b[i] ? 1 : -1;
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return cmp;
|
|
492
|
+
}
|
|
493
|
+
function subtract(a, b, aL, base) {
|
|
494
|
+
var i = 0;
|
|
495
|
+
for (; aL--; ) {
|
|
496
|
+
a[aL] -= i;
|
|
497
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
498
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
499
|
+
}
|
|
500
|
+
for (; !a[0] && a.length > 1; a.splice(0, 1)) ;
|
|
501
|
+
}
|
|
502
|
+
return function(x, y, dp, rm, base) {
|
|
503
|
+
var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0, yL, yz, s = x.s == y.s ? 1 : -1, xc = x.c, yc = y.c;
|
|
504
|
+
if (!xc || !xc[0] || !yc || !yc[0]) {
|
|
505
|
+
return new BigNumber2(
|
|
506
|
+
// Return NaN if either NaN, or both Infinity or 0.
|
|
507
|
+
!x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN : (
|
|
508
|
+
// Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
|
|
509
|
+
xc && xc[0] == 0 || !yc ? s * 0 : s / 0
|
|
510
|
+
)
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
q = new BigNumber2(s);
|
|
514
|
+
qc = q.c = [];
|
|
515
|
+
e = x.e - y.e;
|
|
516
|
+
s = dp + e + 1;
|
|
517
|
+
if (!base) {
|
|
518
|
+
base = BASE;
|
|
519
|
+
e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
|
|
520
|
+
s = s / LOG_BASE | 0;
|
|
521
|
+
}
|
|
522
|
+
for (i = 0; yc[i] == (xc[i] || 0); i++) ;
|
|
523
|
+
if (yc[i] > (xc[i] || 0)) e--;
|
|
524
|
+
if (s < 0) {
|
|
525
|
+
qc.push(1);
|
|
526
|
+
more = true;
|
|
527
|
+
} else {
|
|
528
|
+
xL = xc.length;
|
|
529
|
+
yL = yc.length;
|
|
530
|
+
i = 0;
|
|
531
|
+
s += 2;
|
|
532
|
+
n = mathfloor(base / (yc[0] + 1));
|
|
533
|
+
if (n > 1) {
|
|
534
|
+
yc = multiply(yc, n, base);
|
|
535
|
+
xc = multiply(xc, n, base);
|
|
536
|
+
yL = yc.length;
|
|
537
|
+
xL = xc.length;
|
|
538
|
+
}
|
|
539
|
+
xi = yL;
|
|
540
|
+
rem = xc.slice(0, yL);
|
|
541
|
+
remL = rem.length;
|
|
542
|
+
for (; remL < yL; rem[remL++] = 0) ;
|
|
543
|
+
yz = yc.slice();
|
|
544
|
+
yz = [0].concat(yz);
|
|
545
|
+
yc0 = yc[0];
|
|
546
|
+
if (yc[1] >= base / 2) yc0++;
|
|
547
|
+
do {
|
|
548
|
+
n = 0;
|
|
549
|
+
cmp = compare2(yc, rem, yL, remL);
|
|
550
|
+
if (cmp < 0) {
|
|
551
|
+
rem0 = rem[0];
|
|
552
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
553
|
+
n = mathfloor(rem0 / yc0);
|
|
554
|
+
if (n > 1) {
|
|
555
|
+
if (n >= base) n = base - 1;
|
|
556
|
+
prod = multiply(yc, n, base);
|
|
557
|
+
prodL = prod.length;
|
|
558
|
+
remL = rem.length;
|
|
559
|
+
while (compare2(prod, rem, prodL, remL) == 1) {
|
|
560
|
+
n--;
|
|
561
|
+
subtract(prod, yL < prodL ? yz : yc, prodL, base);
|
|
562
|
+
prodL = prod.length;
|
|
563
|
+
cmp = 1;
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
if (n == 0) {
|
|
567
|
+
cmp = n = 1;
|
|
568
|
+
}
|
|
569
|
+
prod = yc.slice();
|
|
570
|
+
prodL = prod.length;
|
|
571
|
+
}
|
|
572
|
+
if (prodL < remL) prod = [0].concat(prod);
|
|
573
|
+
subtract(rem, prod, remL, base);
|
|
574
|
+
remL = rem.length;
|
|
575
|
+
if (cmp == -1) {
|
|
576
|
+
while (compare2(yc, rem, yL, remL) < 1) {
|
|
577
|
+
n++;
|
|
578
|
+
subtract(rem, yL < remL ? yz : yc, remL, base);
|
|
579
|
+
remL = rem.length;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
} else if (cmp === 0) {
|
|
583
|
+
n++;
|
|
584
|
+
rem = [0];
|
|
585
|
+
}
|
|
586
|
+
qc[i++] = n;
|
|
587
|
+
if (rem[0]) {
|
|
588
|
+
rem[remL++] = xc[xi] || 0;
|
|
589
|
+
} else {
|
|
590
|
+
rem = [xc[xi]];
|
|
591
|
+
remL = 1;
|
|
592
|
+
}
|
|
593
|
+
} while ((xi++ < xL || rem[0] != null) && s--);
|
|
594
|
+
more = rem[0] != null;
|
|
595
|
+
if (!qc[0]) qc.splice(0, 1);
|
|
596
|
+
}
|
|
597
|
+
if (base == BASE) {
|
|
598
|
+
for (i = 1, s = qc[0]; s >= 10; s /= 10, i++) ;
|
|
599
|
+
round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more);
|
|
600
|
+
} else {
|
|
601
|
+
q.e = e;
|
|
602
|
+
q.r = +more;
|
|
603
|
+
}
|
|
604
|
+
return q;
|
|
605
|
+
};
|
|
606
|
+
})();
|
|
607
|
+
function format(n, i, rm, id) {
|
|
608
|
+
var c0, e, ne, len, str;
|
|
609
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
610
|
+
else intCheck(rm, 0, 8);
|
|
611
|
+
if (!n.c) return n.toString();
|
|
612
|
+
c0 = n.c[0];
|
|
613
|
+
ne = n.e;
|
|
614
|
+
if (i == null) {
|
|
615
|
+
str = coeffToString(n.c);
|
|
616
|
+
str = id == 1 || id == 2 && (ne <= TO_EXP_NEG || ne >= TO_EXP_POS) ? toExponential(str, ne) : toFixedPoint(str, ne, "0");
|
|
617
|
+
} else {
|
|
618
|
+
n = round(new BigNumber2(n), i, rm);
|
|
619
|
+
e = n.e;
|
|
620
|
+
str = coeffToString(n.c);
|
|
621
|
+
len = str.length;
|
|
622
|
+
if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
|
|
623
|
+
for (; len < i; str += "0", len++) ;
|
|
624
|
+
str = toExponential(str, e);
|
|
625
|
+
} else {
|
|
626
|
+
i -= ne;
|
|
627
|
+
str = toFixedPoint(str, e, "0");
|
|
628
|
+
if (e + 1 > len) {
|
|
629
|
+
if (--i > 0) for (str += "."; i--; str += "0") ;
|
|
630
|
+
} else {
|
|
631
|
+
i += e - len;
|
|
632
|
+
if (i > 0) {
|
|
633
|
+
if (e + 1 == len) str += ".";
|
|
634
|
+
for (; i--; str += "0") ;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return n.s < 0 && c0 ? "-" + str : str;
|
|
640
|
+
}
|
|
641
|
+
function maxOrMin(args, n) {
|
|
642
|
+
var k, y, i = 1, x = new BigNumber2(args[0]);
|
|
643
|
+
for (; i < args.length; i++) {
|
|
644
|
+
y = new BigNumber2(args[i]);
|
|
645
|
+
if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
|
|
646
|
+
x = y;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
return x;
|
|
650
|
+
}
|
|
651
|
+
function normalise(n, c, e) {
|
|
652
|
+
var i = 1, j = c.length;
|
|
653
|
+
for (; !c[--j]; c.pop()) ;
|
|
654
|
+
for (j = c[0]; j >= 10; j /= 10, i++) ;
|
|
655
|
+
if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
|
|
656
|
+
n.c = n.e = null;
|
|
657
|
+
} else if (e < MIN_EXP) {
|
|
658
|
+
n.c = [n.e = 0];
|
|
659
|
+
} else {
|
|
660
|
+
n.e = e;
|
|
661
|
+
n.c = c;
|
|
662
|
+
}
|
|
663
|
+
return n;
|
|
664
|
+
}
|
|
665
|
+
parseNumeric = /* @__PURE__ */ (function() {
|
|
666
|
+
var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, dotAfter = /^([^.]+)\.$/, dotBefore = /^\.([^.]+)$/, isInfinityOrNaN = /^-?(Infinity|NaN)$/, whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
|
|
667
|
+
return function(x, str, isNum, b) {
|
|
668
|
+
var base, s = isNum ? str : str.replace(whitespaceOrPlus, "");
|
|
669
|
+
if (isInfinityOrNaN.test(s)) {
|
|
670
|
+
x.s = isNaN(s) ? null : s < 0 ? -1 : 1;
|
|
671
|
+
} else {
|
|
672
|
+
if (!isNum) {
|
|
673
|
+
s = s.replace(basePrefix, function(m, p1, p2) {
|
|
674
|
+
base = (p2 = p2.toLowerCase()) == "x" ? 16 : p2 == "b" ? 2 : 8;
|
|
675
|
+
return !b || b == base ? p1 : m;
|
|
676
|
+
});
|
|
677
|
+
if (b) {
|
|
678
|
+
base = b;
|
|
679
|
+
s = s.replace(dotAfter, "$1").replace(dotBefore, "0.$1");
|
|
680
|
+
}
|
|
681
|
+
if (str != s) return new BigNumber2(s, base);
|
|
682
|
+
}
|
|
683
|
+
if (BigNumber2.DEBUG) {
|
|
684
|
+
throw Error(bignumberError + "Not a" + (b ? " base " + b : "") + " number: " + str);
|
|
685
|
+
}
|
|
686
|
+
x.s = null;
|
|
687
|
+
}
|
|
688
|
+
x.c = x.e = null;
|
|
689
|
+
};
|
|
690
|
+
})();
|
|
691
|
+
function round(x, sd, rm, r) {
|
|
692
|
+
var d, i, j, k, n, ni, rd, xc = x.c, pows10 = POWS_TEN;
|
|
693
|
+
if (xc) {
|
|
694
|
+
out: {
|
|
695
|
+
for (d = 1, k = xc[0]; k >= 10; k /= 10, d++) ;
|
|
696
|
+
i = sd - d;
|
|
697
|
+
if (i < 0) {
|
|
698
|
+
i += LOG_BASE;
|
|
699
|
+
j = sd;
|
|
700
|
+
n = xc[ni = 0];
|
|
701
|
+
rd = mathfloor(n / pows10[d - j - 1] % 10);
|
|
702
|
+
} else {
|
|
703
|
+
ni = mathceil((i + 1) / LOG_BASE);
|
|
704
|
+
if (ni >= xc.length) {
|
|
705
|
+
if (r) {
|
|
706
|
+
for (; xc.length <= ni; xc.push(0)) ;
|
|
707
|
+
n = rd = 0;
|
|
708
|
+
d = 1;
|
|
709
|
+
i %= LOG_BASE;
|
|
710
|
+
j = i - LOG_BASE + 1;
|
|
711
|
+
} else {
|
|
712
|
+
break out;
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
n = k = xc[ni];
|
|
716
|
+
for (d = 1; k >= 10; k /= 10, d++) ;
|
|
717
|
+
i %= LOG_BASE;
|
|
718
|
+
j = i - LOG_BASE + d;
|
|
719
|
+
rd = j < 0 ? 0 : mathfloor(n / pows10[d - j - 1] % 10);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
r = r || sd < 0 || // Are there any non-zero digits after the rounding digit?
|
|
723
|
+
// The expression n % pows10[d - j - 1] returns all digits of n to the right
|
|
724
|
+
// of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
|
|
725
|
+
xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
|
|
726
|
+
r = rm < 4 ? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 && // Check whether the digit to the left of the rounding digit is odd.
|
|
727
|
+
(i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
|
|
728
|
+
if (sd < 1 || !xc[0]) {
|
|
729
|
+
xc.length = 0;
|
|
730
|
+
if (r) {
|
|
731
|
+
sd -= x.e + 1;
|
|
732
|
+
xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
|
|
733
|
+
x.e = -sd || 0;
|
|
734
|
+
} else {
|
|
735
|
+
xc[0] = x.e = 0;
|
|
736
|
+
}
|
|
737
|
+
return x;
|
|
738
|
+
}
|
|
739
|
+
if (i == 0) {
|
|
740
|
+
xc.length = ni;
|
|
741
|
+
k = 1;
|
|
742
|
+
ni--;
|
|
743
|
+
} else {
|
|
744
|
+
xc.length = ni + 1;
|
|
745
|
+
k = pows10[LOG_BASE - i];
|
|
746
|
+
xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0;
|
|
747
|
+
}
|
|
748
|
+
if (r) {
|
|
749
|
+
for (; ; ) {
|
|
750
|
+
if (ni == 0) {
|
|
751
|
+
for (i = 1, j = xc[0]; j >= 10; j /= 10, i++) ;
|
|
752
|
+
j = xc[0] += k;
|
|
753
|
+
for (k = 1; j >= 10; j /= 10, k++) ;
|
|
754
|
+
if (i != k) {
|
|
755
|
+
x.e++;
|
|
756
|
+
if (xc[0] == BASE) xc[0] = 1;
|
|
757
|
+
}
|
|
758
|
+
break;
|
|
759
|
+
} else {
|
|
760
|
+
xc[ni] += k;
|
|
761
|
+
if (xc[ni] != BASE) break;
|
|
762
|
+
xc[ni--] = 0;
|
|
763
|
+
k = 1;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
for (i = xc.length; xc[--i] === 0; xc.pop()) ;
|
|
768
|
+
}
|
|
769
|
+
if (x.e > MAX_EXP) {
|
|
770
|
+
x.c = x.e = null;
|
|
771
|
+
} else if (x.e < MIN_EXP) {
|
|
772
|
+
x.c = [x.e = 0];
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
return x;
|
|
776
|
+
}
|
|
777
|
+
function valueOf(n) {
|
|
778
|
+
var str, e = n.e;
|
|
779
|
+
if (e === null) return n.toString();
|
|
780
|
+
str = coeffToString(n.c);
|
|
781
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(str, e) : toFixedPoint(str, e, "0");
|
|
782
|
+
return n.s < 0 ? "-" + str : str;
|
|
783
|
+
}
|
|
784
|
+
P.absoluteValue = P.abs = function() {
|
|
785
|
+
var x = new BigNumber2(this);
|
|
786
|
+
if (x.s < 0) x.s = 1;
|
|
787
|
+
return x;
|
|
788
|
+
};
|
|
789
|
+
P.comparedTo = function(y, b) {
|
|
790
|
+
return compare(this, new BigNumber2(y, b));
|
|
791
|
+
};
|
|
792
|
+
P.decimalPlaces = P.dp = function(dp, rm) {
|
|
793
|
+
var c, n, v, x = this;
|
|
794
|
+
if (dp != null) {
|
|
795
|
+
intCheck(dp, 0, MAX);
|
|
796
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
797
|
+
else intCheck(rm, 0, 8);
|
|
798
|
+
return round(new BigNumber2(x), dp + x.e + 1, rm);
|
|
799
|
+
}
|
|
800
|
+
if (!(c = x.c)) return null;
|
|
801
|
+
n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
802
|
+
if (v = c[v]) for (; v % 10 == 0; v /= 10, n--) ;
|
|
803
|
+
if (n < 0) n = 0;
|
|
804
|
+
return n;
|
|
805
|
+
};
|
|
806
|
+
P.dividedBy = P.div = function(y, b) {
|
|
807
|
+
return div(this, new BigNumber2(y, b), DECIMAL_PLACES, ROUNDING_MODE);
|
|
808
|
+
};
|
|
809
|
+
P.dividedToIntegerBy = P.idiv = function(y, b) {
|
|
810
|
+
return div(this, new BigNumber2(y, b), 0, 1);
|
|
811
|
+
};
|
|
812
|
+
P.exponentiatedBy = P.pow = function(n, m) {
|
|
813
|
+
var half, isModExp, i, k, more, nIsBig, nIsNeg, nIsOdd, y, x = this;
|
|
814
|
+
n = new BigNumber2(n);
|
|
815
|
+
if (n.c && !n.isInteger()) {
|
|
816
|
+
throw Error(bignumberError + "Exponent not an integer: " + valueOf(n));
|
|
817
|
+
}
|
|
818
|
+
if (m != null) m = new BigNumber2(m);
|
|
819
|
+
nIsBig = n.e > 14;
|
|
820
|
+
if (!x.c || !x.c[0] || x.c[0] == 1 && !x.e && x.c.length == 1 || !n.c || !n.c[0]) {
|
|
821
|
+
y = new BigNumber2(Math.pow(+valueOf(x), nIsBig ? n.s * (2 - isOdd(n)) : +valueOf(n)));
|
|
822
|
+
return m ? y.mod(m) : y;
|
|
823
|
+
}
|
|
824
|
+
nIsNeg = n.s < 0;
|
|
825
|
+
if (m) {
|
|
826
|
+
if (m.c ? !m.c[0] : !m.s) return new BigNumber2(NaN);
|
|
827
|
+
isModExp = !nIsNeg && x.isInteger() && m.isInteger();
|
|
828
|
+
if (isModExp) x = x.mod(m);
|
|
829
|
+
} else if (n.e > 9 && (x.e > 0 || x.e < -1 || (x.e == 0 ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7 : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) {
|
|
830
|
+
k = x.s < 0 && isOdd(n) ? -0 : 0;
|
|
831
|
+
if (x.e > -1) k = 1 / k;
|
|
832
|
+
return new BigNumber2(nIsNeg ? 1 / k : k);
|
|
833
|
+
} else if (POW_PRECISION) {
|
|
834
|
+
k = mathceil(POW_PRECISION / LOG_BASE + 2);
|
|
835
|
+
}
|
|
836
|
+
if (nIsBig) {
|
|
837
|
+
half = new BigNumber2(0.5);
|
|
838
|
+
if (nIsNeg) n.s = 1;
|
|
839
|
+
nIsOdd = isOdd(n);
|
|
840
|
+
} else {
|
|
841
|
+
i = Math.abs(+valueOf(n));
|
|
842
|
+
nIsOdd = i % 2;
|
|
843
|
+
}
|
|
844
|
+
y = new BigNumber2(ONE);
|
|
845
|
+
for (; ; ) {
|
|
846
|
+
if (nIsOdd) {
|
|
847
|
+
y = y.times(x);
|
|
848
|
+
if (!y.c) break;
|
|
849
|
+
if (k) {
|
|
850
|
+
if (y.c.length > k) y.c.length = k;
|
|
851
|
+
} else if (isModExp) {
|
|
852
|
+
y = y.mod(m);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
if (i) {
|
|
856
|
+
i = mathfloor(i / 2);
|
|
857
|
+
if (i === 0) break;
|
|
858
|
+
nIsOdd = i % 2;
|
|
859
|
+
} else {
|
|
860
|
+
n = n.times(half);
|
|
861
|
+
round(n, n.e + 1, 1);
|
|
862
|
+
if (n.e > 14) {
|
|
863
|
+
nIsOdd = isOdd(n);
|
|
864
|
+
} else {
|
|
865
|
+
i = +valueOf(n);
|
|
866
|
+
if (i === 0) break;
|
|
867
|
+
nIsOdd = i % 2;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
x = x.times(x);
|
|
871
|
+
if (k) {
|
|
872
|
+
if (x.c && x.c.length > k) x.c.length = k;
|
|
873
|
+
} else if (isModExp) {
|
|
874
|
+
x = x.mod(m);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
if (isModExp) return y;
|
|
878
|
+
if (nIsNeg) y = ONE.div(y);
|
|
879
|
+
return m ? y.mod(m) : k ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
|
|
880
|
+
};
|
|
881
|
+
P.integerValue = function(rm) {
|
|
882
|
+
var n = new BigNumber2(this);
|
|
883
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
884
|
+
else intCheck(rm, 0, 8);
|
|
885
|
+
return round(n, n.e + 1, rm);
|
|
886
|
+
};
|
|
887
|
+
P.isEqualTo = P.eq = function(y, b) {
|
|
888
|
+
return compare(this, new BigNumber2(y, b)) === 0;
|
|
889
|
+
};
|
|
890
|
+
P.isFinite = function() {
|
|
891
|
+
return !!this.c;
|
|
892
|
+
};
|
|
893
|
+
P.isGreaterThan = P.gt = function(y, b) {
|
|
894
|
+
return compare(this, new BigNumber2(y, b)) > 0;
|
|
895
|
+
};
|
|
896
|
+
P.isGreaterThanOrEqualTo = P.gte = function(y, b) {
|
|
897
|
+
return (b = compare(this, new BigNumber2(y, b))) === 1 || b === 0;
|
|
898
|
+
};
|
|
899
|
+
P.isInteger = function() {
|
|
900
|
+
return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
|
|
901
|
+
};
|
|
902
|
+
P.isLessThan = P.lt = function(y, b) {
|
|
903
|
+
return compare(this, new BigNumber2(y, b)) < 0;
|
|
904
|
+
};
|
|
905
|
+
P.isLessThanOrEqualTo = P.lte = function(y, b) {
|
|
906
|
+
return (b = compare(this, new BigNumber2(y, b))) === -1 || b === 0;
|
|
907
|
+
};
|
|
908
|
+
P.isNaN = function() {
|
|
909
|
+
return !this.s;
|
|
910
|
+
};
|
|
911
|
+
P.isNegative = function() {
|
|
912
|
+
return this.s < 0;
|
|
913
|
+
};
|
|
914
|
+
P.isPositive = function() {
|
|
915
|
+
return this.s > 0;
|
|
916
|
+
};
|
|
917
|
+
P.isZero = function() {
|
|
918
|
+
return !!this.c && this.c[0] == 0;
|
|
919
|
+
};
|
|
920
|
+
P.minus = function(y, b) {
|
|
921
|
+
var i, j, t, xLTy, x = this, a = x.s;
|
|
922
|
+
y = new BigNumber2(y, b);
|
|
923
|
+
b = y.s;
|
|
924
|
+
if (!a || !b) return new BigNumber2(NaN);
|
|
925
|
+
if (a != b) {
|
|
926
|
+
y.s = -b;
|
|
927
|
+
return x.plus(y);
|
|
928
|
+
}
|
|
929
|
+
var xe = x.e / LOG_BASE, ye = y.e / LOG_BASE, xc = x.c, yc = y.c;
|
|
930
|
+
if (!xe || !ye) {
|
|
931
|
+
if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber2(yc ? x : NaN);
|
|
932
|
+
if (!xc[0] || !yc[0]) {
|
|
933
|
+
return yc[0] ? (y.s = -b, y) : new BigNumber2(xc[0] ? x : (
|
|
934
|
+
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
|
|
935
|
+
ROUNDING_MODE == 3 ? -0 : 0
|
|
936
|
+
));
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
xe = bitFloor(xe);
|
|
940
|
+
ye = bitFloor(ye);
|
|
941
|
+
xc = xc.slice();
|
|
942
|
+
if (a = xe - ye) {
|
|
943
|
+
if (xLTy = a < 0) {
|
|
944
|
+
a = -a;
|
|
945
|
+
t = xc;
|
|
946
|
+
} else {
|
|
947
|
+
ye = xe;
|
|
948
|
+
t = yc;
|
|
949
|
+
}
|
|
950
|
+
t.reverse();
|
|
951
|
+
for (b = a; b--; t.push(0)) ;
|
|
952
|
+
t.reverse();
|
|
953
|
+
} else {
|
|
954
|
+
j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b;
|
|
955
|
+
for (a = b = 0; b < j; b++) {
|
|
956
|
+
if (xc[b] != yc[b]) {
|
|
957
|
+
xLTy = xc[b] < yc[b];
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
if (xLTy) {
|
|
963
|
+
t = xc;
|
|
964
|
+
xc = yc;
|
|
965
|
+
yc = t;
|
|
966
|
+
y.s = -y.s;
|
|
967
|
+
}
|
|
968
|
+
b = (j = yc.length) - (i = xc.length);
|
|
969
|
+
if (b > 0) for (; b--; xc[i++] = 0) ;
|
|
970
|
+
b = BASE - 1;
|
|
971
|
+
for (; j > a; ) {
|
|
972
|
+
if (xc[--j] < yc[j]) {
|
|
973
|
+
for (i = j; i && !xc[--i]; xc[i] = b) ;
|
|
974
|
+
--xc[i];
|
|
975
|
+
xc[j] += BASE;
|
|
976
|
+
}
|
|
977
|
+
xc[j] -= yc[j];
|
|
978
|
+
}
|
|
979
|
+
for (; xc[0] == 0; xc.splice(0, 1), --ye) ;
|
|
980
|
+
if (!xc[0]) {
|
|
981
|
+
y.s = ROUNDING_MODE == 3 ? -1 : 1;
|
|
982
|
+
y.c = [y.e = 0];
|
|
983
|
+
return y;
|
|
984
|
+
}
|
|
985
|
+
return normalise(y, xc, ye);
|
|
986
|
+
};
|
|
987
|
+
P.modulo = P.mod = function(y, b) {
|
|
988
|
+
var q, s, x = this;
|
|
989
|
+
y = new BigNumber2(y, b);
|
|
990
|
+
if (!x.c || !y.s || y.c && !y.c[0]) {
|
|
991
|
+
return new BigNumber2(NaN);
|
|
992
|
+
} else if (!y.c || x.c && !x.c[0]) {
|
|
993
|
+
return new BigNumber2(x);
|
|
994
|
+
}
|
|
995
|
+
if (MODULO_MODE == 9) {
|
|
996
|
+
s = y.s;
|
|
997
|
+
y.s = 1;
|
|
998
|
+
q = div(x, y, 0, 3);
|
|
999
|
+
y.s = s;
|
|
1000
|
+
q.s *= s;
|
|
1001
|
+
} else {
|
|
1002
|
+
q = div(x, y, 0, MODULO_MODE);
|
|
1003
|
+
}
|
|
1004
|
+
y = x.minus(q.times(y));
|
|
1005
|
+
if (!y.c[0] && MODULO_MODE == 1) y.s = x.s;
|
|
1006
|
+
return y;
|
|
1007
|
+
};
|
|
1008
|
+
P.multipliedBy = P.times = function(y, b) {
|
|
1009
|
+
var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, base, sqrtBase, x = this, xc = x.c, yc = (y = new BigNumber2(y, b)).c;
|
|
1010
|
+
if (!xc || !yc || !xc[0] || !yc[0]) {
|
|
1011
|
+
if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
|
|
1012
|
+
y.c = y.e = y.s = null;
|
|
1013
|
+
} else {
|
|
1014
|
+
y.s *= x.s;
|
|
1015
|
+
if (!xc || !yc) {
|
|
1016
|
+
y.c = y.e = null;
|
|
1017
|
+
} else {
|
|
1018
|
+
y.c = [0];
|
|
1019
|
+
y.e = 0;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return y;
|
|
1023
|
+
}
|
|
1024
|
+
e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
|
|
1025
|
+
y.s *= x.s;
|
|
1026
|
+
xcL = xc.length;
|
|
1027
|
+
ycL = yc.length;
|
|
1028
|
+
if (xcL < ycL) {
|
|
1029
|
+
zc = xc;
|
|
1030
|
+
xc = yc;
|
|
1031
|
+
yc = zc;
|
|
1032
|
+
i = xcL;
|
|
1033
|
+
xcL = ycL;
|
|
1034
|
+
ycL = i;
|
|
1035
|
+
}
|
|
1036
|
+
for (i = xcL + ycL, zc = []; i--; zc.push(0)) ;
|
|
1037
|
+
base = BASE;
|
|
1038
|
+
sqrtBase = SQRT_BASE;
|
|
1039
|
+
for (i = ycL; --i >= 0; ) {
|
|
1040
|
+
c = 0;
|
|
1041
|
+
ylo = yc[i] % sqrtBase;
|
|
1042
|
+
yhi = yc[i] / sqrtBase | 0;
|
|
1043
|
+
for (k = xcL, j = i + k; j > i; ) {
|
|
1044
|
+
xlo = xc[--k] % sqrtBase;
|
|
1045
|
+
xhi = xc[k] / sqrtBase | 0;
|
|
1046
|
+
m = yhi * xlo + xhi * ylo;
|
|
1047
|
+
xlo = ylo * xlo + m % sqrtBase * sqrtBase + zc[j] + c;
|
|
1048
|
+
c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
|
|
1049
|
+
zc[j--] = xlo % base;
|
|
1050
|
+
}
|
|
1051
|
+
zc[j] = c;
|
|
1052
|
+
}
|
|
1053
|
+
if (c) {
|
|
1054
|
+
++e;
|
|
1055
|
+
} else {
|
|
1056
|
+
zc.splice(0, 1);
|
|
1057
|
+
}
|
|
1058
|
+
return normalise(y, zc, e);
|
|
1059
|
+
};
|
|
1060
|
+
P.negated = function() {
|
|
1061
|
+
var x = new BigNumber2(this);
|
|
1062
|
+
x.s = -x.s || null;
|
|
1063
|
+
return x;
|
|
1064
|
+
};
|
|
1065
|
+
P.plus = function(y, b) {
|
|
1066
|
+
var t, x = this, a = x.s;
|
|
1067
|
+
y = new BigNumber2(y, b);
|
|
1068
|
+
b = y.s;
|
|
1069
|
+
if (!a || !b) return new BigNumber2(NaN);
|
|
1070
|
+
if (a != b) {
|
|
1071
|
+
y.s = -b;
|
|
1072
|
+
return x.minus(y);
|
|
1073
|
+
}
|
|
1074
|
+
var xe = x.e / LOG_BASE, ye = y.e / LOG_BASE, xc = x.c, yc = y.c;
|
|
1075
|
+
if (!xe || !ye) {
|
|
1076
|
+
if (!xc || !yc) return new BigNumber2(a / 0);
|
|
1077
|
+
if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber2(xc[0] ? x : a * 0);
|
|
1078
|
+
}
|
|
1079
|
+
xe = bitFloor(xe);
|
|
1080
|
+
ye = bitFloor(ye);
|
|
1081
|
+
xc = xc.slice();
|
|
1082
|
+
if (a = xe - ye) {
|
|
1083
|
+
if (a > 0) {
|
|
1084
|
+
ye = xe;
|
|
1085
|
+
t = yc;
|
|
1086
|
+
} else {
|
|
1087
|
+
a = -a;
|
|
1088
|
+
t = xc;
|
|
1089
|
+
}
|
|
1090
|
+
t.reverse();
|
|
1091
|
+
for (; a--; t.push(0)) ;
|
|
1092
|
+
t.reverse();
|
|
1093
|
+
}
|
|
1094
|
+
a = xc.length;
|
|
1095
|
+
b = yc.length;
|
|
1096
|
+
if (a - b < 0) {
|
|
1097
|
+
t = yc;
|
|
1098
|
+
yc = xc;
|
|
1099
|
+
xc = t;
|
|
1100
|
+
b = a;
|
|
1101
|
+
}
|
|
1102
|
+
for (a = 0; b; ) {
|
|
1103
|
+
a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0;
|
|
1104
|
+
xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE;
|
|
1105
|
+
}
|
|
1106
|
+
if (a) {
|
|
1107
|
+
xc = [a].concat(xc);
|
|
1108
|
+
++ye;
|
|
1109
|
+
}
|
|
1110
|
+
return normalise(y, xc, ye);
|
|
1111
|
+
};
|
|
1112
|
+
P.precision = P.sd = function(sd, rm) {
|
|
1113
|
+
var c, n, v, x = this;
|
|
1114
|
+
if (sd != null && sd !== !!sd) {
|
|
1115
|
+
intCheck(sd, 1, MAX);
|
|
1116
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
1117
|
+
else intCheck(rm, 0, 8);
|
|
1118
|
+
return round(new BigNumber2(x), sd, rm);
|
|
1119
|
+
}
|
|
1120
|
+
if (!(c = x.c)) return null;
|
|
1121
|
+
v = c.length - 1;
|
|
1122
|
+
n = v * LOG_BASE + 1;
|
|
1123
|
+
if (v = c[v]) {
|
|
1124
|
+
for (; v % 10 == 0; v /= 10, n--) ;
|
|
1125
|
+
for (v = c[0]; v >= 10; v /= 10, n++) ;
|
|
1126
|
+
}
|
|
1127
|
+
if (sd && x.e + 1 > n) n = x.e + 1;
|
|
1128
|
+
return n;
|
|
1129
|
+
};
|
|
1130
|
+
P.shiftedBy = function(k) {
|
|
1131
|
+
intCheck(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
|
|
1132
|
+
return this.times("1e" + k);
|
|
1133
|
+
};
|
|
1134
|
+
P.squareRoot = P.sqrt = function() {
|
|
1135
|
+
var m, n, r, rep, t, x = this, c = x.c, s = x.s, e = x.e, dp = DECIMAL_PLACES + 4, half = new BigNumber2("0.5");
|
|
1136
|
+
if (s !== 1 || !c || !c[0]) {
|
|
1137
|
+
return new BigNumber2(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0);
|
|
1138
|
+
}
|
|
1139
|
+
s = Math.sqrt(+valueOf(x));
|
|
1140
|
+
if (s == 0 || s == 1 / 0) {
|
|
1141
|
+
n = coeffToString(c);
|
|
1142
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
1143
|
+
s = Math.sqrt(+n);
|
|
1144
|
+
e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
1145
|
+
if (s == 1 / 0) {
|
|
1146
|
+
n = "5e" + e;
|
|
1147
|
+
} else {
|
|
1148
|
+
n = s.toExponential();
|
|
1149
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
1150
|
+
}
|
|
1151
|
+
r = new BigNumber2(n);
|
|
1152
|
+
} else {
|
|
1153
|
+
r = new BigNumber2(s + "");
|
|
1154
|
+
}
|
|
1155
|
+
if (r.c[0]) {
|
|
1156
|
+
e = r.e;
|
|
1157
|
+
s = e + dp;
|
|
1158
|
+
if (s < 3) s = 0;
|
|
1159
|
+
for (; ; ) {
|
|
1160
|
+
t = r;
|
|
1161
|
+
r = half.times(t.plus(div(x, t, dp, 1)));
|
|
1162
|
+
if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
|
|
1163
|
+
if (r.e < e) --s;
|
|
1164
|
+
n = n.slice(s - 3, s + 1);
|
|
1165
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
1166
|
+
if (!rep) {
|
|
1167
|
+
round(t, t.e + DECIMAL_PLACES + 2, 0);
|
|
1168
|
+
if (t.times(t).eq(x)) {
|
|
1169
|
+
r = t;
|
|
1170
|
+
break;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
dp += 4;
|
|
1174
|
+
s += 4;
|
|
1175
|
+
rep = 1;
|
|
1176
|
+
} else {
|
|
1177
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
1178
|
+
round(r, r.e + DECIMAL_PLACES + 2, 1);
|
|
1179
|
+
m = !r.times(r).eq(x);
|
|
1180
|
+
}
|
|
1181
|
+
break;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
|
|
1187
|
+
};
|
|
1188
|
+
P.toExponential = function(dp, rm) {
|
|
1189
|
+
if (dp != null) {
|
|
1190
|
+
intCheck(dp, 0, MAX);
|
|
1191
|
+
dp++;
|
|
1192
|
+
}
|
|
1193
|
+
return format(this, dp, rm, 1);
|
|
1194
|
+
};
|
|
1195
|
+
P.toFixed = function(dp, rm) {
|
|
1196
|
+
if (dp != null) {
|
|
1197
|
+
intCheck(dp, 0, MAX);
|
|
1198
|
+
dp = dp + this.e + 1;
|
|
1199
|
+
}
|
|
1200
|
+
return format(this, dp, rm);
|
|
1201
|
+
};
|
|
1202
|
+
P.toFormat = function(dp, rm, format2) {
|
|
1203
|
+
var str, x = this;
|
|
1204
|
+
if (format2 == null) {
|
|
1205
|
+
if (dp != null && rm && typeof rm == "object") {
|
|
1206
|
+
format2 = rm;
|
|
1207
|
+
rm = null;
|
|
1208
|
+
} else if (dp && typeof dp == "object") {
|
|
1209
|
+
format2 = dp;
|
|
1210
|
+
dp = rm = null;
|
|
1211
|
+
} else {
|
|
1212
|
+
format2 = FORMAT;
|
|
1213
|
+
}
|
|
1214
|
+
} else if (typeof format2 != "object") {
|
|
1215
|
+
throw Error(bignumberError + "Argument not an object: " + format2);
|
|
1216
|
+
}
|
|
1217
|
+
str = x.toFixed(dp, rm);
|
|
1218
|
+
if (x.c) {
|
|
1219
|
+
var i, arr = str.split("."), g1 = +format2.groupSize, g2 = +format2.secondaryGroupSize, groupSeparator = format2.groupSeparator || "", intPart = arr[0], fractionPart = arr[1], isNeg = x.s < 0, intDigits = isNeg ? intPart.slice(1) : intPart, len = intDigits.length;
|
|
1220
|
+
if (g2) {
|
|
1221
|
+
i = g1;
|
|
1222
|
+
g1 = g2;
|
|
1223
|
+
g2 = i;
|
|
1224
|
+
len -= i;
|
|
1225
|
+
}
|
|
1226
|
+
if (g1 > 0 && len > 0) {
|
|
1227
|
+
i = len % g1 || g1;
|
|
1228
|
+
intPart = intDigits.substr(0, i);
|
|
1229
|
+
for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
|
|
1230
|
+
if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
|
|
1231
|
+
if (isNeg) intPart = "-" + intPart;
|
|
1232
|
+
}
|
|
1233
|
+
str = fractionPart ? intPart + (format2.decimalSeparator || "") + ((g2 = +format2.fractionGroupSize) ? fractionPart.replace(
|
|
1234
|
+
new RegExp("\\d{" + g2 + "}\\B", "g"),
|
|
1235
|
+
"$&" + (format2.fractionGroupSeparator || "")
|
|
1236
|
+
) : fractionPart) : intPart;
|
|
1237
|
+
}
|
|
1238
|
+
return (format2.prefix || "") + str + (format2.suffix || "");
|
|
1239
|
+
};
|
|
1240
|
+
P.toFraction = function(md) {
|
|
1241
|
+
var d, d0, d1, d2, e, exp, n, n0, n1, q, r, s, x = this, xc = x.c;
|
|
1242
|
+
if (md != null) {
|
|
1243
|
+
n = new BigNumber2(md);
|
|
1244
|
+
if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
|
|
1245
|
+
throw Error(bignumberError + "Argument " + (n.isInteger() ? "out of range: " : "not an integer: ") + valueOf(n));
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
if (!xc) return new BigNumber2(x);
|
|
1249
|
+
d = new BigNumber2(ONE);
|
|
1250
|
+
n1 = d0 = new BigNumber2(ONE);
|
|
1251
|
+
d1 = n0 = new BigNumber2(ONE);
|
|
1252
|
+
s = coeffToString(xc);
|
|
1253
|
+
e = d.e = s.length - x.e - 1;
|
|
1254
|
+
d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
|
|
1255
|
+
md = !md || n.comparedTo(d) > 0 ? e > 0 ? d : n1 : n;
|
|
1256
|
+
exp = MAX_EXP;
|
|
1257
|
+
MAX_EXP = 1 / 0;
|
|
1258
|
+
n = new BigNumber2(s);
|
|
1259
|
+
n0.c[0] = 0;
|
|
1260
|
+
for (; ; ) {
|
|
1261
|
+
q = div(n, d, 0, 1);
|
|
1262
|
+
d2 = d0.plus(q.times(d1));
|
|
1263
|
+
if (d2.comparedTo(md) == 1) break;
|
|
1264
|
+
d0 = d1;
|
|
1265
|
+
d1 = d2;
|
|
1266
|
+
n1 = n0.plus(q.times(d2 = n1));
|
|
1267
|
+
n0 = d2;
|
|
1268
|
+
d = n.minus(q.times(d2 = d));
|
|
1269
|
+
n = d2;
|
|
1270
|
+
}
|
|
1271
|
+
d2 = div(md.minus(d0), d1, 0, 1);
|
|
1272
|
+
n0 = n0.plus(d2.times(n1));
|
|
1273
|
+
d0 = d0.plus(d2.times(d1));
|
|
1274
|
+
n0.s = n1.s = x.s;
|
|
1275
|
+
e = e * 2;
|
|
1276
|
+
r = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().comparedTo(
|
|
1277
|
+
div(n0, d0, e, ROUNDING_MODE).minus(x).abs()
|
|
1278
|
+
) < 1 ? [n1, d1] : [n0, d0];
|
|
1279
|
+
MAX_EXP = exp;
|
|
1280
|
+
return r;
|
|
1281
|
+
};
|
|
1282
|
+
P.toNumber = function() {
|
|
1283
|
+
return +valueOf(this);
|
|
1284
|
+
};
|
|
1285
|
+
P.toPrecision = function(sd, rm) {
|
|
1286
|
+
if (sd != null) intCheck(sd, 1, MAX);
|
|
1287
|
+
return format(this, sd, rm, 2);
|
|
1288
|
+
};
|
|
1289
|
+
P.toString = function(b) {
|
|
1290
|
+
var str, n = this, s = n.s, e = n.e;
|
|
1291
|
+
if (e === null) {
|
|
1292
|
+
if (s) {
|
|
1293
|
+
str = "Infinity";
|
|
1294
|
+
if (s < 0) str = "-" + str;
|
|
1295
|
+
} else {
|
|
1296
|
+
str = "NaN";
|
|
1297
|
+
}
|
|
1298
|
+
} else {
|
|
1299
|
+
if (b == null) {
|
|
1300
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(coeffToString(n.c), e) : toFixedPoint(coeffToString(n.c), e, "0");
|
|
1301
|
+
} else if (b === 10 && alphabetHasNormalDecimalDigits) {
|
|
1302
|
+
n = round(new BigNumber2(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
|
|
1303
|
+
str = toFixedPoint(coeffToString(n.c), n.e, "0");
|
|
1304
|
+
} else {
|
|
1305
|
+
intCheck(b, 2, ALPHABET.length, "Base");
|
|
1306
|
+
str = convertBase(toFixedPoint(coeffToString(n.c), e, "0"), 10, b, s, true);
|
|
1307
|
+
}
|
|
1308
|
+
if (s < 0 && n.c[0]) str = "-" + str;
|
|
1309
|
+
}
|
|
1310
|
+
return str;
|
|
1311
|
+
};
|
|
1312
|
+
P.valueOf = P.toJSON = function() {
|
|
1313
|
+
return valueOf(this);
|
|
1314
|
+
};
|
|
1315
|
+
P._isBigNumber = true;
|
|
1316
|
+
P[Symbol.toStringTag] = "BigNumber";
|
|
1317
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.valueOf;
|
|
1318
|
+
if (configObject != null) BigNumber2.set(configObject);
|
|
1319
|
+
return BigNumber2;
|
|
1320
|
+
}
|
|
1321
|
+
function bitFloor(n) {
|
|
1322
|
+
var i = n | 0;
|
|
1323
|
+
return n > 0 || n === i ? i : i - 1;
|
|
1324
|
+
}
|
|
1325
|
+
function coeffToString(a) {
|
|
1326
|
+
var s, z, i = 1, j = a.length, r = a[0] + "";
|
|
1327
|
+
for (; i < j; ) {
|
|
1328
|
+
s = a[i++] + "";
|
|
1329
|
+
z = LOG_BASE - s.length;
|
|
1330
|
+
for (; z--; s = "0" + s) ;
|
|
1331
|
+
r += s;
|
|
1332
|
+
}
|
|
1333
|
+
for (j = r.length; r.charCodeAt(--j) === 48; ) ;
|
|
1334
|
+
return r.slice(0, j + 1 || 1);
|
|
1335
|
+
}
|
|
1336
|
+
function compare(x, y) {
|
|
1337
|
+
var a, b, xc = x.c, yc = y.c, i = x.s, j = y.s, k = x.e, l = y.e;
|
|
1338
|
+
if (!i || !j) return null;
|
|
1339
|
+
a = xc && !xc[0];
|
|
1340
|
+
b = yc && !yc[0];
|
|
1341
|
+
if (a || b) return a ? b ? 0 : -j : i;
|
|
1342
|
+
if (i != j) return i;
|
|
1343
|
+
a = i < 0;
|
|
1344
|
+
b = k == l;
|
|
1345
|
+
if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1;
|
|
1346
|
+
if (!b) return k > l ^ a ? 1 : -1;
|
|
1347
|
+
j = (k = xc.length) < (l = yc.length) ? k : l;
|
|
1348
|
+
for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
|
|
1349
|
+
return k == l ? 0 : k > l ^ a ? 1 : -1;
|
|
1350
|
+
}
|
|
1351
|
+
function intCheck(n, min, max, name) {
|
|
1352
|
+
if (n < min || n > max || n !== mathfloor(n)) {
|
|
1353
|
+
throw Error(bignumberError + (name || "Argument") + (typeof n == "number" ? n < min || n > max ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(n));
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
function isOdd(n) {
|
|
1357
|
+
var k = n.c.length - 1;
|
|
1358
|
+
return bitFloor(n.e / LOG_BASE) == k && n.c[k] % 2 != 0;
|
|
1359
|
+
}
|
|
1360
|
+
function toExponential(str, e) {
|
|
1361
|
+
return (str.length > 1 ? str.charAt(0) + "." + str.slice(1) : str) + (e < 0 ? "e" : "e+") + e;
|
|
1362
|
+
}
|
|
1363
|
+
function toFixedPoint(str, e, z) {
|
|
1364
|
+
var len, zs;
|
|
1365
|
+
if (e < 0) {
|
|
1366
|
+
for (zs = z + "."; ++e; zs += z) ;
|
|
1367
|
+
str = zs + str;
|
|
1368
|
+
} else {
|
|
1369
|
+
len = str.length;
|
|
1370
|
+
if (++e > len) {
|
|
1371
|
+
for (zs = z, e -= len; --e; zs += z) ;
|
|
1372
|
+
str += zs;
|
|
1373
|
+
} else if (e < len) {
|
|
1374
|
+
str = str.slice(0, e) + "." + str.slice(e);
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
return str;
|
|
1378
|
+
}
|
|
1379
|
+
var BigNumber = clone();
|
|
1380
|
+
var bignumber_default = BigNumber;
|
|
1381
|
+
|
|
1382
|
+
// src/protocol.ts
|
|
1383
|
+
var RLUSD_ASSET = "524C555344000000000000000000000000000000";
|
|
1384
|
+
var PAYMENT_HEADERS = {
|
|
1385
|
+
required: "PAYMENT-REQUIRED",
|
|
1386
|
+
signature: "PAYMENT-SIGNATURE",
|
|
1387
|
+
response: "PAYMENT-RESPONSE"
|
|
1388
|
+
};
|
|
1389
|
+
function isRecord(value) {
|
|
1390
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1391
|
+
}
|
|
1392
|
+
function positiveAmount(value, integer = false) {
|
|
1393
|
+
if (typeof value !== "string" || value.length > 100 || !(integer ? /^\d+$/ : /^\d+(?:\.\d+)?(?:[eE][+-]?\d{1,2})?$/).test(value)) {
|
|
1394
|
+
throw new Error("Amount must be a positive decimal string.");
|
|
1395
|
+
}
|
|
1396
|
+
const amount = new bignumber_default(value);
|
|
1397
|
+
if (!amount.isFinite() || amount.lte(0) || amount.gt("100000000000") || (integer ? !amount.isInteger() : amount.precision() > 16)) {
|
|
1398
|
+
throw new Error("Amount is outside the supported XRPL range.");
|
|
1399
|
+
}
|
|
1400
|
+
return amount;
|
|
1401
|
+
}
|
|
1402
|
+
function amountEquals(a, b) {
|
|
1403
|
+
return positiveAmount(a).eq(positiveAmount(b));
|
|
1404
|
+
}
|
|
1405
|
+
function xrpToDrops(amount) {
|
|
1406
|
+
const drops = positiveAmount(amount).times(1e6);
|
|
1407
|
+
if (!drops.isInteger()) throw new Error("XRP amounts must be whole drops.");
|
|
1408
|
+
return drops.toFixed(0);
|
|
1409
|
+
}
|
|
1410
|
+
function validateRequirements(value) {
|
|
1411
|
+
if (!isRecord(value) || value.scheme !== "exact" || !["xrpl:0", "xrpl:1"].includes(value.network) || !["XRP", RLUSD_ASSET].includes(value.asset) || typeof value.payTo !== "string" || !Number.isInteger(value.maxTimeoutSeconds) || value.maxTimeoutSeconds < 10 || value.maxTimeoutSeconds > 600 || !isRecord(value.extra) || value.extra.areFeesSponsored !== false || value.extra.assetTransferMethod !== void 0 && value.extra.assetTransferMethod !== "sequence" || value.extra.invoiceId !== void 0 && (typeof value.extra.invoiceId !== "string" || value.extra.invoiceId.length < 1 || value.extra.invoiceId.length > 1024) || value.asset !== "XRP" && typeof value.extra.issuer !== "string" || value.asset === "XRP" && value.extra.issuer !== void 0 || value.extra.destinationTag !== void 0 && (!Number.isInteger(value.extra.destinationTag) || value.extra.destinationTag < 0 || value.extra.destinationTag > 4294967295)) {
|
|
1412
|
+
throw new Error("Unsupported or invalid XRPL payment requirements.");
|
|
1413
|
+
}
|
|
1414
|
+
positiveAmount(value.amount, value.asset === "XRP");
|
|
1415
|
+
}
|
|
1416
|
+
function requirementsKey(value) {
|
|
1417
|
+
validateRequirements(value);
|
|
1418
|
+
return JSON.stringify([
|
|
1419
|
+
value.scheme,
|
|
1420
|
+
value.network,
|
|
1421
|
+
value.asset,
|
|
1422
|
+
value.amount,
|
|
1423
|
+
value.payTo,
|
|
1424
|
+
value.maxTimeoutSeconds,
|
|
1425
|
+
value.extra.areFeesSponsored,
|
|
1426
|
+
value.extra.assetTransferMethod ?? "sequence",
|
|
1427
|
+
value.extra.invoiceId ?? null,
|
|
1428
|
+
value.extra.issuer ?? null,
|
|
1429
|
+
value.extra.destinationTag ?? null
|
|
1430
|
+
]);
|
|
1431
|
+
}
|
|
1432
|
+
function validatePayload(value, requirement) {
|
|
1433
|
+
validateRequirements(requirement);
|
|
1434
|
+
if (!isRecord(value) || value.x402Version !== 2 || !isRecord(value.payload) || typeof value.payload.signedTxBlob !== "string" || !/^[A-Fa-f0-9]+$/.test(value.payload.signedTxBlob) || value.payload.signedTxBlob.length % 2 !== 0 || value.payload.signedTxBlob.length > 16384 || requirementsKey(value.accepted) !== requirementsKey(requirement)) {
|
|
1435
|
+
throw new Error("Payment payload does not match the required terms.");
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
function encodePaymentHeader(value) {
|
|
1439
|
+
const bytes = new TextEncoder().encode(JSON.stringify(value));
|
|
1440
|
+
let binary = "";
|
|
1441
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
1442
|
+
return btoa(binary);
|
|
1443
|
+
}
|
|
1444
|
+
function decodePaymentHeader(value) {
|
|
1445
|
+
if (value.length > 32768 || !/^[A-Za-z0-9+/]+={0,2}$/.test(value)) throw new Error("Invalid payment header.");
|
|
1446
|
+
const bytes = Uint8Array.from(atob(value), (char) => char.charCodeAt(0));
|
|
1447
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
// src/client.ts
|
|
1451
|
+
async function invoiceHash(invoiceId) {
|
|
1452
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(invoiceId));
|
|
1453
|
+
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("").toUpperCase();
|
|
1454
|
+
}
|
|
1455
|
+
var X402Client = class {
|
|
1456
|
+
options;
|
|
1457
|
+
totalAuthorized = positiveAmount("1").minus(1);
|
|
1458
|
+
queue = Promise.resolve();
|
|
1459
|
+
unresolvedPayment = false;
|
|
1460
|
+
pendingReceipt;
|
|
1461
|
+
constructor(options) {
|
|
1462
|
+
if (!options?.wallet?.sign || !options.ledgerClient?.autofill || !options.ledgerClient.getLedgerIndex || !["xrpl:0", "xrpl:1"].includes(options.network) || !["XRP", "RLUSD"].includes(options.currency) || options.currency === "RLUSD" && !options.issuer) {
|
|
1463
|
+
throw new Error("Configure a signing wallet, connected ledger client, network, and currency.");
|
|
1464
|
+
}
|
|
1465
|
+
positiveAmount(options.maxAmount);
|
|
1466
|
+
positiveAmount(options.maxTotalAmount);
|
|
1467
|
+
positiveAmount(options.maxFeeDrops ?? "1000", true);
|
|
1468
|
+
this.options = options;
|
|
1469
|
+
}
|
|
1470
|
+
get authorizedAmount() {
|
|
1471
|
+
return this.totalAuthorized.toFixed();
|
|
1472
|
+
}
|
|
1473
|
+
/** Serialize requests: the sequence scheme permits one pending payment per payer. */
|
|
1474
|
+
fetch(input, init, beforeSubmit) {
|
|
1475
|
+
const operation = this.queue.then(() => this.fetchOnce(input, init, beforeSubmit));
|
|
1476
|
+
this.queue = operation.catch(() => void 0);
|
|
1477
|
+
return operation;
|
|
1478
|
+
}
|
|
1479
|
+
/** Accept only the receipt matching this client's unresolved signed hash. */
|
|
1480
|
+
reconcileReceipt(receipt) {
|
|
1481
|
+
if (!this.pendingReceipt || !isRecord(receipt) || receipt.success !== true || receipt.network !== this.pendingReceipt.network || receipt.transaction?.toUpperCase() !== this.pendingReceipt.transaction) return false;
|
|
1482
|
+
this.unresolvedPayment = false;
|
|
1483
|
+
this.pendingReceipt = void 0;
|
|
1484
|
+
return true;
|
|
1485
|
+
}
|
|
1486
|
+
selectRequirement(required, url) {
|
|
1487
|
+
if (!isRecord(required) || required.x402Version !== 2 || !isRecord(required.resource) || required.resource.url !== url || !Array.isArray(required.accepts)) throw new Error("Invalid x402 v2 resource challenge.");
|
|
1488
|
+
for (const candidate of required.accepts) {
|
|
1489
|
+
try {
|
|
1490
|
+
validateRequirements(candidate);
|
|
1491
|
+
} catch {
|
|
1492
|
+
continue;
|
|
1493
|
+
}
|
|
1494
|
+
if (candidate.network === this.options.network && candidate.asset === (this.options.currency === "XRP" ? "XRP" : RLUSD_ASSET) && (candidate.asset === "XRP" || candidate.extra.issuer === this.options.issuer)) return candidate;
|
|
1495
|
+
}
|
|
1496
|
+
throw new Error("No payment option matches the configured network and asset.");
|
|
1497
|
+
}
|
|
1498
|
+
async fetchOnce(input, init, beforeSubmit) {
|
|
1499
|
+
if (this.unresolvedPayment) throw new Error("A previously authorized payment has an unresolved outcome. Reconcile it before authorizing another payment.");
|
|
1500
|
+
const request = new Request(input, init);
|
|
1501
|
+
if (request.headers.has(PAYMENT_HEADERS.signature)) throw new Error("The agent SDK manages its own payment signature header.");
|
|
1502
|
+
const fetcher = this.options.fetch ?? globalThis.fetch;
|
|
1503
|
+
const response = await fetcher(request.clone());
|
|
1504
|
+
if (response.status !== 402) return response;
|
|
1505
|
+
const header = response.headers.get(PAYMENT_HEADERS.required);
|
|
1506
|
+
if (!header) throw new Error("402 response missing PAYMENT-REQUIRED header.");
|
|
1507
|
+
const required = decodePaymentHeader(header);
|
|
1508
|
+
const requirement = this.selectRequirement(required, request.url);
|
|
1509
|
+
if (this.options.allowedRecipients && !this.options.allowedRecipients.includes(requirement.payTo)) throw new Error("Payment recipient is not allowed.");
|
|
1510
|
+
const humanAmount = positiveAmount(requirement.amount).div(requirement.asset === "XRP" ? 1e6 : 1);
|
|
1511
|
+
if (humanAmount.gt(positiveAmount(this.options.maxAmount)) || this.totalAuthorized.plus(humanAmount).gt(positiveAmount(this.options.maxTotalAmount))) {
|
|
1512
|
+
throw new Error("Payment exceeds the agent spending limits.");
|
|
1513
|
+
}
|
|
1514
|
+
const ledgerIndex = await this.options.ledgerClient.getLedgerIndex();
|
|
1515
|
+
const amount = requirement.asset === "XRP" ? requirement.amount : {
|
|
1516
|
+
currency: requirement.asset,
|
|
1517
|
+
issuer: requirement.extra.issuer,
|
|
1518
|
+
value: requirement.amount
|
|
1519
|
+
};
|
|
1520
|
+
const transaction = {
|
|
1521
|
+
TransactionType: "Payment",
|
|
1522
|
+
Account: this.options.wallet.address ?? this.options.wallet.classicAddress,
|
|
1523
|
+
Destination: requirement.payTo,
|
|
1524
|
+
Amount: amount,
|
|
1525
|
+
...requirement.extra.invoiceId !== void 0 ? { InvoiceID: await invoiceHash(requirement.extra.invoiceId) } : {},
|
|
1526
|
+
LastLedgerSequence: ledgerIndex + Math.ceil(requirement.maxTimeoutSeconds / 5),
|
|
1527
|
+
...requirement.extra.destinationTag !== void 0 ? { DestinationTag: requirement.extra.destinationTag } : {},
|
|
1528
|
+
...requirement.asset !== "XRP" ? { SendMax: amount } : {}
|
|
1529
|
+
};
|
|
1530
|
+
const prepared = await this.options.ledgerClient.autofill(transaction);
|
|
1531
|
+
if (positiveAmount(prepared.Fee, true).gt(positiveAmount(this.options.maxFeeDrops ?? "1000", true))) throw new Error("Network fee exceeds the agent limit.");
|
|
1532
|
+
if (prepared.TicketSequence !== void 0 || prepared.NetworkID !== void 0 || !Number.isInteger(prepared.Sequence) || prepared.Sequence < 1) {
|
|
1533
|
+
throw new Error("Ledger client returned an unsupported account sequence or network.");
|
|
1534
|
+
}
|
|
1535
|
+
const signed = await this.options.wallet.sign(prepared);
|
|
1536
|
+
if (!signed?.tx_blob || !/^[A-Fa-f0-9]{64}$/.test(signed.hash)) throw new Error("Wallet did not return a signed XRPL transaction.");
|
|
1537
|
+
this.totalAuthorized = this.totalAuthorized.plus(humanAmount);
|
|
1538
|
+
this.unresolvedPayment = true;
|
|
1539
|
+
const payload = { x402Version: 2, resource: required.resource, accepted: requirement, payload: { signedTxBlob: signed.tx_blob } };
|
|
1540
|
+
this.pendingReceipt = { transaction: signed.hash.toUpperCase(), network: requirement.network };
|
|
1541
|
+
if (beforeSubmit) await beforeSubmit(payload);
|
|
1542
|
+
const retry = request.clone();
|
|
1543
|
+
retry.headers.set(PAYMENT_HEADERS.signature, encodePaymentHeader(payload));
|
|
1544
|
+
const result = await fetcher(new Request(retry, { redirect: "error" }));
|
|
1545
|
+
const receiptHeader = result.headers.get(PAYMENT_HEADERS.response);
|
|
1546
|
+
if (receiptHeader) {
|
|
1547
|
+
const receipt = decodePaymentHeader(receiptHeader);
|
|
1548
|
+
this.reconcileReceipt(receipt);
|
|
1549
|
+
}
|
|
1550
|
+
return result;
|
|
1551
|
+
}
|
|
1552
|
+
};
|