Skip to content

Commit

Permalink
Addl library functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cammellos committed May 24, 2019
1 parent e445fae commit 860792c
Show file tree
Hide file tree
Showing 8 changed files with 509 additions and 205 deletions.
31 changes: 31 additions & 0 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/status-im/status-go/services/rpcfilters"
"github.com/status-im/status-go/services/shhext/chat"
"github.com/status-im/status-go/services/shhext/chat/crypto"
"github.com/status-im/status-go/services/shhext/filter"
"github.com/status-im/status-go/services/subscriptions"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/signal"
Expand Down Expand Up @@ -715,6 +716,36 @@ func (b *StatusBackend) SignGroupMembership(content string) (string, error) {
return crypto.Sign(content, selectedChatAccount.AccountKey.PrivateKey)
}

// LoadFilters loads filter on sshext
func (b *StatusBackend) LoadFilters(chats []*filter.Chat) ([]*filter.Chat, error) {
st, err := b.statusNode.ShhExtService()
if err != nil {
return nil, err
}

return st.LoadFilters(chats)
}

// LoadFilter loads filter on sshext
func (b *StatusBackend) LoadFilter(chat *filter.Chat) ([]*filter.Chat, error) {
st, err := b.statusNode.ShhExtService()
if err != nil {
return nil, err
}

return st.LoadFilter(chat)
}

// RemoveFilter remove a filter
func (b *StatusBackend) RemoveFilter(chat *filter.Chat) error {
st, err := b.statusNode.ShhExtService()
if err != nil {
return err
}

return st.RemoveFilter(chat)
}

// EnableInstallation enables an installation for multi-device sync.
func (b *StatusBackend) EnableInstallation(installationID string) error {
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
Expand Down
74 changes: 74 additions & 0 deletions lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling"
"github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/services/shhext/filter"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions"
Expand Down Expand Up @@ -115,6 +116,79 @@ func ExtractIdentityFromContactCode(bundleString *C.char) *C.char {
return C.CString(string(data))
}

// LoadFilters load all whisper filters
//export LoadFilters
func LoadFilters(chatsStr *C.char) *C.char {
var chats []*filter.Chat

if err := json.Unmarshal([]byte(C.GoString(chatsStr)), &chats); err != nil {
return makeJSONResponse(err)
}

response, err := statusBackend.LoadFilters(chats)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Chats []*filter.Chat `json:"result"`
}{Chats: response})
if err != nil {
return makeJSONResponse(err)
}

return C.CString(string(data))
}

// LoadFilter load a whisper filter
//export LoadFilter
func LoadFilter(chatStr *C.char) *C.char {
var chat *filter.Chat

if err := json.Unmarshal([]byte(C.GoString(chatStr)), &chat); err != nil {
return makeJSONResponse(err)
}

response, err := statusBackend.LoadFilter(chat)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Chats []*filter.Chat `json:"result"`
}{Chats: response})

if err != nil {
return makeJSONResponse(err)
}

return C.CString(string(data))
}

// RemoveFilter load a whisper filter
//export RemoveFilter
func RemoveFilter(chatStr *C.char) *C.char {
var chat *filter.Chat

if err := json.Unmarshal([]byte(C.GoString(chatStr)), &chat); err != nil {
return makeJSONResponse(err)
}

err := statusBackend.RemoveFilter(chat)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Response string `json:"response"`
}{Response: "ok"})
if err != nil {
return makeJSONResponse(err)
}

return C.CString(string(data))
}

// ExtractGroupMembershipSignatures extract public keys from tuples of content/signature
//export ExtractGroupMembershipSignatures
func ExtractGroupMembershipSignatures(signaturePairsStr *C.char) *C.char {
Expand Down
71 changes: 71 additions & 0 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/profiling"
"github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/services/shhext/filter"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions"
Expand Down Expand Up @@ -673,3 +674,73 @@ func SignHash(hexEncodedHash string) string {

return hexEncodedSignature
}

// LoadFilters load all whisper filters
func LoadFilters(chatsStr string) string {
var chats []*filter.Chat

if err := json.Unmarshal([]byte(chatsStr), &chats); err != nil {
return makeJSONResponse(err)
}

response, err := statusBackend.LoadFilters(chats)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Chats []*filter.Chat `json:"result"`
}{Chats: response})
if err != nil {
return makeJSONResponse(err)
}

return string(data)
}

// LoadFilter load a whisper filter
func LoadFilter(chatStr string) string {
var chat *filter.Chat

if err := json.Unmarshal([]byte(chatStr), &chat); err != nil {
return makeJSONResponse(err)
}

response, err := statusBackend.LoadFilter(chat)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Chats []*filter.Chat `json:"result"`
}{Chats: response})
if err != nil {
return makeJSONResponse(err)
}

return string(data)
}

// RemoveFilter load a whisper filter
//export RemoveFilter
func RemoveFilter(chatStr string) string {
var chat *filter.Chat

if err := json.Unmarshal([]byte(chatStr), &chat); err != nil {
return makeJSONResponse(err)
}

err := statusBackend.RemoveFilter(chat)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
Response string `json:"response"`
}{Response: "ok"})
if err != nil {
return makeJSONResponse(err)
}

return string(data)
}
34 changes: 10 additions & 24 deletions services/shhext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,32 +472,14 @@ func (api *PublicAPI) ConfirmMessagesProcessedByID(messageIDs [][]byte) error {

// SendPublicMessage sends a public chat message to the underlying transport
func (api *PublicAPI) SendPublicMessage(ctx context.Context, msg chat.SendPublicMessageRPC) (hexutil.Bytes, error) {
privateKey, err := api.service.w.GetPrivateKey(msg.Sig)
if err != nil {
return nil, err
}

// This is transport layer agnostic
protocolMessage, err := api.service.protocol.BuildPublicMessage(privateKey, msg.Payload)
if err != nil {
return nil, err
}

symKeyID, err := api.service.w.AddSymKeyFromPassword(msg.Chat)
if err != nil {
return nil, err
}

// marshal for sending to wire
marshaledMessage, err := proto.Marshal(protocolMessage)
if err != nil {
api.log.Error("encryption-service", "error marshaling message", err)
return nil, err
filter := api.service.filter.GetByID(msg.Chat)
if filter == nil {
return nil, errors.New("not subscribed to chat")
}

// Enrich with transport layer info
whisperMessage := chat.PublicMessageToWhisper(msg, marshaledMessage)
whisperMessage.SymKeyID = symKeyID
whisperMessage := chat.PublicMessageToWhisper(msg, msg.Payload)
whisperMessage.SymKeyID = filter.SymKeyID

// And dispatch
return api.Post(ctx, whisperMessage)
Expand Down Expand Up @@ -560,10 +542,14 @@ func (api *PublicAPI) SendDirectMessage(ctx context.Context, msg chat.SendDirect

if chat != nil {
whisperMessage.SymKeyID = chat.SymKeyID
whisperMessage.Topic = whisper.BytesToTopic(chat.Topic)
whisperMessage.Topic = chat.Topic
whisperMessage.PublicKey = nil
}
} else if partitionedTopicSupported {
// Create filter on demand
if _, err := api.service.filter.LoadPartitioned(privateKey, publicKey, false); err != nil {
return nil, err
}
t := filter.PublicKeyToPartitionedTopicBytes(publicKey)
whisperMessage.Topic = whisper.BytesToTopic(t)

Expand Down
Loading

0 comments on commit 860792c

Please sign in to comment.