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