-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from susumuota/fix-example-js
fix: change example ts to js.
- Loading branch information
Showing
7 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
import { nip26, getPublicKey, generatePrivateKey } from 'nostrain'; | ||
|
||
// delegator | ||
const sk1 = generatePrivateKey(); | ||
const pk1 = getPublicKey(sk1); | ||
|
||
// delegatee | ||
const sk2 = generatePrivateKey(); | ||
const pk2 = getPublicKey(sk2); | ||
|
||
// generate delegation | ||
const delegation = nip26.createDelegation(sk1, { | ||
pubkey: pk2, | ||
kind: 1, | ||
since: Math.round(Date.now() / 1000) - 1, // 1 second ago | ||
until: Math.round(Date.now() / 1000) + 60 * 60 * 24 * 30, // 30 days | ||
}); | ||
|
||
console.log({ delegation }); | ||
|
||
// the delegatee uses the delegation when building an event | ||
const event = { | ||
pubkey: pk2, | ||
kind: 1, | ||
created_at: Math.round(Date.now() / 1000), | ||
content: 'hello from a delegated key', | ||
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]] | ||
}; | ||
|
||
console.log({ tags: event.tags }); | ||
|
||
// finally any receiver of this event can check for the presence of a valid delegation tag | ||
const delegator = nip26.getDelegator(event); | ||
|
||
console.log({ delegator, pk1, success: delegator === pk1 }); // will be null if there is no delegation tag or if it is invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
import crypto from 'node:crypto'; | ||
|
||
// @ts-ignore | ||
globalThis.crypto = crypto; | ||
|
||
import { nip04, getPublicKey, generatePrivateKey } from 'nostrain'; | ||
|
||
// sender | ||
const sk1 = generatePrivateKey(); | ||
const pk1 = getPublicKey(sk1); | ||
|
||
// receiver | ||
const sk2 = generatePrivateKey(); | ||
const pk2 = getPublicKey(sk2); | ||
|
||
// on the sender side | ||
const message = 'hello'; | ||
const ciphertext = await nip04.encrypt(sk1, pk2, message); | ||
|
||
// on the receiver side | ||
const plaintext = await nip04.decrypt(sk2, pk1, ciphertext); | ||
|
||
console.log({ message, ciphertext, plaintext }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// npx tsx examples/event.ts | ||
|
||
import { | ||
validateEvent, | ||
verifySignature, | ||
signEvent, | ||
getEventHash, | ||
generatePrivateKey, | ||
getPublicKey | ||
} from 'nostrain'; | ||
|
||
const privateKey = generatePrivateKey(); | ||
|
||
const event = { | ||
kind: 1, | ||
created_at: Math.floor(Date.now() / 1000), | ||
tags: [], | ||
content: 'hello', | ||
pubkey: getPublicKey(privateKey), | ||
id: '', | ||
sig: '', | ||
}; | ||
|
||
event.id = getEventHash(event); | ||
event.sig = signEvent(event, privateKey); | ||
const validateOk = validateEvent(event); | ||
const verifyOk = verifySignature(event); | ||
|
||
console.log({ event, validateOk, verifyOk }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// npx tsx examples/keys.ts | ||
|
||
import { generatePrivateKey, getPublicKey } from 'nostrain'; | ||
|
||
const sk = generatePrivateKey(); // `sk` is a hex string | ||
const pk = getPublicKey(sk); // `pk` is a hex string | ||
|
||
console.log({ sk, pk }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
import { nip05 } from 'nostrain'; | ||
|
||
const profile = await nip05.queryProfile('jb55.com'); | ||
|
||
console.log({ profile }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-FileCopyrightText: 2023 Susumu OTA <1632335+susumuota@users.noreply.github.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
import { nip19, generatePrivateKey, getPublicKey } from 'nostrain'; | ||
|
||
{ | ||
const sk = generatePrivateKey(); | ||
const nsec = nip19.nsecEncode(sk); | ||
const { type, data } = nip19.decode(nsec); | ||
|
||
console.log({ sk, nsec, type, data }); | ||
} | ||
|
||
{ | ||
const pk = getPublicKey(generatePrivateKey()); | ||
const npub = nip19.npubEncode(pk); | ||
const { type, data } = nip19.decode(npub); | ||
console.log({ pk, npub, type, data }); | ||
} | ||
|
||
{ | ||
const pk = getPublicKey(generatePrivateKey()); | ||
const relays = [ | ||
'wss://relay.nostr.example.mydomain.example.com', | ||
'wss://nostr.banana.com', | ||
]; | ||
const nprofile = nip19.nprofileEncode({ pubkey: pk, relays }); | ||
const { type, data } = nip19.decode(nprofile); | ||
console.log({ pk, relays, nprofile, type, data }); | ||
} |