-
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.
create address subcommand to encode/decode address to/from puzzle hash
- Loading branch information
Showing
6 changed files
with
173 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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("testnet") | ||
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,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/bech32m" | ||
"github.com/chia-network/go-chia-libs/pkg/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var crUseTestnetPrefix bool | ||
|
||
func init() { | ||
addressDecodeCmd.Flags().BoolVarP(&crUseTestnetPrefix, "use-prefix-txch", "t", false, "use testnet prefix 'txch'") | ||
addressDecodeCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { | ||
command.Parent().HelpFunc()(command, strings) | ||
}) | ||
|
||
addressCmd.AddCommand(addressDecodeCmd) | ||
} | ||
|
||
var addressDecodeCmd = &cobra.Command{ | ||
Use: "decode <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]) == true { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid hex value specified: %s", args[0]) | ||
}, | ||
Short: "Decode address from puzzle hash", | ||
Long: `Decode address from puzzle hash`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
prefix := "xch" | ||
if crUseTestnetPrefix { | ||
prefix = "txch" | ||
} | ||
jsonData := map[string]interface{}{} | ||
jsonData["puzzle_hash"] = formatHex(args[0]) | ||
|
||
hexBytes, err := hex.DecodeString(cleanHex(args[0])) | ||
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 | ||
} | ||
jsonData["address"] = address | ||
|
||
byteResult, _ := json.Marshal(jsonData) | ||
if raw { | ||
fmt.Println(string(byteResult)) | ||
return | ||
} | ||
|
||
processJsonData(jsonData) | ||
}, | ||
} |
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,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/bech32m" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
addressEncodeCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { | ||
command.Parent().HelpFunc()(command, strings) | ||
}) | ||
|
||
addressCmd.AddCommand(addressEncodeCmd) | ||
} | ||
|
||
var addressEncodeCmd = &cobra.Command{ | ||
Use: "encode <address>", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if err := cobra.ExactArgs(1)(cmd, args); err != nil { | ||
return err | ||
} | ||
if isAddress(args[0]) == true { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid address value specified: %s", args[0]) | ||
}, | ||
Short: "Encode address to puzzle hash", | ||
Long: `Encode address to puzzle hash`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
jsonData := map[string]interface{}{} | ||
jsonData["address"] = args[0] | ||
_, puzzleHashBytes, err := bech32m.DecodePuzzleHash(args[0]) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
jsonData["puzzle_hash"] = puzzleHashBytes.String() | ||
|
||
byteResult, _ := json.Marshal(jsonData) | ||
if raw { | ||
fmt.Println(string(byteResult)) | ||
return | ||
} | ||
|
||
processJsonData(jsonData) | ||
}, | ||
} |
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