Skip to content

Commit

Permalink
Swiftlint checked
Browse files Browse the repository at this point in the history
  • Loading branch information
6od9i committed Jan 20, 2025
1 parent 95d5cb4 commit c38f0c2
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 55;
objectVersion = 70;
objects = {

/* Begin PBXBuildFile section */
Expand Down
2 changes: 1 addition & 1 deletion Example/myWeb3Wallet/myWeb3Wallet/Core/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Network {
let networkRPC: String
/// Path to network explorer like https://bscscan.com/
let explorer: String?

/// list of tokens added in this network
var tokens: [Token]
}
29 changes: 7 additions & 22 deletions Example/myWeb3Wallet/myWeb3Wallet/Core/WalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,32 @@ import web3swift
import Web3Core
import BigInt

final class Web3Network {
let network: Network

/// web3 - sign, request and etc
let web3: Web3

var tokensBalances: [String: BigUInt] = [:]

init(network: Network, web3: Web3) {
self.network = network
self.web3 = web3
}
}


final class WalletManager {
static let keystorePassword = "password"

/// Container with private keys
private let keystoreManager: KeystoreManager

private(set) var networks: [Web3Network] = []

let address: EthereumAddress

init(keystoreManager: KeystoreManager) async {
self.keystoreManager = keystoreManager
self.address = keystoreManager.addresses!.first!

for model in WalletChainsModel.networks {
let network = Networks.Custom(networkID: BigUInt(model.chainId))
guard let providerURL = URL(string: model.networkRPC),
let provider = try? await Web3HttpProvider(url: providerURL, network: network,
keystoreManager: keystoreManager)
else { continue }

let web3 = web3swift.Web3(provider: provider)
networks.append(Web3Network(network: model, web3: web3))
}
}

func loadBalances() async {
for network in networks {
if let nativeBalance = try? await network.web3.eth.getBalance(for: address),
Expand Down
25 changes: 25 additions & 0 deletions Example/myWeb3Wallet/myWeb3Wallet/Core/Web3Network.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Web3Network.swift
// myWeb3Wallet
//
// Created by 6od9i on 20/01/25.
//

import Foundation
import web3swift
import Web3Core
import BigInt

final class Web3Network {
let network: Network

/// web3 - sign, request and etc
let web3: Web3

var tokensBalances: [String: BigUInt] = [:]

init(network: Network, web3: Web3) {
self.network = network
self.web3 = web3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import web3swift
import Web3Core

class AuthViewController: UIViewController {

@IBOutlet weak var continueButton: UIButton!
@IBOutlet weak var walletAddressLabel: UILabel!
@IBOutlet weak var importWalletButton: UIButton!
@IBOutlet weak var createWalletButton: UIButton!

var walletAddress: String? {
didSet {
self.walletAddressLabel.text = walletAddress
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.createWalletButton.layer.cornerRadius = 5.0
self.importWalletButton.layer.cornerRadius = 5.0
}

@IBAction func onClickCreateWallet(_ sender: UIButton) {
self.createMnemonics()

}
@IBAction func onClickImportWalletButton(_ sender: UIButton) {
self.showImportAlert()
}

@IBAction func onClickContinueButton(_ sender: UIButton) {
}
}
Expand All @@ -61,14 +61,14 @@ extension AuthViewController {
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}

fileprivate func createMnemonics() {
guard let mnemonics = try? BIP39.generateMnemonics(bitsOfEntropy: 256, language: .english) else {
self.showAlertMessage(title: "", message: "We are unable to create wallet", actionName: "Ok")
return
}
print(mnemonics)

guard let keystore = try? BIP32Keystore(mnemonics: mnemonics, password: WalletManager.keystorePassword),
let walletAddress = keystore.addresses?.first else {
self.showAlertMessage(title: "", message: "Unable to create wallet", actionName: "Ok")
Expand All @@ -78,13 +78,13 @@ extension AuthViewController {
let privateKey = try! keystore.UNSAFE_getPrivateKeyData(password: WalletManager.keystorePassword,
account: walletAddress)
print(privateKey)

Task {
let walletManager = await WalletManager(keystoreManager: KeystoreManager([keystore]))
openWallet(walletManager: walletManager)
}
}

func importWalletWith(privateKey: String) {
let formattedKey = privateKey.trimmingCharacters(in: .whitespacesAndNewlines)
guard let dataKey = Data.fromHex(formattedKey) else {
Expand All @@ -94,7 +94,7 @@ extension AuthViewController {
do {
guard let keystore = try EthereumKeystoreV3(privateKey: dataKey, password: WalletManager.keystorePassword),
let address = keystore.addresses?.first?.address else {
throw NSError()
throw NSError(domain: "Unknown", code: 400)
}
self.walletAddress = address
Task { @MainActor in
Expand Down Expand Up @@ -122,7 +122,7 @@ extension AuthViewController {
openWallet(walletManager: walletManager)
}
}

func openWallet(walletManager: WalletManager) {
let walletVC = WalletViewController(walletManager: walletManager)
navigationController?.setViewControllers([walletVC], animated: true)
Expand All @@ -136,5 +136,4 @@ extension UIViewController {
alertController.addAction(action)
self.present(alertController, animated: true)
}

}
Loading

0 comments on commit c38f0c2

Please sign in to comment.