Skip to content

Commit 0ac2810

Browse files
Merge pull request #109 from torusresearch/rearranging_code
Restructing code to use encypt and decypt function in other sdk's
2 parents eda55b8 + b4f0305 commit 0ac2810

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
3+
#if canImport(curveSecp256k1)
4+
import curveSecp256k1
5+
#endif
6+
7+
public class EncryptionUtils {
8+
9+
public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String {
10+
let eciesOpts = ECIES(
11+
iv: eciesData.iv,
12+
ephemPublicKey: eciesData.ephemPublicKey,
13+
ciphertext: ciphertextHex,
14+
mac: eciesData.mac
15+
)
16+
17+
let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString
18+
return decryptedSigBuffer
19+
}
20+
21+
public static func decrypt(privateKey: String, opts: ECIES) throws -> Data {
22+
let secret = try SecretKey(hex: privateKey)
23+
var publicKey = opts.ephemPublicKey
24+
if opts.ephemPublicKey.count == 128 { // missing 04 prefix
25+
publicKey = publicKey.add04PrefixUnchecked()
26+
}
27+
let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac)
28+
let result = try Encryption.decrypt(sk: secret, encrypted: msg)
29+
return result
30+
}
31+
32+
public static func encrypt(publicKey: String, msg: String) throws -> Ecies {
33+
let data = Data(hex: msg)
34+
let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data)
35+
return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac())
36+
}
37+
}

Sources/TorusUtils/Helpers/MetadataUtils.swift

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,15 @@ import OSLog
88

99
internal class MetadataUtils {
1010
public static func decryptNodeData(eciesData: EciesHexOmitCiphertext, ciphertextHex: String, privKey: String) throws -> String {
11-
let eciesOpts = ECIES(
12-
iv: eciesData.iv,
13-
ephemPublicKey: eciesData.ephemPublicKey,
14-
ciphertext: ciphertextHex,
15-
mac: eciesData.mac
16-
)
17-
18-
let decryptedSigBuffer = try decrypt(privateKey: privKey, opts: eciesOpts).hexString
19-
return decryptedSigBuffer
11+
return try EncryptionUtils.decryptNodeData(eciesData: eciesData, ciphertextHex: ciphertextHex, privKey: privKey)
2012
}
2113

2214
public static func decrypt(privateKey: String, opts: ECIES) throws -> Data {
23-
let secret = try SecretKey(hex: privateKey)
24-
var publicKey = opts.ephemPublicKey
25-
if opts.ephemPublicKey.count == 128 { // missing 04 prefix
26-
publicKey = publicKey.add04PrefixUnchecked()
27-
}
28-
let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac)
29-
let result = try Encryption.decrypt(sk: secret, encrypted: msg)
30-
return result
15+
return try EncryptionUtils.decrypt(privateKey: privateKey, opts: opts)
3116
}
3217

3318
public static func encrypt(publicKey: String, msg: String) throws -> Ecies {
34-
let data = Data(hex: msg)
35-
let curveMsg = try Encryption.encrypt(pk: PublicKey(hex: publicKey), plainText: data)
36-
return try .init(iv: curveMsg.iv(), ephemPublicKey: curveMsg.ephemeralPublicKey().serialize(compressed: false), ciphertext: curveMsg.chipherText(), mac: curveMsg.mac())
19+
return try EncryptionUtils.encrypt(publicKey: publicKey, msg: msg)
3720
}
3821

3922
internal static func makeUrlRequest(url: String, httpMethod: httpMethod = .post) throws -> URLRequest {

Sources/TorusUtils/Interfaces/Common/Ecies.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ protocol EciesProtocol {
77
var mac: Data { get }
88
}
99

10-
internal struct ECIES: Codable {
10+
public struct ECIES: Codable {
1111
let iv: String
1212
let ephemPublicKey: String
1313
let ciphertext: String
1414
let mac: String
1515
let mode: String?
1616

17-
init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) {
17+
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String? = nil) {
1818
self.iv = iv
1919
self.ephemPublicKey = ephemPublicKey
2020
self.ciphertext = ciphertext
@@ -23,77 +23,77 @@ internal struct ECIES: Codable {
2323
}
2424
}
2525

26-
internal struct EciesHex: Codable {
26+
public struct EciesHex: Codable {
2727
let iv: String
2828
let ephemPublicKey: String
2929
let ciphertext: String
3030
let mac: String
3131
let mode: String?
3232

33-
init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) {
33+
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String, mode: String?) {
3434
self.iv = iv
3535
self.ephemPublicKey = ephemPublicKey
3636
self.ciphertext = ciphertext
3737
self.mac = mac
3838
self.mode = mode
3939
}
4040

41-
init(from: Ecies) {
41+
public init(from: Ecies) {
4242
ciphertext = from.ciphertext
4343
iv = from.iv
4444
ephemPublicKey = from.ephemPublicKey
4545
mac = from.mac
4646
mode = "AES256"
4747
}
4848

49-
func omitCiphertext() -> EciesHexOmitCiphertext {
49+
public func omitCiphertext() -> EciesHexOmitCiphertext {
5050
return EciesHexOmitCiphertext(iv: iv, ephemPublicKey: ephemPublicKey, mac: mac, mode: mode)
5151
}
5252
}
5353

54-
internal struct EciesHexOmitCiphertext: Codable {
54+
public struct EciesHexOmitCiphertext: Codable {
5555
var iv: String
5656
var ephemPublicKey: String
5757
var mac: String
5858
var mode: String?
5959

60-
init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) {
60+
public init(iv: String, ephemPublicKey: String, mac: String, mode: String? = nil) {
6161
self.iv = iv
6262
self.ephemPublicKey = ephemPublicKey
6363
self.mac = mac
6464
self.mode = mode
6565
}
6666

67-
init(from: ECIES) {
67+
public init(from: ECIES) {
6868
iv = from.iv
6969
ephemPublicKey = from.ephemPublicKey
7070
mac = from.mac
7171
mode = from.mode
7272
}
7373

74-
init(from: Ecies) {
74+
public init(from: Ecies) {
7575
iv = from.iv
7676
ephemPublicKey = from.ephemPublicKey
7777
mac = from.mac
7878
mode = "AES256"
7979
}
8080
}
8181

82-
internal struct Ecies: Codable {
82+
public struct Ecies: Codable {
8383
var iv: String
8484
var ephemPublicKey: String
8585
var ciphertext: String
8686
var mac: String
8787

88-
init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) {
88+
public init(iv: String, ephemPublicKey: String, ciphertext: String, mac: String) {
8989
self.iv = iv
9090
self.ephemPublicKey = ephemPublicKey
9191
self.ciphertext = ciphertext
9292
self.mac = mac
9393
}
9494
}
9595

96-
internal struct EciesOmitCiphertext {
96+
public struct EciesOmitCiphertext {
9797
var iv: String
9898
var ephemPublicKey: String
9999
var mac: String

Torus-utils.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = "Torus-utils"
3-
spec.version = "9.0.1"
3+
spec.version = "9.0.2"
44
spec.ios.deployment_target = "13.0"
55
spec.summary = "Retrieve user shares"
66
spec.homepage = "https://github.com/torusresearch/torus-utils-swift"

0 commit comments

Comments
 (0)