Skip to content

Commit d5af3a5

Browse files
authored
cmd/clef, signer: make fourbyte its own package, break dep cycle (#19450)
* cmd/clef, signer: make fourbytes its own package, break dep cycle * signer/fourbyte: pull in a sanitized 4byte database
1 parent 26b50e3 commit d5af3a5

17 files changed

Lines changed: 765 additions & 695 deletions

‎cmd/clef/main.go‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// signer is a utility that can be used so sign transactions and
18-
// arbitrary data.
1917
package main
2018

21-
//go:generate go-bindata -o bindata.go resources/4byte.json
22-
//go:generate gofmt -s -w bindata.go
23-
2419
import (
2520
"bufio"
2621
"context"
@@ -54,6 +49,7 @@ import (
5449
"github.com/ethereum/go-ethereum/rlp"
5550
"github.com/ethereum/go-ethereum/rpc"
5651
"github.com/ethereum/go-ethereum/signer/core"
52+
"github.com/ethereum/go-ethereum/signer/fourbyte"
5753
"github.com/ethereum/go-ethereum/signer/rules"
5854
"github.com/ethereum/go-ethereum/signer/storage"
5955
"gopkg.in/urfave/cli.v1"
@@ -364,15 +360,12 @@ func signer(c *cli.Context) error {
364360
}
365361
// 4bytedb data
366362
fourByteLocal := c.GlobalString(customDBFlag.Name)
367-
data, err := Asset("resources/4byte.json")
368-
if err != nil {
369-
utils.Fatalf(err.Error())
370-
}
371-
db, err := core.NewAbiDBFromFiles(data, fourByteLocal)
363+
db, err := fourbyte.NewWithFile(fourByteLocal)
372364
if err != nil {
373365
utils.Fatalf(err.Error())
374366
}
375-
log.Info("Loaded 4byte db", "signatures", db.Size(), "local", fourByteLocal)
367+
embeds, locals := db.Size()
368+
log.Info("Loaded 4byte database", "embeds", embeds, "locals", locals, "local", fourByteLocal)
376369

377370
var (
378371
api core.ExternalAPI

‎cmd/clef/resources/4byte.json‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎signer/core/abihelper.go‎

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

‎signer/core/api.go‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,25 @@ type UIClientAPI interface {
9292
RegisterUIServer(api *UIServerAPI)
9393
}
9494

95+
// Validator defines the methods required to validate a transaction against some
96+
// sanity defaults as well as any underlying 4byte method database.
97+
//
98+
// Use fourbyte.Database as an implementation. It is separated out of this package
99+
// to allow pieces of the signer package to be used without having to load the
100+
// 7MB embedded 4byte dump.
101+
type Validator interface {
102+
// ValidateTransaction does a number of checks on the supplied transaction, and
103+
// returns either a list of warnings, or an error (indicating that the transaction
104+
// should be immediately rejected).
105+
ValidateTransaction(selector *string, tx *SendTxArgs) (*ValidationMessages, error)
106+
}
107+
95108
// SignerAPI defines the actual implementation of ExternalAPI
96109
type SignerAPI struct {
97110
chainID *big.Int
98111
am *accounts.Manager
99112
UI UIClientAPI
100-
validator *Validator
113+
validator Validator
101114
rejectMode bool
102115
credentials storage.Storage
103116
}
@@ -235,11 +248,11 @@ var ErrRequestDenied = errors.New("Request denied")
235248
// key that is generated when a new Account is created.
236249
// noUSB disables USB support that is required to support hardware devices such as
237250
// ledger and trezor.
238-
func NewSignerAPI(am *accounts.Manager, chainID int64, noUSB bool, ui UIClientAPI, abidb *AbiDb, advancedMode bool, credentials storage.Storage) *SignerAPI {
251+
func NewSignerAPI(am *accounts.Manager, chainID int64, noUSB bool, ui UIClientAPI, validator Validator, advancedMode bool, credentials storage.Storage) *SignerAPI {
239252
if advancedMode {
240253
log.Info("Clef is in advanced mode: will warn instead of reject")
241254
}
242-
signer := &SignerAPI{big.NewInt(chainID), am, ui, NewValidator(abidb), !advancedMode, credentials}
255+
signer := &SignerAPI{big.NewInt(chainID), am, ui, validator, !advancedMode, credentials}
243256
if !noUSB {
244257
signer.startUSBListener()
245258
}
@@ -458,7 +471,7 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
458471
err error
459472
result SignTxResponse
460473
)
461-
msgs, err := api.validator.ValidateTransaction(&args, methodSelector)
474+
msgs, err := api.validator.ValidateTransaction(methodSelector, &args)
462475
if err != nil {
463476
return nil, err
464477
}

0 commit comments

Comments
 (0)