Skip to content

Commit

Permalink
fix: nonce result bug
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Jul 15, 2024
1 parent 976da30 commit a136aeb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Helpers/MetadataUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ internal class MetadataUtils {
return decoded
}

public static func getOrSetSapphireMetadataNonce(legacyMetadataHost: String, network: TorusNetwork, X: String, Y: String, serverTimeOffset: Int, 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) async throws -> GetOrSetNonceResult {
if case .sapphire = network {
return try await getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, 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)
} else {
throw TorusUtilError.metadataNonceMissing
}
Expand Down
32 changes: 11 additions & 21 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ 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)
nonceResult = metadataNonce
}
}

var serverTimeOffsets: [Int] = []
Expand All @@ -103,15 +108,6 @@ internal class NodeUtils {

let serverTimeOffset = (keyResult != nil) ? calculateMedian(arr: serverTimeOffsets) : 0

if case .sapphire = network {
let X: BigInt = BigInt(nonceResult?.pubNonce?.x ?? "0", radix: 16) ?? BigInt(0)
let Y: BigInt = BigInt(nonceResult?.pubNonce?.x ?? "0", radix: 16) ?? BigInt(0)
if nonceResult == nil || (X == BigInt(0) && Y == BigInt(0)) {
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: serverTimeOffset, getOnly: false)
nonceResult = metadataNonce
}
}

return KeyLookupResult(
keyResult: keyResult,
nodeIndexes: nodeIndexes,
Expand Down Expand Up @@ -379,17 +375,10 @@ internal class NodeUtils {
let serverOffsetTimes = serverTimeOffsets.map({ Int($0) ?? 0 })

let serverTimeOffsetResponse: Int = serverTimeOffset ?? calculateMedian(arr: serverOffsetTimes)

let nX: BigInt = BigInt(thresholdNonceData?.pubNonce?.x ?? "0", radix: 16) ?? BigInt(0)
let nY: BigInt = BigInt(thresholdNonceData?.pubNonce?.x ?? "0", radix: 16) ?? BigInt(0)

if (thresholdNonceData == nil || (nX == BigInt(0) && nY == BigInt(0))) && verifierParams.extended_verifier_id == nil && !TorusUtils.isLegacyNetworkRouteMap(network: network) {
if case .sapphire = network {

if thresholdNonceData == nil && verifierParams.extended_verifier_id == nil && !TorusUtils.isLegacyNetworkRouteMap(network: network) {
let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(legacyMetadataHost: legacyMetadataHost, network: network, X: thresholdPublicKey!.X, Y: thresholdPublicKey!.Y, serverTimeOffset: serverTimeOffsetResponse, getOnly: false)
thresholdNonceData = metadataNonce
} else {
throw TorusUtilError.runtime("invalid metadata result from nodes, nonce metadata is empty")
}
}

let thresholdReqCount = (importedShares != nil && importedShares!.count > 0) ? endpoints.count : threshold
Expand Down Expand Up @@ -503,7 +492,7 @@ internal class NodeUtils {
throw TorusUtilError.privateKeyDeriveFailed
}

let thresholdIsNewKey = try thresholdSame(arr: isNewKeys, threshold: threshold)
let thresholdIsNewKey: String? = try thresholdSame(arr: isNewKeys, threshold: threshold)

let oAuthKey = privateKey!.addLeading0sForLength64()
let oAuthPublicKey = try SecretKey(hex: oAuthKey).toPublic().serialize(compressed: false)
Expand All @@ -517,7 +506,8 @@ internal class NodeUtils {
finalPubKey = oAuthPublicKey
} else if TorusUtils.isLegacyNetworkRouteMap(network: network) {
if enableOneKey {
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: oAuthPublicKeyX, Y: oAuthPublicKeyY, getOnly: !(Bool(thresholdIsNewKey ?? "true")!))
let isNewKey = thresholdIsNewKey == "true";
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: oAuthPublicKeyX, Y: oAuthPublicKeyY, getOnly: !isNewKey)
metadataNonce = BigInt(nonce.nonce?.addLeading0sForLength64() ?? "0", radix: 16) ?? BigInt(0)
typeOfUser = UserType(rawValue: nonce.typeOfUser?.lowercased() ?? "v1")!
if typeOfUser == .v2 {
Expand Down Expand Up @@ -559,7 +549,7 @@ internal class NodeUtils {
finalPrivKey = privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()
}

var isUpgraded: Bool?
var isUpgraded: Bool? = nil
if typeOfUser == .v2 {
isUpgraded = metadataNonce == BigInt(0)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Interfaces/TorusKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class TorusKey: Codable {
public let evmAddress: String
public let X: String
public let Y: String
public let privKey: String?
public let privKey: String

internal init(evmAddress: String, X: String, Y: String, privKey: String?) {
internal init(evmAddress: String, X: String, Y: String, privKey: String) {
self.evmAddress = evmAddress
self.X = X
self.Y = Y
Expand Down
4 changes: 1 addition & 3 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public class TorusUtils {
/// - Returns: `String`
public static func getPostboxKey(torusKey: TorusKey) -> String {
if torusKey.metadata.typeOfUser == .v1 {
if let privKey: String = torusKey.finalKeyData.privKey {
return privKey
}
return torusKey.finalKeyData.privKey
}
return torusKey.oAuthKeyData.privKey
}
Expand Down
37 changes: 13 additions & 24 deletions Tests/TorusUtilsTests/oneKeyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,16 @@ class OneKeyTest: XCTestCase {
XCTAssertEqual(data.finalKeyData.evmAddress, "0x53010055542cCc0f2b6715a5c53838eC4aC96EF7")
XCTAssertEqual(data.finalKeyData.X, "3fa78a0bfb9ec48810bf1ee332360def2600c4aef528ff8b1e49a0d304722c91")
XCTAssertEqual(data.finalKeyData.Y, "46aaca39fc00c0f88f63a79989697c70eeeeec6489300c493dd07a5608ded0d4")
// v2 user, this should be empty.
// XCTAssertEqual(data.finalKeyData.privKey, "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4")
XCTAssertEqual(data.finalKeyData.privKey, "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4")
XCTAssertEqual(data.oAuthKeyData.evmAddress, "0xEfd7eDAebD0D99D1B7C8424b54835457dD005Dc4")
XCTAssertEqual(data.oAuthKeyData.X, "18409385c38e9729eb6b7837dc8f234256233ffab1ed7eeb1c23b230333396b4")
XCTAssertEqual(data.oAuthKeyData.Y, "17d35ffc722d7a8dd88353815e9553cacf567c5f3b8d082adac9d653367ce47a")
XCTAssertEqual(data.oAuthKeyData.privKey, "068ee4f97468ef1ae95d18554458d372e31968190ae38e377be59d8b3c9f7a25")
XCTAssertEqual(data.metadata.pubNonce!.x, "8e8c399d8ba00ff88e6c42eb40c10661f822868ba2ad8fe12a8830e996b1e25d")
XCTAssertEqual(data.metadata.pubNonce!.y, "554b12253694bf9eb98485441bba7ba220b78cb78ee21664e96f934d10b1494d")
// v2 user, This should be zero.
// XCTAssertEqual(data.metadata.nonce, BigUInt("22d160abe5320fe2be52a57c7aca8fe5d7e5eff104ff4d2b32767e3344e040bf", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v2)
// v2 user, this should be true.
// XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertEqual(data.metadata.nonce, BigUInt("22d160abe5320fe2be52a57c7aca8fe5d7e5eff104ff4d2b32767e3344e040bf", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v1)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotNil(data.nodesData)
}

Expand All @@ -83,19 +80,16 @@ class OneKeyTest: XCTestCase {
XCTAssertEqual(data.finalKeyData.evmAddress, "0xE1155dB406dAD89DdeE9FB9EfC29C8EedC2A0C8B")
XCTAssertEqual(data.finalKeyData.X, "78658b2671f1bd6a488baf2afb8ce6f8d8b9a1a70842130b3c8756a9d51d9723")
XCTAssertEqual(data.finalKeyData.Y, "2e5840f47d645afa4bfe93c3715e65974051080d7a1e474eef8d68752924f4fb")
// v2 user, this should be empty.
// XCTAssertEqual(data.finalKeyData.privKey, "ad47959db4cb2e63e641bac285df1b944f54d1a1cecdaeea40042b60d53c35d2")
XCTAssertEqual(data.finalKeyData.privKey, "ad47959db4cb2e63e641bac285df1b944f54d1a1cecdaeea40042b60d53c35d2")
XCTAssertEqual(data.oAuthKeyData.evmAddress, "0x5a165d2Ed4976BD104caDE1b2948a93B72FA91D2")
XCTAssertEqual(data.oAuthKeyData.X, "aba2b085ae6390b3eb26802c3239bb7e3b9ed8ea6e1dcc28aeb67432571f20fc")
XCTAssertEqual(data.oAuthKeyData.Y, "f1a2163cba5620b7b40241a6112e7918e9445b0b9cfbbb9d77b2de6f61ed5c27")
XCTAssertEqual(data.oAuthKeyData.privKey, "d9733fc1098151f3e3289673e7c69c4ed46cbbdbc13416560e14741524d2d51a")
XCTAssertEqual(data.metadata.pubNonce!.x, "376c0ac5e15686633061cf5833dd040365f91377686d7ab5338c5202bd963a2f")
XCTAssertEqual(data.metadata.pubNonce!.y, "794d7edb6a5ec0307dd40789274b377f37f293b0410a6cbd303db309536099b7")
// v2 user, This should be zero.
// XCTAssertEqual(data.metadata.nonce, BigUInt("d3d455dcab49dc700319244e9e187f443596f2acbce238cff1c215d8809fa1f9", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v2)
// v2 user, this should be true.
// XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertEqual(data.metadata.nonce, BigUInt("d3d455dcab49dc700319244e9e187f443596f2acbce238cff1c215d8809fa1f9", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v1)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotNil(data.nodesData)
}

Expand All @@ -121,11 +115,9 @@ class OneKeyTest: XCTestCase {
let verifierParams = VerifierParams(verifier_id: verifierID)
let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierParams: verifierParams, idToken: jwt)

// This swaps between v1 and v2
// XCTAssertEqual(data.metadata.typeOfUser, .v2)
XCTAssertEqual(data.metadata.typeOfUser, .v2)
XCTAssertEqual(data.metadata.nonce, BigUInt(0))
// v2 user, this should be true
// XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotEqual(data.finalKeyData.evmAddress, "")
XCTAssertNotEqual(data.oAuthKeyData.evmAddress, "")
}
Expand All @@ -144,19 +136,16 @@ class OneKeyTest: XCTestCase {
XCTAssertEqual(data.finalKeyData.evmAddress, "0x2876820fd9536BD5dd874189A85d71eE8bDf64c2")
XCTAssertEqual(data.finalKeyData.X, "ad4c223520aac9bc3ec72399869601fd59f29363471131914e2ed2bc4ba46e54")
XCTAssertEqual(data.finalKeyData.Y, "802c6e40b22b49b5ef73fa49b194c2037267215fa01683aa86746907aab37ae1")
// v2 user, this should be empty.
// XCTAssertEqual(data.finalKeyData.privKey, "9ec5b0504e252e35218c7ce1e4660eac190a1505abfbec7102946f92ed750075")
XCTAssertEqual(data.finalKeyData.privKey, "9ec5b0504e252e35218c7ce1e4660eac190a1505abfbec7102946f92ed750075")
XCTAssertEqual(data.oAuthKeyData.evmAddress, "0x54de3Df0CA76AAe3e171FB410F0626Ab759f3c24")
XCTAssertEqual(data.oAuthKeyData.X, "49d69b8550bb0eba77595c73bf57f0463ff96adf6b50d44f9e1bcf2b3fb7976e")
XCTAssertEqual(data.oAuthKeyData.Y, "d63bac65bdfc7484a28d4362347bbd098095db190c14a4ce9dbaafe74803eccc")
XCTAssertEqual(data.oAuthKeyData.privKey, "f4b7e0fb1e6f6fbac539c55e22aff2900947de652d2d6254a9cd8709f505f83a")
XCTAssertEqual(data.metadata.pubNonce!.x, "f494a5bf06a2f0550aafb6aabeb495bd6ea3ef92eaa736819b5b0ad6bfbf1aab")
XCTAssertEqual(data.metadata.pubNonce!.y, "35df3d3a14f88cbba0cfd092a1e5a0e4e725ba52a8d45719614555542d701f18")
// v2 user, This should be zero.
// XCTAssertEqual(data.metadata.nonce, BigUInt("aa0dcf552fb5be7a5c52b783c1b61c1aca7113872e172a5818994715c8a5497c", radix: 16))
XCTAssertEqual(data.metadata.nonce, BigUInt("aa0dcf552fb5be7a5c52b783c1b61c1aca7113872e172a5818994715c8a5497c", radix: 16))
XCTAssertEqual(data.metadata.typeOfUser, .v2)
// v2 user, this should be true.
// XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertEqual(data.metadata.upgraded, false)
XCTAssertNotNil(data.nodesData)
}
}

0 comments on commit a136aeb

Please sign in to comment.