Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds destination type for chifra explore #3894

Merged
merged 6 commits into from
Oct 8, 2024
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
2 changes: 1 addition & 1 deletion docs
2 changes: 1 addition & 1 deletion sdk
8 changes: 2 additions & 6 deletions src/apps/chifra/cmd/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ const notesExplore = ``

func init() {
var capabilities caps.Capability // capabilities for chifra explore
capabilities = capabilities.Add(caps.Verbose)
capabilities = capabilities.Add(caps.Version)
capabilities = capabilities.Add(caps.Noop)
capabilities = capabilities.Add(caps.NoColor)
capabilities = capabilities.Add(caps.Chain)
capabilities = capabilities.Add(caps.File)
capabilities = capabilities.Add(caps.Default)

exploreCmd.Flags().SortFlags = false

exploreCmd.Flags().BoolVarP(&explorePkg.GetOptions().NoOpen, "no_open", "n", false, `return the URL without opening it`)
exploreCmd.Flags().BoolVarP(&explorePkg.GetOptions().Local, "local", "l", false, `open the local TrueBlocks explorer`)
exploreCmd.Flags().BoolVarP(&explorePkg.GetOptions().Google, "google", "g", false, `search google excluding popular blockchain explorers`)
exploreCmd.Flags().BoolVarP(&explorePkg.GetOptions().Dalle, "dalle", "d", false, `open the address to the DalleDress explorer (hidden)`)
Expand Down
9 changes: 5 additions & 4 deletions src/apps/chifra/internal/explore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ Arguments:
terms - one or more address, name, block, or transaction identifier

Flags:
-l, --local open the local TrueBlocks explorer
-g, --google search google excluding popular blockchain explorers
-h, --help display this help screen
-n, --no_open return the URL without opening it
-l, --local open the local TrueBlocks explorer
-g, --google search google excluding popular blockchain explorers
-h, --help display this help screen
```

Data models produced by this tool:

- none
- [destination](/data-model/other/#destination)

### Other Options

Expand Down
20 changes: 11 additions & 9 deletions src/apps/chifra/internal/explore/handle_show.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package explorePkg

import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
)

func (opts *ExploreOptions) HandleShow(rCtx *output.RenderCtx) error {
for _, url := range urls {
ret := url.getUrl(opts)
if !opts.Globals.TestMode {
logger.Info("Opening", ret)
utils.OpenBrowser(ret)
} else {
logger.Info("Not opening", ret, "in test mode")
fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
for _, dest := range opts.Destinations {
dests := dest.Resolve(opts.Globals.Chain, opts.Google, opts.Dalle, opts.Local)
for _, d := range dests {
if !opts.NoOpen && !opts.Globals.TestMode {
utils.OpenBrowser(d.Url)
}
modelChan <- &d
}
}
}

return nil
return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
}
72 changes: 7 additions & 65 deletions src/apps/chifra/internal/explore/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import (
"io"
"net/http"
"net/url"
"path"
"strings"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/caps"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/walk"
// EXISTING_CODE
Expand All @@ -30,13 +29,15 @@ import (
// ExploreOptions provides all command options for the chifra explore command.
type ExploreOptions struct {
Terms []string `json:"terms,omitempty"` // One or more address, name, block, or transaction identifier
NoOpen bool `json:"noOpen,omitempty"` // Return the URL without opening it
Local bool `json:"local,omitempty"` // Open the local TrueBlocks explorer
Google bool `json:"google,omitempty"` // Search google excluding popular blockchain explorers
Dalle bool `json:"dalle,omitempty"` // Open the address to the DalleDress explorer
Globals globals.GlobalOptions `json:"globals,omitempty"` // The global options
Conn *rpc.Connection `json:"conn,omitempty"` // The connection to the RPC server
BadFlag error `json:"badFlag,omitempty"` // An error flag if needed
// EXISTING_CODE
Destinations []types.Destination
// EXISTING_CODE
}

Expand All @@ -45,6 +46,7 @@ var defaultExploreOptions = ExploreOptions{}
// testLog is used only during testing to export the options for this test case.
func (opts *ExploreOptions) testLog() {
logger.TestLog(len(opts.Terms) > 0, "Terms: ", opts.Terms)
logger.TestLog(opts.NoOpen, "NoOpen: ", opts.NoOpen)
logger.TestLog(opts.Local, "Local: ", opts.Local)
logger.TestLog(opts.Google, "Google: ", opts.Google)
logger.TestLog(opts.Dalle, "Dalle: ", opts.Dalle)
Expand Down Expand Up @@ -78,6 +80,8 @@ func ExploreFinishParseInternal(w io.Writer, values url.Values) *ExploreOptions
s := strings.Split(val, " ") // may contain space separated items
opts.Terms = append(opts.Terms, s...)
}
case "noOpen":
opts.NoOpen = true
case "local":
opts.Local = true
case "google":
Expand Down Expand Up @@ -140,12 +144,7 @@ func GetOptions() *ExploreOptions {

func getCaps() caps.Capability {
var capabilities caps.Capability // capabilities for chifra explore
capabilities = capabilities.Add(caps.Verbose)
capabilities = capabilities.Add(caps.Version)
capabilities = capabilities.Add(caps.Noop)
capabilities = capabilities.Add(caps.NoColor)
capabilities = capabilities.Add(caps.Chain)
capabilities = capabilities.Add(caps.File)
capabilities = capabilities.Add(caps.Default)
// EXISTING_CODE
// EXISTING_CODE
return capabilities
Expand All @@ -169,61 +168,4 @@ func (opts *ExploreOptions) getCaches() (caches map[walk.CacheType]bool) {
}

// EXISTING_CODE
func (u *ExploreUrl) getUrl(opts *ExploreOptions) string {

var chain = opts.Globals.Chain

if opts.Google {
var query = "https://www.google.com/search?q=[{TERM}]"
query = strings.Replace(query, "[{TERM}]", u.term, -1)
var exclusions = []string{
"etherscan", "etherchain", "bloxy", "bitquery", "ethplorer", "tokenview", "anyblocks", "explorer",
}
for _, ex := range exclusions {
query += ("+-" + ex)
}
return query
}

if opts.Dalle {
var query = "http://192.34.63.136:8080/dalle/simple/[{TERM}]"
return strings.Replace(query, "[{TERM}]", u.term, -1)
}

if u.termType == ExploreFourByte {
var query = "https://www.4byte.directory/signatures/?bytes4_signature=[{TERM}]"
query = strings.Replace(query, "[{TERM}]", u.term, -1)
return query
}

if u.termType == ExploreEnsName {
var query = "https://app.ens.domains/name/[{TERM}]/details"
query = strings.Replace(query, "[{TERM}]", u.term, -1)
return query
}

url := config.GetChain(chain).RemoteExplorer
query := ""
switch u.termType {
case ExploreNone:
// do nothing
case ExploreTx:
query = path.Join("tx", u.term)
case ExploreBlock:
query = path.Join("block", u.term)
case ExploreAddress:
fallthrough
default:
query = path.Join("address", u.term)
}

if opts.Local {
url = config.GetChain(chain).LocalExplorer
query = strings.Replace(query, "tx/", "explorer/transactions/", -1)
query = strings.Replace(query, "block/", "explorer/blocks/", -1)
}

return url + query
}

// EXISTING_CODE
66 changes: 13 additions & 53 deletions src/apps/chifra/internal/explore/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,11 @@ import (

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
"github.com/ethereum/go-ethereum"
)

type ExploreType uint8

const (
ExploreNone ExploreType = iota
ExploreAddress
ExploreName
ExploreEnsName
ExploreTx
ExploreBlock
ExploreFourByte
)

type ExploreUrl struct {
term string
termType ExploreType
}

var urls []ExploreUrl

func (opts *ExploreOptions) validateExplore() error {
chain := opts.Globals.Chain

Expand Down Expand Up @@ -67,28 +49,24 @@ func (opts *ExploreOptions) validateExplore() error {

if base.IsValidAddress(arg) {
if strings.Contains(arg, ".eth") {
urls = append(urls, ExploreUrl{arg, ExploreEnsName})
opts.Destinations = append(opts.Destinations, types.NewDestination(arg, types.DestinationEnsName))
} else {
urls = append(urls, ExploreUrl{arg, ExploreAddress})
opts.Destinations = append(opts.Destinations, types.NewDestination(arg, types.DestinationAddress))
}
// We got a valid address, we're done checking
continue
}

// The argument is not an address, so we can't use --google
if opts.Google {
return validate.Usage("The {0} option requires {1}.", "--google", "an address term")
}

if opts.Dalle {
return validate.Usage("The {0} option requires {1}.", "--dalle", "an address term")
if opts.Google || opts.Dalle {
continue
}

valid, _ := validate.IsValidTransId(chain, []string{arg}, validate.ValidTransId)
if valid {
txHash, err := opts.idToTxHash(arg, validate.IsBlockHash)
if err == nil {
urls = append(urls, ExploreUrl{txHash, ExploreTx})
opts.Destinations = append(opts.Destinations, types.NewDestination(txHash, types.DestinationTx))
continue
}
// an error here is okay since we can't distinquish between tx hashes and block hashes...
Expand All @@ -98,7 +76,7 @@ func (opts *ExploreOptions) validateExplore() error {
if valid {
blockHash, err := opts.idToBlockHash(chain, arg, validate.IsBlockHash)
if err == nil {
urls = append(urls, ExploreUrl{blockHash.Hex(), ExploreBlock})
opts.Destinations = append(opts.Destinations, types.NewDestination(blockHash.Hex(), types.DestinationBlock))
continue
}
// An error here is not okay because we have a valid hash but it's not a valid on-chain
Expand All @@ -107,41 +85,23 @@ func (opts *ExploreOptions) validateExplore() error {
}

if validate.IsValidFourByte(arg) {
urls = append(urls, ExploreUrl{arg, ExploreFourByte})
opts.Destinations = append(opts.Destinations, types.NewDestination(arg, types.DestinationFourByte))
continue
}

return validate.Usage("The {0} option ({1}) {2}.", "term", arg, "is not valid")
}

if len(urls) == 0 {
urls = append(urls, ExploreUrl{"", ExploreNone})
if len(opts.Destinations) == 0 {
if opts.Google || opts.Dalle {
return validate.Usage("The {0} options require {1}.", "--dalle and --google", "an address term")
}
opts.Destinations = append(opts.Destinations, types.NewDestination("", types.DestinationNone))
}

return opts.Globals.Validate()
}

func (t ExploreType) String() string {
switch t {
case ExploreNone:
return "ExploreNone"
case ExploreAddress:
return "ExploreAddress"
case ExploreName:
return "ExploreName"
case ExploreEnsName:
return "ExploreEnsName"
case ExploreTx:
return "ExploreTx"
case ExploreBlock:
return "ExploreBlock"
case ExploreFourByte:
return "ExploreFourByte"
default:
return fmt.Sprintf("%d", t)
}
}

func (opts *ExploreOptions) idToBlockHash(chain, arg string, isBlockHash func(arg string) bool) (base.Hash, error) {
_ = chain // linter
if isBlockHash(arg) {
Expand Down
Loading
Loading