Skip to content

Commit

Permalink
update mpc ios examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushBherwani1998 committed Sep 18, 2024
1 parent 2b38e77 commit d413c45
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/tkey/mpc-core-kit-swift";
requirement = {
branch = main;
branch = update_dependencies;
kind = branch;
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"originHash" : "6eb679ca10e6ba273c0a4c520af3222830fe4d28810a355d4409ece0c2cf4329",
"originHash" : "4d094b864a39c380b5e848611658ba8bd3cac9cdcd330d5fac7b11f59399ea97",
"pins" : [
{
"identity" : "anycodable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Flight-School/AnyCodable",
"state" : {
"revision" : "862808b2070cd908cb04f9aafe7de83d35f81b05",
"version" : "0.6.7"
}
},
{
"identity" : "auth0.swift",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -42,17 +33,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/customauth-swift-sdk",
"state" : {
"revision" : "60b48acb161fb4341c353c27bc6dd96af76887ff",
"version" : "10.0.1"
"branch" : "features_update",
"revision" : "576139ce446e397bce4f2b68b9b7d28ab5b0082f"
}
},
{
"identity" : "fetch-node-details-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/fetch-node-details-swift.git",
"state" : {
"revision" : "22bfadf7491d77a0bc1953af002cadbd61383e7d",
"version" : "6.0.2"
"revision" : "4bd96c33ba8d02d9e27190c5c7cedf09cfdfd656",
"version" : "6.0.3"
}
},
{
Expand Down Expand Up @@ -87,8 +78,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tkey/mpc-core-kit-swift",
"state" : {
"branch" : "main",
"revision" : "183f205cb24ff8aa9c6c59bea905f365fe03d34e"
"branch" : "update_dependencies",
"revision" : "37cc49d743f7e4b211ec6168bdaf3110288bdd88"
}
},
{
Expand Down Expand Up @@ -132,8 +123,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Web3Auth/single-factor-auth-swift",
"state" : {
"revision" : "73c5066d369c7d2aa1057c56a89b7f1dcdda3270",
"version" : "5.0.0"
"branch" : "7.0.0",
"revision" : "d9a8fd7885e37e74e61010f8fb240cbbafd1e577"
}
},
{
Expand Down Expand Up @@ -249,17 +240,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tkey/tkey-mpc-swift",
"state" : {
"revision" : "8fe0cc8d5cc2ad5d38bf068bc29f6af7e3146d08",
"version" : "3.0.0"
"branch" : "update_features",
"revision" : "00ed189447df83681092aabd5c64e65671ccd0d1"
}
},
{
"identity" : "torus-utils-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/torus-utils-swift.git",
"state" : {
"revision" : "ff85c3e96bfa29013309b487875c4d9383e4ac80",
"version" : "8.1.1"
"revision" : "23083bf8196d1f78c05b085ba8347f94734b1a16",
"version" : "9.0.1"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ struct EthereumClient {
}
}

func broadcastSignedTransaction(transaction: SignedTransaction) async throws -> String {
func broadcastSignedTransaction(transactionHex: String) async throws -> String {
do {
guard let transactionHex = transaction.raw?.web3.hexString else {
throw EthereumClientError.encodeIssue
}

let data = try await web3Client.networkProvider.send(
method: "eth_sendRawTransaction",
params: [transactionHex],
params: ["0x" + transactionHex],
receive: String.self
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import tkey
import MpcProviderSwift
import web3

extension MpcCoreKit : EvmSigner {
public func sign(message: Data) throws -> Data {
let data = try self.tssSign(message: message)
return data
}

public var publicKey: Data {
let fullAddress = try! KeyPoint(
address: self.getTssPubKey().hexString
).getPublicKey(format: .FullAddress)

return Data(hex: fullAddress).suffix(64)
}

}
//extension MpcCoreKit : EvmSigner {
// public func sign(message: Data) throws -> Data {
// let data = try self.tssSign(message: message)
// return data
// }
//
// public var publicKey: Data {
// let fullAddress = try! KeyPoint(
// address: self.getTssPubKey().hexString
// ).getPublicKey(format: .FullAddress)
//
// return Data(hex: fullAddress).suffix(64)
// }
//
//}

Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ class MainViewModel: ObservableObject {
@Published var showAlert: Bool = false
@Published var isLoaderVisible: Bool = false

var publicAddress: String!

var publicAddress: EthereumAddress!

private var mpcCoreKit: MpcCoreKit!
private var ethereumClient: EthereumClient!
private var mpcEthereumProvider: MPCEthereumProvider!
private var webAuth: WebAuth!
var userInfo: [String: Any]!
var alertContent: String = ""
var loaderContent: String = ""

func initialize() {
mpcCoreKit = MpcCoreKit(
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
web3AuthNetwork: .SAPPHIRE_MAINNET,
localStorage: UserStorage()
mpcCoreKit = try! MpcCoreKit(
options: .init(
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
manualSync: false,
web3AuthNetwork: .sapphire(.SAPPHIRE_MAINNET),
localStorage: UserStorage(),
overwriteMetadataUrl: "http://127.0.0.1:5051"
)
)

webAuth = Auth0.webAuth(clientId: "hUVVf4SEsZT7syOiL0gLU9hFEtm2gQ6O", domain: "web3auth.au.auth0.com")
Expand Down Expand Up @@ -72,7 +74,7 @@ class MainViewModel: ObservableObject {

if(!self.isRecoveryRequired) {
try await login()

}

hideLoader()
Expand Down Expand Up @@ -131,12 +133,17 @@ class MainViewModel: ObservableObject {
func signMessage(onSigned: @escaping (_ signedMessage: String?, _ error: String?) -> ()){
Task {
do {
showLoader("Signing Message")
let signature = try mpcEthereumProvider.signMessage(
message: "Welcome to Web3Auth".data(using: .ascii)!
)
let message = "YOUR_MESSAGE".data(using: .ascii)!
let prefix = "\u{19}Ethereum Signed Message:\n\(String(message.count))"
guard var data = prefix.data(using: .ascii) else {
throw "Incorrect Data"
}

data.append(message)
let hash = data.web3.keccak256
let signature = try await mpcCoreKit.tssSign(message: hash)
hideLoader()
onSigned(signature, nil)
onSigned(signature.hexString, nil)
} catch let error {
hideLoader()
onSigned(nil, error.localizedDescription)
Expand All @@ -149,23 +156,20 @@ class MainViewModel: ObservableObject {
Task {
do {
showLoader("Sending Transaction")
let address = EthereumAddress(
stringLiteral: self.publicAddress
)
let transaction = EthereumTransaction.init(
to: address,
data: Data.init(hex: "0x00")
to: self.publicAddress,
data: Data.init(hex: "0x00")!
)

let gasLimit = try await self.ethereumClient.getGasLimit(
transaction: transaction
)
let gasPrice = try await self.ethereumClient.getGasPrice()
let nonce = try await self.ethereumClient.getNonce(address: address)
let nonce = try await self.ethereumClient.getNonce(address: self.publicAddress)

let finalTransaction = EthereumTransaction(
from: address,
to: address,
from: self.publicAddress,
to: self.publicAddress,
value: 1000000000000,
data: transaction.data,
nonce: nonce,
Expand All @@ -174,12 +178,10 @@ class MainViewModel: ObservableObject {
chainId: Int(self.ethereumClient.getChainId())
)

let signedTransaction = try mpcEthereumProvider.sign(
transaction: finalTransaction
)
let signedTransaction = try await mpcCoreKit.tssSign(message: finalTransaction.raw!.web3.keccak256)

let hash = try await ethereumClient.broadcastSignedTransaction(
transaction: signedTransaction
transactionHex: signedTransaction.hexString
)
hideLoader()
onSend(hash, nil)
Expand All @@ -199,18 +201,15 @@ class MainViewModel: ObservableObject {
do {
showLoader("Adding new factor")
let factor = try await mpcCoreKit.createFactor(
tssShareIndex: .RECOVERY,
tssShareIndex: .recovery,
factorKey: nil,
factorDescription: .SeedPhrase
)

guard let seedPhrase = mpcCoreKit.keyToMnemonic(
let seedPhrase = try mpcCoreKit.keyToMnemonic(
factorKey: factor,
format: "mnemonic"
) else {
return
}

)
print(seedPhrase)

UIPasteboard.general.string = seedPhrase
Expand All @@ -228,13 +227,10 @@ class MainViewModel: ObservableObject {
Task {
do {
showLoader("Recovering account")
guard let factorKey = mpcCoreKit.mnemonicToKey(
let factorKey = try mpcCoreKit.mnemonicToKey(
shareMnemonic: seedPhrase,
format: "mnemonic"
) else {
hideLoader()
return
}
)

try await mpcCoreKit.inputFactor(
factorKey: factorKey
Expand All @@ -258,19 +254,16 @@ class MainViewModel: ObservableObject {
Task {
do {
showLoader("Enabling MFA")
let recoveryFactorKey = try await mpcCoreKit.enableMFA()
guard let seedPhrase = mpcCoreKit.keyToMnemonic(
factorKey: recoveryFactorKey!,
format: "mnemonic"
) else {
hideLoader()
return
}

print(seedPhrase)

UIPasteboard.general.string = seedPhrase
showAlert(message: "MFA is enabled, and seedphrase is copied to clipboard. \(seedPhrase)")
let recoveryFactorKey = try await mpcCoreKit.enableMFAWithRecoveryFactor()
// let seedPhrase = try mpcCoreKit.keyToMnemonic(
// factorKey: recoveryFactorKey,
// format: "mnemonic"
// )
//
// print(seedPhrase)
//
// UIPasteboard.general.string = seedPhrase
// showAlert(message: "MFA is enabled, and seedphrase is copied to clipboard. \(seedPhrase)")
hideLoader()
await refreshFactorPubs()
} catch let error {
Expand All @@ -286,19 +279,16 @@ class MainViewModel: ObservableObject {
let keyDetails = try await mpcCoreKit.getKeyDetails()
print(keyDetails.requiredFactors)

mpcEthereumProvider = MPCEthereumProvider(evmSigner: mpcCoreKit)

let fullAddress = try KeyPoint(
address: pubKey.hexString
).getPublicKey(format: .FullAddress)


let address = KeyUtil.generateAddress(
from: Data(hex: fullAddress).suffix(64)
publicAddress = KeyUtil.generateAddress(
from: Data(hex: fullAddress)!.suffix(64)
)
print(address)

publicAddress = mpcEthereumProvider.address.toChecksumAddress()
await refreshFactorPubs()
toggleIsLoggedIn()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct HomeView: View {
Button(
action: {
viewModel.showAlert(message: "Address is copied to clipboard")
UIPasteboard.general.string = viewModel.publicAddress
UIPasteboard.general.string = viewModel.publicAddress.toChecksumAddress()
}, label: {
Text(viewModel.publicAddress)
Text(viewModel.publicAddress.toChecksumAddress())
})

Button(
Expand Down
Loading

0 comments on commit d413c45

Please sign in to comment.