Skip to content

Commit

Permalink
fix(gnoclient): Return error when getting keypair information (#2300)
Browse files Browse the repository at this point in the history
Relate to #2133

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Miloš Živković <[email protected]>
Co-authored-by: Manfred Touron <[email protected]>
  • Loading branch information
3 people authored Jun 20, 2024
1 parent 032d422 commit ed1601e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
36 changes: 18 additions & 18 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func TestRender(t *testing.T) {
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{
Expand Down Expand Up @@ -63,13 +63,13 @@ func TestCallSingle(t *testing.T) {
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{
Expand Down Expand Up @@ -117,13 +117,13 @@ func TestCallMultiple(t *testing.T) {
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{
Expand Down Expand Up @@ -482,13 +482,13 @@ func TestClient_Send_Errors(t *testing.T) {
name: "Invalid To Address",
client: Client{
Signer: &mockSigner{
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{},
Expand All @@ -512,13 +512,13 @@ func TestClient_Send_Errors(t *testing.T) {
name: "Invalid Send Coins",
client: Client{
Signer: &mockSigner{
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{},
Expand Down Expand Up @@ -561,13 +561,13 @@ func TestRunSingle(t *testing.T) {
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{
Expand Down Expand Up @@ -628,13 +628,13 @@ func TestRunMultiple(t *testing.T) {
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{
Expand Down Expand Up @@ -849,13 +849,13 @@ func TestRunErrors(t *testing.T) {
name: "Invalid Empty Package",
client: Client{
Signer: &mockSigner{
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{},
Expand Down Expand Up @@ -1040,13 +1040,13 @@ func TestAddPackageErrors(t *testing.T) {
name: "Invalid Empty Package",
client: Client{
Signer: &mockSigner{
info: func() keys.Info {
info: func() (keys.Info, error) {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
}, nil
},
},
RPCClient: &mockRPCClient{},
Expand Down
35 changes: 27 additions & 8 deletions gno.land/pkg/gnoclient/client_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTx
return nil, err
}

caller, err := c.Signer.Info()
if err != nil {
return nil, err
}

// Unwrap syntax sugar to vm.MsgCall slice
vmMsgs = append(vmMsgs, std.Msg(vm.MsgCall{
Caller: c.Signer.Info().GetAddress(),
Caller: caller.GetAddress(),
PkgPath: msg.PkgPath,
Func: msg.FuncName,
Args: msg.Args,
Expand Down Expand Up @@ -142,14 +147,17 @@ func (c *Client) Run(cfg BaseTxCfg, msgs ...MsgRun) (*ctypes.ResultBroadcastTxCo
return nil, err
}

caller := c.Signer.Info().GetAddress()
caller, err := c.Signer.Info()
if err != nil {
return nil, err
}

msg.Package.Name = "main"
msg.Package.Path = ""

// Unwrap syntax sugar to vm.MsgCall slice
vmMsgs = append(vmMsgs, std.Msg(vm.MsgRun{
Caller: caller,
Caller: caller.GetAddress(),
Package: msg.Package,
Send: send,
}))
Expand Down Expand Up @@ -201,9 +209,14 @@ func (c *Client) Send(cfg BaseTxCfg, msgs ...MsgSend) (*ctypes.ResultBroadcastTx
return nil, err
}

caller, err := c.Signer.Info()
if err != nil {
return nil, err
}

// Unwrap syntax sugar to vm.MsgSend slice
vmMsgs = append(vmMsgs, std.Msg(bank.MsgSend{
FromAddress: c.Signer.Info().GetAddress(),
FromAddress: caller.GetAddress(),
ToAddress: msg.ToAddress,
Amount: send,
}))
Expand Down Expand Up @@ -255,11 +268,14 @@ func (c *Client) AddPackage(cfg BaseTxCfg, msgs ...MsgAddPackage) (*ctypes.Resul
return nil, err
}

caller := c.Signer.Info().GetAddress()
caller, err := c.Signer.Info()
if err != nil {
return nil, err
}

// Unwrap syntax sugar to vm.MsgCall slice
vmMsgs = append(vmMsgs, std.Msg(vm.MsgAddPackage{
Creator: caller,
Creator: caller.GetAddress(),
Package: msg.Package,
Deposit: deposit,
}))
Expand All @@ -284,10 +300,13 @@ func (c *Client) AddPackage(cfg BaseTxCfg, msgs ...MsgAddPackage) (*ctypes.Resul

// signAndBroadcastTxCommit signs a transaction and broadcasts it, returning the result
func (c *Client) signAndBroadcastTxCommit(tx std.Tx, accountNumber, sequenceNumber uint64) (*ctypes.ResultBroadcastTxCommit, error) {
caller := c.Signer.Info().GetAddress()
caller, err := c.Signer.Info()
if err != nil {
return nil, err
}

if sequenceNumber == 0 || accountNumber == 0 {
account, _, err := c.QueryAccount(caller)
account, _, err := c.QueryAccount(caller.GetAddress())
if err != nil {
return nil, errors.Wrap(err, "query account")
}
Expand Down
6 changes: 3 additions & 3 deletions gno.land/pkg/gnoclient/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Signer mock
type (
mockSign func(cfg SignCfg) (*std.Tx, error)
mockInfo func() keys.Info
mockInfo func() (keys.Info, error)
mockValidate func() error
)

Expand All @@ -30,11 +30,11 @@ func (m *mockSigner) Sign(cfg SignCfg) (*std.Tx, error) {
return nil, nil
}

func (m *mockSigner) Info() keys.Info {
func (m *mockSigner) Info() (keys.Info, error) {
if m.info != nil {
return m.info()
}
return nil
return nil, nil
}

func (m *mockSigner) Validate() error {
Expand Down
15 changes: 10 additions & 5 deletions gno.land/pkg/gnoclient/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Signer provides an interface for signing transactions.
type Signer interface {
Sign(SignCfg) (*std.Tx, error) // Signs a transaction and returns a signed tx ready for broadcasting.
Info() keys.Info // Returns key information, including the address.
Info() (keys.Info, error) // Returns key information, including the address.
Validate() error // Checks whether the signer is properly configured.
}

Expand All @@ -35,9 +35,14 @@ func (s SignerFromKeybase) Validate() error {
return err
}

caller, err := s.Info()
if err != nil {
return err
}

// To verify if the password unlocks the account, sign a blank transaction.
msg := vm.MsgCall{
Caller: s.Info().GetAddress(),
Caller: caller.GetAddress(),
}
signCfg := SignCfg{
UnsignedTX: std.Tx{
Expand All @@ -53,12 +58,12 @@ func (s SignerFromKeybase) Validate() error {
}

// Info gets keypair information.
func (s SignerFromKeybase) Info() keys.Info {
func (s SignerFromKeybase) Info() (keys.Info, error) {
info, err := s.Keybase.GetByNameOrAddress(s.Account)
if err != nil {
panic("should not happen")
return nil, err
}
return info
return info, nil
}

// SignCfg provides the signing configuration, containing:
Expand Down

0 comments on commit ed1601e

Please sign in to comment.