-
@ Chris
2023-02-24 08:08:24In this tutorial, you will learn how to create a new node project with TypeScript and generate a public-private keypair using the
nostr-tools
module. This tutorial assumes that you have node and npm installed on your system. We will focus a lot on just setting up the project in this article, future tutorials will focus more on working with nostr using thenostr-tools
library. The code can be found here.Step 1: Create a New Node Project
To create a new node project, open your terminal and navigate to the directory where you want to create the project. Then run the following commands:
sh mkdir nostr-tutorial cd nostr-tutorial npm init -y
This will create a new directory called nostr-tutorial with a
package.json
file.Step 2: Install the Required Dependencies
Next, we need to install the dependencies required for this project. We will use the
nostr-tools
module to generate keys and the typescript module to compile our TypeScript code.sh npm install nostr-tools typescript --save-dev
This will install the
nostr-tools
andtypescript
modules as dev dependencies in your project.Step 3: Create a TypeScript Configuration File
We need to create a TypeScript configuration file to specify the TypeScript compiler options for our project.
sh touch tsconfig.json
In the root of your project directory, create a new file called
tsconfig.json
and add the following code:json { "compilerOptions": { "target": "es2018", "module": "commonjs", "outDir": "./dist", "strict": true, "esModuleInterop": true }, "include": [ "." ] }
This is a pretty basic TypeScript config, you can read more about all of the options here: tsconfig reference
Step 4: Create a TypeScript File
In the root of your project directory, create a new TypeScript file called
keys.ts
and add the following code:```typescript import { generatePrivateKey, getPublicKey } from 'nostr-tools';
const privateKey = generatePrivateKey(); const publicKey = getPublicKey(privateKey);
console.log('Private key:', privateKey); console.log('Public key:', publicKey); ```
We will introduce two functions here,
generatePrivateKey
andgetPublicKey
. We will first need to generate a private key and derive our public key from that value. Both values will be 32-bytes lowercase hex-encoded.Public-private key pairs are a fundamental aspect of modern cryptography and are used to provide secure communication and data exchange over the internet. In future tutorials we'll see how we can use the private key to sign events, use bech32 encoding (so we can tell them apart) and how properly secure your private key. The encodings are done according to the Schnorr signatures standard for the curve secp256k1.
Step 5: Add a Build Script
To compile our TypeScript code to JavaScript, we need to add a build script to our
package.json
file. Open thepackage.json
file in your code editor and add the following script:json { "scripts": { "build": "tsc" } }
This script runs the TypeScript compiler (tsc) to compile your TypeScript code to JavaScript.
Step 6: Build and Run the Project
Now we can build and run our code:
sh npm run build node dist/keys.js
This will compile the TypeScript code to JavaScript and run the file using node. The private and public keys will be logged to the console.
Conclusion
In this tutorial, you learned how to create a new node project with TypeScript and how to generate keys using the
nostr-tools
library. This will be the first of a series of articles explaining nostr from here I recommend the following:-
Checkout nostr.com
-
Read about Nostr Implementation Possibilites NIPs here
-
Install a browser extension like alby (also lightning wallet) or nos2x
-
Try out some clients like: blogstack.io, snort.social, notebin.org
-
You can find more clients, libraries and additional tools over at awesome nostr
-