UNPKG

@roochnetwork/rooch-sdk

Version:
141 lines (140 loc) 4.49 kB
import nacl from "tweetnacl"; import { Authenticator, encodeRoochSercetKey, isValidHardenedPath, Keypair, mnemonicToSeedHex, PRIVATE_KEY_SIZE, decodeRoochSercetKey } from "../../crypto/index.js"; import { derivePath } from "./ed25519-hd-key.js"; import { Ed25519PublicKey } from "./publickey.js"; const DEFAULT_ED25519_DERIVATION_PATH = `m/44'/784'/0'/0'/0'`; class Ed25519Keypair extends Keypair { /** * Create a new Ed25519 keypair instance. * Generate random keypair if no {@link Ed25519Keypair} is provided. * * @param keypair Ed25519 keypair */ constructor(keypair) { super(); if (keypair) { this.keypair = keypair; } else { this.keypair = nacl.sign.keyPair(); } } /** * Get the key scheme of the keypair ED25519 */ getKeyScheme() { return "ED25519"; } /** * Generate a new random Ed25519 keypair */ static generate() { return new Ed25519Keypair(nacl.sign.keyPair()); } /** * Create a Ed25519 keypair from a raw secret key byte array, also known as seed. * This is NOT the private scalar which is result of hashing and bit clamping of * the raw secret key. * * @throws error if the provided secret key is invalid and validation is not skipped. * * @param secretKey secret key byte array * @param skipValidation skip secret key validation */ static fromSecretKey(secretKey, skipValidation) { const decodeSecretKey = typeof secretKey === "string" ? (() => { const decoded = decodeRoochSercetKey(secretKey); if (decoded.schema !== "ED25519") { throw new Error("provided secretKey is invalid"); } return decoded.secretKey; })() : secretKey; const secretKeyLength = decodeSecretKey.length; if (secretKeyLength !== PRIVATE_KEY_SIZE) { throw new Error( `Wrong secretKey size. Expected ${PRIVATE_KEY_SIZE} bytes, got ${secretKeyLength}.` ); } const keypair = nacl.sign.keyPair.fromSeed(decodeSecretKey); if (!skipValidation) { const encoder = new TextEncoder(); const signData = encoder.encode("rooch validation"); const signature = nacl.sign.detached(signData, keypair.secretKey); if (!nacl.sign.detached.verify(signData, signature, keypair.publicKey)) { throw new Error("provided secretKey is invalid"); } } return new Ed25519Keypair(keypair); } getBitcoinAddress() { throw new Error("Method not implemented in Ed25519."); } getRoochAddress() { return this.getPublicKey().toAddress(); } /** * The public key for this Ed25519 keypair */ getPublicKey() { return new Ed25519PublicKey(this.keypair.publicKey); } /** * The Bech32 secret key string for this Ed25519 keypair */ getSecretKey() { return encodeRoochSercetKey( this.keypair.secretKey.slice(0, PRIVATE_KEY_SIZE), this.getKeyScheme() ); } /** * Return the signature for the provided data using Ed25519. */ async sign(input) { return nacl.sign.detached(input, this.keypair.secretKey); } signTransaction(input) { return Authenticator.rooch(input.hashData(), this); } /** * Derive Ed25519 keypair from mnemonics and path. The mnemonics must be normalized * and validated against the english wordlist. * * If path is none, it will default to m/44'/784'/0'/0'/0', otherwise the path must * be compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'. */ static deriveKeypair(mnemonics, path) { const newPath = path ?? DEFAULT_ED25519_DERIVATION_PATH; if (!isValidHardenedPath(newPath)) { throw new Error("Invalid derivation path"); } const { key } = derivePath(newPath, mnemonicToSeedHex(mnemonics)); return Ed25519Keypair.fromSecretKey(key); } /** * Derive Ed25519 keypair from mnemonicSeed and path. * * If path is none, it will default to m/44'/784'/0'/0'/0', otherwise the path must * be compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'. */ static deriveKeypairFromSeed(seedHex, path) { const newPath = path ?? DEFAULT_ED25519_DERIVATION_PATH; if (!isValidHardenedPath(newPath)) { throw new Error("Invalid derivation path"); } const { key } = derivePath(newPath, seedHex); return Ed25519Keypair.fromSecretKey(key); } } export { DEFAULT_ED25519_DERIVATION_PATH, Ed25519Keypair }; //# sourceMappingURL=keypair.js.map