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

Display transaction details and confirmation prompts for transfer and merge commands #326

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions client/cmd/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"fmt"

"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/spf13/cobra"
)

var accountCmd = &cobra.Command{
Use: "account",
Short: "Shows the account address of the managing account",
Run: func(cmd *cobra.Command, args []string) {
peerId := GetPeerIDFromConfig(NodeConfig)
addr, err := poseidon.HashBytes([]byte(peerId))
if err != nil {
panic(err)
}

addrBytes := addr.FillBytes(make([]byte, 32))
fmt.Printf("Account: 0x%x\n", addrBytes)
},
}

func init() {
tokenCmd.AddCommand(accountCmd)
}
21 changes: 19 additions & 2 deletions client/cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"context"
"encoding/hex"
"fmt"
"strings"

"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/spf13/cobra"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
Expand Down Expand Up @@ -109,6 +109,23 @@ var mergeCmd = &cobra.Command{
}
}

// Display merge details and confirmation prompt
fmt.Printf("\nMerge Transaction Details:\n")
fmt.Printf("Number of coins to merge: %d\n", len(coinaddrs))
fmt.Println("Coins to be merged:")
for i, coin := range coinaddrs {
fmt.Printf("%d. 0x%x\n", i+1, coin.Address)
}
fmt.Print("\nDo you want to proceed with this merge? (yes/no): ")

var response string
fmt.Scanln(&response)

if strings.ToLower(response) != "yes" {
fmt.Println("Merge transaction cancelled by user.")
return
}

// Create payload for merge operation
payload := []byte("merge")
for _, coinRef := range coinaddrs {
Expand Down Expand Up @@ -142,7 +159,7 @@ var mergeCmd = &cobra.Command{
panic(err)
}

println("Merge request sent successfully")
fmt.Println("Merge transaction sent successfully.")
},
}

Expand Down
81 changes: 54 additions & 27 deletions client/cmd/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"encoding/hex"
"fmt"
"strings"

"github.com/spf13/cobra"
Expand All @@ -13,15 +14,25 @@ var transferCmd = &cobra.Command{
Use: "transfer",
Short: "Creates a pending transfer of coin",
Long: `Creates a pending transfer of coin:

transfer <ToAccount> <OfCoin>

ToAccount – account address, must be specified
OfCoin – the address of the coin to send in whole
`,
ToAccount – account address, defaults to user's main account if not specified
OfCoin – the address of the coin to send in whole, defaults to user's default coin if not specified`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
panic("invalid arguments")
var toaddr, coinaddr string
var err error

// Prompt for to address if not provided
toaddr, _ = cmd.Flags().GetString("to")
if toaddr == "" {
fmt.Print("Enter recipient address: ")
fmt.Scanln(&toaddr)
}

// Prompt for coin address if not provided
coinaddr, _ = cmd.Flags().GetString("coin")
if coinaddr == "" {
fmt.Print("Enter coin address: ")
fmt.Scanln(&coinaddr)
}

conn, err := GetGRPCClient()
Expand All @@ -36,26 +47,38 @@ var transferCmd = &cobra.Command{
panic(err)
}

var coinaddr *protobufs.CoinRef
var coinRef *protobufs.CoinRef
payload := []byte("transfer")
toaddr := []byte{}
for i, arg := range args {
addrHex, _ := strings.CutPrefix(arg, "0x")
addr, err := hex.DecodeString(addrHex)
if err != nil {
panic(err)
}
if i == 0 {
toaddr = addr
continue
}

coinaddr = &protobufs.CoinRef{
Address: addr,
}
payload = append(payload, addr...)

toAddrBytes, err := hex.DecodeString(strings.TrimPrefix(toaddr, "0x"))
if err != nil {
panic(err)
}

coinAddrBytes, err := hex.DecodeString(strings.TrimPrefix(coinaddr, "0x"))
if err != nil {
panic(err)
}

coinRef = &protobufs.CoinRef{
Address: coinAddrBytes,
}
payload = append(payload, toAddrBytes...)
payload = append(payload, coinAddrBytes...)

// Display transaction details and confirmation prompt
fmt.Printf("\nTransaction Details:\n")
fmt.Printf("To Address: 0x%x\n", toAddrBytes)
fmt.Printf("Coin Address: 0x%x\n", coinAddrBytes)
fmt.Print("\nDo you want to proceed with this transaction? (yes/no): ")

var response string
fmt.Scanln(&response)

if strings.ToLower(response) != "yes" {
fmt.Println("Transaction cancelled by user.")
return
}
payload = append(payload, toaddr...)

sig, err := key.Sign(payload)
if err != nil {
Expand All @@ -72,11 +95,11 @@ var transferCmd = &cobra.Command{
&protobufs.TokenRequest{
Request: &protobufs.TokenRequest_Transfer{
Transfer: &protobufs.TransferCoinRequest{
OfCoin: coinaddr,
OfCoin: coinRef,
ToAccount: &protobufs.AccountRef{
Account: &protobufs.AccountRef_ImplicitAccount{
ImplicitAccount: &protobufs.ImplicitAccount{
Address: toaddr,
Address: toAddrBytes,
},
},
},
Expand All @@ -93,9 +116,13 @@ var transferCmd = &cobra.Command{
if err != nil {
panic(err)
}

fmt.Println("Transaction sent successfully.")
},
}

func init() {
transferCmd.Flags().StringP("to", "", "", "recipient account address")
transferCmd.Flags().StringP("coin", "", "", "coin address to transfer")
tokenCmd.AddCommand(transferCmd)
}