chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
46 lines • 1.57 kB
JavaScript
import { secp256k1 } from '@noble/curves/secp256k1';
import * as wif from 'wif';
import { Secret } from '../Secret';
import { PublicKey } from '../../PublicKey';
import { hexToBytes, isBase58, isHex } from '../../../../InternalUtils/Utils';
export class PrivateKeyEncodingError extends Error {
constructor(message) {
super(message);
if (Error.captureStackTrace)
Error.captureStackTrace(this, PrivateKeyEncodingError);
this.name = this.constructor.name;
}
}
export class PrivateKey extends Secret {
privateKey;
get wif() {
return wif.encodeRaw(128, Buffer.from(this.raw), true); //128 is bitcoin mainnet
}
get publicKey() {
return new PublicKey(secp256k1.getPublicKey(this.raw, true));
}
constructor(source) {
super();
if (source instanceof Uint8Array)
this.privateKey = source;
else {
if (isHex(source)) {
this.privateKey = hexToBytes(source);
}
else if (isBase58(source)) {
try {
this.privateKey = new Uint8Array(wif.decode(source).privateKey);
}
catch (_ex) {
throw new PrivateKeyEncodingError('The string supplied in Wallet Import Format (WIF) is deemed to be invalid');
}
}
else
throw new PrivateKeyEncodingError('Invalid private key');
}
}
get raw() {
return this.privateKey;
}
}
//# sourceMappingURL=PrivateKey.js.map