mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 04:04:21 +01:00
* [server] descriptor-based vtxo script * [server] fix unit tests * [sdk] descriptor based vtxo * empty config check & version flag support * fix: empty config check & version flag support (#309) * fix * [sdk] several fixes * [sdk][server] several fixes * [common][sdk] add reversible VtxoScript type, use it in async payment * [common] improve parser * [common] fix reversible vtxo parser * [sdk] remove logs * fix forfeit map * remove debug log * [sdk] do not allow reversible vtxo script in case of self-transfer * remove signing pubkey * remove signer public key, craft forfeit txs client side * go work sync * fix linter errors * rename MakeForfeitTxs to BuildForfeitTxs * fix conflicts * fix tests * comment VtxoScript type * revert ROUND_INTERVAL value --------- Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com> Co-authored-by: sekulicd <sekula87@gmail.com>
86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package bitcointree
|
|
|
|
import (
|
|
"github.com/ark-network/ark/common"
|
|
"github.com/btcsuite/btcd/btcutil/psbt"
|
|
"github.com/btcsuite/btcd/txscript"
|
|
"github.com/btcsuite/btcd/wire"
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
|
)
|
|
|
|
func BuildForfeitTxs(
|
|
connectorTx *psbt.Packet,
|
|
vtxoInput *wire.OutPoint,
|
|
vtxoAmount,
|
|
connectorAmount,
|
|
feeAmount uint64,
|
|
vtxoScript []byte,
|
|
aspPubKey *secp256k1.PublicKey,
|
|
) (forfeitTxs []*psbt.Packet, err error) {
|
|
aspScript, err := common.P2TRScript(aspPubKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
connectors, prevouts := getConnectorInputs(connectorTx, int64(connectorAmount))
|
|
|
|
for i, connectorInput := range connectors {
|
|
connectorPrevout := prevouts[i]
|
|
|
|
partialTx, err := psbt.New(
|
|
[]*wire.OutPoint{connectorInput, vtxoInput},
|
|
[]*wire.TxOut{{
|
|
Value: int64(vtxoAmount) + int64(connectorAmount) - int64(feeAmount),
|
|
PkScript: aspScript,
|
|
}},
|
|
2,
|
|
0,
|
|
[]uint32{wire.MaxTxInSequenceNum, wire.MaxTxInSequenceNum},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
updater, err := psbt.NewUpdater(partialTx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := updater.AddInWitnessUtxo(connectorPrevout, 0); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := updater.AddInWitnessUtxo(&wire.TxOut{
|
|
Value: int64(vtxoAmount),
|
|
PkScript: vtxoScript,
|
|
}, 1); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := updater.AddInSighashType(txscript.SigHashDefault, 1); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
forfeitTxs = append(forfeitTxs, partialTx)
|
|
}
|
|
|
|
return forfeitTxs, nil
|
|
}
|
|
|
|
func getConnectorInputs(partialTx *psbt.Packet, connectorAmount int64) ([]*wire.OutPoint, []*wire.TxOut) {
|
|
inputs := make([]*wire.OutPoint, 0)
|
|
witnessUtxos := make([]*wire.TxOut, 0)
|
|
|
|
for i, output := range partialTx.UnsignedTx.TxOut {
|
|
if output.Value == connectorAmount {
|
|
inputs = append(inputs, &wire.OutPoint{
|
|
Hash: partialTx.UnsignedTx.TxHash(),
|
|
Index: uint32(i),
|
|
})
|
|
witnessUtxos = append(witnessUtxos, output)
|
|
}
|
|
}
|
|
|
|
return inputs, witnessUtxos
|
|
}
|