UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

65 lines 2.46 kB
import { HDKey } from '@scure/bip32'; import { Secret } from '../Secret'; import { hexToBytes, isHex } from '../../../../InternalUtils/Utils'; import { ExtendedPublicKey } from './ExtendedPublicKey'; import { ExtendedPrivateKey } from './ExtendedPrivateKey'; import { PublicKey } from '../../PublicKey'; import { PrivateKey } from './PrivateKey'; export class SeedEncodingError extends Error { constructor(message) { super(message); if (Error.captureStackTrace) Error.captureStackTrace(this, SeedEncodingError); this.name = this.constructor.name; } } export class Seed extends Secret { seed; get raw() { return this.seed; } async getExtendedPublicKey(derivationPath) { const publicKey = derivationPath ? HDKey.fromMasterSeed(this.raw).derive(derivationPath) : HDKey.fromMasterSeed(this.raw); return new ExtendedPublicKey(publicKey.publicExtendedKey); } async getExtendedPrivateKey(derivationPath) { const privateKey = derivationPath ? HDKey.fromMasterSeed(this.raw).derive(derivationPath) : HDKey.fromMasterSeed(this.raw); return new ExtendedPrivateKey(privateKey.privateExtendedKey); } async getPublicKey(derivationPath) { const publicKey = derivationPath ? HDKey.fromMasterSeed(this.raw).derive(derivationPath) : HDKey.fromMasterSeed(this.raw); return new PublicKey(publicKey.publicKey); } async getPrivateKey(derivationPath) { const privateKey = derivationPath ? HDKey.fromMasterSeed(this.raw).derive(derivationPath) : HDKey.fromMasterSeed(this.raw); return new PrivateKey(privateKey.privateKey); } async getMasterPublicKey() { const publicKey = HDKey.fromMasterSeed(this.raw); return new ExtendedPublicKey(publicKey.publicExtendedKey); } async getMasterPrivateKey() { const privateKey = HDKey.fromMasterSeed(this.raw); return new ExtendedPrivateKey(privateKey.privateExtendedKey); } constructor(source) { super(); if (source instanceof Uint8Array) this.seed = source; else { if (isHex(source)) this.seed = hexToBytes(source); else throw new SeedEncodingError('The string supplied is deemed to be invalid'); } } } //# sourceMappingURL=Seed.js.map