Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Dec 19, 2019
1 parent 5f41500 commit 250d601
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
56 changes: 56 additions & 0 deletions cosmos/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cosmos

import (
"sync"

"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/x/auth"
sdkauth "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/mesg-foundation/engine/x/xmath"
)

const (
// AccNumber is the account number of the account in keybase.
AccNumber = 0
// AccIndex is the account index of the account in keybase.
AccIndex = 0
)

type Account struct {
auth.Account

querier sdkauth.NodeQuerier
mx sync.Mutex
}

func NewAccount(acc auth.Account, querier sdkauth.NodeQuerier) *Account {
return &Account{Account: acc, querier: querier}
}

func NewAccountFromKeybase(kb keys.Keybase, name string, querier sdkauth.NodeQuerier) (*Account, error) {
info, err := kb.Get(name)
if err != nil {
return nil, err
}
acc := auth.NewBaseAccount(
info.GetAddress(),
nil,
info.GetPubKey(),
AccNumber,
0,
)

return &Account{Account: acc, querier: querier}, nil
}

func (a *Account) GetSequence() uint64 {
a.mx.Lock()
seq := a.Account.GetSequence()
if acc, err := auth.NewAccountRetriever(a.querier).GetAccount(a.GetAddress()); err == nil {
seq = xmath.MaxUint64(seq, acc.GetSequence())
a.Account = acc
}
a.SetSequence(seq + 1)
a.mx.Unlock()
return seq
}
28 changes: 0 additions & 28 deletions cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,6 @@ func (c *Client) Stream(ctx context.Context, query string) (chan hash.Hash, chan
return hashC, errC, nil
}

// GetAccount returns the local account.
func (c *Client) GetAccount() (auth.Account, error) {
c.getAccountMutex.Lock()
defer c.getAccountMutex.Unlock()
if c.acc == nil {
accKb, err := c.kb.Get(c.accName)
if err != nil {
return nil, err
}
c.acc = auth.NewBaseAccount(
accKb.GetAddress(),
nil,
accKb.GetPubKey(),
AccNumber,
0,
)
}
localSeq := c.acc.GetSequence()
if accR, err := auth.NewAccountRetriever(c).GetAccount(c.acc.GetAddress()); err == nil {
c.acc = accR
// replace seq if sup
if localSeq > c.acc.GetSequence() {
c.acc.SetSequence(localSeq)
}
}
return c.acc, nil
}

// Sign signs a msg and return a tendermint tx.
func (c *Client) sign(msg sdktypes.Msg) (tenderminttypes.Tx, error) {
acc, err := c.GetAccount()
Expand Down
9 changes: 1 addition & 8 deletions cosmos/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto"
)

const (
// AccNumber is the account number of the account in keybase.
AccNumber = 0
// AccIndex is the account index of the account in keybase.
AccIndex = 0

mnemonicEntropySize = 256
)
const mnemonicEntropySize = 256

// Keybase is a standard cosmos keybase.
type Keybase struct {
Expand Down
11 changes: 11 additions & 0 deletions x/xmath/dim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xmath

// MaxUint64 returns the larger of a and b...
func MaxUint64(a uint64, b ...uint64) uint64 {
for _, i := range b {
if i > a {
a = i
}
}
return a
}

0 comments on commit 250d601

Please sign in to comment.