1
- import crypto from "crypto" ;
1
+ import {
2
+ Cipher ,
3
+ ScryptOptions ,
4
+ createCipheriv ,
5
+ createDecipheriv ,
6
+ } from "crypto" ;
2
7
import { Keccak } from "sha3" ;
3
8
import { v4 as uuid } from "uuid" ;
4
9
import { ExtendedPrivateKey } from "./extended_key" ;
10
+ import { randomBytes } from "@ckb-lumos/crypto" ;
5
11
import { HexString } from "@ckb-lumos/base" ;
6
12
import { syncScrypt } from "scrypt-js" ;
7
13
@@ -94,8 +100,8 @@ export default class Keystore {
94
100
// Create an empty keystore object that contains empty private key
95
101
static createEmpty ( ) : Keystore {
96
102
const saltSize = 32 ;
97
- const salt : Buffer = crypto . randomBytes ( saltSize ) ;
98
- const iv : Buffer = crypto . randomBytes ( 16 ) ;
103
+ const salt : Buffer = Buffer . from ( randomBytes ( saltSize ) ) ;
104
+ const iv : Buffer = Buffer . from ( randomBytes ( 16 ) ) ;
99
105
const kdfparams : KdfParams = {
100
106
dklen : 32 ,
101
107
salt : salt . toString ( "hex" ) ,
@@ -125,8 +131,8 @@ export default class Keystore {
125
131
) : Keystore {
126
132
const saltSize = 32 ;
127
133
const ivSize = 16 ;
128
- const salt : Buffer = options . salt || crypto . randomBytes ( saltSize ) ;
129
- const iv : Buffer = options . iv || crypto . randomBytes ( ivSize ) ;
134
+ const salt : Buffer = options . salt || Buffer . from ( randomBytes ( saltSize ) ) ;
135
+ const iv : Buffer = options . iv || Buffer . from ( randomBytes ( ivSize ) ) ;
130
136
const kdfparams : KdfParams = {
131
137
dklen : 32 ,
132
138
salt : salt . toString ( "hex" ) ,
@@ -145,11 +151,7 @@ export default class Keystore {
145
151
)
146
152
) ;
147
153
148
- const cipher : crypto . Cipher = crypto . createCipheriv (
149
- CIPHER ,
150
- derivedKey . slice ( 0 , 16 ) ,
151
- iv
152
- ) ;
154
+ const cipher : Cipher = createCipheriv ( CIPHER , derivedKey . slice ( 0 , 16 ) , iv ) ;
153
155
if ( ! cipher ) {
154
156
throw new UnsupportedCipher ( ) ;
155
157
}
@@ -190,7 +192,7 @@ export default class Keystore {
190
192
if ( Keystore . mac ( derivedKey , ciphertext ) !== this . crypto . mac ) {
191
193
throw new IncorrectPassword ( ) ;
192
194
}
193
- const decipher = crypto . createDecipheriv (
195
+ const decipher = createDecipheriv (
194
196
this . crypto . cipher ,
195
197
derivedKey . slice ( 0 , 16 ) ,
196
198
Buffer . from ( this . crypto . cipherparams . iv , "hex" )
@@ -239,7 +241,7 @@ export default class Keystore {
239
241
) ;
240
242
}
241
243
242
- static scryptOptions ( kdfparams : KdfParams ) : crypto . ScryptOptions {
244
+ static scryptOptions ( kdfparams : KdfParams ) : ScryptOptions {
243
245
return {
244
246
N : kdfparams . n ,
245
247
r : kdfparams . r ,
0 commit comments