Skip to content

Commit 54dfce8

Browse files
holimankaralabe
authored andcommitted
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
1 parent 31bc2a2 commit 54dfce8

7 files changed

Lines changed: 305 additions & 33 deletions

File tree

‎cmd/clef/4byte.json‎

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

‎cmd/clef/bindata.go‎

Lines changed: 273 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cmd/clef/main.go‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// arbitrary data.
1919
package main
2020

21+
//go:generate go-bindata -o bindata.go resources/4byte.json
22+
//go:generate gofmt -s -w bindata.go
23+
2124
import (
2225
"bufio"
2326
"context"
@@ -57,16 +60,16 @@ import (
5760
)
5861

5962
const legalWarning = `
60-
WARNING!
63+
WARNING!
6164
62-
Clef is an account management tool. It may, like any software, contain bugs.
65+
Clef is an account management tool. It may, like any software, contain bugs.
6366
6467
Please take care to
65-
- backup your keystore files,
68+
- backup your keystore files,
6669
- verify that the keystore(s) can be opened with your password.
6770
68-
Clef is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
69-
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
71+
Clef is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
72+
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7073
PURPOSE. See the GNU General Public License for more details.
7174
`
7275

@@ -104,11 +107,6 @@ var (
104107
Name: "signersecret",
105108
Usage: "A file containing the (encrypted) master seed to encrypt Clef data, e.g. keystore credentials and ruleset hash",
106109
}
107-
dBFlag = cli.StringFlag{
108-
Name: "4bytedb",
109-
Usage: "File containing 4byte-identifiers",
110-
Value: "./4byte.json",
111-
}
112110
customDBFlag = cli.StringFlag{
113111
Name: "4bytedb-custom",
114112
Usage: "File used for writing new 4byte-identifiers submitted via API",
@@ -145,7 +143,7 @@ var (
145143
configdirFlag,
146144
},
147145
Description: `
148-
The init command generates a master seed which Clef can use to store credentials and data needed for
146+
The init command generates a master seed which Clef can use to store credentials and data needed for
149147
the rule-engine to work.`,
150148
}
151149
attestCommand = cli.Command{
@@ -159,10 +157,10 @@ the rule-engine to work.`,
159157
signerSecretFlag,
160158
},
161159
Description: `
162-
The attest command stores the sha256 of the rule.js-file that you want to use for automatic processing of
163-
incoming requests.
160+
The attest command stores the sha256 of the rule.js-file that you want to use for automatic processing of
161+
incoming requests.
164162
165-
Whenever you make an edit to the rule file, you need to use attestation to tell
163+
Whenever you make an edit to the rule file, you need to use attestation to tell
166164
Clef that the file is 'safe' to execute.`,
167165
}
168166

@@ -177,7 +175,7 @@ Clef that the file is 'safe' to execute.`,
177175
signerSecretFlag,
178176
},
179177
Description: `
180-
The setpw command stores a password for a given address (keyfile). If you enter a blank passphrase, it will
178+
The setpw command stores a password for a given address (keyfile). If you enter a blank passphrase, it will
181179
remove any stored credential for that address (keyfile)
182180
`}
183181
gendocCommand = cli.Command{
@@ -206,7 +204,6 @@ func init() {
206204
utils.RPCEnabledFlag,
207205
rpcPortFlag,
208206
signerSecretFlag,
209-
dBFlag,
210207
customDBFlag,
211208
auditLogFlag,
212209
ruleFlag,
@@ -273,12 +270,12 @@ func initializeSecrets(c *cli.Context) error {
273270
}
274271
fmt.Printf("A master seed has been generated into %s\n", location)
275272
fmt.Printf(`
276-
This is required to be able to store credentials, such as :
273+
This is required to be able to store credentials, such as :
277274
* Passwords for keystores (used by rule engine)
278275
* Storage for javascript rules
279276
* Hash of rule-file
280277
281-
You should treat that file with utmost secrecy, and make a backup of it.
278+
You should treat that file with utmost secrecy, and make a backup of it.
282279
NOTE: This file does not contain your accounts. Those need to be backed up separately!
283280
284281
`)
@@ -365,13 +362,17 @@ func signer(c *cli.Context) error {
365362
log.Info("Using CLI as UI-channel")
366363
ui = core.NewCommandlineUI()
367364
}
368-
fourByteDb := c.GlobalString(dBFlag.Name)
365+
// 4bytedb data
369366
fourByteLocal := c.GlobalString(customDBFlag.Name)
370-
db, err := core.NewAbiDBFromFiles(fourByteDb, fourByteLocal)
367+
data, err := Asset("resources/4byte.json")
368+
if err != nil {
369+
utils.Fatalf(err.Error())
370+
}
371+
db, err := core.NewAbiDBFromFiles(data, fourByteLocal)
371372
if err != nil {
372373
utils.Fatalf(err.Error())
373374
}
374-
log.Info("Loaded 4byte db", "signatures", db.Size(), "file", fourByteDb, "local", fourByteLocal)
375+
log.Info("Loaded 4byte db", "signatures", db.Size(), "local", fourByteLocal)
375376

376377
var (
377378
api core.ExternalAPI

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

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎signer/core/abihelper.go‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"bytes"
21+
"encoding/hex"
2122
"encoding/json"
2223
"fmt"
2324
"io/ioutil"
@@ -183,17 +184,13 @@ func NewAbiDBFromFile(path string) (*AbiDb, error) {
183184
return db, nil
184185
}
185186

186-
// NewAbiDBFromFiles loads both the standard signature database and a custom database. The latter will be used
187-
// to write new values into if they are submitted via the API
188-
func NewAbiDBFromFiles(standard, custom string) (*AbiDb, error) {
187+
// NewAbiDBFromFiles loads both the standard signature database (resource file)and a custom database.
188+
// The latter will be used to write new values into if they are submitted via the API
189+
func NewAbiDBFromFiles(raw []byte, custom string) (*AbiDb, error) {
189190

190191
db := &AbiDb{make(map[string]string), make(map[string]string), custom}
191192
db.customdbPath = custom
192193

193-
raw, err := ioutil.ReadFile(standard)
194-
if err != nil {
195-
return nil, err
196-
}
197194
if err := json.Unmarshal(raw, &db.db); err != nil {
198195
return nil, err
199196
}
@@ -207,7 +204,6 @@ func NewAbiDBFromFiles(standard, custom string) (*AbiDb, error) {
207204
return nil, err
208205
}
209206
}
210-
211207
return db, nil
212208
}
213209

@@ -217,7 +213,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) {
217213
if len(id) < 4 {
218214
return "", fmt.Errorf("Expected 4-byte id, got %d", len(id))
219215
}
220-
sig := common.ToHex(id[:4])
216+
sig := hex.EncodeToString(id[:4])
221217
if key, exists := db.db[sig]; exists {
222218
return key, nil
223219
}
@@ -226,6 +222,7 @@ func (db *AbiDb) LookupMethodSelector(id []byte) (string, error) {
226222
}
227223
return "", fmt.Errorf("Signature %v not found", sig)
228224
}
225+
229226
func (db *AbiDb) Size() int {
230227
return len(db.db)
231228
}
@@ -255,6 +252,6 @@ func (db *AbiDb) AddSignature(selector string, data []byte) error {
255252
if err == nil {
256253
return nil
257254
}
258-
sig := common.ToHex(data[:4])
255+
sig := hex.EncodeToString(data[:4])
259256
return db.saveCustomAbi(selector, sig)
260257
}

‎signer/core/abihelper_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestCustomABI(t *testing.T) {
205205
t.Fatal(err)
206206
}
207207
filename := fmt.Sprintf("%s/4byte_custom.json", d)
208-
abidb, err := NewAbiDBFromFiles("../../cmd/clef/4byte.json", filename)
208+
abidb, err := NewAbiDBFromFiles([]byte(""), filename)
209209
if err != nil {
210210
t.Fatal(err)
211211
}

‎signer/core/validation.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Validator struct {
3838
func NewValidator(db *AbiDb) *Validator {
3939
return &Validator{db}
4040
}
41+
4142
func testSelector(selector string, data []byte) (*decodedCallData, error) {
4243
if selector == "" {
4344
return nil, fmt.Errorf("selector not found")

0 commit comments

Comments
 (0)