Skip to content

Commit

Permalink
Add partial support for multi output transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Nov 22, 2023
1 parent 92574e6 commit f164fe9
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 366 deletions.
52 changes: 31 additions & 21 deletions cmd/kaspawallet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ type balanceConfig struct {
}

type sendConfig struct {
KeysFile string `long:"keys-file" short:"f" description:"Keys file location (default: ~/.kaspawallet/keys.json (*nix), %USERPROFILE%\\AppData\\Local\\Kaspawallet\\key.json (Windows))"`
Password string `long:"password" short:"p" description:"Wallet password"`
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to"`
ToAddress string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddresses []string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from. Use multiple times to accept several addresses" required:"false"`
SendAmount float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount)"`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
Verbose bool `long:"show-serialized" short:"s" description:"Show a list of hex encoded sent transactions"`
KeysFile string `long:"keys-file" short:"f" description:"Keys file location (default: ~/.kaspawallet/keys.json (*nix), %USERPROFILE%\\AppData\\Local\\Kaspawallet\\key.json (Windows))"`
Password string `long:"password" short:"p" description:"Wallet password"`
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to"`
ToAddress []string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddresses []string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from. Use multiple times to accept several addresses" required:"false"`
SendAmount []float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount)"`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
Verbose bool `long:"show-serialized" short:"s" description:"Show a list of hex encoded sent transactions"`
config.NetworkFlags
}

Expand All @@ -71,12 +71,12 @@ type sweepConfig struct {
}

type createUnsignedTransactionConfig struct {
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to"`
ToAddress string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddresses []string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from. Use multiple times to accept several addresses" required:"false"`
SendAmount float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount)"`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
DaemonAddress string `long:"daemonaddress" short:"d" description:"Wallet daemon server to connect to"`
ToAddress []string `long:"to-address" short:"t" description:"The public address to send Kaspa to" required:"true"`
FromAddresses []string `long:"from-address" short:"a" description:"Specific public address to send Kaspa from. Use multiple times to accept several addresses" required:"false"`
SendAmount []float64 `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount)"`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
config.NetworkFlags
}

Expand Down Expand Up @@ -296,20 +296,30 @@ func parseCommandLine() (subCommand string, config interface{}) {
}

func validateCreateUnsignedTransactionConf(conf *createUnsignedTransactionConfig) error {
if (!conf.IsSendAll && conf.SendAmount == 0) ||
(conf.IsSendAll && conf.SendAmount > 0) {
if (!conf.IsSendAll && (len(conf.SendAmount) == 0 || conf.SendAmount[0] == 0)) ||
(conf.IsSendAll && len(conf.SendAmount) != 0) {

return errors.New("exactly one of '--send-amount' or '--all' must be specified")
return errors.New("exactly one of '--send-amount' or '--send--all' must be specified")
}

if !conf.IsSendAll && len(conf.SendAmount) != len(conf.ToAddress) {
return errors.Errorf("Number of --send-amount should be identical to number of --to-address")
}

return nil
}

func validateSendConfig(conf *sendConfig) error {
if (!conf.IsSendAll && conf.SendAmount == 0) ||
(conf.IsSendAll && conf.SendAmount > 0) {
if (!conf.IsSendAll && (len(conf.SendAmount) == 0 || conf.SendAmount[0] == 0)) ||
(conf.IsSendAll && len(conf.SendAmount) != 0) {

return errors.New("exactly one of '--send-amount' or '--all' must be specified")
return errors.New("exactly one of '--send-amount' or '--send--all' must be specified")
}

if !conf.IsSendAll && len(conf.SendAmount) != len(conf.ToAddress) {
return errors.Errorf("Number of --send-amount should be identical to number of --to-address")
}

return nil
}

Expand Down
16 changes: 13 additions & 3 deletions cmd/kaspawallet/create_unsigned_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ func createUnsignedTransaction(conf *createUnsignedTransactionConfig) error {
ctx, cancel := context.WithTimeout(context.Background(), daemonTimeout)
defer cancel()

sendAmountSompi := uint64(conf.SendAmount * constants.SompiPerKaspa)
payments := make([]*pb.Payment, len(conf.ToAddress))
if conf.IsSendAll {
payments[0].Address = conf.ToAddress[0]
} else {
for i, address := range conf.ToAddress {
payments[i] = &pb.Payment{
Address: address,
Amount: uint64(conf.SendAmount[i] * constants.SompiPerKaspa),
}
}
}

response, err := daemonClient.CreateUnsignedTransactions(ctx, &pb.CreateUnsignedTransactionsRequest{
From: conf.FromAddresses,
Address: conf.ToAddress,
Amount: sendAmountSompi,
Payments: payments,
IsSendAll: conf.IsSendAll,
UseExistingChangeAddress: conf.UseExistingChangeAddress,
})
Expand Down
Loading

0 comments on commit f164fe9

Please sign in to comment.