-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
address
subcommand to encode/decode address to/from puzzle hash (…
…#2) * add wallet encode/decode commands --------- Co-authored-by: Cameron Cooper <[email protected]>
- Loading branch information
1 parent
0481ae6
commit 1ac07ed
Showing
8 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var addressCmd = &cobra.Command{ | ||
Use: "address", | ||
Short: "Encode/decode address to/from puzzle hash", | ||
Long: `Encode/decode address to/from puzzle hash.`, | ||
} | ||
|
||
func init() { | ||
addressCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { | ||
command.Flags().MarkHidden("api") | ||
command.Flags().MarkHidden("mainnet") | ||
command.Flags().MarkHidden("raw") | ||
command.Flags().MarkHidden("query") | ||
addressCmd.Parent().HelpFunc()(command, strings) | ||
}) | ||
rootCmd.AddCommand(addressCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/bech32m" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
addressDecodeCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { | ||
command.Flags().MarkHidden("testnet") | ||
command.Parent().HelpFunc()(command, strings) | ||
}) | ||
|
||
addressCmd.AddCommand(addressDecodeCmd) | ||
} | ||
|
||
var addressDecodeCmd = &cobra.Command{ | ||
Use: "decode <address>", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.ExactArgs(1)(cmd, args); err != nil { | ||
return err | ||
} | ||
if isAddress(args[0]) { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid address value specified: %s", args[0]) | ||
}, | ||
Short: "Decode address to puzzle hash", | ||
Long: `Decode address to puzzle hash`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_, puzzleHashBytes, err := bech32m.DecodePuzzleHash(args[0]) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println(puzzleHashBytes.String()) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/bech32m" | ||
"github.com/chia-network/go-chia-libs/pkg/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
addressCmd.AddCommand(addressEncodeCmd) | ||
} | ||
|
||
var addressEncodeCmd = &cobra.Command{ | ||
Use: "encode <puzzle_hash>", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.ExactArgs(1)(cmd, args); err != nil { | ||
return err | ||
} | ||
if isHex(args[0]) { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid hex value specified: %s", args[0]) | ||
}, | ||
Short: "Encode puzzle hash to address", | ||
Long: `Encode puzzle hash to address`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
prefix := "xch" | ||
if testnet { | ||
prefix = "txch" | ||
} | ||
var puzzleHash = formatHex(args[0]) | ||
hexBytes, err := hex.DecodeString(puzzleHash[2:]) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
hexBytes32, err := types.BytesToBytes32(hexBytes) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
address, err := bech32m.EncodePuzzleHash(hexBytes32, prefix) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println(address) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters