-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f5d834
commit b1a54ce
Showing
18 changed files
with
795 additions
and
216 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,37 @@ | ||
package nodecmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/multisig-labs/gogotools/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
func newValidatorsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "validators subnetID", | ||
Short: "Get current validators for a subnet (leave empty for Primary Network)", | ||
Long: ``, | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
subnetID := "11111111111111111111111111111111LpoYY" | ||
if len(args) > 0 { | ||
subnetID = args[0] | ||
} | ||
result, err := getValidators(subnetID) | ||
cobra.CheckErr(err) | ||
fmt.Println(result.String()) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func getValidators(subnetID string) (*gjson.Result, error) { | ||
uri := viper.GetString("node-url") | ||
urlP := fmt.Sprintf("%s/ext/bc/P", uri) | ||
|
||
currValdrs, err := utils.FetchRPCGJSON(urlP, "platform.getCurrentValidators", fmt.Sprintf(`{"subnetID":"%s"}`, subnetID)) | ||
return currValdrs, err | ||
} |
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,119 @@ | ||
package utilscmd | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/snow" | ||
"github.com/ava-labs/avalanchego/utils/formatting" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/block" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// TODO Need to fix this to work with avalanchego 1.11.x | ||
|
||
func newInspectBlockCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "inspectblk [block]", | ||
Short: "Decode a hex encoded block", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := genMainnetCtx() | ||
var blkBytes []byte | ||
var err error | ||
if strings.HasPrefix(args[0], "0x") { | ||
blkBytes, err = formatting.Decode(formatting.Hex, args[0]) | ||
} else { | ||
blkBytes, err = base64.StdEncoding.DecodeString(args[0]) | ||
} | ||
cobra.CheckErr(err) | ||
|
||
blk, js, err := decodeBlock(ctx, blkBytes) | ||
cobra.CheckErr(err) | ||
fmt.Printf("%s\n", js) | ||
fmt.Printf("%v\n", blk) | ||
fmt.Printf("%x\n", blk.Txs()[0].Unsigned.Bytes()) | ||
// _, _, b2, err := address.Parse("P-avax1gfpj30csekhwmf4mqkncelus5zl2ztqzvv7aww") | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("%x\n", b2) | ||
|
||
// factory := secp256k1.Factory{} | ||
// pk, err := factory.ToPrivateKey(b) | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("PrivKey: %x\n", pk) | ||
|
||
// a := pk.PublicKey().Bytes() | ||
// fmt.Printf("Serialized compressed pub key bytes: %x\n", a) | ||
|
||
// addr, err := address.FormatBech32("P-avax1", id.Bytes()) | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("Addr: %s\n", addr) | ||
|
||
// fmt.Printf("%x\n", b) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func decodeBlock(ctx *snow.Context, b []byte) (block.Block, string, error) { | ||
decoded := decodeProposerBlock(b) | ||
|
||
blk, js, err := decodeInnerBlock(ctx, decoded) | ||
if err != nil { | ||
return blk, "", err | ||
} | ||
return blk, string(js), nil | ||
} | ||
|
||
// Tries to decode as proposal block (post-Banff) if it fails just return the original bytes | ||
func decodeProposerBlock(b []byte) []byte { | ||
innerBlk, err := block.Parse(b) | ||
if err != nil { | ||
return b | ||
} | ||
return innerBlk.Block() | ||
} | ||
|
||
func decodeInnerBlock(ctx *snow.Context, b []byte) (block.Block, string, error) { | ||
res, err := block.Parse(block.GenesisCodec, b) | ||
if err != nil { | ||
return res, "", fmt.Errorf("blocks.Parse error: %w", err) | ||
} | ||
|
||
res.InitCtx(ctx) | ||
j, err := json.Marshal(res) | ||
if err != nil { | ||
return res, "", fmt.Errorf("json.Marshal error: %w", err) | ||
} | ||
return res, string(j), nil | ||
} | ||
|
||
// Simple context so that Marshal works | ||
func genMainnetCtx() *snow.Context { | ||
pChainID, _ := ids.FromString("11111111111111111111111111111111LpoYY") | ||
xChainID, _ := ids.FromString("2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM") | ||
cChainID, _ := ids.FromString("2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5") | ||
avaxAssetID, _ := ids.FromString("FvwEAhmxKfeiG8SnEvq42hc6whRyY3EFYAvebMqDNDGCgxN5Z") | ||
lookup := ids.NewAliaser() | ||
lookup.Alias(xChainID, "X") | ||
lookup.Alias(cChainID, "C") | ||
lookup.Alias(pChainID, "P") | ||
c := &snow.Context{ | ||
NetworkID: 1, | ||
SubnetID: [32]byte{}, | ||
ChainID: [32]byte{}, | ||
NodeID: [20]byte{}, | ||
XChainID: xChainID, | ||
CChainID: cChainID, | ||
AVAXAssetID: avaxAssetID, | ||
Lock: sync.RWMutex{}, | ||
BCLookup: lookup, | ||
} | ||
return c | ||
} |
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,66 @@ | ||
package utilscmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/utils/cb58" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCB58DecodeCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cb58decode [value]", | ||
Short: "Decode a CB58 encoded string", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
b, err := cb58.Decode(args[0]) | ||
cobra.CheckErr(err) | ||
fmt.Printf("%x\n", b) | ||
// _, _, b2, err := address.Parse("P-avax1gfpj30csekhwmf4mqkncelus5zl2ztqzvv7aww") | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("%x\n", b2) | ||
|
||
// factory := secp256k1.Factory{} | ||
// pk, err := factory.ToPrivateKey(b) | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("PrivKey: %x\n", pk) | ||
|
||
// a := pk.PublicKey().Bytes() | ||
// fmt.Printf("Serialized compressed pub key bytes: %x\n", a) | ||
|
||
// addr, err := address.FormatBech32("P-avax1", id.Bytes()) | ||
// cobra.CheckErr(err) | ||
// fmt.Printf("Addr: %s\n", addr) | ||
|
||
// fmt.Printf("%x\n", b) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func newCB58DecodeSigCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cb58decodesig [value]", | ||
Short: "Decode a CB58 encoded signature into r,s,v", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
b, err := cb58.Decode(args[0]) | ||
cobra.CheckErr(err) | ||
sig := struct { | ||
R string `json:"r"` | ||
S string `json:"s"` | ||
V string `json:"v"` | ||
}{} | ||
sig.R = fmt.Sprintf("0x%x", b[0:32]) | ||
sig.S = fmt.Sprintf("0x%x", b[32:64]) | ||
sig.V = fmt.Sprintf("0x%x", b[64:]) | ||
j, _ := json.Marshal(sig) | ||
fmt.Printf("%s\n", j) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
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,44 @@ | ||
package utilscmd | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newRandomCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "random", | ||
Short: "Generate random ids of various types", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := cmd.Help() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
}, | ||
} | ||
|
||
cmd.AddCommand(newRandomNodeIDCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func newRandomNodeIDCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "nodeid", | ||
Short: "Create random NodeID", | ||
Long: ``, | ||
Args: cobra.ExactArgs(0), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
r := make([]byte, 20) | ||
_, err := rand.Read(r) | ||
cobra.CheckErr(err) | ||
nodeid := ids.NodeID(r) | ||
fmt.Println(nodeid) | ||
}, | ||
} | ||
return cmd | ||
} |
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
Oops, something went wrong.