-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
197 lines (166 loc) · 5.72 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
localFlow "flow-kms-signer/pkg/flow"
txUtils "flow-kms-signer/pkg/templates"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto/cloudkms"
"github.com/urfave/cli/v2"
)
type kmsConf struct {
projectID string
keyRing string
keyVersion string
key string
signerAddress string
}
type AccountInfo struct {
Account *flow.Account
AccountKey *flow.AccountKey
Signer crypto.Signer
}
func main() {
app := &cli.App{}
app.UseShortOptionHandling = true
app.Commands = []*cli.Command{
{
Name: "sasm",
Usage: "sign and send multiple cadence transactions using different arguments with the corresponding KMS key and send transaction to access node",
Flags: []cli.Flag{
&cli.StringFlag{Name: "project", Aliases: []string{"p"}, EnvVars: []string{"KMS_PROJECT"}, Required: true},
&cli.StringFlag{Name: "keyring", Aliases: []string{"kr"}, EnvVars: []string{"KMS_KEYRING"}, Required: true},
&cli.StringFlag{Name: "keyversion", Aliases: []string{"kv"}, EnvVars: []string{"KMS_KEYVERSION"}, Required: true},
&cli.StringFlag{Name: "key", Aliases: []string{"k"}, EnvVars: []string{"KMS_KEY"}, Required: true},
&cli.StringFlag{Name: "signeraddress", Aliases: []string{"s"}, EnvVars: []string{"SIGNER_ADDRESS"}, Required: true},
&cli.StringFlag{Name: "flowaccessnode", Aliases: []string{"f"}, EnvVars: []string{"FLOW_ACCESS_NODE"}, Required: true},
&cli.StringFlag{Name: "cadencefilepath", Aliases: []string{"c"}, Required: true},
&cli.StringFlag{Name: "cadencearguments", Aliases: []string{"ca"}},
},
Action: kmsSigner,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func kmsSigner(c *cli.Context) error {
ctx := context.Background()
projectID := c.String("project")
keyRing := c.String("keyring")
keyVersion := c.String("keyversion")
key := c.String("key")
signerAddress := c.String("signeraddress")
flowAccessNode := c.String("flowaccessnode")
cadenceFilePath := c.String("cadencefilepath")
if flowAccessNode == "" || projectID == "" || keyRing == "" || keyVersion == "" || key == "" || signerAddress == "" || cadenceFilePath == "" {
return errors.New("missing arguments in sign-and-send command")
}
// KMS Configuration
kmsConfig := kmsConf{
projectID: projectID,
keyRing: keyRing,
keyVersion: keyVersion,
key: key,
signerAddress: signerAddress,
}
// Flow client
flowProviderConfig := &localFlow.Config{
FlowEndPoint: flowAccessNode,
MockFlowEnabled: false,
}
flowProvider, err := localFlow.New(flowProviderConfig)
if err != nil {
panic(fmt.Errorf("failed to deploy connect to flow access node: %w", err))
}
// Get Flow account details, key index, sequence number, also KMS signer
accountInfo := getAccountInfo(ctx, flowProvider, kmsConfig)
accountAddress := accountInfo.Account.Address
accountKey := accountInfo.AccountKey
// Setup Cadence script with arguments
// Cadence arguments mapping
cadenceArguments := c.String("cadencearguments")
if cadenceArguments != "" {
cadenceArgsSplit := strings.Split(cadenceArguments, ";")
for i := 0; i < len(cadenceArgsSplit); i++ {
cadenceArgumentsMap := make(map[string]string)
cadenceArgsPerTx := strings.Split(cadenceArgsSplit[i], ",")
for j := 0; j < len(cadenceArgsPerTx); j++ {
cadenceArgumentsMap[fmt.Sprintf("Arg%d", j)] = cadenceArgsPerTx[j]
}
txScript := txUtils.ParseCadenceTemplateV2(cadenceFilePath, cadenceArgumentsMap)
latestBlock, _ := flowProvider.GetLatestBlock(context.Background(), true)
setupTx :=
flow.NewTransaction().
SetScript(txScript).
SetGasLimit(100).
SetProposalKey(
accountAddress,
accountKey.Index,
accountKey.SequenceNumber).
SetReferenceBlockID(latestBlock.ID).
SetPayer(accountAddress).
AddAuthorizer(accountAddress)
// Sign and send to Flow access node
signedTx, err := flowProvider.SignTransaction(ctx, setupTx, accountAddress, accountInfo.Signer)
if err != nil {
fmt.Printf("failed to signTransaction: %+v\n", cadenceArgumentsMap)
return err
}
// Wait for seal
result, err := SendSignedTransactionAndWaitForSeal(ctx, flowProvider, signedTx)
if err != nil {
fmt.Printf("failed to send and wait for seal: %+v\n", cadenceArgumentsMap)
return err
}
fmt.Println("==> Transaction signed and sent")
fmt.Printf("Status: %s\n", result.Status)
fmt.Printf("Events: %s\n", result.Events)
accountKey.SequenceNumber++
}
}
return nil
}
func SendSignedTransactionAndWaitForSeal(ctx context.Context, flowProvider localFlow.Provider, tx *flow.Transaction) (*flow.TransactionResult, error) {
if err := flowProvider.SendSignedTransaction(ctx, tx); err != nil {
return nil, err
}
return flowProvider.WaitTransactionSeal(ctx, tx.ID())
}
func getAccountInfo(ctx context.Context, flowProvider localFlow.Provider, kmsConfig kmsConf) *AccountInfo {
accountKMSKey := cloudkms.Key{
ProjectID: kmsConfig.projectID,
LocationID: "global",
KeyRingID: kmsConfig.keyRing,
KeyID: kmsConfig.key,
KeyVersion: kmsConfig.keyVersion,
}
kmsClient, err := cloudkms.NewClient(ctx)
if err != nil {
panic(fmt.Errorf("err create new cloud kms client %s", err))
}
addr := flow.HexToAddress(kmsConfig.signerAddress)
accountKMSSigner, err := kmsClient.SignerForKey(
ctx,
addr,
accountKMSKey,
)
if err != nil {
panic(fmt.Errorf("err create kms signer: %s", err))
}
account, err := flowProvider.GetFlowClient().GetAccount(ctx, addr)
if err != nil {
panic(fmt.Errorf("failed to get account: %s", err))
}
return &AccountInfo{
Account: account,
AccountKey: account.Keys[0],
Signer: accountKMSSigner,
}
}