-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |