chaingate
Version:
Multi-chain cryptocurrency SDK for TypeScript — unified API for Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash, Polygon, Arbitrum, and any EVM-compatible chain. Create wallets, query balances, send transactions, and manage tokens and NFTs across UTXO
51 lines (50 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicKey = void 0;
const utils_1 = require("../../../utils");
const errors_1 = require("../../errors");
/**
* A public key. Accepts both compressed and uncompressed formats (hex or bytes).
*
* @example
* ```ts
* const pk = new PublicKey('02deadbeef...');
* console.log(pk.hex);
* ```
*/
class PublicKey {
/**
* @param source - Hex string or `Uint8Array`.
* @throws {@link InvalidPublicKeyError} if invalid.
*/
constructor(source) {
let raw;
if (source instanceof Uint8Array) {
raw = source;
}
else if ((0, utils_1.isHex)(source)) {
raw = (0, utils_1.hexToBytes)(source);
}
else {
throw new errors_1.InvalidPublicKeyError('Invalid public key format');
}
if (raw.length !== 33 && raw.length !== 65) {
throw new errors_1.InvalidPublicKeyError(`Invalid public key length: expected 33 (compressed) or 65 (uncompressed) bytes, got ${raw.length}`);
}
try {
this._data = (0, utils_1.compressPublicKey)(raw);
}
catch {
throw new errors_1.InvalidPublicKeyError('Invalid public key: not a valid secp256k1 point');
}
}
/** The compressed public key as raw bytes. */
get raw() {
return this._data;
}
/** The compressed public key as hex. */
get hex() {
return (0, utils_1.bytesToHex)(this._data);
}
}
exports.PublicKey = PublicKey;