Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
12f1b4a9
Commit
12f1b4a9
authored
Jul 22, 2022
by
Jonah Husson
Browse files
Options
Downloads
Patches
Plain Diff
Fix up comments a bit
parent
37e004b3
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!510
Release
,
!285
Add bindings for broadcast, some comment updates
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
bindings/broadcast.go
+27
-15
27 additions, 15 deletions
bindings/broadcast.go
with
27 additions
and
15 deletions
bindings/broadcast.go
+
27
−
15
View file @
12f1b4a9
...
@@ -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
.
//
A
ccepts the result of calling json.Marshal
l
on a BroadcastMessage object
//
It a
ccepts 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
,
},
c
l
.
api
.
GetCmix
(),
c
l
.
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
.
//
R
equires a private key
//
This mode of encryption r
equires 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
()
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment