Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructing code to use encypt and decypt function in other sdk's #109

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sources/TorusUtils/Helpers/EncryptionUtils.swift
Original file line number Diff line number Diff line change
@@ -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)
himanshuchawla009 marked this conversation as resolved.
Show resolved Hide resolved
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())
}
}
23 changes: 3 additions & 20 deletions Sources/TorusUtils/Helpers/MetadataUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 13 additions & 13 deletions Sources/TorusUtils/Interfaces/Common/Ecies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,77 +23,77 @@ 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
self.mac = mac
self.mode = mode
}

init(from: Ecies) {
public init(from: Ecies) {
ciphertext = from.ciphertext
iv = from.iv
ephemPublicKey = from.ephemPublicKey
mac = from.mac
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
mode = "AES256"
}
}

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
self.mac = mac
}
}

internal struct EciesOmitCiphertext {
public struct EciesOmitCiphertext {
var iv: String
var ephemPublicKey: String
var mac: String
Expand Down
2 changes: 1 addition & 1 deletion Torus-utils.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "Torus-utils"
spec.version = "9.0.1"
spec.version = "9.0.2"
spec.ios.deployment_target = "13.0"
spec.summary = "Retrieve user shares"
spec.homepage = "https://github.com/torusresearch/torus-utils-swift"
Expand Down