Skip to content

Commit

Permalink
Merge pull request #87 from torusresearch/feat/fix-encrypt
Browse files Browse the repository at this point in the history
fix: encrypt use publickey instead of empheral publickey
  • Loading branch information
ieow authored Apr 8, 2024
2 parents 04c62fd + 51c71ec commit 4c17ef5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
16 changes: 8 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
},
{
"package": "CryptoSwift",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift",
"state": {
"branch": null,
"revision": "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version": "1.7.2"
"revision": "7892a123f7e8d0fe62f9f03728b17bbd4f94df5c",
"version": "1.8.1"
}
},
{
Expand All @@ -48,20 +48,20 @@
},
{
"package": "jwt-kit",
"repositoryURL": "https://github.com/vapor/jwt-kit.git",
"repositoryURL": "https://github.com/vapor/jwt-kit",
"state": {
"branch": null,
"revision": "9e929d925434b91857661bcd455d1bd53f00bf22",
"version": "4.13.0"
"revision": "e05513b5aec24f88012b6e3034115b6bc915356a",
"version": "4.13.2"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "60f13f60c4d093691934dc6cfdf5f508ada1f894",
"version": "2.6.0"
"revision": "f0525da24dc3c6cbb2b6b338b65042bc91cbc4bb",
"version": "3.3.0"
}
}
]
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ let package = Package(
targets: [
.target(
name: "TorusUtils",
dependencies: ["FetchNodeDetails", "CryptoSwift", "AnyCodable", .product(name: "curveSecp256k1", package: "curvelib.swift")]),
dependencies: ["FetchNodeDetails", "CryptoSwift", "AnyCodable",
.product(name: "curveSecp256k1", package: "curvelib.swift"),
.product(name: "encryption_aes_cbc_sha512", package: "curvelib.swift"),
]
),
.testTarget(
name: "TorusUtilsTests",
dependencies: ["TorusUtils", .product(name: "JWTKit", package: "jwt-kit")]
Expand Down
2 changes: 1 addition & 1 deletion Sources/TorusUtils/Extensions/TorusUtils+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ extension TorusUtils {
let ephemPrivateKey = SecretKey()
let ephemPublicKey = try ephemPrivateKey.toPublic()

let sharedSecret = try ecdh_sha512(publicKey: ephemPublicKey, privateKey: ephemPrivateKey)
let sharedSecret = try ecdh_sha512(publicKey: PublicKey(hex: publicKey), privateKey: ephemPrivateKey)

let encryptionKey = Array(sharedSecret[0 ..< 32])
let macKey = Array(sharedSecret[32 ..< 64])
Expand Down
30 changes: 30 additions & 0 deletions Tests/TorusUtilsTests/SapphireTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import JWTKit
import XCTest

import CommonSources
import curveSecp256k1
import encryption_aes_cbc_sha512

@testable import TorusUtils

Expand Down Expand Up @@ -356,5 +358,33 @@ final class SapphireTest: XCTestCase {
}

}

func testencryption() async throws {
let torus = TorusUtils(enableOneKey: true, network: .sapphire(.SAPPHIRE_MAINNET), clientId: "YOUR_CLIENT_ID")

let pk = curveSecp256k1.SecretKey()
let pk_str = try pk.serialize()

let msg = "hello test data"
let encryptData = try torus.encrypt(publicKey: pk.toPublic().serialize(compressed: false), msg: msg)
// let curveMsg = try Encryption.encrypt(pk: pk.toPublic(), data: msg.data(using: .utf8)!)

// let em = try EncryptedMessage(cipherText: encryptData.ciphertext, ephemeralPublicKey: PublicKey(hex: encryptData.ephemPublicKey) , iv: encryptData.iv, mac: encryptData.mac)


let eciesData = ECIES(iv: encryptData.iv, ephemPublicKey: encryptData.ephemPublicKey, ciphertext: encryptData.ciphertext, mac: encryptData.mac)
// let emp = try curveMsg.ephemeralPublicKey().serialize(compressed: false);
// let eciesData2 = try ECIES(iv: curveMsg.iv(), ephemPublicKey: emp, ciphertext: curveMsg.chipherText(), mac: curveMsg.mac())

let decrypteData = try torus.decrypt(privateKey: pk_str, opts: eciesData)
// let decrypteData2 = try torus.decrypt(privateKey: pk_str, opts: eciesData2)

// let result = try Encryption.decrypt(sk: pk, encrypted: em)
// let result2 = try Encryption.decrypt(sk: pk, encrypted: curveMsg)

XCTAssertEqual(msg.data(using: .utf8)!, decrypteData)
// XCTAssertEqual(msg.data(using: .utf8)!, result2)

}

}

0 comments on commit 4c17ef5

Please sign in to comment.