@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
128 lines • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hexToBytes = hexToBytes;
exports.bytesToHex = bytesToHex;
exports.pointToHex = pointToHex;
exports.hexToPoint = hexToPoint;
exports.signatureToHex = signatureToHex;
exports.hexToSignature = hexToSignature;
exports.adaptorSignatureToHex = adaptorSignatureToHex;
exports.hexToAdaptorSignature = hexToAdaptorSignature;
exports.sha256 = sha256;
exports.sha256Hex = sha256Hex;
exports.be32 = be32;
const secp256k1_1 = require("./crypto/secp256k1");
/**
* Converts a hex string to a Uint8Array
* @param hex - The hex string to convert
* @returns The Uint8Array representation of the hex string
*/
function hexToBytes(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
}
return bytes;
}
/**
* Converts a Uint8Array to a hex string
* @param bytes - The Uint8Array to convert
* @returns The hex string representation of the Uint8Array
*/
function bytesToHex(bytes) {
return Array.from(bytes)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
/**
* Converts a Point to a hex string
* @param point - The Point to convert
* @returns The hex string representation of the Point
*/
function pointToHex(point) {
return bytesToHex(point.toRawBytes(true));
}
/**
* Converts a hex string to a Point
* @param hex - The hex string to convert
* @returns The Point representation of the hex string
*/
function hexToPoint(hex) {
return secp256k1_1.Point.fromHex(hex);
}
/**
* Converts a Signature to a hex string representation
* @param sig - The Signature to convert
* @returns The hex string representation of the Signature
*/
function signatureToHex(sig) {
return {
R: pointToHex(sig.R),
s: sig.s.toString(16)
};
}
/**
* Converts a hex string representation to a Signature
* @param hex - The hex string representation to convert
* @returns The Signature representation of the hex string
*/
function hexToSignature(hex) {
return {
R: hexToPoint(hex.R),
s: BigInt('0x' + hex.s)
};
}
/**
* Converts an AdaptorSignature to a hex string representation
* @param sig - The AdaptorSignature to convert
* @returns The hex string representation of the AdaptorSignature
*/
function adaptorSignatureToHex(sig) {
return {
R_prime: pointToHex(sig.R_prime),
s_prime: sig.s_prime.toString(16)
};
}
/**
* Converts a hex string representation to an AdaptorSignature
* @param hex - The hex string representation to convert
* @returns The AdaptorSignature representation of the hex string
*/
function hexToAdaptorSignature(hex) {
return {
R_prime: hexToPoint(hex.R_prime),
s_prime: BigInt('0x' + hex.s_prime)
};
}
/**
* Computes the hash of a message using SHA256
* @param message - The message to hash
* @returns The hash of the message
*/
async function sha256(message) {
return secp256k1_1.utils.sha256(message);
}
/**
* Computes the hash of a message using SHA256 and returns it as a hex string
* @param message - The message to hash
* @returns The hex string representation of the hash
*/
async function sha256Hex(message) {
const hash = await sha256(hexToBytes(message));
return bytesToHex(hash);
}
/**
* Copies a bigint to a Uint8Array as big-endian
* @param dst - The Uint8Array to copy to
* @param offset - The offset to copy to
* @param n - The bigint to copy
*/
function be32(dst, offset, n) {
for (let i = 31; i >= 0; i--) {
dst[offset + i] = Number(n & 0xffn);
n >>= 8n;
}
if (n)
throw new RangeError('integer > 256 bits');
}
//# sourceMappingURL=utils.js.map