2024-04-19 05:53:45 +00:00
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
2024-10-14 01:37:19 +00:00
|
|
|
|
"context"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"strings"
|
2024-04-19 05:53:45 +00:00
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2024-10-14 01:37:19 +00:00
|
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
2024-04-19 05:53:45 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var transferCmd = &cobra.Command{
|
|
|
|
|
Use: "transfer",
|
|
|
|
|
Short: "Creates a pending transfer of coin",
|
|
|
|
|
Long: `Creates a pending transfer of coin:
|
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
transfer <ToAccount> <OfCoin>
|
2024-04-19 05:53:45 +00:00
|
|
|
|
|
|
|
|
|
ToAccount – account address, must be specified
|
|
|
|
|
OfCoin – the address of the coin to send in whole
|
|
|
|
|
`,
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-10-14 01:37:19 +00:00
|
|
|
|
if len(args) != 2 {
|
|
|
|
|
panic("invalid arguments")
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
conn, err := GetGRPCClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
2024-10-14 01:37:19 +00:00
|
|
|
|
defer conn.Close()
|
2024-04-19 05:53:45 +00:00
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
client := protobufs.NewNodeServiceClient(conn)
|
|
|
|
|
key, err := GetPrivKeyFromConfig(NodeConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-04-19 05:53:45 +00:00
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
var coinaddr *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
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
coinaddr = &protobufs.CoinRef{
|
|
|
|
|
Address: addr,
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
2024-10-14 01:37:19 +00:00
|
|
|
|
payload = append(payload, addr...)
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
2024-10-14 01:37:19 +00:00
|
|
|
|
payload = append(payload, toaddr...)
|
2024-04-19 05:53:45 +00:00
|
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
|
sig, err := key.Sign(payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub, err := key.GetPublic().Raw()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = client.SendMessage(
|
|
|
|
|
context.Background(),
|
|
|
|
|
&protobufs.TokenRequest{
|
|
|
|
|
Request: &protobufs.TokenRequest_Transfer{
|
|
|
|
|
Transfer: &protobufs.TransferCoinRequest{
|
|
|
|
|
OfCoin: coinaddr,
|
|
|
|
|
ToAccount: &protobufs.AccountRef{
|
|
|
|
|
Account: &protobufs.AccountRef_ImplicitAccount{
|
|
|
|
|
ImplicitAccount: &protobufs.ImplicitAccount{
|
|
|
|
|
Address: toaddr,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Signature: &protobufs.Ed448Signature{
|
|
|
|
|
Signature: sig,
|
|
|
|
|
PublicKey: &protobufs.Ed448PublicKey{
|
|
|
|
|
KeyValue: pub,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2024-04-19 05:53:45 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
tokenCmd.AddCommand(transferCmd)
|
|
|
|
|
}
|