-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCircleSmartAccount.swift
More file actions
550 lines (467 loc) · 19.7 KB
/
CircleSmartAccount.swift
File metadata and controls
550 lines (467 loc) · 19.7 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//
// Copyright (c) 2025, Circle Internet Group, Inc. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import BigInt
import Web3Core
import web3swift
/// Creates a Circle smart account.
///
/// - Parameters:
/// - client: The client used to interact with the blockchain.
/// - owner: The owner account associated with the Circle smart account.
/// - version: The version of the Circle smart account. Default is CIRCLE_SMART_ACCOUNT_VERSION_V1.
/// - name: The wallet name assigned to the newly registered account defaults to the format "passkey-yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
///
/// - Throws: BaseError if there are any problems during the wallet creation process.
///
/// - Returns: The created Circle smart account.
public func toCircleSmartAccount<A: Account>(
client: Client,
owner: A,
version: String = CIRCLE_SMART_ACCOUNT_VERSION_V1,
name: String? = nil
) async throws -> CircleSmartAccount<A> where A.T == SignResult {
let version = CIRCLE_SMART_ACCOUNT_VERSION[version] ?? version
let name = name ?? "passkey-\(Utils.getCurrentDateTime())"
return try await .init(client: client, owner: owner, version: version, name: name)
}
public class CircleSmartAccount<A: Account>: SmartAccount, @unchecked Sendable where A.T == SignResult {
public let client: Client
public let entryPoint: EntryPoint
let owner: A
let wallet: ModularWallet
private var deployed: Bool = false
private let nonceManager = NonceManager(source: NonceManagerSourceImpl())
init(client: Client, owner: A, wallet: ModularWallet, entryPoint: EntryPoint = .v07) {
self.client = client
self.owner = owner
self.wallet = wallet
self.entryPoint = entryPoint
}
convenience init(client: Client, owner: A, version: String, name: String?) async throws {
guard let buidlTransport = client.transport as? ModularTransport else {
throw BaseError(shortMessage: "The property client.transport is not the ModularTransport")
}
guard let webAuthnAccount = owner as? WebAuthnAccount else {
throw BaseError(shortMessage: "The property owner is not the WebAuthnAccount")
}
let wallet = try await Self.createWallet(
transport: buidlTransport,
hexPublicKey: webAuthnAccount.credential.publicKey,
version: version,
name: name
)
self.init(client: client, owner: owner, wallet: wallet)
}
public var userOperation: UserOperationConfiguration? {
get async {
let minimumVerificationGasLimit = SmartAccountUtils.getMinimumVerificationGasLimit(
deployed: await self.isDeployed(),
chainId: client.chain.chainId
)
let config = UserOperationConfiguration { userOperation in
let verificationGasLimit = BigInt(minimumVerificationGasLimit)
let maxGasLimit = max(verificationGasLimit, userOperation.verificationGasLimit ?? BigInt(0))
return EstimateUserOperationGasResult(preVerificationGas: nil,
verificationGasLimit: maxGasLimit,
callGasLimit: nil,
paymasterVerificationGasLimit: nil,
paymasterPostOpGasLimit: nil)
}
return config
}
}
public func getAddress() -> String {
return wallet.address ?? ""
}
public func encodeCalls(args: [EncodeCallDataArg]) -> String? {
return Utils.encodeCallData(args: args)
}
public func getFactoryArgs() async throws -> (String, String)? {
if await isDeployed() {
return nil
}
guard let initCode = wallet.getInitCode() else {
throw BaseError(shortMessage: "There is no the initCode (factory address and data)")
}
return Utils.parseFactoryAddressAndData(initCode: initCode)
}
public func getNonce(key: BigInt?) async throws -> BigInt {
let _key = key ?? nonceManager.consume(
params: FunctionParameters(address: getAddress(),
chainId: client.chain.chainId)
)
guard _key >= BigInt(0) else {
throw BaseError(shortMessage: "Cannot convert negative BigInt(\(_key) to BigUInt")
}
let nonce = try await Utils.getNonce(transport: client.transport,
address: getAddress(),
entryPoint: entryPoint,
key: BigUInt(_key))
return nonce
}
public func getStubSignature<T: UserOperation>(userOp: T) -> String {
return STUB_SIGNATURE
}
public func sign(hex: String) async throws -> String {
let digest = Utils.toSha3Data(message: hex)
let hash = getReplaySafeHash(
chainId: client.chain.chainId,
account: getAddress(),
hash: HexUtils.dataToHex(digest)
)
do {
let signResult = try await owner.sign(hex: hash)
let signature = encodePackedForSignature(
signResult: signResult,
publicKey: owner.getAddress(),
hasUserOpGas: false
)
return signature
} catch let error as BaseError {
throw error
} catch {
throw BaseError(shortMessage: "CircleSmartAccount.owner.sign(hex: \"\(hash)\") failure",
args: .init(cause: error, name: String(describing: error)))
}
}
public func signMessage(message: String) async throws -> String {
guard let messageHash = Utilities.hashPersonalMessage(Data(message.utf8)) else {
throw BaseError(shortMessage: "Failed to hash message: \"\(message)\"")
}
let messageHashHex = HexUtils.dataToHex(messageHash)
let digest = Utils.toSha3Data(message: messageHashHex)
let hash = getReplaySafeHash(
chainId: client.chain.chainId,
account: getAddress(),
hash: HexUtils.dataToHex(digest)
)
do {
let signResult = try await owner.sign(hex: hash)
let signature = encodePackedForSignature(
signResult: signResult,
publicKey: owner.getAddress(),
hasUserOpGas: false
)
return signature
} catch let error as BaseError {
throw error
} catch {
throw BaseError(shortMessage: "CircleSmartAccount.owner.sign(hex: \"\(hash)\") failure",
args: .init(cause: error, name: String(describing: error)))
}
}
public func signTypedData(typedData: String) async throws -> String {
guard let typedData = try? EIP712Parser.parse(typedData),
let typedDataHash = try? typedData.signHash() else {
logger.passkeyAccount.error("typedData signHash failure")
throw BaseError(shortMessage: "Failed to hash TypedData: \"\(typedData)\"")
}
let typedDataHashHex = HexUtils.dataToHex(typedDataHash)
let digest = Utils.toSha3Data(message: typedDataHashHex)
let hash = getReplaySafeHash(
chainId: client.chain.chainId,
account: getAddress(),
hash: HexUtils.dataToHex(digest)
)
do {
let signResult = try await owner.sign(hex: hash)
let signature = encodePackedForSignature(
signResult: signResult,
publicKey: owner.getAddress(),
hasUserOpGas: false
)
return signature
} catch let error as BaseError {
throw error
} catch {
throw BaseError(shortMessage: "CircleSmartAccount.owner.sign(hex: \"\(hash)\") failure",
args: .init(cause: error, name: String(describing: error)))
}
}
public func signUserOperation(chainId: Int, userOp: UserOperationV07) async throws -> String {
userOp.sender = getAddress()
let userOpHash = try Utils.getUserOperationHash(
chainId: chainId,
entryPointAddress: EntryPoint.v07.address,
userOp: userOp
)
let hash = Utils.hashMessage(hex: userOpHash)
do {
let signResult = try await owner.sign(hex: hash)
let signature = encodePackedForSignature(
signResult: signResult,
publicKey: owner.getAddress(),
hasUserOpGas: true
)
return signature
} catch let error as BaseError {
throw error
} catch {
throw BaseError(shortMessage: "CircleSmartAccount.owner.sign(hex: hash(\"\(userOpHash)\")) failure",
args: .init(cause: error, name: String(describing: error)))
}
}
public func getInitCode() -> String? {
return wallet.getInitCode()
}
}
extension CircleSmartAccount: PublicRpcApi {
// MARK: Internal Usage
static func createWallet(
transport: ModularTransport,
hexPublicKey: String,
version: String,
name: String? = nil
) async throws -> ModularWallet {
let (publicKeyX, publicKeyY) = Self.extractXYFromCOSE(hexPublicKey)
let request = GetAddressReq(
scaConfiguration: ScaConfiguration(
initialOwnershipConfiguration: .init(
ownershipContractAddress: nil,
weightedMultiSig: .init(
owners: nil,
webauthnOwners: [.init(publicKeyX: publicKeyX.description,
publicKeyY: publicKeyY.description,
weight: PUBLIC_KEY_OWN_WEIGHT)],
thresholdWeight: THRESHOLD_WEIGHT)
),
scaCore: version,
initCode: nil),
metadata: .init(name: name)
)
let wallet = try await transport.getAddress(transport: transport, req: request)
return wallet
}
private func isDeployed() async -> Bool {
if deployed { return true }
do {
let byteCode = try await getCode(transport: client.transport,
address: getAddress())
let isEmpty = try HexUtils.hexToBytes(hex: byteCode).isEmpty
deployed = !isEmpty
return deployed
} catch {
return false
}
}
/// Remove the private access control for unit testing
func getReplaySafeHash(
chainId: Int,
account: String,
hash: String,
verifyingContract: String = CIRCLE_WEIGHTED_WEB_AUTHN_MULTISIG_PLUGIN
) -> String {
// Get the prefix
let messagePrefix = "0x1901"
let prefix = HexUtils.hexToData(hex: messagePrefix) ?? .init()
// Get the domainSeparatorHash
let domainSeparatorTypeHash =
Utils.toSha3Data(message: "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)")
var types: [ABI.Element.ParameterType] = [
.bytes(length: 32),
.bytes(length: 32),
.uint(bits: 256),
.address,
.bytes(length: 32)
]
var values: [Any] = [
domainSeparatorTypeHash,
Self.getModuleIdHash(),
chainId,
verifyingContract,
Utils.pad(data: Utils.toData(value: account), isRight: true)
]
var domainSeparator = Data()
if let encoded = ABIEncoder.encode(types: types, values: values) {
domainSeparator = encoded
}
let domainSeparatorHash = domainSeparator.sha3(.keccak256)
// Get the structHash
guard let bytes = try? HexUtils.hexToBytes(hex: hash) else {
logger.passkeyAccount.error("Failed to decode the hash of getReplaySafeHash into UInt8 array.")
return ""
}
types = [.bytes(length: 32), .bytes(length: 32)]
values = [Self.getModuleTypeHash(), bytes]
var structData = Data()
if let encoded = ABIEncoder.encode(types: types, values: values) {
structData = encoded
}
let structHash = structData.sha3(.keccak256)
// Concat the prefix, domainSeparatorHash and domainSeparatorHash
let replaySafeHash = (prefix + domainSeparatorHash + structHash).sha3(.keccak256)
return HexUtils.dataToHex(replaySafeHash)
}
/// Remove the private access control for unit testing
func encodePackedForSignature(
signResult: SignResult,
publicKey: String,
hasUserOpGas: Bool
) -> String {
let pubKey = Self.extractXYFromCOSE(publicKey)
let sender = Self.getSender(x: pubKey.0, y: pubKey.1)
let formattedSender = Self.getFormattedSender(sender: sender)
let sigType: UInt8 = hasUserOpGas ? 34 : 2
let sigBytes = encodeWebAuthnSigDynamicPart(signResult: signResult)
let encoded = Utils.encodePacked([
formattedSender,
/// dynamicPos
/// 32-bytes public key onchain id
/// 32-bytes webauth, signature and public key position
/// 1-byte signature type
/// https://github.com/circlefin/buidl-wallet-contracts/blob/7388395fac2ac8bcd19af9a1caaac5df3c4813f2/docs/Smart_Contract_Signatures_Encoding.md#sigtype--2
65,
sigType,
sigBytes.count,
sigBytes
]).addHexPrefix()
return encoded
}
private func encodeWebAuthnSigDynamicPart(signResult: SignResult) -> Data {
guard let (rData, sData) = Utils.extractRSFromDER(signResult.signature) else {
logger.passkeyAccount.notice("Can't extract the r, s from a DER-encoded ECDSA signature")
return .init()
}
let (r, s) = (BigUInt(rData), BigUInt(sData))
let encoded = Self.encodeParametersWebAuthnSigDynamicPart(
authenticatorDataString: signResult.webAuthn.authenticatorData,
clientDataJSON: signResult.webAuthn.clientDataJSON,
challengeIndex: signResult.webAuthn.challengeIndex,
typeIndex: signResult.webAuthn.typeIndex,
userVerificationRequired: signResult.webAuthn.userVerificationRequired,
r: r,
s: s
)
return encoded
}
static func getModuleIdHash() -> Data {
let message = Utils.encodePacked(["Weighted Multisig Webauthn Plugin", "1.0.0"])
return Utils.toSha3Data(message:message)
}
static func getModuleTypeHash() -> Data {
return Utils.toSha3Data(message: "CircleWeightedWebauthnMultisigMessage(bytes32 hash)")
}
static func encodeParametersWebAuthnSigDynamicPart(
authenticatorDataString: String, // Hex
clientDataJSON: String, // Base64URL decoded
challengeIndex: Int,
typeIndex:Int,
userVerificationRequired: Bool,
r: BigUInt,
s: BigUInt
) -> Data {
let types: [ABI.Element.ParameterType] = [
.tuple(types: [
.tuple(types: [
.dynamicBytes,
.string,
.uint(bits: 256),
.uint(bits: 256),
.bool]),
.uint(bits: 256),
.uint(bits: 256)
])
]
var authenticatorData = [UInt8]()
if let _authenticatorData = try? HexUtils.hexToBytes(hex: authenticatorDataString) {
authenticatorData = _authenticatorData
}
let values: [Any] = [
[
[
authenticatorData,
clientDataJSON,
BigUInt(challengeIndex),
BigUInt(typeIndex),
userVerificationRequired
],
r,
s
]
]
var encoded = Data()
if let _encoded = ABIEncoder.encode(types: types, values: values) {
encoded = _encoded
}
return encoded
}
private static func extractXYFromCOSE(_ keyHex: String) -> (BigUInt, BigUInt) {
let xy = Self.extractXYFromCOSEBytes(keyHex)
return (BigUInt(Data(xy.0)), BigUInt(Data(xy.1)))
}
private static func extractXYFromCOSEBytes(_ keyHex: String) -> ([UInt8], [UInt8]) {
guard let bytes = try? HexUtils.hexToBytes(hex: keyHex) else {
logger.passkeyAccount.error("Failed to decode the publicKey (COSE_Key format) hex string into UInt8 array.")
return (.init(), .init())
}
// EC2 key type
guard bytes.count == 77 else {
logger.passkeyAccount.error("Insufficient bytes length; does not comply with COSE Key format EC2 key type.")
return (.init(), .init())
}
let offset = 10
let x = Array(bytes[offset..<(offset + 32)])
let y = Array(bytes[(offset + 32 + 3)..<(offset + 64 + 3)])
return (x, y)
}
static func getSender(x: BigUInt, y: BigUInt) -> String {
let encoded = encodeParametersGetSender(x, y)
let hash = encoded.sha3(.keccak256)
return HexUtils.dataToHex(hash)
}
static func encodeParametersGetSender(_ x: BigUInt, _ y: BigUInt) -> Data {
let types: [ABI.Element.ParameterType] = [
.uint(bits: 256),
.uint(bits: 256)
]
let values = [x, y]
let encoded = ABIEncoder.encode(types: types, values: values) ?? Data()
return encoded
}
static func getFormattedSender(sender: String) -> [UInt8] {
func slice(value: String,
start: Int? = nil,
end: Int? = nil,
strict: Bool = false) -> String {
let cleanValue = value.noHexPrefix
let startIndex = (start ?? 0) * 2
let endIndex = (end ?? (cleanValue.count / 2)) * 2
guard startIndex >= 0, endIndex <= cleanValue.count, startIndex <= endIndex else {
logger.passkeyAccount.notice("Return \"0x\" if indices are invalid")
return "0x"
}
let slicedValue = "0x" + cleanValue[startIndex..<endIndex]
// This block is never executed because the `strict` parameter is always set to its default value (`false`).
// if strict {
// guard slicedValue.range(of: "^0x[0-9a-fA-F]*$", options: .regularExpression) != nil else {
// logger.passkeyAccount.notice("Invalid hexadecimal string")
// return "0x"
// }
// }
return slicedValue
}
let slicedSender = slice(value: sender, start: 2)
let paddedSlicedSender = slicedSender.noHexPrefix.leftPadding(toLength: 32 * 2, withPad: "0").addHexPrefix()
guard let formattedSender = try? HexUtils.hexToBytes(hex: paddedSlicedSender) else {
logger.passkeyAccount.error("Failed to get formatted sender")
return .init()
}
return formattedSender
}
}