Skip to content

Commit

Permalink
feat: verify address according to chain type
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Nov 14, 2019
1 parent a7b463b commit 2026921
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/neuron-ui/src/components/Send/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,17 @@ export const useInitialize = (
clear(dispatch)
}, [walletID, dispatch])

const onGetAddressErrorMessage = useCallback(
(addr: string) => {
const onGetAddressErrorMessage: (isMainnet: boolean) => (addr: string) => string = useCallback(
(isMainnet: boolean) => (addr: string) => {
if (addr === '') {
return t(`messages.codes.${ErrorCode.AddressIsEmpty}`)
}
if (isMainnet && !addr.startsWith('ckb')) {
return t(`messages.mainnet-address-required`)
}
if (!isMainnet && !addr.startsWith('ckt')) {
return t(`messages.testnet-address-required`)
}
if (!verifyAddress(addr)) {
return t(`messages.codes.${ErrorCode.FieldInvalid}`, {
fieldName: 'address',
Expand Down
7 changes: 5 additions & 2 deletions packages/neuron-ui/src/components/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const Send = ({
loadings: { sending = false },
},
wallet: { id: walletID = '', balance = '' },
chain: { connectionStatus },
chain: { networkID, connectionStatus },
settings: { networks = [] },
dispatch,
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps<{ address: string }>>) => {
const { t } = useTranslation()
Expand Down Expand Up @@ -66,6 +67,8 @@ const Send = ({
const errorMessageUnderTotal = verifyTotalAmount(totalAmount, fee, balance)
? errorMessage
: t(`messages.codes.${ErrorCode.AmountNotEnough}`)
const network = networks.find(n => n.id === networkID)
const isMainnet = (network && network.chain === 'ckb') || false // TODO: shoudl be replaced after the mainnet tag is introduced

return (
<Stack verticalFill tokens={{ childrenGap: 15, padding: '20px 0 0 0' }}>
Expand Down Expand Up @@ -97,7 +100,7 @@ const Send = ({
onChange={onItemChange}
required
validateOnLoad={false}
onGetErrorMessage={onGetAddressErrorMessage}
onGetErrorMessage={onGetAddressErrorMessage(isMainnet)}
/>
</Stack.Item>
<Stack styles={{ root: { width: '48px' } }} verticalAlign="start">
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@
"keystore-password": "Password",
"deposit": "Deposit"
},
"mainnet-address-required": "Please enter a mainnet address",
"testnet-address-required": "Please enter a testnet address",
"codes": {
"-3": "",
"100": "Amount is not enough",
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@
"keystore-password": "密码",
"deposit": "存入金额"
},
"mainnet-address-required": "请输入主网地址",
"testnet-address-required": "请输出测试网地址",
"codes": {
"-3": "",
"100": "余额不足",
Expand Down
8 changes: 7 additions & 1 deletion packages/neuron-ui/src/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import {
import { CKBToShannonFormatter } from 'utils/formatters'
import { ckbCore } from 'services/chain'

export const verifyAddress = (address: string): boolean => {
export const verifyAddress = (address: string, isMainnet?: boolean): boolean => {
if (typeof address !== 'string' || address.length !== 46) {
return false
}
if (isMainnet === true && !address.startsWith('ckb')) {
return false
}
if (isMainnet === false && !address.startsWith('ckt')) {
return false
}
try {
return ckbCore.utils.parseAddress(address, 'hex').startsWith('0x0100')
} catch (err) {
Expand Down
11 changes: 11 additions & 0 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dialog, SaveDialogReturnValue, BrowserWindow } from 'electron'
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from 'services/wallets'
import Keystore from 'models/keys/keystore'
import Keychain from 'models/keys/keychain'
import ChainInfo from 'models/chain-info'
import { validateMnemonic, mnemonicToSeedSync } from 'models/keys/mnemonic'
import { AccountExtendedPublicKey, ExtendedPrivateKey } from 'models/keys/key'
import { ResponseCode } from 'utils/const'
Expand All @@ -22,6 +23,7 @@ import i18n from 'utils/i18n'
import AddressService from 'services/addresses'
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
import { TransactionWithoutHash, OutPoint } from 'types/cell-types'
import { MainnetAddressRequired, TestnetAddressRequired } from 'exceptions/address'

export default class WalletsController {
public static async getAll(): Promise<Controller.Response<Pick<Wallet, 'id' | 'name'>[]>> {
Expand Down Expand Up @@ -339,7 +341,16 @@ export default class WalletsController {
feeRate = '1000'
}

const isMainnet = ChainInfo.getInstance().isMainnet()
params.items.forEach(item => {
if (isMainnet && !item.address.startsWith('ckb')) {
throw new MainnetAddressRequired(item.address)
}

if (!isMainnet && !item.address.startsWith('ckt')) {
throw new TestnetAddressRequired(item.address)
}

if (!this.verifyAddress(item.address)) {
throw new InvalidAddress(item.address)
}
Expand Down
15 changes: 14 additions & 1 deletion packages/neuron-wallet/src/exceptions/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@ export class InvalidAddress extends Error {
super(i18n.t('invalid-address', { address }))
}
}
export default { InvalidAddress }

export class MainnetAddressRequired extends Error {
constructor(address: string) {
super(i18n.t('mainnet-address-required', { address }))
}
}

export class TestnetAddressRequired extends Error {
constructor(address: string) {
super(i18n.t('testnet-address-required', { address }))
}
}

export default { InvalidAddress, MainnetAddressRequired, TestnetAddressRequired }
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export default {
'invalid-keystore': 'Keystore is invalid',
'invalid-json': 'Invalid JSON file',
'cell-is-not-yet-live': 'Cell is not yet live!',
'transaction-is-not-committed-yet': 'Transaction is not committed yet!'
'transaction-is-not-committed-yet': 'Transaction is not committed yet!',
'mainnet-address-required': '{{address}} is not a mainnet address',
'testnet-address-required': '{{address}} is not a testnet address'
},
contextMenu: {
select: 'Select',
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-wallet/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export default {
'invalid-keystore': 'Keystore 格式不正确',
'invalid-json': 'JSON 文件格式不正确',
'cell-is-not-yet-live': 'Cell 尚未激活',
'transaction-is-not-committed-yet': '交易未提交'
'transaction-is-not-committed-yet': '交易未提交',
'mainnet-address-required': '{{address}} 不是主网地址',
'testnet-address-required': '{{address}} 不是测试网地址'
},
contextMenu: {
select: '选择',
Expand Down

0 comments on commit 2026921

Please sign in to comment.