Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ The following table shows all available CLI commands organized by functionality:
| | `get_coin_records_by_hints <hints>` | Retrieve coin records by multiple hints |
| | `get_coin_records_by_names <coin_names>` | Retrieve coin records by multiple names |
| | `get_coin_records_by_parent_ids <parent_ids>` | Retrieve coin records by parent IDs |
| | `get_coin_records_by_puzzle_hash <puzzle_hash>` | Retrieve coin records by puzzle hash |
| | `get_coin_records_by_puzzle_hashes <puzzle_hashes>` | Retrieve coin records by multiple puzzle hashes |
| | `get_coin_records_by_puzzle_hash <puzzle_hash_or_address>` | Retrieve coin records by puzzle hash or address |
| | `get_coin_records_by_puzzle_hashes <puzzle_hash_or_address> ...` | Retrieve coin records by multiple puzzle hashes or addressees |
| **Block Operations** | `get_block <header_hash>` | Retrieve a full block by header hash |
| | `get_blocks <start_height> <end_height>` | Retrieve multiple blocks in a height range |
| | `get_block_record <header_hash>` | Retrieve a block record by header hash |
Expand Down
18 changes: 9 additions & 9 deletions internal/cmd/coinset/get_coin_records_by_puzzle_hash.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -20,21 +18,23 @@ func init() {
}

var getCoinRecordsByPuzzleHashCmd = &cobra.Command{
Use: "get_coin_records_by_puzzle_hash <hash>",
Use: "get_coin_records_by_puzzle_hash <puzzle_hash_or_address>",
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
_, err := convertAddressOrPuzzleHash(args[0])
if err != nil {
return err
}
return fmt.Errorf("invalid hex value specified: %s", args[0])
return nil
},
Short: "Retrieves coin records by their puzzle hash",
Long: "Retrieves coin records by their puzzle hash",
Short: "Retrieves coin records by their puzzle hash or address",
Long: "Retrieves coin records by their puzzle hash or address",
Run: func(cmd *cobra.Command, args []string) {
puzzleHash, _ := convertAddressOrPuzzleHash(args[0])
jsonData := map[string]interface{}{}
jsonData["puzzle_hash"] = formatHex(args[0])
jsonData["puzzle_hash"] = puzzleHash
if crByPuzzleHashIncludeSpentCoins {
jsonData["include_spent_coins"] = true
}
Expand Down
86 changes: 44 additions & 42 deletions internal/cmd/coinset/get_coin_records_by_puzzle_hashes.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

var (
crByPuzzleHashesIncludeSpentCoins bool
crByPuzzleHashesStart int
crByPuzzleHashesEnd int
crByPuzzleHashesIncludeSpentCoins bool
crByPuzzleHashesStart int
crByPuzzleHashesEnd int
)

func init() {
getCoinRecordsByPuzzleHashesCmd.Flags().BoolVarP(&crByPuzzleHashesIncludeSpentCoins, "include-spent-coins", "s", false, "Include spent coins")
getCoinRecordsByPuzzleHashesCmd.Flags().IntVarP(&crByPuzzleHashesStart, "start-height", "", -1, "Start height")
getCoinRecordsByPuzzleHashesCmd.Flags().IntVarP(&crByPuzzleHashesEnd, "end-height", "", -1, "End height")
rootCmd.AddCommand(getCoinRecordsByPuzzleHashesCmd)
getCoinRecordsByPuzzleHashesCmd.Flags().BoolVarP(&crByPuzzleHashesIncludeSpentCoins, "include-spent-coins", "s", false, "Include spent coins")
getCoinRecordsByPuzzleHashesCmd.Flags().IntVarP(&crByPuzzleHashesStart, "start-height", "", -1, "Start height")
getCoinRecordsByPuzzleHashesCmd.Flags().IntVarP(&crByPuzzleHashesEnd, "end-height", "", -1, "End height")
rootCmd.AddCommand(getCoinRecordsByPuzzleHashesCmd)
}

var getCoinRecordsByPuzzleHashesCmd = &cobra.Command{
Use: "get_coin_records_by_puzzle_hashes <hash> <hash> ...",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("at least one puzzle hash is required")
}
for _, name := range args {
if !isHex(name) {
return fmt.Errorf("invalid hex value specified: %s", name)
}
}
return nil
},
Short: "Retrieves coin records by their puzzle hashes",
Long: "Retrieves coin records by their puzzle hashes",
Run: func(cmd *cobra.Command, args []string) {
var puzzleHashes []string
for _, puzzleHash := range args {
puzzleHashes = append(puzzleHashes, formatHex(puzzleHash))
}
jsonData := map[string]interface{}{
"puzzle_hashes": puzzleHashes,
}
if crByPuzzleHashesIncludeSpentCoins {
jsonData["include_spent_coins"] = true
}
if crByPuzzleHashesStart != -1 {
jsonData["start_height"] = crByPuzzleHashesStart
}
if crByPuzzleHashesEnd != -1 {
jsonData["end_height"] = crByPuzzleHashesEnd
}
makeRequest("get_coin_records_by_puzzle_hashes", jsonData)
},
Use: "get_coin_records_by_puzzle_hashes <puzzle_hash_or_address> <puzzle_hash_or_address> ...",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("at least one puzzle hash or address is required")
}
for _, input := range args {
_, err := convertAddressOrPuzzleHash(input)
if err != nil {
return fmt.Errorf("invalid input '%s': %v", input, err)
}
}
return nil
},
Short: "Retrieves coin records by their puzzle hashes or addresses",
Long: "Retrieves coin records by their puzzle hashes or addresses",
Run: func(cmd *cobra.Command, args []string) {
var puzzleHashes []string
for _, input := range args {
puzzleHash, _ := convertAddressOrPuzzleHash(input)
puzzleHashes = append(puzzleHashes, puzzleHash)
}
jsonData := map[string]interface{}{
"puzzle_hashes": puzzleHashes,
}
if crByPuzzleHashesIncludeSpentCoins {
jsonData["include_spent_coins"] = true
}
if crByPuzzleHashesStart != -1 {
jsonData["start_height"] = crByPuzzleHashesStart
}
if crByPuzzleHashesEnd != -1 {
jsonData["end_height"] = crByPuzzleHashesEnd
}
makeRequest("get_coin_records_by_puzzle_hashes", jsonData)
},
}
15 changes: 15 additions & 0 deletions internal/cmd/coinset/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"

"github.com/TylerBrock/colorjson"
"github.com/chia-network/go-chia-libs/pkg/bech32m"
"github.com/chia-network/go-chia-libs/pkg/rpc"
"github.com/chia-network/go-chia-libs/pkg/rpcinterface"
"github.com/itchyny/gojq"
Expand All @@ -31,6 +32,20 @@ func formatHex(str string) string {
return "0x" + str
}

func convertAddressOrPuzzleHash(input string) (string, error) {
if isAddress(input) {
_, puzzleHashBytes, err := bech32m.DecodePuzzleHash(input)
if err != nil {
return "", fmt.Errorf("invalid address: %v", err)
}
return puzzleHashBytes.String(), nil
} else if isHex(input) {
return formatHex(input), nil
} else {
return "", fmt.Errorf("invalid input: must be either a Chia address or hex puzzle hash")
}
}

func apiHost() string {
baseUrl, err := url.Parse(apiRoot())
if err != nil {
Expand Down