fix merge all

This commit is contained in:
Cassandra Heart 2024-10-31 23:57:06 -05:00
parent f848088c0c
commit a113d3a39e
No known key found for this signature in database
GPG Key ID: 6352152859385958

View File

@ -4,6 +4,8 @@ import (
"context"
"encoding/hex"
"strings"
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/spf13/cobra"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
)
@ -27,36 +29,72 @@ var mergeCmd = &cobra.Command{
panic(err)
}
defer conn.Close()
client := protobufs.NewNodeServiceClient(conn)
key, err := GetPrivKeyFromConfig(NodeConfig)
peerId := GetPeerIDFromConfig(NodeConfig)
privKey, err := GetPrivKeyFromConfig(NodeConfig)
if err != nil {
panic(err)
}
pub, err := privKey.GetPublic().Raw()
if err != nil {
panic(err)
}
addr, err := poseidon.HashBytes([]byte(peerId))
if err != nil {
panic(err)
}
addrBytes := addr.FillBytes(make([]byte, 32))
altAddr, err := poseidon.HashBytes([]byte(pub))
if err != nil {
panic(err)
}
altAddrBytes := altAddr.FillBytes(make([]byte, 32))
var coinaddrs []*protobufs.CoinRef
// Process for "merge all" command
if len(args) == 1 && args[0] == "all" {
// Make a new call to get all existing coins
ctx := context.Background()
response, err := client.GetCoins(ctx, &protobufs.GetCoinsRequest{})
info, err := client.GetTokensByAccount(
context.Background(),
&protobufs.GetTokensByAccountRequest{
Address: addrBytes,
},
)
if err != nil {
panic(err)
}
// Add all coins to the list
for _, coin := range info.Addresses {
coinaddrs = append(coinaddrs, &protobufs.CoinRef{
Address: coin,
})
}
info, err = client.GetTokensByAccount(
context.Background(),
&protobufs.GetTokensByAccountRequest{
Address: altAddrBytes,
},
)
if err != nil {
panic(err)
}
// Add all coins to the list
for _, coin := range info.Addresses {
coinaddrs = append(coinaddrs, &protobufs.CoinRef{
Address: coin,
})
}
// Terminate if no coins available
if len(response.Coins) == 0 {
if len(coinaddrs) == 0 {
println("No coins available to merge")
return
}
// Add all coins to the list
for _, coin := range response.Coins {
coinaddrs = append(coinaddrs, &protobufs.CoinRef{
Address: coin.Address,
})
}
} else {
// Regular coin address processing logic
for _, arg := range args {
@ -78,12 +116,7 @@ var mergeCmd = &cobra.Command{
}
// Signing process
sig, err := key.Sign(payload)
if err != nil {
panic(err)
}
pub, err := key.GetPublic().Raw()
sig, err := privKey.Sign(payload)
if err != nil {
panic(err)
}