coinley-checkout 0.2.5 → 0.2.6
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/dist/coinley-checkout.es.js +1300 -92
- package/dist/coinley-checkout.es.js.map +1 -1
- package/dist/style.css +4 -0
- package/package.json +1 -1
@@ -1,13 +1,20 @@
|
|
1
|
-
import
|
1
|
+
import React, { createContext, useContext, useState, useEffect, forwardRef, useImperativeHandle } from "react";
|
2
2
|
const styles = "";
|
3
3
|
let apiConfig = {
|
4
4
|
apiKey: null,
|
5
5
|
apiSecret: null,
|
6
|
-
apiUrl: "https://coinleyserver-production.up.railway.app"
|
6
|
+
apiUrl: "https://coinleyserver-production.up.railway.app",
|
7
|
+
merchantWalletAddress: null,
|
8
|
+
merchantSolWalletAddress: null
|
7
9
|
};
|
8
10
|
const initializeApi = (config) => {
|
9
11
|
apiConfig = { ...apiConfig, ...config };
|
10
|
-
console.log("API initialized with:",
|
12
|
+
console.log("API initialized with:", {
|
13
|
+
apiUrl: apiConfig.apiUrl,
|
14
|
+
apiKey: apiConfig.apiKey ? `${apiConfig.apiKey.substring(0, 6)}...` : null,
|
15
|
+
hasWalletAddress: !!apiConfig.merchantWalletAddress,
|
16
|
+
hasSolWalletAddress: !!apiConfig.merchantSolWalletAddress
|
17
|
+
});
|
11
18
|
};
|
12
19
|
const getHeaders = () => {
|
13
20
|
return {
|
@@ -20,10 +27,17 @@ const createPayment = async (paymentData) => {
|
|
20
27
|
try {
|
21
28
|
console.log("Creating payment with data:", paymentData);
|
22
29
|
console.log("API URL:", `${apiConfig.apiUrl}/api/payments/create`);
|
30
|
+
const enhancedPaymentData = { ...paymentData };
|
31
|
+
if (apiConfig.merchantWalletAddress && !enhancedPaymentData.walletAddress) {
|
32
|
+
enhancedPaymentData.walletAddress = apiConfig.merchantWalletAddress;
|
33
|
+
}
|
34
|
+
if (apiConfig.merchantSolWalletAddress && !enhancedPaymentData.solWalletAddress) {
|
35
|
+
enhancedPaymentData.solWalletAddress = apiConfig.merchantSolWalletAddress;
|
36
|
+
}
|
23
37
|
const response = await fetch(`${apiConfig.apiUrl}/api/payments/create`, {
|
24
38
|
method: "POST",
|
25
39
|
headers: getHeaders(),
|
26
|
-
body: JSON.stringify(
|
40
|
+
body: JSON.stringify(enhancedPaymentData)
|
27
41
|
});
|
28
42
|
console.log("Create payment response status:", response.status);
|
29
43
|
if (!response.ok) {
|
@@ -33,6 +47,14 @@ const createPayment = async (paymentData) => {
|
|
33
47
|
}
|
34
48
|
const data = await response.json();
|
35
49
|
console.log("Create payment response data:", data);
|
50
|
+
if (data && data.payment) {
|
51
|
+
if (!data.payment.walletAddress && apiConfig.merchantWalletAddress) {
|
52
|
+
data.payment.walletAddress = apiConfig.merchantWalletAddress;
|
53
|
+
}
|
54
|
+
if (!data.payment.solWalletAddress && apiConfig.merchantSolWalletAddress) {
|
55
|
+
data.payment.solWalletAddress = apiConfig.merchantSolWalletAddress;
|
56
|
+
}
|
57
|
+
}
|
36
58
|
return data;
|
37
59
|
} catch (error) {
|
38
60
|
console.error("Create payment error:", error);
|
@@ -53,6 +75,14 @@ const getPayment = async (paymentId) => {
|
|
53
75
|
}
|
54
76
|
const data = await response.json();
|
55
77
|
console.log("Get payment response:", data);
|
78
|
+
if (data && data.payment) {
|
79
|
+
if (!data.payment.walletAddress && apiConfig.merchantWalletAddress) {
|
80
|
+
data.payment.walletAddress = apiConfig.merchantWalletAddress;
|
81
|
+
}
|
82
|
+
if (!data.payment.solWalletAddress && apiConfig.merchantSolWalletAddress) {
|
83
|
+
data.payment.solWalletAddress = apiConfig.merchantSolWalletAddress;
|
84
|
+
}
|
85
|
+
}
|
56
86
|
return data;
|
57
87
|
} catch (error) {
|
58
88
|
console.error("Get payment error:", error);
|
@@ -82,6 +112,35 @@ const processPayment = async (processData) => {
|
|
82
112
|
throw error;
|
83
113
|
}
|
84
114
|
};
|
115
|
+
const getMerchantWalletAddresses = async () => {
|
116
|
+
var _a, _b;
|
117
|
+
try {
|
118
|
+
const response = await fetch(`${apiConfig.apiUrl}/api/merchants/profile`, {
|
119
|
+
method: "GET",
|
120
|
+
headers: getHeaders()
|
121
|
+
});
|
122
|
+
if (!response.ok) {
|
123
|
+
const errorData = await response.json();
|
124
|
+
throw new Error(errorData.error || `Failed to get merchant profile: ${response.status}`);
|
125
|
+
}
|
126
|
+
const data = await response.json();
|
127
|
+
if (data.merchant) {
|
128
|
+
if (data.merchant.walletAddress) {
|
129
|
+
apiConfig.merchantWalletAddress = data.merchant.walletAddress;
|
130
|
+
}
|
131
|
+
if (data.merchant.solWalletAddress) {
|
132
|
+
apiConfig.merchantSolWalletAddress = data.merchant.solWalletAddress;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
return {
|
136
|
+
walletAddress: ((_a = data.merchant) == null ? void 0 : _a.walletAddress) || null,
|
137
|
+
solWalletAddress: ((_b = data.merchant) == null ? void 0 : _b.solWalletAddress) || null
|
138
|
+
};
|
139
|
+
} catch (error) {
|
140
|
+
console.error("Get merchant wallet addresses error:", error);
|
141
|
+
throw error;
|
142
|
+
}
|
143
|
+
};
|
85
144
|
const isMetaMaskInstalled = () => {
|
86
145
|
return typeof window !== "undefined" && typeof window.ethereum !== "undefined";
|
87
146
|
};
|
@@ -128,7 +187,7 @@ function requireReactJsxRuntime_production_min() {
|
|
128
187
|
if (hasRequiredReactJsxRuntime_production_min)
|
129
188
|
return reactJsxRuntime_production_min;
|
130
189
|
hasRequiredReactJsxRuntime_production_min = 1;
|
131
|
-
var f =
|
190
|
+
var f = React, k = Symbol.for("react.element"), l = Symbol.for("react.fragment"), m = Object.prototype.hasOwnProperty, n = f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = { key: true, ref: true, __self: true, __source: true };
|
132
191
|
function q(c, a, g) {
|
133
192
|
var b, d = {}, e = null, h = null;
|
134
193
|
void 0 !== g && (e = "" + g);
|
@@ -163,7 +222,7 @@ function requireReactJsxRuntime_development() {
|
|
163
222
|
hasRequiredReactJsxRuntime_development = 1;
|
164
223
|
if (process.env.NODE_ENV !== "production") {
|
165
224
|
(function() {
|
166
|
-
var React =
|
225
|
+
var React$1 = React;
|
167
226
|
var REACT_ELEMENT_TYPE = Symbol.for("react.element");
|
168
227
|
var REACT_PORTAL_TYPE = Symbol.for("react.portal");
|
169
228
|
var REACT_FRAGMENT_TYPE = Symbol.for("react.fragment");
|
@@ -189,7 +248,7 @@ function requireReactJsxRuntime_development() {
|
|
189
248
|
}
|
190
249
|
return null;
|
191
250
|
}
|
192
|
-
var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
251
|
+
var ReactSharedInternals = React$1.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
193
252
|
function error(format) {
|
194
253
|
{
|
195
254
|
{
|
@@ -1073,21 +1132,38 @@ const CoinleyProvider = ({
|
|
1073
1132
|
apiKey,
|
1074
1133
|
apiSecret,
|
1075
1134
|
apiUrl = "https://coinleyserver-production.up.railway.app",
|
1135
|
+
merchantWalletAddress = null,
|
1136
|
+
// New prop for merchant wallet address
|
1137
|
+
merchantSolWalletAddress = null,
|
1138
|
+
// New prop for Solana wallet address
|
1076
1139
|
debug = false,
|
1077
1140
|
children
|
1078
1141
|
}) => {
|
1079
1142
|
const [isInitialized, setIsInitialized] = useState(false);
|
1080
1143
|
const [error, setError] = useState(null);
|
1144
|
+
const [paymentData, setPaymentData] = useState(null);
|
1081
1145
|
useEffect(() => {
|
1082
1146
|
if (!apiKey || !apiSecret) {
|
1083
1147
|
setError("API key and secret are required");
|
1084
1148
|
return;
|
1085
1149
|
}
|
1086
1150
|
try {
|
1087
|
-
initializeApi({
|
1151
|
+
initializeApi({
|
1152
|
+
apiKey,
|
1153
|
+
apiSecret,
|
1154
|
+
apiUrl,
|
1155
|
+
merchantWalletAddress,
|
1156
|
+
// Pass wallet address to API service
|
1157
|
+
merchantSolWalletAddress
|
1158
|
+
});
|
1088
1159
|
setIsInitialized(true);
|
1089
1160
|
if (debug) {
|
1090
|
-
console.log("Coinley SDK initialized with:", {
|
1161
|
+
console.log("Coinley SDK initialized with:", {
|
1162
|
+
apiKey,
|
1163
|
+
apiUrl,
|
1164
|
+
merchantWalletAddress: merchantWalletAddress ? `${merchantWalletAddress.substring(0, 6)}...${merchantWalletAddress.substring(merchantWalletAddress.length - 4)}` : "Not provided",
|
1165
|
+
merchantSolWalletAddress: merchantSolWalletAddress ? `${merchantSolWalletAddress.substring(0, 6)}...${merchantSolWalletAddress.substring(merchantSolWalletAddress.length - 4)}` : "Not provided"
|
1166
|
+
});
|
1091
1167
|
}
|
1092
1168
|
} catch (err) {
|
1093
1169
|
setError(err.message);
|
@@ -1095,103 +1171,1187 @@ const CoinleyProvider = ({
|
|
1095
1171
|
console.error("Coinley SDK initialization error:", err);
|
1096
1172
|
}
|
1097
1173
|
}
|
1098
|
-
}, [apiKey, apiSecret, apiUrl, debug]);
|
1174
|
+
}, [apiKey, apiSecret, apiUrl, merchantWalletAddress, merchantSolWalletAddress, debug]);
|
1175
|
+
const storePaymentData = (data) => {
|
1176
|
+
setPaymentData(data);
|
1177
|
+
return data;
|
1178
|
+
};
|
1099
1179
|
const value = {
|
1100
1180
|
apiKey,
|
1101
1181
|
apiSecret,
|
1102
1182
|
apiUrl,
|
1183
|
+
merchantWalletAddress,
|
1184
|
+
merchantSolWalletAddress,
|
1103
1185
|
isInitialized,
|
1104
1186
|
error,
|
1105
|
-
debug
|
1187
|
+
debug,
|
1188
|
+
paymentData,
|
1189
|
+
storePaymentData
|
1106
1190
|
};
|
1107
1191
|
return /* @__PURE__ */ jsxRuntimeExports.jsx(CoinleyContext.Provider, { value, children });
|
1108
1192
|
};
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
for (
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1193
|
+
var __defProp = Object.defineProperty;
|
1194
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
1195
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
1196
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
1197
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
1198
|
+
var __spreadValues = (a, b) => {
|
1199
|
+
for (var prop in b || (b = {}))
|
1200
|
+
if (__hasOwnProp.call(b, prop))
|
1201
|
+
__defNormalProp(a, prop, b[prop]);
|
1202
|
+
if (__getOwnPropSymbols)
|
1203
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
1204
|
+
if (__propIsEnum.call(b, prop))
|
1205
|
+
__defNormalProp(a, prop, b[prop]);
|
1206
|
+
}
|
1207
|
+
return a;
|
1208
|
+
};
|
1209
|
+
var __objRest = (source, exclude) => {
|
1210
|
+
var target = {};
|
1211
|
+
for (var prop in source)
|
1212
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
1213
|
+
target[prop] = source[prop];
|
1214
|
+
if (source != null && __getOwnPropSymbols)
|
1215
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
1216
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
1217
|
+
target[prop] = source[prop];
|
1218
|
+
}
|
1219
|
+
return target;
|
1220
|
+
};
|
1221
|
+
/**
|
1222
|
+
* @license QR Code generator library (TypeScript)
|
1223
|
+
* Copyright (c) Project Nayuki.
|
1224
|
+
* SPDX-License-Identifier: MIT
|
1225
|
+
*/
|
1226
|
+
var qrcodegen;
|
1227
|
+
((qrcodegen2) => {
|
1228
|
+
const _QrCode = class _QrCode2 {
|
1229
|
+
/*-- Constructor (low level) and fields --*/
|
1230
|
+
// Creates a new QR Code with the given version number,
|
1231
|
+
// error correction level, data codeword bytes, and mask number.
|
1232
|
+
// This is a low-level API that most users should not use directly.
|
1233
|
+
// A mid-level API is the encodeSegments() function.
|
1234
|
+
constructor(version, errorCorrectionLevel, dataCodewords, msk) {
|
1235
|
+
this.version = version;
|
1236
|
+
this.errorCorrectionLevel = errorCorrectionLevel;
|
1237
|
+
this.modules = [];
|
1238
|
+
this.isFunction = [];
|
1239
|
+
if (version < _QrCode2.MIN_VERSION || version > _QrCode2.MAX_VERSION)
|
1240
|
+
throw new RangeError("Version value out of range");
|
1241
|
+
if (msk < -1 || msk > 7)
|
1242
|
+
throw new RangeError("Mask value out of range");
|
1243
|
+
this.size = version * 4 + 17;
|
1244
|
+
let row = [];
|
1245
|
+
for (let i = 0; i < this.size; i++)
|
1246
|
+
row.push(false);
|
1247
|
+
for (let i = 0; i < this.size; i++) {
|
1248
|
+
this.modules.push(row.slice());
|
1249
|
+
this.isFunction.push(row.slice());
|
1250
|
+
}
|
1251
|
+
this.drawFunctionPatterns();
|
1252
|
+
const allCodewords = this.addEccAndInterleave(dataCodewords);
|
1253
|
+
this.drawCodewords(allCodewords);
|
1254
|
+
if (msk == -1) {
|
1255
|
+
let minPenalty = 1e9;
|
1256
|
+
for (let i = 0; i < 8; i++) {
|
1257
|
+
this.applyMask(i);
|
1258
|
+
this.drawFormatBits(i);
|
1259
|
+
const penalty = this.getPenaltyScore();
|
1260
|
+
if (penalty < minPenalty) {
|
1261
|
+
msk = i;
|
1262
|
+
minPenalty = penalty;
|
1263
|
+
}
|
1264
|
+
this.applyMask(i);
|
1265
|
+
}
|
1266
|
+
}
|
1267
|
+
assert(0 <= msk && msk <= 7);
|
1268
|
+
this.mask = msk;
|
1269
|
+
this.applyMask(msk);
|
1270
|
+
this.drawFormatBits(msk);
|
1271
|
+
this.isFunction = [];
|
1272
|
+
}
|
1273
|
+
/*-- Static factory functions (high level) --*/
|
1274
|
+
// Returns a QR Code representing the given Unicode text string at the given error correction level.
|
1275
|
+
// As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer
|
1276
|
+
// Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible
|
1277
|
+
// QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the
|
1278
|
+
// ecl argument if it can be done without increasing the version.
|
1279
|
+
static encodeText(text, ecl) {
|
1280
|
+
const segs = qrcodegen2.QrSegment.makeSegments(text);
|
1281
|
+
return _QrCode2.encodeSegments(segs, ecl);
|
1282
|
+
}
|
1283
|
+
// Returns a QR Code representing the given binary data at the given error correction level.
|
1284
|
+
// This function always encodes using the binary segment mode, not any text mode. The maximum number of
|
1285
|
+
// bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
1286
|
+
// The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
1287
|
+
static encodeBinary(data, ecl) {
|
1288
|
+
const seg = qrcodegen2.QrSegment.makeBytes(data);
|
1289
|
+
return _QrCode2.encodeSegments([seg], ecl);
|
1290
|
+
}
|
1291
|
+
/*-- Static factory functions (mid level) --*/
|
1292
|
+
// Returns a QR Code representing the given segments with the given encoding parameters.
|
1293
|
+
// The smallest possible QR Code version within the given range is automatically
|
1294
|
+
// chosen for the output. Iff boostEcl is true, then the ECC level of the result
|
1295
|
+
// may be higher than the ecl argument if it can be done without increasing the
|
1296
|
+
// version. The mask number is either between 0 to 7 (inclusive) to force that
|
1297
|
+
// mask, or -1 to automatically choose an appropriate mask (which may be slow).
|
1298
|
+
// This function allows the user to create a custom sequence of segments that switches
|
1299
|
+
// between modes (such as alphanumeric and byte) to encode text in less space.
|
1300
|
+
// This is a mid-level API; the high-level API is encodeText() and encodeBinary().
|
1301
|
+
static encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
|
1302
|
+
if (!(_QrCode2.MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= _QrCode2.MAX_VERSION) || mask < -1 || mask > 7)
|
1303
|
+
throw new RangeError("Invalid value");
|
1304
|
+
let version;
|
1305
|
+
let dataUsedBits;
|
1306
|
+
for (version = minVersion; ; version++) {
|
1307
|
+
const dataCapacityBits2 = _QrCode2.getNumDataCodewords(version, ecl) * 8;
|
1308
|
+
const usedBits = QrSegment.getTotalBits(segs, version);
|
1309
|
+
if (usedBits <= dataCapacityBits2) {
|
1310
|
+
dataUsedBits = usedBits;
|
1311
|
+
break;
|
1312
|
+
}
|
1313
|
+
if (version >= maxVersion)
|
1314
|
+
throw new RangeError("Data too long");
|
1315
|
+
}
|
1316
|
+
for (const newEcl of [_QrCode2.Ecc.MEDIUM, _QrCode2.Ecc.QUARTILE, _QrCode2.Ecc.HIGH]) {
|
1317
|
+
if (boostEcl && dataUsedBits <= _QrCode2.getNumDataCodewords(version, newEcl) * 8)
|
1318
|
+
ecl = newEcl;
|
1319
|
+
}
|
1320
|
+
let bb = [];
|
1321
|
+
for (const seg of segs) {
|
1322
|
+
appendBits(seg.mode.modeBits, 4, bb);
|
1323
|
+
appendBits(seg.numChars, seg.mode.numCharCountBits(version), bb);
|
1324
|
+
for (const b of seg.getData())
|
1325
|
+
bb.push(b);
|
1326
|
+
}
|
1327
|
+
assert(bb.length == dataUsedBits);
|
1328
|
+
const dataCapacityBits = _QrCode2.getNumDataCodewords(version, ecl) * 8;
|
1329
|
+
assert(bb.length <= dataCapacityBits);
|
1330
|
+
appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
|
1331
|
+
appendBits(0, (8 - bb.length % 8) % 8, bb);
|
1332
|
+
assert(bb.length % 8 == 0);
|
1333
|
+
for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 236 ^ 17)
|
1334
|
+
appendBits(padByte, 8, bb);
|
1335
|
+
let dataCodewords = [];
|
1336
|
+
while (dataCodewords.length * 8 < bb.length)
|
1337
|
+
dataCodewords.push(0);
|
1338
|
+
bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
|
1339
|
+
return new _QrCode2(version, ecl, dataCodewords, mask);
|
1340
|
+
}
|
1341
|
+
/*-- Accessor methods --*/
|
1342
|
+
// Returns the color of the module (pixel) at the given coordinates, which is false
|
1343
|
+
// for light or true for dark. The top left corner has the coordinates (x=0, y=0).
|
1344
|
+
// If the given coordinates are out of bounds, then false (light) is returned.
|
1345
|
+
getModule(x, y) {
|
1346
|
+
return 0 <= x && x < this.size && 0 <= y && y < this.size && this.modules[y][x];
|
1347
|
+
}
|
1348
|
+
// Modified to expose modules for easy access
|
1349
|
+
getModules() {
|
1350
|
+
return this.modules;
|
1351
|
+
}
|
1352
|
+
/*-- Private helper methods for constructor: Drawing function modules --*/
|
1353
|
+
// Reads this object's version field, and draws and marks all function modules.
|
1354
|
+
drawFunctionPatterns() {
|
1355
|
+
for (let i = 0; i < this.size; i++) {
|
1356
|
+
this.setFunctionModule(6, i, i % 2 == 0);
|
1357
|
+
this.setFunctionModule(i, 6, i % 2 == 0);
|
1358
|
+
}
|
1359
|
+
this.drawFinderPattern(3, 3);
|
1360
|
+
this.drawFinderPattern(this.size - 4, 3);
|
1361
|
+
this.drawFinderPattern(3, this.size - 4);
|
1362
|
+
const alignPatPos = this.getAlignmentPatternPositions();
|
1363
|
+
const numAlign = alignPatPos.length;
|
1364
|
+
for (let i = 0; i < numAlign; i++) {
|
1365
|
+
for (let j = 0; j < numAlign; j++) {
|
1366
|
+
if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0))
|
1367
|
+
this.drawAlignmentPattern(alignPatPos[i], alignPatPos[j]);
|
1368
|
+
}
|
1369
|
+
}
|
1370
|
+
this.drawFormatBits(0);
|
1371
|
+
this.drawVersion();
|
1372
|
+
}
|
1373
|
+
// Draws two copies of the format bits (with its own error correction code)
|
1374
|
+
// based on the given mask and this object's error correction level field.
|
1375
|
+
drawFormatBits(mask) {
|
1376
|
+
const data = this.errorCorrectionLevel.formatBits << 3 | mask;
|
1377
|
+
let rem = data;
|
1378
|
+
for (let i = 0; i < 10; i++)
|
1379
|
+
rem = rem << 1 ^ (rem >>> 9) * 1335;
|
1380
|
+
const bits = (data << 10 | rem) ^ 21522;
|
1381
|
+
assert(bits >>> 15 == 0);
|
1382
|
+
for (let i = 0; i <= 5; i++)
|
1383
|
+
this.setFunctionModule(8, i, getBit(bits, i));
|
1384
|
+
this.setFunctionModule(8, 7, getBit(bits, 6));
|
1385
|
+
this.setFunctionModule(8, 8, getBit(bits, 7));
|
1386
|
+
this.setFunctionModule(7, 8, getBit(bits, 8));
|
1387
|
+
for (let i = 9; i < 15; i++)
|
1388
|
+
this.setFunctionModule(14 - i, 8, getBit(bits, i));
|
1389
|
+
for (let i = 0; i < 8; i++)
|
1390
|
+
this.setFunctionModule(this.size - 1 - i, 8, getBit(bits, i));
|
1391
|
+
for (let i = 8; i < 15; i++)
|
1392
|
+
this.setFunctionModule(8, this.size - 15 + i, getBit(bits, i));
|
1393
|
+
this.setFunctionModule(8, this.size - 8, true);
|
1394
|
+
}
|
1395
|
+
// Draws two copies of the version bits (with its own error correction code),
|
1396
|
+
// based on this object's version field, iff 7 <= version <= 40.
|
1397
|
+
drawVersion() {
|
1398
|
+
if (this.version < 7)
|
1399
|
+
return;
|
1400
|
+
let rem = this.version;
|
1401
|
+
for (let i = 0; i < 12; i++)
|
1402
|
+
rem = rem << 1 ^ (rem >>> 11) * 7973;
|
1403
|
+
const bits = this.version << 12 | rem;
|
1404
|
+
assert(bits >>> 18 == 0);
|
1405
|
+
for (let i = 0; i < 18; i++) {
|
1406
|
+
const color = getBit(bits, i);
|
1407
|
+
const a = this.size - 11 + i % 3;
|
1408
|
+
const b = Math.floor(i / 3);
|
1409
|
+
this.setFunctionModule(a, b, color);
|
1410
|
+
this.setFunctionModule(b, a, color);
|
1411
|
+
}
|
1412
|
+
}
|
1413
|
+
// Draws a 9*9 finder pattern including the border separator,
|
1414
|
+
// with the center module at (x, y). Modules can be out of bounds.
|
1415
|
+
drawFinderPattern(x, y) {
|
1416
|
+
for (let dy = -4; dy <= 4; dy++) {
|
1417
|
+
for (let dx = -4; dx <= 4; dx++) {
|
1418
|
+
const dist = Math.max(Math.abs(dx), Math.abs(dy));
|
1419
|
+
const xx = x + dx;
|
1420
|
+
const yy = y + dy;
|
1421
|
+
if (0 <= xx && xx < this.size && 0 <= yy && yy < this.size)
|
1422
|
+
this.setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
1423
|
+
}
|
1424
|
+
}
|
1425
|
+
}
|
1426
|
+
// Draws a 5*5 alignment pattern, with the center module
|
1427
|
+
// at (x, y). All modules must be in bounds.
|
1428
|
+
drawAlignmentPattern(x, y) {
|
1429
|
+
for (let dy = -2; dy <= 2; dy++) {
|
1430
|
+
for (let dx = -2; dx <= 2; dx++)
|
1431
|
+
this.setFunctionModule(x + dx, y + dy, Math.max(Math.abs(dx), Math.abs(dy)) != 1);
|
1432
|
+
}
|
1433
|
+
}
|
1434
|
+
// Sets the color of a module and marks it as a function module.
|
1435
|
+
// Only used by the constructor. Coordinates must be in bounds.
|
1436
|
+
setFunctionModule(x, y, isDark) {
|
1437
|
+
this.modules[y][x] = isDark;
|
1438
|
+
this.isFunction[y][x] = true;
|
1439
|
+
}
|
1440
|
+
/*-- Private helper methods for constructor: Codewords and masking --*/
|
1441
|
+
// Returns a new byte string representing the given data with the appropriate error correction
|
1442
|
+
// codewords appended to it, based on this object's version and error correction level.
|
1443
|
+
addEccAndInterleave(data) {
|
1444
|
+
const ver = this.version;
|
1445
|
+
const ecl = this.errorCorrectionLevel;
|
1446
|
+
if (data.length != _QrCode2.getNumDataCodewords(ver, ecl))
|
1447
|
+
throw new RangeError("Invalid argument");
|
1448
|
+
const numBlocks = _QrCode2.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
|
1449
|
+
const blockEccLen = _QrCode2.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver];
|
1450
|
+
const rawCodewords = Math.floor(_QrCode2.getNumRawDataModules(ver) / 8);
|
1451
|
+
const numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
1452
|
+
const shortBlockLen = Math.floor(rawCodewords / numBlocks);
|
1453
|
+
let blocks = [];
|
1454
|
+
const rsDiv = _QrCode2.reedSolomonComputeDivisor(blockEccLen);
|
1455
|
+
for (let i = 0, k = 0; i < numBlocks; i++) {
|
1456
|
+
let dat = data.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
|
1457
|
+
k += dat.length;
|
1458
|
+
const ecc = _QrCode2.reedSolomonComputeRemainder(dat, rsDiv);
|
1459
|
+
if (i < numShortBlocks)
|
1460
|
+
dat.push(0);
|
1461
|
+
blocks.push(dat.concat(ecc));
|
1462
|
+
}
|
1463
|
+
let result = [];
|
1464
|
+
for (let i = 0; i < blocks[0].length; i++) {
|
1465
|
+
blocks.forEach((block, j) => {
|
1466
|
+
if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
|
1467
|
+
result.push(block[i]);
|
1468
|
+
});
|
1469
|
+
}
|
1470
|
+
assert(result.length == rawCodewords);
|
1471
|
+
return result;
|
1472
|
+
}
|
1473
|
+
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
1474
|
+
// data area of this QR Code. Function modules need to be marked off before this is called.
|
1475
|
+
drawCodewords(data) {
|
1476
|
+
if (data.length != Math.floor(_QrCode2.getNumRawDataModules(this.version) / 8))
|
1477
|
+
throw new RangeError("Invalid argument");
|
1478
|
+
let i = 0;
|
1479
|
+
for (let right = this.size - 1; right >= 1; right -= 2) {
|
1480
|
+
if (right == 6)
|
1481
|
+
right = 5;
|
1482
|
+
for (let vert = 0; vert < this.size; vert++) {
|
1483
|
+
for (let j = 0; j < 2; j++) {
|
1484
|
+
const x = right - j;
|
1485
|
+
const upward = (right + 1 & 2) == 0;
|
1486
|
+
const y = upward ? this.size - 1 - vert : vert;
|
1487
|
+
if (!this.isFunction[y][x] && i < data.length * 8) {
|
1488
|
+
this.modules[y][x] = getBit(data[i >>> 3], 7 - (i & 7));
|
1489
|
+
i++;
|
1490
|
+
}
|
1491
|
+
}
|
1492
|
+
}
|
1493
|
+
}
|
1494
|
+
assert(i == data.length * 8);
|
1495
|
+
}
|
1496
|
+
// XORs the codeword modules in this QR Code with the given mask pattern.
|
1497
|
+
// The function modules must be marked and the codeword bits must be drawn
|
1498
|
+
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
1499
|
+
// the same mask value a second time will undo the mask. A final well-formed
|
1500
|
+
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
1501
|
+
applyMask(mask) {
|
1502
|
+
if (mask < 0 || mask > 7)
|
1503
|
+
throw new RangeError("Mask value out of range");
|
1504
|
+
for (let y = 0; y < this.size; y++) {
|
1505
|
+
for (let x = 0; x < this.size; x++) {
|
1506
|
+
let invert;
|
1507
|
+
switch (mask) {
|
1508
|
+
case 0:
|
1509
|
+
invert = (x + y) % 2 == 0;
|
1510
|
+
break;
|
1511
|
+
case 1:
|
1512
|
+
invert = y % 2 == 0;
|
1513
|
+
break;
|
1514
|
+
case 2:
|
1515
|
+
invert = x % 3 == 0;
|
1516
|
+
break;
|
1517
|
+
case 3:
|
1518
|
+
invert = (x + y) % 3 == 0;
|
1519
|
+
break;
|
1520
|
+
case 4:
|
1521
|
+
invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 == 0;
|
1522
|
+
break;
|
1523
|
+
case 5:
|
1524
|
+
invert = x * y % 2 + x * y % 3 == 0;
|
1525
|
+
break;
|
1526
|
+
case 6:
|
1527
|
+
invert = (x * y % 2 + x * y % 3) % 2 == 0;
|
1528
|
+
break;
|
1529
|
+
case 7:
|
1530
|
+
invert = ((x + y) % 2 + x * y % 3) % 2 == 0;
|
1531
|
+
break;
|
1532
|
+
default:
|
1533
|
+
throw new Error("Unreachable");
|
1534
|
+
}
|
1535
|
+
if (!this.isFunction[y][x] && invert)
|
1536
|
+
this.modules[y][x] = !this.modules[y][x];
|
1537
|
+
}
|
1538
|
+
}
|
1539
|
+
}
|
1540
|
+
// Calculates and returns the penalty score based on state of this QR Code's current modules.
|
1541
|
+
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
1542
|
+
getPenaltyScore() {
|
1543
|
+
let result = 0;
|
1544
|
+
for (let y = 0; y < this.size; y++) {
|
1545
|
+
let runColor = false;
|
1546
|
+
let runX = 0;
|
1547
|
+
let runHistory = [0, 0, 0, 0, 0, 0, 0];
|
1548
|
+
for (let x = 0; x < this.size; x++) {
|
1549
|
+
if (this.modules[y][x] == runColor) {
|
1550
|
+
runX++;
|
1551
|
+
if (runX == 5)
|
1552
|
+
result += _QrCode2.PENALTY_N1;
|
1553
|
+
else if (runX > 5)
|
1554
|
+
result++;
|
1555
|
+
} else {
|
1556
|
+
this.finderPenaltyAddHistory(runX, runHistory);
|
1557
|
+
if (!runColor)
|
1558
|
+
result += this.finderPenaltyCountPatterns(runHistory) * _QrCode2.PENALTY_N3;
|
1559
|
+
runColor = this.modules[y][x];
|
1560
|
+
runX = 1;
|
1561
|
+
}
|
1562
|
+
}
|
1563
|
+
result += this.finderPenaltyTerminateAndCount(runColor, runX, runHistory) * _QrCode2.PENALTY_N3;
|
1564
|
+
}
|
1565
|
+
for (let x = 0; x < this.size; x++) {
|
1566
|
+
let runColor = false;
|
1567
|
+
let runY = 0;
|
1568
|
+
let runHistory = [0, 0, 0, 0, 0, 0, 0];
|
1569
|
+
for (let y = 0; y < this.size; y++) {
|
1570
|
+
if (this.modules[y][x] == runColor) {
|
1571
|
+
runY++;
|
1572
|
+
if (runY == 5)
|
1573
|
+
result += _QrCode2.PENALTY_N1;
|
1574
|
+
else if (runY > 5)
|
1575
|
+
result++;
|
1576
|
+
} else {
|
1577
|
+
this.finderPenaltyAddHistory(runY, runHistory);
|
1578
|
+
if (!runColor)
|
1579
|
+
result += this.finderPenaltyCountPatterns(runHistory) * _QrCode2.PENALTY_N3;
|
1580
|
+
runColor = this.modules[y][x];
|
1581
|
+
runY = 1;
|
1582
|
+
}
|
1583
|
+
}
|
1584
|
+
result += this.finderPenaltyTerminateAndCount(runColor, runY, runHistory) * _QrCode2.PENALTY_N3;
|
1585
|
+
}
|
1586
|
+
for (let y = 0; y < this.size - 1; y++) {
|
1587
|
+
for (let x = 0; x < this.size - 1; x++) {
|
1588
|
+
const color = this.modules[y][x];
|
1589
|
+
if (color == this.modules[y][x + 1] && color == this.modules[y + 1][x] && color == this.modules[y + 1][x + 1])
|
1590
|
+
result += _QrCode2.PENALTY_N2;
|
1591
|
+
}
|
1592
|
+
}
|
1593
|
+
let dark = 0;
|
1594
|
+
for (const row of this.modules)
|
1595
|
+
dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
|
1596
|
+
const total = this.size * this.size;
|
1597
|
+
const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
|
1598
|
+
assert(0 <= k && k <= 9);
|
1599
|
+
result += k * _QrCode2.PENALTY_N4;
|
1600
|
+
assert(0 <= result && result <= 2568888);
|
1601
|
+
return result;
|
1602
|
+
}
|
1603
|
+
/*-- Private helper functions --*/
|
1604
|
+
// Returns an ascending list of positions of alignment patterns for this version number.
|
1605
|
+
// Each position is in the range [0,177), and are used on both the x and y axes.
|
1606
|
+
// This could be implemented as lookup table of 40 variable-length lists of integers.
|
1607
|
+
getAlignmentPatternPositions() {
|
1608
|
+
if (this.version == 1)
|
1609
|
+
return [];
|
1610
|
+
else {
|
1611
|
+
const numAlign = Math.floor(this.version / 7) + 2;
|
1612
|
+
const step = this.version == 32 ? 26 : Math.ceil((this.version * 4 + 4) / (numAlign * 2 - 2)) * 2;
|
1613
|
+
let result = [6];
|
1614
|
+
for (let pos = this.size - 7; result.length < numAlign; pos -= step)
|
1615
|
+
result.splice(1, 0, pos);
|
1616
|
+
return result;
|
1617
|
+
}
|
1618
|
+
}
|
1619
|
+
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
1620
|
+
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
1621
|
+
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
1622
|
+
static getNumRawDataModules(ver) {
|
1623
|
+
if (ver < _QrCode2.MIN_VERSION || ver > _QrCode2.MAX_VERSION)
|
1624
|
+
throw new RangeError("Version number out of range");
|
1625
|
+
let result = (16 * ver + 128) * ver + 64;
|
1626
|
+
if (ver >= 2) {
|
1627
|
+
const numAlign = Math.floor(ver / 7) + 2;
|
1628
|
+
result -= (25 * numAlign - 10) * numAlign - 55;
|
1629
|
+
if (ver >= 7)
|
1630
|
+
result -= 36;
|
1631
|
+
}
|
1632
|
+
assert(208 <= result && result <= 29648);
|
1633
|
+
return result;
|
1634
|
+
}
|
1635
|
+
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
|
1636
|
+
// QR Code of the given version number and error correction level, with remainder bits discarded.
|
1637
|
+
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
|
1638
|
+
static getNumDataCodewords(ver, ecl) {
|
1639
|
+
return Math.floor(_QrCode2.getNumRawDataModules(ver) / 8) - _QrCode2.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * _QrCode2.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
|
1640
|
+
}
|
1641
|
+
// Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
1642
|
+
// implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
1643
|
+
static reedSolomonComputeDivisor(degree) {
|
1644
|
+
if (degree < 1 || degree > 255)
|
1645
|
+
throw new RangeError("Degree out of range");
|
1646
|
+
let result = [];
|
1647
|
+
for (let i = 0; i < degree - 1; i++)
|
1648
|
+
result.push(0);
|
1649
|
+
result.push(1);
|
1650
|
+
let root = 1;
|
1651
|
+
for (let i = 0; i < degree; i++) {
|
1652
|
+
for (let j = 0; j < result.length; j++) {
|
1653
|
+
result[j] = _QrCode2.reedSolomonMultiply(result[j], root);
|
1654
|
+
if (j + 1 < result.length)
|
1655
|
+
result[j] ^= result[j + 1];
|
1656
|
+
}
|
1657
|
+
root = _QrCode2.reedSolomonMultiply(root, 2);
|
1658
|
+
}
|
1659
|
+
return result;
|
1660
|
+
}
|
1661
|
+
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
1662
|
+
static reedSolomonComputeRemainder(data, divisor) {
|
1663
|
+
let result = divisor.map((_) => 0);
|
1664
|
+
for (const b of data) {
|
1665
|
+
const factor = b ^ result.shift();
|
1666
|
+
result.push(0);
|
1667
|
+
divisor.forEach((coef, i) => result[i] ^= _QrCode2.reedSolomonMultiply(coef, factor));
|
1668
|
+
}
|
1669
|
+
return result;
|
1670
|
+
}
|
1671
|
+
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
1672
|
+
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
1673
|
+
static reedSolomonMultiply(x, y) {
|
1674
|
+
if (x >>> 8 != 0 || y >>> 8 != 0)
|
1675
|
+
throw new RangeError("Byte out of range");
|
1676
|
+
let z = 0;
|
1677
|
+
for (let i = 7; i >= 0; i--) {
|
1678
|
+
z = z << 1 ^ (z >>> 7) * 285;
|
1679
|
+
z ^= (y >>> i & 1) * x;
|
1680
|
+
}
|
1681
|
+
assert(z >>> 8 == 0);
|
1682
|
+
return z;
|
1683
|
+
}
|
1684
|
+
// Can only be called immediately after a light run is added, and
|
1685
|
+
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
1686
|
+
finderPenaltyCountPatterns(runHistory) {
|
1687
|
+
const n = runHistory[1];
|
1688
|
+
assert(n <= this.size * 3);
|
1689
|
+
const core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
1690
|
+
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
1691
|
+
}
|
1692
|
+
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
1693
|
+
finderPenaltyTerminateAndCount(currentRunColor, currentRunLength, runHistory) {
|
1694
|
+
if (currentRunColor) {
|
1695
|
+
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
1696
|
+
currentRunLength = 0;
|
1697
|
+
}
|
1698
|
+
currentRunLength += this.size;
|
1699
|
+
this.finderPenaltyAddHistory(currentRunLength, runHistory);
|
1700
|
+
return this.finderPenaltyCountPatterns(runHistory);
|
1701
|
+
}
|
1702
|
+
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
1703
|
+
finderPenaltyAddHistory(currentRunLength, runHistory) {
|
1704
|
+
if (runHistory[0] == 0)
|
1705
|
+
currentRunLength += this.size;
|
1706
|
+
runHistory.pop();
|
1707
|
+
runHistory.unshift(currentRunLength);
|
1708
|
+
}
|
1709
|
+
};
|
1710
|
+
_QrCode.MIN_VERSION = 1;
|
1711
|
+
_QrCode.MAX_VERSION = 40;
|
1712
|
+
_QrCode.PENALTY_N1 = 3;
|
1713
|
+
_QrCode.PENALTY_N2 = 3;
|
1714
|
+
_QrCode.PENALTY_N3 = 40;
|
1715
|
+
_QrCode.PENALTY_N4 = 10;
|
1716
|
+
_QrCode.ECC_CODEWORDS_PER_BLOCK = [
|
1717
|
+
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
1718
|
+
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
1719
|
+
[-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
1720
|
+
// Low
|
1721
|
+
[-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
|
1722
|
+
// Medium
|
1723
|
+
[-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
|
1724
|
+
// Quartile
|
1725
|
+
[-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
|
1726
|
+
// High
|
1727
|
+
];
|
1728
|
+
_QrCode.NUM_ERROR_CORRECTION_BLOCKS = [
|
1729
|
+
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
1730
|
+
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
|
1731
|
+
[-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
|
1732
|
+
// Low
|
1733
|
+
[-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
|
1734
|
+
// Medium
|
1735
|
+
[-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
|
1736
|
+
// Quartile
|
1737
|
+
[-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81]
|
1738
|
+
// High
|
1739
|
+
];
|
1740
|
+
qrcodegen2.QrCode = _QrCode;
|
1741
|
+
function appendBits(val, len, bb) {
|
1742
|
+
if (len < 0 || len > 31 || val >>> len != 0)
|
1743
|
+
throw new RangeError("Value out of range");
|
1744
|
+
for (let i = len - 1; i >= 0; i--)
|
1745
|
+
bb.push(val >>> i & 1);
|
1140
1746
|
}
|
1141
|
-
function
|
1142
|
-
|
1143
|
-
const size2 = 25;
|
1144
|
-
const matrix = Array(size2).fill().map(() => Array(size2).fill(0));
|
1145
|
-
addPositionPattern(matrix, 0, 0);
|
1146
|
-
addPositionPattern(matrix, 0, size2 - 7);
|
1147
|
-
addPositionPattern(matrix, size2 - 7, 0);
|
1148
|
-
for (let i = 8; i < size2 - 8; i++) {
|
1149
|
-
matrix[6][i] = i % 2 === 0 ? 1 : 0;
|
1150
|
-
matrix[i][6] = i % 2 === 0 ? 1 : 0;
|
1151
|
-
}
|
1152
|
-
addAlignmentPattern(matrix, size2 - 9, size2 - 9);
|
1153
|
-
fillData(matrix, hash, text);
|
1154
|
-
return matrix;
|
1747
|
+
function getBit(x, i) {
|
1748
|
+
return (x >>> i & 1) != 0;
|
1155
1749
|
}
|
1156
|
-
function
|
1157
|
-
|
1158
|
-
|
1159
|
-
hash = (hash << 5) - hash + str.charCodeAt(i);
|
1160
|
-
hash = hash & hash;
|
1161
|
-
}
|
1162
|
-
return Math.abs(hash);
|
1750
|
+
function assert(cond) {
|
1751
|
+
if (!cond)
|
1752
|
+
throw new Error("Assertion error");
|
1163
1753
|
}
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1754
|
+
const _QrSegment = class _QrSegment2 {
|
1755
|
+
/*-- Constructor (low level) and fields --*/
|
1756
|
+
// Creates a new QR Code segment with the given attributes and data.
|
1757
|
+
// The character count (numChars) must agree with the mode and the bit buffer length,
|
1758
|
+
// but the constraint isn't checked. The given bit buffer is cloned and stored.
|
1759
|
+
constructor(mode, numChars, bitData) {
|
1760
|
+
this.mode = mode;
|
1761
|
+
this.numChars = numChars;
|
1762
|
+
this.bitData = bitData;
|
1763
|
+
if (numChars < 0)
|
1764
|
+
throw new RangeError("Invalid argument");
|
1765
|
+
this.bitData = bitData.slice();
|
1766
|
+
}
|
1767
|
+
/*-- Static factory functions (mid level) --*/
|
1768
|
+
// Returns a segment representing the given binary data encoded in
|
1769
|
+
// byte mode. All input byte arrays are acceptable. Any text string
|
1770
|
+
// can be converted to UTF-8 bytes and encoded as a byte mode segment.
|
1771
|
+
static makeBytes(data) {
|
1772
|
+
let bb = [];
|
1773
|
+
for (const b of data)
|
1774
|
+
appendBits(b, 8, bb);
|
1775
|
+
return new _QrSegment2(_QrSegment2.Mode.BYTE, data.length, bb);
|
1776
|
+
}
|
1777
|
+
// Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
1778
|
+
static makeNumeric(digits) {
|
1779
|
+
if (!_QrSegment2.isNumeric(digits))
|
1780
|
+
throw new RangeError("String contains non-numeric characters");
|
1781
|
+
let bb = [];
|
1782
|
+
for (let i = 0; i < digits.length; ) {
|
1783
|
+
const n = Math.min(digits.length - i, 3);
|
1784
|
+
appendBits(parseInt(digits.substring(i, i + n), 10), n * 3 + 1, bb);
|
1785
|
+
i += n;
|
1168
1786
|
}
|
1787
|
+
return new _QrSegment2(_QrSegment2.Mode.NUMERIC, digits.length, bb);
|
1169
1788
|
}
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1789
|
+
// Returns a segment representing the given text string encoded in alphanumeric mode.
|
1790
|
+
// The characters allowed are: 0 to 9, A to Z (uppercase only), space,
|
1791
|
+
// dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
1792
|
+
static makeAlphanumeric(text) {
|
1793
|
+
if (!_QrSegment2.isAlphanumeric(text))
|
1794
|
+
throw new RangeError("String contains unencodable characters in alphanumeric mode");
|
1795
|
+
let bb = [];
|
1796
|
+
let i;
|
1797
|
+
for (i = 0; i + 2 <= text.length; i += 2) {
|
1798
|
+
let temp = _QrSegment2.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45;
|
1799
|
+
temp += _QrSegment2.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1));
|
1800
|
+
appendBits(temp, 11, bb);
|
1801
|
+
}
|
1802
|
+
if (i < text.length)
|
1803
|
+
appendBits(_QrSegment2.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6, bb);
|
1804
|
+
return new _QrSegment2(_QrSegment2.Mode.ALPHANUMERIC, text.length, bb);
|
1805
|
+
}
|
1806
|
+
// Returns a new mutable list of zero or more segments to represent the given Unicode text string.
|
1807
|
+
// The result may use various segment modes and switch modes to optimize the length of the bit stream.
|
1808
|
+
static makeSegments(text) {
|
1809
|
+
if (text == "")
|
1810
|
+
return [];
|
1811
|
+
else if (_QrSegment2.isNumeric(text))
|
1812
|
+
return [_QrSegment2.makeNumeric(text)];
|
1813
|
+
else if (_QrSegment2.isAlphanumeric(text))
|
1814
|
+
return [_QrSegment2.makeAlphanumeric(text)];
|
1815
|
+
else
|
1816
|
+
return [_QrSegment2.makeBytes(_QrSegment2.toUtf8ByteArray(text))];
|
1817
|
+
}
|
1818
|
+
// Returns a segment representing an Extended Channel Interpretation
|
1819
|
+
// (ECI) designator with the given assignment value.
|
1820
|
+
static makeEci(assignVal) {
|
1821
|
+
let bb = [];
|
1822
|
+
if (assignVal < 0)
|
1823
|
+
throw new RangeError("ECI assignment value out of range");
|
1824
|
+
else if (assignVal < 1 << 7)
|
1825
|
+
appendBits(assignVal, 8, bb);
|
1826
|
+
else if (assignVal < 1 << 14) {
|
1827
|
+
appendBits(2, 2, bb);
|
1828
|
+
appendBits(assignVal, 14, bb);
|
1829
|
+
} else if (assignVal < 1e6) {
|
1830
|
+
appendBits(6, 3, bb);
|
1831
|
+
appendBits(assignVal, 21, bb);
|
1832
|
+
} else
|
1833
|
+
throw new RangeError("ECI assignment value out of range");
|
1834
|
+
return new _QrSegment2(_QrSegment2.Mode.ECI, 0, bb);
|
1835
|
+
}
|
1836
|
+
// Tests whether the given string can be encoded as a segment in numeric mode.
|
1837
|
+
// A string is encodable iff each character is in the range 0 to 9.
|
1838
|
+
static isNumeric(text) {
|
1839
|
+
return _QrSegment2.NUMERIC_REGEX.test(text);
|
1840
|
+
}
|
1841
|
+
// Tests whether the given string can be encoded as a segment in alphanumeric mode.
|
1842
|
+
// A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
1843
|
+
// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
1844
|
+
static isAlphanumeric(text) {
|
1845
|
+
return _QrSegment2.ALPHANUMERIC_REGEX.test(text);
|
1846
|
+
}
|
1847
|
+
/*-- Methods --*/
|
1848
|
+
// Returns a new copy of the data bits of this segment.
|
1849
|
+
getData() {
|
1850
|
+
return this.bitData.slice();
|
1851
|
+
}
|
1852
|
+
// (Package-private) Calculates and returns the number of bits needed to encode the given segments at
|
1853
|
+
// the given version. The result is infinity if a segment has too many characters to fit its length field.
|
1854
|
+
static getTotalBits(segs, version) {
|
1855
|
+
let result = 0;
|
1856
|
+
for (const seg of segs) {
|
1857
|
+
const ccbits = seg.mode.numCharCountBits(version);
|
1858
|
+
if (seg.numChars >= 1 << ccbits)
|
1859
|
+
return Infinity;
|
1860
|
+
result += 4 + ccbits + seg.bitData.length;
|
1861
|
+
}
|
1862
|
+
return result;
|
1863
|
+
}
|
1864
|
+
// Returns a new array of bytes representing the given string encoded in UTF-8.
|
1865
|
+
static toUtf8ByteArray(str) {
|
1866
|
+
str = encodeURI(str);
|
1867
|
+
let result = [];
|
1868
|
+
for (let i = 0; i < str.length; i++) {
|
1869
|
+
if (str.charAt(i) != "%")
|
1870
|
+
result.push(str.charCodeAt(i));
|
1871
|
+
else {
|
1872
|
+
result.push(parseInt(str.substring(i + 1, i + 3), 16));
|
1873
|
+
i += 2;
|
1176
1874
|
}
|
1177
1875
|
}
|
1876
|
+
return result;
|
1178
1877
|
}
|
1878
|
+
};
|
1879
|
+
_QrSegment.NUMERIC_REGEX = /^[0-9]*$/;
|
1880
|
+
_QrSegment.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/;
|
1881
|
+
_QrSegment.ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
1882
|
+
let QrSegment = _QrSegment;
|
1883
|
+
qrcodegen2.QrSegment = _QrSegment;
|
1884
|
+
})(qrcodegen || (qrcodegen = {}));
|
1885
|
+
((qrcodegen2) => {
|
1886
|
+
((QrCode2) => {
|
1887
|
+
const _Ecc = class _Ecc {
|
1888
|
+
// The QR Code can tolerate about 30% erroneous codewords
|
1889
|
+
/*-- Constructor and fields --*/
|
1890
|
+
constructor(ordinal, formatBits) {
|
1891
|
+
this.ordinal = ordinal;
|
1892
|
+
this.formatBits = formatBits;
|
1893
|
+
}
|
1894
|
+
};
|
1895
|
+
_Ecc.LOW = new _Ecc(0, 1);
|
1896
|
+
_Ecc.MEDIUM = new _Ecc(1, 0);
|
1897
|
+
_Ecc.QUARTILE = new _Ecc(2, 3);
|
1898
|
+
_Ecc.HIGH = new _Ecc(3, 2);
|
1899
|
+
QrCode2.Ecc = _Ecc;
|
1900
|
+
})(qrcodegen2.QrCode || (qrcodegen2.QrCode = {}));
|
1901
|
+
})(qrcodegen || (qrcodegen = {}));
|
1902
|
+
((qrcodegen2) => {
|
1903
|
+
((QrSegment2) => {
|
1904
|
+
const _Mode = class _Mode {
|
1905
|
+
/*-- Constructor and fields --*/
|
1906
|
+
constructor(modeBits, numBitsCharCount) {
|
1907
|
+
this.modeBits = modeBits;
|
1908
|
+
this.numBitsCharCount = numBitsCharCount;
|
1909
|
+
}
|
1910
|
+
/*-- Method --*/
|
1911
|
+
// (Package-private) Returns the bit width of the character count field for a segment in
|
1912
|
+
// this mode in a QR Code at the given version number. The result is in the range [0, 16].
|
1913
|
+
numCharCountBits(ver) {
|
1914
|
+
return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
|
1915
|
+
}
|
1916
|
+
};
|
1917
|
+
_Mode.NUMERIC = new _Mode(1, [10, 12, 14]);
|
1918
|
+
_Mode.ALPHANUMERIC = new _Mode(2, [9, 11, 13]);
|
1919
|
+
_Mode.BYTE = new _Mode(4, [8, 16, 16]);
|
1920
|
+
_Mode.KANJI = new _Mode(8, [8, 10, 12]);
|
1921
|
+
_Mode.ECI = new _Mode(7, [0, 0, 0]);
|
1922
|
+
QrSegment2.Mode = _Mode;
|
1923
|
+
})(qrcodegen2.QrSegment || (qrcodegen2.QrSegment = {}));
|
1924
|
+
})(qrcodegen || (qrcodegen = {}));
|
1925
|
+
var qrcodegen_default = qrcodegen;
|
1926
|
+
/**
|
1927
|
+
* @license qrcode.react
|
1928
|
+
* Copyright (c) Paul O'Shannessy
|
1929
|
+
* SPDX-License-Identifier: ISC
|
1930
|
+
*/
|
1931
|
+
var ERROR_LEVEL_MAP = {
|
1932
|
+
L: qrcodegen_default.QrCode.Ecc.LOW,
|
1933
|
+
M: qrcodegen_default.QrCode.Ecc.MEDIUM,
|
1934
|
+
Q: qrcodegen_default.QrCode.Ecc.QUARTILE,
|
1935
|
+
H: qrcodegen_default.QrCode.Ecc.HIGH
|
1936
|
+
};
|
1937
|
+
var DEFAULT_SIZE = 128;
|
1938
|
+
var DEFAULT_LEVEL = "L";
|
1939
|
+
var DEFAULT_BGCOLOR = "#FFFFFF";
|
1940
|
+
var DEFAULT_FGCOLOR = "#000000";
|
1941
|
+
var DEFAULT_INCLUDEMARGIN = false;
|
1942
|
+
var DEFAULT_MINVERSION = 1;
|
1943
|
+
var SPEC_MARGIN_SIZE = 4;
|
1944
|
+
var DEFAULT_MARGIN_SIZE = 0;
|
1945
|
+
var DEFAULT_IMG_SCALE = 0.1;
|
1946
|
+
function generatePath(modules, margin = 0) {
|
1947
|
+
const ops = [];
|
1948
|
+
modules.forEach(function(row, y) {
|
1949
|
+
let start = null;
|
1950
|
+
row.forEach(function(cell, x) {
|
1951
|
+
if (!cell && start !== null) {
|
1952
|
+
ops.push(
|
1953
|
+
`M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`
|
1954
|
+
);
|
1955
|
+
start = null;
|
1956
|
+
return;
|
1957
|
+
}
|
1958
|
+
if (x === row.length - 1) {
|
1959
|
+
if (!cell) {
|
1960
|
+
return;
|
1961
|
+
}
|
1962
|
+
if (start === null) {
|
1963
|
+
ops.push(`M${x + margin},${y + margin} h1v1H${x + margin}z`);
|
1964
|
+
} else {
|
1965
|
+
ops.push(
|
1966
|
+
`M${start + margin},${y + margin} h${x + 1 - start}v1H${start + margin}z`
|
1967
|
+
);
|
1968
|
+
}
|
1969
|
+
return;
|
1970
|
+
}
|
1971
|
+
if (cell && start === null) {
|
1972
|
+
start = x;
|
1973
|
+
}
|
1974
|
+
});
|
1975
|
+
});
|
1976
|
+
return ops.join("");
|
1977
|
+
}
|
1978
|
+
function excavateModules(modules, excavation) {
|
1979
|
+
return modules.slice().map((row, y) => {
|
1980
|
+
if (y < excavation.y || y >= excavation.y + excavation.h) {
|
1981
|
+
return row;
|
1982
|
+
}
|
1983
|
+
return row.map((cell, x) => {
|
1984
|
+
if (x < excavation.x || x >= excavation.x + excavation.w) {
|
1985
|
+
return cell;
|
1986
|
+
}
|
1987
|
+
return false;
|
1988
|
+
});
|
1989
|
+
});
|
1990
|
+
}
|
1991
|
+
function getImageSettings(cells, size, margin, imageSettings) {
|
1992
|
+
if (imageSettings == null) {
|
1993
|
+
return null;
|
1994
|
+
}
|
1995
|
+
const numCells = cells.length + margin * 2;
|
1996
|
+
const defaultSize = Math.floor(size * DEFAULT_IMG_SCALE);
|
1997
|
+
const scale = numCells / size;
|
1998
|
+
const w = (imageSettings.width || defaultSize) * scale;
|
1999
|
+
const h = (imageSettings.height || defaultSize) * scale;
|
2000
|
+
const x = imageSettings.x == null ? cells.length / 2 - w / 2 : imageSettings.x * scale;
|
2001
|
+
const y = imageSettings.y == null ? cells.length / 2 - h / 2 : imageSettings.y * scale;
|
2002
|
+
const opacity = imageSettings.opacity == null ? 1 : imageSettings.opacity;
|
2003
|
+
let excavation = null;
|
2004
|
+
if (imageSettings.excavate) {
|
2005
|
+
let floorX = Math.floor(x);
|
2006
|
+
let floorY = Math.floor(y);
|
2007
|
+
let ceilW = Math.ceil(w + x - floorX);
|
2008
|
+
let ceilH = Math.ceil(h + y - floorY);
|
2009
|
+
excavation = { x: floorX, y: floorY, w: ceilW, h: ceilH };
|
2010
|
+
}
|
2011
|
+
const crossOrigin = imageSettings.crossOrigin;
|
2012
|
+
return { x, y, h, w, excavation, opacity, crossOrigin };
|
2013
|
+
}
|
2014
|
+
function getMarginSize(includeMargin, marginSize) {
|
2015
|
+
if (marginSize != null) {
|
2016
|
+
return Math.max(Math.floor(marginSize), 0);
|
2017
|
+
}
|
2018
|
+
return includeMargin ? SPEC_MARGIN_SIZE : DEFAULT_MARGIN_SIZE;
|
2019
|
+
}
|
2020
|
+
function useQRCode({
|
2021
|
+
value,
|
2022
|
+
level,
|
2023
|
+
minVersion,
|
2024
|
+
includeMargin,
|
2025
|
+
marginSize,
|
2026
|
+
imageSettings,
|
2027
|
+
size,
|
2028
|
+
boostLevel
|
2029
|
+
}) {
|
2030
|
+
let qrcode = React.useMemo(() => {
|
2031
|
+
const values = Array.isArray(value) ? value : [value];
|
2032
|
+
const segments = values.reduce((accum, v) => {
|
2033
|
+
accum.push(...qrcodegen_default.QrSegment.makeSegments(v));
|
2034
|
+
return accum;
|
2035
|
+
}, []);
|
2036
|
+
return qrcodegen_default.QrCode.encodeSegments(
|
2037
|
+
segments,
|
2038
|
+
ERROR_LEVEL_MAP[level],
|
2039
|
+
minVersion,
|
2040
|
+
void 0,
|
2041
|
+
void 0,
|
2042
|
+
boostLevel
|
2043
|
+
);
|
2044
|
+
}, [value, level, minVersion, boostLevel]);
|
2045
|
+
const { cells, margin, numCells, calculatedImageSettings } = React.useMemo(() => {
|
2046
|
+
let cells2 = qrcode.getModules();
|
2047
|
+
const margin2 = getMarginSize(includeMargin, marginSize);
|
2048
|
+
const numCells2 = cells2.length + margin2 * 2;
|
2049
|
+
const calculatedImageSettings2 = getImageSettings(
|
2050
|
+
cells2,
|
2051
|
+
size,
|
2052
|
+
margin2,
|
2053
|
+
imageSettings
|
2054
|
+
);
|
2055
|
+
return {
|
2056
|
+
cells: cells2,
|
2057
|
+
margin: margin2,
|
2058
|
+
numCells: numCells2,
|
2059
|
+
calculatedImageSettings: calculatedImageSettings2
|
2060
|
+
};
|
2061
|
+
}, [qrcode, size, imageSettings, includeMargin, marginSize]);
|
2062
|
+
return {
|
2063
|
+
qrcode,
|
2064
|
+
margin,
|
2065
|
+
cells,
|
2066
|
+
numCells,
|
2067
|
+
calculatedImageSettings
|
2068
|
+
};
|
2069
|
+
}
|
2070
|
+
var SUPPORTS_PATH2D = function() {
|
2071
|
+
try {
|
2072
|
+
new Path2D().addPath(new Path2D());
|
2073
|
+
} catch (e) {
|
2074
|
+
return false;
|
1179
2075
|
}
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
2076
|
+
return true;
|
2077
|
+
}();
|
2078
|
+
var QRCodeCanvas = React.forwardRef(
|
2079
|
+
function QRCodeCanvas2(props, forwardedRef) {
|
2080
|
+
const _a = props, {
|
2081
|
+
value,
|
2082
|
+
size = DEFAULT_SIZE,
|
2083
|
+
level = DEFAULT_LEVEL,
|
2084
|
+
bgColor = DEFAULT_BGCOLOR,
|
2085
|
+
fgColor = DEFAULT_FGCOLOR,
|
2086
|
+
includeMargin = DEFAULT_INCLUDEMARGIN,
|
2087
|
+
minVersion = DEFAULT_MINVERSION,
|
2088
|
+
boostLevel,
|
2089
|
+
marginSize,
|
2090
|
+
imageSettings
|
2091
|
+
} = _a, extraProps = __objRest(_a, [
|
2092
|
+
"value",
|
2093
|
+
"size",
|
2094
|
+
"level",
|
2095
|
+
"bgColor",
|
2096
|
+
"fgColor",
|
2097
|
+
"includeMargin",
|
2098
|
+
"minVersion",
|
2099
|
+
"boostLevel",
|
2100
|
+
"marginSize",
|
2101
|
+
"imageSettings"
|
2102
|
+
]);
|
2103
|
+
const _b = extraProps, { style } = _b, otherProps = __objRest(_b, ["style"]);
|
2104
|
+
const imgSrc = imageSettings == null ? void 0 : imageSettings.src;
|
2105
|
+
const _canvas = React.useRef(null);
|
2106
|
+
const _image = React.useRef(null);
|
2107
|
+
const setCanvasRef = React.useCallback(
|
2108
|
+
(node) => {
|
2109
|
+
_canvas.current = node;
|
2110
|
+
if (typeof forwardedRef === "function") {
|
2111
|
+
forwardedRef(node);
|
2112
|
+
} else if (forwardedRef) {
|
2113
|
+
forwardedRef.current = node;
|
1186
2114
|
}
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
2115
|
+
},
|
2116
|
+
[forwardedRef]
|
2117
|
+
);
|
2118
|
+
const [isImgLoaded, setIsImageLoaded] = React.useState(false);
|
2119
|
+
const { margin, cells, numCells, calculatedImageSettings } = useQRCode({
|
2120
|
+
value,
|
2121
|
+
level,
|
2122
|
+
minVersion,
|
2123
|
+
boostLevel,
|
2124
|
+
includeMargin,
|
2125
|
+
marginSize,
|
2126
|
+
imageSettings,
|
2127
|
+
size
|
2128
|
+
});
|
2129
|
+
React.useEffect(() => {
|
2130
|
+
if (_canvas.current != null) {
|
2131
|
+
const canvas = _canvas.current;
|
2132
|
+
const ctx = canvas.getContext("2d");
|
2133
|
+
if (!ctx) {
|
2134
|
+
return;
|
2135
|
+
}
|
2136
|
+
let cellsToDraw = cells;
|
2137
|
+
const image = _image.current;
|
2138
|
+
const haveImageToRender = calculatedImageSettings != null && image !== null && image.complete && image.naturalHeight !== 0 && image.naturalWidth !== 0;
|
2139
|
+
if (haveImageToRender) {
|
2140
|
+
if (calculatedImageSettings.excavation != null) {
|
2141
|
+
cellsToDraw = excavateModules(
|
2142
|
+
cells,
|
2143
|
+
calculatedImageSettings.excavation
|
2144
|
+
);
|
2145
|
+
}
|
2146
|
+
}
|
2147
|
+
const pixelRatio = window.devicePixelRatio || 1;
|
2148
|
+
canvas.height = canvas.width = size * pixelRatio;
|
2149
|
+
const scale = size / numCells * pixelRatio;
|
2150
|
+
ctx.scale(scale, scale);
|
2151
|
+
ctx.fillStyle = bgColor;
|
2152
|
+
ctx.fillRect(0, 0, numCells, numCells);
|
2153
|
+
ctx.fillStyle = fgColor;
|
2154
|
+
if (SUPPORTS_PATH2D) {
|
2155
|
+
ctx.fill(new Path2D(generatePath(cellsToDraw, margin)));
|
2156
|
+
} else {
|
2157
|
+
cells.forEach(function(row, rdx) {
|
2158
|
+
row.forEach(function(cell, cdx) {
|
2159
|
+
if (cell) {
|
2160
|
+
ctx.fillRect(cdx + margin, rdx + margin, 1, 1);
|
2161
|
+
}
|
2162
|
+
});
|
2163
|
+
});
|
2164
|
+
}
|
2165
|
+
if (calculatedImageSettings) {
|
2166
|
+
ctx.globalAlpha = calculatedImageSettings.opacity;
|
2167
|
+
}
|
2168
|
+
if (haveImageToRender) {
|
2169
|
+
ctx.drawImage(
|
2170
|
+
image,
|
2171
|
+
calculatedImageSettings.x + margin,
|
2172
|
+
calculatedImageSettings.y + margin,
|
2173
|
+
calculatedImageSettings.w,
|
2174
|
+
calculatedImageSettings.h
|
2175
|
+
);
|
2176
|
+
}
|
2177
|
+
}
|
2178
|
+
});
|
2179
|
+
React.useEffect(() => {
|
2180
|
+
setIsImageLoaded(false);
|
2181
|
+
}, [imgSrc]);
|
2182
|
+
const canvasStyle = __spreadValues({ height: size, width: size }, style);
|
2183
|
+
let img = null;
|
2184
|
+
if (imgSrc != null) {
|
2185
|
+
img = /* @__PURE__ */ React.createElement(
|
2186
|
+
"img",
|
2187
|
+
{
|
2188
|
+
src: imgSrc,
|
2189
|
+
key: imgSrc,
|
2190
|
+
style: { display: "none" },
|
2191
|
+
onLoad: () => {
|
2192
|
+
setIsImageLoaded(true);
|
2193
|
+
},
|
2194
|
+
ref: _image,
|
2195
|
+
crossOrigin: calculatedImageSettings == null ? void 0 : calculatedImageSettings.crossOrigin
|
2196
|
+
}
|
2197
|
+
);
|
2198
|
+
}
|
2199
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(
|
2200
|
+
"canvas",
|
2201
|
+
__spreadValues({
|
2202
|
+
style: canvasStyle,
|
2203
|
+
height: size,
|
2204
|
+
width: size,
|
2205
|
+
ref: setCanvasRef,
|
2206
|
+
role: "img"
|
2207
|
+
}, otherProps)
|
2208
|
+
), img);
|
2209
|
+
}
|
2210
|
+
);
|
2211
|
+
QRCodeCanvas.displayName = "QRCodeCanvas";
|
2212
|
+
var QRCodeSVG = React.forwardRef(
|
2213
|
+
function QRCodeSVG2(props, forwardedRef) {
|
2214
|
+
const _a = props, {
|
2215
|
+
value,
|
2216
|
+
size = DEFAULT_SIZE,
|
2217
|
+
level = DEFAULT_LEVEL,
|
2218
|
+
bgColor = DEFAULT_BGCOLOR,
|
2219
|
+
fgColor = DEFAULT_FGCOLOR,
|
2220
|
+
includeMargin = DEFAULT_INCLUDEMARGIN,
|
2221
|
+
minVersion = DEFAULT_MINVERSION,
|
2222
|
+
boostLevel,
|
2223
|
+
title,
|
2224
|
+
marginSize,
|
2225
|
+
imageSettings
|
2226
|
+
} = _a, otherProps = __objRest(_a, [
|
2227
|
+
"value",
|
2228
|
+
"size",
|
2229
|
+
"level",
|
2230
|
+
"bgColor",
|
2231
|
+
"fgColor",
|
2232
|
+
"includeMargin",
|
2233
|
+
"minVersion",
|
2234
|
+
"boostLevel",
|
2235
|
+
"title",
|
2236
|
+
"marginSize",
|
2237
|
+
"imageSettings"
|
2238
|
+
]);
|
2239
|
+
const { margin, cells, numCells, calculatedImageSettings } = useQRCode({
|
2240
|
+
value,
|
2241
|
+
level,
|
2242
|
+
minVersion,
|
2243
|
+
boostLevel,
|
2244
|
+
includeMargin,
|
2245
|
+
marginSize,
|
2246
|
+
imageSettings,
|
2247
|
+
size
|
2248
|
+
});
|
2249
|
+
let cellsToDraw = cells;
|
2250
|
+
let image = null;
|
2251
|
+
if (imageSettings != null && calculatedImageSettings != null) {
|
2252
|
+
if (calculatedImageSettings.excavation != null) {
|
2253
|
+
cellsToDraw = excavateModules(
|
2254
|
+
cells,
|
2255
|
+
calculatedImageSettings.excavation
|
2256
|
+
);
|
1190
2257
|
}
|
2258
|
+
image = /* @__PURE__ */ React.createElement(
|
2259
|
+
"image",
|
2260
|
+
{
|
2261
|
+
href: imageSettings.src,
|
2262
|
+
height: calculatedImageSettings.h,
|
2263
|
+
width: calculatedImageSettings.w,
|
2264
|
+
x: calculatedImageSettings.x + margin,
|
2265
|
+
y: calculatedImageSettings.y + margin,
|
2266
|
+
preserveAspectRatio: "none",
|
2267
|
+
opacity: calculatedImageSettings.opacity,
|
2268
|
+
crossOrigin: calculatedImageSettings.crossOrigin
|
2269
|
+
}
|
2270
|
+
);
|
1191
2271
|
}
|
2272
|
+
const fgPath = generatePath(cellsToDraw, margin);
|
2273
|
+
return /* @__PURE__ */ React.createElement(
|
2274
|
+
"svg",
|
2275
|
+
__spreadValues({
|
2276
|
+
height: size,
|
2277
|
+
width: size,
|
2278
|
+
viewBox: `0 0 ${numCells} ${numCells}`,
|
2279
|
+
ref: forwardedRef,
|
2280
|
+
role: "img"
|
2281
|
+
}, otherProps),
|
2282
|
+
!!title && /* @__PURE__ */ React.createElement("title", null, title),
|
2283
|
+
/* @__PURE__ */ React.createElement(
|
2284
|
+
"path",
|
2285
|
+
{
|
2286
|
+
fill: bgColor,
|
2287
|
+
d: `M0,0 h${numCells}v${numCells}H0z`,
|
2288
|
+
shapeRendering: "crispEdges"
|
2289
|
+
}
|
2290
|
+
),
|
2291
|
+
/* @__PURE__ */ React.createElement("path", { fill: fgColor, d: fgPath, shapeRendering: "crispEdges" }),
|
2292
|
+
image
|
2293
|
+
);
|
1192
2294
|
}
|
2295
|
+
);
|
2296
|
+
QRCodeSVG.displayName = "QRCodeSVG";
|
2297
|
+
const QRCode = ({
|
2298
|
+
walletAddress,
|
2299
|
+
amount,
|
2300
|
+
currency,
|
2301
|
+
theme = "light",
|
2302
|
+
size = 200
|
2303
|
+
}) => {
|
2304
|
+
const [qrData, setQrData] = useState("");
|
2305
|
+
const [qrError, setQrError] = useState(null);
|
2306
|
+
useEffect(() => {
|
2307
|
+
if (!walletAddress) {
|
2308
|
+
setQrError("No wallet address provided");
|
2309
|
+
return;
|
2310
|
+
}
|
2311
|
+
try {
|
2312
|
+
let paymentData;
|
2313
|
+
if (currency === "SOL" || currency === "USDC_SOL") {
|
2314
|
+
paymentData = `solana:${walletAddress}?amount=${amount}&spl-token=USDC`;
|
2315
|
+
} else if (currency === "USDT" || currency === "USDC") {
|
2316
|
+
const tokenAddress = getTokenAddressForCurrency(currency);
|
2317
|
+
paymentData = `ethereum:${tokenAddress}/transfer?address=${walletAddress}&uint256=${convertToSmallestUnit(amount, currency)}`;
|
2318
|
+
} else {
|
2319
|
+
paymentData = `ethereum:${walletAddress}@1?value=${convertToWei(amount)}`;
|
2320
|
+
}
|
2321
|
+
setQrData(paymentData);
|
2322
|
+
setQrError(null);
|
2323
|
+
} catch (err) {
|
2324
|
+
console.error("Error generating QR code:", err);
|
2325
|
+
setQrError("Error generating payment QR code");
|
2326
|
+
}
|
2327
|
+
}, [walletAddress, amount, currency]);
|
2328
|
+
const getTokenAddressForCurrency = (currency2) => {
|
2329
|
+
const tokenAddresses = {
|
2330
|
+
"USDT": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
2331
|
+
"USDC": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
2332
|
+
"BNB": "0xB8c77482e45F1F44dE1745F52C74426C631bDD52"
|
2333
|
+
};
|
2334
|
+
return tokenAddresses[currency2] || "";
|
2335
|
+
};
|
2336
|
+
const convertToSmallestUnit = (amount2, currency2) => {
|
2337
|
+
const decimals = currency2 === "USDT" || currency2 === "USDC" ? 6 : 18;
|
2338
|
+
return Math.floor(parseFloat(amount2) * Math.pow(10, decimals)).toString();
|
2339
|
+
};
|
2340
|
+
const convertToWei = (amount2) => {
|
2341
|
+
return Math.floor(parseFloat(amount2) * 1e18).toString();
|
2342
|
+
};
|
1193
2343
|
return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex flex-col items-center", children: [
|
1194
|
-
/* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "p-4 rounded-lg bg-
|
2344
|
+
qrError ? /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "p-4 rounded-lg bg-red-100 mb-3 text-red-700", children: qrError }) : /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "p-4 rounded-lg bg-white mb-3", children: qrData && /* @__PURE__ */ jsxRuntimeExports.jsx(
|
2345
|
+
QRCodeSVG,
|
2346
|
+
{
|
2347
|
+
value: qrData,
|
2348
|
+
size,
|
2349
|
+
bgColor: theme === "dark" ? "#374151" : "#FFFFFF",
|
2350
|
+
fgColor: theme === "dark" ? "#FFFFFF" : "#000000",
|
2351
|
+
level: "H",
|
2352
|
+
includeMargin: true
|
2353
|
+
}
|
2354
|
+
) }),
|
1195
2355
|
/* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-center text-sm text-gray-700", children: "Scan with your wallet app to pay" }),
|
1196
2356
|
/* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mt-3 w-full", children: [
|
1197
2357
|
/* @__PURE__ */ jsxRuntimeExports.jsxs("p", { className: "text-xs text-gray-500 mb-1", children: [
|
@@ -1206,7 +2366,7 @@ const QRCode = ({
|
|
1206
2366
|
/* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "mt-4 w-full", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "p-3 rounded bg-gray-100", children: [
|
1207
2367
|
/* @__PURE__ */ jsxRuntimeExports.jsx("h4", { className: "text-sm font-medium mb-2 text-gray-800", children: "Payment Instructions" }),
|
1208
2368
|
/* @__PURE__ */ jsxRuntimeExports.jsxs("ol", { className: "text-xs space-y-2 text-gray-600", children: [
|
1209
|
-
/* @__PURE__ */ jsxRuntimeExports.jsx("li", { children: "1. Open your crypto wallet app" }),
|
2369
|
+
/* @__PURE__ */ jsxRuntimeExports.jsx("li", { children: "1. Open your crypto wallet app (MetaMask, Trust Wallet, etc.)" }),
|
1210
2370
|
/* @__PURE__ */ jsxRuntimeExports.jsx("li", { children: "2. Scan the QR code above" }),
|
1211
2371
|
/* @__PURE__ */ jsxRuntimeExports.jsxs("li", { children: [
|
1212
2372
|
"3. Confirm the payment amount (",
|
@@ -1643,13 +2803,19 @@ const CoinleyCheckout = forwardRef(({
|
|
1643
2803
|
apiUrl,
|
1644
2804
|
customerEmail,
|
1645
2805
|
merchantName = "Merchant",
|
2806
|
+
merchantWalletAddress,
|
2807
|
+
// New prop for direct wallet address passing
|
2808
|
+
merchantSolWalletAddress,
|
2809
|
+
// New prop for direct Solana wallet address passing
|
1646
2810
|
onSuccess,
|
1647
2811
|
onError,
|
1648
2812
|
onClose,
|
1649
2813
|
theme,
|
1650
2814
|
autoOpen = false,
|
1651
2815
|
debug = false,
|
1652
|
-
testMode = false
|
2816
|
+
testMode = false,
|
2817
|
+
preferredNetwork = "ethereum",
|
2818
|
+
preferredWallet = "metamask"
|
1653
2819
|
}, ref) => {
|
1654
2820
|
const coinleyContext = useCoinley();
|
1655
2821
|
const { theme: contextTheme } = useTheme();
|
@@ -1660,11 +2826,17 @@ const CoinleyCheckout = forwardRef(({
|
|
1660
2826
|
const [error, setError] = useState(null);
|
1661
2827
|
const [transactionHash, setTransactionHash] = useState(null);
|
1662
2828
|
const [walletConnected, setWalletConnected] = useState(false);
|
2829
|
+
const [walletAddresses, setWalletAddresses] = useState({
|
2830
|
+
ethereum: null,
|
2831
|
+
solana: null
|
2832
|
+
});
|
1663
2833
|
apiKey || (coinleyContext == null ? void 0 : coinleyContext.apiKey);
|
1664
2834
|
apiSecret || (coinleyContext == null ? void 0 : coinleyContext.apiSecret);
|
1665
2835
|
apiUrl || (coinleyContext == null ? void 0 : coinleyContext.apiUrl);
|
1666
2836
|
const effectiveTheme = theme || contextTheme;
|
1667
2837
|
const effectiveDebug = debug || (coinleyContext == null ? void 0 : coinleyContext.debug);
|
2838
|
+
const effectiveWalletAddress = merchantWalletAddress || (coinleyContext == null ? void 0 : coinleyContext.merchantWalletAddress) || walletAddresses.ethereum;
|
2839
|
+
const effectiveSolWalletAddress = merchantSolWalletAddress || (coinleyContext == null ? void 0 : coinleyContext.merchantSolWalletAddress) || walletAddresses.solana;
|
1668
2840
|
useImperativeHandle(ref, () => ({
|
1669
2841
|
open: (paymentDetails) => {
|
1670
2842
|
handleOpen(paymentDetails);
|
@@ -1695,8 +2867,24 @@ const CoinleyCheckout = forwardRef(({
|
|
1695
2867
|
}
|
1696
2868
|
};
|
1697
2869
|
checkWalletConnection();
|
2870
|
+
if (!effectiveWalletAddress || !effectiveSolWalletAddress) {
|
2871
|
+
fetchMerchantWalletAddresses();
|
2872
|
+
}
|
1698
2873
|
}
|
1699
2874
|
}, [effectiveDebug]);
|
2875
|
+
const fetchMerchantWalletAddresses = async () => {
|
2876
|
+
try {
|
2877
|
+
log("Fetching merchant wallet addresses...");
|
2878
|
+
const addresses = await getMerchantWalletAddresses();
|
2879
|
+
log("Received merchant wallet addresses:", addresses);
|
2880
|
+
setWalletAddresses({
|
2881
|
+
ethereum: addresses.walletAddress,
|
2882
|
+
solana: addresses.solWalletAddress
|
2883
|
+
});
|
2884
|
+
} catch (err) {
|
2885
|
+
log("Error fetching merchant wallet addresses:", err);
|
2886
|
+
}
|
2887
|
+
};
|
1700
2888
|
useEffect(() => {
|
1701
2889
|
if (typeof window !== "undefined" && window.ethereum) {
|
1702
2890
|
const handleAccountsChanged = (accounts) => {
|
@@ -1726,14 +2914,25 @@ const CoinleyCheckout = forwardRef(({
|
|
1726
2914
|
currency: paymentDetails.currency || "USDT",
|
1727
2915
|
customerEmail: paymentDetails.customerEmail || customerEmail,
|
1728
2916
|
callbackUrl: paymentDetails.callbackUrl,
|
1729
|
-
metadata: paymentDetails.metadata || {}
|
2917
|
+
metadata: paymentDetails.metadata || {},
|
2918
|
+
// Add wallet addresses if available
|
2919
|
+
walletAddress: effectiveWalletAddress,
|
2920
|
+
solWalletAddress: effectiveSolWalletAddress
|
1730
2921
|
});
|
1731
2922
|
log("Payment created:", paymentResponse);
|
1732
|
-
|
2923
|
+
const enhancedPayment = {
|
2924
|
+
...paymentResponse.payment,
|
2925
|
+
walletAddress: paymentResponse.payment.walletAddress || effectiveWalletAddress,
|
2926
|
+
solWalletAddress: paymentResponse.payment.solWalletAddress || effectiveSolWalletAddress
|
2927
|
+
};
|
2928
|
+
setPayment(enhancedPayment);
|
1733
2929
|
setPaymentStatus("idle");
|
1734
2930
|
setError(null);
|
2931
|
+
if (preferredNetwork === "solana") {
|
2932
|
+
setSelectedCurrency("SOL");
|
2933
|
+
}
|
1735
2934
|
log("Payment created and state updated:", {
|
1736
|
-
payment:
|
2935
|
+
payment: enhancedPayment,
|
1737
2936
|
status: "idle"
|
1738
2937
|
});
|
1739
2938
|
} catch (err) {
|
@@ -1778,7 +2977,13 @@ const CoinleyCheckout = forwardRef(({
|
|
1778
2977
|
setPaymentStatus("loading");
|
1779
2978
|
setTransactionHash(null);
|
1780
2979
|
try {
|
1781
|
-
log("Processing payment:", {
|
2980
|
+
log("Processing payment:", {
|
2981
|
+
paymentId: payment.id,
|
2982
|
+
currency: selectedCurrency,
|
2983
|
+
isQrCodeMode,
|
2984
|
+
walletAddress: payment.walletAddress,
|
2985
|
+
solWalletAddress: payment.solWalletAddress
|
2986
|
+
});
|
1782
2987
|
let txHash;
|
1783
2988
|
if (testMode) {
|
1784
2989
|
log("Test mode: Generating mock transaction...");
|
@@ -1793,7 +2998,8 @@ const CoinleyCheckout = forwardRef(({
|
|
1793
2998
|
throw new Error("Please connect your wallet to proceed with payment");
|
1794
2999
|
}
|
1795
3000
|
}
|
1796
|
-
const
|
3001
|
+
const isSolana = selectedCurrency === "SOL" || selectedCurrency === "USDC_SOL";
|
3002
|
+
const merchantAddress = isSolana ? payment.solWalletAddress || effectiveSolWalletAddress || "3GfmovLct5PFL9TRbzGMXvrBGHRkQBdFzG3haEv44LYx" : payment.walletAddress || effectiveWalletAddress || "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
|
1797
3003
|
const txParams = {
|
1798
3004
|
from: await window.ethereum.request({ method: "eth_requestAccounts" }).then((accounts) => accounts[0]),
|
1799
3005
|
to: merchantAddress,
|
@@ -1848,7 +3054,9 @@ const CoinleyCheckout = forwardRef(({
|
|
1848
3054
|
transactionHash,
|
1849
3055
|
walletConnected,
|
1850
3056
|
onConnectWallet: connectToWallet,
|
1851
|
-
testMode
|
3057
|
+
testMode,
|
3058
|
+
preferredNetwork,
|
3059
|
+
preferredWallet
|
1852
3060
|
}
|
1853
3061
|
) });
|
1854
3062
|
});
|
@@ -1859,7 +3067,7 @@ const DEFAULT_CONFIG = {
|
|
1859
3067
|
testMode: false,
|
1860
3068
|
theme: "light"
|
1861
3069
|
};
|
1862
|
-
const VERSION = "0.2.
|
3070
|
+
const VERSION = "0.2.5";
|
1863
3071
|
export {
|
1864
3072
|
CoinleyCheckout,
|
1865
3073
|
CoinleyModal,
|