Skip to content

Commit fb46ac5

Browse files
authored
Update README.md
1 parent 4483083 commit fb46ac5

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
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

0 commit comments

Comments
 (0)