@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
49 lines • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sighashForAdaptorSig = sighashForAdaptorSig;
exports.createCetAdaptorSig = createCetAdaptorSig;
exports.tapleafHash = tapleafHash;
const btc_1 = require("../btc");
const counterparty_1 = require("../crypto/counterparty");
function sighashForAdaptorSig(transaction, inputIndex, tapleafHash) {
return btc_1.Transaction.SighashSchnorr.sighash(transaction, btc_1.crypto.Signature.SIGHASH_ALL | btc_1.crypto.Signature.SIGHASH_ANYONECANPAY, inputIndex, btc_1.crypto.Signature.Version.TAPSCRIPT, {
annexPresent: false,
annexInit: true,
codeseparatorPosInit: true,
codeseparatorPos: new btc_1.crypto.BN(0xffffffff), // BIP-342: 0xffffffff means no codeseparator
tapleafHashInit: true,
tapleafHash
});
}
/**
* Creates and applies an adaptor signature to a CET
* @param cet - The CET transaction to sign
* @param inputIndex - The index of the input to sign
* @param privKey - The private key to sign with
* @param oracleSigPoint - The oracle's signature point for the event outcome
* @param tapleafHash - The hash of the tapleaf
* @returns The adaptor signature
*/
async function createCetAdaptorSig(cet, inputIndex, privKey, oracleSigPoint, tapleafHash) {
// Get the sighash of the CET
const sighash = sighashForAdaptorSig(cet, inputIndex, tapleafHash);
// Create the adaptor signature
const adaptorSig = await (0, counterparty_1.createAdaptorSig)(privKey, oracleSigPoint, sighash);
return adaptorSig;
}
/**
* Creates a tapleaf hash for a given script
* @param script - The script to create a tapleaf hash for
* @param leafVersion - The version of the leaf
* @returns The tapleaf hash
*/
function tapleafHash(script, leafVersion = 0xc0 // BIP-341
) {
const scriptBuf = script.toBuffer();
const tagWriter = btc_1.crypto.TaggedHash.TAPLEAF;
tagWriter.writeUInt8(leafVersion);
tagWriter.writeVarintNum(scriptBuf.length);
tagWriter.write(scriptBuf);
return tagWriter.finalize();
}
//# sourceMappingURL=sighash.js.map