|
1 | 1 | package lnd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "crypto/sha256" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
@@ -234,6 +235,48 @@ func (s *Signer) AddPartialSignatureForPrivateKey(packet *psbt.Packet, |
234 | 235 | return nil |
235 | 236 | } |
236 | 237 |
|
| 238 | +func (s *Signer) AddTaprootSignature(packet *psbt.Packet, inputIndex int, |
| 239 | + utxo *wire.TxOut, privateKey *btcec.PrivateKey) error { |
| 240 | + |
| 241 | + pIn := &packet.Inputs[inputIndex] |
| 242 | + |
| 243 | + // Now we add our partial signature. |
| 244 | + prevOutFetcher := wallet.PsbtPrevOutputFetcher(packet) |
| 245 | + signDesc := &input.SignDescriptor{ |
| 246 | + Output: utxo, |
| 247 | + InputIndex: inputIndex, |
| 248 | + HashType: txscript.SigHashDefault, |
| 249 | + PrevOutputFetcher: prevOutFetcher, |
| 250 | + SigHashes: txscript.NewTxSigHashes( |
| 251 | + packet.UnsignedTx, prevOutFetcher, |
| 252 | + ), |
| 253 | + SignMethod: input.TaprootKeySpendBIP0086SignMethod, |
| 254 | + } |
| 255 | + |
| 256 | + if len(pIn.TaprootMerkleRoot) > 0 { |
| 257 | + signDesc.SignMethod = input.TaprootKeySpendSignMethod |
| 258 | + signDesc.TapTweak = pIn.TaprootMerkleRoot |
| 259 | + } |
| 260 | + |
| 261 | + ourSigRaw, err := s.SignOutputRawWithPrivateKey( |
| 262 | + packet.UnsignedTx, signDesc, privateKey, |
| 263 | + ) |
| 264 | + if err != nil { |
| 265 | + return fmt.Errorf("error signing with our key: %w", err) |
| 266 | + } |
| 267 | + |
| 268 | + witness := wire.TxWitness{ourSigRaw.Serialize()} |
| 269 | + var witnessBuf bytes.Buffer |
| 270 | + err = psbt.WriteTxWitness(&witnessBuf, witness) |
| 271 | + if err != nil { |
| 272 | + return fmt.Errorf("error serializing witness: %w", err) |
| 273 | + } |
| 274 | + |
| 275 | + pIn.FinalScriptWitness = witnessBuf.Bytes() |
| 276 | + |
| 277 | + return nil |
| 278 | +} |
| 279 | + |
237 | 280 | // maybeTweakPrivKey examines the single tweak parameters on the passed sign |
238 | 281 | // descriptor and may perform a mapping on the passed private key in order to |
239 | 282 | // utilize the tweaks, if populated. |
|
0 commit comments