1
+ /* globals localStorage */
1
2
'use strict'
2
3
3
4
const EC = require ( 'elliptic' ) . ec
@@ -11,7 +12,9 @@ class Keystore {
11
12
}
12
13
13
14
hasKey ( id ) {
14
- if ( ! id ) throw new Error ( 'id needed to check a key' )
15
+ if ( ! id ) {
16
+ throw new Error ( 'id needed to check a key' )
17
+ }
15
18
let hasKey = false
16
19
let storedKey = this . _cache . get ( id ) || this . _storage . getItem ( id )
17
20
try {
@@ -24,13 +27,15 @@ class Keystore {
24
27
}
25
28
26
29
createKey ( id ) {
27
- if ( ! id ) throw new Error ( 'id needed to create a key' )
30
+ if ( ! id ) {
31
+ throw new Error ( 'id needed to create a key' )
32
+ }
28
33
29
34
const keyPair = ec . genKeyPair ( )
30
35
31
36
const key = {
32
37
publicKey : keyPair . getPublic ( 'hex' ) ,
33
- privateKey : keyPair . getPrivate ( 'hex' ) ,
38
+ privateKey : keyPair . getPrivate ( 'hex' )
34
39
}
35
40
36
41
this . _storage . setItem ( id , JSON . stringify ( key ) )
@@ -40,7 +45,9 @@ class Keystore {
40
45
}
41
46
42
47
getKey ( id ) {
43
- if ( ! id ) throw new Error ( 'id needed to get a key' )
48
+ if ( ! id ) {
49
+ throw new Error ( 'id needed to get a key' )
50
+ }
44
51
const cachedKey = this . _cache . get ( id )
45
52
let storedKey
46
53
try {
@@ -49,46 +56,59 @@ class Keystore {
49
56
// ignore ENOENT error
50
57
}
51
58
52
- if ( ! storedKey )
59
+ if ( ! storedKey ) {
53
60
return
61
+ }
54
62
55
63
const deserializedKey = cachedKey || JSON . parse ( storedKey )
56
64
57
- if ( ! deserializedKey )
65
+ if ( ! deserializedKey ) {
58
66
return
67
+ }
59
68
60
- if ( ! cachedKey )
69
+ if ( ! cachedKey ) {
61
70
this . _cache . set ( id , deserializedKey )
71
+ }
62
72
63
73
const key = ec . keyPair ( {
64
- pub : deserializedKey . publicKey ,
74
+ pub : deserializedKey . publicKey ,
65
75
priv : deserializedKey . privateKey ,
66
76
pubEnc : 'hex' ,
67
- privEnc : 'hex' ,
77
+ privEnc : 'hex'
68
78
} )
69
79
70
80
return key
71
81
}
72
82
73
- sign ( key , data ) {
74
- if ( ! key ) throw new Error ( 'No signing key given' )
75
- if ( ! data ) throw new Error ( 'Given input data was undefined' )
83
+ sign ( key , data ) {
84
+ if ( ! key ) {
85
+ throw new Error ( 'No signing key given' )
86
+ }
87
+ if ( ! data ) {
88
+ throw new Error ( 'Given input data was undefined' )
89
+ }
76
90
const sig = ec . sign ( data , key )
77
91
return Promise . resolve ( sig . toDER ( 'hex' ) )
78
92
}
79
93
80
- verify ( signature , publicKey , data ) {
94
+ verify ( signature , publicKey , data ) {
81
95
return Keystore . verify ( signature , publicKey , data )
82
96
}
83
97
84
- static verify ( signature , publicKey , data ) {
85
- if ( ! signature ) throw new Error ( 'No signature given' )
86
- if ( ! publicKey ) throw new Error ( 'Given publicKey was undefined' )
87
- if ( ! data ) throw new Error ( 'Given input data was undefined' )
98
+ static verify ( signature , publicKey , data ) {
99
+ if ( ! signature ) {
100
+ throw new Error ( 'No signature given' )
101
+ }
102
+ if ( ! publicKey ) {
103
+ throw new Error ( 'Given publicKey was undefined' )
104
+ }
105
+ if ( ! data ) {
106
+ throw new Error ( 'Given input data was undefined' )
107
+ }
88
108
let res = false
89
109
const key = ec . keyPair ( {
90
- pub : publicKey ,
91
- pubEnc : 'hex' ,
110
+ pub : publicKey ,
111
+ pubEnc : 'hex'
92
112
} )
93
113
try {
94
114
res = ec . verify ( data , signature , key )
0 commit comments