@humanjavaenterprises/nostr-nsec-seedphrase
Version:
A comprehensive TypeScript library designed to make Nostr keys human-readable and easier to manage. It supports seedphrase generation, key conversions between nsec/npub and hex formats, and provides secure key management utilities.
197 lines (130 loc) • 5.73 kB
Markdown
[](https://badge.fury.io/js/nostr-nsec-seedphrase)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
[](https://github.com/HumanjavaEnterprises/nostr-nsec-seedphrase-library/actions)
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.
⚠️ **Important Security Notice**
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.
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.
## Features
- Generate secure BIP39 mnemonics for Nostr key pairs
- Create nsec private keys from hex format
- Convert between nsec/npub and hex representations
- TypeScript support with comprehensive type definitions
- Secure key management utilities
- Extensive testing and documentation
## Installation
```bash
npm install nostr-nsec-seedphrase
```
## Usage
### Basic Key Generation
```typescript
import { NostrSeedPhrase } from 'nostr-nsec-seedphrase';
// Generate new keys with mnemonic
const keys = await NostrSeedPhrase.generateNew();
console.log(keys);
// {
// mnemonic: "your twelve word mnemonic phrase here",
// nsec: "nsec1...",
// npub: "npub1...",
// privateKeyHex: "hex...",
// publicKeyHex: "hex..."
// }
```
```typescript
const seedPhrase = await NostrSeedPhrase.nsecToSeed('nsec1...');
console.log(seedPhrase); // "your twelve word mnemonic phrase here"
```
```typescript
const nsec = await NostrSeedPhrase.seedToNsec('your twelve word mnemonic phrase here');
console.log(nsec); // "nsec1..."
```
```typescript
const nsec = await NostrSeedPhrase.hexToNsec('your-hex-private-key');
console.log(nsec); // "nsec1..."
```
```typescript
const hex = await NostrSeedPhrase.nsecToHex('nsec1...');
console.log(hex); // "hex..."
```
```typescript
const hex = await NostrSeedPhrase.npubToHex('npub1...');
console.log(hex); // "hex..."
```
```typescript
const npub = await NostrSeedPhrase.hexToNpub('hex...');
console.log(npub); // "npub1..."
```
Generates a new Nostr key pair with mnemonic phrase.
**Returns:**
```typescript
{
mnemonic: string;
nsec: string;
npub: string;
privateKeyHex: string;
publicKeyHex: string;
}
```
Converts a Nostr private key to a BIP39 seed phrase.
**Parameters:**
- `nsec`: The nsec format private key
**Returns:** `Promise<string>` - The BIP39 seed phrase
Converts a BIP39 seed phrase back to a Nostr key pair.
**Parameters:**
- `seedPhrase`: The BIP39 seed phrase
**Returns:** `Promise<string>` - The nsec format private key
Converts a hex private key to nsec format.
**Parameters:**
- `hexPrivateKey`: Hex string of the private key
**Returns:** `Promise<string>` - The nsec format private key
Converts an nsec private key to hex format.
**Parameters:**
- `nsec`: The nsec format private key
**Returns:** `Promise<string>` - The hex format private key
Converts an npub public key to hex format.
**Parameters:**
- `npub`: The npub format public key
**Returns:** `Promise<string>` - The hex format public key
Converts a hex public key to npub format.
**Parameters:**
- `hexPublicKey`: Hex string of the public key
**Returns:** `Promise<string>` - The npub format public key
1. **Never Share Private Keys**: Keep your nsec and private key hex values secure and never share them.
2. **Backup Mnemonics**: Safely store your mnemonic phrase in a secure location.
3. **Verify Keys**: Always verify key pairs after generation or conversion.
4. **Environment Variables**: Use environment variables for storing sensitive keys in production.
5. **Memory Management**: Clear sensitive data from memory when no longer needed.
We welcome contributions! Please see our [Contributing Guidelines](CONTRIBUTING.md) for details.
Run the test suite:
```bash
npm test
```
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
- Create an [Issue](https://github.com/HumanjavaEnterprises/nostr-nsec-seedphrase/issues)
- Follow on [Nostr](https://snort.social/p/npub12xyl6w6aacmqa3gmmzwrr9m3u0ldx3dwqhczuascswvew9am9q4sfg99cx)
- Follow on [X (Twitter)](https://x.com/vveerrgg)
- [nostr-tools](https://github.com/nbd-wtf/nostr-tools) - For Nostr protocol utilities
- [bip39](https://github.com/bitcoinjs/bip39) - For mnemonic generation