Skip to content
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

update example #12

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ContentView: View {
ProgressView()
} else {
if vm.loggedIn {
ThresholdKeyView(userData: vm.userData)
ThresholdKeyView(userData: vm.userData!)
.tabItem {
Image(systemName: "house.circle")
Text("Home")
Expand Down
27 changes: 8 additions & 19 deletions Shared/LoginModel.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Foundation
import CustomAuth
import Foundation
import FetchNodeDetails
import TorusUtils
import CommonSources

let ClientID = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"

let Network = TorusNetwork.sapphire(.SAPPHIRE_MAINNET)
let Network: TorusNetwork = .sapphire(.SAPPHIRE_MAINNET)
let verifier = "w3a-google-demo"

class LoginModel: ObservableObject {
@Published var loggedIn: Bool = false
@Published var isLoading = false
@Published var navigationTitle: String = ""
@Published var userData: TorusKeyData!
@Published var userData: TorusLoginResponse?

func setup() async {
await MainActor.run(body: {
Expand All @@ -28,20 +28,11 @@ class LoginModel: ObservableObject {
}

func loginWithCustomAuth() {

Task {
let verifier = "w3a-google-demo"
let sub = SubVerifierDetails(loginType: .web,
loginProvider: .google,
clientId: "519228911939-cri01h55lsjbsia1k7ll6qpalrus75ps.apps.googleusercontent.com",
verifier: verifier,
redirectURL: "tdsdk://tdsdk/oauthCallback",
browserRedirectURL: "https://scripts.toruswallet.io/redirect.html")
let tdsdk = CustomAuth( web3AuthClientId: ClientID, aggregateVerifierType: .singleLogin, aggregateVerifier: verifier, subVerifierDetails: [sub], network: Network, enableOneKey: true)

do {
let data = try await tdsdk.triggerLogin()
print(data)
let tdsdk = try CustomAuth(config: CustomAuthArgs(urlScheme: "tdsdk://tdsdk/oauthCallback", network: Network, enableOneKey: true, web3AuthClientId: ClientID))

let data = try await tdsdk.triggerLogin(args: SubVerifierDetails(typeOfLogin: .google, verifier: verifier, clientId: "519228911939-cri01h55lsjbsia1k7ll6qpalrus75ps.apps.googleusercontent.com", redirectURL: "https://scripts.toruswallet.io/redirect.html"))

await MainActor.run(body: {
self.userData = data
Expand All @@ -50,8 +41,6 @@ class LoginModel: ObservableObject {
} catch {
print(error)
}

}
}

}
71 changes: 16 additions & 55 deletions Shared/ThresholdKeyView.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import SwiftUI
import TorusUtils
import FetchNodeDetails
import CommonSources
import CustomAuth
import tkey_mpc_swift
import tkey

enum SpinnerLocation {
case add_password_btn, change_password_btn, init_reconstruct_btn, nowhere
Expand All @@ -15,7 +14,7 @@ enum SpinnerLocation {
// }

struct ThresholdKeyView: View {
@State var userData: TorusKeyData
@State var userData: TorusLoginResponse
@State private var showAlert = false
@State private var alertContent = ""
@State private var totalShares = 0
Expand Down Expand Up @@ -53,18 +52,8 @@ struct ThresholdKeyView: View {
showAlert = true
alertContent = "Resetting your accuont.."
do {
guard let finalKeyData = userData.torusKey.finalKeyData else {
alertContent = "Failed to get public address from userinfo"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
guard let postboxkey = finalKeyData.privKey else {
alertContent = "Failed to get public address from userinfo"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
let finalKeyData = userData.torusKey
let postboxkey = finalKeyData.finalKeyData.privKey
let temp_storage_layer = try StorageLayer(enable_logging: true, host_url: "https://metadata.tor.us", server_time_offset: 2)
let temp_service_provider = try ServiceProvider(enable_logging: true, postbox_key: postboxkey)
let temp_threshold_key = try ThresholdKey(
Expand Down Expand Up @@ -151,42 +140,19 @@ struct ThresholdKeyView: View {
func initialize () {
Task {
showSpinner = SpinnerLocation.init_reconstruct_btn
guard let finalKeyData = userData.torusKey.finalKeyData else {
alertContent = "Failed to get public address from userinfo"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
let finalKeyData = userData.torusKey.finalKeyData

guard let verifierLocal = userData.userInfo["verifier"] as? String, let verifierIdLocal = userData.userInfo["verifierId"] as? String else {
alertContent = "Failed to get verifier or verifierId from userinfo"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
verifier = verifierLocal
verifierId = verifierIdLocal
let postboxkey = finalKeyData.privKey

guard let postboxkey = finalKeyData.privKey else {
alertContent = "Failed to get postboxkey"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
verifier = userData.singleVerifierResponse.userInfo.verifier
verifierId = userData.singleVerifierResponse.userInfo.verifierId

print(finalKeyData)
print(postboxkey)
postboxkeyGlobal = postboxkey
guard let sessionData = userData.torusKey.sessionData else {
alertContent = "Failed to get sessionData"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
let sessionData = userData.torusKey.sessionData
let sessionTokenData = sessionData.sessionTokenData

signatures = sessionTokenData.map { token in
return [ "data": Data(hex: token!.token).base64EncodedString(),
return [ "data": token!.token,
"sig": token!.signature ]
}
assert(signatures.isEmpty != true)
Expand All @@ -197,11 +163,11 @@ struct ThresholdKeyView: View {
showSpinner = SpinnerLocation.nowhere
return
}
torusUtils = TorusUtils( enableOneKey: true,
network: Network,
clientId: ClientID
)

torusUtils = try TorusUtils(params: TorusOptions(clientId: ClientID, network: Network, enableOneKey: true))

let fnd = NodeDetailManager(network: Network)

nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierId)

tssEndpoint = nodeDetails!.torusNodeTSSEndpoints
Expand Down Expand Up @@ -490,13 +456,8 @@ struct ThresholdKeyView: View {
showAlert = true
alertContent = "Resetting your accuont.."
do {
guard let finalKeyData = userData.torusKey.finalKeyData else {
alertContent = "Failed to get public address from userinfo"
showAlert = true
showSpinner = SpinnerLocation.nowhere
return
}
let postboxkey = finalKeyData.privKey!
let finalKeyData = userData.torusKey.finalKeyData
let postboxkey = finalKeyData.privKey
let temp_storage_layer = try StorageLayer(enable_logging: true, host_url: "https://metadata.tor.us", server_time_offset: 2)
let temp_service_provider = try ServiceProvider(enable_logging: true, postbox_key: postboxkey)
let temp_threshold_key = try ThresholdKey(
Expand Down
18 changes: 9 additions & 9 deletions Shared/TssView.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import BigInt
import CommonSources
import CryptoKit
import FetchNodeDetails
import Foundation
import SwiftUI
import TorusUtils
import tss_client_swift
import tssClientSwift
import Web3SwiftMpcProvider
import web3
import tkey_mpc_swift
import tkey

struct TssView: View {
@Binding var threshold_key: ThresholdKey!
Expand Down Expand Up @@ -376,16 +375,16 @@ struct TssView: View {

let params = EthTssAccountParams(publicKey: fullAddress, factorKey: factorKey, tssNonce: tssNonce, tssShare: tssShare, tssIndex: tssIndex, selectedTag: selected_tag, verifier: verifier, verifierID: verifierId, nodeIndexes: [], tssEndpoints: tssEndpoints, authSigs: sigs)

let account = EthereumTssAccount(params: params)
let account = try EthereumTssAccount(params: params)

let msg = "hello world"
let signature = try account.sign(message: msg)
let r = BigInt( sign: .plus, magnitude: BigUInt(signature.prefix(32)))
let s = BigInt( sign: .plus, magnitude: BigUInt(signature.prefix(64).suffix(32)))
let v = UInt8(signature.suffix(1).toHexString(), radix: 16 )!

let msgHash = TSSHelpers.hashMessage(message: msg)
if TSSHelpers.verifySignature(msgHash: msgHash, s: s, r: r, v: v, pubKey: Data(hex: fullAddress)) {
let msgHash = try TSSHelpers.hashMessage(message: msg)
if TSSHelpers.verifySignature(msgHash: msgHash, s: s, r: r, v: v, pubKey: Data(hex: fullAddress)!) {
let sigHex = try TSSHelpers.hexSignature(s: s, r: r, v: v)
alertContent = "Signature: " + sigHex
showAlert = true
Expand Down Expand Up @@ -428,21 +427,22 @@ struct TssView: View {
let tssPubKeyPoint = try KeyPoint(address: finalPubKey)
let fullTssPubKey = try tssPubKeyPoint.getPublicKey(format: PublicKeyEncoding.FullAddress)

let evmAddress = KeyUtil.generateAddress(from: Data(hex: fullTssPubKey).suffix(64) )
let evmAddress = KeyUtil.generateAddress(from: Data(hex: fullTssPubKey)!.suffix(64) )
print(evmAddress.toChecksumAddress())

// step 2. getting signature
let sigs: [String] = try signatures.map { String(decoding: try JSONSerialization.data(withJSONObject: $0), as: UTF8.self) }

let params = EthTssAccountParams(publicKey: fullTssPubKey, factorKey: factorKey, tssNonce: tssNonce, tssShare: tssShare, tssIndex: tssIndex, selectedTag: selected_tag, verifier: verifier, verifierID: verifierId, nodeIndexes: tssPublicAddressInfo.nodeIndexes, tssEndpoints: tssEndpoints, authSigs: sigs)

let tssAccount = EthereumTssAccount(params: params)
let tssAccount = try EthereumTssAccount(params: params)

let RPC_URL = "https://rpc.sepolia.org"
let chainID = 11155111
// let RPC_URL = "https://rpc.ankr.com/eth_goerli"
// let chainID = 5
let web3Client = EthereumHttpClient(url: URL(string: RPC_URL)!, network: EthereumNetwork.sepolia )

let web3Client = EthereumHttpClient(url: URL(string: RPC_URL)!, network: .sepolia)

let amount = 0.001
let toAddress = tssAccount.address
Expand Down
2 changes: 1 addition & 1 deletion Shared/helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import tkey_mpc_swift
import tkey

func convertPublicKeyFormat ( publicKey: String, outFormat: PublicKeyEncoding ) throws -> String {
let point = try KeyPoint(address: publicKey)
Expand Down
28 changes: 15 additions & 13 deletions tkey_ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
B30B6B3B28BCC8AC00812C1E /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30B6B1128BCC8A900812C1E /* ContentView.swift */; };
B30B6B3C28BCC8AC00812C1E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B30B6B1228BCC8AC00812C1E /* Assets.xcassets */; };
B30B6B3D28BCC8AC00812C1E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B30B6B1228BCC8AC00812C1E /* Assets.xcassets */; };
B33DD88A2B9AC576006EDB31 /* tkey-mpc-swift in Frameworks */ = {isa = PBXBuildFile; productRef = B33DD8892B9AC576006EDB31 /* tkey-mpc-swift */; };
B3E5316629EC4E7E00174AF1 /* ThresholdKeyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316529EC4E7E00174AF1 /* ThresholdKeyView.swift */; };
B3E5316729EC4E7E00174AF1 /* ThresholdKeyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316529EC4E7E00174AF1 /* ThresholdKeyView.swift */; };
B3E5316929EC4ED000174AF1 /* LoaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316829EC4ED000174AF1 /* LoaderView.swift */; };
B3E5316A29EC4ED000174AF1 /* LoaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316829EC4ED000174AF1 /* LoaderView.swift */; };
B3E5316F29EE75CA00174AF1 /* KeyChain.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316E29EE75CA00174AF1 /* KeyChain.swift */; };
B3E5317029EE75CA00174AF1 /* KeyChain.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3E5316E29EE75CA00174AF1 /* KeyChain.swift */; };
B3E673252C5CB9EE0006A66B /* tkey in Frameworks */ = {isa = PBXBuildFile; productRef = B3E673242C5CB9EE0006A66B /* tkey */; };
DD694C2D29713D2B00B34585 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B30B6B1128BCC8A900812C1E /* ContentView.swift */; };
F00423A22A8B64D600F11B11 /* helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F00423A12A8B64D600F11B11 /* helper.swift */; };
F00423A32A8B64D600F11B11 /* helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F00423A12A8B64D600F11B11 /* helper.swift */; };
Expand Down Expand Up @@ -98,8 +98,8 @@
buildActionMask = 2147483647;
files = (
146B0D6929CC01F7000473A8 /* CustomAuth in Frameworks */,
B3E673252C5CB9EE0006A66B /* tkey in Frameworks */,
F0B2A2282A9758C000DA2F79 /* Web3SwiftMpcProvider in Frameworks */,
B33DD88A2B9AC576006EDB31 /* tkey-mpc-swift in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -221,7 +221,7 @@
packageProductDependencies = (
146B0D6829CC01F7000473A8 /* CustomAuth */,
F0B2A2272A9758C000DA2F79 /* Web3SwiftMpcProvider */,
B33DD8892B9AC576006EDB31 /* tkey-mpc-swift */,
B3E673242C5CB9EE0006A66B /* tkey */,
);
productName = "tkey_ios (iOS)";
productReference = B30B6B1728BCC8AC00812C1E /* tkey_ios.app */;
Expand Down Expand Up @@ -562,9 +562,10 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 4LS2P2RS45;
DEVELOPMENT_TEAM = 2Q63NCPY55;
ENABLE_PREVIEWS = YES;
EXCLUDED_ARCHS = "";
GCC_NO_COMMON_BLOCKS = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -601,8 +602,9 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = 4LS2P2RS45;
DEVELOPMENT_TEAM = 2Q63NCPY55;
ENABLE_PREVIEWS = YES;
GCC_NO_COMMON_BLOCKS = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -892,24 +894,24 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/torusresearch/customauth-swift-sdk";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 9.0.0;
branch = features_update;
kind = branch;
};
};
B33DD8882B9AC576006EDB31 /* XCRemoteSwiftPackageReference "tkey-mpc-swift" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/tkey/tkey-mpc-swift";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 2.1.0;
branch = update_features;
kind = branch;
};
};
F0B2A2262A9758C000DA2F79 /* XCRemoteSwiftPackageReference "web3-swift-mpc-provider" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/tkey/web3-swift-mpc-provider/";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 2.0.0;
branch = spm_fix_path;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand All @@ -920,10 +922,10 @@
package = 146B0D6729CC01F7000473A8 /* XCRemoteSwiftPackageReference "customauth-swift-sdk" */;
productName = CustomAuth;
};
B33DD8892B9AC576006EDB31 /* tkey-mpc-swift */ = {
B3E673242C5CB9EE0006A66B /* tkey */ = {
isa = XCSwiftPackageProductDependency;
package = B33DD8882B9AC576006EDB31 /* XCRemoteSwiftPackageReference "tkey-mpc-swift" */;
productName = "tkey-mpc-swift";
productName = tkey;
};
F0B2A2272A9758C000DA2F79 /* Web3SwiftMpcProvider */ = {
isa = XCSwiftPackageProductDependency;
Expand Down
Loading