D-Stacks 57c6118be8
Adds a "sweep" command to kaspawallet (#2018)
* Add "sweep" command to kaspawallet

* minor linting & layout improvements.

* contain code in "sweep"

* update to sweep.go

* formatting and bugs

* clean up renaming of ExtractTransaction functions

* Nicer formating of display

* address reveiw.

* one more sweeped -> swept

* missed one reveiw comment. (extra mass).

* remove redundant code from split function.

Co-authored-by: Ori Newman <orinewman1@gmail.com>
2022-04-14 02:16:16 +03:00

62 lines
2.2 KiB
Go

package libkaspawallet
import (
"encoding/hex"
"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionid"
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
)
//KaspawalletdUTXOsTolibkaspawalletUTXOs converts a []*pb.UtxosByAddressesEntry to a []*libkaspawallet.UTXO
func KaspawalletdUTXOsTolibkaspawalletUTXOs(kaspawalletdUtxoEntires []*pb.UtxosByAddressesEntry) ([]*UTXO, error) {
UTXOs := make([]*UTXO, len(kaspawalletdUtxoEntires))
for i, entry := range kaspawalletdUtxoEntires {
script, err := hex.DecodeString(entry.UtxoEntry.ScriptPublicKey.ScriptPublicKey)
if err != nil {
return nil, err
}
transactionID, err := transactionid.FromString(entry.Outpoint.TransactionId)
if err != nil {
return nil, err
}
UTXOs[i] = &UTXO{
UTXOEntry: utxo.NewUTXOEntry(
entry.UtxoEntry.Amount,
&externalapi.ScriptPublicKey{
Script: script,
Version: uint16(entry.UtxoEntry.ScriptPublicKey.Version),
},
entry.UtxoEntry.IsCoinbase,
entry.UtxoEntry.BlockDaaScore,
),
Outpoint: &externalapi.DomainOutpoint{
TransactionID: *transactionID,
Index: entry.Outpoint.Index,
},
}
}
return UTXOs, nil
}
// AppMessageUTXOToKaspawalletdUTXO converts an appmessage.UTXOsByAddressesEntry to a pb.UtxosByAddressesEntry
func AppMessageUTXOToKaspawalletdUTXO(appUTXOsByAddressesEntry *appmessage.UTXOsByAddressesEntry) *pb.UtxosByAddressesEntry {
return &pb.UtxosByAddressesEntry{
Outpoint: &pb.Outpoint{
TransactionId: appUTXOsByAddressesEntry.Outpoint.TransactionID,
Index: appUTXOsByAddressesEntry.Outpoint.Index,
},
UtxoEntry: &pb.UtxoEntry{
Amount: appUTXOsByAddressesEntry.UTXOEntry.Amount,
ScriptPublicKey: &pb.ScriptPublicKey{
Version: uint32(appUTXOsByAddressesEntry.UTXOEntry.ScriptPublicKey.Version),
ScriptPublicKey: appUTXOsByAddressesEntry.UTXOEntry.ScriptPublicKey.Script,
},
BlockDaaScore: appUTXOsByAddressesEntry.UTXOEntry.BlockDAAScore,
IsCoinbase: appUTXOsByAddressesEntry.UTXOEntry.IsCoinbase,
},
}
}