trustwallet / trust-wallet-ios

:iphone: Trust - Ethereum Wallet and Web3 DApp Browser for iOS
https://trustwallet.com
GNU General Public License v3.0
1.52k stars 721 forks source link

When import KeyStore file , address changed #899

Closed zhaojiewen closed 6 years ago

zhaojiewen commented 6 years ago

Replacing this Method in KeyStore.swift file with below code,this bug will disappear.

/// Imports an encrypted JSON key. /// /// - Parameters: /// - key: key to import /// - password: key password /// - newPassword: password to use for the imported key /// - Returns: new account public func import(json: Data, password: String, newPassword: String) throws -> Account { let key = try JSONDecoder().decode(KeystoreKey.self, from: json) if self.account(for: key.address) != nil { throw Error.accountAlreadyExists }

    var privateKey = try key.decrypt(password: password)
    print(privateKey.hexString)
    defer {
        privateKey.resetBytes(in: 0..<privateKey.count)
    }

    var newKey : KeystoreKey

    switch key.type {
    case .encryptedKey:
        newKey = try KeystoreKey(password: newPassword, key: privateKey)
    case .hierarchicalDeterministicWallet:
        guard let mnemonic = String(data: privateKey, encoding: .ascii) else {
            throw EncryptError.invalidMnemonic
        }
        newKey = try KeystoreKey(password: newPassword, mnemonic: mnemonic)
    }

    keysByAddress[key.address] = newKey
    let url = makeAccountURL(for: key.address)
    let account = Account(address: newKey.address, type: key.type, url: url)
    try save(account: account, in: keyDirectory)
    accountsByAddress[newKey.address] = account

    return account

}