@bithive/bitcoin-sdk
Version:
BitHive SDK
149 lines • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Signature = exports.PublicKey = exports.H256 = void 0;
const buffer_1 = require("buffer");
/**
* 32-byte hash with reversed hex encoding.
* This is useful for Bitcoin transaction hash, block hash and merkle proof
*/
class H256 {
buffer;
constructor(buffer) {
if (buffer.byteLength !== 32) {
throw Error(`Invalid byte length of the hash. Expected: 32. Received: ${buffer.byteLength}`);
}
this.buffer = buffer_1.Buffer.from(buffer);
}
equals(hash) {
return this.buffer.equals(hash.buffer);
}
compare(hash) {
return this.buffer.compare(hash.buffer);
}
static fromBuffer(buffer) {
return new H256(buffer);
}
toBuffer() {
return buffer_1.Buffer.from(this.buffer);
}
static fromHex(s) {
const buffer = decodeHex(s, true);
return H256.fromBuffer(buffer);
}
toHex() {
return encodeHex(this.buffer, true);
}
toString() {
return this.toHex();
}
toJSON() {
return this.toHex();
}
}
exports.H256 = H256;
/**
* 33-byte compressed public key
*/
class PublicKey {
buffer;
constructor(buffer) {
if (buffer.byteLength !== 33) {
throw Error(`Invalid byte length of the public key. Expected: 33. Received: ${buffer.byteLength}`);
}
const prefix = buffer[0];
if (prefix !== 0x02 && prefix !== 0x03) {
throw Error(`Invalid prefix of the public key. Expected: 0x02, 0x03. Received: 0x${parseHex(prefix)}`);
}
this.buffer = buffer_1.Buffer.from(buffer);
}
equals(hash) {
return this.buffer.equals(hash.buffer);
}
compare(hash) {
return this.buffer.compare(hash.buffer);
}
static fromBuffer(buffer) {
return new PublicKey(buffer);
}
toBuffer() {
return buffer_1.Buffer.from(this.buffer);
}
static fromHex(s) {
const buffer = decodeHex(s);
return PublicKey.fromBuffer(buffer);
}
toHex() {
return encodeHex(this.buffer);
}
toString() {
return this.toHex();
}
toJSON() {
return this.toHex();
}
}
exports.PublicKey = PublicKey;
/**
* 65-byte signature with recovery id
*/
class Signature {
buffer;
constructor(buffer) {
if (buffer.byteLength !== 65) {
throw Error(`Invalid byte length of the signature. Expected: 65. Received: ${buffer.byteLength}`);
}
const prefix = buffer[0];
if (prefix < 27 || prefix > 34) {
throw Error(`Invalid prefix of the signature. Expected: 0x1b - 0x22. Received: 0x${parseHex(prefix)}`);
}
this.buffer = buffer_1.Buffer.from(buffer);
}
equals(hash) {
return this.buffer.equals(hash.buffer);
}
compare(hash) {
return this.buffer.compare(hash.buffer);
}
static fromBuffer(buffer) {
return new Signature(buffer);
}
toBuffer() {
return buffer_1.Buffer.from(this.buffer);
}
static fromHex(s) {
const buffer = decodeHex(s);
return Signature.fromBuffer(buffer);
}
toHex() {
return encodeHex(this.buffer);
}
toString() {
return this.toHex();
}
toJSON() {
return this.toHex();
}
}
exports.Signature = Signature;
function encodeHex(buffer, reversed = false) {
if (reversed) {
buffer = buffer.toReversed();
}
return buffer_1.Buffer.from(buffer).toString('hex');
}
function decodeHex(s, reversed = false) {
if (!/^[A-Fa-f0-9]+$/.test(s)) {
throw Error('Invalid hex string');
}
const buffer = buffer_1.Buffer.from(s, 'hex');
if (reversed) {
buffer.reverse();
}
return buffer;
}
function parseHex(n) {
const s = n.toString(16);
const length = Math.ceil(s.length / 2) * 2;
return s.padStart(length, '0');
}
//# sourceMappingURL=crypto.js.map