-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPrivyManager.swift
281 lines (240 loc) · 9.81 KB
/
PrivyManager.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
import Foundation
import BigInt
import PrivySDK
struct BalanceResponse: Decodable {
let result: String
}
func divideBigInt(_ lhs: BigInt, _ rhs: BigInt) -> Decimal {
let (quotient, remainder) = lhs.quotientAndRemainder(dividingBy: rhs)
return Decimal(string: String(quotient))! + Decimal(string: String(remainder))! / Decimal(string: String(rhs))!
}
class PrivyManager: ObservableObject {
let authManager: Auth0Manager
let privy: Privy
@Published var authState = AuthState.unauthenticated
@Published var embeddedWalletState = EmbeddedWalletState.notCreated
@Published var lastSign: String?
@Published var txs: [String] = []
@Published var lastSignTypeData: String?
@Published var chain: SupportedChain = SupportedChain.sepolia
@Published var balance = "0.00"
@Published var isLoading = false
@Published var wallets = [Wallet]()
@Published var selectedWallet: Wallet?
@MainActor
init(authManager: Auth0Manager) {
self.authManager = authManager
self.privy = Privy(config: PrivyConfig(appId: "your-app-id"))
configure()
}
}
extension PrivyManager {
@MainActor
func configure() {
privy.tokenProvider = {
return try await self.authManager.getCredentials().accessToken
}
privy.onAuthStateChange = { state in
print(state)
self.authState = state
}
privy.onEmbeddedWalletStateChange = { state in
self.embeddedWalletState = state
guard case .connected(_, let wallets) = self.embeddedWalletState else { return }
self.wallets = wallets
self.selectedWallet = wallets.first
}
if (authManager.hasCredentials) {
Task {
isLoading.toggle()
_ = try? await privy.loginWithCustomAccessToken()
isLoading.toggle()
}
}
}
@MainActor
func loginWithCustomAccessToken() async throws {
_ = try await privy.loginWithCustomAccessToken()
}
@MainActor
func refreshSession() async throws {
_ = try await privy.refreshSession()
}
@MainActor
func clear() async {
await authManager.clear()
privy.logout()
}
func switchChain(_ newChain: SupportedChain) throws {
self.chain = newChain
guard case .connected(let provider, _) = embeddedWalletState else { throw PrivyError.notReady }
provider.configure(chainId: newChain.id)
Task { try await getBalance() }
}
func switchToSepolia() throws {
try switchChain(SupportedChain.sepolia)
}
@MainActor
func createWallet() async throws {
_ = try await privy.createWallet()
try switchToSepolia()
}
@MainActor
func createAdditionalWallet() async throws {
guard case .connected(let provider, _) = embeddedWalletState else { throw PrivyError.notReady }
_ = try await provider.createAdditionalWallet()
}
@MainActor
func connectWallet() async throws {
_ = try await privy.connectWallet()
selectedWallet = wallets[0]
try switchToSepolia()
}
@MainActor
func recoverWallet(password: String?) async throws {
_ = try? await privy.recover()
}
@MainActor
func sendTransaction(address: String, amount: String) async throws {
guard case .connected(let provider, _) = embeddedWalletState else { throw PrivyError.notReady }
guard let wallet = selectedWallet else { throw PrivyWalletError.noWalletAvailable }
do {
let tx = try JSONEncoder().encode([
// Value here is in wei (see conversion tool @ https://eth-converter.com/)
"value": amount,
"to": address,
"chainId": Utils.toHexString(provider.chainId),
"from": wallet.address
])
guard let txString = String(data: tx, encoding: .utf8) else {
throw PrivyError.dataParse
}
let data = RpcRequest(method: "eth_sendTransaction", params: [txString])
let response = try await provider.request(data, wallet.address)
txs.append(response.response.data)
print(txs)
} catch {
print(error)
}
}
@MainActor
func signMessage() async throws {
guard case .connected(let provider, _) = embeddedWalletState else { throw PrivyError.notReady }
guard let wallet = selectedWallet else { throw PrivyWalletError.noWalletAvailable }
do {
let data = RpcRequest(method: "personal_sign", params: ["I am the message", wallet.address])
let response = try await provider.request(data, wallet.address)
lastSign = response.response.data
} catch {
print(error)
}
}
@MainActor
func getBalance() async throws {
guard case .authenticated = authState else {
throw PrivyError.notLoggedIn
}
guard case .connected(let provider, _) = embeddedWalletState else {
throw PrivyWalletError.notConnected
}
guard let wallet = selectedWallet else {
throw PrivyWalletError.noWalletAvailable
}
do {
let data = RpcRequest(method: "eth_getBalance", params: [wallet.address, "latest"])
let response = try await provider.request(data, wallet.address)
guard let encoded = response.response.data.data(using: .utf8) else {return}
let responseData = try JSONDecoder().decode(BalanceResponse.self, from: encoded)
guard let bigIntValue = BigInt(responseData.result.dropFirst(2), radix: 16) else {
self.balance = "0.00"
return
}
let ethDivisor = BigInt(1000000000000000000)
let eth = divideBigInt(bigIntValue, ethDivisor)
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 4
formatter.maximumFractionDigits = 8
guard let formatted = formatter.string(from: eth as NSDecimalNumber) else { return }
self.balance = formatted
} catch {
print(error)
}
}
@MainActor
func signTypedData() async throws {
guard case .connected(let provider, _) = embeddedWalletState else { throw PrivyError.notReady }
do {
// https://docs.metamask.io/wallet/reference/eth_signtypeddata_v4/
struct Param: Codable {
let types: ParamTypes
let primaryType: String
let domain: Domain
let message: Message
}
struct ParamTypes: Codable {
let eIP712Domain: [DomainType]
let person: [DomainType]
let mail: [DomainType]
enum CodingKeys: String, CodingKey {
case eIP712Domain = "EIP712Domain"
case person = "Person"
case mail = "Mail"
}
}
struct DomainType: Codable {
let name: String
let type: String
}
struct Domain: Codable {
let name: String
let version: String
let chainId: Int
let verifyingContract: String
}
struct Message: Codable {
struct W: Codable {
let name: String
let wallet: String
}
let from: W
let to: W
let contents: String
}
let eIP712Domain = [
DomainType(name: "name", type: "string"),
DomainType(name: "version", type: "string"),
DomainType(name: "chainId", type: "uint256"),
DomainType(name: "verifyingContract", type: "address"),
]
let person = [
DomainType(name: "name", type: "string"),
DomainType(name: "wallet", type: "address")
]
let mail = [
DomainType(name: "from", type: "Person"),
DomainType(name: "to", type: "Person"),
DomainType(name: "contents", type: "string"),
]
let types = ParamTypes(eIP712Domain: eIP712Domain, person: person, mail: mail)
let primaryType = "Mail"
let domain = Domain(name: "Ether Mail", version: "1", chainId: 1, verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC")
let to = Message.W(name: "Bob", wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB")
let from = Message.W(name: "Cow", wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
let contents = "Hello, Bob!"
let message = Message(from: from, to: to, contents: contents)
let param = Param(types: types, primaryType: primaryType, domain: domain, message: message)
let encodedParam = try JSONEncoder().encode(param)
guard let paramJsonString = String(data: encodedParam, encoding: .utf8) else {
throw PrivyError.dataParse
}
guard let wallet = selectedWallet else {
throw PrivyWalletError.noWalletAvailable
}
let data = RpcRequest(method: "eth_signTypedData_v4", params: [wallet.address, paramJsonString])
let response = try await provider.request(data, wallet.address)
lastSignTypeData = response.response.data
} catch {
print(error)
}
}
}