UNPKG

@covenance/dlc

Version:

Crypto and Bitcoin functions for Covenance DLC implementation

70 lines (63 loc) 2.09 kB
import { Transaction, Script, crypto } from '../btc'; import { Point } from '../crypto/secp256k1'; import { createAdaptorSig } from '../crypto/counterparty'; import { PrivKey, AdaptorSignature } from '../crypto/types'; export function sighashForAdaptorSig( transaction: Transaction, inputIndex: number, tapleafHash: Buffer ): Buffer { return Transaction.SighashSchnorr.sighash( transaction, crypto.Signature.SIGHASH_ALL | crypto.Signature.SIGHASH_ANYONECANPAY, inputIndex, crypto.Signature.Version.TAPSCRIPT, { annexPresent: false, annexInit: true, codeseparatorPosInit: true, codeseparatorPos: new 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 */ export async function createCetAdaptorSig( cet: Transaction, inputIndex: number, privKey: PrivKey, oracleSigPoint: Point, tapleafHash: Buffer ): Promise<AdaptorSignature> { // Get the sighash of the CET const sighash = sighashForAdaptorSig(cet, inputIndex, tapleafHash); // Create the adaptor signature const adaptorSig = await 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 */ export function tapleafHash( script: Script, leafVersion = 0xc0 // BIP-341 ): Buffer { const scriptBuf = script.toBuffer(); const tagWriter = crypto.TaggedHash.TAPLEAF; tagWriter.writeUInt8(leafVersion); tagWriter.writeVarintNum(scriptBuf.length); tagWriter.write(scriptBuf); return tagWriter.finalize(); }