1- import debug from "debug" ;
2-
31import * as Nip55 from "expo-nip55" ;
42import { NDKEncryptionScheme , NDKSigner , NDKUser , NostrEvent } from "@nostr-dev-kit/ndk" ;
53
@@ -69,8 +67,41 @@ export class NDKNip55Signer implements NDKSigner {
6967 * @param value - The value to be encrypted.
7068 * @param nip - which NIP is being implemented ('nip04', 'nip44')
7169 */
72- async encrypt ( recipient : NDKUser , value : string , scheme ?: NDKEncryptionScheme ) : Promise < string > {
73- return "" ;
70+ async encrypt ( recipient : NDKUser , value : string , scheme : NDKEncryptionScheme ) : Promise < string > {
71+ const recipientHexPubKey = recipient . pubkey ;
72+
73+ let result : { encryptedText : string } ;
74+
75+ try {
76+ if ( scheme === 'nip44' ) {
77+ result = await Nip55 . nip44Encrypt (
78+ this . packageName ,
79+ value ,
80+ this . _pubkey ,
81+ recipientHexPubKey ,
82+ this . _pubkey
83+ )
84+ } else if ( scheme === 'nip04' ) {
85+ result = await Nip55 . nip04Encrypt (
86+ this . packageName ,
87+ value ,
88+ this . _pubkey ,
89+ recipientHexPubKey ,
90+ this . _pubkey
91+ )
92+ }
93+
94+ const encryptedText = result . encryptedText ;
95+
96+ if ( encryptedText === undefined ) {
97+ throw new Error ( "Encryption failed" ) ;
98+ }
99+
100+ return result . encryptedText ;
101+ } catch ( error ) {
102+ console . error ( "Error encryption failed:" , error ) ;
103+ throw error ;
104+ }
74105 }
75106 /**
76107 * Decrypts the given value.
@@ -79,7 +110,40 @@ export class NDKNip55Signer implements NDKSigner {
79110 * @param value - The value to be decrypted
80111 * @param scheme - which NIP is being implemented ('nip04', 'nip44', 'nip49')
81112 */
82- async decrypt ( sender : NDKUser , value : string , scheme ?: NDKEncryptionScheme ) : Promise < string > {
83- return "" ;
113+ async decrypt ( sender : NDKUser , value : string , scheme : NDKEncryptionScheme ) : Promise < string > {
114+ const senderHexPubKey = sender . pubkey ;
115+
116+ let result : { plainText : string } ;
117+
118+ try {
119+ if ( scheme === 'nip44' ) {
120+ result = await Nip55 . nip44Decrypt (
121+ this . packageName ,
122+ value ,
123+ this . _pubkey ,
124+ senderHexPubKey ,
125+ this . _pubkey
126+ )
127+ } else if ( scheme === 'nip04' ) {
128+ result = await Nip55 . nip04Decrypt (
129+ this . packageName ,
130+ value ,
131+ this . _pubkey ,
132+ senderHexPubKey ,
133+ this . _pubkey
134+ )
135+ }
136+
137+ const plainText = result . plainText ;
138+
139+ if ( plainText === undefined ) {
140+ throw new Error ( "Decryption failed" ) ;
141+ }
142+
143+ return result . plainText ;
144+ } catch ( error ) {
145+ console . error ( "Error decryption failed:" , error ) ;
146+ throw error ;
147+ }
84148 }
85149}
0 commit comments