Skip to content

Commit

Permalink
Add support for partitioned topic
Browse files Browse the repository at this point in the history
  • Loading branch information
cammellos committed Jun 19, 2019
1 parent 78ed35d commit 1aa3e28
Show file tree
Hide file tree
Showing 40 changed files with 2,409 additions and 1,741 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ setup: setup-build setup-dev tidy ##@other Prepare project for development and b

generate: ##@other Regenerate assets and other auto-generated stuff
go generate ./static ./static/chat_db_migrations ./static/mailserver_db_migrations ./t
$(shell cd ./services/shhext/chat && exec protoc --go_out=. ./*.proto)
$(shell cd ./services/shhext/chat/protobuf && exec protoc --go_out=. ./*.proto)

prepare-release: clean-release
mkdir -p $(RELEASE_DIR)
Expand Down
100 changes: 22 additions & 78 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand All @@ -26,8 +25,8 @@ import (
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/personal"
"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 @@ -645,104 +644,49 @@ func appendIf(condition bool, services []gethnode.ServiceConstructor, service ge
return append(services, service)
}

// CreateContactCode create or return the latest contact code
func (b *StatusBackend) CreateContactCode() (string, error) {
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
if err != nil {
return "", err
}

st, err := b.statusNode.ShhExtService()
if err != nil {
return "", err
}

bundle, err := st.GetBundle(selectedChatAccount.AccountKey.PrivateKey)
if err != nil {
return "", err
}

return bundle.ToBase64()
// ExtractGroupMembershipSignatures extract signatures from tuples of content/signature
func (b *StatusBackend) ExtractGroupMembershipSignatures(signaturePairs [][2]string) ([]string, error) {
return crypto.ExtractSignatures(signaturePairs)
}

// GetContactCode return the latest contact code
func (b *StatusBackend) GetContactCode(identity string) (string, error) {
st, err := b.statusNode.ShhExtService()
if err != nil {
return "", err
}

publicKeyBytes, err := hex.DecodeString(identity)
if err != nil {
return "", err
}

publicKey, err := ethcrypto.UnmarshalPubkey(publicKeyBytes)
if err != nil {
return "", err
}

bundle, err := st.GetPublicBundle(publicKey)
// SignGroupMembership signs a piece of data containing membership information
func (b *StatusBackend) SignGroupMembership(content string) (string, error) {
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
if err != nil {
return "", err
}

if bundle == nil {
return "", nil
}

return bundle.ToBase64()
return crypto.Sign(content, selectedChatAccount.AccountKey.PrivateKey)
}

// ProcessContactCode process and adds the someone else's bundle
func (b *StatusBackend) ProcessContactCode(contactCode string) error {
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
if err != nil {
return err
}

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

bundle, err := chat.FromBase64(contactCode)
if err != nil {
b.log.Error("error decoding base64", "err", err)
return err
}

if _, err := st.ProcessPublicBundle(selectedChatAccount.AccountKey.PrivateKey, bundle); err != nil {
b.log.Error("error adding bundle", "err", err)
return err
return nil, err
}

return nil
return st.LoadFilters(chats)
}

// ExtractIdentityFromContactCode extract the identity of the user generating the contact code
func (b *StatusBackend) ExtractIdentityFromContactCode(contactCode string) (string, error) {
bundle, err := chat.FromBase64(contactCode)
// LoadFilter loads filter on sshext
func (b *StatusBackend) LoadFilter(chat *filter.Chat) ([]*filter.Chat, error) {
st, err := b.statusNode.ShhExtService()
if err != nil {
return "", err
return nil, err
}

return chat.ExtractIdentity(bundle)
}

// ExtractGroupMembershipSignatures extract signatures from tuples of content/signature
func (b *StatusBackend) ExtractGroupMembershipSignatures(signaturePairs [][2]string) ([]string, error) {
return crypto.ExtractSignatures(signaturePairs)
return st.LoadFilter(chat)
}

// SignGroupMembership signs a piece of data containing membership information
func (b *StatusBackend) SignGroupMembership(content string) (string, error) {
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
// RemoveFilter remove a filter
func (b *StatusBackend) RemoveFilter(chat *filter.Chat) error {
st, err := b.statusNode.ShhExtService()
if err != nil {
return "", err
return err
}

return crypto.Sign(content, selectedChatAccount.AccountKey.PrivateKey)
return st.RemoveFilter(chat)
}

// EnableInstallation enables an installation for multi-device sync.
Expand Down
64 changes: 37 additions & 27 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 @@ -51,63 +52,72 @@ func StopNode() *C.char {
return makeJSONResponse(nil)
}

// Create an X3DH bundle
//export CreateContactCode
func CreateContactCode() *C.char {
bundle, err := statusBackend.CreateContactCode()
if err != nil {
// 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)
}

cstr := C.CString(bundle)

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

//export ProcessContactCode
func ProcessContactCode(bundleString *C.char) *C.char {
err := statusBackend.ProcessContactCode(C.GoString(bundleString))
data, err := json.Marshal(struct {
Chats []*filter.Chat `json:"result"`
}{Chats: response})
if err != nil {
return makeJSONResponse(err)
}

return nil
return C.CString(string(data))
}

// Get an X3DH bundle
//export GetContactCode
func GetContactCode(identityString *C.char) *C.char {
bundle, err := statusBackend.GetContactCode(C.GoString(identityString))
// 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 {
ContactCode string `json:"code"`
}{ContactCode: bundle})
Chats []*filter.Chat `json:"result"`
}{Chats: response})

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

return C.CString(string(data))
}

//export ExtractIdentityFromContactCode
func ExtractIdentityFromContactCode(bundleString *C.char) *C.char {
bundle := C.GoString(bundleString)
// RemoveFilter load a whisper filter
//export RemoveFilter
func RemoveFilter(chatStr *C.char) *C.char {
var chat *filter.Chat

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

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

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

0 comments on commit 1aa3e28

Please sign in to comment.