-
Notifications
You must be signed in to change notification settings - Fork 250
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
Add initial gomobile support #1164
Changes from 3 commits
7a05ce6
94b9880
041990b
f2610b5
6fa22db
b178f25
0718c78
606ddfd
a628bd2
6e217ef
43d965e
6c68d39
c4063f0
55a28cb
9d76ebe
275d35d
1c6a332
7dc20ae
4cb9885
719711e
0f55f56
4105b09
af58279
0b92aa6
8177afa
145192d
103eb85
648ba84
2f65110
5866167
71669fe
ff42934
3bccb96
441bd5a
bf2c932
523d38b
a433f7a
a56a3a1
1524e29
57493f4
6c8ac96
2b52042
7577690
e6452b4
f4e64c0
7614f89
cd5f884
0468ac3
ed70137
8f142b9
e7580e5
12f459b
63ad571
44e373a
09c9025
3553a48
0e0f7f4
bd32400
0e2aeba
820e398
c787063
bd8285d
ae68018
a0141f6
e1fb48e
a88795b
3395494
4a5ef3a
1fa9103
5634b92
47557cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Mobile | ||
|
||
Package mobile implements [gomobile](https://github.com/golang/mobile) bindings for status-go. Current implementation servers as a drop-in replacement for `lib` package. | ||
|
||
# Usage | ||
|
||
For properly using this package, please refer to Makefile in the root of `status-go` directory. | ||
|
||
To manually build library, run following commands: | ||
|
||
### iOS | ||
|
||
``` | ||
gomobile bind -v -target=ios -ldflags="-s -w" github.com/status-im/status-go/mobile | ||
``` | ||
This will produce `Status.framework` file in the current directory, which can be used in iOS project. | ||
|
||
### Android | ||
|
||
``` | ||
gomobile bind -v -target=android -ldflags="-s -w" github.com/status-im/status-go/mobile | ||
``` | ||
This will generate `Status.aar` file in the current dir. | ||
|
||
# Notes | ||
|
||
See [https://github.com/golang/go/wiki/Mobile](https://github.com/golang/go/wiki/Mobile) for more information on `gomobile` usage. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can either move it to It's an issue that we have so many response types but this one was introduced for bindings that are meant to replace JSON-RPC calls. In status-react, commands like |
||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/status-im/status-go/account" | ||
"github.com/status-im/status-go/transactions" | ||
) | ||
|
||
const ( | ||
codeUnknown int = iota | ||
// special codes | ||
codeFailedParseResponse | ||
codeFailedParseParams | ||
// account related codes | ||
codeErrNoAccountSelected | ||
codeErrInvalidTxSender | ||
codeErrDecrypt | ||
) | ||
|
||
var errToCodeMap = map[error]int{ | ||
account.ErrNoAccountSelected: codeErrNoAccountSelected, | ||
transactions.ErrInvalidTxSender: codeErrInvalidTxSender, | ||
keystore.ErrDecrypt: codeErrDecrypt, | ||
} | ||
|
||
type jsonrpcSuccessfulResponse struct { | ||
Result interface{} `json:"result"` | ||
} | ||
|
||
type jsonrpcErrorResponse struct { | ||
Error jsonError `json:"error"` | ||
} | ||
|
||
type jsonError struct { | ||
Code int `json:"code,omitempty"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func prepareJSONResponse(result interface{}, err error) string { | ||
code := codeUnknown | ||
if c, ok := errToCodeMap[err]; ok { | ||
code = c | ||
} | ||
|
||
return prepareJSONResponseWithCode(result, err, code) | ||
} | ||
|
||
func prepareJSONResponseWithCode(result interface{}, err error, code int) string { | ||
if err != nil { | ||
errResponse := jsonrpcErrorResponse{ | ||
Error: jsonError{Code: code, Message: err.Error()}, | ||
} | ||
response, _ := json.Marshal(&errResponse) | ||
return string(response) | ||
} | ||
|
||
data, err := json.Marshal(jsonrpcSuccessfulResponse{result}) | ||
if err != nil { | ||
return prepareJSONResponseWithCode(nil, err, codeFailedParseResponse) | ||
} | ||
return string(data) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package status | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type nonJSON struct{} | ||
|
||
func (*nonJSON) MarshalJSON() ([]byte, error) { | ||
return nil, errors.New("invalid JSON") | ||
} | ||
|
||
func TestPrepareJSONResponseErrorWithResult(t *testing.T) { | ||
data := prepareJSONResponse("0x123", nil) | ||
require.Equal(t, `{"result":"0x123"}`, data) | ||
|
||
data = prepareJSONResponse(&nonJSON{}, nil) | ||
require.Contains(t, data, `{"error":{"code":1,"message":`) | ||
} | ||
|
||
func TestPrepareJSONResponseErrorWithError(t *testing.T) { | ||
data := prepareJSONResponse("0x123", errors.New("some error")) | ||
require.Contains(t, data, `{"error":{"message":"some error"}}`) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gomobile-install
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sense, thanks!