Skip to content

Commit

Permalink
fix: signature for pushed events
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Aug 28, 2019
1 parent 7c62c82 commit 93fb733
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
7 changes: 6 additions & 1 deletion core/crypto/keypair/keypair.go
Expand Up @@ -95,7 +95,12 @@ func (m *Revocation) GetSignedValue() SignableValue {
}

func CheckSignature(signedValue SignedValue, pubKey crypto.PublicKey) error {
hashFunc, err := signedValue.GetSignature().SignatureAlgorithm.GetHashFunction()
sig := signedValue.GetSignature()
if sig == nil {
return errors.New("no signature found")
}

hashFunc, err := sig.SignatureAlgorithm.GetHashFunction()

if err != nil {
return err
Expand Down
23 changes: 11 additions & 12 deletions core/node/mainloop.go
Expand Up @@ -346,10 +346,19 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch,
// tracer := tracing.EnterFunc(ctx, dispatch, envelope)
// defer tracer.Finish()
// ctx = tracer.Context()
var err error

if dispatch.ContactID == n.UserID() {
return nil
}

envCopy := *envelope
envCopy.ChannelID = dispatch.ContactID
envCopy.Signature, err = keypair.Sign(n.crypto, &envCopy)
if err != nil {
return errors.Wrap(err, "failed to sign envelope")
}

// Async subscribe to conversation
// wait for 1s to simulate a sync subscription,
// if too long, the task will be done in background
Expand All @@ -358,16 +367,6 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch,
tctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

var err error

envCopy := *envelope
envCopy.ChannelID = dispatch.ContactID
envCopy.Signature, err = keypair.Sign(n.crypto, &envCopy)
if err != nil {
n.LogBackgroundError(ctx, errors.Wrap(err, "failed to sign envelope"))
return
}

data, err := proto.Marshal(&envCopy)
if err != nil {
n.LogBackgroundWarn(ctx, errors.Wrap(err, "failed to marshal envelope"))
Expand All @@ -384,7 +383,7 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch,
n.LogBackgroundWarn(ctx, errors.Wrap(err, "failed to emit envelope on network"))

// push the outgoing event on the client stream
go n.queuePushEvent(ctx, event, envelope)
go n.queuePushEvent(ctx, event, &envCopy)
return
}

Expand All @@ -395,7 +394,7 @@ func (n *Node) sendDispatch(ctx context.Context, dispatch *entity.EventDispatch,
case <-done:
case <-time.After(1 * time.Second):
// push the outgoing event on the client stream
go n.queuePushEvent(ctx, event, envelope)
go n.queuePushEvent(ctx, event, &envCopy)
}
return nil
}
Expand Down
16 changes: 11 additions & 5 deletions core/node/p2papi.go
Expand Up @@ -192,18 +192,24 @@ func (n *Node) pushEvent(ctx context.Context, event *entity.Event, envelope *ent
return errorcodes.ErrPushBroadcast.Wrap(err)
}

pushClient := pushService.NewPushServiceClient(conn)

for _, pushIdentifier := range pushIdentifiers {
var pushMessages []*push.PushData

logger().Info(fmt.Sprintf("connecting to push notification server on %s", pushServerAddr))

pushClient := pushService.NewPushServiceClient(conn)

for _, c := range chunks {
envelopeData, err := c.Marshal()
if err != nil {
logger().Error("unable to marshal envelope")
break
}

pushData := &push.PushData{
Priority: event.PushPriority(),
PushIdentifier: pushIdentifier.PushInfo,
Envelope: c.GetData(),
Envelope: envelopeData,
}

pushMessages = append(pushMessages, pushData)
Expand All @@ -221,10 +227,10 @@ func (n *Node) pushEvent(ctx context.Context, event *entity.Event, envelope *ent
logger().Error("error while pushing data to device", zap.Error(err))
continue
}

_ = conn.Close()
}

_ = conn.Close()

return nil
}

Expand Down

0 comments on commit 93fb733

Please sign in to comment.