A lightweight shim for the window.nostr
interface implementing NIP-07 for Nostr-enabled web applications. This shim includes key management, event signing, and basic encryption/decryption, all in the browser using localStorage
to store the private key.
- Implements the NIP-07 interface (
getPublicKey
,signEvent
,getRelays
) - Stores the private key in
localStorage
(nostr:privkey
) - Uses secp256k1 for public/private key generation and event signing
- Basic NIP-04 encryption/decryption using shared secrets
- Works entirely in the browser without external dependencies (other than cryptography)
- Modular and extendable
You can install this shim via npm:
npm install window-nostr
Or include it directly in your HTML as a script using ES modules:
<script type="module">
import * as secp256k1 from 'https://esm.sh/noble-secp256k1'
import 'https://cdn.jsdelivr.net/npm/window-nostr'
</script>
Once included, the window.nostr
object will be available globally. Here are some examples of its usage:
window.nostr.getPublicKey().then(pubKey => {
console.log('Public Key:', pubKey)
})
const event = {
id: 'some-event-id',
content: 'Hello, Nostr!',
created_at: Math.floor(Date.now() / 1000),
kind: 1,
pubkey: 'example-pubkey',
tags: []
}
window.nostr.signEvent(event).then(signedEvent => {
console.log('Signed Event:', signedEvent)
})
window.nostr.getRelays().then(relays => {
console.log('Relays:', relays)
})
const recipientPubKey = 'recipient-public-key'
const message = 'Hello, this is a secret message.'
window.nostr.nip04.encrypt(recipientPubKey, message).then(ciphertext => {
console.log('Encrypted Message:', ciphertext)
})
const recipientPubKey = 'recipient-public-key'
const encryptedMessage = 'encrypted_message_string'
window.nostr.nip04
.decrypt(recipientPubKey, encryptedMessage)
.then(plaintext => {
console.log('Decrypted Message:', plaintext)
})
- The private key is stored in
localStorage
under the keynostr:privkey
. If no private key is found, a new one is generated automatically and stored for future use. - The public key is derived from the stored private key and is returned via
window.nostr.getPublicKey()
.
To clear or reset the private key, simply remove it from localStorage
:
localStorage.removeItem('nostr:privkey')
-
Private Key Management: The private key is securely stored in
localStorage
. If no key is found, the shim generates one using thesecp256k1
elliptic curve. -
Public Key: The public key is derived from the private key using the
getPublicKey()
method and returned in hex format. -
Event Signing: Events are serialized and signed using the private key. The signature is added to the event as
sig
. -
Relays: The
getRelays()
method returns a mock relay configuration. This can be customized to support real-world relays. -
Encryption/Decryption: The
nip04.encrypt()
andnip04.decrypt()
methods use a shared secret derived from the sender's private key and the recipient's public key to "encrypt" and "decrypt" messages (currently mocked for simplicity).
- noble-secp256k1 - A fast, secure, and pure JavaScript implementation of the secp256k1 elliptic curve, which is used for generating key pairs and signing messages.
MIT License
Feel free to contribute, submit issues, or request features! Let's build the future of decentralized communication together.