Skip to content

Commit

Permalink
another try on signing
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 19, 2024
1 parent f537e83 commit 2f5aaab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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")
Expand Down
50 changes: 29 additions & 21 deletions Sources/XMTPiOS/KeyUtil.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import secp256k1Swift
import CSecp256k1
import LibXMTP
import CryptoSwift

Expand All @@ -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<secp256k1_ecdsa_recoverable_signature>.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<UInt8>.allocate(capacity: 64)
defer {
outputPtr.deallocate()
}
var recid: Int32 = 0
secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, outputPtr, &recid, signaturePtr)

let outputWithRecidPtr = UnsafeMutablePointer<UInt8>.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 {
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
20 changes: 10 additions & 10 deletions XMTPiOSExample/XMTPiOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */,
);
Expand Down Expand Up @@ -207,7 +207,7 @@
A6606A192B5EE80000E2ED4D /* XMTPiOS */,
E513AEA22CE6AF2700BC31C3 /* CryptoSwift */,
E58362542CEC0B94003D5D00 /* secp256k1 */,
E58362582CEC42A3003D5D00 /* secp256k1Swift */,
E583625B2CED1643003D5D00 /* CSecp256k1 */,
);
productName = XMTPiOSExample;
productReference = A628198F292DC825004B9117 /* XMTPiOSExample.app */;
Expand Down Expand Up @@ -241,7 +241,7 @@
A65F0708297B5E8600C3C76E /* XCRemoteSwiftPackageReference "KeychainAccess" */,
A6C0F3792AC1E321008C6AA7 /* XCRemoteSwiftPackageReference "Starscream" */,
E513AEA12CE6AF2700BC31C3 /* XCRemoteSwiftPackageReference "CryptoSwift" */,
E58362572CEC42A3003D5D00 /* XCRemoteSwiftPackageReference "secp256k1" */,
E583625A2CED1643003D5D00 /* XCRemoteSwiftPackageReference "CSecp256k1.swift" */,
);
productRefGroup = A6281990292DC825004B9117 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 2f5aaab

Please sign in to comment.