1
- // web3swift
2
1
//
3
2
// Created by Alex Vlasov.
4
3
// Copyright © 2018 Alex Vlasov. All rights reserved.
@@ -71,7 +70,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
71
70
}
72
71
}
73
72
74
- public init ? ( password: String = " web3swift " , aesMode: String = " aes-128-cbc " ) throws {
73
+ public init ? ( password: String , aesMode: String = " aes-128-cbc " ) throws {
75
74
guard var newPrivateKey = SECP256K1 . generatePrivateKey ( ) else {
76
75
return nil
77
76
}
@@ -81,7 +80,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
81
80
try encryptDataToStorage ( password, keyData: newPrivateKey, aesMode: aesMode)
82
81
}
83
82
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 {
85
84
guard privateKey. count == 32 else {
86
85
return nil
87
86
}
@@ -95,14 +94,18 @@ public class EthereumKeystoreV3: AbstractKeystore {
95
94
if keyData == nil {
96
95
throw AbstractKeystoreError . encryptionError ( " Encryption without key data " )
97
96
}
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
+ }
100
101
guard let derivedKey = scrypt ( password: password, salt: saltData, length: dkLen, N: N, R: R, P: P) else {
101
102
throw AbstractKeystoreError . keyDerivationError
102
103
}
103
104
let last16bytes = Data ( derivedKey [ ( derivedKey. count - 16 ) ... ( derivedKey. count - 1 ) ] )
104
105
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
+ }
106
109
var aesCipher : AES ?
107
110
switch aesMode {
108
111
case " aes-128-cbc " :
0 commit comments