Remove rust-nostr dependency

This commit removes rust-nostr dependency, and replaces the NIP-44 logic
with a new NIP-44 module based on the Swift NostrSDK implementation.

The decision to move away from rust-nostr and the Swift NostrSDK was
made for the following reasons:
1. `rust-nostr` caused the app size to double
2. We only need NIP44 functionality, and we don't need to bring
   everything else
3. The Swift NostrSDK caused conflicts around the secp256k1 dependency
   that is hard to address
4. The way we do things in the codebase is far different from the Swift
   NostrSDK, and we optimize it for use with NostrDB. Bringing it an
   outside library causes significant complexity in integration with
   NostrDB, and would effectively cause the codebase to be split into
   two different ways of achieving the same results. Therefore it is
   cleaner if we stick to our own Nostr structures and functions and
   focus on maintaining them.

However, the library CryptoSwift was added as a dependency, to bring in
ChaCha20 which is not supported by CryptoKit (CryptoKit supports the
ChaCha20-Poly1305 cipher, but NIP-44 uses ChaCha20 with HMAC-SHA256
instead)

Closes: https://github.com/damus-io/damus/issues/2849
Changelog-Changed: Made internal changes to reduce the app binary size
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-02-10 15:07:36 -08:00
parent 9e7943e0e9
commit a6e123e928
9 changed files with 1559 additions and 74 deletions

View File

@@ -4,7 +4,6 @@
//
// Created by Daniel DAquino on 2025-01-20.
//
import NostrSDK
import Foundation
/// This models a NIP-37 draft.
@@ -77,13 +76,7 @@ struct NIP37Draft {
guard let note_json_string = String(data: note_json_data, encoding: .utf8) else {
throw NIP37DraftEventError.encoding_error
}
guard let secret_key = SecretKey.from(privkey: keypair.privkey) else {
throw NIP37DraftEventError.invalid_keypair
}
guard let pubkey = PublicKey.from(pubkey: keypair.pubkey) else {
throw NIP37DraftEventError.invalid_keypair
}
guard let contents = try? nip44Encrypt(secretKey: secret_key, publicKey: pubkey, content: note_json_string, version: Nip44Version.v2) else {
guard let contents = try? NIP44v2Encryption.encrypt(plaintext: note_json_string, privateKeyA: keypair.privkey, publicKeyB: keypair.pubkey) else {
return nil
}
var tags = [
@@ -111,16 +104,10 @@ struct NIP37Draft {
static func unwrap(note: NdbNote, keypair: FullKeypair) throws -> NdbNote? {
let wrapped_note = note
guard wrapped_note.known_kind == .draft else { return nil }
guard let private_key = SecretKey.from(privkey: keypair.privkey) else {
throw NIP37DraftEventError.invalid_keypair
}
guard let pubkey = PublicKey.from(pubkey: keypair.pubkey) else {
throw NIP37DraftEventError.invalid_keypair
}
guard let draft_event_json = try? nip44Decrypt(
secretKey: private_key,
publicKey: pubkey,
payload: wrapped_note.content
guard let draft_event_json = try? NIP44v2Encryption.decrypt(
payload: wrapped_note.content,
privateKeyA: keypair.privkey,
publicKeyB: keypair.pubkey
) else { return nil }
return NdbNote.owned_from_json(json: draft_event_json)
}
@@ -130,17 +117,3 @@ struct NIP37Draft {
case encoding_error
}
}
// MARK: - Convenience extensions
fileprivate extension PublicKey {
static func from(pubkey: Pubkey) -> PublicKey? {
return try? PublicKey.parse(publicKey: pubkey.hex())
}
}
fileprivate extension SecretKey {
static func from(privkey: Privkey) -> SecretKey? {
return try? SecretKey.parse(secretKey: privkey.hex())
}
}