Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for from address in kaspawallet send #1964

Merged
merged 12 commits into from
Apr 27, 2022
Prev Previous commit
Next Next commit
Fixed after merge to kaspad0.12
  • Loading branch information
tmrlvi committed Apr 15, 2022
commit c6fc72339ddb4586398554771f0cde8d94b729d3
4 changes: 2 additions & 2 deletions cmd/kaspawallet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type sendConfig struct {
Password string `long:"password" short:"p" description:"Wallet password"`
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to (default: localhost:8082)"`
ToAddress string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddress string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from" required:"false"`
FromAddresses string `long:"from-addresses" short:"a" description:"Specific public address to send Kaspa from" required:"false"`
SendAmount float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)" required:"true"`
config.NetworkFlags
}
Expand All @@ -70,7 +70,7 @@ type sweepConfig struct {
type createUnsignedTransactionConfig struct {
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to (default: localhost:8082)"`
ToAddress string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddress string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from" required:"false"`
FromAddresses string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from" required:"false"`
SendAmount float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)" required:"true"`
config.NetworkFlags
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kaspawallet/create_unsigned_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strings"

"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
Expand All @@ -21,7 +22,7 @@ func createUnsignedTransaction(conf *createUnsignedTransactionConfig) error {

sendAmountSompi := uint64(conf.SendAmount * constants.SompiPerKaspa)
response, err := daemonClient.CreateUnsignedTransactions(ctx, &pb.CreateUnsignedTransactionsRequest{
From: conf.FromAddress,
From: strings.Split(conf.FromAddresses, ","),
Address: conf.ToAddress,
Amount: sendAmountSompi,
})
Expand Down
295 changes: 148 additions & 147 deletions cmd/kaspawallet/daemon/pb/kaspawalletd.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/kaspawallet/daemon/pb/kaspawalletd.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ message AddressBalances {
message CreateUnsignedTransactionsRequest {
string address = 1;
uint64 amount = 2;
string from = 3;
repeated string from = 3;
}

message CreateUnsignedTransactionsResponse {
Expand Down
36 changes: 21 additions & 15 deletions cmd/kaspawallet/daemon/server/create_unsigned_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"golang.org/x/exp/slices"

"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
Expand All @@ -18,15 +19,15 @@ func (s *server) CreateUnsignedTransactions(_ context.Context, request *pb.Creat
s.lock.Lock()
defer s.lock.Unlock()

unsignedTransactions, err := s.createUnsignedTransactions(request.Address, request.Amount)
unsignedTransactions, err := s.createUnsignedTransactions(request.Address, request.Amount, request.From)
if err != nil {
return nil, err
}

return &pb.CreateUnsignedTransactionsResponse{UnsignedTransactions: unsignedTransactions}, nil
}

func (s *server) createUnsignedTransactions(address string, amount uint64) ([][]byte, error) {
func (s *server) createUnsignedTransactions(address string, amount uint64, fromAddressesString []string) ([][]byte, error) {
if !s.isSynced() {
return nil, errors.New("server is not synced")
}
Expand All @@ -41,15 +42,16 @@ func (s *server) createUnsignedTransactions(address string, amount uint64) ([][]
return nil, err
}

var fromAddress util.Address = nil
if request.From != "" {
fromAddress, err = util.DecodeAddress(request.From, s.params.Prefix)
var fromAddresses []string
for _, from := range fromAddressesString {
decodedFrom, err := util.DecodeAddress(from, s.params.Prefix)
if err != nil {
return nil, err
}
fromAddresses = append(fromAddresses, decodedFrom.String())
}

selectedUTXOs, changeSompi, err := s.selectUTXOs(amount, feePerInput)
selectedUTXOs, changeSompi, err := s.selectUTXOs(amount, feePerInput, fromAddresses)
if err != nil {
return nil, err
}
Expand All @@ -59,15 +61,19 @@ func (s *server) createUnsignedTransactions(address string, amount uint64) ([][]
return nil, err
}

unsignedTransaction, err := libkaspawallet.CreateUnsignedTransaction(s.keysFile.ExtendedPublicKeys,
s.keysFile.MinimumSignatures,
[]*libkaspawallet.Payment{{
Address: toAddress,
Amount: amount,
}, {
payments := []*libkaspawallet.Payment{{
Address: toAddress,
Amount: amount,
}}
if changeSompi > 0 {
payments = append(payments, &libkaspawallet.Payment{
Address: changeAddress,
Amount: changeSompi,
}}, selectedUTXOs)
})
}
unsignedTransaction, err := libkaspawallet.CreateUnsignedTransaction(s.keysFile.ExtendedPublicKeys,
s.keysFile.MinimumSignatures,
payments, selectedUTXOs)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +85,7 @@ func (s *server) createUnsignedTransactions(address string, amount uint64) ([][]
return unsignedTransactions, nil
}

func (s *server) selectUTXOs(spendAmount uint64, feePerInput uint64, from util.Address) (
func (s *server) selectUTXOs(spendAmount uint64, feePerInput uint64, fromAddresses []string) (
selectedUTXOs []*libkaspawallet.UTXO, changeSompi uint64, err error) {

selectedUTXOs = []*libkaspawallet.UTXO{}
Expand All @@ -92,7 +98,7 @@ func (s *server) selectUTXOs(spendAmount uint64, feePerInput uint64, from util.A

for _, utxo := range s.utxosSortedByAmount {
addr, err := s.walletAddressString(utxo.address)
if err != nil || (from != nil && addr != from.String()) ||
if err != nil || slices.Contains(fromAddresses, addr) ||
!isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, s.params.BlockCoinbaseMaturity) {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kaspawallet/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strings"

"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
Expand Down Expand Up @@ -34,7 +35,7 @@ func send(conf *sendConfig) error {
sendAmountSompi := uint64(conf.SendAmount * constants.SompiPerKaspa)
createUnsignedTransactionsResponse, err :=
daemonClient.CreateUnsignedTransactions(ctx, &pb.CreateUnsignedTransactionsRequest{
From: conf.FromAddress,
From: strings.Split(conf.FromAddresses, ","),
Address: conf.ToAddress,
Amount: sendAmountSompi,
})
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ require (

require (
github.com/golang/snappy v0.0.1 // indirect
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd h1:zVFyTKZN/Q7mNRWSs1GOYnHM9NiFSJ54YVRsD0rNWT4=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down Expand Up @@ -118,6 +120,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea h1:+WiDlPBBaO+h9vPNZi8uJ3k4BkKQB7Iow3aqwHVA5hI=
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
Expand Down