From a1b50c16ac6b4a442443eae480177aaefce47e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Cidre?= Date: Tue, 8 May 2018 12:44:06 +0200 Subject: [PATCH] Small fixes and opened symkey methods --- account.go | 21 +++++++++++++++------ chan.go | 19 ++++++++++++------- msg.go | 7 +++---- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/account.go b/account.go index d767ba9..224f894 100644 --- a/account.go +++ b/account.go @@ -15,19 +15,28 @@ type Account struct { } // JoinPublicChannel joins a status public channel -func (a *Account) JoinPublicChannel(channelName string) (*Channel, error) { - symkeyResponse, err := shhGenerateSymKeyFromPasswordRequest(a.conn, []string{channelName}) +func (a *Account) JoinPublicChannel(name string) (*Channel, error) { + return a.createAndJoin(name, name) +} + +// CreatePrivateChannel creates and joins a private channel +func (a *Account) CreatePrivateChannel(name, password string) (*Channel, error) { + return a.createAndJoin(name, password) +} + +func (a *Account) createAndJoin(name, password string) (*Channel, error) { + symkeyResponse, err := shhGenerateSymKeyFromPasswordRequest(a.conn, []string{password}) if err != nil { return nil, err } symKey := symkeyResponse.Key - topicID, err := a.calculatePublicChannelTopicID(channelName, symkeyResponse.ID) + topicID, err := a.calculatePublicChannelTopicID(name, symkeyResponse.ID) if err != nil { return nil, err } - return a.Join(channelName, topicID, symKey) + return a.Join(name, topicID, symKey) } // Join joins a status channel @@ -43,8 +52,8 @@ func (a *Account) Join(channelName, topicID, symKey string) (*Channel, error) { account: a, name: channelName, filterID: filterID, - topicID: topicID, - channelKey: symKey, + TopicID: topicID, + ChannelKey: symKey, } a.channels = append(a.channels, ch) diff --git a/chan.go b/chan.go index 2fd65de..3311712 100644 --- a/chan.go +++ b/chan.go @@ -12,8 +12,8 @@ type Channel struct { account *Account name string filterID string - channelKey string - topicID string + ChannelKey string + TopicID string visibility string subscriptions []*Subscription } @@ -52,7 +52,7 @@ func (c *Channel) NewContactKeyRequest(username string) error { contactRequest := fmt.Sprintf(format, ContactRequestType, username, "", "", "") format = `["%s",["%s","%s",%s]` - msg := fmt.Sprintf(format, NewContactKeyType, c.account.Address, c.topicID, contactRequest) + msg := fmt.Sprintf(format, NewContactKeyType, c.account.Address, c.TopicID, contactRequest) return c.SendPostRawMsg(msg) } @@ -118,9 +118,9 @@ func (c *Channel) ContactUpdateRequest(username, image string) error { func (c *Channel) SendPostRawMsg(body string) error { param := shhPostParam{ Signature: c.account.Address, - SymKeyID: c.channelKey, + SymKeyID: c.ChannelKey, Payload: rawrChatMessage(body), - Topic: c.topicID, + Topic: c.TopicID, TTL: 10, PowTarget: c.account.conn.minimumPoW, PowTime: 1, @@ -151,7 +151,7 @@ func (c *Channel) PNBroadcastAvailabilityRequest() error { // Additionally a device token will identify the device on the push notification // provider. func (c *Channel) PNRegistrationRequest(symkey, topic, deviceToken string, slotAvailabilityRatio float32) error { - format := `["%s",["%s","%s","%s"]]]` + format := `["%s",["%s","%s","%s", %g]]` msg := fmt.Sprintf(format, PNRegistrationType, symkey, topic, deviceToken, slotAvailabilityRatio) return c.SendPostRawMsg(msg) @@ -161,7 +161,7 @@ func (c *Channel) PNRegistrationRequest(symkey, topic, deviceToken string, slotA // server to let a client know what's the pubkey associated with its registered // token. func (c *Channel) PNRegistrationConfirmationRequest(pubkey string) error { - format := `["%s",["%s"]]]` + format := `["%s",["%s"]]` msg := fmt.Sprintf(format, PNRegistrationConfirmationType, pubkey) return c.SendPostRawMsg(msg) @@ -197,6 +197,11 @@ func (c *Channel) pollMessages() (msg *Msg) { msg.Channel = c msg.ChannelName = c.name return + } else if err != nil { + log.Println("[ ERROR ]", err.Error()) + return + } else { + log.Println("[ ERROR ]", "Invalid message type", msg.Type) } return nil } diff --git a/msg.go b/msg.go index b776bf8..82fb7b1 100644 --- a/msg.go +++ b/msg.go @@ -87,7 +87,6 @@ func messageFromPayload(payload string) (*Msg, error) { if err != nil { return nil, err } - if err = json.Unmarshal(rawMsg, &msg); err != nil { return nil, err } @@ -126,7 +125,7 @@ func messageFromPayload(payload string) (*Msg, error) { case PNRegistrationType: message.Properties = pnRegistrationMsgFromProperties(properties) case PNRegistrationConfirmationType: - // message.Properties = newPublishMessageFromProperties(properties) + message.Properties = pnRegistrationConfirmationMsgFromProperties(properties) default: return nil, errors.New("unsupported message type") } @@ -248,7 +247,7 @@ type PNRegistrationMsg struct { Symkey string Topic string DeviceToken string - SlotAvailability float32 + SlotAvailability float64 } func pnRegistrationMsgFromProperties(properties []interface{}) *PNRegistrationMsg { @@ -256,7 +255,7 @@ func pnRegistrationMsgFromProperties(properties []interface{}) *PNRegistrationMs Symkey: properties[0].(string), Topic: properties[1].(string), DeviceToken: properties[2].(string), - SlotAvailability: properties[3].(float32), + SlotAvailability: properties[3].(float64), } }