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 // JoinPublicChannel joins a status public channel
func (a *Account) JoinPublicChannel(channelName string) (*Channel, error) { func (a *Account) JoinPublicChannel(name string) (*Channel, error) {
symkeyResponse, err := shhGenerateSymKeyFromPasswordRequest(a.conn, []string{channelName}) 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 { if err != nil {
return nil, err return nil, err
} }
symKey := symkeyResponse.Key symKey := symkeyResponse.Key
topicID, err := a.calculatePublicChannelTopicID(channelName, symkeyResponse.ID) topicID, err := a.calculatePublicChannelTopicID(name, symkeyResponse.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return a.Join(channelName, topicID, symKey) return a.Join(name, topicID, symKey)
} }
// Join joins a status channel // Join joins a status channel
@ -43,8 +52,8 @@ func (a *Account) Join(channelName, topicID, symKey string) (*Channel, error) {
account: a, account: a,
name: channelName, name: channelName,
filterID: filterID, filterID: filterID,
topicID: topicID, TopicID: topicID,
channelKey: symKey, ChannelKey: symKey,
} }
a.channels = append(a.channels, ch) a.channels = append(a.channels, ch)

19
chan.go
View File

@ -12,8 +12,8 @@ type Channel struct {
account *Account account *Account
name string name string
filterID string filterID string
channelKey string ChannelKey string
topicID string TopicID string
visibility string visibility string
subscriptions []*Subscription subscriptions []*Subscription
} }
@ -52,7 +52,7 @@ func (c *Channel) NewContactKeyRequest(username string) error {
contactRequest := fmt.Sprintf(format, ContactRequestType, username, "", "", "") contactRequest := fmt.Sprintf(format, ContactRequestType, username, "", "", "")
format = `["%s",["%s","%s",%s]` 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) return c.SendPostRawMsg(msg)
} }
@ -118,9 +118,9 @@ func (c *Channel) ContactUpdateRequest(username, image string) error {
func (c *Channel) SendPostRawMsg(body string) error { func (c *Channel) SendPostRawMsg(body string) error {
param := shhPostParam{ param := shhPostParam{
Signature: c.account.Address, Signature: c.account.Address,
SymKeyID: c.channelKey, SymKeyID: c.ChannelKey,
Payload: rawrChatMessage(body), Payload: rawrChatMessage(body),
Topic: c.topicID, Topic: c.TopicID,
TTL: 10, TTL: 10,
PowTarget: c.account.conn.minimumPoW, PowTarget: c.account.conn.minimumPoW,
PowTime: 1, PowTime: 1,
@ -151,7 +151,7 @@ func (c *Channel) PNBroadcastAvailabilityRequest() error {
// Additionally a device token will identify the device on the push notification // Additionally a device token will identify the device on the push notification
// provider. // provider.
func (c *Channel) PNRegistrationRequest(symkey, topic, deviceToken string, slotAvailabilityRatio float32) error { 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) msg := fmt.Sprintf(format, PNRegistrationType, symkey, topic, deviceToken, slotAvailabilityRatio)
return c.SendPostRawMsg(msg) 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 // server to let a client know what's the pubkey associated with its registered
// token. // token.
func (c *Channel) PNRegistrationConfirmationRequest(pubkey string) error { func (c *Channel) PNRegistrationConfirmationRequest(pubkey string) error {
format := `["%s",["%s"]]]` format := `["%s",["%s"]]`
msg := fmt.Sprintf(format, PNRegistrationConfirmationType, pubkey) msg := fmt.Sprintf(format, PNRegistrationConfirmationType, pubkey)
return c.SendPostRawMsg(msg) return c.SendPostRawMsg(msg)
@ -197,6 +197,11 @@ func (c *Channel) pollMessages() (msg *Msg) {
msg.Channel = c msg.Channel = c
msg.ChannelName = c.name msg.ChannelName = c.name
return return
} else if err != nil {
log.Println("[ ERROR ]", err.Error())
return
} else {
log.Println("[ ERROR ]", "Invalid message type", msg.Type)
} }
return nil return nil
} }

7
msg.go
View File

@ -87,7 +87,6 @@ func messageFromPayload(payload string) (*Msg, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = json.Unmarshal(rawMsg, &msg); err != nil { if err = json.Unmarshal(rawMsg, &msg); err != nil {
return nil, err return nil, err
} }
@ -126,7 +125,7 @@ func messageFromPayload(payload string) (*Msg, error) {
case PNRegistrationType: case PNRegistrationType:
message.Properties = pnRegistrationMsgFromProperties(properties) message.Properties = pnRegistrationMsgFromProperties(properties)
case PNRegistrationConfirmationType: case PNRegistrationConfirmationType:
// message.Properties = newPublishMessageFromProperties(properties) message.Properties = pnRegistrationConfirmationMsgFromProperties(properties)
default: default:
return nil, errors.New("unsupported message type") return nil, errors.New("unsupported message type")
} }
@ -248,7 +247,7 @@ type PNRegistrationMsg struct {
Symkey string Symkey string
Topic string Topic string
DeviceToken string DeviceToken string
SlotAvailability float32 SlotAvailability float64
} }
func pnRegistrationMsgFromProperties(properties []interface{}) *PNRegistrationMsg { func pnRegistrationMsgFromProperties(properties []interface{}) *PNRegistrationMsg {
@ -256,7 +255,7 @@ func pnRegistrationMsgFromProperties(properties []interface{}) *PNRegistrationMs
Symkey: properties[0].(string), Symkey: properties[0].(string),
Topic: properties[1].(string), Topic: properties[1].(string),
DeviceToken: properties[2].(string), DeviceToken: properties[2].(string),
SlotAvailability: properties[3].(float32), SlotAvailability: properties[3].(float64),
} }
} }