OSDN Git Service

Dev wallet sa (#1238)
authormuscle_boy <shenao.78@163.com>
Tue, 7 Aug 2018 11:34:33 +0000 (19:34 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 7 Aug 2018 11:34:33 +0000 (19:34 +0800)
* 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
accesstoken/accesstoken_test.go
api/accesstokens.go
net/http/authn/authn.go
net/http/authn/authn_test.go
node/node.go

index 6925067..b357625 100644 (file)
@@ -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)
        }
index a8c26d7..7b6525d 100644 (file)
@@ -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)
        }
index d3d8ef8..32dd8c5 100644 (file)
@@ -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)
        }
 
index e192715..8f5c3d3 100644 (file)
@@ -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
                }
index 7b0ee8b..d9a6dfd 100644 (file)
@@ -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")
        }
index 509eb2c..8516f03 100644 (file)
@@ -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
 }