@publicspace/crypto
v1.0.1
Published
Crypto library for deriving mnemonics and keypairs, signing and verifying messages, and authentication
Downloads
7
Readme
Crypto
Crypto library for deriving mnemonics and keypairs, signing and verifying messages, and authentication
Installation
Install @publicspace/crypto with npm:
npm install @publicspace/crypto
Generate Mnemonic and Keypair
Import:
import Keys from "@publicspace/crypto";
Generate Mnemonic
Generate 12-word English mnemonic:
const mnemonic = Keys.mnemonic({ words: 12, language: "english" });
Generate 18-word English mnemonic:
const mnemonic = Keys.mnemonic({ words: 18, language: "english" });
Generate 24-word English mnemonic:
const mnemonic = Keys.mnemonic({ words: 24, language: "english" });
Define Curve Type
Define curve type for keypair generation:
const type = "ed25519" || "secp256k1";
Generate Keypair
Generate keypair:
Keys.keypair({ type });
Generate Keypair from Mnemonic
CAUTION: The keypairFromMnemonic
function derives a single keypair directly from the seed (private key) without adhering to HD wallet standards typically used by most wallets. As a result, importing the same mnemonic into other wallets will likely generate different keypairs due to those wallets’ use of hierarchical deterministic paths. While the keypair generated by this function is secure, it is important to understand that it is not compatible with HD wallet-compatible systems, limiting its interoperability with standard wallets. This approach is intended to keep the package lightweight.
Generate keypair from mnemonic:
Keys.keypairFromMnemonic({ mnemonic, type });
Generate keypair from mnemonic and passphrase:
Keys.keypairFromMnemonic({ mnemonic, passphrase: "passphrase123", type });
Sign and Verify Message
Import:
import Signature from "@publicspace/crypto";
Define ed25519 or secp256k1 type:
const type = "ed25519" || "secp256k1";
Optionally create keypair:
import Keys from "@publicspace/crypto";
const keypair = Keys.keypair({ type });
Create message:
const message = "Hello, world!";
Sign ed25519 message with secret key:
const signedMessage = Signature.sign({ message, secretKey: keypair.secretKey, type });
Sign secp256k1 message with private key:
const signedMessage = Signature.sign({ message, privateKey: keypair.privateKey, type });
Verify message:
Signature.verify({ message, publicKey: keypair.publicKey, signature: signedMessage.signature, type });
Authentication
Import:
import Auth from "@publicspace/crypto";
Define token params:
import { keypair } from "@publicspace/crypto";
const type = "ed25519" || "secp256k1";
const domain = "example.com";
const keys = keypair({ type });
const statement = Auth.prepare({ domain, publicKey: keys.publicKey });
Sign ed25519 message with secret key:
import { sign } from "@publicspace/crypto";
const signature = sign({ message, secretKey: keys.secretKey, type });
Sign secp256k1 message with private key:
import { sign } from "@publicspace/crypto";
const signature = sign({ message, privateKey: keys.privateKey, type });
Generate token that never expires:
const token = Auth.token({ domain, publicKey: keypair.publicKey, statement, signature });
Generate token that expires in 24 hours:
const token = Auth.token({ domain, publicKey: keypair.publicKey, statement, signature, expires: 86400000 });
Create certificate:
Auth.certificate({ token, type });
Utilities
Number to Mnemonic Word
Import:
import { wordFromNumber } from "@publicspace/crypto";
English word from number:
const word = wordFromNumber({ number: 42, language: "english" });
Mnemonic Word to Number
Import:
import { numberFromWord } from "@publicspace/crypto";
Number from English word:
const number = numberFromWord({ word: "aim", language: "english" });
HTTP Response Status Codes
Import:
import Auth, { response } from "@publicspace/crypto";
Usage on certificate:
const certificate = Auth.certificate({ token, type });
return new Response(response({ data: certificate }), {
headers: { "Content-Type": "application/json" }
});