6
6
// Copyright © 2018 Alexander Vlasov. All rights reserved.
7
7
//
8
8
9
- import Foundation
10
9
import UIKit
11
- import WebKit
12
10
import web3swift
13
- import WKBridge
14
11
15
- class BrowserViewController : UIViewController {
16
-
17
- enum Method : String {
18
- case getAccounts
19
- case signTransaction
20
- case signMessage
21
- case signPersonalMessage
22
- case publishTransaction
23
- case approveTransaction
24
- }
25
-
26
- lazy var webView : WKWebView = {
27
- let websiteDataTypes = NSSet ( array: [ WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache] )
28
- let date = NSDate ( timeIntervalSince1970: 0 )
29
-
30
- WKWebsiteDataStore . default ( ) . removeData ( ofTypes: websiteDataTypes as! Set < String > , modifiedSince: date as Date , completionHandler: { } )
31
- let webView = WKWebView (
32
- frame: . zero,
33
- configuration: self . config
34
- )
35
- webView. allowsBackForwardNavigationGestures = true
36
- webView. scrollView. isScrollEnabled = true
37
- webView. navigationDelegate = self
38
- webView. configuration. preferences. setValue ( true , forKey: " developerExtrasEnabled " )
39
- return webView
40
- } ( )
41
-
42
- lazy var config : WKWebViewConfiguration = {
43
- let config = WKWebViewConfiguration ( )
44
-
45
- var js = " "
46
-
47
- if let filepath = Bundle . main. path ( forResource: " Web3Swift.min " , ofType: " js " ) {
48
- do {
49
- js += try String ( contentsOfFile: filepath)
50
- NSLog ( " Loaded web3swift.js " )
51
- } catch {
52
- NSLog ( " Failed to load web.js " )
53
- }
54
- } else {
55
- NSLog ( " web3.js not found in bundle " )
56
- }
57
- let userScript = WKUserScript ( source: js, injectionTime: . atDocumentStart, forMainFrameOnly: false )
58
- config. userContentController. addUserScript ( userScript)
59
- return config
60
- } ( )
61
-
62
-
12
+ class ViewController : BrowserViewController {
63
13
override func viewWillAppear( _ animated: Bool ) {
64
14
super. viewWillAppear ( animated)
65
15
webView. translatesAutoresizingMaskIntoConstraints = false
@@ -72,8 +22,7 @@ class BrowserViewController: UIViewController {
72
22
webView. bottomAnchor. constraint ( equalTo: view. bottomAnchor) ,
73
23
] )
74
24
75
- // webView.load(URLRequest(url: URL(string: "https://plasma-testnet.thematter.io/")!))
76
- webView. load ( URLRequest ( url: URL ( string: " https://instant.airswap.io " ) !) )
25
+ webView. load ( URLRequest ( url: URL ( string: " https://app.compound.finance " ) !) )
77
26
78
27
do {
79
28
let userDir = NSSearchPathForDirectoriesInDomains ( . documentDirectory, . userDomainMask, true ) [ 0 ]
@@ -87,78 +36,14 @@ class BrowserViewController: UIViewController {
87
36
}
88
37
guard let sender = keystoreManager? . addresses![ 0 ] else { return }
89
38
print ( sender)
39
+
90
40
let web3 = Web3 . InfuraRinkebyWeb3 ( )
91
41
web3. addKeystoreManager ( keystoreManager)
92
42
93
- self . webView. bridge. register ( { ( parameters, completion) in
94
- let url = web3. provider. url. absoluteString
95
- completion ( . success( [ " rpcURL " : url as Any ] ) )
96
- } , for: " getRPCurl " )
97
-
98
- self . webView. bridge. register ( { ( parameters, completion) in
99
- let allAccounts = web3. browserFunctions. getAccounts ( )
100
- completion ( . success( [ " accounts " : allAccounts as Any ] ) )
101
- } , for: " eth_getAccounts " )
102
-
103
- self . webView. bridge. register ( { ( parameters, completion) in
104
- let coinbase = web3. browserFunctions. getCoinbase ( )
105
- completion ( . success( [ " coinbase " : coinbase as Any ] ) )
106
- } , for: " eth_coinbase " )
107
- self . webView. bridge. register ( { ( parameters, completion) in
108
- if parameters == nil {
109
- completion ( . failure( Bridge . JSError ( code: 0 , description: " No parameters provided " ) ) )
110
- return
111
- }
112
- let payload = parameters![ " payload " ] as? [ String : Any ]
113
- if payload == nil {
114
- completion ( . failure( Bridge . JSError ( code: 0 , description: " No parameters provided " ) ) )
115
- return
116
- }
117
- let personalMessage = payload![ " data " ] as? String
118
- let account = payload![ " from " ] as? String
119
- if personalMessage == nil || account == nil {
120
- completion ( . failure( Bridge . JSError ( code: 0 , description: " Not enough parameters provided " ) ) )
121
- return
122
- }
123
- let result = web3. browserFunctions. personalSign ( personalMessage!, account: account!)
124
- if result == nil {
125
- completion ( . failure( Bridge . JSError ( code: 0 , description: " Account or data is invalid " ) ) )
126
- return
127
- }
128
- completion ( . success( [ " signedMessage " : result as Any ] ) )
129
- } , for: " eth_sign " )
130
- self . webView. bridge. register ( { ( parameters, completion) in
131
- if parameters == nil {
132
- completion ( . failure( Bridge . JSError ( code: 0 , description: " No parameters provided " ) ) )
133
- return
134
- }
135
- let transaction = parameters![ " transaction " ] as? [ String : Any ]
136
- if transaction == nil {
137
- completion ( . failure( Bridge . JSError ( code: 0 , description: " Not enough parameters provided " ) ) )
138
- return
139
- }
140
- let result = web3. browserFunctions. signTransaction ( transaction!)
141
- if result == nil {
142
- completion ( . failure( Bridge . JSError ( code: 0 , description: " Data is invalid " ) ) )
143
- return
144
- }
145
- completion ( . success( [ " signedTransaction " : result as Any ] ) )
146
- } , for: " eth_signTransaction " )
43
+ self . registerBridges ( for: web3)
147
44
}
148
45
catch {
149
46
print ( error)
150
47
}
151
48
}
152
49
}
153
-
154
- extension BrowserViewController : WKNavigationDelegate {
155
- func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
156
-
157
- }
158
- }
159
-
160
- extension BrowserViewController : WKScriptMessageHandler {
161
- func userContentController( _ userContentController: WKUserContentController , didReceive message: WKScriptMessage ) {
162
- NSLog ( " message \( message. body) " )
163
- }
164
- }
0 commit comments