Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GiftWrap and NIP-17 DMs #279

Merged
merged 24 commits into from
Jan 12, 2025

Conversation

rodant
Copy link
Contributor

@rodant rodant commented Nov 3, 2024

This PR is a continuation of the work by @manimejia in #233. Besides fixing test errors support for NIP-46 signers must still be added.

Thanks to @manimejia for the great work so far!

@rodant
Copy link
Contributor Author

rodant commented Dec 9, 2024

I'm glad for bringing this PR on the table now. Besides adding support for Nip-46 signers and convenience methods for Nip-17 encryption, some bugs were fixed and several unit tests were added.

I'd still format all the PR files according to the projects configuration, but haven't done it yet to keep the review simpler. I'm looking forward to your thoughts.

@ekzyis
Copy link

ekzyis commented Dec 10, 2024

Hello, thanks for the PR! Will this also support NIP-44 for NWC?

I think not but wanted to make sure if I understand correctly that NIP-44 for NWC will require another PR.

@rodant
Copy link
Contributor Author

rodant commented Dec 10, 2024

Hello, thanks for the PR! Will this also support NIP-44 for NWC?

I think not but wanted to make sure if I understand correctly that NIP-44 for NWC will require another PR.

Hi, I'm not sure about NWC, but Nip-44 encryption is generally supported, even before this PR. It comes to the signer used, if it support it, you can use Nip-44 encryption with NDK.

@rodant
Copy link
Contributor Author

rodant commented Dec 17, 2024

Hi all of you, just bumping this PR. Any thoughts from the contributors, @pablof7z , @erskingardner ?

@erskingardner
Copy link
Collaborator

@rodant can you fix the merge conflicts? I'll try and review then.

@rodant
Copy link
Contributor Author

rodant commented Dec 18, 2024

@rodant can you fix the merge conflicts? I'll try and review then.

Hi, sure. I'll take care of this in the next 2 hours. Thanks for reacting!

@erskingardner
Copy link
Collaborator

@rodant had a quick spin through now and left a few comments - mostly very small.

@rodant rodant requested a review from erskingardner December 18, 2024 16:02
@erskingardner
Copy link
Collaborator

@rodant FYI, i've not been working on NDK for a few months so I've pinged @pablof7z to have a look before merging just to make sure it's not clobbering anything else he's working on

@rodant
Copy link
Contributor Author

rodant commented Dec 19, 2024

@rodant FYI, i've not been working on NDK for a few months so I've pinged @pablof7z to have a look before merging just to make sure it's not clobbering anything else he's working on

All right, thanks for your quick and helpful review. It was a pleasure to interact with you.

Copy link
Collaborator

@pablof7z pablof7z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some changes requested, most importantly there is no signature validation of the inner event!

this: NDKEvent,
recipient?: NDKUser,
signer?: NDKSigner,
nip : EncryptionNip | undefined = defaultEncryption
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nip04 is/should be getting deprecated, so this should just default to nip44, if a developer wants to stick with nip04 encryption they should always pass in that scheme; this approach makes the choice between nip04 and nip44 appear much more subjective.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds good, I'll change it.

export type EncryptionMethod = 'encrypt' | 'decrypt'

// some clients may wish to set a default for message encryption...
let defaultEncryption : EncryptionNip | undefined = undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be anchored to the singleton ndk -- some applications might want to have multiple instances of NDK with different default encryptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it makes sense. I'll move this as well as the EncryptionNip type definition to the NDK class. This way there are less circular dependencies.

const signer = NDKPrivateKeySigner.generate();
const content = await signer.encrypt(recipient, JSON.stringify(sealed), params?.encryptionNip || 'nip44')
const pubkey = (await signer.user()).pubkey
let wrap : any = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should just be something like:

const wrap = new NDKEvent();
wrap.kind = NDKKind.GiftWrap,
wrap.created_at = approximateNow(5)
if (params?.wrapTags) wrap.tags = params.wrapTags;
wrap.tag(recipient);
wrap.content = JSON.stringify(sealed.rawEvent())
await wrap.encrypt(recipient, signer);
await wrap.sign(signer);
return wrap;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, your version is kind of more elegant. I'll take over your proposal.

if (!rumor) throw new Error("Failed to decrypt seal")

if (seal.pubkey === rumor.pubkey) {
return new NDKEvent(this.ndk, rumor as NostrEvent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: need to manually perform a signature validation of the rumor otherwise anyone could fake these

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean the seal event, right? The rumor must not be signed according to the nip. That's really important, good point.

@rodant rodant requested a review from pablof7z December 20, 2024 12:08
@rodant
Copy link
Contributor Author

rodant commented Dec 28, 2024

@pablof7z just for the case, I pushed changes after your comments last week.

@pablof7z
Copy link
Collaborator

@rodant thanks for tackling this and for the followups. this was a large PR to review so thanks for the enormous patience.

@pablof7z pablof7z merged commit 82b60c3 into nostr-dev-kit:master Jan 12, 2025
@rodant
Copy link
Contributor Author

rodant commented Jan 13, 2025

@rodant thanks for tackling this and for the followups. this was a large PR to review so thanks for the enormous patience.

Low time preference is our strength, right? ;-) I'm very happy to see the PR merged in NDK. Now users have a much better option for Nostr DMs. Kudos to all people involved in this.

pablof7z added a commit that referenced this pull request Jan 13, 2025
commit 80efbd8
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 13:00:01 2025 +0000

    nutzap monitor hook

commit 42edebf
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 12:58:54 2025 +0000

    be more careful with unpublished events

commit 620c078
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:39:02 2025 +0000

    track pools

commit af92e4a
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:36:58 2025 +0000

    fix imeta loading in NDKVideo

commit 965bb64
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:36:40 2025 +0000

    fix accidental client tag removal

commit 8248e83
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 00:12:28 2025 +0000

    refactor gift wraps to avoid circular dependencies

commit ddb82d1
Author: pablof7z <[email protected]>
Date:   Sun Jan 12 23:40:21 2025 +0000

    wip

commit 99fe7af
Author: pablof7z <[email protected]>
Date:   Sun Jan 12 22:45:03 2025 +0000

    doc fixes

commit 108a1b4
Author: pablof7z <[email protected]>
Date:   Fri Jan 10 08:40:10 2025 -0600

    avoids distributing events that were NIP-70ed

    nostr-protocol/nips#1679

commit b974b71
Author: Pablo Fernandez <[email protected]>
Date:   Sun Jan 12 21:17:58 2025 +0000

    normalize opt

commit 597a92a
Author: DanConwayDev <[email protected]>
Date:   Sun Jan 12 21:16:40 2025 +0000

    add subscription option: dont save to cache (#241)

    so that, optionally, only events from important subscriptions are
    retained in the cache to keep it lean and mean, like a venison steak

    Co-authored-by: Pablo Fernandez <[email protected]>

commit 2263f32
Author: Vivek Ganesan <[email protected]>
Date:   Mon Jan 13 02:43:29 2025 +0530

    Correct the ndk wallet usage instructions (#292)

commit 82b60c3
Author: rodant <[email protected]>
Date:   Sun Jan 12 22:12:56 2025 +0100

    Add GiftWrap and NIP-17 DMs (#279)

    * rename nip04.ts to encryption.ts

    * implement nip44 as optional encryption nip for signers

    * UNTESTED implement nip44 as optional encryption for NDKPrivateSigner

    UNTESTED with a test written

    * BUGFIX wrong import and wrong privateKey params

    * ADDED Nip44 support for NDKNip07Signer

    also some buigfiixes for encryptionEnabled()
    UNTESTED : this commit has not been tested yet.

    * UNTESTED adds Nip59 giftWrap and giftUnwrap for NDKEvents

    * ADDED tests to encryption.test.ts

    coppied Nip44 test for encrypt and decrypt
    new Nip59 test for gift wrap and unwrap

    * UPDATE add encryoption kinds to NDKKind enum

    * Fix compile errors matching current master of upstream ndk repo.

    * Fix compile errors.

    * Revert dev dependency changes.

    * Fallback to original pnpm-lock file.

    * Extract gift-wrapping from encryption.ts

    * Fix MAC error in encryption test.

    * Add tests for Nip07 signer and refactorings.

    * Small refactoring.

    * Support for Nip46 signers and unit tests for that.

    * Test for forced Nip04 encryption and bug fixing.

    * Nip17 encryption methods and more tests.

    * Fix some of the review comments.

    * Changes after review from @pablof7z.

    ---------

    Co-authored-by: manimejia <[email protected]>
pablof7z added a commit that referenced this pull request Jan 13, 2025
commit ba5be49
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 13:06:26 2025 +0000

    refactor wip

commit 80efbd8
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 13:00:01 2025 +0000

    nutzap monitor hook

commit 42edebf
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 12:58:54 2025 +0000

    be more careful with unpublished events

commit 620c078
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:39:02 2025 +0000

    track pools

commit af92e4a
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:36:58 2025 +0000

    fix imeta loading in NDKVideo

commit 965bb64
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 11:36:40 2025 +0000

    fix accidental client tag removal

commit 8248e83
Author: pablof7z <[email protected]>
Date:   Mon Jan 13 00:12:28 2025 +0000

    refactor gift wraps to avoid circular dependencies

commit ddb82d1
Author: pablof7z <[email protected]>
Date:   Sun Jan 12 23:40:21 2025 +0000

    wip

commit 99fe7af
Author: pablof7z <[email protected]>
Date:   Sun Jan 12 22:45:03 2025 +0000

    doc fixes

commit 108a1b4
Author: pablof7z <[email protected]>
Date:   Fri Jan 10 08:40:10 2025 -0600

    avoids distributing events that were NIP-70ed

    nostr-protocol/nips#1679

commit b974b71
Author: Pablo Fernandez <[email protected]>
Date:   Sun Jan 12 21:17:58 2025 +0000

    normalize opt

commit 597a92a
Author: DanConwayDev <[email protected]>
Date:   Sun Jan 12 21:16:40 2025 +0000

    add subscription option: dont save to cache (#241)

    so that, optionally, only events from important subscriptions are
    retained in the cache to keep it lean and mean, like a venison steak

    Co-authored-by: Pablo Fernandez <[email protected]>

commit 2263f32
Author: Vivek Ganesan <[email protected]>
Date:   Mon Jan 13 02:43:29 2025 +0530

    Correct the ndk wallet usage instructions (#292)

commit 82b60c3
Author: rodant <[email protected]>
Date:   Sun Jan 12 22:12:56 2025 +0100

    Add GiftWrap and NIP-17 DMs (#279)

    * rename nip04.ts to encryption.ts

    * implement nip44 as optional encryption nip for signers

    * UNTESTED implement nip44 as optional encryption for NDKPrivateSigner

    UNTESTED with a test written

    * BUGFIX wrong import and wrong privateKey params

    * ADDED Nip44 support for NDKNip07Signer

    also some buigfiixes for encryptionEnabled()
    UNTESTED : this commit has not been tested yet.

    * UNTESTED adds Nip59 giftWrap and giftUnwrap for NDKEvents

    * ADDED tests to encryption.test.ts

    coppied Nip44 test for encrypt and decrypt
    new Nip59 test for gift wrap and unwrap

    * UPDATE add encryoption kinds to NDKKind enum

    * Fix compile errors matching current master of upstream ndk repo.

    * Fix compile errors.

    * Revert dev dependency changes.

    * Fallback to original pnpm-lock file.

    * Extract gift-wrapping from encryption.ts

    * Fix MAC error in encryption test.

    * Add tests for Nip07 signer and refactorings.

    * Small refactoring.

    * Support for Nip46 signers and unit tests for that.

    * Test for forced Nip04 encryption and bug fixing.

    * Nip17 encryption methods and more tests.

    * Fix some of the review comments.

    * Changes after review from @pablof7z.

    ---------

    Co-authored-by: manimejia <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants