Skip to content

Commit ebc9f53

Browse files
JeneaVranceanucclauss
authored andcommitted
parent 3081a92
parent 2c3a236 author Jenea Vranceanu <[email protected]> 1675983082 +0200 committer Christian Clauss <[email protected]> 1676021928 +0100 parent 3081a92 parent 2c3a236 author Jenea Vranceanu <[email protected]> 1675983082 +0200 committer Christian Clauss <[email protected]> 1676021923 +0100 parent 3081a92 parent 2c3a236 author Jenea Vranceanu <[email protected]> 1675983082 +0200 committer Christian Clauss <[email protected]> 1676021917 +0100 Merge pull request #764 from JeneaVranceanu/fix/web3-wallet-example fix: web3 wallet example
2 parents 3081a92 + 2c3a236 commit ebc9f53

File tree

24 files changed

+190
-386
lines changed

24 files changed

+190
-386
lines changed

Example/myWeb3Wallet/Podfile

Lines changed: 0 additions & 30 deletions
This file was deleted.

Example/myWeb3Wallet/Podfile.lock

Lines changed: 0 additions & 53 deletions
This file was deleted.

Example/myWeb3Wallet/myWeb3Wallet.xcodeproj/project.pbxproj

Lines changed: 27 additions & 140 deletions
Large diffs are not rendered by default.

Example/myWeb3Wallet/myWeb3Wallet/ViewControllers/WalletController/WalletViewController.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import UIKit
99
import web3swift
10+
import Web3Core
11+
1012
class WalletViewController: UIViewController {
1113

1214
@IBOutlet weak var continueButton: UIButton!
@@ -46,9 +48,9 @@ class WalletViewController: UIViewController {
4648
@IBAction func onClickContinueButton(_ sender: UIButton) {
4749
print("Clicked on COntinue button")
4850
guard let dashboardScreen = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController") as? DashboardViewController else {
49-
#if DEBUG
51+
#if DEBUG
5052
printContent("Unable to get Wallet controller")
51-
#endif
53+
#endif
5254
return
5355
}
5456
self.navigationController?.pushViewController(dashboardScreen, animated: true)
@@ -83,7 +85,7 @@ class WalletViewController: UIViewController {
8385
return
8486
}
8587
do {
86-
let keystore = try EthereumKeystoreV3(privateKey: dataKey)
88+
let keystore = try EthereumKeystoreV3(privateKey: dataKey, password: "")
8789
if let myWeb3KeyStore = keystore {
8890
let manager = KeystoreManager([myWeb3KeyStore])
8991
let address = keystore?.addresses?.first
@@ -110,14 +112,13 @@ class WalletViewController: UIViewController {
110112
}
111113

112114
}
115+
113116
func importWalletWith(mnemonics: String) {
114-
let walletAddress = try? BIP32Keystore(mnemonics: mnemonics, prefixPath: "m/44'/77777'/0'/0")
115-
print(walletAddress?.addresses)
117+
let walletAddress = try? BIP32Keystore(mnemonics: mnemonics, password: "", prefixPath: "m/44'/77777'/0'/0")
116118
self.walletAddressLabel.text = "\(walletAddress?.addresses?.first?.address ?? "0x")"
117-
118119
}
119-
120120
}
121+
121122
extension WalletViewController {
122123

123124
fileprivate func createMnemonics() {
@@ -132,10 +133,10 @@ extension WalletViewController {
132133
}
133134
self._mnemonics = tMnemonics
134135
print(_mnemonics)
135-
let tempWalletAddress = try? BIP32Keystore(mnemonics: self._mnemonics, prefixPath: "m/44'/77777'/0'/0")
136-
print(tempWalletAddress?.addresses?.first?.address)
136+
137+
let tempWalletAddress = try? BIP32Keystore(mnemonics: self._mnemonics, password: "", prefixPath: "m/44'/77777'/0'/0")
137138
guard let walletAddress = tempWalletAddress?.addresses?.first else {
138-
self.showAlertMessage(title: "", message: "We are unable to create wallet", actionName: "Ok")
139+
self.showAlertMessage(title: "", message: "Unable to create wallet", actionName: "Ok")
139140
return
140141
}
141142
self._walletAddress = walletAddress.address
@@ -144,7 +145,7 @@ extension WalletViewController {
144145
print(privateKey, "Is the private key")
145146
#endif
146147
let keyData = try? JSONEncoder().encode(tempWalletAddress?.keystoreParams)
147-
FileManager.default.createFile(atPath: userDir + "/keystore"+"/key.json", contents: keyData, attributes: nil)
148+
FileManager.default.createFile(atPath: userDir + "/keystore" + "/key.json", contents: keyData, attributes: nil)
148149
}
149150
} catch {
150151

Sources/Web3Core/EthereumABI/ABIDecoding.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ extension ABIDecoder {
2121
var consumed: UInt64 = 0
2222
for i in 0 ..< types.count {
2323
let (v, c) = decodeSingleType(type: types[i], data: data, pointer: consumed)
24-
guard let valueUnwrapped = v, let consumedUnwrapped = c else {return nil}
24+
guard let valueUnwrapped = v, let consumedUnwrapped = c else { return nil }
2525
toReturn.append(valueUnwrapped)
2626
consumed = consumed + consumedUnwrapped
2727
}
28-
guard toReturn.count == types.count else {return nil}
28+
guard toReturn.count == types.count else { return nil }
2929
return toReturn
3030
}
3131

@@ -235,23 +235,23 @@ extension ABIDecoder {
235235
let nonIndexedTypes = nonIndexedInputs.compactMap { inp -> ABI.Element.ParameterType in
236236
return inp.type
237237
}
238-
guard logs.count == indexedInputs.count + 1 else {return nil}
238+
guard logs.count == indexedInputs.count + 1 else { return nil }
239239
var indexedValues = [Any]()
240240
for i in 0 ..< indexedInputs.count {
241241
let data = logs[i+1]
242242
let input = indexedInputs[i]
243243
if !input.type.isStatic || input.type.isArray || input.type.memoryUsage != 32 {
244244
let (v, _) = ABIDecoder.decodeSingleType(type: .bytes(length: 32), data: data)
245-
guard let valueUnwrapped = v else {return nil}
245+
guard let valueUnwrapped = v else { return nil }
246246
indexedValues.append(valueUnwrapped)
247247
} else {
248248
let (v, _) = ABIDecoder.decodeSingleType(type: input.type, data: data)
249-
guard let valueUnwrapped = v else {return nil}
249+
guard let valueUnwrapped = v else { return nil }
250250
indexedValues.append(valueUnwrapped)
251251
}
252252
}
253253
let v = ABIDecoder.decode(types: nonIndexedTypes, data: dataForProcessing)
254-
guard let nonIndexedValues = v else {return nil}
254+
guard let nonIndexedValues = v else { return nil }
255255
var indexedInputCounter = 0
256256
var nonIndexedInputCounter = 0
257257
for i in 0 ..< event.inputs.count {

Sources/Web3Core/EthereumABI/ABIEncoding.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public struct ABIEncoder {
119119
case let d as [IntegerLiteralType]:
120120
var bytesArray = [UInt8]()
121121
for el in d {
122-
guard el >= 0, el <= 255 else {return nil}
122+
guard el >= 0, el <= 255 else { return nil }
123123
bytesArray.append(UInt8(el))
124124
}
125125
return Data(bytesArray)
@@ -140,7 +140,7 @@ public struct ABIEncoder {
140140
/// - `types.count != values.count`;
141141
/// - encoding of at least one value has failed (e.g. type mismatch).
142142
public static func encode(types: [ABI.Element.InOut], values: [Any]) -> Data? {
143-
guard types.count == values.count else {return nil}
143+
guard types.count == values.count else { return nil }
144144
let params = types.compactMap { el -> ABI.Element.ParameterType in
145145
return el.type
146146
}
@@ -156,12 +156,12 @@ public struct ABIEncoder {
156156
/// - `types.count != values.count`;
157157
/// - encoding of at least one value has failed (e.g. type mismatch).
158158
public static func encode(types: [ABI.Element.ParameterType], values: [Any]) -> Data? {
159-
guard types.count == values.count else {return nil}
159+
guard types.count == values.count else { return nil }
160160
var tails = [Data]()
161161
var heads = [Data]()
162162
for i in 0 ..< types.count {
163163
let enc = encodeSingleType(type: types[i], value: values[i])
164-
guard let encoding = enc else {return nil}
164+
guard let encoding = enc else { return nil }
165165
if types[i].isStatic {
166166
heads.append(encoding)
167167
tails.append(Data())
@@ -181,7 +181,7 @@ public struct ABIEncoder {
181181
let head = heads[i]
182182
let tail = tails[i]
183183
if !types[i].isStatic {
184-
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
184+
guard let newHead = tailsPointer.abiEncode(bits: 256) else { return nil }
185185
headsConcatenated.append(newHead)
186186
tailsConcatenated.append(tail)
187187
tailsPointer = tailsPointer + BigUInt(tail.count)
@@ -227,7 +227,7 @@ public struct ABIEncoder {
227227
return bigint == nil ? nil : bigint!.abiEncode(bits: 256)
228228
case .address:
229229
if let string = value as? String {
230-
guard let address = EthereumAddress(string) else {return nil}
230+
guard let address = EthereumAddress(string) else { return nil }
231231
let data = address.addressData
232232
return data.setLengthLeft(32)
233233
} else if let address = value as? EthereumAddress {
@@ -295,7 +295,7 @@ public struct ABIEncoder {
295295
var heads = [Data]()
296296
for i in 0 ..< val.count {
297297
let enc = encodeSingleType(type: subType, value: val[i])
298-
guard let encoding = enc else {return nil}
298+
guard let encoding = enc else { return nil }
299299
heads.append(Data(repeating: 0x0, count: 32))
300300
tails.append(encoding)
301301
}
@@ -310,7 +310,7 @@ public struct ABIEncoder {
310310
let head = heads[i]
311311
let tail = tails[i]
312312
if tail != Data() {
313-
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
313+
guard let newHead = tailsPointer.abiEncode(bits: 256) else { return nil }
314314
headsConcatenated.append(newHead)
315315
tailsConcatenated.append(tail)
316316
tailsPointer = tailsPointer + BigUInt(tail.count)
@@ -342,7 +342,7 @@ public struct ABIEncoder {
342342
var heads = [Data]()
343343
for i in 0 ..< val.count {
344344
let enc = encodeSingleType(type: subType, value: val[i])
345-
guard let encoding = enc else {return nil}
345+
guard let encoding = enc else { return nil }
346346
heads.append(Data(repeating: 0x0, count: 32))
347347
tails.append(encoding)
348348
}
@@ -355,7 +355,7 @@ public struct ABIEncoder {
355355
var tailsConcatenated = Data()
356356
for i in 0 ..< val.count {
357357
let tail = tails[i]
358-
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
358+
guard let newHead = tailsPointer.abiEncode(bits: 256) else { return nil }
359359
headsConcatenated.append(newHead)
360360
tailsConcatenated.append(tail)
361361
tailsPointer = tailsPointer + BigUInt(tail.count)
@@ -372,7 +372,7 @@ public struct ABIEncoder {
372372
guard let val = value as? [Any] else {break}
373373
for i in 0 ..< subTypes.count {
374374
let enc = encodeSingleType(type: subTypes[i], value: val[i])
375-
guard let encoding = enc else {return nil}
375+
guard let encoding = enc else { return nil }
376376
if subTypes[i].isStatic {
377377
heads.append(encoding)
378378
tails.append(Data())
@@ -392,7 +392,7 @@ public struct ABIEncoder {
392392
let head = heads[i]
393393
let tail = tails[i]
394394
if !subTypes[i].isStatic {
395-
guard let newHead = tailsPointer.abiEncode(bits: 256) else {return nil}
395+
guard let newHead = tailsPointer.abiEncode(bits: 256) else { return nil }
396396
headsConcatenated.append(newHead)
397397
tailsConcatenated.append(tail)
398398
tailsPointer = tailsPointer + BigUInt(tail.count)

Sources/Web3Core/EthereumAddress/EthereumAddress.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public struct EthereumAddress: Equatable {
6363
/// represented as `ASCII` data. Otherwise, checksummed address is returned with `0x` prefix.
6464
public static func toChecksumAddress(_ addr: String) -> String? {
6565
let address = addr.lowercased().stripHexPrefix()
66-
guard let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else {return nil}
66+
guard let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else { return nil }
6767
var ret = "0x"
6868

6969
for (i, char) in address.enumerated() {
7070
let startIdx = hash.index(hash.startIndex, offsetBy: i)
7171
let endIdx = hash.index(hash.startIndex, offsetBy: i+1)
7272
let hashChar = String(hash[startIdx..<endIdx])
7373
let c = String(char)
74-
guard let int = Int(hashChar, radix: 16) else {return nil}
74+
guard let int = Int(hashChar, radix: 16) else { return nil }
7575
if int >= 8 {
7676
ret += c.uppercased()
7777
} else {
@@ -96,8 +96,8 @@ extension EthereumAddress {
9696
public init?(_ addressString: String, type: AddressType = .normal, ignoreChecksum: Bool = false) {
9797
switch type {
9898
case .normal:
99-
guard let data = Data.fromHex(addressString) else {return nil}
100-
guard data.count == 20 else {return nil}
99+
guard let data = Data.fromHex(addressString) else { return nil }
100+
guard data.count == 20 else { return nil }
101101
if !addressString.hasHexPrefix() {
102102
return nil
103103
}
@@ -113,7 +113,7 @@ extension EthereumAddress {
113113
return
114114
} else {
115115
let checksummedAddress = EthereumAddress.toChecksumAddress(data.toHexString().addHexPrefix())
116-
guard checksummedAddress == addressString else {return nil}
116+
guard checksummedAddress == addressString else { return nil }
117117
self._address = data.toHexString().addHexPrefix()
118118
self.type = .normal
119119
return
@@ -131,7 +131,7 @@ extension EthereumAddress {
131131
}
132132

133133
public init?(_ addressData: Data, type: AddressType = .normal) {
134-
guard addressData.count == 20 else {return nil}
134+
guard addressData.count == 20 else { return nil }
135135
self._address = addressData.toHexString().addHexPrefix()
136136
self.type = type
137137
}

0 commit comments

Comments
 (0)