Skip to content

Commit

Permalink
fix: most one key test
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Jul 15, 2024
1 parent a136aeb commit 53a2b75
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
19 changes: 10 additions & 9 deletions Sources/TorusUtils/Helpers/MetadataUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class MetadataUtils {
return rq
}

public static func generateMetadataParams(serverTimeOffset: Int, message: String, privateKey: String) throws -> MetadataParams {
public static func generateMetadataParams(serverTimeOffset: Int, message: String, privateKey: String, keyType: TorusKeyType? = nil) throws -> MetadataParams {
let privKey = try SecretKey(hex: privateKey)
let publicKey = try privKey.toPublic().serialize(compressed: false)

Expand All @@ -60,7 +60,7 @@ internal class MetadataUtils {
let sigData = try ECDSA.signRecoverable(key: privKey, hash: hash).serialize()
_ = try ECDSA.recover(signature: Signature(hex: sigData), hash: hash)
let (X, Y) = try KeyUtils.getPublicKeyCoords(pubKey: publicKey)
return .init(pub_key_X: X, pub_key_Y: Y, setData: setData, signature: Data(hex: sigData).base64EncodedString())
return .init(pub_key_X: X, pub_key_Y: Y, setData: setData, signature: Data(hex: sigData).base64EncodedString(), keyType: keyType)
}

public static func getMetadata(legacyMetadataHost: String, dictionary: [String: String]) async throws -> BigUInt {
Expand All @@ -81,29 +81,30 @@ internal class MetadataUtils {
return ret
}

public static func getOrSetNonce(legacyMetadataHost: String, serverTimeOffset: Int, X: String, Y: String, privateKey: String? = nil, getOnly: Bool = false) async throws -> GetOrSetNonceResult {
public static func getOrSetNonce(legacyMetadataHost: String, serverTimeOffset: Int, X: String, Y: String, privateKey: String? = nil, getOnly: Bool = false, keyType: TorusKeyType? = nil) async throws -> GetOrSetNonceResult {
var data: Data
let msg = getOnly ? "getNonce" : "getOrSetNonce"
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
if privateKey != nil {
let val = try generateMetadataParams(serverTimeOffset: serverTimeOffset, message: msg, privateKey: privateKey!)
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
data = try encoder.encode(val)
} else {
let dict: [String: Any] = ["pub_key_X": X, "pub_key_Y": Y, "set_data": ["data": msg]]
data = try JSONSerialization.data(withJSONObject: dict, options: .sortedKeys)
let val = GetNonceParams(pub_key_X: X, pub_key_Y: Y, set_data: GetNonceSetDataParams(data: msg))
data = try encoder.encode(val)
}
var request = try makeUrlRequest(url: "\(legacyMetadataHost)/get_or_set_nonce")
request.httpBody = data
let urlSession = URLSession(configuration: .default)
let val = try await urlSession.data(for: request)

let decoded = try JSONDecoder().decode(GetOrSetNonceResult.self, from: val.0)
return decoded
}

public static func getOrSetSapphireMetadataNonce(legacyMetadataHost: String, network: TorusNetwork, X: String, Y: String, serverTimeOffset: Int? = nil, privateKey: String? = nil, getOnly: Bool = false) async throws -> GetOrSetNonceResult {
public static func getOrSetSapphireMetadataNonce(legacyMetadataHost: String, network: TorusNetwork, X: String, Y: String, serverTimeOffset: Int? = nil, privateKey: String? = nil, getOnly: Bool = false, keyType: TorusKeyType = .secp256k1) async throws -> GetOrSetNonceResult {
if case .sapphire = network {
return try await getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? Int(trunc(Double((0) + Int(Date().timeIntervalSince1970)))), X: X, Y: Y, privateKey: privateKey, getOnly: getOnly)
return try await getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? Int(trunc(Double((0) + Int(Date().timeIntervalSince1970)))), X: X, Y: Y, privateKey: privateKey, getOnly: getOnly, keyType: keyType)
} else {
throw TorusUtilError.metadataNonceMissing
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal class NodeUtils {
}

if nonceResult == nil {
let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(legacyMetadataHost: legacyMetadataHost, network: network, X: keyResult!.keys[0].pub_key_X, Y: keyResult!.keys[0].pub_key_Y, serverTimeOffset: nil, getOnly: false)
let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(legacyMetadataHost: legacyMetadataHost, network: network, X: keyResult!.keys[0].pub_key_X, Y: keyResult!.keys[0].pub_key_Y)
nonceResult = metadataNonce
}
}
Expand Down Expand Up @@ -506,8 +506,8 @@ internal class NodeUtils {
finalPubKey = oAuthPublicKey
} else if TorusUtils.isLegacyNetworkRouteMap(network: network) {
if enableOneKey {
let isNewKey = thresholdIsNewKey == "true";
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: oAuthPublicKeyX, Y: oAuthPublicKeyY, getOnly: !isNewKey)
let isNewKey = !(thresholdIsNewKey == "true");
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: oAuthPublicKeyX, Y: oAuthPublicKeyY, privateKey: oAuthKey, getOnly: isNewKey)
metadataNonce = BigInt(nonce.nonce?.addLeading0sForLength64() ?? "0", radix: 16) ?? BigInt(0)
typeOfUser = UserType(rawValue: nonce.typeOfUser?.lowercased() ?? "v1")!
if typeOfUser == .v2 {
Expand Down
11 changes: 11 additions & 0 deletions Sources/TorusUtils/Helpers/jsonRPC/Requests/GetNonceParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

internal struct GetNonceSetDataParams: Codable {
public var data: String
}

internal struct GetNonceParams: Codable {
public var pub_key_X: String
public var pub_key_Y: String
public var set_data: GetNonceSetDataParams
}
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Interfaces/MetaData/MetadataParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ internal struct MetadataParams: Codable {
public var namespace: String?
public var pub_key_X: String
public var pub_key_Y: String
public var key_type: TorusKeyType
public var key_type: TorusKeyType?
public var set_data: SetData
public var signature: String

public init(pub_key_X: String, pub_key_Y: String, setData: SetData, signature: String, namespace: String? = nil, keyType: TorusKeyType = .secp256k1) {
public init(pub_key_X: String, pub_key_Y: String, setData: SetData, signature: String, namespace: String? = nil, keyType: TorusKeyType? = nil) {
self.namespace = namespace
self.pub_key_X = pub_key_X
self.pub_key_Y = pub_key_Y
Expand Down
4 changes: 2 additions & 2 deletions Tests/TorusUtilsTests/oneKeyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class OneKeyTest: XCTestCase {
XCTAssertEqual(data.metadata.pubNonce!.x, "8e8c399d8ba00ff88e6c42eb40c10661f822868ba2ad8fe12a8830e996b1e25d")
XCTAssertEqual(data.metadata.pubNonce!.y, "554b12253694bf9eb98485441bba7ba220b78cb78ee21664e96f934d10b1494d")
XCTAssertEqual(data.metadata.nonce, BigUInt("22d160abe5320fe2be52a57c7aca8fe5d7e5eff104ff4d2b32767e3344e040bf", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v1)
XCTAssertEqual(data.metadata.typeOfUser, .v2)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotNil(data.nodesData)
}
Expand All @@ -88,7 +88,7 @@ class OneKeyTest: XCTestCase {
XCTAssertEqual(data.metadata.pubNonce!.x, "376c0ac5e15686633061cf5833dd040365f91377686d7ab5338c5202bd963a2f")
XCTAssertEqual(data.metadata.pubNonce!.y, "794d7edb6a5ec0307dd40789274b377f37f293b0410a6cbd303db309536099b7")
XCTAssertEqual(data.metadata.nonce, BigUInt("d3d455dcab49dc700319244e9e187f443596f2acbce238cff1c215d8809fa1f9", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v1)
XCTAssertEqual(data.metadata.typeOfUser, .v2)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotNil(data.nodesData)
}
Expand Down

0 comments on commit 53a2b75

Please sign in to comment.