mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-04-23 01:28:02 +00:00
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
// Copyright 2017-2020 @polkadot/react-qr authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
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: name?.length ? name.join(':') : undefined });
|
|
} 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);
|