bertybridge

package
v2.247.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2021 License: Apache-2.0, MIT Imports: 18 Imported by: 0

Documentation

Overview

Package bertybridge is the main gomobile entrypoint, used to generate iOS and Android frameworks.

Example
package main

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/gogo/protobuf/proto"

	"berty.tech/berty/v2/go/framework/bertybridge"
	"berty.tech/berty/v2/go/pkg/bertyaccount"
	bridge_svc "berty.tech/berty/v2/go/pkg/bertybridge"
	"berty.tech/berty/v2/go/pkg/protocoltypes"
)

func main() {
	tmpdir, err := ioutil.TempDir("", "example")
	checkErr(err)
	defer os.RemoveAll(tmpdir)

	// create and start the bridge
	var b *bertybridge.Bridge
	{
		config := bertybridge.NewConfig()
		if false { // disabled in example, but not commented to be sure that compiler performs various checks
			config.SetLifeCycleDriver(nil)
			config.SetLoggerDriver(nil)
			config.SetNotificationDriver(nil)
			config.SetRootDir(tmpdir)
		}

		b, err = bertybridge.NewBridge(config)
		checkErr(err)

		defer b.Close()
		fmt.Println("[+] initialized.")
	}

	args := []string{
		"--log.filters=info+:bty*,-*.grpc warn+:*.grpc error+:*",
		"--log.format=console",
		"--node.display-name=",
		"--node.listeners=/ip4/127.0.0.1/tcp/0/grpcws",
		"--p2p.swarm-listeners=/ip4/0.0.0.0/tcp/0,/ip6/::/tcp/0",
		"--p2p.local-discovery=false",
		"--p2p.webui-listener=:3000",
		"--store.dir=" + tmpdir,
	}

	// open account
	{
		// create `CreateAccount` Input
		input := &bertyaccount.CreateAccount_Request{Args: args}
		payload, err := proto.Marshal(input)
		checkErr(err)

		// Serialize request
		in := &bridge_svc.ClientInvokeUnary_Request{
			MethodDesc: &bridge_svc.MethodDesc{
				Name: "/berty.account.v1.AccountService/CreateAccount",
			},
			Payload: payload,
		}

		reqb64, err := encodeProtoMessage(in)
		checkErr(err)

		// invoke through bridge client
		ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
		checkErr(err)

		var output bridge_svc.ClientInvokeUnary_Reply
		err = decodeProtoMessage(ret, &output)
		checkErr(err)

		// deserialize reply
		var res bridge_svc.ClientInvokeUnary_Reply
		err = decodeProtoMessage(ret, &res)
		checkErr(err)

		fmt.Println("[+] account opened.")
	}

	// check for GRPC listeners
	{
		// create `InstanceGetConfiguration` Input
		input := &bertyaccount.GetGRPCListenerAddrs_Request{}
		payload, err := proto.Marshal(input)
		checkErr(err)

		// Serialize request
		in := &bridge_svc.ClientInvokeUnary_Request{
			MethodDesc: &bridge_svc.MethodDesc{
				Name: "/berty.account.v1.AccountService/GetGRPCListenerAddrs",
			},
			Payload: payload,
		}

		reqb64, err := encodeProtoMessage(in)
		checkErr(err)

		// invoke through bridge client
		ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
		checkErr(err)

		var output bridge_svc.ClientInvokeUnary_Reply
		err = decodeProtoMessage(ret, &output)
		checkErr(err)

		// deserialize reply
		var res bertyaccount.GetGRPCListenerAddrs_Reply
		err = proto.Unmarshal(output.Payload, &res)
		checkErr(err)

		hasGRPCWeb := false
		hasGRPCWebSocket := false
		for _, entry := range res.Entries {
			switch entry.Proto {
			case "ip4/tcp/grpcweb":
				hasGRPCWeb = true
			case "ip4/tcp/grpcws":
				hasGRPCWebSocket = true
			}
		}

		fmt.Println("[+] has grpc-web listener:           ", hasGRPCWeb)       // no, because `--node.listeners` does not contain grpcweb
		fmt.Println("[+] has websocket listener:          ", hasGRPCWebSocket) // yes, because `--node.listeners`` contains grpcws
	}

	// make unary call to underlying `BertyMessenger` Service
	{
		// create `InstanceGetConfiguration` Input
		input := &protocoltypes.InstanceGetConfiguration_Request{}
		payload, err := proto.Marshal(input)
		checkErr(err)

		// Serialize request
		in := &bridge_svc.ClientInvokeUnary_Request{
			MethodDesc: &bridge_svc.MethodDesc{
				Name: "/berty.protocol.v1.ProtocolService/InstanceGetConfiguration",
			},
			Payload: payload,
		}

		reqb64, err := encodeProtoMessage(in)
		checkErr(err)

		// invoke through bridge client
		ret, err := b.InvokeBridgeMethod("/berty.bridge.v1.BridgeService/ClientInvokeUnary", reqb64)
		checkErr(err)

		var output bridge_svc.ClientInvokeUnary_Reply
		err = decodeProtoMessage(ret, &output)
		checkErr(err)

		var res protocoltypes.InstanceGetConfiguration_Reply
		err = proto.Unmarshal(output.Payload, &res)
		checkErr(err)

		fmt.Println("[+] has more than one swarm listener:", len(res.Listeners) > 1)
		// log.Println("ret", godev.PrettyJSON(output))
	}

}

func checkErr(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "%+v\n", err)
		panic(err)
	}
}

func decodeProtoMessage(input string, output proto.Message) error {
	dec, err := base64.StdEncoding.DecodeString(input)
	if err != nil {
		return err
	}

	return proto.Unmarshal(dec, output)
}

func encodeProtoMessage(input proto.Message) (string, error) {
	data, err := proto.Marshal(input)
	if err != nil {
		return "", err
	}

	return base64.StdEncoding.EncodeToString(data), nil
}
Output:

[+] initialized.
[+] account opened.
[+] has grpc-web listener:            false
[+] has websocket listener:           true
[+] has more than one swarm listener: true

Index

Examples

Constants

View Source
const (
	AppStateUnknown int = iota
	AppStateActive
	AppStateInactive
	AppStateBackground
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bridge

type Bridge struct {
	// contains filtered or unexported fields
}

func NewBridge added in v2.190.1

func NewBridge(config *Config) (*Bridge, error)

func (*Bridge) Close

func (b *Bridge) Close() error

func (*Bridge) HandleState added in v2.190.1

func (b *Bridge) HandleState(appstate int)

func (*Bridge) HandleTask added in v2.190.1

func (b *Bridge) HandleTask() LifeCycleBackgroundTask

func (*Bridge) InvokeBridgeMethod added in v2.121.0

func (b *Bridge) InvokeBridgeMethod(method string, b64message string) (string, error)

func (*Bridge) InvokeBridgeMethodWithPromiseBlock added in v2.121.0

func (b *Bridge) InvokeBridgeMethodWithPromiseBlock(promise PromiseBlock, method string, b64message string)

func (*Bridge) WillTerminate added in v2.190.1

func (b *Bridge) WillTerminate()

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config is used to build a bertybridge configuration using only simple types or types returned by the bertybridge package.

func NewConfig

func NewConfig() *Config

func (*Config) AppendCLIArg added in v2.190.1

func (c *Config) AppendCLIArg(arg string)

func (*Config) SetLifeCycleDriver added in v2.190.1

func (c *Config) SetLifeCycleDriver(lc LifeCycleDriver)

func (*Config) SetLoggerDriver added in v2.190.1

func (c *Config) SetLoggerDriver(dLogger NativeLoggerDriver)

func (*Config) SetNotificationDriver added in v2.190.1

func (c *Config) SetNotificationDriver(driver NotificationDriver)

func (*Config) SetRootDir added in v2.202.0

func (c *Config) SetRootDir(rootdir string)

type LifeCycleBackgroundTask added in v2.124.0

type LifeCycleBackgroundTask interface {
	Execute() (success bool)
	Cancel()
}

type LifeCycleDriver added in v2.124.0

type LifeCycleDriver interface {
	GetCurrentState() int
	RegisterHandler(handler LifeCycleHandler)
}

type LifeCycleHandler added in v2.124.0

type LifeCycleHandler interface {
	HandleState(appstate int)
	HandleTask() LifeCycleBackgroundTask
	WillTerminate()
}

type LocalNotification added in v2.134.1

type LocalNotification struct {
	Title    string
	Body     string
	Interval float64
}

type NativeLoggerDriver

type NativeLoggerDriver interface {
	Log(level, namespace, message string) error
	LevelEnabler(level string) bool
}

type NotificationDriver added in v2.134.1

type NotificationDriver interface {
	Post(notif *LocalNotification) error
}

type PromiseBlock added in v2.121.0

type PromiseBlock interface {
	CallResolve(reply string)
	CallReject(error error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL