Skip to content
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

[vm] Temporarily brought back otto as a VM #105

Open
wants to merge 43 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
12c54f9
[documents] added document notarize call
Sep 11, 2018
bbc8cf9
[docs] added notarise command
Sep 11, 2018
2993dad
[eth] fix
Sep 13, 2018
13cd42d
[deps] updated protobuf
Sep 17, 2018
acaefbf
[docs] ipfs fix attempt
Sep 18, 2018
007ac65
[docs] Added more logs for notarise call error.
seland Sep 18, 2018
6d6ae2e
[docs] removed ipfs
Sep 18, 2018
1729c3e
[docs] fixed merge conflicts
Sep 18, 2018
f627e5b
[chat] added base group chat
Sep 18, 2018
bbba096
[tests] fixed tests
Sep 18, 2018
b549b5b
[chat] worked on group chat fixes
Sep 19, 2018
dee7277
[chat] worked on group id for messages
Sep 19, 2018
c56480f
[chat] fixed tests
Sep 19, 2018
009a263
[fix] small changes
Sep 19, 2018
733ffb4
[chat] added method to create private chat
Sep 19, 2018
4dfdf47
[notary] added signer
Sep 19, 2018
8733c8e
[notary] Fixed typo.
seland Sep 19, 2018
0c2d825
[notary] fix for submit
Sep 19, 2018
0a99922
Merge branch 'feature/notary' of github.com:Bit-Nation/panthalassa in…
Sep 19, 2018
c8cc640
[docs] fixed eth sign action
Sep 24, 2018
d726f39
[documents] fixed notary calls
Sep 25, 2018
69e7970
[api] changed tx data
Sep 25, 2018
102df01
[km] fix for signing with int encoding
Sep 25, 2018
1d9329d
[km] added tx transform for unmarshal tx
Sep 25, 2018
ba9897c
[chat] fixed chat return arguments
Sep 26, 2018
c51ede5
[chat] fixed group chat tests
Sep 26, 2018
3288de0
[chat] fixed tests added group name
Sep 26, 2018
f3b2ffd
[chat] renamed chat name to group chat name
Sep 26, 2018
7c48539
[panthalassa] added chat_partner and partners to chat
Sep 26, 2018
92be238
[ios] fixed compile error
Sep 26, 2018
3b672a6
[chat] added sender to ui events
Sep 26, 2018
b936d95
[vm] Temporarily brought back otto as a VM
borjantrajanoski Sep 27, 2018
f5b977c
[docs] decrypt content
Sep 27, 2018
ceebf38
[docs] added content decryption
Sep 27, 2018
d8c893a
[docs] added new notary contract
Sep 28, 2018
1ed8f4f
[panthalassa] added tx fee to notary
Sep 28, 2018
6b8d026
[panthalassa] fixed chat storage nil bug
Sep 28, 2018
7ee0154
[chat] added sender field
Sep 28, 2018
3048611
[docs] fixed decryption
Sep 28, 2018
452f71d
Merge branch 'feature/notary' into feature/old_vm
borjantrajanoski Sep 28, 2018
c7a865c
[tests] fixed api test
Sep 28, 2018
36206a4
[docs] fixed call to notarize document
Sep 28, 2018
76b1d90
Merge branch 'feature/group-chat' into feature/old_vm
borjantrajanoski Sep 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
386 changes: 376 additions & 10 deletions Gopkg.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

[[constraint]]
name = "github.com/ethereum/go-ethereum"
version = "1.8.11"
version = "1.8.15"

[[constraint]]
branch = "master"
Expand Down Expand Up @@ -138,4 +138,4 @@

[[constraint]]
name = "github.com/Bit-Nation/protobuffers"
version = "1.1.0"
revision = "b104bab4e16e94443febee60f003a2e5bacd6e03"
2 changes: 1 addition & 1 deletion api/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (a *DAppApi) SendEthereumTransaction(value, to, data string) (string, error
"v": ethTx.V,
"r": ethTx.R,
"s": ethTx.S,
"chainId": ethTx.ChainID,
"from": ethTx.From,
"hash": ethTx.Hash,
"chainId": ethTx.ChainID,
}

raw, err := json.Marshal(objTx)
Expand Down
111 changes: 84 additions & 27 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,90 @@ import (
"encoding/json"
"errors"
"strconv"

ed25519 "golang.org/x/crypto/ed25519"
)

func SendMessage(partner, message string) error {
func SendMessage(chatID int, message string) error {

// make sure panthalassa has been started
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa first")
}

// partner public key
partnerPub, err := hex.DecodeString(partner)
if err != nil {
// persist message
return panthalassaInstance.chat.SaveMessage(chatID, []byte(message))

}

func AddUsersToGroupChat(users string, chatID int) error {

// make sure panthalassa has been started
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa first")
}

// json unmarshal partners
rawPartners := []string{}
if err := json.Unmarshal([]byte(users), &rawPartners); err != nil {
return err
}

// make sure public key has the right length
if len(partnerPub) != 32 {
return errors.New("partner must have a length of 32 bytes")
// unmarshal hex partners
partners := []ed25519.PublicKey{}
for _, hexPartner := range rawPartners {
rawPartner, err := hex.DecodeString(hexPartner)
if err != nil {
return err
}
partners = append(partners, rawPartner)
}

// persist private message
return panthalassaInstance.chat.SavePrivateMessage(partnerPub, []byte(message))
return panthalassaInstance.chat.AddUserToGroupChat(partners, chatID)

}

func CreatePrivateChat(partnerStr string) (int, error) {

// make sure panthalassa has been started
if panthalassaInstance == nil {
return 0, errors.New("you have to start panthalassa first")
}

partner, err := hex.DecodeString(partnerStr)
if err != nil {
return 0, err
}

return panthalassaInstance.chatDB.CreateChat(partner)

}

// return chatID
func CreateGroupChat(users string, name string) (int, error) {

// make sure panthalassa has been started
if panthalassaInstance == nil {
return 0, errors.New("you have to start panthalassa first")
}

// json unmarshal partners
rawPartners := []string{}
if err := json.Unmarshal([]byte(users), &rawPartners); err != nil {
return 0, err
}

// unmarshal hex partners
partners := []ed25519.PublicKey{}
for _, hexPartner := range rawPartners {
rawPartner, err := hex.DecodeString(hexPartner)
if err != nil {
return 0, err
}
partners = append(partners, rawPartner)
}

return panthalassaInstance.chat.CreateGroupChat(partners, name)

}

Expand All @@ -45,8 +107,11 @@ func AllChats() (string, error) {
chatsRep := []map[string]interface{}{}
for _, chat := range chats {
chatsRep = append(chatsRep, map[string]interface{}{
"chat": chat.Partner,
"chat_id": chat.ID,
"chat_partner": chat.Partner,
"unread_messages": chat.UnreadMessages,
"group_chat_name": chat.GroupChatName,
"partners": chat.Partners,
})
}

Expand All @@ -58,7 +123,7 @@ func AllChats() (string, error) {
return string(chatList), nil
}

func Messages(partner string, startStr string, amount int) (string, error) {
func Messages(chatID int, startStr string, amount int) (string, error) {

// unmarshal start
start, err := strconv.ParseInt(startStr, 10, 64)
Expand All @@ -71,19 +136,18 @@ func Messages(partner string, startStr string, amount int) (string, error) {
return "", errors.New("you have to start panthalassa first")
}

// partner public key
partnerPub, err := hex.DecodeString(partner)
// fetch chat
chat, err := panthalassaInstance.chatDB.GetChat(chatID)
if err != nil {
return "", err
}

// make sure public key has the right length
if len(partnerPub) != 32 {
return "", errors.New("partner must have a length of 32 bytes")
if chat == nil {
return "", errors.New("chat doesn't exit")
}

// database messages
databaseMessages, err := panthalassaInstance.chat.Messages(partnerPub, int64(start), uint(amount))
databaseMessages, err := chat.Messages(start, uint(amount))
if err != nil {
return "", err
}
Expand All @@ -108,6 +172,7 @@ func Messages(partner string, startStr string, amount int) (string, error) {
"created_at": msg.CreatedAt,
"received": msg.Received,
"dapp": dapp,
"sender": msg.Sender,
})
}

Expand All @@ -121,21 +186,13 @@ func Messages(partner string, startStr string, amount int) (string, error) {

}

func MarkMessagesAsRead(partner string) error {
func MarkMessagesAsRead(chatID int) error {

// make sure panthalassa has been started
if panthalassaInstance == nil {
return errors.New("you have to start panthalassa first")
}

partnerByte, err := hex.DecodeString(partner)
if err != nil {
return err
}
if len(partnerByte) != 32 {
return errors.New("partner must have length of 32 bytes")
}

return panthalassaInstance.chat.MarkMessagesAsRead(partnerByte)
return panthalassaInstance.chat.MarkMessagesAsRead(chatID)

}
2 changes: 1 addition & 1 deletion chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Chat) AllChats() ([]db.Chat, error) {
}

func (c *Chat) Messages(partner ed25519.PublicKey, start int64, amount uint) ([]*db.Message, error) {
chat, err := c.chatStorage.GetChat(partner)
chat, err := c.chatStorage.GetChatByPartner(partner)
if err != nil {
return nil, err
}
Expand Down
145 changes: 145 additions & 0 deletions chat/group_chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package chat

import (
"encoding/hex"
"errors"
"time"

db "github.com/Bit-Nation/panthalassa/db"
uid "github.com/satori/go.uuid"
ed25519 "golang.org/x/crypto/ed25519"
)

func (c *Chat) AddUserToGroupChat(partners []ed25519.PublicKey, chatID int) error {

chat, err := c.chatStorage.GetChat(chatID)
if err != nil {
return err
}

if chat == nil {
return errors.New("couldn't find chat")
}

if !chat.IsGroupChat() {
return errors.New("chat must be a group chat")
}

// update chat partners
if err := chat.AddChatPartners(partners); err != nil {
return err
}

// fetch chat
for _, partner := range partners {

// fetch chat
partnerChat, err := c.chatStorage.GetChatByPartner(partner)
if err != nil {
return err
}
if partnerChat == nil {
_, err = c.chatStorage.CreateChat(partner)
if err != nil {
return err
}
}
partnerChat, err = c.chatStorage.GetChatByPartner(partner)
if err != nil {
return err
}
if partnerChat == nil {
return errors.New("chat with partner should exist at this point in time")
}

// persist message
msg := db.Message{
AddUserToChat: &db.AddUserToChat{
Users: partners,
ChatID: chat.GroupChatRemoteID,
},
}
if err := partnerChat.PersistMessage(msg); err != nil {
return err
}

}

return nil

}

func (c *Chat) CreateGroupChat(partners []ed25519.PublicKey, name string) (int, error) {

// create chat
chatID, err := c.chatStorage.CreateGroupChat(partners, name)
if err != nil {
return 0, err
}

// fetch group chat
groupChat, err := c.chatStorage.GetChat(chatID)
if err != nil {
return 0, err
}

// sender
idKeyStr, err := c.km.IdentityPublicKey()
if err != nil {
return 0, err
}

idKey, err := hex.DecodeString(idKeyStr)
if err != nil {
return 0, err
}

// send message to our group chat partners
for _, partner := range partners {

// fetch chat
partnerChat, err := c.chatStorage.GetChatByPartner(partner)
if err != nil {
return 0, err
}
if partnerChat == nil {
_, err = c.chatStorage.CreateChat(partner)
if err != nil {
return 0, err
}
}
partnerChat, err = c.chatStorage.GetChatByPartner(partner)
if err != nil {
return 0, err
}
if partnerChat == nil {
return 0, errors.New("chat with partner should exist at this point in time")
}

msgID, err := uid.NewV4()
if err != nil {
return 0, err
}

// persist message
msg := db.Message{
AddUserToChat: &db.AddUserToChat{
Users: partners,
ChatID: groupChat.GroupChatRemoteID,
},
Version: 1,
CreatedAt: time.Now().UnixNano(),
Status: db.StatusPersisted,
Sender: idKey,
GroupChatID: groupChat.GroupChatRemoteID,
ID: msgID.String(),
}
if err := partnerChat.PersistMessage(msg); err != nil {
return 0, err
}

}

return chatID, nil

}
Loading