mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-04-22 20:48:03 +00:00
4c58f48645
* Bump deps * Adjust for new eslint parser
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
// Copyright 2017-2020 @polkadot/react-qr authors & contributors
|
|
// This software may be modified and distributed under the terms
|
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
|
|
|
import React, { useCallback } from 'react';
|
|
import { assert } from '@polkadot/util';
|
|
import { decodeAddress } from '@polkadot/util-crypto';
|
|
|
|
import { ADDRESS_PREFIX, SEED_PREFIX } from './constants';
|
|
import QrScan from './Scan';
|
|
|
|
interface ScanType {
|
|
isAddress: boolean;
|
|
content: string;
|
|
genesisHash: string;
|
|
name?: string;
|
|
}
|
|
|
|
interface Props {
|
|
className?: string;
|
|
onError?: (error: Error) => void;
|
|
onScan: (scanned: ScanType) => void;
|
|
size?: string | number;
|
|
style?: React.CSSProperties;
|
|
}
|
|
|
|
function ScanAddress ({ className, onError, onScan, size, style }: Props): React.ReactElement<Props> {
|
|
const _onScan = useCallback(
|
|
(data: string | null): void => {
|
|
if (data) {
|
|
try {
|
|
const [prefix, content, genesisHash, name] = data.split(':');
|
|
const isValidPrefix = prefix === ADDRESS_PREFIX || prefix === SEED_PREFIX;
|
|
|
|
assert(isValidPrefix, `Invalid prefix received, expected '${ADDRESS_PREFIX}/${SEED_PREFIX}' , found '${prefix}'`);
|
|
|
|
const isAddress = prefix === ADDRESS_PREFIX;
|
|
|
|
if (isAddress) {
|
|
decodeAddress(content);
|
|
}
|
|
|
|
onScan({ content, genesisHash, isAddress, name });
|
|
} catch (error) {
|
|
console.error('@polkadot/react-qr:QrScanAddress', (error as Error).message, data);
|
|
}
|
|
}
|
|
},
|
|
[onScan]
|
|
);
|
|
|
|
return (
|
|
<QrScan
|
|
className={className}
|
|
onError={onError}
|
|
onScan={_onScan}
|
|
size={size}
|
|
style={style}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default React.memo(ScanAddress);
|