1
+ import AnyCodable
1
2
import BigInt
2
3
import FetchNodeDetails
3
4
import Foundation
4
5
import OSLog
5
- import AnyCodable
6
6
#if canImport(curveSecp256k1)
7
7
import curveSecp256k1
8
8
#endif
@@ -32,8 +32,8 @@ open class TorusUtils: AbstractTorusUtils {
32
32
self . urlSession = urlSession
33
33
utilsLogType = loglevel
34
34
self . enableOneKey = enableOneKey
35
- self . allowHost = network. signerMap + " /api/allow "
36
- self . signerHost = network. signerMap + " /api/sign "
35
+ allowHost = network. signerMap + " /api/allow "
36
+ signerHost = network. signerMap + " /api/sign "
37
37
self . network = network
38
38
self . serverTimeOffset = serverTimeOffset
39
39
self . clientId = clientId
@@ -71,7 +71,7 @@ open class TorusUtils: AbstractTorusUtils {
71
71
do {
72
72
let result = try await session. data ( for: allowHostRequest)
73
73
let responseData = try JSONDecoder ( ) . decode ( AllowSuccess . self, from: result. 0 )
74
- if ( responseData. success == false ) {
74
+ if responseData. success == false {
75
75
let errorData = try JSONDecoder ( ) . decode ( AllowRejected . self, from: result. 0 )
76
76
throw TorusUtilError . gatingError ( " code: \( errorData. code) , error: \( errorData. error) " )
77
77
}
@@ -244,26 +244,15 @@ open class TorusUtils: AbstractTorusUtils {
244
244
245
245
let timestamp = String ( Int ( getTimestamp ( ) ) )
246
246
247
- let hashedToken = keccak256Data ( idToken. data ( using: . utf8) ?? Data ( ) ) . toHexString ( )
248
- var lookupPubkeyX : String = " "
249
- var lookupPubkeyY : String = " "
247
+ let hashedToken = keccak256Data ( idToken. data ( using: . utf8) ?? Data ( ) ) . toHexString ( )
250
248
do {
251
- let getPublicAddressData = try await getPublicAddress ( endpoints: endpoints, torusNodePubs: torusNodePubs, verifier: verifier, verifierId: verifierId)
252
- guard ( getPublicAddressData. finalKeyData? . evmAddress) != nil
253
- else {
254
- throw TorusUtilError . runtime ( " Unable to provide evmAddress " )
255
- }
256
- let localPubkeyX = getPublicAddressData. finalKeyData!. X. addLeading0sForLength64 ( )
257
- let localPubkeyY = getPublicAddressData. finalKeyData!. Y. addLeading0sForLength64 ( )
258
- lookupPubkeyX = localPubkeyX
259
- lookupPubkeyY = localPubkeyY
260
249
let commitmentRequestData = try await commitmentRequest ( endpoints: endpoints, verifier: verifier, pubKeyX: pubKeyX, pubKeyY: pubKeyY, timestamp: timestamp, tokenCommitment: hashedToken)
261
250
os_log ( " retrieveShares - data after commitment request: %@ " , log: getTorusLogger ( log: TorusUtilsLogger . core, type: . info) , type: . info, commitmentRequestData)
262
251
263
252
let ( oAuthKeyX, oAuthKeyY, oAuthKey) = try await retrieveDecryptAndReconstruct (
264
253
endpoints: endpoints,
265
254
indexes: indexes,
266
- extraParams: extraParams, verifier: verifier, tokenCommitment: idToken, nodeSignatures: commitmentRequestData, verifierId: verifierId, lookupPubkeyX : lookupPubkeyX , lookupPubkeyY : lookupPubkeyY , privateKey: privateKey. serialize ( ) . addLeading0sForLength64 ( ) )
255
+ extraParams: extraParams, verifier: verifier, tokenCommitment: idToken, nodeSignatures: commitmentRequestData, verifierId: verifierId, xCoordinate : pubKeyX , yCoordinate : pubKeyY , privateKey: privateKey. serialize ( ) . addLeading0sForLength64 ( ) )
267
256
268
257
var metadataNonce : BigUInt
269
258
var typeOfUser : UserType = . v1
@@ -285,9 +274,9 @@ open class TorusUtils: AbstractTorusUtils {
285
274
metadataNonce = try await getMetadata ( dictionary: [ " pub_key_X " : oAuthKeyX, " pub_key_Y " : oAuthKeyY] )
286
275
var privateKeyWithNonce = BigInt ( metadataNonce) + BigInt( oAuthKey, radix: 16 ) !
287
276
privateKeyWithNonce = privateKeyWithNonce. modulus ( modulusValue)
288
- let serializedKey = privateKeyWithNonce. magnitude. serialize ( ) . hexString. addLeading0sForLength64 ( )
277
+ let serializedKey = privateKeyWithNonce. magnitude. serialize ( ) . hexString. addLeading0sForLength64 ( )
289
278
let finalPrivateKey = try
290
- SecretKey ( hex: serializedKey)
279
+ SecretKey ( hex: serializedKey)
291
280
finalPubKey = try finalPrivateKey. toPublic ( ) . serialize ( compressed: false )
292
281
}
293
282
} else {
@@ -356,7 +345,7 @@ open class TorusUtils: AbstractTorusUtils {
356
345
357
346
private func retrieveDecryptAndReconstruct( endpoints: [ String ] ,
358
347
indexes: [ BigUInt ] ,
359
- extraParams: [ String : Codable ] , verifier: String , tokenCommitment: String , nodeSignatures: [ CommitmentRequestResponse ] , verifierId: String , lookupPubkeyX : String , lookupPubkeyY : String , privateKey: String ) async throws -> ( String , String , String ) {
348
+ extraParams: [ String : Codable ] , verifier: String , tokenCommitment: String , nodeSignatures: [ CommitmentRequestResponse ] , verifierId: String , xCoordinate : String , yCoordinate : String , privateKey: String ) async throws -> ( String , String , String ) {
360
349
// Rebuild extraParams
361
350
let session = createURLSession ( )
362
351
let threshold = Int ( endpoints. count / 2 ) + 1
@@ -423,8 +412,8 @@ open class TorusUtils: AbstractTorusUtils {
423
412
throw TorusUtilError . decodingFailed ( decoded. error? . data)
424
413
}
425
414
os_log ( " retrieveDecryptAndReconstuct: %@ " , log: getTorusLogger ( log: TorusUtilsLogger . core, type: . info) , type: . info, " \( decoded) " )
426
- var X = lookupPubkeyX . addLeading0sForLength64 ( )
427
- var Y = lookupPubkeyY . addLeading0sForLength64 ( )
415
+ var X = xCoordinate . addLeading0sForLength64 ( )
416
+ var Y = yCoordinate . addLeading0sForLength64 ( )
428
417
if let decodedResult = decoded. result as? LegacyLookupResponse {
429
418
// case non migration
430
419
let keyObj = decodedResult. keys
@@ -442,8 +431,8 @@ open class TorusUtils: AbstractTorusUtils {
442
431
let pointHex = PointHex ( from: . init( x: first. publicKey. X, y: first. publicKey. Y) )
443
432
shareResponses. append ( pointHex)
444
433
let metadata = first. metadata
445
- X = pointHex. x
446
- Y = pointHex. y
434
+ X = pointHex. x. addLeading0sForLength64 ( )
435
+ Y = pointHex. y. addLeading0sForLength64 ( )
447
436
let model = RetrieveDecryptAndReconstuctResponseModel ( iv: metadata. iv, ephemPublicKey: metadata. ephemPublicKey, share: first. share, pubKeyX: pointHex. x, pubKeyY: pointHex. y, mac: metadata. mac)
448
437
resultArray [ i] = model
449
438
}
@@ -452,7 +441,6 @@ open class TorusUtils: AbstractTorusUtils {
452
441
}
453
442
454
443
// Due to multiple keyAssign
455
-
456
444
let lookupShares = shareResponses. filter { $0 != nil } // Nonnil elements
457
445
458
446
// Comparing dictionaries, so the order of keys doesn't matter
@@ -468,7 +456,7 @@ open class TorusUtils: AbstractTorusUtils {
468
456
let filteredData = data. filter { $0. value != TorusUtilError . decodingFailed ( nil ) . debugDescription }
469
457
470
458
if filteredData. count < threshold { throw TorusUtilError . thresholdError }
471
- let thresholdLagrangeInterpolationData = try thresholdLagrangeInterpolation ( data: filteredData, endpoints: endpoints, lookupPubkeyX : X . addLeading0sForLength64 ( ) , lookupPubkeyY : Y . addLeading0sForLength64 ( ) )
459
+ let thresholdLagrangeInterpolationData = try thresholdLagrangeInterpolation ( data: filteredData, endpoints: endpoints, xCoordinate : X . addLeading0sForLength64 ( ) , yCoordinate : Y . addLeading0sForLength64 ( ) )
472
460
session. invalidateAndCancel ( )
473
461
return thresholdLagrangeInterpolationData
474
462
case let . failure( error) :
0 commit comments