Skip to content

Commit

Permalink
Merge pull request #119 from onflow/revert-77-kan/sign-transaction
Browse files Browse the repository at this point in the history
Revert "Kan/sign transaction"
  • Loading branch information
sideninja authored Mar 24, 2021
2 parents bd9ef43 + cbcaab9 commit ff9081f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 288 deletions.
3 changes: 1 addition & 2 deletions flow/accounts/add-contract/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ var Cmd = &cobra.Command{
},
)

cli.PrepareAndSendTransaction(
cli.SendTransaction(
projectConf.HostWithOverride(conf.Host),
signerAccount,
tx,
signerAccount.Address,
conf.Results,
)
},
Expand Down
8 changes: 1 addition & 7 deletions flow/accounts/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ var Cmd = &cobra.Command{

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

cli.PrepareAndSendTransaction(
projectConf.HostWithOverride(conf.Host),
signerAccount,
tx,
signerAccount.Address,
conf.Results,
)
cli.SendTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, conf.Results)
},
}

Expand Down
3 changes: 1 addition & 2 deletions flow/accounts/update-contract/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ var Cmd = &cobra.Command{
},
)

cli.PrepareAndSendTransaction(
cli.SendTransaction(
projectConf.HostWithOverride(conf.Host),
signerAccount,
tx,
signerAccount.Address,
conf.Results,
)
},
Expand Down
72 changes: 4 additions & 68 deletions flow/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@ import (
"google.golang.org/grpc"
)

type SignerRole string

const (
SignerRoleAuthorizer SignerRole = "authorizer"
SignerRoleProposer SignerRole = "proposer"
SignerRolePayer SignerRole = "payer"
)

func PrepareTransaction(host string, signerAccount *Account, tx *flow.Transaction, payer flow.Address) *flow.Transaction {
func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction, withResults bool) {
ctx := context.Background()

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

tx.SetReferenceBlockID(sealed.ID).
SetProposalKey(signerAddress, accountKey.Index, accountKey.SequenceNumber).
SetPayer(payer)

return tx
}

func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction, withResults bool) {
ctx := context.Background()
SetPayer(signerAddress)

flowClient, err := client.New(host, grpc.WithInsecure())
err = tx.SignEnvelope(signerAddress, accountKey.Index, signerAccount.Signer)
if err != nil {
Exitf(1, "Failed to connect to host: %s", err)
Exitf(1, "Failed to sign transaction: %s", err)
}

tx = signTransaction(ctx, flowClient, signerAccount, SignerRolePayer, tx)

fmt.Printf("Submitting transaction with ID %s ...\n", tx.ID())

err = flowClient.SendTransaction(context.Background(), *tx)
Expand All @@ -93,51 +77,3 @@ func SendTransaction(host string, signerAccount *Account, tx *flow.Transaction,
printTxResult(tx, res, true)
}
}

func PrepareAndSendTransaction(host string, signerAccount *Account, tx *flow.Transaction, payer flow.Address, withResults bool) {
preparedTx := PrepareTransaction(host, signerAccount, tx, payer)
SendTransaction(host, signerAccount, preparedTx, withResults)
}

func SignTransaction(host string, signerAccount *Account, signerRole SignerRole, tx *flow.Transaction) *flow.Transaction {
ctx := context.Background()

flowClient, err := client.New(host, grpc.WithInsecure())
if err != nil {
Exitf(1, "Failed to connect to host: %s", err)
}

tx = signTransaction(ctx, flowClient, signerAccount, signerRole, tx)
return tx
}

func signTransaction(
ctx context.Context,
flowClient *client.Client,
signerAccount *Account,
signerRole SignerRole,
tx *flow.Transaction,
) *flow.Transaction {
signerAddress := signerAccount.Address
account, err := flowClient.GetAccount(ctx, signerAddress)
if err != nil {
Exitf(1, "Failed to get account with address %s: %s", signerAddress.Hex(), err)
}
accountKey := account.Keys[signerAccount.KeyIndex]
switch signerRole {
case SignerRoleAuthorizer:
err := tx.SignPayload(signerAddress, accountKey.Index, signerAccount.Signer)
if err != nil {
Exitf(1, "Failed to sign transaction: %s", err)
}
case SignerRolePayer:
err := tx.SignEnvelope(signerAddress, accountKey.Index, signerAccount.Signer)
if err != nil {
Exitf(1, "Failed to sign transaction: %s", err)
}
default:
Exitf(1, "Failed to sign transaction: unknown signer role %s", signerRole)
}

return tx
}
62 changes: 18 additions & 44 deletions flow/transactions/send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package send

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

validateKeyPreReq(signerAccount)
var (
tx *flow.Transaction
code []byte
err error
)

if conf.Partial != "" && conf.Code != "" {
cli.Exitf(1, "Both a partial transaction and Cadence code file provided, but cannot use both")
} else if conf.Partial != "" {
partialTxHex, err := ioutil.ReadFile(conf.Partial)
if conf.Code != "" {
code, err = ioutil.ReadFile(conf.Code)
if err != nil {
cli.Exitf(1, "Failed to read partial transaction from %s: %v", conf.Partial, err)
cli.Exitf(1, "Failed to read transaction script from %s", conf.Code)
}
partialTxBytes, err := hex.DecodeString(string(partialTxHex))
if err != nil {
cli.Exitf(1, "Failed to decode partial transaction from %s: %v", conf.Partial, err)
}
tx, err = flow.DecodeTransaction(partialTxBytes)
}

tx := flow.
NewTransaction().
SetScript(code).
AddAuthorizer(signerAccount.Address)

// Arguments
if conf.Args != "" {
transactionArguments, err := cli.ParseArguments(conf.Args)
if err != nil {
cli.Exitf(1, "Failed to decode transaction from %s: %v", conf.Partial, err)
}
} else {
if conf.Code != "" {
code, err = ioutil.ReadFile(conf.Code)
if err != nil {
cli.Exitf(1, "Failed to read transaction script from %s: %v", conf.Code, err)
}
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
}

tx = flow.NewTransaction().
SetScript(code).
AddAuthorizer(signerAccount.Address)
for _, arg := range transactionArguments {
err := tx.AddArgument(arg)

// Arguments
if conf.Args != "" {
transactionArguments, err := cli.ParseArguments(conf.Args)
if err != nil {
cli.Exitf(1, "Invalid arguments passed: %s", conf.Args)
}

for _, arg := range transactionArguments {
err := tx.AddArgument(arg)

if err != nil {
cli.Exitf(1, "Failed to add %s argument to a transaction ", conf.Code)
}
cli.Exitf(1, "Failed to add %s argument to a transaction ", conf.Code)
}
}

tx = cli.PrepareTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, signerAccount.Address)
}

cli.SendTransaction(
projectConf.HostWithOverride(conf.Host),
signerAccount,
tx,
conf.Results,
)
cli.SendTransaction(projectConf.HostWithOverride(conf.Host), signerAccount, tx, conf.Results)
},
}

Expand Down
163 changes: 0 additions & 163 deletions flow/transactions/sign/sign.go

This file was deleted.

Loading

0 comments on commit ff9081f

Please sign in to comment.