github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/cmd/burrow/commands/accounts.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"time"
     7  
     8  	"github.com/hyperledger/burrow/encoding"
     9  
    10  	"github.com/hyperledger/burrow/deploy/compile"
    11  	"github.com/hyperledger/burrow/execution/evm/abi"
    12  	"github.com/hyperledger/burrow/rpc/rpcquery"
    13  	cli "github.com/jawher/mow.cli"
    14  )
    15  
    16  // Accounts lists all the accounts in a chain, alongside with any metadata like contract name and ABI
    17  func Accounts(output Output) func(cmd *cli.Cmd) {
    18  	return func(cmd *cli.Cmd) {
    19  		chainURLOpt := cmd.StringOpt("c chain", "127.0.0.1:10997", "chain to be used in IP:PORT format")
    20  		timeoutOpt := cmd.IntOpt("t timeout", 0, "Timeout in seconds")
    21  
    22  		cmd.Action = func() {
    23  			ctx, cancel := context.WithCancel(context.Background())
    24  			if *timeoutOpt != 0 {
    25  				timeout := time.Duration(*timeoutOpt) * time.Second
    26  				ctx, cancel = context.WithTimeout(context.Background(), timeout)
    27  			}
    28  			defer cancel()
    29  
    30  			conn, err := encoding.GRPCDialContext(ctx, *chainURLOpt)
    31  			if err != nil {
    32  				output.Fatalf("failed to connect: %v", err)
    33  			}
    34  
    35  			qCli := rpcquery.NewQueryClient(conn)
    36  
    37  			stream, err := qCli.ListAccounts(context.Background(), &rpcquery.ListAccountsParam{})
    38  			if err != nil {
    39  				output.Fatalf("failed to list accounts: %v", err)
    40  			}
    41  
    42  			for acc, err := stream.Recv(); err == nil; acc, err = stream.Recv() {
    43  				output.Printf("Account: %s\n  Sequence: %d",
    44  					acc.Address.String(), acc.Sequence)
    45  
    46  				publicKey := acc.GetPublicKey()
    47  				if publicKey != nil && len(publicKey.PublicKey) > 0 {
    48  					output.Printf("  Public Key: %s\n", acc.PublicKey.String())
    49  				}
    50  				if acc.WASMCode != nil && len(acc.WASMCode) > 0 {
    51  					output.Printf("  WASM Code Hash: %s", acc.CodeHash.String())
    52  				}
    53  				if acc.EVMCode != nil && len(acc.EVMCode) > 0 {
    54  					output.Printf("  EVM Code Hash: %s", acc.CodeHash.String())
    55  				}
    56  
    57  				meta, err := qCli.GetMetadata(context.Background(), &rpcquery.GetMetadataParam{Address: &acc.Address})
    58  				if err != nil {
    59  					output.Fatalf("failed to get metadata for %s: %v", acc.Address, err)
    60  				}
    61  				if meta.Metadata != "" {
    62  					var metadata compile.Metadata
    63  					err = json.Unmarshal([]byte(meta.Metadata), &metadata)
    64  					if err != nil {
    65  						output.Fatalf("failed to unmarshal metadata %s: %v", meta.Metadata, err)
    66  					}
    67  
    68  					output.Printf("  Contract Name: %s", metadata.ContractName)
    69  					output.Printf("  Source File: %s", metadata.SourceFile)
    70  					output.Printf("  Compiler version: %s", metadata.CompilerVersion)
    71  
    72  					spec, err := abi.ReadSpec(metadata.Abi)
    73  					if err != nil {
    74  						output.Fatalf("failed to unmarshall abi %s: %v", string(metadata.Abi), err)
    75  					}
    76  
    77  					if len(spec.Functions) > 0 {
    78  						output.Printf("  Functions:")
    79  						for _, f := range spec.Functions {
    80  							output.Printf("    %s", f.String())
    81  						}
    82  					}
    83  
    84  					if len(spec.EventsByID) > 0 {
    85  						output.Printf("  Events:")
    86  						for _, e := range spec.EventsByID {
    87  							output.Printf("    %s", e.String())
    88  						}
    89  					}
    90  				}
    91  
    92  				output.Printf("")
    93  			}
    94  		}
    95  	}
    96  }