From d22336f59011d5df647a8447e29d0d90ae00c361 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Tue, 1 Oct 2024 12:35:58 +0530 Subject: [PATCH] Restructing code to use encypt and decypt function in other sdk's --- .../TorusUtils/Helpers/EncryptionUtils.swift | 37 +++++++++++++++++++ .../TorusUtils/Helpers/MetadataUtils.swift | 23 ++---------- .../TorusUtils/Interfaces/Common/Ecies.swift | 26 ++++++------- 3 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 Sources/TorusUtils/Helpers/EncryptionUtils.swift diff --git a/Sources/TorusUtils/Helpers/EncryptionUtils.swift b/Sources/TorusUtils/Helpers/EncryptionUtils.swift new file mode 100644 index 00000000..b92f8cb4 --- /dev/null +++ b/Sources/TorusUtils/Helpers/EncryptionUtils.swift @@ -0,0 +1,37 @@ +import Foundation + +#if canImport(curveSecp256k1) + import curveSecp256k1 +#endif + +public class EncryptionUtils { + + public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String { + let eciesOpts = ECIES( + iv: eciesData.iv, + ephemPublicKey: eciesData.ephemPublicKey, + ciphertext: ciphertextHex, + mac: eciesData.mac + ) + + let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString + return decryptedSigBuffer + } + + public static func decrypt(privateKey: String, opts: ECIES) throws -> Data { + let secret = try SecretKey(hex: privateKey) + var publicKey = opts.ephemPublicKey + if opts.ephemPublicKey.count == 128 { // missing 04 prefix + publicKey = publicKey.add04PrefixUnchecked() + } + let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac) + let result = try Encryption.decrypt(sk: secret, encrypted: msg) + return result + } + + public static func encrypt(publicKey: String, msg: String) throws -> Ecies { + let data = Data(hex: msg) + let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data) + return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac()) + } +} diff --git a/Sources/TorusUtils/Helpers/MetadataUtils.swift b/Sources/TorusUtils/Helpers/MetadataUtils.swift index ecb70c0c..5ff84d07 100644 --- a/Sources/TorusUtils/Helpers/MetadataUtils.swift +++ b/Sources/TorusUtils/Helpers/MetadataUtils.swift @@ -8,32 +8,15 @@ import OSLog internal class MetadataUtils { public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String { - let eciesOpts = ECIES( - iv: eciesData.iv, - ephemPublicKey: eciesData.ephemPublicKey, - ciphertext: ciphertextHex, - mac: eciesData.mac - ) - - let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString - return decryptedSigBuffer + return try EncryptionUtils.decryptNodeData(eciesData: eciesData, ciphertextHex: ciphertextHex, privKey: privKey) } public static func decrypt(privateKey: String, opts: ECIES) throws -> Data { - let secret = try SecretKey(hex: privateKey) - var publicKey = opts.ephemPublicKey - if opts.ephemPublicKey.count == 128 { // missing 04 prefix - publicKey = publicKey.add04PrefixUnchecked() - } - let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac) - let result = try Encryption.decrypt(sk: secret, encrypted: msg) - return result + return try EncryptionUtils.decrypt(privateKey: privateKey, opts: opts) } public static func encrypt(publicKey: String, msg: String) throws -> Ecies { - let data = Data(hex: msg) - let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data) - return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac()) + return try EncryptionUtils.encrypt(publicKey: publicKey, msg: msg) } internal static func makeUrlRequest(url: String, httpMethod: httpMethod = .post) throws -> URLRequest { diff --git a/Sources/TorusUtils/Interfaces/Common/Ecies.swift b/Sources/TorusUtils/Interfaces/Common/Ecies.swift index 3ca27b9c..6f60727c 100644 --- a/Sources/TorusUtils/Interfaces/Common/Ecies.swift +++ b/Sources/TorusUtils/Interfaces/Common/Ecies.swift @@ -7,14 +7,14 @@ protocol EciesProtocol { var mac: Data { get } } -internal struct ECIES: Codable { +public struct ECIES: Codable { let iv: String let ephemPublicKey: String let ciphertext: String let mac: String let mode: String? - init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) { + public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) { self.iv = iv self.ephemPublicKey = ephemPublicKey self.ciphertext = ciphertext @@ -23,14 +23,14 @@ internal struct ECIES: Codable { } } -internal struct EciesHex: Codable { +public struct EciesHex: Codable { let iv: String let ephemPublicKey: String let ciphertext: String let mac: String let mode: String? - init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) { + public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) { self.iv = iv self.ephemPublicKey = ephemPublicKey self.ciphertext = ciphertext @@ -38,7 +38,7 @@ internal struct EciesHex: Codable { self.mode = mode } - init(from: Ecies) { + public init(from: Ecies) { ciphertext = from.ciphertext iv = from.iv ephemPublicKey = from.ephemPublicKey @@ -46,32 +46,32 @@ internal struct EciesHex: Codable { mode = "AES256" } - func omitCiphertext() -> EciesHexOmitCiphertext { + public func omitCiphertext() -> EciesHexOmitCiphertext { return EciesHexOmitCiphertext(iv: iv, ephemPublicKey: ephemPublicKey, mac: mac, mode: mode) } } -internal struct EciesHexOmitCiphertext: Codable { +public struct EciesHexOmitCiphertext: Codable { var iv: String var ephemPublicKey: String var mac: String var mode: String? - init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) { + public init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) { self.iv = iv self.ephemPublicKey = ephemPublicKey self.mac = mac self.mode = mode } - init(from: ECIES) { + public init(from: ECIES) { iv = from.iv ephemPublicKey = from.ephemPublicKey mac = from.mac mode = from.mode } - init(from: Ecies) { + public init(from: Ecies) { iv = from.iv ephemPublicKey = from.ephemPublicKey mac = from.mac @@ -79,13 +79,13 @@ internal struct EciesHexOmitCiphertext: Codable { } } -internal struct Ecies: Codable { +public struct Ecies: Codable { var iv: String var ephemPublicKey: String var ciphertext: String var mac: String - init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) { + public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) { self.iv = iv self.ephemPublicKey = ephemPublicKey self.ciphertext = ciphertext @@ -93,7 +93,7 @@ internal struct Ecies: Codable { } } -internal struct EciesOmitCiphertext { +public struct EciesOmitCiphertext { var iv: String var ephemPublicKey: String var mac: String