Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #671 from Bytom/dev
Browse files Browse the repository at this point in the history
merge dev
  • Loading branch information
wliyongfeng authored Apr 17, 2018
2 parents f7709ca + 18a304e commit 9d3dab4
Show file tree
Hide file tree
Showing 67 changed files with 837 additions and 715 deletions.
20 changes: 15 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ profile.cov
profile.tmp

.DS_Store
cmd/bytomd/bytomd_error/config.toml
cmd/bytomd/bytomd_error/genesis.json
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.848287391Z--931de33f-e62e-4de7-8036-f82ad5efa77e
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.953012475Z--36b63ec7-b585-4a0e-95ff-e3f71b4ccb8b
blockchain/pseudohsm/UTC--2017-11-01T09-24-30.596579759Z--2178d885-aca6-4436-a865-e7e439e4bd9c
cmd/bytomd/bytomd_error/config.toml
cmd/bytomd/bytomd_error/genesis.json
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.848287391Z--931de33f-e62e-4de7-8036-f82ad5efa77e
blockchain/pseudohsm/testdata/pseudo/UTC--2017-11-01T09-24-31.953012475Z--36b63ec7-b585-4a0e-95ff-e3f71b4ccb8b
blockchain/pseudohsm/UTC--2017-11-01T09-24-30.596579759Z--2178d885-aca6-4436-a865-e7e439e4bd9c

# ====> Sublime Text
# Sublime Text Project and Workspace Files
*.sublime-workspace
*.sublime-project

# ====> VS Code
# VS Code Config Files
.vscode/

4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ all: test target release-all

bytomd:
@echo "Building bytomd to cmd/bytomd/bytomd"
go build $(BUILD_FLAGS) -o cmd/bytomd/bytomd cmd/bytomd/main.go
@go build $(BUILD_FLAGS) -o cmd/bytomd/bytomd cmd/bytomd/main.go

bytomcli:
@echo "Building bytomcli to cmd/bytomcli/bytomcli"
go build $(BUILD_FLAGS) -o cmd/bytomcli/bytomcli cmd/bytomcli/main.go
@go build $(BUILD_FLAGS) -o cmd/bytomcli/bytomcli cmd/bytomcli/main.go

target:
mkdir -p $@
Expand Down Expand Up @@ -80,7 +80,11 @@ release-all: clean
GOOS=windows make release

clean:
rm -rf target
@echo "Cleaning binaries built"
@rm -rf cmd/bytomd/bytomd
@rm -rf cmd/bytomcli/bytomcli
@rm -rf cmd/miner/miner
@rm -rf target

target/$(BYTOMD_BINARY32):
CGO_ENABLED=0 GOARCH=386 go build $(BUILD_FLAGS) -o $@ cmd/bytomd/main.go
Expand Down
236 changes: 125 additions & 111 deletions README.md

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions accesstoken/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
ErrBadType = errors.New("type must be client or network")
// ErrNoMatchID is returned when Delete is called on nonexisting ID.
ErrNoMatchID = errors.New("nonexisting access token ID")
// ErrInvalidToken is returned when Check is called on invalid token
ErrInvalidToken = errors.New("invalid token")

// validIDRegexp checks that all characters are alphumeric, _ or -.
// It also must have a length of at least 1.
Expand Down Expand Up @@ -90,26 +92,26 @@ 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) (bool, error) {
func (cs *CredentialStore) Check(ctx context.Context, id string, secret string) error {
if !validIDRegexp.MatchString(id) {
return false, errors.WithDetailf(ErrBadID, "invalid id %q", id)
return errors.WithDetailf(ErrBadID, "invalid id %q", id)
}

var value []byte
token := &Token{}

if value = cs.DB.Get([]byte(id)); value == nil {
return false, errors.WithDetailf(ErrNoMatchID, "check id %q nonexisting", id)
return errors.WithDetailf(ErrNoMatchID, "check id %q nonexisting", id)
}
if err := json.Unmarshal(value, token); err != nil {
return false, err
return err
}

if strings.Compare(strings.Split(token.Token, ":")[1], secret) == 0 {
return true, nil
if strings.Split(token.Token, ":")[1] == secret {
return nil
}

return false, nil
return ErrInvalidToken
}

// List lists all access tokens.
Expand All @@ -134,6 +136,10 @@ func (cs *CredentialStore) Delete(ctx context.Context, id string) error {
return errors.WithDetailf(ErrBadID, "invalid id %q", id)
}

if value := cs.DB.Get([]byte(id)); value == nil {
return errors.WithDetailf(ErrNoMatchID, "check id %q", id)
}

cs.DB.Delete([]byte(id))
return nil
}
16 changes: 4 additions & 12 deletions accesstoken/accesstoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestList(t *testing.T) {
t.Error("List errored: get invalid length")
}
for _, v := range got {
if m := strings.Compare(v.Token, tokenMap[v.ID].Token); m != 0 {
if v.Token != tokenMap[v.ID].Token {
t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
}
continue
Expand All @@ -72,20 +72,12 @@ func TestCheck(t *testing.T) {
token := mustCreateToken(ctx, t, cs, "x", "client")
tokenParts := strings.Split(token.Token, ":")

valid, err := cs.Check(ctx, tokenParts[0], tokenParts[1])
if err != nil {
if err := cs.Check(ctx, tokenParts[0], tokenParts[1]); err != nil {
t.Fatal(err)
}
if !valid {
t.Fatal("expected token and secret to be valid")
}

valid, err = cs.Check(ctx, "x", "badsecret")
if err != nil {
t.Fatal(err)
}
if valid {
t.Fatal("expected bad secret to not be valid")
if err := cs.Check(ctx, "x", "badsecret"); err != ErrInvalidToken {
t.Fatal("invalid token check passed")
}
}

Expand Down
53 changes: 18 additions & 35 deletions account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var (
ErrDuplicateAlias = errors.New("duplicate account alias")
ErrFindAccount = errors.New("fail to find account")
ErrMarshalAccount = errors.New("failed marshal account")
ErrMarshalTags = errors.New("failed marshal account to update tags")
)

func aliasKey(name string) []byte {
Expand Down Expand Up @@ -131,7 +130,6 @@ type Account struct {
*signers.Signer
ID string
Alias string
Tags map[string]interface{}
}

func (m *Manager) getNextXpubsIndex(xpubs []chainkd.XPub) uint64 {
Expand All @@ -149,7 +147,7 @@ func (m *Manager) getNextXpubsIndex(xpubs []chainkd.XPub) uint64 {
}

// Create creates a new Account.
func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int, alias string, tags map[string]interface{}) (*Account, error) {
func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int, alias string) (*Account, error) {
normalizedAlias := strings.ToLower(strings.TrimSpace(alias))
if existed := m.db.Get(aliasKey(normalizedAlias)); existed != nil {
return nil, ErrDuplicateAlias
Expand All @@ -161,7 +159,7 @@ func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int,
return nil, errors.Wrap(err)
}

account := &Account{Signer: signer, ID: id, Alias: normalizedAlias, Tags: tags}
account := &Account{Signer: signer, ID: id, Alias: normalizedAlias}
rawAccount, err := json.Marshal(account)
if err != nil {
return nil, ErrMarshalAccount
Expand All @@ -176,29 +174,6 @@ func (m *Manager) Create(ctx context.Context, xpubs []chainkd.XPub, quorum int,
return account, nil
}

// UpdateTags modifies the tags of the specified account. The account may be
// identified either by ID or Alias, but not both.
func (m *Manager) UpdateTags(ctx context.Context, accountInfo string, tags map[string]interface{}) (err error) {
account := &Account{}
if account, err = m.FindByAlias(nil, accountInfo); err != nil {
if account, err = m.findByID(ctx, accountInfo); err != nil {
return err
}
}

account.Tags = tags
rawAccount, err := json.Marshal(account)
if err != nil {
return ErrMarshalTags
}

m.db.Set(Key(account.ID), rawAccount)
m.cacheMu.Lock()
m.cache.Add(account.ID, account)
m.cacheMu.Unlock()
return nil
}

// FindByAlias retrieves an account's Signer record by its alias
func (m *Manager) FindByAlias(ctx context.Context, alias string) (*Account, error) {
m.cacheMu.Lock()
Expand Down Expand Up @@ -262,21 +237,26 @@ func (m *Manager) GetAliasByID(id string) string {
return account.Alias
}

// CreateAddressForChange generate an address for the UTXO change
func (m *Manager) CreateCtrlProgramForChange(ctx context.Context, accountID string) (cp *CtrlProgram, err error) {
return m.CreateAddress(ctx, accountID, true)
}

// CreateAddress generate an address for the select account
func (m *Manager) CreateAddress(ctx context.Context, accountID string) (cp *CtrlProgram, err error) {
func (m *Manager) CreateAddress(ctx context.Context, accountID string, change bool) (cp *CtrlProgram, err error) {
account, err := m.findByID(ctx, accountID)
if err != nil {
return nil, err
}
return m.createAddress(ctx, account)
return m.createAddress(ctx, account, change)
}

// CreateAddress generate an address for the select account
func (m *Manager) createAddress(ctx context.Context, account *Account) (cp *CtrlProgram, err error) {
func (m *Manager) createAddress(ctx context.Context, account *Account, change bool) (cp *CtrlProgram, err error) {
if len(account.XPubs) == 1 {
cp, err = m.createP2PKH(ctx, account)
cp, err = m.createP2PKH(ctx, account, change)
} else {
cp, err = m.createP2SH(ctx, account)
cp, err = m.createP2SH(ctx, account, change)
}
if err != nil {
return nil, err
Expand All @@ -288,7 +268,7 @@ func (m *Manager) createAddress(ctx context.Context, account *Account) (cp *Ctrl
return cp, nil
}

func (m *Manager) createP2PKH(ctx context.Context, account *Account) (*CtrlProgram, error) {
func (m *Manager) createP2PKH(ctx context.Context, account *Account, change bool) (*CtrlProgram, error) {
idx := m.getNextXpubsIndex(account.Signer.XPubs)
path := signers.Path(account.Signer, signers.AccountKeySpace, idx)
derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
Expand All @@ -311,10 +291,11 @@ func (m *Manager) createP2PKH(ctx context.Context, account *Account) (*CtrlProgr
Address: address.EncodeAddress(),
KeyIndex: idx,
ControlProgram: control,
Change: change,
}, nil
}

func (m *Manager) createP2SH(ctx context.Context, account *Account) (*CtrlProgram, error) {
func (m *Manager) createP2SH(ctx context.Context, account *Account, change bool) (*CtrlProgram, error) {
idx := m.getNextXpubsIndex(account.Signer.XPubs)
path := signers.Path(account.Signer, signers.AccountKeySpace, idx)
derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
Expand All @@ -341,6 +322,7 @@ func (m *Manager) createP2SH(ctx context.Context, account *Account) (*CtrlProgra
Address: address.EncodeAddress(),
KeyIndex: idx,
ControlProgram: control,
Change: change,
}, nil
}

Expand All @@ -350,6 +332,7 @@ type CtrlProgram struct {
Address string
KeyIndex uint64
ControlProgram []byte
Change bool // Mark whether this control program is for UTXO change
}

func (m *Manager) insertAccountControlProgram(ctx context.Context, progs ...*CtrlProgram) error {
Expand Down Expand Up @@ -393,7 +376,7 @@ func (m *Manager) GetCoinbaseControlProgram() ([]byte, error) {
return nil, err
}

program, err := m.createAddress(nil, account)
program, err := m.createAddress(nil, account, false)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9d3dab4

Please sign in to comment.