Commit 54dfce8a authored by Martin Holst Swende's avatar Martin Holst Swende Committed by Péter Szilágyi

cmd/clef: bundle 4byte db into clef, (#19112)

* clef: bundle 4byte db into clef, fix #19048

* clef: add go-generate directive, remove internal abidb parser tool

* cmd/clef: extend go generate to format asset file
parent 31bc2a24
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
// arbitrary data. // arbitrary data.
package main package main
//go:generate go-bindata -o bindata.go resources/4byte.json
//go:generate gofmt -s -w bindata.go
import ( import (
"bufio" "bufio"
"context" "context"
...@@ -57,16 +60,16 @@ import ( ...@@ -57,16 +60,16 @@ import (
) )
const legalWarning = ` const legalWarning = `
WARNING! WARNING!
Clef is an account management tool. It may, like any software, contain bugs. Clef is an account management tool. It may, like any software, contain bugs.
Please take care to Please take care to
- backup your keystore files, - backup your keystore files,
- verify that the keystore(s) can be opened with your password. - verify that the keystore(s) can be opened with your password.
Clef is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; Clef is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details. PURPOSE. See the GNU General Public License for more details.
` `
...@@ -104,11 +107,6 @@ var ( ...@@ -104,11 +107,6 @@ var (
Name: "signersecret", Name: "signersecret",
Usage: "A file containing the (encrypted) master seed to encrypt Clef data, e.g. keystore credentials and ruleset hash", Usage: "A file containing the (encrypted) master seed to encrypt Clef data, e.g. keystore credentials and ruleset hash",
} }
dBFlag = cli.StringFlag{
Name: "4bytedb",
Usage: "File containing 4byte-identifiers",
Value: "./4byte.json",
}
customDBFlag = cli.StringFlag{ customDBFlag = cli.StringFlag{
Name: "4bytedb-custom", Name: "4bytedb-custom",
Usage: "File used for writing new 4byte-identifiers submitted via API", Usage: "File used for writing new 4byte-identifiers submitted via API",
...@@ -145,7 +143,7 @@ var ( ...@@ -145,7 +143,7 @@ var (
configdirFlag, configdirFlag,
}, },
Description: ` Description: `
The init command generates a master seed which Clef can use to store credentials and data needed for The init command generates a master seed which Clef can use to store credentials and data needed for
the rule-engine to work.`, the rule-engine to work.`,
} }
attestCommand = cli.Command{ attestCommand = cli.Command{
...@@ -159,10 +157,10 @@ the rule-engine to work.`, ...@@ -159,10 +157,10 @@ the rule-engine to work.`,
signerSecretFlag, signerSecretFlag,
}, },
Description: ` Description: `
The attest command stores the sha256 of the rule.js-file that you want to use for automatic processing of The attest command stores the sha256 of the rule.js-file that you want to use for automatic processing of
incoming requests. incoming requests.
Whenever you make an edit to the rule file, you need to use attestation to tell Whenever you make an edit to the rule file, you need to use attestation to tell
Clef that the file is 'safe' to execute.`, Clef that the file is 'safe' to execute.`,
} }
...@@ -177,7 +175,7 @@ Clef that the file is 'safe' to execute.`, ...@@ -177,7 +175,7 @@ Clef that the file is 'safe' to execute.`,
signerSecretFlag, signerSecretFlag,
}, },
Description: ` Description: `
The setpw command stores a password for a given address (keyfile). If you enter a blank passphrase, it will The setpw command stores a password for a given address (keyfile). If you enter a blank passphrase, it will
remove any stored credential for that address (keyfile) remove any stored credential for that address (keyfile)
`} `}
gendocCommand = cli.Command{ gendocCommand = cli.Command{
...@@ -206,7 +204,6 @@ func init() { ...@@ -206,7 +204,6 @@ func init() {
utils.RPCEnabledFlag, utils.RPCEnabledFlag,
rpcPortFlag, rpcPortFlag,
signerSecretFlag, signerSecretFlag,
dBFlag,
customDBFlag, customDBFlag,
auditLogFlag, auditLogFlag,
ruleFlag, ruleFlag,
...@@ -273,12 +270,12 @@ func initializeSecrets(c *cli.Context) error { ...@@ -273,12 +270,12 @@ func initializeSecrets(c *cli.Context) error {
} }
fmt.Printf("A master seed has been generated into %s\n", location) fmt.Printf("A master seed has been generated into %s\n", location)
fmt.Printf(` fmt.Printf(`
This is required to be able to store credentials, such as : This is required to be able to store credentials, such as :
* Passwords for keystores (used by rule engine) * Passwords for keystores (used by rule engine)
* Storage for javascript rules * Storage for javascript rules
* Hash of rule-file * Hash of rule-file
You should treat that file with utmost secrecy, and make a backup of it. You should treat that file with utmost secrecy, and make a backup of it.
NOTE: This file does not contain your accounts. Those need to be backed up separately! NOTE: This file does not contain your accounts. Those need to be backed up separately!
`) `)
...@@ -365,13 +362,17 @@ func signer(c *cli.Context) error { ...@@ -365,13 +362,17 @@ func signer(c *cli.Context) error {
log.Info("Using CLI as UI-channel") log.Info("Using CLI as UI-channel")
ui = core.NewCommandlineUI() ui = core.NewCommandlineUI()
} }
fourByteDb := c.GlobalString(dBFlag.Name) // 4bytedb data
fourByteLocal := c.GlobalString(customDBFlag.Name) fourByteLocal := c.GlobalString(customDBFlag.Name)
db, err := core.NewAbiDBFromFiles(fourByteDb, fourByteLocal) data, err := Asset("resources/4byte.json")
if err != nil {
utils.Fatalf(err.Error())
}
db, err := core.NewAbiDBFromFiles(data, fourByteLocal)
if err != nil { if err != nil {
utils.Fatalf(err.Error()) utils.Fatalf(err.Error())
} }
log.Info("Loaded 4byte db", "signatures", db.Size(), "file", fourByteDb, "local", fourByteLocal) log.Info("Loaded 4byte db", "signatures", db.Size(), "local", fourByteLocal)
var ( var (
api core.ExternalAPI api core.ExternalAPI
......
This diff is collapsed.
...@@ -18,6 +18,7 @@ package core ...@@ -18,6 +18,7 @@ package core
import ( import (
"bytes" "bytes"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -183,17 +184,13 @@ func NewAbiDBFromFile(path string) (*AbiDb, error) { ...@@ -183,17 +184,13 @@ func NewAbiDBFromFile(path string) (*AbiDb, error) {
return db, nil return db, nil
} }
// NewAbiDBFromFiles loads both the standard signature database and a custom database. The latter will be used // NewAbiDBFromFiles loads both the standard signature database (resource file)and a custom database.
// to write new values into if they are submitted via the API // The latter will be used to write new values into if they are submitted via the API
func NewAbiDBFromFiles(standard, custom string) (*AbiDb, error) { func NewAbiDBFromFiles(raw []byte, custom string) (*AbiDb, error) {
db := &AbiDb{make(map[string]string), make(map[string]string), custom} db := &AbiDb{make(map[string]string), make(map[string]string), custom}
db.customdbPath = custom db.customdbPath = custom
raw, err := ioutil.ReadFile(standard)
if err != nil {
return nil, err
}
if err := json.Unmarshal(raw, &db.db); err != nil { if err := json.Unmarshal(raw, &db.db); err != nil {
return nil, err return nil, err
} }
...@@ -207,7 +204,6 @@ func NewAbiDBFromFiles(standard, custom string) (*AbiDb, error) { ...@@ -207,7 +204,6 @@ func NewAbiDBFromFiles(standard, custom string) (*AbiDb, error) {
return nil, err return nil, err
} }
} }
return db, nil return db, nil
} }
...@@ -217,7 +213,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) { ...@@ -217,7 +213,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) {
if len(id) < 4 { if len(id) < 4 {
return "", fmt.Errorf("Expected 4-byte id, got %d", len(id)) return "", fmt.Errorf("Expected 4-byte id, got %d", len(id))
} }
sig := common.ToHex(id[:4]) sig := hex.EncodeToString(id[:4])
if key, exists := db.db[sig]; exists { if key, exists := db.db[sig]; exists {
return key, nil return key, nil
} }
...@@ -226,6 +222,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) { ...@@ -226,6 +222,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) {
} }
return "", fmt.Errorf("Signature %v not found", sig) return "", fmt.Errorf("Signature %v not found", sig)
} }
func (db *AbiDb) Size() int { func (db *AbiDb) Size() int {
return len(db.db) return len(db.db)
} }
...@@ -255,6 +252,6 @@ func (db *AbiDb) AddSignature(selector string, data []byte) error { ...@@ -255,6 +252,6 @@ func (db *AbiDb) AddSignature(selector string, data []byte) error {
if err == nil { if err == nil {
return nil return nil
} }
sig := common.ToHex(data[:4]) sig := hex.EncodeToString(data[:4])
return db.saveCustomAbi(selector, sig) return db.saveCustomAbi(selector, sig)
} }
...@@ -205,7 +205,7 @@ func TestCustomABI(t *testing.T) { ...@@ -205,7 +205,7 @@ func TestCustomABI(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
filename := fmt.Sprintf("%s/4byte_custom.json", d) filename := fmt.Sprintf("%s/4byte_custom.json", d)
abidb, err := NewAbiDBFromFiles("../../cmd/clef/4byte.json", filename) abidb, err := NewAbiDBFromFiles([]byte(""), filename)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -38,6 +38,7 @@ type Validator struct { ...@@ -38,6 +38,7 @@ type Validator struct {
func NewValidator(db *AbiDb) *Validator { func NewValidator(db *AbiDb) *Validator {
return &Validator{db} return &Validator{db}
} }
func testSelector(selector string, data []byte) (*decodedCallData, error) { func testSelector(selector string, data []byte) (*decodedCallData, error) {
if selector == "" { if selector == "" {
return nil, fmt.Errorf("selector not found") return nil, fmt.Errorf("selector not found")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment