From 2a7cd55627b5d6fa2fb1f9c9dab48af0f27c51c5 Mon Sep 17 00:00:00 2001 From: muscle_boy Date: Tue, 7 Aug 2018 19:34:33 +0800 Subject: [PATCH] Dev wallet sa (#1238) * the transaction output amout prohibit set zero * add network access control api * format import code style * refactor * code refactor * bug fix * the struct node_info add json field * estimate gas support multi-sign * add testcase of estimate gas * add testcase * bug fix * add test case * test case refactor * list-tx,list-address,list-utxo support partition * list-addresses list-tx list-utxo support pagging * refactor pagging * fix save asset * fix save external assets * remove blank * remove useless context * remove redudant web address config * fix bug * remove useless ctx --- accesstoken/accesstoken.go | 9 ++++----- accesstoken/accesstoken_test.go | 16 +++++++--------- api/accesstokens.go | 8 ++++---- net/http/authn/authn.go | 2 +- net/http/authn/authn_test.go | 5 +---- node/node.go | 12 +++++++++--- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/accesstoken/accesstoken.go b/accesstoken/accesstoken.go index 69250675..b357625e 100644 --- a/accesstoken/accesstoken.go +++ b/accesstoken/accesstoken.go @@ -3,7 +3,6 @@ package accesstoken import ( - "context" "crypto/rand" "encoding/json" "fmt" @@ -57,7 +56,7 @@ func NewStore(db dbm.DB) *CredentialStore { } // Create generates a new access token with the given ID. -func (cs *CredentialStore) Create(ctx context.Context, id, typ string) (*Token, error) { +func (cs *CredentialStore) Create(id, typ string) (*Token, error) { if !validIDRegexp.MatchString(id) { return nil, errors.WithDetailf(ErrBadID, "invalid id %q", id) } @@ -92,7 +91,7 @@ func (cs *CredentialStore) Create(ctx context.Context, id, typ string) (*Token, } // Check returns whether or not an id-secret pair is a valid access token. -func (cs *CredentialStore) Check(ctx context.Context, id string, secret string) error { +func (cs *CredentialStore) Check(id string, secret string) error { if !validIDRegexp.MatchString(id) { return errors.WithDetailf(ErrBadID, "invalid id %q", id) } @@ -115,7 +114,7 @@ func (cs *CredentialStore) Check(ctx context.Context, id string, secret string) } // List lists all access tokens. -func (cs *CredentialStore) List(ctx context.Context) ([]*Token, error) { +func (cs *CredentialStore) List() ([]*Token, error) { tokens := make([]*Token, 0) iter := cs.DB.Iterator() defer iter.Release() @@ -131,7 +130,7 @@ func (cs *CredentialStore) List(ctx context.Context) ([]*Token, error) { } // Delete deletes an access token by id. -func (cs *CredentialStore) Delete(ctx context.Context, id string) error { +func (cs *CredentialStore) Delete(id string) error { if !validIDRegexp.MatchString(id) { return errors.WithDetailf(ErrBadID, "invalid id %q", id) } diff --git a/accesstoken/accesstoken_test.go b/accesstoken/accesstoken_test.go index a8c26d78..7b6525dc 100644 --- a/accesstoken/accesstoken_test.go +++ b/accesstoken/accesstoken_test.go @@ -15,7 +15,6 @@ func TestCreate(t *testing.T) { testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") cs := NewStore(testDB) - ctx := context.Background() cases := []struct { id, typ string @@ -29,7 +28,7 @@ func TestCreate(t *testing.T) { } for _, c := range cases { - _, err := cs.Create(ctx, c.id, c.typ) + _, err := cs.Create(c.id, c.typ) if errors.Root(err) != c.want { t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want) } @@ -47,7 +46,7 @@ func TestList(t *testing.T) { tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test") tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test") - got, err := cs.List(ctx) + got, err := cs.List() if err != nil { t.Errorf("List errored: get list error") } @@ -72,11 +71,11 @@ func TestCheck(t *testing.T) { token := mustCreateToken(ctx, t, cs, "x", "client") tokenParts := strings.Split(token.Token, ":") - if err := cs.Check(ctx, tokenParts[0], tokenParts[1]); err != nil { + if err := cs.Check(tokenParts[0], tokenParts[1]); err != nil { t.Fatal(err) } - if err := cs.Check(ctx, "x", "badsecret"); err != ErrInvalidToken { + if err := cs.Check("x", "badsecret"); err != ErrInvalidToken { t.Fatal("invalid token check passed") } } @@ -90,7 +89,7 @@ func TestDelete(t *testing.T) { const id = "Y" mustCreateToken(ctx, t, cs, id, "client") - err := cs.Delete(ctx, id) + err := cs.Delete(id) if err != nil { t.Fatal(err) } @@ -102,19 +101,18 @@ func TestDelete(t *testing.T) { } func TestDeleteWithInvalidId(t *testing.T) { - ctx := context.Background() testDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") cs := NewStore(testDB) - err := cs.Delete(ctx, "@") + err := cs.Delete("@") if errors.Root(err) != ErrBadID { t.Errorf("Deletion with invalid id success, while it should not") } } func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token { - token, err := cs.Create(ctx, id, typ) + token, err := cs.Create(id, typ) if err != nil { t.Fatal(err) } diff --git a/api/accesstokens.go b/api/accesstokens.go index d3d8ef8d..32dd8c5e 100644 --- a/api/accesstokens.go +++ b/api/accesstokens.go @@ -10,7 +10,7 @@ func (a *API) createAccessToken(ctx context.Context, x struct { ID string `json:"id"` Type string `json:"type"` }) Response { - token, err := a.accessTokens.Create(ctx, x.ID, x.Type) + token, err := a.accessTokens.Create(x.ID, x.Type) if err != nil { return NewErrorResponse(err) } @@ -18,7 +18,7 @@ func (a *API) createAccessToken(ctx context.Context, x struct { } func (a *API) listAccessTokens(ctx context.Context) Response { - tokens, err := a.accessTokens.List(ctx) + tokens, err := a.accessTokens.List() if err != nil { log.Errorf("listAccessTokens: %v", err) return NewErrorResponse(err) @@ -31,7 +31,7 @@ func (a *API) deleteAccessToken(ctx context.Context, x struct { ID string `json:"id"` }) Response { //TODO Add delete permission verify. - if err := a.accessTokens.Delete(ctx, x.ID); err != nil { + if err := a.accessTokens.Delete(x.ID); err != nil { return NewErrorResponse(err) } return NewSuccessResponse(nil) @@ -41,7 +41,7 @@ func (a *API) checkAccessToken(ctx context.Context, x struct { ID string `json:"id"` Secret string `json:"secret"` }) Response { - if err := a.accessTokens.Check(ctx, x.ID, x.Secret); err != nil { + if err := a.accessTokens.Check(x.ID, x.Secret); err != nil { return NewErrorResponse(err) } diff --git a/net/http/authn/authn.go b/net/http/authn/authn.go index e1927151..8f5c3d36 100644 --- a/net/http/authn/authn.go +++ b/net/http/authn/authn.go @@ -140,7 +140,7 @@ func (a *API) cachedTokenAuthnCheck(ctx context.Context, user, pw string) error res, ok := a.tokenMap[user+pw] a.tokenMu.Unlock() if !ok || time.Now().After(res.lastLookup.Add(tokenExpiry)) { - err := a.tokens.Check(ctx, user, pw) + err := a.tokens.Check(user, pw) if err != nil { return ErrInvalidToken } diff --git a/net/http/authn/authn_test.go b/net/http/authn/authn_test.go index 7b0ee8bf..d9a6dfd1 100644 --- a/net/http/authn/authn_test.go +++ b/net/http/authn/authn_test.go @@ -1,7 +1,6 @@ package authn import ( - "context" "net/http" "os" "strings" @@ -14,12 +13,10 @@ import ( ) func TestAuthenticate(t *testing.T) { - ctx := context.Background() - tokenDB := dbm.NewDB("testdb", "leveldb", "temp") defer os.RemoveAll("temp") tokenStore := accesstoken.NewStore(tokenDB) - token, err := tokenStore.Create(ctx, "alice", "test") + token, err := tokenStore.Create("alice", "test") if err != nil { t.Errorf("create token error") } diff --git a/node/node.go b/node/node.go index 509eb2c5..8516f03a 100644 --- a/node/node.go +++ b/node/node.go @@ -1,6 +1,7 @@ package node import ( + "strings" "context" "errors" "net/http" @@ -35,7 +36,7 @@ import ( ) const ( - webAddress = "http://127.0.0.1:9888" + webHost = "http://127.0.0.1" maxNewBlockChSize = 1024 ) @@ -212,7 +213,8 @@ func initLogFile(config *cfg.Config) { } // Lanch web broser or not -func launchWebBrowser() { +func launchWebBrowser(port string) { + webAddress := webHost + ":" + port log.Info("Launching System Browser with :", webAddress) if err := browser.Open(webAddress); err != nil { log.Error(err.Error()) @@ -242,7 +244,11 @@ func (n *Node) OnStart() error { } n.initAndstartApiServer() if !n.config.Web.Closed { - launchWebBrowser() + s := strings.Split(n.config.ApiAddress, ":") + if len(s) != 2 { + log.Error("Invalid api address") + } + launchWebBrowser(s[1]) } return nil } -- 2.11.0