From 2f5aaab461adeeeaea16b092e117c8d517ec5c6e Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 19 Nov 2024 11:33:17 -0800 Subject: [PATCH] another try on signing --- Package.swift | 4 +- Sources/XMTPiOS/KeyUtil.swift | 50 +++++++++++-------- XMTP.podspec | 2 +- .../XMTPiOSExample.xcodeproj/project.pbxproj | 20 ++++---- .../xcshareddata/swiftpm/Package.resolved | 27 ++++------ 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/Package.swift b/Package.swift index 5b010e3c..1ecb5bc9 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/blocto/secp256k1.swift", from: "0.7.4"), + .package(url: "https://github.com/tesseract-one/CSecp256k1.swift.git", from: "0.2.0"), .package(url: "https://github.com/bufbuild/connect-swift", exact: "1.0.0"), .package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.4.3"), .package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "3.0.3"), @@ -27,7 +27,7 @@ let package = Package( .target( name: "XMTPiOS", dependencies: [ - .product(name: "secp256k1Swift", package: "secp256k1.swift"), + .product(name: "CSecp256k1", package: "CSecp256k1.swift"), .product(name: "Connect", package: "connect-swift"), .product(name: "LibXMTP", package: "libxmtp-swift"), .product(name: "CryptoSwift", package: "CryptoSwift") diff --git a/Sources/XMTPiOS/KeyUtil.swift b/Sources/XMTPiOS/KeyUtil.swift index 054b0f44..ea28ff9a 100644 --- a/Sources/XMTPiOS/KeyUtil.swift +++ b/Sources/XMTPiOS/KeyUtil.swift @@ -1,5 +1,5 @@ import Foundation -import secp256k1Swift +import CSecp256k1 import LibXMTP import CryptoSwift @@ -26,36 +26,44 @@ enum KeyUtilx { static func recoverPublicKeyKeccak256(from data: Data, message: Data) throws -> Data { return Data(try LibXMTP.recoverPublicKeyK256Keccak256(message: message, signature: data)) } - + static func sign(message: Data, with privateKey: Data, hashing: Bool) throws -> Data { - // Hash the message if required - let msgData = hashing ? message.sha3(.keccak256) : message - - // Ensure the private key is valid - guard privateKey.count == 32 else { - throw KeyUtilError.privateKeyInvalid + guard let ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY)) else { + throw KeyUtilError.invalidContext } - // Create a Signing.PrivateKey instance - guard let signingKey = try? secp256k1.Signing.PrivateKey(rawRepresentation: privateKey) else { - throw KeyUtilError.privateKeyInvalid + defer { + secp256k1_context_destroy(ctx) } - - // Sign the message - guard let signature = try? signingKey.ecdsa.signature(for: msgData) else { + let msgData = hashing ? Util.keccak256(message) : message + let msg = (msgData as NSData).bytes.assumingMemoryBound(to: UInt8.self) + let privateKeyPtr = (privateKey as NSData).bytes.assumingMemoryBound(to: UInt8.self) + let signaturePtr = UnsafeMutablePointer.allocate(capacity: 1) + defer { + signaturePtr.deallocate() + } + guard secp256k1_ecdsa_sign_recoverable(ctx, signaturePtr, msg, privateKeyPtr, nil, nil) == 1 else { throw KeyUtilError.signatureFailure } - // Obtain the compact signature and recovery ID - let compactSignature = try signature.compactRepresentation - let recoveryID: UInt8 = 0 + let outputPtr = UnsafeMutablePointer.allocate(capacity: 64) + defer { + outputPtr.deallocate() + } + var recid: Int32 = 0 + secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, outputPtr, &recid, signaturePtr) + + let outputWithRecidPtr = UnsafeMutablePointer.allocate(capacity: 65) + defer { + outputWithRecidPtr.deallocate() + } + outputWithRecidPtr.assign(from: outputPtr, count: 64) + outputWithRecidPtr.advanced(by: 64).pointee = UInt8(recid) - // Combine the compact signature and recovery ID - var signatureWithRecid = Data(compactSignature) - signatureWithRecid.append(recoveryID) + let signature = Data(bytes: outputWithRecidPtr, count: 65) - return signatureWithRecid + return signature } static func generateAddress(from publicKey: Data) -> String { diff --git a/XMTP.podspec b/XMTP.podspec index b3674da8..63c9919e 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -20,7 +20,7 @@ Pod::Spec.new do |spec| spec.source_files = "Sources/**/*.swift" spec.frameworks = "CryptoKit", "UIKit" - spec.dependency 'secp256k1Swift' + spec.dependency 'CSecp256k1', '~> 0.2' spec.dependency "Connect-Swift", "= 1.0.0" spec.dependency 'LibXMTP', '= 3.0.3' spec.dependency 'CryptoSwift', '= 1.8.3' diff --git a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj index 04c35af4..68926c96 100644 --- a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj +++ b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj @@ -34,7 +34,7 @@ A6C0F3862AC1E549008C6AA7 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6C0F3852AC1E549008C6AA7 /* Data.swift */; }; A6D192D0293A7B97006B49F2 /* ConversationListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6D192CF293A7B97006B49F2 /* ConversationListView.swift */; }; E513AEA32CE6AF2700BC31C3 /* CryptoSwift in Frameworks */ = {isa = PBXBuildFile; productRef = E513AEA22CE6AF2700BC31C3 /* CryptoSwift */; }; - E58362592CEC42A3003D5D00 /* secp256k1Swift in Frameworks */ = {isa = PBXBuildFile; productRef = E58362582CEC42A3003D5D00 /* secp256k1Swift */; }; + E583625C2CED1643003D5D00 /* CSecp256k1 in Frameworks */ = {isa = PBXBuildFile; productRef = E583625B2CED1643003D5D00 /* CSecp256k1 */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -84,9 +84,9 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - E58362592CEC42A3003D5D00 /* secp256k1Swift in Frameworks */, E513AEA32CE6AF2700BC31C3 /* CryptoSwift in Frameworks */, A6606A1A2B5EE80000E2ED4D /* XMTPiOS in Frameworks */, + E583625C2CED1643003D5D00 /* CSecp256k1 in Frameworks */, A6C0F37B2AC1E321008C6AA7 /* Starscream in Frameworks */, A65F070A297B5E8600C3C76E /* KeychainAccess in Frameworks */, ); @@ -207,7 +207,7 @@ A6606A192B5EE80000E2ED4D /* XMTPiOS */, E513AEA22CE6AF2700BC31C3 /* CryptoSwift */, E58362542CEC0B94003D5D00 /* secp256k1 */, - E58362582CEC42A3003D5D00 /* secp256k1Swift */, + E583625B2CED1643003D5D00 /* CSecp256k1 */, ); productName = XMTPiOSExample; productReference = A628198F292DC825004B9117 /* XMTPiOSExample.app */; @@ -241,7 +241,7 @@ A65F0708297B5E8600C3C76E /* XCRemoteSwiftPackageReference "KeychainAccess" */, A6C0F3792AC1E321008C6AA7 /* XCRemoteSwiftPackageReference "Starscream" */, E513AEA12CE6AF2700BC31C3 /* XCRemoteSwiftPackageReference "CryptoSwift" */, - E58362572CEC42A3003D5D00 /* XCRemoteSwiftPackageReference "secp256k1" */, + E583625A2CED1643003D5D00 /* XCRemoteSwiftPackageReference "CSecp256k1.swift" */, ); productRefGroup = A6281990292DC825004B9117 /* Products */; projectDirPath = ""; @@ -556,12 +556,12 @@ minimumVersion = 1.8.3; }; }; - E58362572CEC42A3003D5D00 /* XCRemoteSwiftPackageReference "secp256k1" */ = { + E583625A2CED1643003D5D00 /* XCRemoteSwiftPackageReference "CSecp256k1.swift" */ = { isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/blocto/secp256k1.swift"; + repositoryURL = "https://github.com/tesseract-one/CSecp256k1.swift.git"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 0.7.4; + minimumVersion = 0.2.0; }; }; /* End XCRemoteSwiftPackageReference section */ @@ -590,10 +590,10 @@ isa = XCSwiftPackageProductDependency; productName = secp256k1; }; - E58362582CEC42A3003D5D00 /* secp256k1Swift */ = { + E583625B2CED1643003D5D00 /* CSecp256k1 */ = { isa = XCSwiftPackageProductDependency; - package = E58362572CEC42A3003D5D00 /* XCRemoteSwiftPackageReference "secp256k1" */; - productName = secp256k1Swift; + package = E583625A2CED1643003D5D00 /* XCRemoteSwiftPackageReference "CSecp256k1.swift" */; + productName = CSecp256k1; }; /* End XCSwiftPackageProductDependency section */ }; diff --git a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 7dea0195..21d422e2 100644 --- a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -18,6 +18,15 @@ "version" : "1.8.3" } }, + { + "identity" : "csecp256k1.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tesseract-one/CSecp256k1.swift.git", + "state" : { + "revision" : "cfbd6f540d5084bc96a60af841121472fbe725a3", + "version" : "0.2.0" + } + }, { "identity" : "keychainaccess", "kind" : "remoteSourceControl", @@ -36,24 +45,6 @@ "version" : "3.0.3" } }, - { - "identity" : "secp256k1", - "kind" : "remoteSourceControl", - "location" : "https://github.com/portto/secp256k1.git", - "state" : { - "revision" : "6864a2560066cedede330c4b344689432a7300f7", - "version" : "0.0.5" - } - }, - { - "identity" : "secp256k1.swift", - "kind" : "remoteSourceControl", - "location" : "https://github.com/blocto/secp256k1.swift", - "state" : { - "revision" : "23aa6bab1f60e513297d0d58a863418f68534e56", - "version" : "0.7.4" - } - }, { "identity" : "starscream", "kind" : "remoteSourceControl",