Skip to content

Commit

Permalink
chore: Remove unused gnokey-mobile gRPC communication (#194)
Browse files Browse the repository at this point in the history
gnokey-mobile PR gnolang/gnokey-mobile#35
removed the unused gRPC server, since we now use deep links.
Importantly, the code to call functions like
`gnoNativeService.ClientSignTx` are removed. So we can remove these
functions in gnonative.

* In the `GnoNativeService` interface and `gnoNativeService` type,
remove `ClientGetRemote`, `ClientListKeyInfo` and `ClientSignTx` which
were only used by the Gnokey Mobile gRPC server.
* In the `Config` and `gnoNativeService` structs, remove
`useGnokeyMobile` flag and the code in api.go which does special
handling for this flag.
* In bridge.go and the related Swift and Kotlin files, remove support
for `StartGnokeyMobileService`. This allows us to remove the dependency
on `github.com/gnolang/gnokey-mobile/service`, so remove it from go.mod.
* Run `make regenerate` which updates files in api/gen/go, including
changes for the new buf version.

This PR was tested by running `make npm.pack` in the expo folder,
installing this package in gnokey-mobile, then testing with dsocial on
Android and iOS simulators.

(Note that examples/js/react-native/hello has a copies of files from the
expo folder. These are out-of-date for recent changes in the API,
including this PR. We will do a future PR to update the hello example.)

---------

Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 authored Dec 19, 2024
1 parent e4c8952 commit 0bb37cb
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 544 deletions.
572 changes: 255 additions & 317 deletions api/gen/go/gnonativetypes.pb.go

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions api/gen/go/rpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions expo/android/src/main/java/land/gno/gnonative/GnonativeModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ class GnonativeModule : Module() {
}
}

AsyncFunction("startGnokeyMobileService") { promise: Promise ->
try {
bridgeGnoNative?.let {
bridgeGnoNative!!.startGnokeyMobileService()
promise.resolve(true)
} ?: run {
throw GoBridgeNotStartedError()
}
} catch (err: CodedException) {
promise.reject(err)
}
}

AsyncFunction("invokeGrpcMethod") { method: String, jsonMessage: String, promise: Promise ->
try {
bridgeGnoNative?.let {
Expand Down
12 changes: 0 additions & 12 deletions expo/ios/GnonativeModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,6 @@ public class GnonativeModule: Module {
}
}

AsyncFunction("startGnokeyMobileService") { (promise: Promise) in
do {
guard let service = self.bridge else {
throw GnoError(.notStarted)
}
try service.startGnokeyMobileService()
promise.resolve(true)
} catch let error {
promise.reject(error)
}
}

AsyncFunction("closeBridge") { (promise: Promise) in
do {
guard let service = self.bridge else {
Expand Down
5 changes: 0 additions & 5 deletions expo/src/GoBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface GoBridgeInterface {
initBridge(): Promise<void>;
closeBridge(): Promise<void>;
getTcpPort(): Promise<number>;
startGnokeyMobileService(): Promise<void>;
invokeGrpcMethod(method: string, jsonMessage: string): Promise<string>;
createStreamClient(method: string, jsonMessage: string): Promise<string>;
streamClientReceive(id: string): Promise<string>;
Expand All @@ -24,10 +23,6 @@ class GoBridge implements GoBridgeInterface {
return GnonativeModule.getTcpPort();
}

startGnokeyMobileService(): Promise<void> {
return GnonativeModule.startGnokeyMobileService();
}

invokeGrpcMethod(method: string, jsonMessage: string): Promise<string> {
return GnonativeModule.invokeGrpcMethod(method, jsonMessage);
}
Expand Down
7 changes: 0 additions & 7 deletions expo/src/api/GnoNativeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
new SetChainIDRequest({ chainId: this.config.chain_id }),
);
console.log('✅ GnoNative bridge initialized.');
if (this.config.start_gnokey_mobile_service) {
await this.startGnokeyMobileService();
console.log('✅ Gnokey Mobile service started.');
}
return true;
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -472,9 +468,6 @@ export class GnoNativeApi implements GnoKeyApi, GoBridgeInterface {
getTcpPort(): Promise<number> {
return GoBridge.getTcpPort();
}
startGnokeyMobileService(): Promise<void> {
return GoBridge.startGnokeyMobileService();
}
invokeGrpcMethod(method: string, jsonMessage: string): Promise<string> {
return GoBridge.invokeGrpcMethod(method, jsonMessage);
}
Expand Down
2 changes: 0 additions & 2 deletions expo/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export enum BridgeStatus {
export interface Config {
remote: string;
chain_id: string;
// If true, initBridge also starts a Gnokey Mobile service. (Only needed for the Gnokey Mobile app.)
start_gnokey_mobile_service?: boolean;
}

export interface GnoKeyApi {
Expand Down
32 changes: 0 additions & 32 deletions framework/service/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/peterbourgon/unixtransport"
"go.uber.org/multierr"

gnokey_mobile_service "github.com/gnolang/gnokey-mobile/service"
api_gen "github.com/gnolang/gnonative/api/gen/go"
"github.com/gnolang/gnonative/api/gen/go/_goconnect"
"github.com/gnolang/gnonative/service"
Expand All @@ -24,7 +23,6 @@ type BridgeConfig struct {
TmpDir string
UseTcpListener bool
DisableUdsListener bool
UseGnokeyMobile bool
}

func NewBridgeConfig() *BridgeConfig {
Expand All @@ -40,8 +38,6 @@ type Bridge struct {

serviceServer service.GnoNativeService

gnokeyMobileService gnokey_mobile_service.GnokeyMobileService

ServiceClient
}

Expand Down Expand Up @@ -81,10 +77,6 @@ func NewBridge(config *BridgeConfig) (*Bridge, error) {
svcOpts = append(svcOpts, service.WithDisableUdsListener())
}

if config.UseGnokeyMobile {
svcOpts = append(svcOpts, service.WithUseGnokeyMobile())
}

serviceServer, err := service.NewGnoNativeService(svcOpts...)
if err != nil {
return nil, errors.Wrap(err, "unable to create bridge service")
Expand Down Expand Up @@ -160,28 +152,6 @@ func (b *Bridge) GetTcpAddr() string {
return b.serviceServer.GetTcpAddr()
}

// Start the Gnokey Mobile service and save it in gnokeyMobileService. This will be closed in Close().
// If the gnonative serviceServer is not started, do nothing.
// If gnokeyMobileService is already started, do nothing.
func (b *Bridge) StartGnokeyMobileService() error {
if b.serviceServer == nil {
return nil
}
if b.gnokeyMobileService != nil {
// Already started
return nil
}

// Use the default options
gnokeyMobileService, err := gnokey_mobile_service.NewGnokeyMobileService(b.serviceServer)
if err != nil {
return err
}

b.gnokeyMobileService = gnokeyMobileService
return nil
}

func (b *Bridge) Close() error {
var errs error

Expand All @@ -207,8 +177,6 @@ func (b *Bridge) Close() error {
errs = multierr.Append(errs, err)
}

// TODO: Close b.gnokeyMobileService

cancel()
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
connectrpc.com/grpchealth v1.3.0
connectrpc.com/grpcreflect v1.2.0
github.com/gnolang/gno v0.0.0-20241029141810-12bd8da50dc1
github.com/gnolang/gnokey-mobile v0.0.0-20241030095249-f1dc64684f11
github.com/oklog/run v1.1.0
github.com/peterbourgon/ff/v3 v3.4.0
github.com/peterbourgon/unixtransport v0.0.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gnolang/gno v0.0.0-20241029141810-12bd8da50dc1 h1:MCnDuYOD9MCO5AVN0eHXs9jrLWdHq5P5rYxR1xtQ4XI=
github.com/gnolang/gno v0.0.0-20241029141810-12bd8da50dc1/go.mod h1:vmnj2WrqRU9/FsHcRSXQdXzQPYYaYns/kYa+x+C0v14=
github.com/gnolang/gnokey-mobile v0.0.0-20241030095249-f1dc64684f11 h1:+vcuV5AEjgMJnRwTq/PodMDWl0Zhg6KkIN7FTFC6Ad0=
github.com/gnolang/gnokey-mobile v0.0.0-20241030095249-f1dc64684f11/go.mod h1:VDPyDoL7KOpjlhwmM8gO8aBwo04KbM23RbubKOGFDz8=
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 h1:GKvsK3oLWG9B1GL7WP/VqwM6C92j5tIvB844oggL9Lk=
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216/go.mod h1:xJhtEL7ahjM1WJipt89gel8tHzfIl/LyMY+lCYh38d8=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down
80 changes: 2 additions & 78 deletions service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,7 @@ func (s *gnoNativeService) SetRemote(ctx context.Context, req *connect.Request[a
}

func (s *gnoNativeService) GetRemote(ctx context.Context, req *connect.Request[api_gen.GetRemoteRequest]) (*connect.Response[api_gen.GetRemoteResponse], error) {
if s.useGnokeyMobile {
// Always get the remote from the Gnokey Mobile service
res, err := s.gnokeyMobileClient.GetRemote(context.Background(), req)
if err != nil {
return nil, err
}

return connect.NewResponse(res.Msg), nil
}

return connect.NewResponse(&api_gen.GetRemoteResponse{Remote: s.ClientGetRemote()}), nil
}

func (s *gnoNativeService) ClientGetRemote() string {
return s.remote
return connect.NewResponse(&api_gen.GetRemoteResponse{Remote: s.remote}), nil
}

func (s *gnoNativeService) SetChainID(ctx context.Context, req *connect.Request[api_gen.SetChainIDRequest]) (*connect.Response[api_gen.SetChainIDResponse], error) {
Expand Down Expand Up @@ -91,17 +77,7 @@ func ConvertKeyInfo(key crypto_keys.Info) (*api_gen.KeyInfo, error) {
func (s *gnoNativeService) ListKeyInfo(ctx context.Context, req *connect.Request[api_gen.ListKeyInfoRequest]) (*connect.Response[api_gen.ListKeyInfoResponse], error) {
s.logger.Debug("ListKeyInfo called")

if s.useGnokeyMobile {
// Always get the list of keys from the Gnokey Mobile service
res, err := s.gnokeyMobileClient.ListKeyInfo(context.Background(), req)
if err != nil {
return nil, err
}

return connect.NewResponse(res.Msg), nil
}

keys, err := s.ClientListKeyInfo()
keys, err := s.keybase.List()
if err != nil {
return nil, err
}
Expand All @@ -120,10 +96,6 @@ func (s *gnoNativeService) ListKeyInfo(ctx context.Context, req *connect.Request
return connect.NewResponse(&api_gen.ListKeyInfoResponse{Keys: formatedKeys}), nil
}

func (s *gnoNativeService) ClientListKeyInfo() ([]crypto_keys.Info, error) {
return s.keybase.List()
}

func (s *gnoNativeService) HasKeyByName(ctx context.Context, req *connect.Request[api_gen.HasKeyByNameRequest]) (*connect.Response[api_gen.HasKeyByNameResponse], error) {
s.logger.Debug("HasKeyByName called")

Expand Down Expand Up @@ -444,54 +416,6 @@ func (s *gnoNativeService) Call(ctx context.Context, req *connect.Request[api_ge
return err
}

if s.useGnokeyMobile {
tx, err := gnoclient.NewCallTx(*cfg, msgs...)
if err != nil {
return err
}
txJSON, err := amino.MarshalJSON(tx)
if err != nil {
return err
}

// Use Gnokey Mobile to sign.
// Note that req.Msg.CallerAddress must be set to the desired signer. The app can get the
// address using ListKeyInfo.
signedTxJSON, err := s.gnokeyMobileClient.SignTx(
context.Background(),
connect.NewRequest(&api_gen.SignTxRequest{
TxJson: string(txJSON),
Address: req.Msg.CallerAddress,
}),
)
if err != nil {
return err
}
signedTx := &std.Tx{}
if err := amino.UnmarshalJSON([]byte(signedTxJSON.Msg.SignedTxJson), signedTx); err != nil {
return err
}

// Now broadcast
c, err := s.getClient(nil)
if err != nil {
return getGrpcError(err)
}
bres, err := c.BroadcastTxCommit(signedTx)
if err != nil {
return getGrpcError(err)
}

if err := stream.Send(&api_gen.CallResponse{
Result: bres.DeliverTx.Data,
}); err != nil {
s.logger.Error("Call stream.Send returned error", zap.Error(err))
return err
}

return nil
}

signer, err := s.getSigner(req.Msg.CallerAddress)
if err != nil {
return err
Expand Down
11 changes: 0 additions & 11 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Config struct {
UdsPath string
UseTcpListener bool
DisableUdsListener bool
UseGnokeyMobile bool
}

type GnoNativeOption func(cfg *Config) error
Expand Down Expand Up @@ -328,16 +327,6 @@ var WithDisableUdsListener = func() GnoNativeOption {
}
}

// --- Gnokey Mobile options ---

// WithUseGnokeyMobile sets the gRPC service to use Gnokey Mobile for key-based operations.
var WithUseGnokeyMobile = func() GnoNativeOption {
return func(cfg *Config) error {
cfg.UseGnokeyMobile = true
return nil
}
}

// --- Fallback options ---

var defaults = []FallBackOption{
Expand Down
Loading

0 comments on commit 0bb37cb

Please sign in to comment.