Skip to content

Feature/pre swiftlint cleanup #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 56 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b0f88d2
Basic code cleanup of Browser/*.swift
mloit Mar 22, 2022
50bda8c
Basic code cleanup of Contract/*.swift
mloit Mar 22, 2022
e49e448
Basic code cleanup of Convenience/*.swift
mloit Mar 22, 2022
27dc172
Basic code cleanup of EthereumABI/*.swift
mloit Mar 22, 2022
8afe107
Basic code cleanup of EthereumAddress/*.swift and HookedFunctions/*.s…
mloit Mar 22, 2022
6a93560
Basic code cleanup of KeystoreManager/*.swift
mloit Mar 22, 2022
6744c12
Basic code cleanup of Promises/*.swift
mloit Mar 22, 2022
4c3019d
Basic code cleanup of Tokens/*/*.swift
mloit Mar 22, 2022
5b89798
Basic code cleanup of SwiftRLP/*.swift and Transaction/*.swift
mloit Mar 22, 2022
7c63356
Basic code cleanup of Utils/EIP/*.swift and Utils/Hooks/*.swift
mloit Mar 22, 2022
5019fa3
Basic code cleanup of Utils/ENS/*.swift
mloit Mar 22, 2022
6fc5268
Basic code cleanup of Web3/*.swift
mloit Mar 22, 2022
d3f74ac
Restore Quick Help friendly style comments
mloit Mar 22, 2022
d9b077d
Removed unnecessary commented code
mloit Mar 22, 2022
d3fd2b3
removed code that has sat commented out since 2018
mloit Mar 22, 2022
a2a4ea2
removed old commented code from 2018
mloit Mar 22, 2022
0f9b013
removed old commented code from 2019
mloit Mar 22, 2022
20c9ea7
removed old commented code
mloit Mar 22, 2022
fe1acdd
removed old commented code
mloit Mar 22, 2022
489293c
removed leading and trailing spaces within parenthesis
mloit Mar 22, 2022
fdb214c
removed commented code blocks that are identical to the actual code
mloit Mar 22, 2022
efcc798
Removed empty file committed in 2018 and never used
mloit Mar 22, 2022
2d87478
removed missed commented import
mloit Mar 22, 2022
157b64d
removed unnecessary trailing comment
mloit Mar 22, 2022
787c74c
Basic code cleanup of Browser/*.swift
mloit Mar 22, 2022
2314d1d
Basic code cleanup of Contract/*.swift
mloit Mar 22, 2022
d11f988
Basic code cleanup of Convenience/*.swift
mloit Mar 22, 2022
ffb7fdb
Basic code cleanup of EthereumABI/*.swift
mloit Mar 22, 2022
91f258c
Basic code cleanup of EthereumAddress/*.swift and HookedFunctions/*.s…
mloit Mar 22, 2022
27eb75d
Basic code cleanup of KeystoreManager/*.swift
mloit Mar 22, 2022
c163550
Basic code cleanup of Promises/*.swift
mloit Mar 22, 2022
d1ff1bf
Basic code cleanup of Tokens/*/*.swift
mloit Mar 22, 2022
449a3d2
Basic code cleanup of SwiftRLP/*.swift and Transaction/*.swift
mloit Mar 22, 2022
d62c6f1
Basic code cleanup of Utils/EIP/*.swift and Utils/Hooks/*.swift
mloit Mar 22, 2022
873d731
Basic code cleanup of Utils/ENS/*.swift
mloit Mar 22, 2022
afd83d1
Basic code cleanup of Web3/*.swift
mloit Mar 22, 2022
b0a8c90
Restore Quick Help friendly style comments
mloit Mar 22, 2022
9a03706
Removed unnecessary commented code
mloit Mar 22, 2022
27d1538
removed code that has sat commented out since 2018
mloit Mar 22, 2022
e8dd090
removed old commented code from 2018
mloit Mar 22, 2022
f70f58c
removed old commented code from 2019
mloit Mar 22, 2022
93285ac
removed old commented code
mloit Mar 22, 2022
f9cfbdb
removed old commented code
mloit Mar 22, 2022
0b21f85
removed leading and trailing spaces within parenthesis
mloit Mar 22, 2022
a930e7d
removed commented code blocks that are identical to the actual code
mloit Mar 22, 2022
d0a1850
Removed empty file committed in 2018 and never used
mloit Mar 22, 2022
7255105
removed missed commented import
mloit Mar 22, 2022
517675e
removed unnecessary trailing comment
mloit Mar 22, 2022
6350481
a light lint pass on the package.swift files
mloit Mar 22, 2022
feca1dd
Merge branch 'feature/pre-swiftlint-cleanup' of https://github.com/ml…
mloit Mar 22, 2022
e772da5
restoring the deleted file for now, as it fails automated checks with…
mloit Mar 22, 2022
2cce76a
Squashed commit of the following:
mloit Mar 24, 2022
62686d9
Squashed commit of the following:
mloit Mar 24, 2022
25d4fda
minor stylistic cleanup in Web3+Structures
mloit Mar 24, 2022
545bf3a
Merge branch 'develop' into feature/pre-swiftlint-cleanup
mloit Mar 24, 2022
9412b3b
Merge branch 'skywinder:develop' into feature/pre-swiftlint-cleanup
mloit Mar 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// swift-tools-version:5.4
// swift-tools-version: 5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription


#if os(macOS)
let excludeFiles = [
"./Browser/BrowserViewController.swift", // Because of inheriting iOS only class failed to build on macOS.
"./Browser/BrowserViewController.swift" // Because of inheriting iOS only class failed to build on macOS.
]
#elseif os(iOS)
let excludeFiles: String = []
Expand All @@ -20,7 +19,7 @@ let package = Package(
products: [
.library(name: "web3swift", targets: ["web3swift"])
],

dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.16.2"),
Expand Down Expand Up @@ -60,6 +59,6 @@ let package = Package(
.copy("../../../TestToken/Helpers/TokenBasics/IERC20.sol"),
.copy("../../../TestToken/Token/Web3SwiftToken.sol")
]
),
)
]
)
10 changes: 5 additions & 5 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// swift-tools-version:5.0
// swift-tools-version: 5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Web3swift",
platforms: [
.macOS(.v10_12), .iOS(.v11),
.macOS(.v10_12), .iOS(.v11)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(name: "web3swift", targets: ["web3swift"]),
],

dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.15.4"),
.package(url: "https://github.com/daltoniam/Starscream.git", from: "4.0.4"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.4.2"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.4.2")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -30,6 +30,6 @@ let package = Package(
]),
.testTarget(
name: "web3swiftTests",
dependencies: ["web3swift"]),
dependencies: ["web3swift"])
]
)
62 changes: 31 additions & 31 deletions Sources/web3swift/Browser/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,76 @@ import WebKit

/// Bridge for WKWebView and JavaScript
open class Bridge: NSObject {

static let name: String = "pacific"

fileprivate static let callbackEventName = "PacificDidReceiveNativeCallback"
fileprivate static let postEventName = "PacificDidReceiveNativeBroadcast"

fileprivate enum MessageKey {
static let action = "action"
static let parameters = "parameters"
static let callback = "callback"
static let printable = "print"
}

public struct JSError {
public let code: Int
public let description: String

public init(code: Int, description: String) {
self.code = code
self.description = description
}
}

/// Used to callback to webpage whether a message from webpage was handled successful or encountered an error.
///
/// - success: The result of message was successful
///
/// - failure: Unable to handle the message, notify js with error by **Object Error** { code: Int, description: String}
///
public enum Results {

case success([String: Any]?)

case failure(JSError)
}

/// Bridge Callback to webpage
/// - Parameter results: Value pass to webpage
public typealias Callback = (_ results: Results) -> Void

/// Closure when js send message to native
/// - Parameter parameters: js parameters
/// - Parameter callback: callback func
public typealias Handler = (_ parameters: [String: Any]?, _ callback: @escaping Callback) -> Void

public typealias DefaultHandler = (_ name: String, _ parameters: [String: Any]?, _ callback: @escaping Callback) -> Void

private(set) var handlers = [String: Handler]()

public var defaultHandler: DefaultHandler?

fileprivate let configuration: WKWebViewConfiguration
fileprivate weak var webView: WKWebView?

/// Print message body from webpage automatically.
public var printScriptMessageAutomatically = false

deinit {
configuration.removeObserver(self, forKeyPath: #keyPath(WKWebViewConfiguration.userContentController))
configuration.userContentController.removeScriptMessageHandler(forName: Bridge.name)
}

fileprivate init(webView: WKWebView) {
self.webView = webView
self.configuration = webView.configuration
super.init()
configuration.addObserver(self, forKeyPath: #keyPath(WKWebViewConfiguration.userContentController), options: [.new, .old], context: nil)
configuration.userContentController.add(self, name: Bridge.name)
}

/// Register to handle action
/// - Parameter handler: closure when handle message from webpage
/// - parameter action: name of action
Expand All @@ -95,13 +95,13 @@ open class Bridge: NSObject {
public func register(_ handler: @escaping Handler, for action: String) {
handlers[action] = handler
}

/// Unregister an action
/// - Parameters action: name of action
public func unregister(for action: String) {
handlers[action] = nil
}

/// send action to webpage
/// - Parameter action: action listened by js `window.bridge.on(**action**, handler)`
/// - Parameter parameters: parameters pass to js
Expand All @@ -114,16 +114,16 @@ open class Bridge: NSObject {
guard let webView = webView else { return }
webView.st_dispatchBridgeEvent(Bridge.postEventName, parameters: ["name": action], results: .success(parameters), completionHandler: nil)
}

/// Evaluates the given JavaScript string.
/// - Parameter javaScriptString: The JavaScript string to evaluate.
/// - Parameter completion: A block to invoke when script evaluation completes or fails.
public func evaluate(_ javaScriptString: String, completion: ((Any?, Error?) -> Void)? = nil) {
guard let webView = webView else { return }
webView.evaluateJavaScript(javaScriptString, completionHandler: completion)
}
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if let obj = object as? WKWebViewConfiguration, let kp = keyPath, obj == configuration && kp == #keyPath(WKWebViewConfiguration.userContentController) {
if let change = change {
if let oldContentController = change[.oldKey] as? WKUserContentController {
Expand All @@ -138,7 +138,7 @@ open class Bridge: NSObject {
}

extension Bridge: WKScriptMessageHandler {

/*! @abstract Invoked when a script message is received from a webpage.
@param userContentController The user content controller invoking the
delegate method.
Expand Down Expand Up @@ -171,7 +171,7 @@ extension Bridge: WKScriptMessageHandler {
}
return
}

if let callbackID = (body[MessageKey.callback] as? NSNumber) {
handler(body[MessageKey.parameters] as? [String: Any]) { [weak self] (results) in
guard let strongSelf = self else {
Expand All @@ -190,11 +190,11 @@ extension Bridge: WKScriptMessageHandler {
}

public extension WKWebView {

private struct STPrivateStatic {
fileprivate static var bridgeKey = "STPrivateStatic.bridgeKey"
}

/// Bridge for WKWebView and JavaScript. Initialize `lazy`
var bridge: Bridge {
if let bridge = objc_getAssociatedObject(self, &STPrivateStatic.bridgeKey) as? Bridge {
Expand All @@ -204,7 +204,7 @@ public extension WKWebView {
objc_setAssociatedObject(self, &STPrivateStatic.bridgeKey, bridge, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return bridge
}

/// Remove Bridge And Reset, All the handlers will be removed
func removeBridge() {
if let bridge = objc_getAssociatedObject(self, &STPrivateStatic.bridgeKey) as? Bridge {
Expand All @@ -216,12 +216,12 @@ public extension WKWebView {
}

fileprivate extension WKWebView {

func st_dispatchBridgeEvent(_ eventName: String,
parameters: [String: Any],
results: Bridge.Results,
completionHandler: ((Any?, Error?) -> Void)? = nil) {

var eventDetail: [String: Any] = parameters
switch results {
case .failure(let error):
Expand All @@ -234,7 +234,7 @@ fileprivate extension WKWebView {
if
let _data = try? JSONSerialization.data(withJSONObject: eventBody, options: JSONSerialization.WritingOptions()),
let eventString = String(data: _data, encoding: .utf8) {

jsString = "(function() { var event = new CustomEvent('\(eventName)', \(eventString)); document.dispatchEvent(event)}());"
} else {
// When JSON Not Serializable, Invoke with Default Parameters
Expand Down
30 changes: 15 additions & 15 deletions Sources/web3swift/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import WebKit

// FIXME: Rewrite me, because i'm building only on iOS.
open class BrowserViewController: UIViewController {

public enum Method: String {
case getAccounts
case signTransaction
Expand All @@ -19,12 +19,12 @@ open class BrowserViewController: UIViewController {
case publishTransaction
case approveTransaction
}

public lazy var webView: WKWebView = {
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{ })

WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler: { })
let webView = WKWebView(
frame: .zero,
configuration: self.config
Expand All @@ -35,12 +35,12 @@ open class BrowserViewController: UIViewController {
webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
return webView
}()

lazy var config: WKWebViewConfiguration = {
let config = WKWebViewConfiguration()

var js = ""

if let filepath = Bundle.main.path(forResource: "browser.min", ofType: "js") {
do {
js += try String(contentsOfFile: filepath)
Expand All @@ -55,29 +55,29 @@ open class BrowserViewController: UIViewController {
config.userContentController.addUserScript(userScript)
return config
}()

public func registerBridges(for web3: web3) {
self.webView.bridge.register({ (parameters, completion) in
let url = web3.provider.url.absoluteString
completion(.success(["rpcURL": url as Any]))
}, for: "getRPCurl")

self.webView.bridge.register({ (parameters, completion) in
let allAccounts = web3.browserFunctions.getAccounts()
completion(.success(["accounts": allAccounts as Any]))
}, for: "eth_getAccounts")

self.webView.bridge.register({ (parameters, completion) in
let coinbase = web3.browserFunctions.getCoinbase()
completion(.success(["coinbase": coinbase as Any]))
}, for: "eth_coinbase")

self.webView.bridge.register({ (parameters, completion) in
if parameters == nil {
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
return
}
let payload = parameters!["payload"] as? [String:Any]
let payload = parameters!["payload"] as? [String: Any]
if payload == nil {
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
return
Expand All @@ -95,13 +95,13 @@ open class BrowserViewController: UIViewController {
}
completion(.success(["signedMessage": result as Any]))
}, for: "eth_sign")

self.webView.bridge.register({ (parameters, completion) in
if parameters == nil {
completion(.failure(Bridge.JSError(code: 0, description: "No parameters provided")))
return
}
let transaction = parameters!["transaction"] as? [String:Any]
let transaction = parameters!["transaction"] as? [String: Any]
if transaction == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
return
Expand All @@ -118,7 +118,7 @@ open class BrowserViewController: UIViewController {

extension BrowserViewController: WKNavigationDelegate {
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

}
}

Expand Down
2 changes: 0 additions & 2 deletions Sources/web3swift/Contract/ComparisonExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import Foundation
import BigInt
//import EthereumAddress

extension BigUInt: EventFilterComparable {
public func isEqualTo(_ other: AnyObject) -> Bool {
Expand Down Expand Up @@ -92,4 +91,3 @@ extension EthereumAddress: EventFilterComparable {
}
}
}

Loading