Skip to content

Commit a9b4a46

Browse files
committed
Update EthereumKeystoreV3.swift
1 parent 037f227 commit a9b4a46

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Sources/Web3Core/KeystoreManager/EthereumKeystoreV3.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// web3swift
21
//
32
// Created by Alex Vlasov.
43
// Copyright © 2018 Alex Vlasov. All rights reserved.
@@ -71,7 +70,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
7170
}
7271
}
7372

74-
public init?(password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
73+
public init?(password: String, aesMode: String = "aes-128-cbc") throws {
7574
guard var newPrivateKey = SECP256K1.generatePrivateKey() else {
7675
return nil
7776
}
@@ -81,7 +80,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
8180
try encryptDataToStorage(password, keyData: newPrivateKey, aesMode: aesMode)
8281
}
8382

84-
public init?(privateKey: Data, password: String = "web3swift", aesMode: String = "aes-128-cbc") throws {
83+
public init?(privateKey: Data, password: String, aesMode: String = "aes-128-cbc") throws {
8584
guard privateKey.count == 32 else {
8685
return nil
8786
}
@@ -95,14 +94,18 @@ public class EthereumKeystoreV3: AbstractKeystore {
9594
if keyData == nil {
9695
throw AbstractKeystoreError.encryptionError("Encryption without key data")
9796
}
98-
let saltLen = 32;
99-
let saltData = Data.randomBytes(length: saltLen)!
97+
let saltLen = 32
98+
guard let saltData = Data.randomBytes(length: saltLen) else {
99+
throw AbstractKeystoreError.noEntropyError
100+
}
100101
guard let derivedKey = scrypt(password: password, salt: saltData, length: dkLen, N: N, R: R, P: P) else {
101102
throw AbstractKeystoreError.keyDerivationError
102103
}
103104
let last16bytes = Data(derivedKey[(derivedKey.count - 16)...(derivedKey.count - 1)])
104105
let encryptionKey = Data(derivedKey[0...15])
105-
let IV = Data.randomBytes(length: 16)!
106+
guard let IV = Data.randomBytes(length: 16) else {
107+
throw AbstractKeystoreError.noEntropyError
108+
}
106109
var aesCipher: AES?
107110
switch aesMode {
108111
case "aes-128-cbc":

0 commit comments

Comments
 (0)