CONTEXT: encryption and decryption

This commit is contained in:
Alex Gleason 2025-05-16 13:43:59 -05:00
parent b24578f0db
commit 856d333c30
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

View File

@ -304,7 +304,8 @@ function MyComponent() {
const handleUpload = async (file: File) => { const handleUpload = async (file: File) => {
try { try {
// The first tuple in the array contains the URL // Provides an array of NIP-94 compatible tags
// The first tag in the array contains the URL
const [[_, url]] = await uploadFile(file); const [[_, url]] = await uploadFile(file);
// ...use the url // ...use the url
} catch (error) { } catch (error) {
@ -316,6 +317,27 @@ function MyComponent() {
} }
``` ```
To attach files to kind 1 events, each file's URL should be appended to the event's `content`, and an `imeta` tag should be added for each file. For kind 0 events, the URL by itself can be used in relevant fields of the JSON content.
## Encryption and Decryption
The logged-in user has a `signer` object (matching the NIP-07 signer interface) that can be used for encryption and decryption.
```ts
// Get the current user
const { user } = useCurrentUser();
// Optional guard to check that nip44 is available
if (!user.signer.nip44) {
throw new Error("Please upgrade your signer extension to a version that supports NIP-44 encryption");
}
// Encrypt message to self
const encrypted = await user.signer.nip44.encrypt(user.pubkey, "hello world");
// Decrypt message to self
const decrypted = await user.signer.nip44.decrypt(user.pubkey, encrypted) // "hello world"
```
## Development Practices ## Development Practices
- Uses React Query for data fetching and caching - Uses React Query for data fetching and caching