-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multiple accounts (backport) (#3504)
- Loading branch information
Showing
7 changed files
with
1,467 additions
and
463 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
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,79 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type Account struct { | ||
name string | ||
address types.AccAddress | ||
pubKey cryptotypes.PubKey | ||
accountNumber uint64 | ||
|
||
// the signers local view of the sequence number | ||
sequence uint64 | ||
} | ||
|
||
func NewAccount(keyName string, accountNumber, sequenceNumber uint64) *Account { | ||
return &Account{ | ||
name: keyName, | ||
accountNumber: accountNumber, | ||
sequence: sequenceNumber, | ||
} | ||
} | ||
|
||
func (a Account) Name() string { | ||
return a.name | ||
} | ||
|
||
func (a Account) Address() types.AccAddress { | ||
return a.address | ||
} | ||
|
||
func (a Account) PubKey() cryptotypes.PubKey { | ||
return a.pubKey | ||
} | ||
|
||
// Sequence returns the sequence number of the account. | ||
// This is locally tracked | ||
func (a Account) Sequence() uint64 { | ||
return a.sequence | ||
} | ||
|
||
func (a *Account) Copy() *Account { | ||
return &Account{ | ||
name: a.name, | ||
address: a.address, | ||
pubKey: a.pubKey, | ||
accountNumber: a.accountNumber, | ||
sequence: a.sequence, | ||
} | ||
} | ||
|
||
// QueryAccountInfo fetches the account number and sequence number from the celestia-app node. | ||
func QueryAccountInfo(ctx context.Context, conn *grpc.ClientConn, registry codectypes.InterfaceRegistry, address types.AccAddress) (accNum uint64, seqNum uint64, err error) { | ||
qclient := authtypes.NewQueryClient(conn) | ||
// TODO: ideally we add a way to prove that the accounts rather than simply trusting the full node we are connected with | ||
resp, err := qclient.Account( | ||
ctx, | ||
&authtypes.QueryAccountRequest{Address: address.String()}, | ||
) | ||
if err != nil { | ||
return accNum, seqNum, err | ||
} | ||
|
||
var acc authtypes.AccountI | ||
err = registry.UnpackAny(resp.Account, &acc) | ||
if err != nil { | ||
return accNum, seqNum, err | ||
} | ||
|
||
accNum, seqNum = acc.GetAccountNumber(), acc.GetSequence() | ||
return accNum, seqNum, nil | ||
} |
Oops, something went wrong.