Skip to content

Commit 49c569f

Browse files
authored
Merge pull request #70 from BANKEX/develop
Fix BIP32 keystore when used through Manager
2 parents 093c08d + 7ffcc6a commit 49c569f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

Example/web3swiftExample/web3swiftExample/ViewController.swift

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class ViewController: UIViewController {
1818
super.viewDidLoad()
1919

2020
let jsonString = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"version\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"},{\"name\":\"_extraData\",\"type\":\"bytes\"}],\"name\":\"approveAndCall\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"_initialAmount\",\"type\":\"uint256\"},{\"name\":\"_tokenName\",\"type\":\"string\"},{\"name\":\"_decimalUnits\",\"type\":\"uint8\"},{\"name\":\"_tokenSymbol\",\"type\":\"string\"}],\"type\":\"constructor\"},{\"payable\":false,\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},]"
21+
// create normal keystore
22+
2123
let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
2224
let keystoreManager = KeystoreManager.managerForPath(userDir + "/keystore")
2325
var ks: EthereumKeystoreV3?
@@ -31,6 +33,20 @@ class ViewController: UIViewController {
3133
guard let sender = ks?.addresses?.first else {return}
3234
print(sender)
3335

36+
//create BIP32 keystore
37+
let bip32keystoreManager = KeystoreManager.managerForPath(userDir + "/bip32_keystore")
38+
var bip32ks: BIP32Keystore?
39+
if (bip32keystoreManager?.addresses?.count == 0) {
40+
bip32ks = try! BIP32Keystore.init(mnemonics: "normal dune pole key case cradle unfold require tornado mercy hospital buyer", password: "BANKEXFOUNDATION", mnemonicsPassword: "", language: .english)
41+
let keydata = try! JSONEncoder().encode(bip32ks!.keystoreParams)
42+
FileManager.default.createFile(atPath: userDir + "/bip32_keystore"+"/key.json", contents: keydata, attributes: nil)
43+
} else {
44+
bip32ks = bip32keystoreManager?.walletForAddress((bip32keystoreManager?.addresses![0])!) as! BIP32Keystore
45+
}
46+
guard let bip32sender = bip32ks?.addresses?.first else {return}
47+
print(bip32sender)
48+
49+
3450
// BKX TOKEN
3551
let web3Main = Web3.InfuraMainnetWeb3()
3652
let coldWalletAddress = EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")
@@ -60,12 +76,12 @@ class ViewController: UIViewController {
6076
let encoding = eip67Data.toImage(scale: 10.0)
6177
self.imageView.image = UIImage(ciImage: encoding)
6278
self.imageView.contentMode = .scaleAspectFit
63-
//Send on Rinkeby
79+
80+
//Send on Rinkeby using normal keystore
6481

6582
let web3Rinkeby = Web3.InfuraRinkebyWeb3()
6683
web3Rinkeby.addKeystoreManager(keystoreManager)
6784
let coldWalletABI = "[{\"payable\":true,\"type\":\"fallback\"}]"
68-
6985
options = Web3Options.defaultOptions()
7086
options.gasLimit = BigUInt(21000)
7187
options.from = ks?.addresses?.first!
@@ -85,13 +101,24 @@ class ViewController: UIViewController {
85101
guard case .success(let sendingResult) = sendResult else {return}
86102
let txid = sendingResult["txhash"] as? String
87103
print("On Rinkeby TXid = " + txid!)
88-
104+
105+
//Send ETH on Rinkeby using BIP32 keystore. Should fail due to insufficient balance
106+
web3Rinkeby.addKeystoreManager(bip32keystoreManager)
107+
options.from = bip32ks?.addresses?.first!
108+
intermediateSend = web3Rinkeby.contract(coldWalletABI, at: coldWalletAddress, abiVersion: 2)!.method(options: options)!
109+
let sendResultBip32 = intermediateSend.send(password: "BANKEXFOUNDATION")
110+
switch sendResultBip32 {
111+
case .success(let r):
112+
print(r)
113+
case .failure(let err):
114+
print(err)
115+
}
116+
89117
//Balance on Rinkeby
90118
let balanceResult = web3Rinkeby.eth.getBalance(address: coldWalletAddress)
91119
guard case .success(let balance) = balanceResult else {return}
92120
print("Balance of " + coldWalletAddress.address + " = " + String(balance))
93-
94-
121+
95122
// Send mutating transaction taking parameters
96123
let testABIonRinkeby = "[{\"constant\":true,\"inputs\":[],\"name\":\"counter\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_value\",\"type\":\"uint8\"}],\"name\":\"increaseCounter\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"
97124
let deployedTestAddress = EthereumAddress("0x1e528b190b6acf2d7c044141df775c7a79d68eba")

web3swift/KeystoreManager/Classes/KeystoreManager.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public class KeystoreManager: AbstractKeystore {
5151
return keystore as AbstractKeystore?
5252
}
5353
}
54+
for keystore in _bip32keystores {
55+
guard let allAddresses = keystore.addresses else {continue}
56+
for addr in allAddresses {
57+
if addr == address && addr.isValid {
58+
return keystore as AbstractKeystore?
59+
}
60+
}
61+
}
5462
return nil
5563
}
5664

0 commit comments

Comments
 (0)