Skip to content

Commit bd964a8

Browse files
authored
feat(wallet-cmd): add Neuter command for wallet (#1683)
1 parent b46e966 commit bd964a8

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

cmd/wallet/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func main() {
6464
buildAllAddrCmd(rootCmd)
6565
buildAllHistoryCmd(rootCmd)
6666
buildInfoCmd(rootCmd)
67+
buildNeuterCmd(rootCmd)
6768

6869
err := rootCmd.Execute()
6970
if err != nil {

cmd/wallet/neuter.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ipfs/boxo/util"
7+
"github.com/pactus-project/pactus/cmd"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func buildNeuterCmd(parentCmd *cobra.Command) {
12+
neuterCmd := &cobra.Command{
13+
Use: "neuter",
14+
Short: "convert full wallet to neutered wallet and can only be used to retrieve balances or stakes",
15+
}
16+
parentCmd.AddCommand(neuterCmd)
17+
18+
neuterCmd.Run = func(_ *cobra.Command, _ []string) {
19+
wlt, err := openWallet()
20+
cmd.FatalErrorCheck(err)
21+
22+
path := wlt.Path() + ".neutered"
23+
24+
if util.FileExists(path) {
25+
cmd.FatalErrorCheck(fmt.Errorf("neutered wallet already exists, at %s", path))
26+
}
27+
28+
neuteredWallet := wlt.Neuter(path)
29+
30+
err = neuteredWallet.Save()
31+
cmd.FatalErrorCheck(err)
32+
33+
cmd.PrintSuccessMsgf("neutered wallet created at %s", path)
34+
}
35+
}

wallet/store.go

+15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ func (s *Store) ToBytes() ([]byte, error) {
4949
return json.MarshalIndent(s, " ", " ")
5050
}
5151

52+
func (s *Store) Clone() *Store {
53+
clonedVault := *s.Vault // Assuming Vault has proper pointer handling internally
54+
clonedHistory := s.History
55+
56+
return &Store{
57+
Version: s.Version,
58+
UUID: s.UUID,
59+
CreatedAt: s.CreatedAt,
60+
Network: s.Network,
61+
VaultCRC: s.VaultCRC,
62+
Vault: &clonedVault,
63+
History: clonedHistory,
64+
}
65+
}
66+
5267
func (s *Store) ValidateCRC() error {
5368
crc := s.calcVaultCRC()
5469
if s.VaultCRC != crc {

wallet/wallet.go

+13
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,16 @@ func (w *Wallet) Info() *Info {
548548
CreatedAt: w.store.CreatedAt,
549549
}
550550
}
551+
552+
// Neuter clones the wallet and neuters it and saves it at the given path.
553+
func (w *Wallet) Neuter(path string) *Wallet {
554+
clonedStore := w.store.Clone()
555+
clonedStore.Vault = w.store.Vault.Neuter()
556+
557+
neuteredWallet := &Wallet{
558+
store: clonedStore,
559+
path: path,
560+
}
561+
562+
return neuteredWallet
563+
}

0 commit comments

Comments
 (0)