This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ViewController.swift
357 lines (322 loc) · 16.7 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// ViewController.swift
// Blockstack
//
// Created by Yukan Liao on 03/27/2018.
//
import UIKit
import Blockstack
import SafariServices
fileprivate let filename = "testFile"
class ViewController: UIViewController {
@IBOutlet var nameLabel: UILabel!
@IBOutlet weak var optionsContainerView: UIScrollView!
@IBOutlet weak var resetKeychainButton: UIButton!
@IBOutlet var signInButton: UIButton!
override func viewDidLoad() {
self.updateUI()
Blockstack.shared.isBetaBrowserEnabled = false
}
@IBAction func signIn() {
// Address of deployed example web app
Blockstack.shared.signIn(redirectURI: URL(string: "https://pedantic-mahavira-f15d04.netlify.app/redirect.html")!,
appDomain: URL(string: "https://pedantic-mahavira-f15d04.netlify.app")!, scopes: [.storeWrite, .publishData]) { authResult in
switch authResult {
case .success(let userData):
print("sign in success")
self.handleSignInSuccess(userData: userData)
case .cancelled:
print("sign in cancelled")
case .failed(let error):
print("sign in failed, error: ", error ?? "n/a")
}
}
}
func handleSignInSuccess(userData: UserData) {
print(userData.profile?.name as Any)
self.updateUI()
// Check if signed in
// checkIfSignedIn()
}
@IBAction func signOut(_ sender: Any) {
// Sign user out
Blockstack.shared.signUserOut()
self.updateUI()
}
@IBAction func resetDeviceKeychain(_ sender: Any) {
// Blockstack.shared.promptClearDeviceKeychain()
}
@IBAction func putFileTapped(_ sender: Any) {
// guard self.saveInvalidGaiaConfig() else {
// return
// }
// Put file example
let alert = UIAlertController(title: "Put File", message: "Type a message to put in the file:", preferredStyle: .alert)
alert.addTextField { field in
field.placeholder = "Hello world!"
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Send", style: .default) { _ in
let text: String = alert.textFields?.first?.text ?? "Default Text"
Blockstack.shared.putFile(to: filename, text: text, sign: true, signingKey: nil) { (publicURL, error) in
if error != nil {
print("put file error")
} else {
print("put file success \(publicURL ?? "NA")")
}
}
})
}
@IBAction func getNameInfo(_ sender: Any) {
let alert = UIAlertController(title: "Type Name", message: "Type a name to get WHOIS-like info about it.", preferredStyle: .alert)
alert.addTextField { field in
field.placeholder = "helloworld.id"
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Next", style: .default) { _ in
guard let name = alert.textFields?.first?.text else {
return
}
Blockstack.shared.getNameInfo(fullyQualifiedName: name) { data, error in
guard error == nil, let json = data else {
let alert = UIAlertController(title: "Oops", message: "Something went wrong. Are you sure that name exists?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let nameInfoDescription = json.reduce("", { return String(describing: "\($0)\n\n\"\($1.key)\": \"\($1)\"")})
let alert = UIAlertController(title: "Get Name Info", message: nameInfoDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func getNamePriceTapped(_ sender: Any) {
let alert = UIAlertController(title: "Get Name Price", message: "Type a name to get its price.", preferredStyle: .alert)
alert.addTextField { field in
field.placeholder = "helloworld.id"
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Next", style: .default) { _ in
guard let name = alert.textFields?.first?.text else {
return
}
Blockstack.shared.getNamePrice(fullyQualifiedName: name) { data, error in
guard error == nil, let amount = data?.amount, let units = data?.units else {
let alert = UIAlertController(title: "Oops", message: "Something went wrong. Are you sure that name exists?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let measurement = units == "BTC" ? "satoshis" : "microstacks"
let alert = UIAlertController(title: "Get Name Price", message: "\(amount) \(measurement) (\(units))", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func getNamespacePriceTapped(_ sender: Any) {
let alert = UIAlertController(title: "Get Namespace Price", message: "Type a namespace to get its price.", preferredStyle: .alert)
alert.addTextField { field in
field.placeholder = "id"
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Next", style: .default) { _ in
guard let name = alert.textFields?.first?.text else {
return
}
Blockstack.shared.getNamespacePrice(namespaceId: name) { data, error in
guard error == nil, let amount = data?.amount, let units = data?.units else {
let alert = UIAlertController(title: "Oops", message: "Something went wrong. Are you sure that name exists?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let measurement = units == "BTC" ? "satoshis" : "microstacks"
let alert = UIAlertController(title: "Get Namespace Price", message: "\(amount) \(measurement) (\(units))", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func getNamesOwned(_ sender: Any) {
let alert = UIAlertController(title: "Get Names Owned", message: "Type an address to get names owned by it.", preferredStyle: .alert)
alert.addTextField { _ in
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Next", style: .default) { _ in
guard let address = alert.textFields?.first?.text else {
return
}
Blockstack.shared.getNamesOwned(address: address) { names, error in
guard error == nil, let names = names else {
let alert = UIAlertController(title: "Oops", message: "Something went wrong. Are you sure that is a valid address?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let alert = UIAlertController(title: "Get Names Owned", message: String(describing: names.reduce("", { "\($0)\n\($1)" })), preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func getNamespaceBurnAddress(_ sender: Any) {
let alert = UIAlertController(title: "Get Namespace Burn Address", message: "Type a namespace to get the the blockchain address to which a name's registration fee must be sent.", preferredStyle: .alert)
alert.addTextField { _ in
}
self.present(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Next", style: .default) { _ in
guard let namespace = alert.textFields?.first?.text else {
return
}
Blockstack.shared.getNamespaceBurnAddress(namespace: namespace) { address, error in
guard error == nil, let address = address else {
let alert = UIAlertController(title: "Oops", message: "Please enter a valid namespace!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let alert = UIAlertController(title: "\"\(namespace)\" burn address", message: address, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func getFileTapped(_ sender: Any) {
// Read data from Gaia
Blockstack.shared.getFile(at: filename, verify: true) { response, error in
var text: String?
if error != nil {
print("get file error")
text = "Could not get file. Try putting something first!"
} else {
print("get file success")
text = (response as? DecryptedValue)?.plainText ?? "Invalid content"
}
let alert = UIAlertController(title: "Get File", message: text, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func multiplayerGetFileTapped(_ sender: Any) {
let alert = UIAlertController(title: "Multiplayer Get File", message: "What is the Blockstack ID of the other user?\n\nNote: this will only work if the other user has PUT the file using this sample app.", preferredStyle: .alert)
alert.addTextField {
$0.placeholder = "i.e. testuser.id"
}
alert.addAction(UIAlertAction(title: "Confirm", style: .default) { _ in
guard let userID = alert.textFields?.first?.text else {
let errorAlert = UIAlertController(title: "Oops!", message: "You must enter a valid ID.", preferredStyle: .alert)
errorAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(errorAlert, animated: true)
return
}
// Read data from Gaia
Blockstack.shared.getFile(at: filename, username: userID) { response, error in
if error != nil {
print("get file error")
} else {
print("get file success")
print(response as Any)
let text = response as? String ?? "Oops--something went wrong."
let errorAlert = UIAlertController(title: "Get File Result", message: text, preferredStyle: .alert)
errorAlert.addAction(UIAlertAction(title: "Done", style: .cancel, handler: nil))
self.present(errorAlert, animated: true)
}
}
})
self.present(alert, animated: true)
}
@IBAction func deleteFile(_ sender: Any) {
Blockstack.shared.deleteFile(at: filename, wasSigned: false) { error in
var message: String?
if let gaiaError = error as? GaiaError {
switch gaiaError {
case .itemNotFoundError:
message = "'\(filename)' was not found."
default:
message = "Something went wrong, could not delete file."
}
} else {
message = "Success! '\(filename)' was deleted."
}
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func listFiles(_ sender: Any) {
let sheet = UIAlertController(title: "List Files", message: "List all of your files in this application's Gaia storage bucket?", preferredStyle: .actionSheet)
sheet.addAction(UIAlertAction(title: "Yes", style: .default) { _ in
var files = [String]()
Blockstack.shared.listFiles(callback: {
// Continue until there are no more files
files.append($0)
return true
}, completion: { fileCount, error in
var message = "\(fileCount) files.\n"
for i in 0..<fileCount {
if i < 50 {
message += "\n\(files[i])"
} else {
message += "\n...and \(fileCount - i + 1) more!"
break
}
}
let alert = UIAlertController(title: "List Files", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
})
})
sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(sheet, animated: true, completion: nil)
}
// MARK: - Private
private func saveInvalidGaiaConfig() -> Bool {
// Ensure existing hub connection
// Blockstack.shared.putFile(to: "test", text: "hello") { _, _ in
// }
// Get previous gaia config
guard let data = UserDefaults.standard.value(forKey:
BlockstackConstants.GaiaHubConfigUserDefaultLabel) as? Data,
let config = try? PropertyListDecoder().decode(GaiaConfig.self, from: data) else {
return false
}
// Create invalid config
let invalidConfig = GaiaConfig(URLPrefix: config.URLPrefix, address: config.address, token: "v1:invalidated", server: config.server)
// Save invalid gaia config
Blockstack.shared.clearGaiaSession()
guard let encodedInvalidConfig = try? PropertyListEncoder().encode(invalidConfig) else {
return false
}
UserDefaults.standard.set(encodedInvalidConfig, forKey: BlockstackConstants.GaiaHubConfigUserDefaultLabel)
return true
}
private func updateUI() {
DispatchQueue.main.async {
if Blockstack.shared.isUserSignedIn() {
// Read user profile data
let retrievedUserData = Blockstack.shared.loadUserData()
print(retrievedUserData?.profile?.name as Any)
self.nameLabel.text =
retrievedUserData?.profile?.name ?? "Nameless User"
self.optionsContainerView.isHidden = false
self.signInButton.isHidden = true
self.resetKeychainButton.isHidden = true
} else {
self.optionsContainerView.isHidden = true
self.signInButton.isHidden = false
self.resetKeychainButton.isHidden = true
}
}
}
private func checkIfSignedIn() {
Blockstack.shared.isUserSignedIn() ? print("currently signed in") : print("not signed in")
}
}