Skip to content

Commit

Permalink
Merge pull request #883 from Keith-CY/check-address-before-submitssion
Browse files Browse the repository at this point in the history
refactor(neuron-ui): verify the addresses before submission
  • Loading branch information
ashchan authored Aug 20, 2019
2 parents f9df65b + 4908626 commit 4d88216
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/neuron-ui/src/components/Send/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
type: 'warning',
timestamp: Date.now(),
content: Message.AtLeastOneAddressNeeded,
meta: {},
},
}
if (!items.length || !items[0].address) {
Expand All @@ -28,8 +29,10 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
}
const invalid = items.some(
(item): boolean => {
if (!verifyAddress(item.address)) {
const isAddressValid = verifyAddress(item.address)
if (typeof isAddressValid === 'string') {
errorAction.payload.content = Message.InvalidAddress
errorAction.payload.meta = { address: item.address }
return true
}
if (Number.isNaN(+item.amount) || +item.amount < 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/containers/Notification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const NoticeContent = ({ dispatch }: React.PropsWithoutRef<StateWithDispa
<TopAlertActions dispatch={dispatch} count={notificationsInDesc.length} onDismiss={onTopAlertDismiss} />
}
>
{t(notification.content)}
{t(notification.content, notification.meta)}
</MessageBar>
) : null}

Expand Down Expand Up @@ -154,7 +154,7 @@ export const NoticeContent = ({ dispatch }: React.PropsWithoutRef<StateWithDispa
</Text>
<IconButton iconProps={{ iconName: 'Dismiss' }} onClick={onNotificationDismiss(n.timestamp)} />
</Stack>
<Text as="p">{t(n.content)}</Text>
<Text as="p">{t(n.content, n.meta)}</Text>
</Stack>
)
})}
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@
"protocol-required": "Protocol is required",
"length-of-name-should-be-less-than-or-equal-to": "Length of name should be less than or equal to {{length}}",
"network-name-used": "Network name is used",
"invalid-address": "Invalid address",
"invalid-amount": "Invalid amount",
"amount-not-enough": "Amount is not enough",
"is-unremovable": "{{target}} is unremovable",
Expand Down Expand Up @@ -272,7 +271,8 @@
"rpc-url-should-have-protocol": "The RPC URL should start with http(s)://",
"rpc-url-should-have-no-whitespaces": "The RPC URL should have no whitespaces",
"is-required": "{{field}} is required",
"is-used": "{{field}} is used"
"is-used": "{{field}} is used",
"invalid-address": "{{address}} is an invalid address"
},
"sync": {
"syncing": "Syncing",
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@
"protocol-required": "请指定 URL 协议",
"length-of-name-should-be-less-than-or-equal-to": "名称长度应不大于 {{length}}",
"network-name-used": "节点名称已存在",
"invalid-address": "无效的地址",
"invalid-amount": "无效的价值",
"amount-not-enough": "余额不足",
"is-unremovable": "{{target}}不可删除",
Expand Down Expand Up @@ -272,7 +271,8 @@
"network-address-should-have-protocol": "RPC 地址应以 http(s)//: 开始",
"network-address-should-have-no-whitespaces": "RPC 地址不能包含空格",
"is-required": "{{field}}是必须的",
"is-used": "{{field}}已使用"
"is-used": "{{field}}已使用",
"invalid-address": "{{address}} 是无效的地址"
},
"sync": {
"syncing": "同步中",
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare namespace State {
type: 'success' | 'warning' | 'alert'
timestamp: number
content: string
meta?: { [key: string]: string }
}
interface Send {
txID: string
Expand Down
13 changes: 11 additions & 2 deletions packages/neuron-ui/src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { ckbCore } from 'services/chain'
import { ADDRESS_LENGTH, MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH, MIN_AMOUNT } from './const'

export const verifyAddress = (address: string): boolean => {
export const verifyAddress = (address: string): boolean | string => {
// TODO: verify address, prd required
return address.length === ADDRESS_LENGTH
try {
if (address.length !== ADDRESS_LENGTH) {
throw new Error('Address length is incorrect')
}
ckbCore.utils.parseAddress(address)
return true
} catch (err) {
return err.message
}
}

export const verifyAmountRange = (amount: string) => {
Expand Down

0 comments on commit 4d88216

Please sign in to comment.