mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-25 08:16:46 +00:00

* Add send and sign commands to protobuf * Added Send and Sign stubs in kaspawalletd server * Implement Sign * Implemented Send * Allow Broadcast command to supply multiple transactions * No longer prompt for password in DecryptMnemonics * Rename TransactionIDs -> TxIDs to keep consistency with Broadcast * Add some comments and formatting Co-authored-by: Ori Newman <orinewman1@gmail.com>
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
|
"github.com/kaspanet/kaspad/util"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// TODO: Implement a better fee estimation mechanism
|
|
const feePerInput = 10000
|
|
|
|
func (s *server) CreateUnsignedTransactions(_ context.Context, request *pb.CreateUnsignedTransactionsRequest) (
|
|
*pb.CreateUnsignedTransactionsResponse, error) {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
unsignedTransactions, err := s.createUnsignedTransactions(request.Address, request.Amount)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &pb.CreateUnsignedTransactionsResponse{UnsignedTransactions: unsignedTransactions}, nil
|
|
}
|
|
|
|
func (s *server) createUnsignedTransactions(address string, amount uint64) ([][]byte, error) {
|
|
if !s.isSynced() {
|
|
return nil, errors.New("server is not synced")
|
|
}
|
|
|
|
err := s.refreshUTXOs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
toAddress, err := util.DecodeAddress(address, s.params.Prefix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
selectedUTXOs, changeSompi, err := s.selectUTXOs(amount, feePerInput)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
changeAddress, changeWalletAddress, err := s.changeAddress()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
unsignedTransaction, err := libkaspawallet.CreateUnsignedTransaction(s.keysFile.ExtendedPublicKeys,
|
|
s.keysFile.MinimumSignatures,
|
|
[]*libkaspawallet.Payment{{
|
|
Address: toAddress,
|
|
Amount: amount,
|
|
}, {
|
|
Address: changeAddress,
|
|
Amount: changeSompi,
|
|
}}, selectedUTXOs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
unsignedTransactions, err := s.maybeAutoCompoundTransaction(unsignedTransaction, toAddress, changeAddress, changeWalletAddress)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return unsignedTransactions, nil
|
|
}
|
|
|
|
func (s *server) selectUTXOs(spendAmount uint64, feePerInput uint64) (
|
|
selectedUTXOs []*libkaspawallet.UTXO, changeSompi uint64, err error) {
|
|
|
|
selectedUTXOs = []*libkaspawallet.UTXO{}
|
|
totalValue := uint64(0)
|
|
|
|
dagInfo, err := s.rpcClient.GetBlockDAGInfo()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
for _, utxo := range s.utxosSortedByAmount {
|
|
if !isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, s.params.BlockCoinbaseMaturity) {
|
|
continue
|
|
}
|
|
|
|
selectedUTXOs = append(selectedUTXOs, &libkaspawallet.UTXO{
|
|
Outpoint: utxo.Outpoint,
|
|
UTXOEntry: utxo.UTXOEntry,
|
|
DerivationPath: s.walletAddressPath(utxo.address),
|
|
})
|
|
totalValue += utxo.UTXOEntry.Amount()
|
|
|
|
fee := feePerInput * uint64(len(selectedUTXOs))
|
|
totalSpend := spendAmount + fee
|
|
if totalValue >= totalSpend {
|
|
break
|
|
}
|
|
}
|
|
|
|
fee := feePerInput * uint64(len(selectedUTXOs))
|
|
totalSpend := spendAmount + fee
|
|
if totalValue < totalSpend {
|
|
return nil, 0, errors.Errorf("Insufficient funds for send: %f required, while only %f available",
|
|
float64(totalSpend)/constants.SompiPerKaspa, float64(totalValue)/constants.SompiPerKaspa)
|
|
}
|
|
|
|
return selectedUTXOs, totalValue - totalSpend, nil
|
|
}
|