|
| 1 | +import React, { useCallback, useState, useMemo } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import { ckbCore } from 'services/chain' |
| 4 | +import { shannonToCKBFormatter, useDidMount, isSuccessResponse, CONSTANTS } from 'utils' |
| 5 | +import { getSystemCodeHash, getAllNetworks, getCurrentNetworkID } from 'services/remote' |
| 6 | +import CopyZone from 'widgets/CopyZone' |
| 7 | +import { DeviceSignIndex as DeviceSignIndexSubject } from 'services/subjects' |
| 8 | + |
| 9 | +import styles from '../HardwareSign/hardwareSign.module.scss' |
| 10 | + |
| 11 | +const { MAINNET_TAG } = CONSTANTS |
| 12 | + |
| 13 | +const HDWalletSign = ({ tx }: { tx: State.DetailedTransaction }) => { |
| 14 | + const [t] = useTranslation() |
| 15 | + const [isMainnet, setIsMainnet] = useState(false) |
| 16 | + const addressPrefix = isMainnet ? ckbCore.utils.AddressPrefix.Mainnet : ckbCore.utils.AddressPrefix.Testnet |
| 17 | + const [systemCodeHash, setSystemCodeHash] = useState<string>('') |
| 18 | + const [inputVisable, setInputVisable] = useState(true) |
| 19 | + const [activeInputIndex, setActiveInputIndex] = useState(0) |
| 20 | + |
| 21 | + useDidMount(() => { |
| 22 | + getSystemCodeHash().then(res => { |
| 23 | + if (isSuccessResponse(res)) { |
| 24 | + setSystemCodeHash(res.result) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + Promise.all([getAllNetworks(), getCurrentNetworkID()]) |
| 29 | + .then(([networksRes, idRes]) => { |
| 30 | + if (isSuccessResponse(networksRes) && isSuccessResponse(idRes)) { |
| 31 | + const network = networksRes.result.find((n: any) => n.id === idRes.result) |
| 32 | + if (!network) { |
| 33 | + throw new Error('Cannot find current network in the network list') |
| 34 | + } |
| 35 | + |
| 36 | + setIsMainnet(network.chain === MAINNET_TAG) |
| 37 | + } |
| 38 | + }) |
| 39 | + .catch(err => console.warn(err)) |
| 40 | + |
| 41 | + const DeviceSignIndexSubscription = DeviceSignIndexSubject.subscribe(index => { |
| 42 | + setActiveInputIndex(index) |
| 43 | + }) |
| 44 | + |
| 45 | + return () => { |
| 46 | + DeviceSignIndexSubscription.unsubscribe() |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + const renderList = useCallback( |
| 51 | + (cells: Readonly<(State.DetailedInput | State.DetailedOutput)[]>, activeIndex: number, isInput: boolean) => |
| 52 | + cells.map((cell, index) => { |
| 53 | + let address = '' |
| 54 | + if (!cell.lock) { |
| 55 | + address = t('transaction.cell-from-cellbase') |
| 56 | + } else { |
| 57 | + try { |
| 58 | + if (cell.lock.codeHash === systemCodeHash && cell.lock.hashType === 'type') { |
| 59 | + address = ckbCore.utils.bech32Address(cell.lock.args, { |
| 60 | + prefix: addressPrefix, |
| 61 | + type: ckbCore.utils.AddressType.HashIdx, |
| 62 | + codeHashOrCodeHashIndex: '0x00', |
| 63 | + }) |
| 64 | + } else { |
| 65 | + address = ckbCore.utils.fullPayloadToAddress({ |
| 66 | + arg: cell.lock.args, |
| 67 | + prefix: addressPrefix, |
| 68 | + type: |
| 69 | + cell.lock.hashType === 'data' |
| 70 | + ? ckbCore.utils.AddressType.DataCodeHash |
| 71 | + : ckbCore.utils.AddressType.TypeCodeHash, |
| 72 | + codeHash: cell.lock.codeHash, |
| 73 | + }) |
| 74 | + } |
| 75 | + } catch (err) { |
| 76 | + console.error(err) |
| 77 | + } |
| 78 | + } |
| 79 | + const capacity = shannonToCKBFormatter(cell.capacity || '0') |
| 80 | + |
| 81 | + const classNames = [styles.tr] |
| 82 | + |
| 83 | + if (activeIndex === index && isInput) { |
| 84 | + classNames.push(styles.active) |
| 85 | + } |
| 86 | + |
| 87 | + if (index < activeIndex && isInput) { |
| 88 | + classNames.push(styles.signed) |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <tr key={cell.lockHash || ''} data-address={address} className={classNames.join('')}> |
| 93 | + <td title={`${index + 1}`}> |
| 94 | + <div>{index + 1}</div> |
| 95 | + </td> |
| 96 | + <td title={address} className={styles.addressCell}> |
| 97 | + <CopyZone content={address} name={t('history.copy-address')}> |
| 98 | + {address} |
| 99 | + </CopyZone> |
| 100 | + </td> |
| 101 | + <td> |
| 102 | + <CopyZone content={capacity.replace(/,/g, '')} name={t('history.copy-balance')}> |
| 103 | + {`${capacity} CKB`} |
| 104 | + </CopyZone> |
| 105 | + </td> |
| 106 | + </tr> |
| 107 | + ) |
| 108 | + }), |
| 109 | + [t, addressPrefix, systemCodeHash] |
| 110 | + ) |
| 111 | + |
| 112 | + const inputBody = useMemo(() => { |
| 113 | + return renderList(tx.inputs, activeInputIndex, true) |
| 114 | + }, [activeInputIndex, tx.inputs, renderList]) |
| 115 | + |
| 116 | + const outputBody = useMemo(() => { |
| 117 | + return renderList(tx.outputs, activeInputIndex, false) |
| 118 | + }, [activeInputIndex, tx.outputs, renderList]) |
| 119 | + |
| 120 | + const showInput = useCallback(() => { |
| 121 | + setInputVisable(true) |
| 122 | + }, [setInputVisable]) |
| 123 | + |
| 124 | + const showOutput = useCallback(() => { |
| 125 | + setInputVisable(false) |
| 126 | + }, [setInputVisable]) |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className={styles.sign}> |
| 130 | + <div className={styles.tabs}> |
| 131 | + <button className={inputVisable ? styles.active : ''} onClick={showInput} type="button"> |
| 132 | + {t('hardware-sign.inputs', { index: activeInputIndex, length: tx.inputs.length })} |
| 133 | + </button> |
| 134 | + <button className={!inputVisable ? styles.active : ''} onClick={showOutput} type="button"> |
| 135 | + {t('hardware-sign.outputs', { length: tx.outputs.length })} |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + <div className={styles.table}> |
| 139 | + <hr /> |
| 140 | + <table className={styles.inputList}> |
| 141 | + <tbody>{inputVisable ? inputBody : outputBody}</tbody> |
| 142 | + </table> |
| 143 | + <hr /> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +HDWalletSign.displayName = 'HDWalletSign' |
| 150 | + |
| 151 | +export default HDWalletSign |
0 commit comments