Skip to content

Commit 4249965

Browse files
Merge pull request #397 from skywinder/minor-keystore-fix
Fixed keystore backward compatibility
2 parents 963f3c8 + 44e7349 commit 4249965

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

Sources/web3swift/KeystoreManager/BIP32Keystore.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ public class BIP32Keystore: AbstractKeystore {
1919
public var keystoreParams: KeystoreParamsBIP32?
2020

2121
@available(*, deprecated, message: "Please use addressStorage instead")
22-
public var paths: [String: EthereumAddress] = [String: EthereumAddress]()
22+
public var paths: [String: EthereumAddress] {
23+
get {
24+
return self.addressStorage.toPathAddressPairs().reduce(into: [String: EthereumAddress]()) {
25+
$0[$1.path] = EthereumAddress($1.address)!
26+
}
27+
}
28+
set {
29+
for pair in newValue {
30+
self.addressStorage.add(address: pair.value, for: pair.key)
31+
}
32+
}
33+
}
2334

2435
public var rootPrefix: String
2536

@@ -73,9 +84,6 @@ public class BIP32Keystore: AbstractKeystore {
7384
if (keystorePars.crypto.version != nil && keystorePars.crypto.version != "1") {return nil}
7485
if (!keystorePars.isHDWallet) {return nil}
7586

76-
for (p, ad) in keystorePars.pathToAddress {
77-
paths[p] = EthereumAddress(ad)
78-
}
7987
addressStorage = PathAddressStorage(pathAddressPairs: keystorePars.pathAddressPairs)
8088

8189
if keystorePars.rootPath == nil {
@@ -195,7 +203,6 @@ public class BIP32Keystore: AbstractKeystore {
195203
} else {
196204
newPath = prefixPath + "/" + pathAppendix!
197205
}
198-
paths[newPath] = newAddress
199206
addressStorage.add(address: newAddress, for: newPath)
200207
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {throw AbstractKeystoreError.keyDerivationError}
201208
try encryptDataToStorage(password, data: serializedRootNode, aesMode: self.keystoreParams!.crypto.cipher)
@@ -244,10 +251,7 @@ public class BIP32Keystore: AbstractKeystore {
244251
let kdfparams = KdfParamsV3(salt: saltData.toHexString(), dklen: dkLen, n: N, p: P, r: R, c: nil, prf: nil)
245252
let cipherparams = CipherParamsV3(iv: IV.toHexString())
246253
let crypto = CryptoParamsV3(ciphertext: encryptedKeyData.toHexString(), cipher: aesMode, cipherparams: cipherparams, kdf: "scrypt", kdfparams: kdfparams, mac: mac.toHexString(), version: nil)
247-
var pathToAddress = [String: String]()
248-
for (path, address) in paths {
249-
pathToAddress[path] = address.address
250-
}
254+
251255
var keystorePars = KeystoreParamsBIP32(crypto: crypto, id: UUID().uuidString.lowercased(), version: Self.KeystoreParamsBIP32Version)
252256
keystorePars.pathAddressPairs = addressStorage.toPathAddressPairs()
253257
keystorePars.rootPath = self.rootPrefix

Sources/web3swift/KeystoreManager/KeystoreParams.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ public struct KeystoreParamsBIP32: AbstractKeystoreParams {
5050
public var isHDWallet: Bool
5151

5252
@available(*, deprecated, message: "Please use pathAddressPairs instead")
53-
var pathToAddress: [String: String]
53+
var pathToAddress: [String: String] {
54+
get {
55+
return self.pathAddressPairs.reduce(into: [String: String]()) {
56+
$0[$1.path] = $1.address
57+
}
58+
}
59+
set {
60+
for pair in newValue {
61+
self.pathAddressPairs.append(PathAddressPair(path: pair.0, address: pair.1))
62+
}
63+
}
64+
}
5465

5566
var pathAddressPairs: [PathAddressPair]
5667
var rootPath: String?
@@ -59,7 +70,6 @@ public struct KeystoreParamsBIP32: AbstractKeystoreParams {
5970
self.crypto = cr
6071
self.id = i
6172
self.version = ver
62-
pathToAddress = [String: String]()
6373
pathAddressPairs = [PathAddressPair]()
6474
self.rootPath = rootPath
6575
self.isHDWallet = true

0 commit comments

Comments
 (0)