Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
change legacyMetadataHost to metadataHost for getOrSetSapphireMetadataNonce() function.

add test for hexEncodedToString()
  • Loading branch information
metalurgical committed Jul 18, 2024
1 parent 3655710 commit feb727b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Sources/TorusUtils/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ extension String {

public func hexEncodedToString() -> String {
var finalString = ""
let chars = Array(self)
var chars = Array(self)

if (chars.count % 2) != 0 { // odd number of characters in hex, pad with single zero.
chars.insert("0", at: 0)
}

for count in stride(from: 0, to: chars.count - 1, by: 2) {
let firstDigit = Int("\(chars[count])", radix: 16) ?? 0
let lastDigit = Int("\(chars[count + 1])", radix: 16) ?? 0
let decimal = firstDigit * 16 + lastDigit
let decimalString = String(format: "%c", decimal) as String
finalString.append(Character(decimalString))
if !(decimalString.isEmpty) { // lossy conversion
finalString.append(Character(decimalString))
}
}
return finalString
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TorusUtils/Helpers/MetadataUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ internal class MetadataUtils {
return decoded
}

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 {
public static func getOrSetSapphireMetadataNonce(metadataHost: 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, keyType: keyType)
return try await getOrSetNonce(legacyMetadataHost: metadataHost, 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
4 changes: 2 additions & 2 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)
let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(metadataHost: legacyMetadataHost, network: network, X: keyResult!.keys[0].pub_key_X, Y: keyResult!.keys[0].pub_key_Y)
nonceResult = metadataNonce
}
}
Expand Down Expand Up @@ -377,7 +377,7 @@ internal class NodeUtils {
let serverTimeOffsetResponse: Int = serverTimeOffset ?? calculateMedian(arr: serverOffsetTimes)

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)
let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(metadataHost: legacyMetadataHost, network: network, X: thresholdPublicKey!.X, Y: thresholdPublicKey!.Y, serverTimeOffset: serverTimeOffsetResponse, getOnly: false)
thresholdNonceData = metadataNonce
if thresholdNonceData != nil {
if thresholdNonceData!.nonce != nil {
Expand Down
19 changes: 19 additions & 0 deletions Tests/TorusUtilsTests/BaseTests/HexEncodedString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation
@testable import TorusUtils
import XCTest

class HexEndodedTest: XCTestCase {
func testOddAndEvenStrings() throws {
let odd = "6F6464".hexEncodedToString()
let even = "6576656E".hexEncodedToString()
let extra_zero_padded = "06576656E".hexEncodedToString()
let double_padded = "00006576656E".hexEncodedToString()
let unpadded = "56E".hexEncodedToString()

XCTAssertEqual(odd, "odd") // 6F 64 64
XCTAssertEqual(even, "even") // 65 76 65 6E
XCTAssertEqual(extra_zero_padded, "even") // 00 65 76 65 6E
XCTAssertEqual(double_padded, "even") // 00 00 65 76 65 6E
XCTAssertEqual(unpadded, "\u{5}n") // 05 6E
}
}

0 comments on commit feb727b

Please sign in to comment.