Skip to content

Commit c7677e5

Browse files
committed
Merge branch 'master' into develop
2 parents 474d549 + fb46ac5 commit c7677e5

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,32 +124,61 @@ $ pod install
124124
- [x] Manage user's private keys through encrypted keystore abstractions
125125
- [x] Batched requests in concurrent mode, checks balances of 580 tokens (from the latest MyEtherWallet repo) over 3 seconds
126126

127-
## Usage
127+
## Getting started
128128

129-
Here's a few use cases of our library:
129+
Here's a few use cases:
130130

131131
### Create Account
132-
Create keystore and account with password.
133132

134-
```
133+
```swift
134+
// Create keystore and account with password.
135+
135136
let keystore = try! EthereumKeystoreV3(password: "changeme"); // generates a private key internally if node "privateKey" parameter supplied
136137
let account = keystore!.addresses![0]
137138
print(account)
139+
138140
let data = try! keystore!.serialize() // internally serializes to JSON
139141
print(try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue:0)))
140-
let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "changeme", account: account) // you should rarely use this and expose a key manually
142+
let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "changeme", account: account) // you should rarely use this and expose a key manually
141143
```
142144

143-
### Initializing Ethereum address
145+
### Save keystore to the memory
146+
147+
```swift
148+
//First you need a `KeystoreManager` instance:
149+
guard let userDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first,
150+
let keystoreManager = KeystoreManager.managerForPath(userDirectory + "/keystore")
151+
else {
152+
fatalError("Couldn't create a KeystoreManager.")
153+
}
154+
155+
//Next you create the a new Keystore:
156+
157+
let newKeystore = try? EthereumKeystoreV3(password: "YOUR_PASSWORD")
158+
159+
// Then you save the created keystore to the file system:
160+
161+
let newKeystoreJSON = try? JSONEncoder().encode(newKeystore.keystoreParams)
162+
FileManager.default.createFile(atPath: "\(keystoreManager.path)/keystore.json", contents: newKeystoreJSON, attributes: nil)
163+
164+
// Later you can retreive it:
165+
166+
if let address = keystoreManager.addresses?.first,
167+
let retrievedKeystore = keystoreManager.walletForAddress(address) as? EthereumKeystoreV3 {
168+
return retrievedKeystore
169+
}
144170
```
171+
172+
### Initializing Ethereum address
173+
```swift
145174
let coldWalletAddress = EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")
146175
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b", ignoreChecksum: true)
147176
```
148177
Ethereum addresses are checksum checked if they are not lowercased or uppercased and always length checked
149178

150179
### Setting options
151180

152-
```
181+
```swift
153182
var options = Web3Options.defaultOptions()
154183
// public var to: EthereumAddress? = nil - to what address transaction is aimed
155184
// public var from: EthereumAddress? = nil - form what address it should be sent (either signed locally or on the node)
@@ -173,21 +202,21 @@ options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")
173202
//TODO
174203
```
175204
### Getting ETH balance
176-
```
205+
```swift
177206
let address = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")!
178207
let web3Main = Web3.InfuraMainnetWeb3()
179208
let balanceResult = web3Main.eth.getBalance(address)
180209
guard case .success(let balance) = balanceResult else {return}
181210
```
182211
### Getting gas price
183-
```
212+
```swift
184213
let web3Main = Web3.InfuraMainnetWeb3()
185214
let gasPriceResult = web3Main.eth.getGasPrice()
186215
guard case .success(let gasPrice) = gasPriceResult else {return}
187216
```
188217

189218
### Sending ETH
190-
```
219+
```swift
191220
let web3Rinkeby = Web3.InfuraRinkebyWeb3()
192221
web3Rinkeby.addKeystoreManager(bip32keystoreManager) // attach a keystore if you want to sign locally. Otherwise unsigned request will be sent to remote node
193222
options.from = bip32ks?.addresses?.first! // specify from what address you want to send it
@@ -198,7 +227,7 @@ let sendResultBip32 = intermediateSend.send(password: "changeme")
198227
### ERC20 Iteraction:
199228

200229
#### Getting ERC20 token balance
201-
```
230+
```swift
202231
let contractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")! // BKX token on Ethereum mainnet
203232
let contract = web3.contract(Web3.Utils.erc20ABI, at: contractAddress, abiVersion: 2)! // utilize precompiled ERC20 ABI for your concenience
204233
guard let bkxBalanceResult = contract.method("balanceOf", parameters: [coldWalletAddress] as [AnyObject], options: options)?.call(options: nil) else {return} // encode parameters for transaction
@@ -207,7 +236,7 @@ print("BKX token balance = " + String(bal))
207236
```
208237

209238
#### Sending ERC20
210-
```
239+
```swift
211240
var convenienceTransferOptions = Web3Options.defaultOptions()
212241
convenienceTransferOptions.gasPrice = gasPriceRinkeby
213242
let convenienceTokenTransfer = web3Rinkeby.eth.sendERC20tokensWithNaturalUnits(tokenAddress: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a")!, from: (ks?.addresses?.first!)!, to: EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")!, amount: "0.0001", options: convenienceTransferOptions) // there are also convenience functions to send ETH and ERC20 under the .eth structure

web3swift/ABIv2/Classes/ABIv2Elements.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010
import BigInt
1111

12-
extension ABIv2 {
12+
public extension ABIv2 {
1313
// JSON Decoding
1414
public struct Input: Decodable {
1515
var name: String?
@@ -48,16 +48,16 @@ extension ABIv2 {
4848
case event(Event)
4949

5050
public struct InOut {
51-
let name: String
52-
let type: ParameterType
51+
public let name: String
52+
public let type: ParameterType
5353
}
5454

5555
public struct Function {
56-
let name: String?
57-
let inputs: [InOut]
58-
let outputs: [InOut]
59-
let constant: Bool
60-
let payable: Bool
56+
public let name: String?
57+
public let inputs: [InOut]
58+
public let outputs: [InOut]
59+
public let constant: Bool
60+
public let payable: Bool
6161
}
6262

6363
public struct Constructor {

web3swift/Utils/Classes/EIP681.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ extension Web3 {
116116

117117
var code = EIP681Code(targetAddress)
118118
if chainIDString != nil {
119+
chainIDString!.remove(at: chainIDString!.startIndex)
119120
code.chainID = BigUInt(chainIDString!)
120121
}
121122
if tail == nil {
@@ -137,7 +138,13 @@ extension Web3 {
137138
switch inputType {
138139
case .address:
139140
let val = EIP681Code.TargetAddress(value)
140-
nativeValue = val as AnyObject
141+
switch val {
142+
case .ethereumAddress(let ethereumAddress):
143+
nativeValue = ethereumAddress as AnyObject
144+
case .ensAddress(let ens):
145+
//TODO: - convert ens into ethereum
146+
nativeValue = ens as AnyObject
147+
}
141148
case .uint(bits: _):
142149
if let val = BigUInt(value, radix: 10) {
143150
nativeValue = val as AnyObject
@@ -164,6 +171,15 @@ extension Web3 {
164171
} else if let val = value.data(using: .utf8) {
165172
nativeValue = val as AnyObject
166173
}
174+
case .bool:
175+
switch value {
176+
case "true","True","1":
177+
nativeValue = true as AnyObject
178+
case "false", "False", "0":
179+
nativeValue = false as AnyObject
180+
default:
181+
nativeValue = true as AnyObject
182+
}
167183
default:
168184
continue
169185
}

web3swift/Web3/Classes/Web3+Protocols.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public enum Networks {
4040
case Kovan
4141
case Custom(networkID: BigUInt)
4242

43-
var name: String {
43+
public var name: String {
4444
switch self {
4545
case .Rinkeby: return "rinkeby"
4646
case .Ropsten: return "ropsten"
@@ -50,7 +50,7 @@ public enum Networks {
5050
}
5151
}
5252

53-
var chainID: BigUInt {
53+
public var chainID: BigUInt {
5454
switch self {
5555
case .Custom(let networkID): return networkID
5656
case .Mainnet: return BigUInt(1)

0 commit comments

Comments
 (0)