Skip to content

Commit

Permalink
signer: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Apr 16, 2018
1 parent 8472645 commit 0942fcf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 73 deletions.
41 changes: 19 additions & 22 deletions cmd/clef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const ExternalApiVersion = "2.0.0"
// InternalApiVersion -- see intapi_changelog.md
const InternalApiVersion = "2.0.0"

const legal_warning = `
const legalWarning = `
WARNING!
Clef is alpha software, and not yet publically released. This software has _not_ been audited, and there
Expand Down Expand Up @@ -170,7 +170,7 @@ remove any stored credential for that address (keyfile)

func init() {
app.Name = "Clef"
app.Usage = "Manage ethereum Account operations"
app.Usage = "Manage Ethereum account operations"
app.Flags = []cli.Flag{
logLevelFlag,
keystoreFlag,
Expand Down Expand Up @@ -215,7 +215,7 @@ func initializeSecrets(c *cli.Context) error {
return err
}
if n != len(masterSeed) {
return fmt.Errorf("Failed to read enough random")
return fmt.Errorf("failed to read enough random")
}
err = os.Mkdir(configDir, 0700)
if err != nil && !os.IsExist(err) {
Expand Down Expand Up @@ -283,13 +283,13 @@ func addCredential(ctx *cli.Context) error {
pwkey := crypto.Keccak256([]byte("credentials"), stretchedKey)

// Initialize the encrypted storages
pw_storage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey)
pwStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey)
key := ctx.Args().First()
value := ""
if len(ctx.Args()) > 1 {
value = ctx.Args().Get(1)
}
pw_storage.Put(key, value)
pwStorage.Put(key, value)
log.Info("Credential store updated", "key", key)
return nil
}
Expand All @@ -300,9 +300,9 @@ func initialize(c *cli.Context) error {
if c.Bool(stdiouiFlag.Name) {
logOutput = os.Stderr
// If using the stdioui, we can't do the 'confirm'-flow
fmt.Fprintf(logOutput, legal_warning)
fmt.Fprintf(logOutput, legalWarning)
} else {
if !confirm(legal_warning) {
if !confirm(legalWarning) {
return fmt.Errorf("aborted by user")
}
}
Expand Down Expand Up @@ -351,9 +351,9 @@ func signer(c *cli.Context) error {
confkey := crypto.Keccak256([]byte("config"), stretchedKey)

// Initialize the encrypted storages
pw_storage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey)
js_storage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "jsstorage.json"), jskey)
config_storage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "config.json"), confkey)
pwStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "credentials.json"), pwkey)
jsStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "jsstorage.json"), jskey)
configStorage := storage.NewAESEncryptedStorage(filepath.Join(vaultLocation, "config.json"), confkey)

//Do we have a rule-file?
ruleJS, err := ioutil.ReadFile(c.String(ruleFlag.Name))
Expand All @@ -363,12 +363,12 @@ func signer(c *cli.Context) error {
hasher := sha256.New()
hasher.Write(ruleJS)
shasum := hasher.Sum(nil)
stored_shasum := config_storage.Get("ruleset_sha256")
if stored_shasum != hex.EncodeToString(shasum) {
log.Info("Could not validate ruleset hash, rules not enabled", "got", hex.EncodeToString(shasum), "expected", stored_shasum)
storedShasum := configStorage.Get("ruleset_sha256")
if storedShasum != hex.EncodeToString(shasum) {
log.Info("Could not validate ruleset hash, rules not enabled", "got", hex.EncodeToString(shasum), "expected", storedShasum)
} else {
// Initialize rules
ruleEngine, err := rules.NewRuleEvaluator(ui, js_storage, pw_storage)
ruleEngine, err := rules.NewRuleEvaluator(ui, jsStorage, pwStorage)
if err != nil {
utils.Fatalf(err.Error())
}
Expand Down Expand Up @@ -411,8 +411,7 @@ func signer(c *cli.Context) error {
if c.Bool(utils.RPCEnabledFlag.Name) {

vhosts := splitAndTrim(c.GlobalString(utils.RPCVirtualHostsFlag.Name))
//!TODO
cors := []string{"*"}
cors := splitAndTrim(c.GlobalString(utils.RPCCORSDomainFlag.Name))

// start http server
httpEndpoint := fmt.Sprintf("%s:%d", c.String(utils.RPCListenAddrFlag.Name), c.Int(rpcPortFlag.Name))
Expand All @@ -433,7 +432,7 @@ func signer(c *cli.Context) error {
if c.IsSet(utils.IPCPathFlag.Name) {
ipcApiUrl = c.String(utils.IPCPathFlag.Name)
} else {
ipcApiUrl = fmt.Sprintf("%s", filepath.Join(configDir, "clef.ipc"))
ipcApiUrl = filepath.Join(configDir, "clef.ipc")
}

listener, _, err := rpc.StartIPCEndpoint(func() bool { return true }, ipcApiUrl, rpcApi)
Expand Down Expand Up @@ -464,10 +463,8 @@ func signer(c *cli.Context) error {
abortChan := make(chan os.Signal)
signal.Notify(abortChan, os.Interrupt)

select {
case sig := <-abortChan:
log.Info("Exiting...", "signal", sig)
}
sig := <-abortChan
log.Info("Exiting...", "signal", sig)

return nil
}
Expand Down Expand Up @@ -527,7 +524,7 @@ func readMasterKey(ctx *cli.Context) ([]byte, error) {
return nil, err
}
if len(masterKey) < 256 {
return nil, fmt.Errorf("Master key of insufficient length, expected >255 bytes, got %d", len(masterKey))
return nil, fmt.Errorf("master key of insufficient length, expected >255 bytes, got %d", len(masterKey))
}
// Create vault location
vaultLocation := filepath.Join(configDir, common.Bytes2Hex(crypto.Keccak256([]byte("vault"), masterKey)[:10]))
Expand Down
2 changes: 1 addition & 1 deletion signer/core/abihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func parseCallData(calldata []byte, abidata string) (*decodedCallData, error) {
// which can be consumed by the standard abi package.
func MethodSelectorToAbi(selector string) ([]byte, error) {

re := regexp.MustCompile("^([^\\)]+)\\(([a-z0-9,\\[\\]]*)\\)")
re := regexp.MustCompile(`^([^\)]+)\(([a-z0-9,\[\]]*)\)`)

type fakeArg struct {
Type string `json:"type"`
Expand Down
3 changes: 1 addition & 2 deletions signer/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ func setup(t *testing.T) (*SignerAPI, chan string) {

controller := make(chan string, 10)

db, err := NewAbiDBFromFile(fmt.Sprintf("../4byte.json"))

db, err := NewAbiDBFromFile("../../cmd/clef/4byte.json")
if err != nil {
utils.Fatalf(err.Error())
}
Expand Down
36 changes: 12 additions & 24 deletions signer/core/stdioui.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,38 @@ func (ui *StdIOUI) dispatch(serviceMethod string, args interface{}, reply interf

func (ui *StdIOUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
var result SignTxResponse
if err := ui.dispatch("ApproveTx", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveTx", request, &result)
return result, err
}

func (ui *StdIOUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
var result SignDataResponse
if err := ui.dispatch("ApproveSignData", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveSignData", request, &result)
return result, err
}

func (ui *StdIOUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
var result ExportResponse
if err := ui.dispatch("ApproveExport", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveExport", request, &result)
return result, err
}

func (ui *StdIOUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
var result ImportResponse
if err := ui.dispatch("ApproveImport", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveImport", request, &result)
return result, err
}

func (ui *StdIOUI) ApproveListing(request *ListRequest) (ListResponse, error) {
var result ListResponse
if err := ui.dispatch("ApproveListing", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveListing", request, &result)
return result, err
}

func (ui *StdIOUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
var result NewAccountResponse
if err := ui.dispatch("ApproveNewAccount", request, &result); err != nil {
return result, err
}
return result, nil
err := ui.dispatch("ApproveNewAccount", request, &result)
return result, err
}

func (ui *StdIOUI) ShowError(message string) {
Expand Down
1 change: 0 additions & 1 deletion signer/core/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (v *Validator) validateCallData(msgs *ValidationMessages, data []byte, meth
} else {
msgs.info(info.String())
}
return
}

// validateSemantics checks if the transactions 'makes sense', and generate warnings for a couple of typical scenarios
Expand Down
43 changes: 20 additions & 23 deletions signer/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function test(thing){
`

func hexAddr(a string) common.Address { return common.BytesToAddress(common.Hex2Bytes(a)) }
func mixAddr(a string) (*common.MixedcaseAddress, error) {
return common.NewMixedcaseAddressFromString(a)
}
Expand All @@ -79,27 +78,27 @@ func (alwaysDenyUi) OnSignerStartup(info core.StartupInfo) {
}

func (alwaysDenyUi) ApproveTx(request *core.SignTxRequest) (core.SignTxResponse, error) {
return core.SignTxResponse{request.Transaction, false, ""}, nil
return core.SignTxResponse{Transaction: request.Transaction, Approved: false, Password: ""}, nil
}

func (alwaysDenyUi) ApproveSignData(request *core.SignDataRequest) (core.SignDataResponse, error) {
return core.SignDataResponse{false, ""}, nil
return core.SignDataResponse{Approved: false, Password: ""}, nil
}

func (alwaysDenyUi) ApproveExport(request *core.ExportRequest) (core.ExportResponse, error) {
return core.ExportResponse{false}, nil
return core.ExportResponse{Approved: false}, nil
}

func (alwaysDenyUi) ApproveImport(request *core.ImportRequest) (core.ImportResponse, error) {
return core.ImportResponse{false, "", ""}, nil
return core.ImportResponse{Approved: false, OldPassword: "", NewPassword: ""}, nil
}

func (alwaysDenyUi) ApproveListing(request *core.ListRequest) (core.ListResponse, error) {
return core.ListResponse{nil}, nil
return core.ListResponse{Accounts: nil}, nil
}

func (alwaysDenyUi) ApproveNewAccount(request *core.NewAccountRequest) (core.NewAccountResponse, error) {
return core.NewAccountResponse{false, ""}, nil
return core.NewAccountResponse{Approved: false, Password: ""}, nil
}

func (alwaysDenyUi) ShowError(message string) {
Expand All @@ -117,10 +116,10 @@ func (alwaysDenyUi) OnApprovedTx(tx ethapi.SignTransactionResult) {
func initRuleEngine(js string) (*rulesetUi, error) {
r, err := NewRuleEvaluator(&alwaysDenyUi{}, storage.NewEphemeralStorage(), storage.NewEphemeralStorage())
if err != nil {
return nil, fmt.Errorf("Failed to create js engine: %v", err)
return nil, fmt.Errorf("failed to create js engine: %v", err)
}
if err = r.Init(js); err != nil {
return nil, fmt.Errorf("Failed to load bootstrap js: %v", err)
return nil, fmt.Errorf("failed to load bootstrap js: %v", err)
}
return r, nil
}
Expand All @@ -145,10 +144,8 @@ func TestListRequest(t *testing.T) {
return
}
resp, err := r.ApproveListing(&core.ListRequest{
accs,
core.Metadata{
"remoteip", "localip", "inproc",
},
Accounts: accs,
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
})
if len(resp.Accounts) != len(accs) {
t.Errorf("Expected check to resolve to 'Approve'")
Expand Down Expand Up @@ -189,7 +186,7 @@ func TestSignTxRequest(t *testing.T) {
From: *from,
To: to},
Callinfo: nil,
Meta: core.Metadata{"remoteip", "localip", "inproc"},
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
})
if err != nil {
t.Errorf("Unexpected error %v", err)
Expand Down Expand Up @@ -252,9 +249,9 @@ func TestForwarding(t *testing.T) {

js := ""
ui := &dummyUi{make([]string, 0)}
jsbackend := storage.NewEphemeralStorage()
credbackend := storage.NewEphemeralStorage()
r, err := NewRuleEvaluator(ui, jsbackend, credbackend)
jsBackend := storage.NewEphemeralStorage()
credBackend := storage.NewEphemeralStorage()
r, err := NewRuleEvaluator(ui, jsBackend, credBackend)
if err != nil {
t.Fatalf("Failed to create js engine: %v", err)
}
Expand All @@ -273,10 +270,10 @@ func TestForwarding(t *testing.T) {
//This one is not forwarded
r.OnApprovedTx(ethapi.SignTransactionResult{})

exp_calls := 8
if len(ui.calls) != exp_calls {
expCalls := 8
if len(ui.calls) != expCalls {

t.Errorf("Expected %d forwarded calls, got %d: %s", exp_calls, len(ui.calls), strings.Join(ui.calls, ","))
t.Errorf("Expected %d forwarded calls, got %d: %s", expCalls, len(ui.calls), strings.Join(ui.calls, ","))

}

Expand Down Expand Up @@ -451,9 +448,9 @@ func dummyTx(value hexutil.Big) *core.SignTxRequest {
Gas: gas,
},
Callinfo: []core.ValidationInfo{
{"Warning", "All your base are bellong to us"},
{Typ: "Warning", Message: "All your base are bellong to us"},
},
Meta: core.Metadata{"remoteip", "localip", "inproc"},
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
}
}
func dummyTxWithV(value uint64) *core.SignTxRequest {
Expand Down Expand Up @@ -622,7 +619,7 @@ function ApproveSignData(r){
Address: *addr,
Message: msg,
Hash: hash,
Meta: core.Metadata{"remoteip", "localip", "inproc"},
Meta: core.Metadata{Remote: "remoteip", Local: "localip", Scheme: "inproc"},
Rawdata: raw,
})
if err != nil {
Expand Down

0 comments on commit 0942fcf

Please sign in to comment.