mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
make the examples on the readme use the new import method.
This commit is contained in:
36
README.md
36
README.md
@@ -19,7 +19,7 @@ If using TypeScript, this package requires TypeScript >= 5.0.
|
|||||||
### Generating a private key and a public key
|
### Generating a private key and a public key
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { generateSecretKey, getPublicKey } from 'nostr-tools'
|
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||||
|
|
||||||
let sk = generateSecretKey() // `sk` is a Uint8Array
|
let sk = generateSecretKey() // `sk` is a Uint8Array
|
||||||
let pk = getPublicKey(sk) // `pk` is a hex string
|
let pk = getPublicKey(sk) // `pk` is a hex string
|
||||||
@@ -28,7 +28,7 @@ let pk = getPublicKey(sk) // `pk` is a hex string
|
|||||||
### Creating, signing and verifying events
|
### Creating, signing and verifying events
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { finalizeEvent, verifyEvent } from 'nostr-tools'
|
import { finalizeEvent, verifyEvent } from 'nostr-tools/pure'
|
||||||
|
|
||||||
let event = finalizeEvent({
|
let event = finalizeEvent({
|
||||||
kind: 1,
|
kind: 1,
|
||||||
@@ -43,7 +43,8 @@ let isGood = verifyEvent(event)
|
|||||||
### Interacting with a relay
|
### Interacting with a relay
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { Relay, finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools'
|
import { finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||||
|
import { Relay } from 'nostr-tools/relay'
|
||||||
|
|
||||||
const relay = await Relay.connect('wss://relay.example.com')
|
const relay = await Relay.connect('wss://relay.example.com')
|
||||||
console.log(`connected to ${relay.url}`)
|
console.log(`connected to ${relay.url}`)
|
||||||
@@ -100,7 +101,7 @@ import 'websocket-polyfill'
|
|||||||
### Interacting with multiple relays
|
### Interacting with multiple relays
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { SimplePool } from 'nostr-tools'
|
import { SimplePool } from 'nostr-tools/pool'
|
||||||
|
|
||||||
const pool = new SimplePool()
|
const pool = new SimplePool()
|
||||||
|
|
||||||
@@ -136,7 +137,7 @@ let event = await pool.get(relays, {
|
|||||||
### Parsing references (mentions) from a content using NIP-10 and NIP-27
|
### Parsing references (mentions) from a content using NIP-10 and NIP-27
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { parseReferences } from 'nostr-tools'
|
import { parseReferences } from 'nostr-tools/references'
|
||||||
|
|
||||||
let references = parseReferences(event)
|
let references = parseReferences(event)
|
||||||
let simpleAugmentedContent = event.content
|
let simpleAugmentedContent = event.content
|
||||||
@@ -156,9 +157,9 @@ for (let i = 0; i < references.length; i++) {
|
|||||||
### Querying profile data from a NIP-05 address
|
### Querying profile data from a NIP-05 address
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { nip05 } from 'nostr-tools'
|
import { queryProfile } from 'nostr-tools/nip05'
|
||||||
|
|
||||||
let profile = await nip05.queryProfile('jb55.com')
|
let profile = await queryProfile('jb55.com')
|
||||||
console.log(profile.pubkey)
|
console.log(profile.pubkey)
|
||||||
// prints: 32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245
|
// prints: 32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245
|
||||||
console.log(profile.relays)
|
console.log(profile.relays)
|
||||||
@@ -168,13 +169,15 @@ console.log(profile.relays)
|
|||||||
To use this on Node.js < v18, you first must install `node-fetch@2` and call something like this:
|
To use this on Node.js < v18, you first must install `node-fetch@2` and call something like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
nip05.useFetchImplementation(require('node-fetch'))
|
import { useFetchImplementation } from 'nostr-tools/nip05'
|
||||||
|
useFetchImplementation(require('node-fetch'))
|
||||||
```
|
```
|
||||||
|
|
||||||
### Encoding and decoding NIP-19 codes
|
### Encoding and decoding NIP-19 codes
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { nip19, generateSecretKey, getPublicKey } from 'nostr-tools'
|
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'
|
||||||
|
import * as nip19 from 'nostr-tools/nip19'
|
||||||
|
|
||||||
let sk = generateSecretKey()
|
let sk = generateSecretKey()
|
||||||
let nsec = nip19.nsecEncode(sk)
|
let nsec = nip19.nsecEncode(sk)
|
||||||
@@ -197,21 +200,6 @@ assert(data.pubkey === pk)
|
|||||||
assert(data.relays.length === 2)
|
assert(data.relays.length === 2)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Import modes
|
|
||||||
|
|
||||||
### Using just the packages you want
|
|
||||||
|
|
||||||
Importing the entirety of `nostr-tools` may bloat your build, so you should probably import individual packages instead:
|
|
||||||
|
|
||||||
```js
|
|
||||||
import { generateSecretKey, finalizeEvent, verifyEvent } from 'nostr-tools/pure'
|
|
||||||
import { SimplePool } from 'nostr-tools/pool'
|
|
||||||
import { Relay, Subscription } from 'nostr-tools/relay'
|
|
||||||
import { matchFilter } from 'nostr-tools/filter'
|
|
||||||
import { decode, nprofileEncode, neventEncode, npubEncode } from 'nostr-tools/nip19'
|
|
||||||
// and so on and so forth
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using it with `nostr-wasm`
|
### Using it with `nostr-wasm`
|
||||||
|
|
||||||
[`nostr-wasm`](https://github.com/fiatjaf/nostr-wasm) is a thin wrapper over [libsecp256k1](https://github.com/bitcoin-core/secp256k1) compiled to WASM just for hashing, signing and verifying Nostr events.
|
[`nostr-wasm`](https://github.com/fiatjaf/nostr-wasm) is a thin wrapper over [libsecp256k1](https://github.com/bitcoin-core/secp256k1) compiled to WASM just for hashing, signing and verifying Nostr events.
|
||||||
|
|||||||
Reference in New Issue
Block a user