Nostr: sign events directly with Schnorr keys; update call sites to use Schnorr and remove temporary Signing.PrivateKey conversions (#521)

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
jack
2025-08-25 18:31:53 +02:00
committed by GitHub
co-authored by jack
parent 60b0deee7b
commit 680a390b2d
+9 -14
View File
@@ -122,8 +122,8 @@ struct NostrProtocol {
tags: tags, tags: tags,
content: content content: content
) )
let signingKey = try senderIdentity.signingKey() let schnorrKey = try senderIdentity.schnorrSigningKey()
return try event.sign(with: signingKey) return try event.sign(with: schnorrKey)
} }
// MARK: - Private Methods // MARK: - Private Methods
@@ -149,9 +149,8 @@ struct NostrProtocol {
content: encrypted content: encrypted
) )
// Convert to P256K.Signing.PrivateKey for signing (temporary until we update sign method) // Sign the seal with the sender's Schnorr private key
let signingKey = try P256K.Signing.PrivateKey(dataRepresentation: senderKey.dataRepresentation) return try seal.sign(with: senderKey)
return try seal.sign(with: signingKey)
} }
private static func createGiftWrap( private static func createGiftWrap(
@@ -181,9 +180,8 @@ struct NostrProtocol {
content: encrypted content: encrypted
) )
// Convert to P256K.Signing.PrivateKey for signing (temporary until we update sign method) // Sign the gift wrap with the wrap Schnorr private key
let signingKey = try P256K.Signing.PrivateKey(dataRepresentation: wrapKey.dataRepresentation) return try giftWrap.sign(with: wrapKey)
return try giftWrap.sign(with: signingKey)
} }
private static func unwrapGiftWrap( private static func unwrapGiftWrap(
@@ -472,19 +470,16 @@ struct NostrEvent: Codable {
self.sig = dict["sig"] as? String self.sig = dict["sig"] as? String
} }
func sign(with key: P256K.Signing.PrivateKey) throws -> NostrEvent { func sign(with key: P256K.Schnorr.PrivateKey) throws -> NostrEvent {
let (eventId, eventIdHash) = try calculateEventId() let (eventId, eventIdHash) = try calculateEventId()
// Convert to Schnorr key for Nostr signing // Sign with Schnorr (BIP-340)
let schnorrKey = try P256K.Schnorr.PrivateKey(dataRepresentation: key.dataRepresentation)
// Sign with Schnorr
var messageBytes = [UInt8](eventIdHash) var messageBytes = [UInt8](eventIdHash)
var auxRand = [UInt8](repeating: 0, count: 32) var auxRand = [UInt8](repeating: 0, count: 32)
_ = auxRand.withUnsafeMutableBytes { ptr in _ = auxRand.withUnsafeMutableBytes { ptr in
SecRandomCopyBytes(kSecRandomDefault, 32, ptr.baseAddress!) SecRandomCopyBytes(kSecRandomDefault, 32, ptr.baseAddress!)
} }
let schnorrSignature = try schnorrKey.signature(message: &messageBytes, auxiliaryRand: &auxRand) let schnorrSignature = try key.signature(message: &messageBytes, auxiliaryRand: &auxRand)
let signatureHex = schnorrSignature.dataRepresentation.hexEncodedString() let signatureHex = schnorrSignature.dataRepresentation.hexEncodedString()