Skip to content

Commit

Permalink
Merge branch 'feature/more-context'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Vachon committed Feb 27, 2020
2 parents 6e32ae2 + 47b8be1 commit 1b19518
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
12 changes: 6 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type API struct {
lastGetInfoStamp time.Time
lastGetInfoLock sync.Mutex

customGetRequiredKeys func(tx *Transaction) ([]ecc.PublicKey, error)
customGetRequiredKeys func(ctx context.Context, tx *Transaction) ([]ecc.PublicKey, error)
}

func New(baseURL string) *API {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (api *API) EnableKeepAlives() bool {
return false
}

func (api *API) SetCustomGetRequiredKeys(f func(tx *Transaction) ([]ecc.PublicKey, error)) {
func (api *API) SetCustomGetRequiredKeys(f func(ctx context.Context, tx *Transaction) ([]ecc.PublicKey, error)) {
api.customGetRequiredKeys = f
}

Expand All @@ -118,7 +118,7 @@ func (api *API) ProducerPause(ctx context.Context) error {

// CreateSnapshot will write a snapshot file on a nodeos with
// `producer_api` plugin loaded.
func (api *API) CreateSnapshot() (ctx context.Context, out *CreateSnapshotResp, err error) {
func (api *API) CreateSnapshot(ctx context.Context) (out *CreateSnapshotResp, err error) {
err = api.call(ctx, "producer", "create_snapshot", nil, &out)
return
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func (api *API) SignTransaction(ctx context.Context, tx *Transaction, chainID Ch
var requiredKeys []ecc.PublicKey
if api.customGetRequiredKeys != nil {
var err error
requiredKeys, err = api.customGetRequiredKeys(tx)
requiredKeys, err = api.customGetRequiredKeys(ctx, tx)
if err != nil {
return nil, nil, fmt.Errorf("custom_get_required_keys: %s", err)
}
Expand All @@ -366,7 +366,7 @@ func (api *API) SignTransaction(ctx context.Context, tx *Transaction, chainID Ch
requiredKeys = resp.RequiredKeys
}

signedTx, err := api.Signer.Sign(stx, chainID, requiredKeys...)
signedTx, err := api.Signer.Sign(ctx, stx, chainID, requiredKeys...)
if err != nil {
return nil, nil, fmt.Errorf("signing through wallet: %s", err)
}
Expand Down Expand Up @@ -531,7 +531,7 @@ func (api *API) GetRawABI(ctx context.Context, params GetRawABIRequest) (out *Ge
}

func (api *API) GetRequiredKeys(ctx context.Context, tx *Transaction) (out *GetRequiredKeysResp, err error) {
keys, err := api.Signer.AvailableKeys()
keys, err := api.Signer.AvailableKeys(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion example_api_transfer_eos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ExampleAPI_PushTransaction_transfer_EOS() {
api := eos.New(getAPIURL())

keyBag := &eos.KeyBag{}
err := keyBag.ImportPrivateKey(readPrivateKey())
err := keyBag.ImportPrivateKey(context.Background(), readPrivateKey())
if err != nil {
panic(fmt.Errorf("import private key: %s", err))
}
Expand Down
17 changes: 7 additions & 10 deletions signer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package eos

import (
"bufio"
"context"
"crypto/sha256"
"fmt"

"os"

"bufio"

"strings"

"github.com/eoscanada/eos-go/ecc"
)

type Signer interface {
AvailableKeys() (out []ecc.PublicKey, err error)
AvailableKeys(ctx context.Context) (out []ecc.PublicKey, err error)

// Sign signs a `tx` transaction. It gets passed a
// SignedTransaction because it is possible that it holds a few
// signatures and requests this wallet only to add one or more
// signatures it requires.
Sign(tx *SignedTransaction, chainID []byte, requiredKeys ...ecc.PublicKey) (*SignedTransaction, error)
Sign(ctx context.Context, tx *SignedTransaction, chainID []byte, requiredKeys ...ecc.PublicKey) (*SignedTransaction, error)

ImportPrivateKey(wifPrivKey string) error
ImportPrivateKey(ctx context.Context, wifPrivKey string) error
}

// `eosiowd` wallet-based signer
Expand Down Expand Up @@ -110,14 +107,14 @@ func (b *KeyBag) ImportFromFile(path string) error {
return nil
}

func (b *KeyBag) AvailableKeys() (out []ecc.PublicKey, err error) {
func (b *KeyBag) AvailableKeys(ctx context.Context) (out []ecc.PublicKey, err error) {
for _, k := range b.Keys {
out = append(out, k.PublicKey())
}
return
}

func (b *KeyBag) ImportPrivateKey(wifPrivKey string) (err error) {
func (b *KeyBag) ImportPrivateKey(ctx context.Context, wifPrivKey string) (err error) {
return b.Add(wifPrivKey)
}

Expand All @@ -131,7 +128,7 @@ func (b *KeyBag) SignDigest(digest []byte, requiredKey ecc.PublicKey) (ecc.Signa
return privateKey.Sign(digest)
}

func (b *KeyBag) Sign(tx *SignedTransaction, chainID []byte, requiredKeys ...ecc.PublicKey) (*SignedTransaction, error) {
func (b *KeyBag) Sign(ctx context.Context, tx *SignedTransaction, chainID []byte, requiredKeys ...ecc.PublicKey) (*SignedTransaction, error) {
// TODO: probably want to use `tx.packed` and hash the ContextFreeData also.
txdata, cfd, err := tx.PackedTransactionAndCFD()
if err != nil {
Expand Down

0 comments on commit 1b19518

Please sign in to comment.