Skip to content
Snippets Groups Projects
Commit 12f1b4a9 authored by Jonah Husson's avatar Jonah Husson
Browse files

Fix up comments a bit

parent 37e004b3
No related branches found
No related tags found
2 merge requests!510Release,!285Add bindings for broadcast, some comment updates
...@@ -25,6 +25,7 @@ type Channel struct { ...@@ -25,6 +25,7 @@ type Channel struct {
} }
// ChannelDef is the bindings representation of an elixxir/crypto broadcast.Channel object. // ChannelDef is the bindings representation of an elixxir/crypto broadcast.Channel object.
//
// Example JSON: // Example JSON:
// {"Name": "My broadcast channel", // {"Name": "My broadcast channel",
// "Description":"A broadcast channel for me to test things", // "Description":"A broadcast channel for me to test things",
...@@ -39,6 +40,7 @@ type ChannelDef struct { ...@@ -39,6 +40,7 @@ type ChannelDef struct {
} }
// BroadcastMessage is the bindings representation of a broadcast message. // BroadcastMessage is the bindings representation of a broadcast message.
//
// Example JSON: // Example JSON:
// {"RoundID":42, // {"RoundID":42,
// "EphID":[0,0,0,0,0,0,24,61], // "EphID":[0,0,0,0,0,0,24,61],
...@@ -50,6 +52,7 @@ type BroadcastMessage struct { ...@@ -50,6 +52,7 @@ type BroadcastMessage struct {
} }
// BroadcastReport is the bindings representation of the info on how a broadcast message was sent // BroadcastReport is the bindings representation of the info on how a broadcast message was sent
//
// Example JSON: // Example JSON:
// {"RoundID":42, // {"RoundID":42,
// "EphID":[0,0,0,0,0,0,24,61] // "EphID":[0,0,0,0,0,0,24,61]
...@@ -59,22 +62,25 @@ type BroadcastReport struct { ...@@ -59,22 +62,25 @@ type BroadcastReport struct {
EphID ephemeral.Id EphID ephemeral.Id
} }
// BroadcastListener is the public function type bindings can use to listen for broadcast messages // BroadcastListener is the public function type bindings can use to listen for broadcast messages.
// Accepts the result of calling json.Marshall on a BroadcastMessage object // It accepts the result of calling json.Marshal on a BroadcastMessage object.
type BroadcastListener interface { type BroadcastListener interface {
Callback([]byte, error) Callback([]byte, error)
} }
// NewBroadcastClient creates a bindings-layer broadcast channel & starts listening for new messages // NewBroadcastChannel creates a bindings-layer broadcast channel & starts listening for new messages
// Accepts a client ID, broadcast method, JSON marshalled channel definition & a BroadcastListener interface //
func NewBroadcastClient(clientID int, rawDef []byte) (*Channel, error) { // Params
cl, err := cmixTrackerSingleton.get(clientID) // - cmixId - internal ID of cmix
// - channelDefinition - JSON marshalled ChannelDef object
func NewBroadcastChannel(cmixId int, channelDefinition []byte) (*Channel, error) {
c, err := cmixTrackerSingleton.get(cmixId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
def := &ChannelDef{} def := &ChannelDef{}
err = json.Unmarshal(rawDef, def) err = json.Unmarshal(channelDefinition, def)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "Failed to unmarshal underlying channel definition") return nil, errors.WithMessage(err, "Failed to unmarshal underlying channel definition")
} }
...@@ -94,7 +100,7 @@ func NewBroadcastClient(clientID int, rawDef []byte) (*Channel, error) { ...@@ -94,7 +100,7 @@ func NewBroadcastClient(clientID int, rawDef []byte) (*Channel, error) {
Description: def.Description, Description: def.Description,
Salt: def.Salt, Salt: def.Salt,
RsaPubKey: chanPubLoaded, RsaPubKey: chanPubLoaded,
}, cl.api.GetCmix(), cl.api.GetRng()) }, c.api.GetCmix(), c.api.GetRng())
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "Failed to create broadcast channel client") return nil, errors.WithMessage(err, "Failed to create broadcast channel client")
} }
...@@ -102,6 +108,12 @@ func NewBroadcastClient(clientID int, rawDef []byte) (*Channel, error) { ...@@ -102,6 +108,12 @@ func NewBroadcastClient(clientID int, rawDef []byte) (*Channel, error) {
return &Channel{ch: ch}, nil return &Channel{ch: ch}, nil
} }
// Listen registers a BroadcastListener for a given method.
// This allows users to handle incoming broadcast messages.
//
// Params:
// - l - BroadcastListener object
// - method - int corresponding to broadcast.Method constant, 0 for symmetric or 1 for asymmetric
func (c *Channel) Listen(l BroadcastListener, method int) error { func (c *Channel) Listen(l BroadcastListener, method int) error {
broadcastMethod := broadcast.Method(method) broadcastMethod := broadcast.Method(method)
listen := func(payload []byte, listen := func(payload []byte,
...@@ -117,7 +129,7 @@ func (c *Channel) Listen(l BroadcastListener, method int) error { ...@@ -117,7 +129,7 @@ func (c *Channel) Listen(l BroadcastListener, method int) error {
return c.ch.RegisterListener(listen, broadcastMethod) return c.ch.RegisterListener(listen, broadcastMethod)
} }
// Broadcast sends a given payload over the broadcast channel using symmetric broadcast // Broadcast sends a given payload over the broadcast channel using symmetric broadcast.
func (c *Channel) Broadcast(payload []byte) ([]byte, error) { func (c *Channel) Broadcast(payload []byte) ([]byte, error) {
rid, eid, err := c.ch.Broadcast(payload, cmix.GetDefaultCMIXParams()) rid, eid, err := c.ch.Broadcast(payload, cmix.GetDefaultCMIXParams())
if err != nil { if err != nil {
...@@ -129,8 +141,8 @@ func (c *Channel) Broadcast(payload []byte) ([]byte, error) { ...@@ -129,8 +141,8 @@ func (c *Channel) Broadcast(payload []byte) ([]byte, error) {
}) })
} }
// BroadcastAsymmetric sends a given payload over the broadcast channel using asymmetric broadcast // BroadcastAsymmetric sends a given payload over the broadcast channel using asymmetric broadcast.
// Requires a private key // This mode of encryption requires a private key.
func (c *Channel) BroadcastAsymmetric(payload, pk []byte) ([]byte, error) { func (c *Channel) BroadcastAsymmetric(payload, pk []byte) ([]byte, error) {
pkLoaded, err := rsa.LoadPrivateKeyFromPem(pk) pkLoaded, err := rsa.LoadPrivateKeyFromPem(pk)
if err != nil { if err != nil {
...@@ -146,17 +158,17 @@ func (c *Channel) BroadcastAsymmetric(payload, pk []byte) ([]byte, error) { ...@@ -146,17 +158,17 @@ func (c *Channel) BroadcastAsymmetric(payload, pk []byte) ([]byte, error) {
}) })
} }
// MaxPayloadSize returns the maximum possible payload size which can be broadcast // MaxPayloadSize returns the maximum possible payload size which can be broadcast.
func (c *Channel) MaxPayloadSize() int { func (c *Channel) MaxPayloadSize() int {
return c.ch.MaxPayloadSize() return c.ch.MaxPayloadSize()
} }
// MaxAsymmetricPayloadSize returns the maximum possible payload size which can be broadcast // MaxAsymmetricPayloadSize returns the maximum possible payload size which can be broadcast.
func (c *Channel) MaxAsymmetricPayloadSize() int { func (c *Channel) MaxAsymmetricPayloadSize() int {
return c.ch.MaxAsymmetricPayloadSize() return c.ch.MaxAsymmetricPayloadSize()
} }
// Get returns the result of calling json.Marshal on a ChannelDef based on the underlying crypto broadcast.Channel // Get returns the result of calling json.Marshal on a ChannelDef based on the underlying crypto broadcast.Channel.
func (c *Channel) Get() ([]byte, error) { func (c *Channel) Get() ([]byte, error) {
def := c.ch.Get() def := c.ch.Get()
return json.Marshal(&ChannelDef{ return json.Marshal(&ChannelDef{
...@@ -167,7 +179,7 @@ func (c *Channel) Get() ([]byte, error) { ...@@ -167,7 +179,7 @@ func (c *Channel) Get() ([]byte, error) {
}) })
} }
// Stop stops the channel from listening for more messages // Stop stops the channel from listening for more messages.
func (c *Channel) Stop() { func (c *Channel) Stop() {
c.ch.Stop() c.ch.Stop()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment