mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 04:34:19 +01:00
Co-authored-by: Marco Argentieri <tiero@users.noreply.github.com> * Add claim command * Persist pending data in sqlite repo * Remove debug log * Return pending data at interface level * Fix unlocking btc wallet after restart * Lint & Fix whitelist permissions * Fix send command for covenant * Update client/covenantless/claim.go Signed-off-by: Marco Argentieri <3596602+tiero@users.noreply.github.com> * Fix * Pay for min relay fee instead of estimating fees for redeem and unconf forfeit txs * Add support for pending payments (coventanless) * Fixes * Fixes * Improve verbosity * Fix coin selection * Fix --------- Signed-off-by: Marco Argentieri <3596602+tiero@users.noreply.github.com> Co-authored-by: louisinger <louis@vulpem.com> Co-authored-by: Marco Argentieri <tiero@users.noreply.github.com> Co-authored-by: Marco Argentieri <3596602+tiero@users.noreply.github.com>
132 lines
3.5 KiB
Go
132 lines
3.5 KiB
Go
package btcwallet
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
|
"github.com/btcsuite/btcd/btcutil/psbt"
|
|
"github.com/btcsuite/btcd/txscript"
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (s *service) signPsbt(packet *psbt.Packet) ([]uint32, error) {
|
|
// iterates over the inputs and set the default sighash flags
|
|
updater, err := psbt.NewUpdater(packet)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for idx, input := range packet.Inputs {
|
|
if input.WitnessUtxo == nil {
|
|
continue
|
|
}
|
|
|
|
script, err := txscript.ParsePkScript(input.WitnessUtxo.PkScript)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error detecting signing method, "+
|
|
"couldn't parse pkScript: %v", err)
|
|
}
|
|
|
|
sighashType := txscript.SigHashAll
|
|
|
|
if script.Class() == txscript.WitnessV1TaprootTy {
|
|
sighashType = txscript.SigHashDefault
|
|
}
|
|
|
|
if err := updater.AddInSighashType(sighashType, idx); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
tx := packet.UnsignedTx
|
|
for idx := range tx.TxIn {
|
|
in := &packet.Inputs[idx]
|
|
|
|
// skip if the input does not have a witness utxo
|
|
if in.WitnessUtxo == nil {
|
|
continue
|
|
}
|
|
|
|
// skip if already signed
|
|
if len(in.FinalScriptWitness) > 0 {
|
|
continue
|
|
}
|
|
|
|
var managedAddress waddrmgr.ManagedPubKeyAddress
|
|
var isTaproot bool
|
|
|
|
if len(in.TaprootLeafScript) > 0 && txscript.IsPayToTaproot(in.WitnessUtxo.PkScript) {
|
|
// segwit v1
|
|
isTaproot = true
|
|
managedAddress = s.aspTaprootAddr
|
|
} else {
|
|
// segwit v0
|
|
managedAddress, _, _, err = s.wallet.ScriptForOutput(in.WitnessUtxo)
|
|
if err != nil {
|
|
log.Debugf("SignPsbt: Skipping input %d, error "+
|
|
"fetching script for output: %v", idx, err)
|
|
continue
|
|
}
|
|
}
|
|
bip32Infos := derivationPathForAddress(managedAddress)
|
|
packet.Inputs[idx].Bip32Derivation = []*psbt.Bip32Derivation{bip32Infos}
|
|
|
|
if isTaproot {
|
|
leafHashes := make([][]byte, 0, len(in.TaprootLeafScript))
|
|
for _, leafScript := range in.TaprootLeafScript {
|
|
leafHash := txscript.NewBaseTapLeaf(leafScript.Script).TapHash()
|
|
leafHashes = append(leafHashes, leafHash[:])
|
|
}
|
|
|
|
xonlypubkey := schnorr.SerializePubKey(managedAddress.PubKey())
|
|
|
|
packet.Inputs[idx].TaprootBip32Derivation = []*psbt.TaprootBip32Derivation{
|
|
{
|
|
XOnlyPubKey: xonlypubkey,
|
|
MasterKeyFingerprint: bip32Infos.MasterKeyFingerprint,
|
|
Bip32Path: bip32Infos.Bip32Path,
|
|
LeafHashes: leafHashes,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO (@louisinger): shall we delete this code?
|
|
// prevOutputFetcher := wallet.PsbtPrevOutputFetcher(packet)
|
|
// sigHashes := txscript.NewTxSigHashes(tx, prevOutputFetcher)
|
|
|
|
// in := packet.Inputs[0]
|
|
|
|
// preimage, err := txscript.CalcTapscriptSignaturehash(
|
|
// sigHashes,
|
|
// txscript.SigHashType(in.SighashType),
|
|
// tx,
|
|
// 0,
|
|
// txscript.NewCannedPrevOutputFetcher(in.WitnessUtxo.PkScript, in.WitnessUtxo.Value),
|
|
// txscript.NewBaseTapLeaf(in.TaprootLeafScript[0].Script),
|
|
// )
|
|
// if err != nil {
|
|
// return nil, err
|
|
// }
|
|
|
|
return s.wallet.SignPsbt(packet)
|
|
}
|
|
|
|
func derivationPathForAddress(addr waddrmgr.ManagedPubKeyAddress) *psbt.Bip32Derivation {
|
|
keyscope, derivationInfos, _ := addr.DerivationInfo()
|
|
|
|
return &psbt.Bip32Derivation{
|
|
PubKey: addr.PubKey().SerializeCompressed(),
|
|
MasterKeyFingerprint: derivationInfos.MasterKeyFingerprint,
|
|
Bip32Path: []uint32{
|
|
keyscope.Purpose + hdkeychain.HardenedKeyStart,
|
|
keyscope.Coin + hdkeychain.HardenedKeyStart,
|
|
derivationInfos.Account,
|
|
derivationInfos.Branch,
|
|
derivationInfos.Index,
|
|
},
|
|
}
|
|
}
|