Skip to content

Commit 1bc213a

Browse files
committed
feat: strengthen address validation
1 parent 22ab3d8 commit 1bc213a

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

packages/neuron-ui/src/utils/validators.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { ckbCore } from 'services/chain'
33
import { MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH, MIN_AMOUNT, MAX_DECIMAL_DIGITS, ErrorCode } from './const'
44

55
export const verifyAddress = (address: string): boolean => {
6+
if (typeof address !== 'string' || address.length !== 46) {
7+
return false
8+
}
69
try {
710
return ckbCore.utils.parseAddress(address, 'hex').startsWith('0x0100')
811
} catch (err) {

packages/neuron-wallet/src/controllers/wallets.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs'
2+
import { parseAddress } from '@nervosnetwork/ckb-sdk-utils'
23
import { dialog, SaveDialogReturnValue, BrowserWindow } from 'electron'
34
import WalletsService, { Wallet, WalletProperties, FileKeystoreWallet } from 'services/wallets'
45
import Keystore from 'models/keys/keystore'
@@ -15,6 +16,7 @@ import {
1516
EmptyPassword,
1617
IncorrectPassword,
1718
InvalidJSON,
19+
InvalidAddress,
1820
} from 'exceptions'
1921
import i18n from 'utils/i18n'
2022
import AddressService from 'services/addresses'
@@ -338,6 +340,13 @@ export default class WalletsController {
338340
if (!params.fee || params.fee === '0') {
339341
feeRate = '1000'
340342
}
343+
344+
params.items.forEach(item => {
345+
if (!this.verifyAddress(item.address)) {
346+
throw new InvalidAddress(item.address)
347+
}
348+
})
349+
341350
const walletsService = WalletsService.getInstance()
342351
const hash = await walletsService.sendCapacity(
343352
params.walletID,
@@ -435,4 +444,15 @@ export default class WalletsController {
435444
},
436445
}
437446
}
447+
448+
private static verifyAddress = (address: string): boolean => {
449+
if (typeof address !== 'string' || address.length !== 46) {
450+
return false
451+
}
452+
try {
453+
return parseAddress(address, 'hex').startsWith('0x0100')
454+
} catch (err) {
455+
return false
456+
}
457+
}
438458
}

0 commit comments

Comments
 (0)