/
account.go
154 lines (133 loc) · 4.71 KB
/
account.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package registry
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/kaleido-io/kaleido-sdk-go/contracts/directory"
)
// Account ...
type Account struct {
Parent string `json:"parentName,omitempty"`
Name string `json:"name,omitempty"`
Value string `json:"-"`
}
// InvokeCreate create an account tied to an existing org or group
func (acct *Account) InvokeCreate(keystorePath string, signer string) error {
ks := keystore.NewKeyStore(keystorePath, keystore.StandardScryptN, keystore.StandardScryptP)
if account, err := Utils().getAccountForAddress(ks, signer); err == nil {
client := Utils().getNodeClient()
nonce, err := client.PendingNonceAt(context.Background(), account.Address)
if err != nil {
fmt.Printf("Error finding nonce.\n")
fmt.Printf("Check your .kld.yaml file for correct node endpoint.\n")
return err
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return err
}
auth := Utils().newKeyStoreTransactor(account, ks, nil) // TODO add chain id
auth.Nonce = big.NewInt(int64(nonce))
auth.GasPrice = gasPrice
auth.GasLimit = uint64(5000000)
auth.Value = big.NewInt(0)
instance, err := directory.NewDirectory(common.HexToAddress(Utils().getDirectoryAddress()), client)
if err != nil {
return err
}
parentNodeID := Utils().GenerateNodeID(acct.Parent)
var parent [32]byte
parentBytes, _ := hexutil.Decode(parentNodeID)
copy(parent[:], parentBytes)
fmt.Println("Adding account to following parent node: ", parentNodeID)
tx, err := instance.SetAccount(auth, parent, acct.Name, common.HexToAddress(acct.Value))
if err != nil {
return err
}
fmt.Println("tx sent:", tx.Hash().Hex())
fmt.Println("waiting for tx to be mined (may take a few seconds)...")
receipt, err := bind.WaitMined(context.Background(), client, tx)
if err != nil {
return err
}
fmt.Printf("tx receipt: status=%v, gas used=%v\n", receipt.Status, receipt.CumulativeGasUsed)
} else {
return err
}
return nil
}
// InvokeGet retrieve an account
func (acct *Account) InvokeGet() error {
client := Utils().getNodeClient()
instance, err := directory.NewDirectory(common.HexToAddress(Utils().getDirectoryAddress()), client)
if err != nil {
return err
}
nodeID := Utils().GenerateNodeID(acct.Parent)
var node [32]byte
nodeBytes, _ := hexutil.Decode(nodeID)
copy(node[:], nodeBytes)
var parentID [32]byte
var parentName string
var name string
var address common.Address
var versionDescr string
if acct.Name[:2] == "0x" {
acctDecoded, _ := hexutil.Decode(acct.Name)
var acctBytes [32]byte
copy(acctBytes[:], acctDecoded)
parentID, parentName, name, address, versionDescr, err = instance.GetLatestAccount(&bind.CallOpts{}, node, acctBytes)
} else {
parentID, parentName, name, address, versionDescr, err = instance.GetLatestAccountByName(&bind.CallOpts{}, node, acct.Name)
}
if err != nil {
fmt.Println("Failed to find account in registry. Smart contract kicked back the function call.")
return nil
}
if parentName == "" {
fmt.Println("Failed to find account in registry.")
return nil
}
fmt.Println("Successfully found account.")
fmt.Println("**********************************************************")
fmt.Printf("parent node = 0x%x\n", parentID)
fmt.Println("parent name =", parentName)
fmt.Println("account name =", name)
fmt.Printf("account address = 0x%x\n", address)
fmt.Println("version descr. =", versionDescr)
return nil
}
// InvokeReverseLookup retrieve an account's details using Ethereum address
func (acct *Account) InvokeReverseLookup() error {
client := Utils().getNodeClient()
instance, err := directory.NewDirectory(common.HexToAddress(Utils().getDirectoryAddress()), client)
if err != nil {
return err
}
var parentID [32]byte
var parentName string
var name string
var address common.Address
var versionDescr string
parentID, parentName, name, address, versionDescr, err = instance.AccountLookup(&bind.CallOpts{}, common.HexToAddress(acct.Value))
if err != nil {
fmt.Println("Failed to find account in registry. Smart contract kicked back the function call.")
return nil
}
if parentName == "" {
fmt.Println("Failed to find account in registry.")
return nil
}
fmt.Println("Successfully found account.")
fmt.Println("**********************************************************")
fmt.Printf("parent node = 0x%x\n", parentID)
fmt.Println("parent name =", parentName)
fmt.Println("account name =", name)
fmt.Printf("account address = 0x%x\n", address)
fmt.Println("version descr. =", versionDescr)
return nil
}