ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Nip44 Encryption & Nip59 Gift Wrapping

Open manimejia opened this issue 1 year ago • 18 comments

  • updates encryption processing for NDKEvent to optionally handle Nip44
  • adds Nip44 as an optional encryption method for all NDKSigner classes
  • implements Nip44 encryption for NDKPrivateKeySigner and NDKNip07Signer
  • adds NIP59 gift wrap processing to NDKEvent using NIP44 or NIP04.

This code is being tested.

manimejia avatar Jun 04 '24 15:06 manimejia

Im having a hard time testing this. Can't run test scripts locally and can't run this fork (via git) as dependency of my existing project.
On both accounts npm install fails with Unsupported URL Type "workspace:" workspace:*. I tried installing pnpm locally but could not due to node version conflicts or something. (It got difficult)

Maybe I'm a dunce, but I have no way of "testing" this code at the moment. Please help? @erskingardner @pablof7z

manimejia avatar Jun 05 '24 17:06 manimejia

I started working on the same Nip44 encryption for Signers yesterday. Before starting I checked there was no PR. Now I just saw your PR. I completed the implementation earlier in the morning but I have been testing this since then. Initially, I had no idea how to test this but I remembered that we can create an installable file by running pnpm pack.

First run pnpm run build from the root of the project Then go to ndk folder and run pnpm pack After this you should have a file like nostr-dev-kit-ndk-5.0.0.tgz

copy the whole file path and go to some other project where you use this ndk and run npm i /path/to/your-package-name-version.tgz

sabhas avatar Jun 05 '24 18:06 sabhas

I'm just now adding NIP59 (gift wrap) support to encryption.ts. This will make a "feature complete" implementation of best practices for (private message) event encryption, using NDKEvent.

manimejia avatar Jun 05 '24 18:06 manimejia

During some testing, I observed that this statement always returns false. I provided a hex string and it returned false, which is logical as it's not an instance of the String class.

Should I create a separate issue for this, or can it be fixed in this pull request?

sabhas avatar Jun 05 '24 19:06 sabhas

@sabhas issues with instantiation of NDKNip46Signer would not be covered by this PR ... until I start messing around with implementing NIP44 encryption for NDKNip46Signer ... which I have not yet.

manimejia avatar Jun 05 '24 19:06 manimejia

@sabhas issues with instantiation of NDKNip46Signer would not be covered by this PR ... until I start messing around with implementing NIP44 encryption for NDKNip46Signer ... which I have not yet.

So, we'll have to wait for another PR that will support nip44 encryption for NDKNip46Signer.

sabhas avatar Jun 05 '24 19:06 sabhas

@sabhas probably. Only because I have a release deadline for my own project, that requires "gift wrapped" direct messages, but does not implement Nip46 (remote signing). In addition ... it doesn't seem that the issue you highlighted has anything to do with NIP44 encryption in general, or the code in this PR specifically.

manimejia avatar Jun 05 '24 19:06 manimejia

@manimejia I agree that the issue I highlighted is not relevant to nip44 encryption. But you have already implemented nip44 encryption for 2 signer classes (nip07 and private-key signer). I thought it would make sense to implement this for nip46 too.

sabhas avatar Jun 05 '24 20:06 sabhas

@sabhas yes it would make sense. But TBH, I have not wrapped my head around Nip46, and NDK implementation yet. So I table it.

I'm under a deadline (working unpaid hours) to get a release pushed so that I can maybe raise some funding to continue work. So time is critical, and THIS pr will be limited. Thanks.

manimejia avatar Jun 05 '24 20:06 manimejia

I like the approach of trying to keep the event.encrypt() and event.decrypt() methods in the same place. But adding an non-optional param to those methods still breaks user space (which @pablof7z and I are very against). Making the nip param optional and default to nip-04 would maintain the current functionality.

Long term I think we probably want to refactor all the encryption code into their own modules/etc. so that we can continue to add different types of encryption but for now this is probably ok.

@pablof7z thoughts?

erskingardner avatar Jun 08 '24 08:06 erskingardner

I am following @sabhas suggestion for testing.

First run pnpm run build from the root of the project Then go to ndk folder and run pnpm pack. After this you should have a file like nostr-dev-kit-ndk-5.0.0.tgz

I am testing now for NIP44 and NIP59 functionality. Some bugs to iron out.

manimejia avatar Jun 08 '24 21:06 manimejia

I just added some tests, but I'm not (yet) sure how to get jest running on my local. I DID test the gift wrapping functionality in my running Svelte app, using the SAME code as the gift wrap test in this latest commit. It failed when calling giftUnwrap(), with the following error:

Error: invalid MAC
    decrypt22 chunk-F5SKXYI2.js:4938
    decrypt @nostr-dev-kit_ndk.js:7064
    giftUnwrap @nostr-dev-kit_ndk.js:7161
    instance testencryption.svelte:25

I've tried to get to the bottom of this... but I don't have a clue how encryption can succeed but decryption fails. This is where I'm stuck for now.

manimejia avatar Jun 12 '24 15:06 manimejia

Bump. Still stuck. Eager to get this PR merged. Any feedback? @erskingardner @pablof7z

manimejia avatar Jun 25 '24 16:06 manimejia

Hey @manimejia sorry for the delay. I've been deep down an MLS rabbit hole. I'll have a look through this PR properly tomorrow morning.

erskingardner avatar Jun 25 '24 16:06 erskingardner

We can drop a $210 bounty on this if you can add NIP46 / nsecbunker signing support.. ?

ghost avatar Jun 26 '24 10:06 ghost

@manimejia Overall this looks good to me. From what I could see, this defaults to using NIP-04 encryption so we're not breaking anything for library users.

I do think that we need to cover remote signers before we can merge this PR though.

erskingardner avatar Jun 26 '24 12:06 erskingardner

@erskingardner before I move on to Nip46 support, I need to make sure what I've built already works. See above for my troubles in testing. Do you have suggestions or help for me to get this working "as it is"?

manimejia avatar Jun 28 '24 18:06 manimejia

We can drop a $210 bounty on this if you can add NIP46 / nsecbunker signing support.. ?

@sigit-io i will tackle this as soon as I can test my existing contribution. See above comment. Thanks for the generous offer.

manimejia avatar Jun 28 '24 18:06 manimejia

Hi everyone, it seems the work on this PR is paused since the end of June. I'm interested in concluding this or cooperate if you agree. From my understanding after a first look, support for NIP-46 must still be added to conclude the PR. I think finishing this is important to make NIP-17 DMs available for NDK developers. What are your thoughts on this?

rodant avatar Nov 02 '24 11:11 rodant

Hi everyone, it seems the work on this PR is paused since the end of June. I'm interested in concluding this or cooperate if you agree. From my understanding after a first look, support for NIP-46 must still be added to conclude the PR. I think finishing this is important to make NIP-17 DMs available for NDK developers. What are your thoughts on this?

Hi -- I'd love to have GiftWrap support; I haven't had a reason to do it yet, but NIP-44 is supported in NDK. Would love to see GW and NIP-17 DMs added!

pablof7z avatar Nov 02 '24 12:11 pablof7z

Hi everyone, it seems the work on this PR is paused since the end of June. I'm interested in concluding this or cooperate if you agree. From my understanding after a first look, support for NIP-46 must still be added to conclude the PR. I think finishing this is important to make NIP-17 DMs available for NDK developers. What are your thoughts on this?

Hi -- I'd love to have GiftWrap support; I haven't had a reason to do it yet, but NIP-44 is supported in NDK. Would love to see GW and NIP-17 DMs added!

I merged the current master branch from ndk in my feature branch and fixed some small errors. But I'm stuck running the tests. It seems some configuration for jest is missing, I get compile errors when running pnpm run test, like:

TypeError: Class extends value undefined is not a constructor or null

       5 | import { NDKKind } from "../index.js";
       6 |
    >  7 | export class NDKCashuMintList extends NDKEvent {

How can I run the tests?

rodant avatar Nov 04 '24 15:11 rodant

I decided to jump forward to the current state of the master branch instead of trying to use this old branch. But I can't run the jest tests successfully at this clean master state.


Test Suites: 6 failed, 1 skipped, 19 passed, 25 of 26 total
Tests:       10 failed, 8 skipped, 134 passed, 152 total
Snapshots:   0 total
Time:        14.841 s, estimated 17 s
Ran all test suites.

Some of the tests failing are:

  • src/ndk/fetch-event-from-tag.test.ts
  • src/relay/sets/calculate.test.ts
  • src/signers/nip46/index.test.ts
  • src/relay/connectivity.test.ts
  • src/subscription/index.test.ts
  • src/events/encode.test.ts

I'm running under node 20.17.0 and I run pnpm run pretest and then pnpm run test. @pablof7z @erskingardner am I missing some important step to run the tests?

rodant avatar Nov 13 '24 16:11 rodant

Hi everyone, for your information, I just activated a new PR (https://github.com/nostr-dev-kit/ndk/pull/279) which is a continuation (imo a replacement) of the work done in this one. I'm looking forward to your feedback.

rodant avatar Dec 09 '24 12:12 rodant

@manimejia / @rodant - feel free to jump onto https://chat.nostrdev.com to discuss that bounty

ghost avatar Dec 09 '24 12:12 ghost

this was completed

pablof7z avatar Mar 20 '25 23:03 pablof7z