multi: add p2tr tapscript key path signing capabilities

This commit is contained in:
Oliver Gugger
2022-03-18 18:37:51 +01:00
parent 78db46be7e
commit ef98f2df8a
8 changed files with 330 additions and 104 deletions

View File

@@ -363,17 +363,38 @@ func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx,
sigHashes := txscript.NewTxSigHashes(
tx, signDesc.PrevOutputFetcher,
)
leaf := txscript.TapLeaf{
LeafVersion: txscript.BaseLeafVersion,
Script: witnessScript,
}
rawSig, err := txscript.RawTxInTapscriptSignature(
tx, sigHashes, signDesc.InputIndex,
signDesc.Output.Value, signDesc.Output.PkScript,
leaf, signDesc.HashType, privKey,
)
if err != nil {
return nil, err
// Are we spending a script path or the key path? The API is
// slightly different, so we need to account for that to get the
// raw signature.
var rawSig []byte
if signDesc.TaprootKeySpend {
// This function tweaks the private key using the tap
// root key supplied as the tweak. So we pass in the
// original private key to avoid it being double
// tweaked!
rawSig, err = txscript.RawTxInTaprootSignature(
tx, sigHashes, signDesc.InputIndex,
signDesc.Output.Value, signDesc.Output.PkScript,
signDesc.WitnessScript, signDesc.HashType,
privKey,
)
if err != nil {
return nil, err
}
} else {
leaf := txscript.TapLeaf{
LeafVersion: txscript.BaseLeafVersion,
Script: witnessScript,
}
rawSig, err = txscript.RawTxInTapscriptSignature(
tx, sigHashes, signDesc.InputIndex,
signDesc.Output.Value, signDesc.Output.PkScript,
leaf, signDesc.HashType, privKey,
)
if err != nil {
return nil, err
}
}
sig, err := schnorr.ParseSignature(rawSig)