From 62a9d9197aecea12acd042a76332c70bd9c319f8 Mon Sep 17 00:00:00 2001 From: cameroncooper Date: Tue, 19 Nov 2024 10:00:38 -0600 Subject: [PATCH] added get_puzzle_and_solution_with_conditions (#11) * added get_puzzle_and_solution_with_conditions * added coin_id command * updated README --- README.md | 29 +++++++++ internal/cmd/coinset/coin_id.go | 59 +++++++++++++++++++ ...get_puzzle_and_solution_with_conditions.go | 31 ++++++++++ 3 files changed, 119 insertions(+) create mode 100644 internal/cmd/coinset/coin_id.go create mode 100644 internal/cmd/coinset/get_puzzle_and_solution_with_conditions.go diff --git a/README.md b/README.md index d054c6f..70fa9c0 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,35 @@ $ coinset get_coin_records_by_parent_ids 0xa908ee64a5821b7bda5d798c053a79c8b3d7c } ``` +### Coin IDs + +Using the `coin_id` command you can encode coin IDs. For example: + +```bash +coinset coin_id 0xeca65946d1b80b527bcab5e94673f30bb3fd8a9466b31379fa5fa1f49c492031 0x66e55285340258cb79e6eda4d16f230bec2df7a2d7b40b8c6268247be9e659cb 2000000007 +``` +```bash +0xce2a2dd052bdbcf7fffc309e2a5e1f8589513335f85232a6c8cfb4a7d49ee32b +``` + +### Address Encoding + +Using the `address` command you can encode and decode adresses. For example: + +```bash +coinset address encode 0xbf3d35bba83d984be6cc4db0d6c84922e275a39ca4f8e1dd3cddfe2fa5eb2e2f +``` +```bash +xch1hu7ntwag8kvyhekvfkcddjzfyt38tguu5nuwrhfumhlzlf0t9chs6cj5k8 +``` + +```bash +coinset address decode xch1hu7ntwag8kvyhekvfkcddjzfyt38tguu5nuwrhfumhlzlf0t9chs6cj5k8 +``` +```bash +0xbf3d35bba83d984be6cc4db0d6c84922e275a39ca4f8e1dd3cddfe2fa5eb2e2f +``` + ### JQ Filtering Using the `-q` option you can pass in a jq filter to be used on the output. For example: diff --git a/internal/cmd/coinset/coin_id.go b/internal/cmd/coinset/coin_id.go new file mode 100644 index 0000000..da54fee --- /dev/null +++ b/internal/cmd/coinset/coin_id.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "fmt" + "strconv" + + "github.com/chia-network/go-chia-libs/pkg/types" + + "github.com/spf13/cobra" +) + +var coin types.Coin + +func init() { + rootCmd.AddCommand(coinIdCmd) +} + +var coinIdCmd = &cobra.Command{ + Use: "coin_id ", + Args: func(cmd *cobra.Command, args []string) error { + if err := cobra.ExactArgs(3)(cmd, args); err != nil { + return err + } + + // Parent + if !isHex(args[0]) { + return fmt.Errorf("invalid hex value specified: %s", args[0]) + } + parent, err := types.Bytes32FromHexString(formatHex(args[0])) + if err != nil { + return fmt.Errorf("invalid hex value specified: %s", args[0]) + } + coin.ParentCoinInfo = parent + + // Puzzle Hash + if !isHex(args[1]) { + return fmt.Errorf("invalid hex value specified: %s", args[1]) + } + puzzle_hash, err := types.Bytes32FromHexString(formatHex(args[1])) + if err != nil { + return fmt.Errorf("invalid hex value specified: %s", args[1]) + } + coin.PuzzleHash = puzzle_hash + + // Amount + amount, err := strconv.ParseUint(args[2], 10, 64) + if err != nil { + return fmt.Errorf("invalid amount: %s", args[2]) + } + coin.Amount = amount + + return nil + }, + Short: "Compute a coin id from parent, puzzle and amount", + Long: `Compute a coin id from parent, puzzle and amount`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("%s\n", coin.ID().String()) + }, +} diff --git a/internal/cmd/coinset/get_puzzle_and_solution_with_conditions.go b/internal/cmd/coinset/get_puzzle_and_solution_with_conditions.go new file mode 100644 index 0000000..1c367b2 --- /dev/null +++ b/internal/cmd/coinset/get_puzzle_and_solution_with_conditions.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(getPuzzleAndSolutionWithConditionsCmd) +} + +var getPuzzleAndSolutionWithConditionsCmd = &cobra.Command{ + Use: "get_puzzle_and_solution_with_conditions ", + 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: "Retrieves a coin's spend record by its name including conditions", + Long: "Retrieves a coin's spend record by its name including conditions", + Run: func(cmd *cobra.Command, args []string) { + jsonData := map[string]interface{}{} + jsonData["coin_id"] = formatHex(args[0]) + makeRequest("get_puzzle_and_solution_with_conditions", jsonData) + }, +}