@@ -11,6 +11,7 @@ var sjcl = require("sjcl");
11
11
var uuid = require ( "uuid" ) ;
12
12
var secp256k1 = require ( "secp256k1/elliptic" ) ;
13
13
var createKeccakHash = require ( "keccak/js" ) ;
14
+ var scrypt = require ( "scrypt-js" ) ;
14
15
15
16
function isFunction ( f ) {
16
17
return typeof f === "function" ;
@@ -152,7 +153,9 @@ module.exports = {
152
153
privateKeyBuffer
153
154
] ) ;
154
155
}
155
- publicKey = secp256k1 . publicKeyCreate ( privateKeyBuffer , false ) . slice ( 1 ) ;
156
+ publicKey = Buffer . from (
157
+ secp256k1 . publicKeyCreate ( privateKeyBuffer , false ) . slice ( 1 )
158
+ ) ;
156
159
return "0x" + keccak256 ( publicKey ) . slice ( - 20 ) . toString ( "hex" ) ;
157
160
} ,
158
161
@@ -177,44 +180,21 @@ module.exports = {
177
180
/**
178
181
* Used internally.
179
182
*/
180
- deriveKeyUsingScryptInNode : function ( password , salt , options , cb ) {
181
- if ( ! isFunction ( cb ) ) return this . deriveKeyUsingScryptInBrowser ( password , salt , options ) ;
182
- require ( "scrypt" ) . hash ( password , {
183
- N : options . kdfparams . n || this . constants . scrypt . n ,
184
- r : options . kdfparams . r || this . constants . scrypt . r ,
185
- p : options . kdfparams . p || this . constants . scrypt . p
186
- } , options . kdfparams . dklen || this . constants . scrypt . dklen , salt ) . then ( cb ) . catch ( cb ) ;
187
- } ,
188
-
189
- /**
190
- * Used internally.
191
- */
192
- deriveKeyUsingScryptInBrowser : function ( password , salt , options , cb ) {
193
- var self = this ;
194
- if ( this . scrypt === null ) this . scrypt = require ( "./lib/scrypt" ) ;
195
- if ( isFunction ( this . scrypt ) ) {
196
- this . scrypt = this . scrypt ( options . kdfparams . memory || this . constants . scrypt . memory ) ;
197
- }
198
- if ( ! isFunction ( cb ) ) {
199
- return Buffer . from ( this . scrypt . to_hex ( this . scrypt . crypto_scrypt (
200
- password ,
201
- salt ,
202
- options . kdfparams . n || this . constants . scrypt . n ,
203
- options . kdfparams . r || this . constants . scrypt . r ,
204
- options . kdfparams . p || this . constants . scrypt . p ,
205
- options . kdfparams . dklen || this . constants . scrypt . dklen
206
- ) ) , "hex" ) ;
183
+ deriveKeyUsingScrypt : function ( password , salt , options , cb ) {
184
+ var n = options . kdfparams . n || this . constants . scrypt . n ;
185
+ var r = options . kdfparams . r || this . constants . scrypt . r ;
186
+ var p = options . kdfparams . p || this . constants . scrypt . p ;
187
+ var dklen = options . kdfparams . dklen || this . constants . scrypt . dklen ;
188
+ if ( isFunction ( cb ) ) {
189
+ scrypt
190
+ . scrypt ( password , salt , n , r , p , dklen )
191
+ . then ( function ( key ) {
192
+ cb ( Buffer . from ( key ) ) ;
193
+ } )
194
+ . catch ( cb ) ;
195
+ } else {
196
+ return Buffer . from ( scrypt . syncScrypt ( password , salt , n , r , p , dklen ) ) ;
207
197
}
208
- setTimeout ( function ( ) {
209
- cb ( Buffer . from ( self . scrypt . to_hex ( self . scrypt . crypto_scrypt (
210
- password ,
211
- salt ,
212
- options . kdfparams . n || self . constants . scrypt . n ,
213
- options . kdfparams . r || self . constants . scrypt . r ,
214
- options . kdfparams . p || self . constants . scrypt . p ,
215
- options . kdfparams . dklen || self . constants . scrypt . dklen
216
- ) ) , "hex" ) ) ;
217
- } , 0 ) ;
218
198
} ,
219
199
220
200
/**
@@ -242,8 +222,7 @@ module.exports = {
242
222
243
223
// use scrypt as key derivation function
244
224
if ( options . kdf === "scrypt" ) {
245
- if ( ! this . browser ) return this . deriveKeyUsingScryptInNode ( password , salt , options , cb ) ;
246
- return this . deriveKeyUsingScryptInBrowser ( password , salt , options , cb ) ;
225
+ return this . deriveKeyUsingScrypt ( password , salt , options , cb ) ;
247
226
}
248
227
249
228
// use default key derivation function (PBKDF2)
0 commit comments