Skip to content

Commit

Permalink
add unpin method
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 8, 2018
1 parent 3e6fe30 commit d902ab0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ go:
- "master"

script:
- make deps/fix
- make test
29 changes: 25 additions & 4 deletions hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ func (w *Wallet) Contains(account accounts.Account) bool {
return exists
}

// Unpin unpins account from list of pinned accounts.
func (w *Wallet) Unpin(account accounts.Account) error {
w.stateLock.RLock()
defer w.stateLock.RUnlock()

for i, acct := range w.accounts {
if acct.Address.String() == account.Address.String() {
w.accounts = removeAtIndex(w.accounts, i)
delete(w.paths, account.Address)
return nil
}
}

return errors.New("account not found")
}

// Derive implements accounts.Wallet, deriving a new account at the specific
// derivation path. If pin is set to true, the account will be added to the list
// of tracked accounts.
Expand Down Expand Up @@ -346,7 +362,7 @@ func NewSeed() ([]byte, error) {
return bip32.NewSeed()
}

// NewSeedFromMnemonic ...
// NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic.
func NewSeedFromMnemonic(mnemonic string) ([]byte, error) {
if mnemonic == "" {
return nil, errors.New("mnemonic is required")
Expand All @@ -357,7 +373,7 @@ func NewSeedFromMnemonic(mnemonic string) ([]byte, error) {
return seed, nil
}

// DerivePrivateKey derives the private key of the derivation path
// DerivePrivateKey derives the private key of the derivation path.
func (w *Wallet) derivePrivateKey(path accounts.DerivationPath) (*ecdsa.PrivateKey, error) {
var err error
key := w.masterKey
Expand All @@ -377,7 +393,7 @@ func (w *Wallet) derivePrivateKey(path accounts.DerivationPath) (*ecdsa.PrivateK
return privateKeyECDSA, nil
}

// DerivePublicKey derives the public key of the derivation path
// DerivePublicKey derives the public key of the derivation path.
func (w *Wallet) derivePublicKey(path accounts.DerivationPath) (*ecdsa.PublicKey, error) {
privateKeyECDSA, err := w.derivePrivateKey(path)
if err != nil {
Expand All @@ -393,7 +409,7 @@ func (w *Wallet) derivePublicKey(path accounts.DerivationPath) (*ecdsa.PublicKey
return publicKeyECDSA, nil
}

// DeriveAddress derives the account address of the derivation path
// DeriveAddress derives the account address of the derivation path.
func (w *Wallet) deriveAddress(path accounts.DerivationPath) (common.Address, error) {
publicKeyECDSA, err := w.derivePublicKey(path)
if err != nil {
Expand All @@ -403,3 +419,8 @@ func (w *Wallet) deriveAddress(path accounts.DerivationPath) (common.Address, er
address := crypto.PubkeyToAddress(*publicKeyECDSA)
return address, nil
}

// removeAtIndex removes an account at index.
func removeAtIndex(accts []accounts.Account, index int) []accounts.Account {
return append(accts[:index], accts[index+1:]...)
}
42 changes: 42 additions & 0 deletions hdwallet_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hdwallet

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -49,6 +50,19 @@ func TestWallet(t *testing.T) {
t.Error("expected 1")
}

if !wallet.Contains(account) {
t.Error("expected to contain account")
}

err = wallet.Unpin(account)
if err != nil {
t.Error(err)
}

if wallet.Contains(account) {
t.Error("expected to not contain account")
}

url := wallet.URL()
if url.String() != "" {
t.Error("expected empty url")
Expand Down Expand Up @@ -128,4 +142,32 @@ func TestWallet(t *testing.T) {
if account.Address.Hex() != "0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947" {
t.Error("wrong address")
}

seed, err = NewSeed()
if err != nil {
t.Error(err)
}

if len(seed) != 256 {
t.Error("expected size of 256")
}

seed, err = NewSeedFromMnemonic(mnemonic)
if err != nil {
t.Error(err)
}

if len(seed) != 64 {
t.Error("expected size of 64")
}

mnemonic, err = NewMnemonic()
if err != nil {
t.Error(err)
}

words := strings.Split(mnemonic, " ")
if len(words) != 12 {
t.Error("expected 12 words")
}
}

0 comments on commit d902ab0

Please sign in to comment.