Skip to content

Commit cbcaab9

Browse files
authored
Revert "Kan/sign transaction (#77)"
This reverts commit bd9ef43.
1 parent bd9ef43 commit cbcaab9

7 files changed

Lines changed: 25 additions & 288 deletions

File tree

flow/accounts/add-contract/add.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ var Cmd = &cobra.Command{
6666
},
6767
)
6868

69-
cli.PrepareAndSendTransaction(
69+
cli.SendTransaction(
7070
projectConf.HostWithOverride(conf.Host),
7171
signerAccount,
7272
tx,
73-
signerAccount.Address,
7473
conf.Results,
7574
)
7675
},

flow/accounts/create/create.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,7 @@ var Cmd = &cobra.Command{
100100

101101
tx := templates.CreateAccount(accountKeys, contracts, signerAccount.Address)
102102

103-
cli.PrepareAndSendTransaction(
104-
projectConf.HostWithOverride(conf.Host),
105-
signerAccount,
106-
tx,
107-
signerAccount.Address,
108-
conf.Results,
109-
)
103+
cli.SendTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, conf.Results)
110104
},
111105
}
112106

flow/accounts/update-contract/update.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ var Cmd = &cobra.Command{
6666
},
6767
)
6868

69-
cli.PrepareAndSendTransaction(
69+
cli.SendTransaction(
7070
projectConf.HostWithOverride(conf.Host),
7171
signerAccount,
7272
tx,
73-
signerAccount.Address,
7473
conf.Results,
7574
)
7675
},

flow/send.go

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ import (
2727
"google.golang.org/grpc"
2828
)
2929

30-
type SignerRole string
31-
32-
const (
33-
SignerRoleAuthorizer SignerRole = "authorizer"
34-
SignerRoleProposer SignerRole = "proposer"
35-
SignerRolePayer SignerRole = "payer"
36-
)
37-
38-
func PrepareTransaction(host string, signerAccount *Account, tx *flow.Transaction, payer flow.Address) *flow.Transaction {
30+
func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction, withResults bool) {
3931
ctx := context.Background()
4032

4133
flowClient, err := client.New(host, grpc.WithInsecure())
@@ -62,21 +54,13 @@ func PrepareTransaction(host string, signerAccount *Account, tx *flow.Transactio
6254

6355
tx.SetReferenceBlockID(sealed.ID).
6456
SetProposalKey(signerAddress, accountKey.Index, accountKey.SequenceNumber).
65-
SetPayer(payer)
66-
67-
return tx
68-
}
69-
70-
func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction, withResults bool) {
71-
ctx := context.Background()
57+
SetPayer(signerAddress)
7258

73-
flowClient, err := client.New(host, grpc.WithInsecure())
59+
err = tx.SignEnvelope(signerAddress, accountKey.Index, signerAccount.Signer)
7460
if err != nil {
75-
Exitf(1, "Failed to connect to host: %s", err)
61+
Exitf(1, "Failed to sign transaction: %s", err)
7662
}
7763

78-
tx = signTransaction(ctx, flowClient, signerAccount, SignerRolePayer, tx)
79-
8064
fmt.Printf("Submitting transaction with ID %s ...\n", tx.ID())
8165

8266
err = flowClient.SendTransaction(context.Background(), *tx)
@@ -93,51 +77,3 @@ func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction,
9377
printTxResult(tx, res, true)
9478
}
9579
}
96-
97-
func PrepareAndSendTransaction(host string, signerAccount *Account, tx *flow.Transaction, payer flow.Address, withResults bool) {
98-
preparedTx := PrepareTransaction(host, signerAccount, tx, payer)
99-
SendTransaction(host, signerAccount, preparedTx, withResults)
100-
}
101-
102-
func SignTransaction(host string, signerAccount *Account, signerRole SignerRole, tx *flow.Transaction) *flow.Transaction {
103-
ctx := context.Background()
104-
105-
flowClient, err := client.New(host, grpc.WithInsecure())
106-
if err != nil {
107-
Exitf(1, "Failed to connect to host: %s", err)
108-
}
109-
110-
tx = signTransaction(ctx, flowClient, signerAccount, signerRole, tx)
111-
return tx
112-
}
113-
114-
func signTransaction(
115-
ctx context.Context,
116-
flowClient *client.Client,
117-
signerAccount *Account,
118-
signerRole SignerRole,
119-
tx *flow.Transaction,
120-
) *flow.Transaction {
121-
signerAddress := signerAccount.Address
122-
account, err := flowClient.GetAccount(ctx, signerAddress)
123-
if err != nil {
124-
Exitf(1, "Failed to get account with address %s: %s", signerAddress.Hex(), err)
125-
}
126-
accountKey := account.Keys[signerAccount.KeyIndex]
127-
switch signerRole {
128-
case SignerRoleAuthorizer:
129-
err := tx.SignPayload(signerAddress, accountKey.Index, signerAccount.Signer)
130-
if err != nil {
131-
Exitf(1, "Failed to sign transaction: %s", err)
132-
}
133-
case SignerRolePayer:
134-
err := tx.SignEnvelope(signerAddress, accountKey.Index, signerAccount.Signer)
135-
if err != nil {
136-
Exitf(1, "Failed to sign transaction: %s", err)
137-
}
138-
default:
139-
Exitf(1, "Failed to sign transaction: unknown signer role %s", signerRole)
140-
}
141-
142-
return tx
143-
}

flow/transactions/send/send.go

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package send
2020

2121
import (
22-
"encoding/hex"
2322
"io/ioutil"
2423
"log"
2524
"os"
@@ -34,7 +33,6 @@ import (
3433
type Config struct {
3534
Args string `default:"" flag:"args" info:"arguments in JSON-Cadence format"`
3635
Code string `flag:"code,c" info:"path to Cadence file"`
37-
Partial string `flag:"partial-tx" info:"path to Partial Transaction file"`
3836
Host string `flag:"host" info:"Flow Access API host address"`
3937
Signer string `default:"service" flag:"signer,s"`
4038
Results bool `default:"false" flag:"results" info:"Display the results of the transaction"`
@@ -57,63 +55,39 @@ var Cmd = &cobra.Command{
5755

5856
validateKeyPreReq(signerAccount)
5957
var (
60-
tx *flow.Transaction
6158
code []byte
6259
err error
6360
)
6461

65-
if conf.Partial != "" && conf.Code != "" {
66-
cli.Exitf(1, "Both a partial transaction and Cadence code file provided, but cannot use both")
67-
} else if conf.Partial != "" {
68-
partialTxHex, err := ioutil.ReadFile(conf.Partial)
62+
if conf.Code != "" {
63+
code, err = ioutil.ReadFile(conf.Code)
6964
if err != nil {
70-
cli.Exitf(1, "Failed to read partial transaction from %s: %v", conf.Partial, err)
65+
cli.Exitf(1, "Failed to read transaction script from %s", conf.Code)
7166
}
72-
partialTxBytes, err := hex.DecodeString(string(partialTxHex))
73-
if err != nil {
74-
cli.Exitf(1, "Failed to decode partial transaction from %s: %v", conf.Partial, err)
75-
}
76-
tx, err = flow.DecodeTransaction(partialTxBytes)
67+
}
68+
69+
tx := flow.
70+
NewTransaction().
71+
SetScript(code).
72+
AddAuthorizer(signerAccount.Address)
73+
74+
// Arguments
75+
if conf.Args != "" {
76+
transactionArguments, err := cli.ParseArguments(conf.Args)
7777
if err != nil {
78-
cli.Exitf(1, "Failed to decode transaction from %s: %v", conf.Partial, err)
79-
}
80-
} else {
81-
if conf.Code != "" {
82-
code, err = ioutil.ReadFile(conf.Code)
83-
if err != nil {
84-
cli.Exitf(1, "Failed to read transaction script from %s: %v", conf.Code, err)
85-
}
78+
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
8679
}
8780

88-
tx = flow.NewTransaction().
89-
SetScript(code).
90-
AddAuthorizer(signerAccount.Address)
81+
for _, arg := range transactionArguments {
82+
err := tx.AddArgument(arg)
9183

92-
// Arguments
93-
if conf.Args != "" {
94-
transactionArguments, err := cli.ParseArguments(conf.Args)
9584
if err != nil {
96-
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
97-
}
98-
99-
for _, arg := range transactionArguments {
100-
err := tx.AddArgument(arg)
101-
102-
if err != nil {
103-
cli.Exitf(1, "Failed to add %s argument to a transaction ", conf.Code)
104-
}
85+
cli.Exitf(1, "Failed to add %s argument to a transaction ", conf.Code)
10586
}
10687
}
107-
108-
tx = cli.PrepareTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, signerAccount.Address)
10988
}
11089

111-
cli.SendTransaction(
112-
projectConf.HostWithOverride(conf.Host),
113-
signerAccount,
114-
tx,
115-
conf.Results,
116-
)
90+
cli.SendTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, conf.Results)
11791
},
11892
}
11993

flow/transactions/sign/sign.go

Lines changed: 0 additions & 163 deletions
This file was deleted.

0 commit comments

Comments
 (0)