Skip to content

Commit

Permalink
Merge pull request #80 from torusresearch/extension
Browse files Browse the repository at this point in the history
feat: remove extension
  • Loading branch information
metalurgical authored Jan 23, 2024
2 parents dd10acf + 8f433ad commit f4367dd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 133 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"package": "curvelib.swift",
"repositoryURL": "https://github.com/tkey/curvelib.swift",
"state": {
"branch": "refactor",
"revision": "3477abff071170d005f05397c6049478fa6ecaf3",
"branch": "extension",
"revision": "d5acae05bfd832393524ec8e0bf8820a745e5331",
"version": null
}
},
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
targets: ["TorusUtils"])
],
dependencies: [
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", .branch("refactor")),
.package(name: "curvelib.swift", url: "https://github.com/tkey/curvelib.swift", .branch("extension")),
.package(name:"FetchNodeDetails", url: "https://github.com/torusresearch/fetch-node-details-swift", from: "5.1.0"),
.package(name:"CryptoSwift", url: "https://github.com/krzyzanowskim/CryptoSwift",from: "1.5.1"),
.package(name:"jwt-kit", url: "https://github.com/vapor/jwt-kit", from: "4.0.0"),
Expand Down
109 changes: 0 additions & 109 deletions Sources/TorusUtils/Extensions/CurveSecp256k1+Extension.swift

This file was deleted.

42 changes: 24 additions & 18 deletions Sources/TorusUtils/Extensions/TorusUtils+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import OSLog
extension TorusUtils {
// MARK: - utils

internal func ecdh_sha512(publicKey: PublicKey, privateKey: SecretKey) throws -> [UInt8] {
let shared = try ECDH.ecdhStandard(sk: privateKey, pk: publicKey)
let data = Data(hex: shared).dropFirst()
return data.bytes.sha512()
}

internal func combinations<T>(elements: ArraySlice<T>, k: Int) -> [[T]] {
if k == 0 {
return [[]]
Expand Down Expand Up @@ -120,7 +126,7 @@ extension TorusUtils {

internal func generateParams(message: String, privateKey: String) throws -> MetadataParams {
let privKey = try SecretKey(hex: privateKey)
let publicKey = try privKey.to_public().serialize(compressed: false)
let publicKey = try privKey.toPublic().serialize(compressed: false)

let timeStamp = String(BigUInt(serverTimeOffset + Date().timeIntervalSince1970), radix: 16)
let setData: MetadataParams.SetData = .init(data: message, timestamp: timeStamp)
Expand All @@ -130,7 +136,7 @@ extension TorusUtils {
.encode(setData)

let hash = keccak256Data(encodedData).hexString
let sigData = try CurveSecp256k1.signForRecovery(hash: hash, privateKey: privKey).serialize()
let sigData = try ECDSA.signRecoverable(key: privKey, hash: hash).serialize()

return .init(pub_key_X: String(publicKey.suffix(128).prefix(64)), pub_key_Y: String(publicKey.suffix(64)), setData: setData, signature: Data(hex: sigData).base64EncodedString())
}
Expand Down Expand Up @@ -200,7 +206,7 @@ extension TorusUtils {
}
let derivedPrivateKey = try SecretKey(hex: try lagrangeInterpolation(shares: currentCombiShares, offset: 0).addLeading0sForLength64())

let decryptedPubKey = try derivedPrivateKey.to_public().serialize(compressed: false)
let decryptedPubKey = try derivedPrivateKey.toPublic().serialize(compressed: false)
let decryptedPubKeyX = String(decryptedPubKey.suffix(128).prefix(64))
let decryptedPubKeyY = String(decryptedPubKey.suffix(64))
if decryptedPubKeyX == thresholdPublicKey.X.addLeading0sForLength64() && decryptedPubKeyY == thresholdPublicKey.Y.addLeading0sForLength64() {
Expand Down Expand Up @@ -231,7 +237,7 @@ extension TorusUtils {
let threshold = (endpoints.count / 2) + 1

let sessionAuthKey = SecretKey()
let serializedPublicKey = try sessionAuthKey.to_public().serialize(compressed: false)
let serializedPublicKey = try sessionAuthKey.toPublic().serialize(compressed: false)

// Split key in 2 parts, X and Y
let pubKeyX = String(serializedPublicKey.suffix(128).prefix(64))
Expand Down Expand Up @@ -441,7 +447,7 @@ extension TorusUtils {

let derivedPrivateKey = try SecretKey(hex: oAuthKey)

let oAuthPubKey = try derivedPrivateKey.to_public().serialize(compressed: false)
let oAuthPubKey = try derivedPrivateKey.toPublic().serialize(compressed: false)
let oAuthPubKeyX = String(oAuthPubKey.suffix(128).prefix(64))
let oAuthPubKeyY = String(oAuthPubKey.suffix(64))

Expand Down Expand Up @@ -661,7 +667,7 @@ extension TorusUtils {

public func encryptData(privkeyHex: String, _ dataToEncrypt: String) throws -> String {
let privKey = try SecretKey(hex: privkeyHex)
let pubKey = try privKey.to_public().serialize(compressed: false)
let pubKey = try privKey.toPublic().serialize(compressed: false)
let encParams = try encrypt(publicKey: pubKey, msg: dataToEncrypt, opts: nil)
let data = try JSONEncoder().encode(encParams)
guard let string = String(data: data, encoding: .utf8) else { throw TorusUtilError.runtime("Invalid String from enc Params") }
Expand All @@ -679,9 +685,9 @@ extension TorusUtils {

public func encrypt(publicKey: String, msg: String, opts: Ecies? = nil) throws -> Ecies {
let ephemPrivateKey = SecretKey()
let ephemPublicKey = try ephemPrivateKey.to_public()
let ephemPublicKey = try ephemPrivateKey.toPublic()

let sharedSecret = try CurveSecp256k1.ecdh(publicKey: ephemPublicKey, privateKey: ephemPrivateKey)
let sharedSecret = try ecdh_sha512(publicKey: ephemPublicKey, privateKey: ephemPrivateKey)

let encryptionKey = Array(sharedSecret[0 ..< 32])
let macKey = Array(sharedSecret[32 ..< 64])
Expand All @@ -707,7 +713,7 @@ extension TorusUtils {
let nodeIndex = el.key

let publicKeyHex = el.value.ephemPublicKey
let sharedSecret = try CurveSecp256k1.ecdhWithHex(pubKeyHex: publicKeyHex, privateKeyHex: privateKey)
let sharedSecret = try ecdh_sha512(publicKey: PublicKey(hex: publicKeyHex), privateKey: SecretKey(hex: privateKey))

guard
let data = Data(base64Encoded: el.value.share),
Expand Down Expand Up @@ -745,7 +751,7 @@ extension TorusUtils {
do {
let data = try lagrangeInterpolation(shares: sharesToInterpolate)
let finalPrivateKey = try SecretKey(hex: data)
let finalPublicKey = try finalPrivateKey.to_public().serialize(compressed: false)
let finalPublicKey = try finalPrivateKey.toPublic().serialize(compressed: false)
// Split key in 2 parts, X and Y
let pubKeyX = String(finalPublicKey.suffix(128).prefix(64))
let pubKeyY = String(finalPublicKey.suffix(64))
Expand Down Expand Up @@ -1206,7 +1212,7 @@ extension TorusUtils {

internal func generateNonceMetadataParams(message: String, privateKey: BigInt, nonce: BigInt?) throws -> NonceMetadataParams {
let privKey = try SecretKey(hex: privateKey.magnitude.serialize().hexString.addLeading0sForLength64())
let publicKey = try privKey.to_public().serialize(compressed: false)
let publicKey = try privKey.toPublic().serialize(compressed: false)

let timeStamp = String(BigUInt(serverTimeOffset + Date().timeIntervalSince1970), radix: 16)
var setData: NonceMetadataParams.SetNonceData = .init(data: message, timestamp: timeStamp)
Expand All @@ -1218,7 +1224,7 @@ extension TorusUtils {
let encodedData = try JSONEncoder()
.encode(setData)
let hash = keccak256Data(encodedData).hexString
let sigData = try CurveSecp256k1.signForRecovery(hash: hash, privateKey: privKey).serialize()
let sigData = try ECDSA.signRecoverable(key: privKey, hash: hash).serialize()

return .init(pub_key_X: String(publicKey.suffix(128).prefix(64)), pub_key_Y: String(publicKey.suffix(64)), setData: setData, signature: Data(hex: sigData).base64EncodedString())
}
Expand All @@ -1236,13 +1242,13 @@ extension TorusUtils {
}

internal func combinePublicKeys(keys: [String], compressed: Bool) throws -> String {
let collection = PublicKeyCollection();
let collection = PublicKeyCollection()
for item in keys {
let pk = try PublicKey(hex: item)
try collection.insert(key: pk)
}
let added = try CurveSecp256k1.combineSerializedPublicKeys(keys: collection, outputCompressed: compressed)

let added = try PublicKey.combine(collection: collection).serialize(compressed: compressed)
return added
}

Expand All @@ -1264,7 +1270,7 @@ extension TorusUtils {
finalPubKey = (pubKeyX.addLeading0sForLength64() + pubKeyY.addLeading0sForLength64()).add04Prefix()
if nonce != BigInt(0) {
let noncePrivateKey = try SecretKey(hex: BigUInt(nonce).magnitude.serialize().addLeading0sForLength64().hexString)
let noncePublicKey = try noncePrivateKey.to_public().serialize(compressed: false)
let noncePublicKey = try noncePrivateKey.toPublic().serialize(compressed: false)
finalPubKey = try combinePublicKeys(keys: [finalPubKey, noncePublicKey], compressed: false)
} else {
finalPubKey = String(finalPubKey)
Expand Down Expand Up @@ -1293,7 +1299,7 @@ extension TorusUtils {
if localNonce != BigInt(0) {
let nonce2 = BigInt(localNonce)
let noncePrivateKey = try SecretKey(hex: BigUInt(nonce2).magnitude.serialize().addLeading0sForLength64().hexString)
let noncePublicKey = try noncePrivateKey.to_public().serialize(compressed: false)
let noncePublicKey = try noncePrivateKey.toPublic().serialize(compressed: false)
finalPubKey = try combinePublicKeys(keys: [finalPubKey, noncePublicKey], compressed: false)
} else {
finalPubKey = String(finalPubKey)
Expand Down Expand Up @@ -1342,7 +1348,7 @@ extension TorusUtils {
}

public func decrypt(privateKey: String, opts: ECIES, padding: Padding = .pkcs7) throws -> Data {
let sharedSecret = try CurveSecp256k1.ecdhWithHex(pubKeyHex: opts.ephemPublicKey, privateKeyHex: privateKey)
let sharedSecret = try ecdh_sha512(publicKey: PublicKey(hex: opts.ephemPublicKey), privateKey: SecretKey(hex: privateKey))

let aesKey = Array(sharedSecret[0 ..< 32])
_ = Array(sharedSecret[32 ..< 64]) // TODO: check mac
Expand Down
6 changes: 3 additions & 3 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ open class TorusUtils: AbstractTorusUtils {
indexes: [BigUInt],
endpoints: [String], verifier: String, verifierId: String, idToken: String, extraParams: [String: Codable]) async throws -> TorusKey {
let privateKey = SecretKey()
let serializedPublicKey = try privateKey.to_public().serialize(compressed: false)
let serializedPublicKey = try privateKey.toPublic().serialize(compressed: false)

// Split key in 2 parts, X and Y
// let publicKeyHex = publicKey.toHexString()
Expand Down Expand Up @@ -287,15 +287,15 @@ open class TorusUtils: AbstractTorusUtils {
let serializedKey = privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()
let finalPrivateKey = try
SecretKey(hex: serializedKey)
finalPubKey = try finalPrivateKey.to_public().serialize(compressed: false)
finalPubKey = try finalPrivateKey.toPublic().serialize(compressed: false)
}
} else {
// for imported keys in legacy networks
metadataNonce = try await getMetadata(dictionary: ["pub_key_X": oAuthKeyX, "pub_key_Y": oAuthKeyY])
var privateKeyWithNonce = BigInt(metadataNonce) + BigInt(oAuthKey, radix: 16)!
privateKeyWithNonce = privateKeyWithNonce.modulus(modulusValue)
let finalPrivateKey = try SecretKey(hex: privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64())
finalPubKey = try finalPrivateKey.to_public().serialize(compressed: false)
finalPubKey = try finalPrivateKey.toPublic().serialize(compressed: false)
}

let oAuthKeyAddress = generateAddressFromPubKey(publicKeyX: oAuthKeyX, publicKeyY: oAuthKeyY)
Expand Down

0 comments on commit f4367dd

Please sign in to comment.