Skip to content

Commit 252163e

Browse files
committed
Add path validation
1 parent 86a2e8d commit 252163e

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

src/modules/select/wallets/hd-wallet.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import HDKey from 'hdkey'
22
import { publicToAddress, toChecksumAddress } from 'ethereumjs-util'
33
import buffer from 'buffer'
44

5-
const numberToGet = 30
5+
const numberToGet = 5
66

77
export function generateAddresses(
88
account: {
@@ -23,6 +23,7 @@ export function generateAddresses(
2323
for (let i = offset; i < numberToGet + offset; i++) {
2424
const dkey = hdk.deriveChild(i)
2525
const address = publicToAddress(dkey.publicKey, true).toString('hex')
26+
2627
addresses.push({
2728
dPath: `${path}/${i}`,
2829
address: toChecksumAddress(address)
@@ -32,25 +33,54 @@ export function generateAddresses(
3233
return addresses
3334
}
3435

35-
// import * as hdKey from 'ethereumjs-wallet/hdkey'
36+
export function isValidPath(path: string) {
37+
const parts = path.split('/')
3638

37-
// export function generateAddresses(xpub: string, basePath: string, amount = 30) {
38-
// const node = new hdKey.fromExtendedKey(xpub)
39+
if (parts[0] !== 'm') {
40+
return false
41+
}
3942

40-
// const children = []
43+
if (parts[1] !== "44'") {
44+
return false
45+
}
4146

42-
// for (let i = 0; i < amount; i++) {
43-
// children.push({ childNode: node.deriveChild(i), dPath: `${basePath}/${i}` })
44-
// }
47+
if (parts[2] !== "60'" && parts[2] !== "1'") {
48+
return false
49+
}
4550

46-
// console.log({ children })
51+
if (parts[3] === undefined) {
52+
return true
53+
}
4754

48-
// const addresses = children.map(({ childNode, dPath }) => ({
49-
// address: childNode.getWallet().getChecksumAddressString(),
50-
// dPath
51-
// }))
55+
const accountFieldDigit = Number(parts[3][0])
56+
57+
if (
58+
isNaN(accountFieldDigit) ||
59+
accountFieldDigit < 0 ||
60+
parts[3][1] !== "'"
61+
) {
62+
return false
63+
}
64+
65+
if (parts[4] === undefined) {
66+
return true
67+
}
5268

53-
// console.log({ addresses })
69+
const changeFieldDigit = Number(parts[4][0])
5470

55-
// return addresses
56-
// }
71+
if (isNaN(changeFieldDigit) || changeFieldDigit < 0) {
72+
return false
73+
}
74+
75+
if (parts[5] === undefined) {
76+
return true
77+
}
78+
79+
const addressFieldDigit = Number(parts[5][0])
80+
81+
if (isNaN(addressFieldDigit) || addressFieldDigit < 0) {
82+
return false
83+
}
84+
85+
return true
86+
}

0 commit comments

Comments
 (0)