UNPKG

@helium/crypto

Version:

Cryptography utilities including mnemonics, keypairs and base58-check encoding

86 lines 3.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ed25519_1 = require("@noble/curves/ed25519"); const address_1 = __importStar(require("@helium/address")); const Mnemonic_1 = __importDefault(require("./Mnemonic")); const { ED25519_KEY_TYPE } = address_1.KeyTypes; const { MAINNET } = address_1.NetTypes; class Keypair { constructor(keypair, netType) { this.keypair = keypair; this.publicKey = keypair.publicKey; this.privateKey = new Uint8Array(64); this.privateKey.set(keypair.privateKey, 0); this.privateKey.set(keypair.publicKey, 32); this.keyType = keypair.keyType || 'ed25519'; this.netType = netType || MAINNET; } get address() { return new address_1.default(0, this.netType, ED25519_KEY_TYPE, this.publicKey); } static async makeRandom(netType) { const privateKey = ed25519_1.ed25519.utils.randomPrivateKey(); const publicKey = ed25519_1.ed25519.getPublicKey(privateKey); const keypair = { publicKey, privateKey, }; return new Keypair(keypair, netType); } static async fromWords(words, netType) { const mnenomic = new Mnemonic_1.default(words); const keypair = await this.fromMnemonic(mnenomic, netType); return keypair; } static async fromMnemonic(mnenomic, netType) { const entropy = mnenomic.toEntropy(); const seed = entropy.length === 16 ? Buffer.concat([entropy, entropy]) : entropy; return Keypair.fromEntropy(seed, netType); } static async fromEntropy(entropy, netType) { const entropyBuffer = Buffer.from(entropy); if (Buffer.byteLength(entropyBuffer) !== 32) throw new Error('Invalid entropy, must be 32 bytes'); const privateKey = entropyBuffer; const publicKey = ed25519_1.ed25519.getPublicKey(privateKey); const keypair = { publicKey, privateKey, }; return new Keypair(keypair, netType); } async sign(message) { const messageBuffer = Buffer.from(message); const actualPrivateKey = this.privateKey.slice(0, 32); const signature = ed25519_1.ed25519.sign(messageBuffer, actualPrivateKey); return signature; } } exports.default = Keypair; //# sourceMappingURL=Keypair.js.map