Skip to content

Commit f5dfb3a

Browse files
committed
feat(nip-19): enhance bech32 entity support
- Add naddr encoding/decoding for addressable entities - Add nrelay encoding/decoding for relay URLs - Improve validation and error handling - Add support for multiple relays in profile/event encodings - Update documentation with new features and examples BREAKING CHANGE: TLV encoding now supports 32-bit kinds
1 parent de6b324 commit f5dfb3a

File tree

3 files changed

+237
-31
lines changed

3 files changed

+237
-31
lines changed

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,78 @@ This library implements the following Nostr Implementation Possibilities (NIPs):
6565
- Note IDs (`note`)
6666
- Profile references (`nprofile`)
6767
- Event references (`nevent`)
68+
- Addressable entities (`naddr`)
69+
- Relay URLs (`nrelay`)
70+
- Comprehensive validation and error handling
71+
- Support for multiple relay URLs
72+
- TLV (Type-Length-Value) encoding for complex entities
73+
74+
### NIP-19 Usage Examples
75+
76+
```typescript
77+
import {
78+
npubEncode,
79+
nsecEncode,
80+
noteEncode,
81+
nprofileEncode,
82+
neventEncode,
83+
naddrEncode,
84+
nrelayEncode,
85+
decode
86+
} from '@humanjavaenterprises/nostr-crypto-utils';
87+
88+
// Encode a public key
89+
const npub = npubEncode('7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0');
90+
// npub1xtscya34g58tk0z6v2g0r6w5gpqrxdgkz9xeav
91+
92+
// Encode a private key
93+
const nsec = nsecEncode('7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0');
94+
// nsec1xtscya34g58tk0z6v2g0r6w5gpqrxdgkz9xeav
95+
96+
// Encode a note ID
97+
const note = noteEncode('7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0');
98+
// note1xtscya34g58tk0z6v2g0r6w5gpqrxdgkz9xeav
99+
100+
// Encode a profile with multiple relays
101+
const nprofile = nprofileEncode(
102+
'7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0',
103+
['wss://relay1.example.com', 'wss://relay2.example.com']
104+
);
105+
106+
// Encode an event with author and kind
107+
const nevent = neventEncode(
108+
'7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0',
109+
['wss://relay.example.com'],
110+
'7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0',
111+
1
112+
);
113+
114+
// Encode an addressable entity (NIP-33)
115+
const naddr = naddrEncode(
116+
'7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0',
117+
30023,
118+
'my-article',
119+
['wss://relay.example.com']
120+
);
121+
122+
// Encode a relay URL
123+
const nrelay = nrelayEncode('wss://relay.example.com');
124+
125+
// Decode any bech32-encoded entity
126+
const decoded = decode(nprofile);
127+
console.log(decoded);
128+
// {
129+
// type: 'nprofile',
130+
// data: '7f3b6c2444c526fc7b3a48b0a1e38fb6a5a4062d4a097c9e96feb3c1df2f36d0',
131+
// relays: ['wss://relay1.example.com', 'wss://relay2.example.com']
132+
// }
133+
134+
// All functions include validation
135+
try {
136+
const invalidNpub = npubEncode('invalid-hex');
137+
} catch (error) {
138+
console.error(error); // Error: Invalid hex string
139+
}
68140

69141
### NIP-26 Features
70142
- Create delegation tokens

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nostr-crypto-utils",
3-
"version": "0.4.3",
3+
"version": "0.4.4",
44
"description": "A comprehensive TypeScript library for Nostr protocol implementation, supporting multiple NIPs with strict validation",
55
"type": "module",
66
"main": "./dist/index.js",

0 commit comments

Comments
 (0)