Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnoclient): Return error when getting keypair information #2300

Merged
merged 26 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
78c6ddd
return error when getting unexisted key info
linhpn99 Jun 7, 2024
1da9e4f
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 7, 2024
b87773e
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 8, 2024
e6e300f
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 11, 2024
1f98fda
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 11, 2024
5da14e2
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 11, 2024
d9999b7
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 12, 2024
9c1ecc9
Update gno.land/pkg/gnoclient/mock_test.go
linhpn99 Jun 12, 2024
43976c2
Update mock_test.go
linhpn99 Jun 12, 2024
f07b27f
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 13, 2024
1784be2
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 13, 2024
68490e7
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 13, 2024
89ed70e
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 14, 2024
d9422e7
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 14, 2024
3251b24
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 14, 2024
bee5fdb
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 17, 2024
6123a74
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 17, 2024
8437497
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 19, 2024
fc44c29
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 19, 2024
de2f038
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 19, 2024
013f154
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 19, 2024
9cd5bef
Merge branch 'master' into fix/dont-panic-signer
moul Jun 19, 2024
880a9c4
Merge branch 'master' into fix/dont-panic-signer
moul Jun 19, 2024
9cf7b51
Merge branch 'master' into fix/dont-panic-signer
linhpn99 Jun 20, 2024
61a2706
fix wrong module path
linhpn99 Jun 20, 2024
9c8231c
Merge branch 'master' into fix/dont-panic-signer
thehowl Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
return nil, err
}

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

Check warning on line 91 in gno.land/pkg/gnoclient/client_txs.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L91

Added line #L91 was not covered by tests
}

// 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 @@
return nil, err
}

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

Check warning on line 152 in gno.land/pkg/gnoclient/client_txs.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L152

Added line #L152 was not covered by tests
}

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 @@
return nil, err
}

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

Check warning on line 214 in gno.land/pkg/gnoclient/client_txs.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L214

Added line #L214 was not covered by tests
}

// 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 @@
return nil, err
}

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

Check warning on line 273 in gno.land/pkg/gnoclient/client_txs.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L273

Added line #L273 was not covered by tests
}

// 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 @@

// 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

Check warning on line 305 in gno.land/pkg/gnoclient/client_txs.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_txs.go#L305

Added line #L305 was not covered by tests
}

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
7 changes: 4 additions & 3 deletions gno.land/pkg/gnoclient/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/hd"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
)

// 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 +31,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, errors.New("empty key info")
linhpn99 marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@
// 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 @@
return err
}

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

Check warning on line 40 in gno.land/pkg/gnoclient/signer.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L38-L40

Added lines #L38 - L40 were not covered by tests
}

// To verify if the password unlocks the account, sign a blank transaction.
msg := vm.MsgCall{
Caller: s.Info().GetAddress(),
Caller: caller.GetAddress(),

Check warning on line 45 in gno.land/pkg/gnoclient/signer.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L45

Added line #L45 was not covered by tests
}
signCfg := SignCfg{
UnsignedTX: std.Tx{
Expand All @@ -53,12 +58,12 @@
}

// 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

Check warning on line 64 in gno.land/pkg/gnoclient/signer.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/signer.go#L64

Added line #L64 was not covered by tests
}
return info
return info, nil
}

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