Skip to content

Commit

Permalink
add basic documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed May 15, 2024
1 parent 7becb7a commit 9eab850
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 26 deletions.
1 change: 1 addition & 0 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ internal class NodeUtils {
finalPubKey = try SecretKey(hex: privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()).toPublic().serialize(compressed: false)
}
} else {
// TODO: BUG: Nonce is returned as (x=0,y=0) sometimes, this is invalid investigate further
typeOfUser = .v2
let publicNonce = KeyUtils.getPublicKeyFromCoords(pubKeyX: thresholdNonceData!.pubNonce!.x, pubKeyY: thresholdNonceData!.pubNonce!.y)
let oAuthPubKey = KeyUtils.getPublicKeyFromCoords(pubKeyX: oAuthPublicKeyX, pubKeyY: oAuthPublicKeyY)
Expand Down
11 changes: 11 additions & 0 deletions Sources/TorusUtils/Helpers/jsonRPC/JRPCResponse.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import Foundation

internal struct AllowSuccess: Codable {
public let success: Bool
}

internal struct AllowRejected: Codable {
public let code: Int32
public let error: String
public let success: Bool
}


internal struct JRPCResponse<T: Codable>: Codable {
public var id: Int
public var jsonrpc = "2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal struct KeyAssignRequestParams: Encodable {
public var torusSignature: String
public var torusTimestamp: String

enum KeyAssignRequestKeys: String, CodingKey {
enum CodingKeys: String, CodingKey {
case id
case jsonrpc
case method
Expand All @@ -20,7 +20,7 @@ internal struct KeyAssignRequestParams: Encodable {
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: KeyAssignRequestKeys.self)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)

try container.encode(jsonrpc, forKey: .jsonrpc)
Expand Down
46 changes: 29 additions & 17 deletions Sources/TorusUtils/Interfaces/TorusKey.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
import BigInt
import Foundation

public struct TorusKey {
public struct FinalKeyData {
public class TorusKey {
public class FinalKeyData {
public let evmAddress: String
public let X: String
public let Y: String
public let privKey: String?

internal init(evmAddress: String, X: String, Y: String, privKey: String?) {
self.evmAddress = evmAddress
self.X = X
self.Y = Y
self.privKey = privKey
}
}

public struct OAuthKeyData {
public class OAuthKeyData {
public let evmAddress: String
public let X: String
public let Y: String
public let privKey: String

internal init(evmAddress: String, X: String, Y: String, privKey: String) {
self.evmAddress = evmAddress
self.X = X
self.Y = Y
self.privKey = privKey
}
}

public struct SessionData {
public class SessionData {
public let sessionTokenData: [SessionToken?]
public let sessionAuthKey: String

internal init(sessionTokenData: [SessionToken?], sessionAuthKey: String) {
self.sessionTokenData = sessionTokenData
self.sessionAuthKey = sessionAuthKey
}
}

public struct NodesData {
public class NodesData {
public let nodeIndexes: [Int]

internal init(nodeIndexes: [Int]) {
self.nodeIndexes = nodeIndexes
}
}

public init(finalKeyData: FinalKeyData,
internal init(finalKeyData: FinalKeyData,
oAuthKeyData: OAuthKeyData,
sessionData: SessionData,
metadata: TorusPublicKey.Metadata,
Expand All @@ -43,14 +66,3 @@ public struct TorusKey {
public let metadata: TorusPublicKey.Metadata
public let nodesData: NodesData
}

// allow response
public struct AllowSuccess: Codable {
public let success: Bool
}

public struct AllowRejected: Codable {
public let code: Int32
public let error: String
public let success: Bool
}
13 changes: 12 additions & 1 deletion Sources/TorusUtils/Interfaces/TorusOptions.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import FetchNodeDetails
import Foundation

public struct TorusOptions {
/// TorusOptions is a configuration class that is used to initialize `TorusUtils`.
public class TorusOptions {
public var enableOneKey: Bool
public var clientId: String
public var network: TorusNetwork
public var serverTimeOffset: Int
public var legacyMetadataHost: String?

/// Initializes TorusOptions
///
/// - Parameters:
/// - clientId: The client identity.
/// - network: `TorusNetwork`. Please note that new users should be using .sapphire(.SAPPHIRE_MAINNET).
/// - legacyMetadataHost: The url of the metadata server, this only needs to be supplied if the default is not being used.
/// - serverTimeOffset: The offset from Coordinated Universal Time (UCT).
/// - enableOneKey: Use the oneKey flow.
///
/// - Returns: `TorusOptions`
public init(clientId: String, network: TorusNetwork, legacyMetadataHost: String = "https://metadata.tor.us", serverTimeOffset: Int = 0, enableOneKey: Bool = false) {
self.clientId = clientId
self.enableOneKey = enableOneKey
Expand Down
36 changes: 30 additions & 6 deletions Sources/TorusUtils/Interfaces/TorusPublicKey.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import BigInt
import Foundation

public struct TorusPublicKey {
public struct OAuthKeyData {
public class TorusPublicKey {
public class OAuthKeyData {
public let evmAddress: String
public let X: String
public let Y: String

internal init(evmAddress: String, X: String, Y: String) {
self.evmAddress = evmAddress
self.X = X
self.Y = Y
}
}

public struct FinalKeyData {
public class FinalKeyData {
public let evmAddress: String
public let X: String
public let Y: String

internal init(evmAddress: String, X: String, Y: String) {
self.evmAddress = evmAddress
self.X = X
self.Y = Y
}
}

public struct Metadata {
public class Metadata {
public let pubNonce: PubNonce?
public let nonce: BigUInt?
public let typeOfUser: UserType
public let upgraded: Bool?
public let serverTimeOffset: Int

internal init(pubNonce: PubNonce?, nonce: BigUInt?, typeOfUser: UserType, upgraded: Bool?, serverTimeOffset: Int) {
self.pubNonce = pubNonce
self.nonce = nonce
self.typeOfUser = typeOfUser
self.upgraded = upgraded
self.serverTimeOffset = serverTimeOffset
}
}

public struct NodesData: Equatable {
public class NodesData {
public let nodeIndexes: [Int]

internal init(nodeIndexes: [Int]) {
self.nodeIndexes = nodeIndexes
}
}

public init(oAuthKeyData: OAuthKeyData?, finalKeyData: FinalKeyData?, metadata: Metadata?, nodesData: NodesData?) {
internal init(oAuthKeyData: OAuthKeyData?, finalKeyData: FinalKeyData?, metadata: Metadata?, nodesData: NodesData?) {
self.finalKeyData = finalKeyData
self.oAuthKeyData = oAuthKeyData
self.metadata = metadata
Expand Down
72 changes: 72 additions & 0 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public class TorusUtils {

var apiKey: String = "torus-default"

/// Initializes TorusUtils with the provided options
///
/// - Parameters:
/// - params: `TorusOptions`
/// - logLevel: `OSLogType`, only needs to be provided if the default logging level should be changed
///
/// - Returns: `TorusUtils`
public init(params: TorusOptions, loglevel: OSLogType = .default) {
var defaultHost = ""
if params.legacyMetadataHost == nil {
Expand Down Expand Up @@ -60,18 +67,33 @@ public class TorusUtils {
return false
}

/// Sets the apiKey
///
/// - Parameters:
/// - apiKey: The api key to be assigned
public func setApiKey(apiKey: String) {
self.apiKey = apiKey
}

/// Reverts the apiKey for `TorusUtils` to the default value
public func removeApiKey() {
apiKey = "torus-default"
}

/// Sets the sessionTime
///
/// - Parameters:
/// - sessionTime: The amount of time a session should be valid for in seconds, default is 24 hours.
public func setSessionTime(sessionTime: Int) {
self.sessionTime = sessionTime
}

/// Convenience function to quickly retrieve the postbox key from `TorusKey`
///
/// - Parameters:
/// - torusKey: `TorusKey`
///
/// - Returns: `String`
public static func getPostboxKey(torusKey: TorusKey) -> String {
if torusKey.metadata.typeOfUser == .v1 {
if let privKey: String = torusKey.finalKeyData.privKey {
Expand All @@ -81,6 +103,19 @@ public class TorusUtils {
return torusKey.oAuthKeyData.privKey
}

/// Login for the provided user
///
/// - Parameters:
/// - endpoints: The endpoints to be queried for the relevant network.
/// - indexes: The node indexes for the endpoints.
/// - verifier: The verifier to query, this can be a single verifier or an aggregate verifier.
/// - verifier_id: The identity of the user to be queried against the verifier, this is usually an emal.
/// - verifierParams: `VerifierParams`
/// - idToken: This is the identity token of the user. For single verifiers this will be a jwt, in the case of an aggregate verifier, this will be a keccak256 hash of the jwt.
///
/// - Returns: `TorusKey`
///
/// - Throws: `TorusUtilError`
public func retrieveShares(
endpoints: [String],
indexes: [BigUInt],
Expand All @@ -96,10 +131,36 @@ public class TorusUtils {
return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: [], apiKey: apiKey, extraParams: params)
}

/// Retrieves user information, defaulting the user type to .v2
///
/// - Parameters:
/// - endpoints: The endpoints to be queried for the relevant network.
/// - verifier: The verifier to query, this can be a single verifier or an aggregate verifier.
/// - verifier_id: The identity of the user to be queried against the verifier, this is usually an emal.
/// - extended_verifier_id: This is only used if querying a tss verifier, otherwise it is not supplied. Format is (verifierId + "\u{0015}" + tssTag + "\u{0016}" + randomNonce)
///
/// - Returns: `TorusPublicKey`
///
/// - Throws: `TorusUtilError
public func getPublicAddress(endpoints: [String], verifier: String, verifierId: String, extendedVerifierId: String? = nil) async throws -> TorusPublicKey {
return try await getNewPublicAddress(endpoints: endpoints, verifier: verifier, verifierId: verifierId, extendedVerifierId: extendedVerifierId, enableOneKey: enableOneKey)
}

/// Imports a private key for the provided user
///
/// - Parameters:
/// - endpoints: The endpoints to be queried for the relevant network.
/// - nodeIndexes: The node indexes for the endpoints.
/// - nodePubKeys: The public keys for the endpoints. `TorusNodePubModel`
/// - verifier: The verifier to query, this can be a single verifier or an aggregate verifier.
/// - verifier_id: The identity of the user to be queried against the verifier, this is usually an emal.
/// - verifierParams: `VerifierParams`
/// - idToken: This is the identity token of the user. For single verifiers this will be a jwt, in the case of an aggregate verifier, this will be a keccak256 hash of the jwt.
/// - newPrivateKey: The private key that is being imported.
///
/// - Returns: `TorusKey`
///
/// - Throws: `TorusUtilError`
public func importPrivateKey(
endpoints: [String],
nodeIndexes: [BigUInt],
Expand Down Expand Up @@ -172,6 +233,17 @@ public class TorusUtils {
return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? 0, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: shareDatas)
}

/// Retrieves user information
///
/// - Parameters:
/// - endpoints: The endpoints to be queried for the relevant network.
/// - verifier: The verifier to query, this can be a single verifier or an aggregate verifier.
/// - verifier_id: The identity of the user to be queried against the verifier, this is usually an emal.
/// - extended_verifier_id: This is only used if querying a tss verifier, otherwise it is not supplied. Format is (verifierId + "\u{0015}" + tssTag + "\u{0016}" + randomNonce)
///
/// - Returns: `TorusPublicKey`
///
/// - Throws: `TorusUtilError`
public func getUserTypeAndAddress(
endpoints: [String],
verifier: String,
Expand Down

0 comments on commit 9eab850

Please sign in to comment.