@pezkuwi/react-qr 3.16.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/README.md +3 -0
- package/build/Display.d.ts +12 -0
- package/build/Display.js +91 -0
- package/build/DisplayAddress.d.ts +12 -0
- package/build/DisplayAddress.js +12 -0
- package/build/DisplayPayload.d.ts +15 -0
- package/build/DisplayPayload.js +12 -0
- package/build/LICENSE +201 -0
- package/build/NetworkSpecs.d.ts +11 -0
- package/build/NetworkSpecs.js +12 -0
- package/build/README.md +3 -0
- package/build/Scan.d.ts +12 -0
- package/build/Scan.js +53 -0
- package/build/ScanAddress.d.ts +19 -0
- package/build/ScanAddress.js +41 -0
- package/build/ScanSignature.d.ts +15 -0
- package/build/ScanSignature.js +8 -0
- package/build/bundle-pezkuwi-react-qr.js +26227 -0
- package/build/bundle.d.ts +6 -0
- package/build/bundle.js +6 -0
- package/build/cjs/Display.d.ts +12 -0
- package/build/cjs/Display.js +95 -0
- package/build/cjs/DisplayAddress.d.ts +12 -0
- package/build/cjs/DisplayAddress.js +16 -0
- package/build/cjs/DisplayPayload.d.ts +15 -0
- package/build/cjs/DisplayPayload.js +16 -0
- package/build/cjs/NetworkSpecs.d.ts +11 -0
- package/build/cjs/NetworkSpecs.js +16 -0
- package/build/cjs/Scan.d.ts +12 -0
- package/build/cjs/Scan.js +57 -0
- package/build/cjs/ScanAddress.d.ts +19 -0
- package/build/cjs/ScanAddress.js +45 -0
- package/build/cjs/ScanSignature.d.ts +15 -0
- package/build/cjs/ScanSignature.js +12 -0
- package/build/cjs/bundle.d.ts +6 -0
- package/build/cjs/bundle.js +15 -0
- package/build/cjs/constants.d.ts +9 -0
- package/build/cjs/constants.js +12 -0
- package/build/cjs/index.d.ts +2 -0
- package/build/cjs/index.js +5 -0
- package/build/cjs/package.json +3 -0
- package/build/cjs/packageDetect.d.ts +1 -0
- package/build/cjs/packageDetect.js +5 -0
- package/build/cjs/packageInfo.d.ts +6 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/qrcode.d.ts +3 -0
- package/build/cjs/qrcode.js +8 -0
- package/build/cjs/styled.d.ts +1 -0
- package/build/cjs/styled.js +5 -0
- package/build/cjs/util.d.ts +7 -0
- package/build/cjs/util.js +59 -0
- package/build/constants.d.ts +9 -0
- package/build/constants.js +9 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +2 -0
- package/build/package.json +274 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageDetect.js +3 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +1 -0
- package/build/qrcode.d.ts +3 -0
- package/build/qrcode.js +4 -0
- package/build/styled.d.ts +1 -0
- package/build/styled.js +1 -0
- package/build/util.d.ts +7 -0
- package/build/util.js +50 -0
- package/build-tsc/Display.d.ts +12 -0
- package/build-tsc/DisplayAddress.d.ts +12 -0
- package/build-tsc/DisplayPayload.d.ts +15 -0
- package/build-tsc/NetworkSpecs.d.ts +11 -0
- package/build-tsc/Scan.d.ts +12 -0
- package/build-tsc/ScanAddress.d.ts +19 -0
- package/build-tsc/ScanSignature.d.ts +15 -0
- package/build-tsc/bundle.d.ts +6 -0
- package/build-tsc/constants.d.ts +9 -0
- package/build-tsc/index.d.ts +2 -0
- package/build-tsc/packageDetect.d.ts +1 -0
- package/build-tsc/packageInfo.d.ts +6 -0
- package/build-tsc/qrcode.d.ts +3 -0
- package/build-tsc/styled.d.ts +1 -0
- package/build-tsc/util.d.ts +7 -0
- package/build-tsc-cjs/Display.js +95 -0
- package/build-tsc-cjs/DisplayAddress.js +16 -0
- package/build-tsc-cjs/DisplayPayload.js +16 -0
- package/build-tsc-cjs/NetworkSpecs.js +16 -0
- package/build-tsc-cjs/Scan.js +57 -0
- package/build-tsc-cjs/ScanAddress.js +45 -0
- package/build-tsc-cjs/ScanSignature.js +12 -0
- package/build-tsc-cjs/bundle.js +15 -0
- package/build-tsc-cjs/constants.js +12 -0
- package/build-tsc-cjs/index.js +5 -0
- package/build-tsc-cjs/packageDetect.js +5 -0
- package/build-tsc-cjs/packageInfo.js +4 -0
- package/build-tsc-cjs/qrcode.js +8 -0
- package/build-tsc-cjs/styled.js +5 -0
- package/build-tsc-cjs/util.js +59 -0
- package/build-tsc-esm/Display.js +91 -0
- package/build-tsc-esm/DisplayAddress.js +12 -0
- package/build-tsc-esm/DisplayPayload.js +12 -0
- package/build-tsc-esm/NetworkSpecs.js +12 -0
- package/build-tsc-esm/Scan.js +53 -0
- package/build-tsc-esm/ScanAddress.js +41 -0
- package/build-tsc-esm/ScanSignature.js +8 -0
- package/build-tsc-esm/bundle.js +6 -0
- package/build-tsc-esm/constants.js +9 -0
- package/build-tsc-esm/index.js +2 -0
- package/build-tsc-esm/packageDetect.js +3 -0
- package/build-tsc-esm/packageInfo.js +1 -0
- package/build-tsc-esm/qrcode.js +4 -0
- package/build-tsc-esm/styled.js +1 -0
- package/build-tsc-esm/util.js +50 -0
- package/package.json +40 -0
- package/src/Display.tsx +153 -0
- package/src/DisplayAddress.tsx +40 -0
- package/src/DisplayPayload.tsx +42 -0
- package/src/NetworkSpecs.tsx +39 -0
- package/src/Scan.tsx +100 -0
- package/src/ScanAddress.tsx +82 -0
- package/src/ScanSignature.tsx +39 -0
- package/src/bundle.ts +9 -0
- package/src/constants.ts +12 -0
- package/src/index.ts +6 -0
- package/src/packageDetect.ts +11 -0
- package/src/packageInfo.ts +6 -0
- package/src/qrcode.ts +15 -0
- package/src/styled.ts +4 -0
- package/src/util.spec.ts +100 -0
- package/src/util.ts +83 -0
- package/tsconfig.build.json +14 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +16 -0
- package/tsconfig.spec.tsbuildinfo +1 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QrNetworkSpecs = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
7
|
+
const Display_js_1 = require("./Display.js");
|
|
8
|
+
const util_js_1 = require("./util.js");
|
|
9
|
+
function DisplayNetworkSpecs({ className, networkSpecs, size, style }) {
|
|
10
|
+
const data = (0, react_1.useMemo)(() => (0, util_js_1.encodeString)(JSON.stringify(networkSpecs)), [networkSpecs]);
|
|
11
|
+
if (!data) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return ((0, jsx_runtime_1.jsx)(Display_js_1.QrDisplay, { className: className, size: size, skipEncoding: true, style: style, value: data }));
|
|
15
|
+
}
|
|
16
|
+
exports.QrNetworkSpecs = react_1.default.memo(DisplayNetworkSpecs);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QrScan = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const browser_1 = require("@zxing/browser");
|
|
7
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
8
|
+
const styled_js_1 = require("./styled.js");
|
|
9
|
+
const util_js_1 = require("./util.js");
|
|
10
|
+
const DEFAULT_DELAY = 150;
|
|
11
|
+
const DEFAULT_ERROR = (error) => {
|
|
12
|
+
console.error('@pezkuwi/react-qr:Scan', error.message);
|
|
13
|
+
};
|
|
14
|
+
function Scan({ className = '', delay = DEFAULT_DELAY, onError = DEFAULT_ERROR, onScan, size, style = {} }) {
|
|
15
|
+
const videoRef = (0, react_1.useRef)(null);
|
|
16
|
+
const controlsRef = (0, react_1.useRef)(null);
|
|
17
|
+
const containerStyle = (0, react_1.useMemo)(() => (0, util_js_1.createImgSize)(size), [size]);
|
|
18
|
+
const _onError = (0, react_1.useCallback)((error) => onError(error), [onError]);
|
|
19
|
+
(0, react_1.useEffect)(() => {
|
|
20
|
+
const codeReader = new browser_1.BrowserQRCodeReader();
|
|
21
|
+
const startScanning = async () => {
|
|
22
|
+
try {
|
|
23
|
+
const videoInputDevices = await browser_1.BrowserQRCodeReader.listVideoInputDevices();
|
|
24
|
+
const selectedDeviceId = videoInputDevices[0].deviceId;
|
|
25
|
+
controlsRef.current = await codeReader.decodeFromVideoDevice(selectedDeviceId, videoRef.current ?? undefined, (result, error) => {
|
|
26
|
+
if (result) {
|
|
27
|
+
onScan(result.getText());
|
|
28
|
+
}
|
|
29
|
+
if (error && !(error instanceof Error)) {
|
|
30
|
+
_onError(new Error(error));
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
_onError(error instanceof Error ? error : new Error('Unknown error occurred'));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
39
|
+
const timeoutId = setTimeout(startScanning, delay);
|
|
40
|
+
return () => {
|
|
41
|
+
clearTimeout(timeoutId);
|
|
42
|
+
if (controlsRef.current) {
|
|
43
|
+
controlsRef.current.stop();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}, [onScan, _onError, delay]);
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)(StyledDiv, { className: className, style: containerStyle, children: (0, jsx_runtime_1.jsx)("video", { className: 'ui--qr-Scan', ref: videoRef, style: style }) }));
|
|
48
|
+
}
|
|
49
|
+
const StyledDiv = styled_js_1.styled.div `
|
|
50
|
+
.ui--qr-Scan {
|
|
51
|
+
display: inline-block;
|
|
52
|
+
height: 100%;
|
|
53
|
+
transform: matrix(-1, 0, 0, 1, 0, 0);
|
|
54
|
+
width: 100%;
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
exports.QrScan = react_1.default.memo(Scan);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QrScanAddress = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
7
|
+
const util_crypto_1 = require("@pezkuwi/util-crypto");
|
|
8
|
+
const constants_js_1 = require("./constants.js");
|
|
9
|
+
const Scan_js_1 = require("./Scan.js");
|
|
10
|
+
function ScanAddress({ className, isEthereum, onError, onScan, size, style }) {
|
|
11
|
+
const _onScan = (0, react_1.useCallback)((data) => {
|
|
12
|
+
if (data) {
|
|
13
|
+
try {
|
|
14
|
+
let prefix;
|
|
15
|
+
let content;
|
|
16
|
+
let genesisHash;
|
|
17
|
+
let name;
|
|
18
|
+
if (!isEthereum) {
|
|
19
|
+
[prefix, content, genesisHash, ...name] = data.split(':');
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
[prefix, content, ...name] = data.split(':');
|
|
23
|
+
genesisHash = null;
|
|
24
|
+
content = content.substring(0, 42);
|
|
25
|
+
}
|
|
26
|
+
const expectedPrefix = (isEthereum ? 'ethereum' : constants_js_1.ADDRESS_PREFIX);
|
|
27
|
+
const isValidPrefix = (prefix === expectedPrefix) || (prefix === constants_js_1.SEED_PREFIX);
|
|
28
|
+
if (!isValidPrefix) {
|
|
29
|
+
throw new Error(`Invalid prefix received, expected '${expectedPrefix} or ${constants_js_1.SEED_PREFIX}' , found '${prefix}'`);
|
|
30
|
+
}
|
|
31
|
+
const isAddress = prefix === expectedPrefix;
|
|
32
|
+
if (isAddress && !isEthereum) {
|
|
33
|
+
(0, util_crypto_1.decodeAddress)(content);
|
|
34
|
+
}
|
|
35
|
+
onScan({ content, genesisHash: genesisHash, isAddress, name: name?.length ? name.join(':') : undefined });
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
onError && onError(error);
|
|
39
|
+
console.error('@pezkuwi/react-qr:QrScanAddress', error.message, data);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}, [onScan, onError, isEthereum]);
|
|
43
|
+
return ((0, jsx_runtime_1.jsx)(Scan_js_1.QrScan, { className: className, onError: onError, onScan: _onScan, size: size, style: style }));
|
|
44
|
+
}
|
|
45
|
+
exports.QrScanAddress = react_1.default.memo(ScanAddress);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QrScanSignature = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = tslib_1.__importStar(require("react"));
|
|
7
|
+
const Scan_js_1 = require("./Scan.js");
|
|
8
|
+
function ScanSignature({ className, onError, onScan, size, style }) {
|
|
9
|
+
const _onScan = (0, react_1.useCallback)((signature) => signature && onScan({ signature: `0x${signature}` }), [onScan]);
|
|
10
|
+
return ((0, jsx_runtime_1.jsx)(Scan_js_1.QrScan, { className: className, onError: onError, onScan: _onScan, size: size, style: style }));
|
|
11
|
+
}
|
|
12
|
+
exports.QrScanSignature = react_1.default.memo(ScanSignature);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QrScanSignature = exports.QrScanAddress = exports.packageInfo = exports.QrNetworkSpecs = exports.QrDisplayPayload = exports.QrDisplayAddress = void 0;
|
|
4
|
+
var DisplayAddress_js_1 = require("./DisplayAddress.js");
|
|
5
|
+
Object.defineProperty(exports, "QrDisplayAddress", { enumerable: true, get: function () { return DisplayAddress_js_1.QrDisplayAddress; } });
|
|
6
|
+
var DisplayPayload_js_1 = require("./DisplayPayload.js");
|
|
7
|
+
Object.defineProperty(exports, "QrDisplayPayload", { enumerable: true, get: function () { return DisplayPayload_js_1.QrDisplayPayload; } });
|
|
8
|
+
var NetworkSpecs_js_1 = require("./NetworkSpecs.js");
|
|
9
|
+
Object.defineProperty(exports, "QrNetworkSpecs", { enumerable: true, get: function () { return NetworkSpecs_js_1.QrNetworkSpecs; } });
|
|
10
|
+
var packageInfo_js_1 = require("./packageInfo.js");
|
|
11
|
+
Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } });
|
|
12
|
+
var ScanAddress_js_1 = require("./ScanAddress.js");
|
|
13
|
+
Object.defineProperty(exports, "QrScanAddress", { enumerable: true, get: function () { return ScanAddress_js_1.QrScanAddress; } });
|
|
14
|
+
var ScanSignature_js_1 = require("./ScanSignature.js");
|
|
15
|
+
Object.defineProperty(exports, "QrScanSignature", { enumerable: true, get: function () { return ScanSignature_js_1.QrScanSignature; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CMD_SIGN_MSG = exports.CMD_SIGN_IMMORTAL_TX = exports.CMD_SIGN_TX_HASH = exports.CMD_SIGN_TX = exports.CRYPTO_SR25519 = exports.SUBSTRATE_ID = exports.FRAME_SIZE = exports.SEED_PREFIX = exports.ADDRESS_PREFIX = void 0;
|
|
4
|
+
exports.ADDRESS_PREFIX = 'substrate';
|
|
5
|
+
exports.SEED_PREFIX = 'secret';
|
|
6
|
+
exports.FRAME_SIZE = 1024;
|
|
7
|
+
exports.SUBSTRATE_ID = new Uint8Array([0x53]);
|
|
8
|
+
exports.CRYPTO_SR25519 = new Uint8Array([0x01]);
|
|
9
|
+
exports.CMD_SIGN_TX = new Uint8Array([0]);
|
|
10
|
+
exports.CMD_SIGN_TX_HASH = new Uint8Array([1]);
|
|
11
|
+
exports.CMD_SIGN_IMMORTAL_TX = new Uint8Array([2]);
|
|
12
|
+
exports.CMD_SIGN_MSG = new Uint8Array([3]);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.qrcode = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const qrcode_generator_1 = tslib_1.__importDefault(require("qrcode-generator"));
|
|
6
|
+
const qrcode = qrcode_generator_1.default;
|
|
7
|
+
exports.qrcode = qrcode;
|
|
8
|
+
qrcode.stringToBytes = (data) => data;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.styled = void 0;
|
|
4
|
+
var styled_components_1 = require("styled-components");
|
|
5
|
+
Object.defineProperty(exports, "styled", { enumerable: true, get: function () { return styled_components_1.styled; } });
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeNumber = encodeNumber;
|
|
4
|
+
exports.encodeString = encodeString;
|
|
5
|
+
exports.decodeString = decodeString;
|
|
6
|
+
exports.createAddressPayload = createAddressPayload;
|
|
7
|
+
exports.createSignPayload = createSignPayload;
|
|
8
|
+
exports.createFrames = createFrames;
|
|
9
|
+
exports.createImgSize = createImgSize;
|
|
10
|
+
const util_1 = require("@pezkuwi/util");
|
|
11
|
+
const util_crypto_1 = require("@pezkuwi/util-crypto");
|
|
12
|
+
const constants_js_1 = require("./constants.js");
|
|
13
|
+
const MULTIPART = new Uint8Array([0]);
|
|
14
|
+
function encodeNumber(value) {
|
|
15
|
+
return new Uint8Array([value >> 8, value & 0xff]);
|
|
16
|
+
}
|
|
17
|
+
function encodeString(value) {
|
|
18
|
+
const count = value.length;
|
|
19
|
+
const u8a = new Uint8Array(count);
|
|
20
|
+
for (let i = 0; i < count; i++) {
|
|
21
|
+
u8a[i] = value.charCodeAt(i);
|
|
22
|
+
}
|
|
23
|
+
return u8a;
|
|
24
|
+
}
|
|
25
|
+
function decodeString(value) {
|
|
26
|
+
return value.reduce((str, code) => {
|
|
27
|
+
return str + String.fromCharCode(code);
|
|
28
|
+
}, '');
|
|
29
|
+
}
|
|
30
|
+
function createAddressPayload(address, genesisHash) {
|
|
31
|
+
return encodeString(`${constants_js_1.ADDRESS_PREFIX}:${address}:${genesisHash}`);
|
|
32
|
+
}
|
|
33
|
+
function createSignPayload(address, cmd, payload, genesisHash) {
|
|
34
|
+
return (0, util_1.u8aConcat)(constants_js_1.SUBSTRATE_ID, constants_js_1.CRYPTO_SR25519, new Uint8Array([cmd]), (0, util_crypto_1.decodeAddress)(address), (0, util_1.u8aToU8a)(payload), (0, util_1.u8aToU8a)(genesisHash));
|
|
35
|
+
}
|
|
36
|
+
function createFrames(input) {
|
|
37
|
+
const frames = [];
|
|
38
|
+
let idx = 0;
|
|
39
|
+
while (idx < input.length) {
|
|
40
|
+
frames.push(input.subarray(idx, idx + constants_js_1.FRAME_SIZE));
|
|
41
|
+
idx += constants_js_1.FRAME_SIZE;
|
|
42
|
+
}
|
|
43
|
+
return frames.map((frame, index) => (0, util_1.u8aConcat)(MULTIPART, encodeNumber(frames.length), encodeNumber(index), frame));
|
|
44
|
+
}
|
|
45
|
+
function createImgSize(size) {
|
|
46
|
+
if (!size) {
|
|
47
|
+
return {
|
|
48
|
+
height: 'auto',
|
|
49
|
+
width: '100%'
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const height = (0, util_1.isString)(size)
|
|
53
|
+
? size
|
|
54
|
+
: `${size}px`;
|
|
55
|
+
return {
|
|
56
|
+
height,
|
|
57
|
+
width: height
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { objectSpread } from '@pezkuwi/util';
|
|
4
|
+
import { xxhashAsHex } from '@pezkuwi/util-crypto';
|
|
5
|
+
import { qrcode } from './qrcode.js';
|
|
6
|
+
import { styled } from './styled.js';
|
|
7
|
+
import { createFrames, createImgSize } from './util.js';
|
|
8
|
+
const DEFAULT_FRAME_DELAY = 2750;
|
|
9
|
+
const TIMER_INC = 500;
|
|
10
|
+
function getDataUrl(value) {
|
|
11
|
+
const qr = qrcode(0, 'M');
|
|
12
|
+
// HACK See our qrcode stringToBytes override as used internally. This
|
|
13
|
+
// will only work for the case where we actually pass `Bytes` in here
|
|
14
|
+
qr.addData(value, 'Byte');
|
|
15
|
+
qr.make();
|
|
16
|
+
return qr.createDataURL(16, 0);
|
|
17
|
+
}
|
|
18
|
+
function Display({ className = '', size, skipEncoding, style = {}, timerDelay = DEFAULT_FRAME_DELAY, value }) {
|
|
19
|
+
const [{ image }, setFrameState] = useState({ frameIdx: 0, frames: [], image: null, valueHash: null });
|
|
20
|
+
const timerRef = useRef({ timerDelay, timerId: null });
|
|
21
|
+
const containerStyle = useMemo(() => createImgSize(size), [size]);
|
|
22
|
+
// run on initial load to setup the global timer and provide and unsubscribe
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const nextFrame = () => setFrameState((state) => {
|
|
25
|
+
// when we have a single frame, we only ever fire once
|
|
26
|
+
if (state.frames.length <= 1) {
|
|
27
|
+
return state;
|
|
28
|
+
}
|
|
29
|
+
let frameIdx = state.frameIdx + 1;
|
|
30
|
+
// when we overflow, skip to the first and slightly increase the delay between frames
|
|
31
|
+
if (frameIdx === state.frames.length) {
|
|
32
|
+
frameIdx = 0;
|
|
33
|
+
timerRef.current.timerDelay = timerRef.current.timerDelay + TIMER_INC;
|
|
34
|
+
}
|
|
35
|
+
// only encode the frames on demand, not above as part of the
|
|
36
|
+
// state derivation - in the case of large payloads, this should
|
|
37
|
+
// be slightly more responsive on initial load
|
|
38
|
+
const newState = objectSpread({}, state, {
|
|
39
|
+
frameIdx,
|
|
40
|
+
image: getDataUrl(state.frames[frameIdx])
|
|
41
|
+
});
|
|
42
|
+
// set the new timer last
|
|
43
|
+
timerRef.current.timerId = setTimeout(nextFrame, timerRef.current.timerDelay);
|
|
44
|
+
return newState;
|
|
45
|
+
});
|
|
46
|
+
timerRef.current.timerId = setTimeout(nextFrame, timerRef.current.timerDelay);
|
|
47
|
+
return () => {
|
|
48
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
49
|
+
timerRef.current.timerId && clearTimeout(timerRef.current.timerId);
|
|
50
|
+
};
|
|
51
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, []);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
setFrameState((state) => {
|
|
55
|
+
const valueHash = xxhashAsHex(value);
|
|
56
|
+
if (valueHash === state.valueHash) {
|
|
57
|
+
return state;
|
|
58
|
+
}
|
|
59
|
+
const frames = skipEncoding
|
|
60
|
+
? [value]
|
|
61
|
+
: createFrames(value);
|
|
62
|
+
// encode on demand
|
|
63
|
+
return {
|
|
64
|
+
frameIdx: 0,
|
|
65
|
+
frames,
|
|
66
|
+
image: getDataUrl(frames[0]),
|
|
67
|
+
valueHash
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
}, [skipEncoding, value]);
|
|
71
|
+
if (!image) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return (_jsx(StyledDiv, { className: className, style: containerStyle, children: _jsx("div", { className: 'ui--qr-Display', style: style, children: _jsx("img", { src: image }) }) }));
|
|
75
|
+
}
|
|
76
|
+
const StyledDiv = styled.div `
|
|
77
|
+
.ui--qr-Display {
|
|
78
|
+
height: 100%;
|
|
79
|
+
width: 100%;
|
|
80
|
+
|
|
81
|
+
img,
|
|
82
|
+
svg {
|
|
83
|
+
background: white;
|
|
84
|
+
height: auto !important;
|
|
85
|
+
max-height: 100%;
|
|
86
|
+
max-width: 100%;
|
|
87
|
+
width: auto !important;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
91
|
+
export const QrDisplay = React.memo(Display);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
import { QrDisplay } from './Display.js';
|
|
4
|
+
import { createAddressPayload } from './util.js';
|
|
5
|
+
function DisplayAddress({ address, className, genesisHash, size, style, timerDelay }) {
|
|
6
|
+
const data = useMemo(() => createAddressPayload(address, genesisHash), [address, genesisHash]);
|
|
7
|
+
if (!data) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return (_jsx(QrDisplay, { className: className, size: size, skipEncoding: true, style: style, timerDelay: timerDelay, value: data }));
|
|
11
|
+
}
|
|
12
|
+
export const QrDisplayAddress = React.memo(DisplayAddress);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
import { QrDisplay } from './Display.js';
|
|
4
|
+
import { createSignPayload } from './util.js';
|
|
5
|
+
function DisplayPayload({ address, className, cmd, genesisHash, payload, size, style, timerDelay }) {
|
|
6
|
+
const data = useMemo(() => createSignPayload(address, cmd, payload, genesisHash), [address, cmd, payload, genesisHash]);
|
|
7
|
+
if (!data) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return (_jsx(QrDisplay, { className: className, size: size, style: style, timerDelay: timerDelay, value: data }));
|
|
11
|
+
}
|
|
12
|
+
export const QrDisplayPayload = React.memo(DisplayPayload);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useMemo } from 'react';
|
|
3
|
+
import { QrDisplay } from './Display.js';
|
|
4
|
+
import { encodeString } from './util.js';
|
|
5
|
+
function DisplayNetworkSpecs({ className, networkSpecs, size, style }) {
|
|
6
|
+
const data = useMemo(() => encodeString(JSON.stringify(networkSpecs)), [networkSpecs]);
|
|
7
|
+
if (!data) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return (_jsx(QrDisplay, { className: className, size: size, skipEncoding: true, style: style, value: data }));
|
|
11
|
+
}
|
|
12
|
+
export const QrNetworkSpecs = React.memo(DisplayNetworkSpecs);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BrowserQRCodeReader } from '@zxing/browser';
|
|
3
|
+
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import { styled } from './styled.js';
|
|
5
|
+
import { createImgSize } from './util.js';
|
|
6
|
+
const DEFAULT_DELAY = 150;
|
|
7
|
+
const DEFAULT_ERROR = (error) => {
|
|
8
|
+
console.error('@pezkuwi/react-qr:Scan', error.message);
|
|
9
|
+
};
|
|
10
|
+
function Scan({ className = '', delay = DEFAULT_DELAY, onError = DEFAULT_ERROR, onScan, size, style = {} }) {
|
|
11
|
+
const videoRef = useRef(null);
|
|
12
|
+
const controlsRef = useRef(null);
|
|
13
|
+
const containerStyle = useMemo(() => createImgSize(size), [size]);
|
|
14
|
+
const _onError = useCallback((error) => onError(error), [onError]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
const codeReader = new BrowserQRCodeReader();
|
|
17
|
+
const startScanning = async () => {
|
|
18
|
+
try {
|
|
19
|
+
const videoInputDevices = await BrowserQRCodeReader.listVideoInputDevices();
|
|
20
|
+
const selectedDeviceId = videoInputDevices[0].deviceId;
|
|
21
|
+
controlsRef.current = await codeReader.decodeFromVideoDevice(selectedDeviceId, videoRef.current ?? undefined, (result, error) => {
|
|
22
|
+
if (result) {
|
|
23
|
+
onScan(result.getText());
|
|
24
|
+
}
|
|
25
|
+
if (error && !(error instanceof Error)) {
|
|
26
|
+
_onError(new Error(error));
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
_onError(error instanceof Error ? error : new Error('Unknown error occurred'));
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
35
|
+
const timeoutId = setTimeout(startScanning, delay);
|
|
36
|
+
return () => {
|
|
37
|
+
clearTimeout(timeoutId);
|
|
38
|
+
if (controlsRef.current) {
|
|
39
|
+
controlsRef.current.stop();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}, [onScan, _onError, delay]);
|
|
43
|
+
return (_jsx(StyledDiv, { className: className, style: containerStyle, children: _jsx("video", { className: 'ui--qr-Scan', ref: videoRef, style: style }) }));
|
|
44
|
+
}
|
|
45
|
+
const StyledDiv = styled.div `
|
|
46
|
+
.ui--qr-Scan {
|
|
47
|
+
display: inline-block;
|
|
48
|
+
height: 100%;
|
|
49
|
+
transform: matrix(-1, 0, 0, 1, 0, 0);
|
|
50
|
+
width: 100%;
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
export const QrScan = React.memo(Scan);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useCallback } from 'react';
|
|
3
|
+
import { decodeAddress } from '@pezkuwi/util-crypto';
|
|
4
|
+
import { ADDRESS_PREFIX, SEED_PREFIX } from './constants.js';
|
|
5
|
+
import { QrScan } from './Scan.js';
|
|
6
|
+
function ScanAddress({ className, isEthereum, onError, onScan, size, style }) {
|
|
7
|
+
const _onScan = useCallback((data) => {
|
|
8
|
+
if (data) {
|
|
9
|
+
try {
|
|
10
|
+
let prefix;
|
|
11
|
+
let content;
|
|
12
|
+
let genesisHash;
|
|
13
|
+
let name;
|
|
14
|
+
if (!isEthereum) {
|
|
15
|
+
[prefix, content, genesisHash, ...name] = data.split(':');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
[prefix, content, ...name] = data.split(':');
|
|
19
|
+
genesisHash = null;
|
|
20
|
+
content = content.substring(0, 42);
|
|
21
|
+
}
|
|
22
|
+
const expectedPrefix = (isEthereum ? 'ethereum' : ADDRESS_PREFIX);
|
|
23
|
+
const isValidPrefix = (prefix === expectedPrefix) || (prefix === SEED_PREFIX);
|
|
24
|
+
if (!isValidPrefix) {
|
|
25
|
+
throw new Error(`Invalid prefix received, expected '${expectedPrefix} or ${SEED_PREFIX}' , found '${prefix}'`);
|
|
26
|
+
}
|
|
27
|
+
const isAddress = prefix === expectedPrefix;
|
|
28
|
+
if (isAddress && !isEthereum) {
|
|
29
|
+
decodeAddress(content);
|
|
30
|
+
}
|
|
31
|
+
onScan({ content, genesisHash: genesisHash, isAddress, name: name?.length ? name.join(':') : undefined });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
onError && onError(error);
|
|
35
|
+
console.error('@pezkuwi/react-qr:QrScanAddress', error.message, data);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}, [onScan, onError, isEthereum]);
|
|
39
|
+
return (_jsx(QrScan, { className: className, onError: onError, onScan: _onScan, size: size, style: style }));
|
|
40
|
+
}
|
|
41
|
+
export const QrScanAddress = React.memo(ScanAddress);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { useCallback } from 'react';
|
|
3
|
+
import { QrScan } from './Scan.js';
|
|
4
|
+
function ScanSignature({ className, onError, onScan, size, style }) {
|
|
5
|
+
const _onScan = useCallback((signature) => signature && onScan({ signature: `0x${signature}` }), [onScan]);
|
|
6
|
+
return (_jsx(QrScan, { className: className, onError: onError, onScan: _onScan, size: size, style: style }));
|
|
7
|
+
}
|
|
8
|
+
export const QrScanSignature = React.memo(ScanSignature);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { QrDisplayAddress } from './DisplayAddress.js';
|
|
2
|
+
export { QrDisplayPayload } from './DisplayPayload.js';
|
|
3
|
+
export { QrNetworkSpecs } from './NetworkSpecs.js';
|
|
4
|
+
export { packageInfo } from './packageInfo.js';
|
|
5
|
+
export { QrScanAddress } from './ScanAddress.js';
|
|
6
|
+
export { QrScanSignature } from './ScanSignature.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const ADDRESS_PREFIX = 'substrate';
|
|
2
|
+
export const SEED_PREFIX = 'secret';
|
|
3
|
+
export const FRAME_SIZE = 1024;
|
|
4
|
+
export const SUBSTRATE_ID = new Uint8Array([0x53]);
|
|
5
|
+
export const CRYPTO_SR25519 = new Uint8Array([0x01]);
|
|
6
|
+
export const CMD_SIGN_TX = new Uint8Array([0]);
|
|
7
|
+
export const CMD_SIGN_TX_HASH = new Uint8Array([1]);
|
|
8
|
+
export const CMD_SIGN_IMMORTAL_TX = new Uint8Array([2]);
|
|
9
|
+
export const CMD_SIGN_MSG = new Uint8Array([3]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const packageInfo = { name: '@pezkuwi/react-qr', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '3.16.6' };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { styled } from 'styled-components';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isString, u8aConcat, u8aToU8a } from '@pezkuwi/util';
|
|
2
|
+
import { decodeAddress } from '@pezkuwi/util-crypto';
|
|
3
|
+
import { ADDRESS_PREFIX, CRYPTO_SR25519, FRAME_SIZE, SUBSTRATE_ID } from './constants.js';
|
|
4
|
+
const MULTIPART = new Uint8Array([0]);
|
|
5
|
+
export function encodeNumber(value) {
|
|
6
|
+
return new Uint8Array([value >> 8, value & 0xff]);
|
|
7
|
+
}
|
|
8
|
+
export function encodeString(value) {
|
|
9
|
+
const count = value.length;
|
|
10
|
+
const u8a = new Uint8Array(count);
|
|
11
|
+
for (let i = 0; i < count; i++) {
|
|
12
|
+
u8a[i] = value.charCodeAt(i);
|
|
13
|
+
}
|
|
14
|
+
return u8a;
|
|
15
|
+
}
|
|
16
|
+
export function decodeString(value) {
|
|
17
|
+
return value.reduce((str, code) => {
|
|
18
|
+
return str + String.fromCharCode(code);
|
|
19
|
+
}, '');
|
|
20
|
+
}
|
|
21
|
+
export function createAddressPayload(address, genesisHash) {
|
|
22
|
+
return encodeString(`${ADDRESS_PREFIX}:${address}:${genesisHash}`);
|
|
23
|
+
}
|
|
24
|
+
export function createSignPayload(address, cmd, payload, genesisHash) {
|
|
25
|
+
return u8aConcat(SUBSTRATE_ID, CRYPTO_SR25519, new Uint8Array([cmd]), decodeAddress(address), u8aToU8a(payload), u8aToU8a(genesisHash));
|
|
26
|
+
}
|
|
27
|
+
export function createFrames(input) {
|
|
28
|
+
const frames = [];
|
|
29
|
+
let idx = 0;
|
|
30
|
+
while (idx < input.length) {
|
|
31
|
+
frames.push(input.subarray(idx, idx + FRAME_SIZE));
|
|
32
|
+
idx += FRAME_SIZE;
|
|
33
|
+
}
|
|
34
|
+
return frames.map((frame, index) => u8aConcat(MULTIPART, encodeNumber(frames.length), encodeNumber(index), frame));
|
|
35
|
+
}
|
|
36
|
+
export function createImgSize(size) {
|
|
37
|
+
if (!size) {
|
|
38
|
+
return {
|
|
39
|
+
height: 'auto',
|
|
40
|
+
width: '100%'
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const height = isString(size)
|
|
44
|
+
? size
|
|
45
|
+
: `${size}px`;
|
|
46
|
+
return {
|
|
47
|
+
height,
|
|
48
|
+
width: height
|
|
49
|
+
};
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Jaco Greeff <jacogr@gmail.com>",
|
|
3
|
+
"bugs": "https://github.com/pezkuwichain/ui/issues",
|
|
4
|
+
"description": "Generates and reads QR codes",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=18"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/pezkuwichain/ui/tree/master/packages/react-qr#readme",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"name": "@pezkuwi/react-qr",
|
|
11
|
+
"repository": {
|
|
12
|
+
"directory": "packages/react-qr",
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/pezkuwichain/ui.git"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./packageDetect.js",
|
|
18
|
+
"./packageDetect.cjs"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"version": "3.16.6",
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pezkuwi/ui-settings": "3.16.6",
|
|
25
|
+
"@pezkuwi/util": "^14.0.5",
|
|
26
|
+
"@pezkuwi/util-crypto": "^14.0.5",
|
|
27
|
+
"@zxing/browser": "^0.1.5",
|
|
28
|
+
"@zxing/library": "^0.21.2",
|
|
29
|
+
"qrcode-generator": "^1.4.4",
|
|
30
|
+
"styled-components": "^6.1.1",
|
|
31
|
+
"tslib": "^2.8.1"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@pezkuwi/util": "*",
|
|
35
|
+
"@pezkuwi/util-crypto": "*",
|
|
36
|
+
"react": "*",
|
|
37
|
+
"react-dom": "*",
|
|
38
|
+
"react-is": "*"
|
|
39
|
+
}
|
|
40
|
+
}
|