Skip to content

Commit

Permalink
Move multidevice logic to its own namespace
Browse files Browse the repository at this point in the history
Multidevice has been extracted from the encryption logic in order to
make things clearer.

Some old endpoint have also been removed as not used anymore.
  • Loading branch information
cammellos committed May 31, 2019
1 parent b71e76e commit d38d3a3
Show file tree
Hide file tree
Showing 25 changed files with 1,005 additions and 1,045 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ setup: setup-build setup-dev ##@other Prepare project for development and buildi

generate: ##@other Regenerate assets and other auto-generated stuff
go generate ./static ./static/chat_db_migrations ./static/mailserver_db_migrations
$(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
87 changes: 0 additions & 87 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 @@ -25,7 +24,6 @@ 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"
Expand Down Expand Up @@ -616,91 +614,6 @@ 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()
}

// 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)
if err != nil {
return "", err
}

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

return bundle.ToBase64()
}

// 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
}

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
}

// ExtractIdentityFromContactCode extract the identity of the user generating the contact code
func (b *StatusBackend) ExtractIdentityFromContactCode(contactCode string) (string, error) {
bundle, err := chat.FromBase64(contactCode)
if err != nil {
return "", 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)
Expand Down
64 changes: 0 additions & 64 deletions lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,70 +52,6 @@ func StopNode() *C.char {
return makeJSONResponse(nil)
}

// Create an X3DH bundle
//export CreateContactCode
func CreateContactCode() *C.char {
bundle, err := statusBackend.CreateContactCode()
if err != nil {
return makeJSONResponse(err)
}

cstr := C.CString(bundle)

return cstr
}

//export ProcessContactCode
func ProcessContactCode(bundleString *C.char) *C.char {
err := statusBackend.ProcessContactCode(C.GoString(bundleString))
if err != nil {
return makeJSONResponse(err)
}

return nil
}

// Get an X3DH bundle
//export GetContactCode
func GetContactCode(identityString *C.char) *C.char {
bundle, err := statusBackend.GetContactCode(C.GoString(identityString))
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
ContactCode string `json:"code"`
}{ContactCode: bundle})
if err != nil {
return makeJSONResponse(err)
}

return C.CString(string(data))
}

//export ExtractIdentityFromContactCode
func ExtractIdentityFromContactCode(bundleString *C.char) *C.char {
bundle := C.GoString(bundleString)

identity, err := statusBackend.ExtractIdentityFromContactCode(bundle)
if err != nil {
return makeJSONResponse(err)
}

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

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

return C.CString(string(data))
}

// LoadFilters load all whisper filters
//export LoadFilters
func LoadFilters(chatsStr *C.char) *C.char {
Expand Down
60 changes: 0 additions & 60 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,48 +65,6 @@ func StopNode() string {
return makeJSONResponse(nil)
}

// CreateContactCode creates an X3DH bundle.
func CreateContactCode() string {
bundle, err := statusBackend.CreateContactCode()
if err != nil {
return makeJSONResponse(err)
}

return bundle
}

// ProcessContactCode processes an X3DH bundle.
// TODO(adam): it looks like the return should be error.
func ProcessContactCode(bundle string) string {
err := statusBackend.ProcessContactCode(bundle)
if err != nil {
return makeJSONResponse(err)
}

return ""
}

// ExtractIdentityFromContactCode extracts an identity from an X3DH bundle.
func ExtractIdentityFromContactCode(bundle string) string {
identity, err := statusBackend.ExtractIdentityFromContactCode(bundle)
if err != nil {
return makeJSONResponse(err)
}

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

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

return string(data)
}

// ExtractGroupMembershipSignatures extract public keys from tuples of content/signature.
func ExtractGroupMembershipSignatures(signaturePairsStr string) string {
var signaturePairs [][2]string
Expand Down Expand Up @@ -618,24 +576,6 @@ func SetSignalEventCallback(cb unsafe.Pointer) {
signal.SetSignalEventCallback(cb)
}

// Get an X3DH bundle
//export GetContactCode
func GetContactCode(identity string) string {
bundle, err := statusBackend.GetContactCode(identity)
if err != nil {
return makeJSONResponse(err)
}

data, err := json.Marshal(struct {
ContactCode string `json:"code"`
}{ContactCode: bundle})
if err != nil {
return makeJSONResponse(err)
}

return string(data)
}

// ExportNodeLogs reads current node log and returns content to a caller.
//export ExportNodeLogs
func ExportNodeLogs() string {
Expand Down
5 changes: 3 additions & 2 deletions services/shhext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/status-im/status-go/db"
"github.com/status-im/status-go/mailserver"
"github.com/status-im/status-go/services/shhext/chat"
"github.com/status-im/status-go/services/shhext/chat/protobuf"
"github.com/status-im/status-go/services/shhext/dedup"
"github.com/status-im/status-go/services/shhext/filter"
"github.com/status-im/status-go/services/shhext/mailservers"
Expand Down Expand Up @@ -502,7 +503,7 @@ func (api *PublicAPI) SendDirectMessage(ctx context.Context, msg chat.SendDirect
}

// This is transport layer-agnostic
var protocolMessage *chat.ProtocolMessage
var protocolMessage *protobuf.ProtocolMessage
// The negotiated secret
var msgSpec *chat.ProtocolMessageSpec
var partitionedTopicSupported bool
Expand Down Expand Up @@ -688,7 +689,7 @@ func (api *PublicAPI) processPFSMessage(dedupMessage dedup.DeduplicateMessage) e
}

// Unmarshal message
protocolMessage := &chat.ProtocolMessage{}
protocolMessage := &protobuf.ProtocolMessage{}

if err := proto.Unmarshal(msg.Payload, protocolMessage); err != nil {
api.log.Debug("Not a protocol message", "err", err)
Expand Down
24 changes: 12 additions & 12 deletions services/shhext/chat/db/migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d38d3a3

Please sign in to comment.