Skip to content

Commit

Permalink
Cleaned webview + added textView
Browse files Browse the repository at this point in the history
  • Loading branch information
Meyanis95 committed Nov 12, 2023
1 parent 2e3c4bf commit 3d71c35
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 27 deletions.
19 changes: 9 additions & 10 deletions mopro-ios/MoproKit/Bindings/mopro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,13 @@ public func `generateProof2`(`circuitInputs`: [String: [String]]) throws -> Gene
)
}

public func verifyProof2(proof: Data, publicInput: Data) throws -> Bool {
return try FfiConverterBool.lift(
rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_fn_func_verify_proof2(
FfiConverterData.lower(proof),
FfiConverterData.lower(publicInput), $0
)
}
public func `verifyProof2`(`proof`: Data, `publicInput`: Data) throws -> Bool {
return try FfiConverterBool.lift(
try rustCallWithError(FfiConverterTypeMoproError.lift) {
uniffi_mopro_fn_func_verify_proof2(
FfiConverterData.lower(`proof`),
FfiConverterData.lower(`publicInput`),$0)
}
)
}

Expand Down Expand Up @@ -759,10 +758,10 @@ private var initializationResult: InitializationResult {
if (uniffi_mopro_checksum_func_generate_proof2() != 6969) {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_checksum_func_verify_proof2() != 6153 {
if (uniffi_mopro_checksum_func_verify_proof2() != 6153) {
return InitializationResult.apiChecksumMismatch
}
if uniffi_mopro_checksum_method_moprocircom_setup() != 40345 {
if (uniffi_mopro_checksum_method_moprocircom_setup() != 40345) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_mopro_checksum_method_moprocircom_generate_proof() != 30646) {
Expand Down
86 changes: 69 additions & 17 deletions mopro-ios/MoproKit/Example/MoproKit/AnonAadhaarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class AnonAadhaarViewController: UIViewController, WKScriptMessageHandler, WKNav
//var setupResult: SetupResult?
var generatedProof: Data?
var publicInputs: Data?
let containerView = UIView()
var textView = UITextView()

override func viewDidLoad() {
super.viewDidLoad()
runInitAction()
setupUI()

let contentController = WKUserContentController()
contentController.add(self, name: "startProvingHandler")
Expand All @@ -31,39 +35,67 @@ class AnonAadhaarViewController: UIViewController, WKScriptMessageHandler, WKNav
// Assign the configuration to the WKWebView
let webView = WKWebView(frame: view.bounds, configuration: configuration)
webView.navigationDelegate = self

view.addSubview(webView)
view.addSubview(textView)

guard let url = URL(string: "https://webview-anon-adhaar.vercel.app/") else { return }
webView.load(URLRequest(url: url))
}

func setupUI() {
textView.isEditable = false

textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)

// Make text view visible
textView.heightAnchor.constraint(equalToConstant: 200).isActive = true

NSLayoutConstraint.activate([
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20)
])
}



@objc func runInitAction() {
// Logic for init
do {
//textView.text += "Initializing library\n"
// Update the textView on the main thread
DispatchQueue.main.async {
self.textView.text += "Initializing library\n"
}

// Execute long-running tasks in the background
DispatchQueue.global(qos: .userInitiated).async {
// Record start time
let start = CFAbsoluteTimeGetCurrent()

try initializeMopro()
do {
try initializeMopro()

// Record end time and compute duration
let end = CFAbsoluteTimeGetCurrent()
let timeTaken = end - start
// Record end time and compute duration
let end = CFAbsoluteTimeGetCurrent()
let timeTaken = end - start

//textView.text += "Initializing arkzkey took \(timeTaken) seconds.\n"
} catch let error as MoproError {
print("MoproError: \(error)")
} catch {
print("Unexpected error: \(error)")
// Again, update the UI on the main thread
DispatchQueue.main.async {
self.textView.text += "Initializing arkzkey took \(timeTaken) seconds.\n"
}
} catch {
// Handle errors - update UI on main thread
DispatchQueue.main.async {
self.textView.text += "An error occurred during initialization: \(error)\n"
}
}
}
}

@objc func runProveAction(inputs: [String: [String]]) {
// Logic for prove (generate_proof2)
do {
textView.text += "Starts proving...\n"
// Record start time
let start = CFAbsoluteTimeGetCurrent()

Expand All @@ -82,10 +114,10 @@ class AnonAadhaarViewController: UIViewController, WKScriptMessageHandler, WKNav

print("Proof generation took \(timeTaken) seconds.\n")

//textView.text += "Proof generation took \(timeTaken) seconds.\n"
// TODO: Enable verify
//verifyButton.isEnabled = false
//verifyButton.isEnabled = true // Enable the Verify button once proof has been generated
textView.text += "Proof generated!!! \n"
textView.text += "Proof generation took \(timeTaken) seconds.\n"
textView.text += "---\n"
runVerifyAction()
} catch let error as MoproError {
print("MoproError: \(error)")
} catch {
Expand Down Expand Up @@ -137,4 +169,24 @@ class AnonAadhaarViewController: UIViewController, WKScriptMessageHandler, WKNav
}
}
}

@objc func runVerifyAction() {
// Logic for verify
guard let proof = generatedProof,
let publicInputs = publicInputs else {
print("Proof has not been generated yet.")
return
}
do {
// Verify Proof
let isValid = try verifyProof2(proof: proof, publicInput: publicInputs)
assert(isValid, "Proof verification should succeed")

textView.text += "Proof verification succeeded.\n"
} catch let error as MoproError {
print("MoproError: \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
}

0 comments on commit 3d71c35

Please sign in to comment.