Small fixes and opened symkey methods

This commit is contained in:
Adrià Cidre 2018-05-08 12:44:06 +02:00
parent 2e09479c69
commit a1b50c16ac
No known key found for this signature in database
GPG Key ID: D246A27D58A92CAB
3 changed files with 30 additions and 17 deletions

View File

@ -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)

19
chan.go
View File

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

7
msg.go
View File

@ -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),
}
}