@@ -2,7 +2,7 @@ import HDKey from 'hdkey'
2
2
import { publicToAddress , toChecksumAddress } from 'ethereumjs-util'
3
3
import buffer from 'buffer'
4
4
5
- const numberToGet = 30
5
+ const numberToGet = 5
6
6
7
7
export function generateAddresses (
8
8
account : {
@@ -23,6 +23,7 @@ export function generateAddresses(
23
23
for ( let i = offset ; i < numberToGet + offset ; i ++ ) {
24
24
const dkey = hdk . deriveChild ( i )
25
25
const address = publicToAddress ( dkey . publicKey , true ) . toString ( 'hex' )
26
+
26
27
addresses . push ( {
27
28
dPath : `${ path } /${ i } ` ,
28
29
address : toChecksumAddress ( address )
@@ -32,25 +33,54 @@ export function generateAddresses(
32
33
return addresses
33
34
}
34
35
35
- // import * as hdKey from 'ethereumjs-wallet/hdkey'
36
+ export function isValidPath ( path : string ) {
37
+ const parts = path . split ( '/' )
36
38
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
+ }
39
42
40
- // const children = []
43
+ if ( parts [ 1 ] !== "44'" ) {
44
+ return false
45
+ }
41
46
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
+ }
45
50
46
- // console.log({ children })
51
+ if ( parts [ 3 ] === undefined ) {
52
+ return true
53
+ }
47
54
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
+ }
52
68
53
- // console.log({ addresses } )
69
+ const changeFieldDigit = Number ( parts [ 4 ] [ 0 ] )
54
70
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