Skip to content

Commit 104ce4b

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 5257be4 + d8daac3 commit 104ce4b

11 files changed

+3152
-3594
lines changed

.eslintrc.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
"parser": "@typescript-eslint/parser",
1212
"parserOptions": {
1313
"ecmaVersion": "latest",
14-
"sourceType": "module"
14+
"sourceType": "module",
15+
"project": "./tsconfig.eslint.json"
1516
},
1617
"plugins": [
1718
"@typescript-eslint"
1819
],
1920
"rules": {
20-
"@typescript-eslint/no-explicit-any": "warn",
21+
"@typescript-eslint/no-explicit-any": "off",
2122
"@typescript-eslint/explicit-function-return-type": "off",
2223
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
23-
}
24+
},
25+
"ignorePatterns": ["dist/", "node_modules/"]
2426
}

README.md

Lines changed: 75 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# Nostr Nsec Seedphrase Library
1+
# nostr-nsec-seedphrase
22

3-
[![npm version](https://badge.fury.io/js/nostr-nsec-seedphrase.svg)](https://badge.fury.io/js/nostr-nsec-seedphrase)
4-
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5-
[![TypeScript](https://img.shields.io/badge/TypeScript-4.9.5-blue.svg)](https://www.typescriptlang.org/)
6-
[![Node.js CI](https://github.com/HumanjavaEnterprises/nostr-nsec-seedphrase-library/workflows/Node.js%20CI/badge.svg)](https://github.com/HumanjavaEnterprises/nostr-nsec-seedphrase-library/actions)
3+
<div align="center">
74

8-
A comprehensive TypeScript library designed to make Nostr keys more human-readable and easier to manage. By converting `nsec` keys to mnemonic seed phrases, this library simplifies the process of storing and re-entering keys into Nostr applications.
5+
[![npm version](https://img.shields.io/npm/v/nostr-nsec-seedphrase.svg)](https://www.npmjs.com/package/nostr-nsec-seedphrase)
6+
[![npm downloads](https://img.shields.io/npm/dm/nostr-nsec-seedphrase.svg)](https://www.npmjs.com/package/nostr-nsec-seedphrase)
7+
[![License](https://img.shields.io/npm/l/nostr-nsec-seedphrase.svg)](https://github.com/vergelevans/nostr-nsec-seedphrase/blob/main/LICENSE)
8+
[![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
9+
[![Test Status](https://img.shields.io/github/actions/workflow/status/vergelevans/nostr-nsec-seedphrase/test.yml?branch=main&label=tests)](https://github.com/vergelevans/nostr-nsec-seedphrase/actions)
10+
[![Code Style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io/)
911

10-
⚠️ **Important Security Notice**
12+
</div>
1113

12-
This library handles cryptographic keys and seed phrases that are critical for securing your Nostr identity and data. Just like Bitcoin, any seed phrase or private key (`nsec`) generated by this library must be stored with the utmost security and care.
13-
14-
Developers using this library must inform their users about the critical nature of managing seed phrases, `nsec`, and hex keys. It is the user's responsibility to securely store and manage these keys. The library and its authors disclaim any responsibility or liability for lost keys, seed phrases, or data resulting from mismanagement.
14+
A comprehensive TypeScript library for managing Nostr keys with seed phrases, including event signing, verification, and WebSocket utilities.
1515

1616
## Installation
1717

@@ -35,16 +35,18 @@ Full API documentation is available at [https://humanjavaenterprises.github.io/n
3535

3636
## Features
3737

38-
- Generate secure BIP39 mnemonics for Nostr key pairs
39-
- Create nsec private keys from hex format
40-
- Convert between nsec/npub and hex representations
41-
- TypeScript support with comprehensive type definitions
42-
- Secure key management utilities
43-
- Extensive testing and documentation
38+
- 🔑 Generate and manage seed phrases for Nostr keys
39+
- 🔄 Convert between different key formats (hex, nsec, npub)
40+
- ✍️ Sign and verify messages
41+
- 📝 Create and verify Nostr events
42+
- 🌐 WebSocket utilities for Nostr applications
43+
- 📦 TypeScript support with full type definitions
44+
- ✅ Comprehensive test coverage
45+
- 🔒 Secure key management practices
4446

4547
## Usage
4648

47-
### Basic Key Generation
49+
### Key Generation and Management
4850

4951
```typescript
5052
import {
@@ -57,181 +59,91 @@ import {
5759
seedPhraseToKeyPair
5860
} from 'nostr-nsec-seedphrase';
5961

60-
// Generate new keys with seed phrase
62+
// Generate a new key pair with seed phrase
6163
const keyPair = generateKeyPairWithSeed();
6264
console.log(keyPair);
6365
// {
64-
// seedPhrase: "your twelve word seed phrase here",
65-
// nsec: "nsec1...",
66-
// npub: "npub1...",
67-
// privateKey: "hex...",
68-
// publicKey: "hex..."
66+
// privateKey: '...',
67+
// publicKey: '...',
68+
// nsec: '...',
69+
// npub: '...',
70+
// seedPhrase: '...'
6971
// }
7072

71-
// Validate the seed phrase
72-
const isValid = validateSeedPhrase(keyPair.seedPhrase);
73-
console.log(isValid); // true
74-
```
75-
76-
### Converting Between Formats
77-
78-
```typescript
79-
// Convert hex to nsec/npub
80-
const nsec = hexToNsec('your-hex-private-key');
81-
const npub = hexToNpub('your-hex-public-key');
82-
83-
// Convert nsec/npub to hex
84-
const privateKeyHex = nsecToHex('nsec1...');
85-
const publicKeyHex = npubToHex('npub1...');
86-
87-
// Convert seed phrase to key pair
88-
const keyPair = seedPhraseToKeyPair('your twelve word seed phrase here');
73+
// Convert existing seed phrase to key pair
74+
const existingKeyPair = seedPhraseToKeyPair('your twelve word seed phrase here');
8975
```
9076

91-
### Converting Nsec to Seedphrase
77+
### Message Signing and Verification
9278

9379
```typescript
94-
const seedPhrase = await NostrSeedPhrase.nsecToSeed('nsec1...');
95-
console.log(seedPhrase); // "your twelve word mnemonic phrase here"
96-
```
80+
import { signMessage, verifySignature } from 'nostr-nsec-seedphrase';
9781

98-
### Converting Seedphrase to Nsec
82+
// Sign a message
83+
const signature = await signMessage('Hello Nostr!', keyPair.privateKey);
9984

100-
```typescript
101-
const nsec = await NostrSeedPhrase.seedToNsec('your twelve word mnemonic phrase here');
102-
console.log(nsec); // "nsec1..."
85+
// Verify a signature
86+
const isValid = await verifySignature('Hello Nostr!', signature, keyPair.publicKey);
10387
```
10488

105-
### Converting Hex to Nsec
89+
### Event Creation and Verification
10690

10791
```typescript
108-
const nsec = await NostrSeedPhrase.hexToNsec('your-hex-private-key');
109-
console.log(nsec); // "nsec1..."
92+
import { createEvent, verifyEvent } from 'nostr-nsec-seedphrase';
93+
94+
// Create a new event
95+
const event = await createEvent(
96+
'Hello Nostr!', // content
97+
1, // kind (1 = text note)
98+
keyPair.privateKey,
99+
[] // tags (optional)
100+
);
101+
102+
// Verify an event
103+
const isValidEvent = await verifyEvent(event);
110104
```
111105

112-
### Converting Nsec to Hex
106+
## Recent Updates
113107

114-
```typescript
115-
const hex = await NostrSeedPhrase.nsecToHex('nsec1...');
116-
console.log(hex); // "hex..."
117-
```
118-
119-
### Converting Npub to Hex
120-
121-
```typescript
122-
const hex = await NostrSeedPhrase.npubToHex('npub1...');
123-
console.log(hex); // "hex..."
124-
```
125-
126-
### Converting Hex to Npub
127-
128-
```typescript
129-
const npub = await NostrSeedPhrase.hexToNpub('hex...');
130-
console.log(npub); // "npub1..."
131-
```
108+
### v0.2.0
109+
- 🔧 Fixed HMAC configuration for secp256k1
110+
- ✅ Added comprehensive test coverage
111+
- 🎯 Improved TypeScript types
112+
- 📚 Enhanced documentation
132113

133114
## API Reference
134115

135-
### `generateKeyPairWithSeed()`
136-
137-
Generates a new Nostr key pair with seed phrase. This is useful when you need to provide users with a recoverable key pair.
138-
139-
**Returns:**
140-
```typescript
141-
{
142-
seedPhrase: string; // BIP39 seed phrase for key recovery
143-
nsec: string; // Private key in bech32 format
144-
npub: string; // Public key in bech32 format
145-
privateKey: string; // Private key in hex format
146-
publicKey: string; // Public key in hex format
147-
}
148-
```
149-
150-
### `nsecToSeed(nsec: string)`
151-
152-
Converts a Nostr private key to a BIP39 seed phrase.
153-
154-
**Parameters:**
155-
- `nsec`: The nsec format private key
156-
157-
**Returns:** `string` - The BIP39 seed phrase
116+
### Key Management
117+
- `generateKeyPairWithSeed()`: Generate a new key pair with seed phrase
118+
- `seedPhraseToKeyPair(seedPhrase: string)`: Convert seed phrase to key pair
119+
- `fromHex(privateKeyHex: string)`: Create key pair from hex private key
120+
- `validateSeedPhrase(seedPhrase: string)`: Validate a seed phrase
158121

159-
### `seedToNsec(seedPhrase: string)`
122+
### Format Conversion
123+
- `nsecToHex(nsec: string)`: Convert nsec to hex format
124+
- `npubToHex(npub: string)`: Convert npub to hex format
125+
- `hexToNsec(privateKeyHex: string)`: Convert hex to nsec format
126+
- `hexToNpub(publicKeyHex: string)`: Convert hex to npub format
160127

161-
Converts a BIP39 seed phrase back to a Nostr key pair.
162-
163-
**Parameters:**
164-
- `seedPhrase`: The BIP39 seed phrase
165-
166-
**Returns:** `string` - The nsec format private key
167-
168-
### `hexToNsec(hexPrivateKey: string)`
169-
170-
Converts a hex private key to nsec format.
171-
172-
**Parameters:**
173-
- `hexPrivateKey`: Hex string of the private key
174-
175-
**Returns:** `Promise<string>` - The nsec format private key
176-
177-
### `nsecToHex(nsec: string)`
178-
179-
Converts an nsec private key to hex format.
180-
181-
**Parameters:**
182-
- `nsec`: The nsec format private key
183-
184-
**Returns:** `Promise<string>` - The hex format private key
185-
186-
### `npubToHex(npub: string)`
187-
188-
Converts an npub public key to hex format.
189-
190-
**Parameters:**
191-
- `npub`: The npub format public key
192-
193-
**Returns:** `Promise<string>` - The hex format public key
194-
195-
### `hexToNpub(hexPublicKey: string)`
196-
197-
Converts a hex public key to npub format.
198-
199-
**Parameters:**
200-
- `hexPublicKey`: Hex string of the public key
201-
202-
**Returns:** `Promise<string>` - The npub format public key
203-
204-
## Security Best Practices
205-
206-
1. **Never Share Private Keys**: Keep your nsec and private key hex values secure and never share them.
207-
2. **Backup Seed Phrases**: Safely store your seed phrase in a secure location.
208-
3. **Verify Keys**: Always verify key pairs after generation or conversion.
209-
4. **Environment Variables**: Use environment variables for storing sensitive keys in production.
210-
5. **Memory Management**: Clear sensitive data from memory when no longer needed.
128+
### Signing and Verification
129+
- `signMessage(message: string, privateKey: string)`: Sign a message
130+
- `verifySignature(message: string, signature: string, publicKey: string)`: Verify a signature
131+
- `createEvent(content: string, kind: number, privateKey: string, tags?: string[][])`: Create a Nostr event
132+
- `verifyEvent(event: NostrEvent)`: Verify a Nostr event
211133

212134
## Contributing
213135

214-
We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details.
215-
216-
## Testing
217-
218-
Run the test suite:
219-
220-
```bash
221-
npm test
222-
```
136+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
223137

224138
## License
225139

226-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
227-
228-
## Support
140+
[MIT](LICENSE)
229141

230-
- Create an [Issue](https://github.com/HumanjavaEnterprises/nostr-nsec-seedphrase/issues)
231-
- Follow on [Nostr](https://snort.social/p/npub12xyl6w6aacmqa3gmmzwrr9m3u0ldx3dwqhczuascswvew9am9q4sfg99cx)
232-
- Follow on [X (Twitter)](https://x.com/vveerrgg)
142+
## Author
233143

234-
## Acknowledgments
144+
[Vergel Evans](https://github.com/vergelevans)
235145

236-
- [nostr-tools](https://github.com/nbd-wtf/nostr-tools) - For Nostr protocol utilities
237-
- [bip39](https://github.com/bitcoinjs/bip39) - For mnemonic generation
146+
---
147+
<div align="center">
148+
Made with ❤️ by <a href="https://github.com/humanjavaenterprises">Human Java Enterprises</a>
149+
</div>

mocks/mock-secp256k1.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const utils = {
2+
hmacSha256: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
3+
// Mock implementation of HMAC SHA256
4+
const result = new Uint8Array(32); // Dummy result
5+
result.fill(1); // Fill with dummy data
6+
return result;
7+
},
8+
hmacSha256Sync: (key: Uint8Array, ...messages: Uint8Array[]): Uint8Array => {
9+
// Mock implementation of synchronous HMAC SHA256
10+
const result = new Uint8Array(32); // Dummy result
11+
result.fill(1); // Fill with dummy data
12+
return result;
13+
}
14+
};

0 commit comments

Comments
 (0)