Skip to content

Commit

Permalink
Merge pull request #5 from kaleido-io/anyfrom
Browse files Browse the repository at this point in the history
Update common, and defer parsing of from address
  • Loading branch information
nguyer authored May 24, 2022
2 parents 82eae80 + 6af8062 commit adb2999
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 112 deletions.
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@
"ccache",
"Debugf",
"dklen",
"ethsigner",
"ethsignermocks",
"ethtypes",
"ffresty",
"ffsigner",
"fftypes",
"filewallet",
"GJSON",
"httpserver",
"hyperledger",
"Infof",
"Kaleido",
"kdfparams",
"Keccak",
"keypair",
"keystorev",
"resty",
"rpcbackendmocks",
"secp",
"signerconfig",
"signermsgs",
"stretchr",
"Tracef",
Expand Down
31 changes: 16 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ module github.com/hyperledger/firefly-signer
go 1.17

require (
github.com/btcsuite/btcd v0.22.0-beta
github.com/btcsuite/btcd v0.22.1
github.com/go-resty/resty/v2 v2.7.0
github.com/gorilla/mux v1.8.0
github.com/hyperledger/firefly-common v0.1.2
github.com/hyperledger/firefly-common v0.1.5
github.com/karlseguin/ccache v2.0.3+incompatible
github.com/pelletier/go-toml v1.9.4
github.com/pelletier/go-toml v1.9.5
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/aidarkhanov/nanoid v1.0.8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -31,23 +32,23 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
98 changes: 41 additions & 57 deletions go.sum

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions internal/filewallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,16 @@ func (w *fileWallet) Close() error {
return nil
}

func (w *fileWallet) getSignerForAccount(ctx context.Context, addr *ethtypes.Address0xHex) (*secp256k1.KeyPair, error) {
func (w *fileWallet) getSignerForAccount(ctx context.Context, addr json.RawMessage) (*secp256k1.KeyPair, error) {

addrString := addr.String()
// We require an ethereum address in the "from" field
var from ethtypes.Address0xHex
err := json.Unmarshal(addr, &from)
if err != nil {
return nil, err
}

addrString := from.String()
if !w.filenames0xPrefix {
addrString = strings.TrimPrefix(addrString, "0x")
}
Expand Down
40 changes: 25 additions & 15 deletions internal/filewallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package filewallet

import (
"context"
"encoding/json"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-signer/internal/signerconfig"
"github.com/hyperledger/firefly-signer/pkg/ethsigner"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func TestGetAccountSimpleFilenamesOK(t *testing.T) {
ctx, f, done := newTestFilenameOnlyWallet(t)
defer done()

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestSignOK(t *testing.T) {
assert.NoError(t, err)

b, err := f.Sign(ctx, &ethsigner.Transaction{
From: ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"),
From: json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`),
}, 2022)
assert.NoError(t, err)
assert.NotNil(t, b)
Expand All @@ -124,7 +124,7 @@ func TestSignNotFound(t *testing.T) {
defer done()

_, err := f.Sign(ctx, &ethsigner.Transaction{
From: ethtypes.MustNewAddress("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
From: json.RawMessage(`"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"`),
}, 2022)
assert.Regexp(t, "FF22014", err)

Expand All @@ -135,12 +135,12 @@ func TestGetAccountCached(t *testing.T) {
ctx, f, done := newTestTOMLMetadataWallet(t)
defer done()

s, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
s, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.NoError(t, err)
assert.NotNil(t, s)

// 2nd time is cached
s, err = f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
s, err = f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.NoError(t, err)
assert.NotNil(t, s)

Expand All @@ -152,18 +152,28 @@ func TestGetAccountBadYAML(t *testing.T) {
defer done()
f.metadataFormat = "yaml"

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}

func TestGetAccountBadAddress(t *testing.T) {

ctx, f, done := newTestTOMLMetadataWallet(t)
defer done()

_, err := f.getSignerForAccount(ctx, json.RawMessage(`"bad address"`))
assert.Regexp(t, "bad address", err)

}

func TestGetAccountBadJSON(t *testing.T) {

ctx, f, done := newTestTOMLMetadataWallet(t)
defer done()
f.metadataFormat = "json"

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}
Expand Down Expand Up @@ -208,7 +218,7 @@ func TestGetAccountBadTOMLRefKey(t *testing.T) {
assert.NoError(t, err)
f := ff.(*fileWallet)

_, err = f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err = f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)
}

Expand All @@ -224,7 +234,7 @@ func TestGetAccountNoTemplates(t *testing.T) {
assert.NoError(t, err)
f := ff.(*fileWallet)

_, err = f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err = f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)
}

Expand All @@ -236,7 +246,7 @@ func TestGetAccountBadKeyfile(t *testing.T) {
f.metadataPasswordFileProperty = nil
f.defaultPasswordFile = "../../test/keystore_toml/1f185718734552d08278aa70f804580bab5fd2b4.pwd"

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}
Expand All @@ -249,7 +259,7 @@ func TestGetAccountBadDefaultPasswordfile(t *testing.T) {
f.metadataPasswordFileProperty = nil
f.defaultPasswordFile = "!!!"

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}
Expand All @@ -260,7 +270,7 @@ func TestGetAccountNoPassword(t *testing.T) {
defer done()
f.metadataPasswordFileProperty = nil

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0x1f185718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0x1f185718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22015", err)

}
Expand All @@ -271,7 +281,7 @@ func TestGetAccountWrongPath(t *testing.T) {
defer done()
f.metadataPasswordFileProperty = nil

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("5d093e9b41911be5f5c4cf91b108bac5d130fa83"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"5d093e9b41911be5f5c4cf91b108bac5d130fa83"`))
assert.Regexp(t, "FF22015", err)

}
Expand All @@ -284,7 +294,7 @@ func TestGetAccountNotFound(t *testing.T) {
f.metadataPasswordFileProperty = nil
f.defaultPasswordFile = "!!!"

_, err := f.getSignerForAccount(ctx, ethtypes.MustNewAddress("0xFFFF5718734552d08278aa70f804580bab5fd2b4"))
_, err := f.getSignerForAccount(ctx, json.RawMessage(`"0xFFFF5718734552d08278aa70f804580bab5fd2b4"`))
assert.Regexp(t, "FF22014", err)

}
2 changes: 1 addition & 1 deletion internal/rpcbackend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Backend interface {
// NewRPCBackend Constructor
func NewRPCBackend(ctx context.Context) Backend {
return &rpcBackend{
client: ffresty.New(ctx, signerconfig.BackendPrefix),
client: ffresty.New(ctx, signerconfig.BackendConfig),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/rpcbackend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newTestServer(t *testing.T, rpcHandler testRPCHander) (context.Context, *rp
}))

signerconfig.Reset()
prefix := signerconfig.BackendPrefix
prefix := signerconfig.BackendConfig
prefix.Set(ffresty.HTTPConfigURL, fmt.Sprintf("http://%s", server.Listener.Addr()))

rb := NewRPCBackend(ctx).(*rpcBackend)
Expand Down
7 changes: 6 additions & 1 deletion internal/rpcserver/rpcprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ func (s *rpcServer) processEthSendTransaction(ctx context.Context, rpcReq *rpcba
// to the up-stream node. This should not be relied upon for production use cases.
// See FireFly Transaction Manager, or FireFly EthConnect, for more advanced nonce management capabilities.
if txn.Nonce == nil {
err = s.backend.CallRPC(ctx, &txn.Nonce, "eth_getTransactionCount", txn.From, "pending")
var from ethtypes.Address0xHex
err := json.Unmarshal(txn.From, &from)
if err != nil {
return nil, err
}
err = s.backend.CallRPC(ctx, &txn.Nonce, "eth_getTransactionCount", &from, "pending")
if err != nil {
return rpcbackend.RPCErrorResponse(err, rpcReq.ID, rpcbackend.RPCCodeInternalError), err
}
Expand Down
21 changes: 21 additions & 0 deletions internal/rpcserver/rpcprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ func TestSignMissingFrom(t *testing.T) {

}

func TestSignGetNonceBadAddress(t *testing.T) {

_, s, done := newTestServer(t)
defer done()

bm := s.backend.(*rpcbackendmocks.Backend)
bm.On("CallRPC", mock.Anything, mock.Anything, "eth_getTransactionCount", mock.Anything, "pending").Return(fmt.Errorf("pop"))

_, err := s.processRPC(s.ctx, &rpcbackend.RPCRequest{
ID: fftypes.JSONAnyPtr("1"),
Method: "eth_sendTransaction",
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr(`{
"from": "bad address"
}`),
},
})
assert.Regexp(t, "bad address", err)

}

func TestSignGetNonceFail(t *testing.T) {

_, s, done := newTestServer(t)
Expand Down
2 changes: 1 addition & 1 deletion internal/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewServer(ctx context.Context, wallet ethsigner.Wallet) (ss Server, err err
}
s.ctx, s.cancelCtx = context.WithCancel(ctx)

s.apiServer, err = httpserver.NewHTTPServer(ctx, "server", s.router(), s.apiServerDone, signerconfig.ServerPrefix, signerconfig.CorsPrefix)
s.apiServer, err = httpserver.NewHTTPServer(ctx, "server", s.router(), s.apiServerDone, signerconfig.ServerConfig, signerconfig.CorsConfig)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/rpcserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func newTestServer(t *testing.T) (string, *rpcServer, func()) {
assert.NoError(t, err)
serverPort := strings.Split(ln.Addr().String(), ":")[1]
ln.Close()
signerconfig.ServerPrefix.Set(httpserver.HTTPConfPort, serverPort)
signerconfig.ServerPrefix.Set(httpserver.HTTPConfAddress, "127.0.0.1")
signerconfig.ServerConfig.Set(httpserver.HTTPConfPort, serverPort)
signerconfig.ServerConfig.Set(httpserver.HTTPConfAddress, "127.0.0.1")

w := &ethsignermocks.Wallet{}

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestStartFailInitialize(t *testing.T) {
func TestBadConfig(t *testing.T) {

signerconfig.Reset()
signerconfig.ServerPrefix.Set(httpserver.HTTPConfAddress, ":::::")
signerconfig.ServerConfig.Set(httpserver.HTTPConfAddress, ":::::")
_, err := NewServer(context.Background(), &ethsignermocks.Wallet{})
assert.Error(t, err)

Expand Down
18 changes: 9 additions & 9 deletions internal/signerconfig/signerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ var (
FileWalletMetadataPasswordFileProperty = ffc("fileWallet.metadata.passwordFileProperty")
)

var ServerPrefix config.Prefix
var ServerConfig config.Section

var CorsPrefix config.Prefix
var CorsConfig config.Section

var BackendPrefix config.Prefix
var BackendConfig config.Section

func setDefaults() {
viper.SetDefault(string(BackendChainID), -1)
Expand All @@ -69,12 +69,12 @@ func setDefaults() {
func Reset() {
config.RootConfigReset(setDefaults)

ServerPrefix = config.NewPluginConfig("server")
httpserver.InitHTTPConfPrefix(ServerPrefix, 8545)
ServerConfig = config.RootSection("server")
httpserver.InitHTTPConfig(ServerConfig, 8545)

CorsPrefix = config.NewPluginConfig("cors")
httpserver.InitCORSConfig(CorsPrefix)
CorsConfig = config.RootSection("cors")
httpserver.InitCORSConfig(CorsConfig)

BackendPrefix = config.NewPluginConfig("backend")
wsclient.InitPrefix(BackendPrefix)
BackendConfig = config.RootSection("backend")
wsclient.InitConfig(BackendConfig)
}
Loading

0 comments on commit adb2999

Please sign in to comment.