安装

可参考如下链接:

编写 go 函数

有如下注意点:

  • 导出的函数首字母应大写
  • 导出函数的参数和返回值是基础类型,不支持 []string,可以支持 []byte
  • 返回值只能有一个,如果有多个返回值,可以返回一个 struct 的指针,struct 的成员是基础类型。需要为该 struct 编写类似 func (result MpcExecResult) ToJson() string 的方法,前端调用时先调用返回的 struct 的 ToJson() 方法,然后再转为 map
  • 文件夹内的函数不会被导出,只有执行编译命令 gomobile bind -target=android . 所在目录下的文件中的函数会被导出

一个例子如下:

package hwallet

//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
import "C"

import (
    "fmt"
    "encoding/json"
    "hwallet/tss/eddsacmp/keygen"
    "hwallet/tss/eddsacmp/onsign"
)

type MpcExecResult struct {
    Ok           bool   `json:"ok"`
    Err          string `json:"error"`
    MsgWireBytes []byte `json:"data"`
}

func (result MpcExecResult) ToJson() string {
    b, _ := json.Marshal(result)
    return string(b)
}

var Parties = map[string]*keygen.LocalParty{}

func NewLocalParty(
    key string,
    partyIndex int,
) {
    p := &keygen.LocalParty{
        PartyIndex: partyIndex
    }
    Parties[key] = p
}

func KeygenRound1Exec(key string) *MpcExecResult {
    party, ok := Parties[key]
    if !ok {
        return &MpcExecResult{
            Ok:  false,
            Err: fmt.Sprintf("party not found: %s", key),
        }
    }

    res := keygen.KeygenRound1Exec(key)
    return &MpcExecResult{
        Ok:           res.Ok,
        Err:          res.Err,
        MsgWireBytes: res.MsgWireBytes,
    }
}

编译

gomobile init

gomobile bind -target=android .

# 27 is version
# ls Android/Sdk/ndk/
# 27.0.11718014
# gomobile bind -androidapi 27 -target=android .

# gomobile bind -target=ios .