Skip to content

Commit

Permalink
protocol and snapshot changes for public chunked media (#468)
Browse files Browse the repository at this point in the history
Main changes:
- `deriveKeyAndIV` function that will return the same `key` and `iv` for
the same `spaceId`.
- space snaphot that will return the latest chunked media with the
`DerivedAESGCM` encryption.
  • Loading branch information
tak-hntlabs authored Aug 1, 2024
1 parent 33c4f69 commit a29aa89
Show file tree
Hide file tree
Showing 19 changed files with 1,821 additions and 1,084 deletions.
17 changes: 17 additions & 0 deletions core/node/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ func Make_SpacePayload_Membership(op MembershipOp, userId string, initiatorId st
return Make_MemberPayload_Membership(op, userAddress, initiatorAddress, nil)
}

func Make_SpacePayload_SpaceImage(
ciphertext string,
algorithm string,
) *StreamEvent_SpacePayload {
encryptedData := &EncryptedData{
Ciphertext: ciphertext,
Algorithm: algorithm,
}
return &StreamEvent_SpacePayload{
SpacePayload: &SpacePayload{
Content: &SpacePayload_SpaceImage{
SpaceImage: encryptedData,
},
},
}
}

func Make_SpacePayload_ChannelUpdate(
op ChannelOp,
channelId StreamId,
Expand Down
16 changes: 13 additions & 3 deletions core/node/events/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Update_Snapshot(iSnapshot *Snapshot, event *ParsedEvent, miniblockNum int64
iSnapshot = migrations.MigrateSnapshot(iSnapshot)
switch payload := event.Event.Payload.(type) {
case *StreamEvent_SpacePayload:
return update_Snapshot_Space(iSnapshot, payload.SpacePayload, eventNum)
return update_Snapshot_Space(iSnapshot, payload.SpacePayload, event.Event.CreatorAddress, eventNum)
case *StreamEvent_ChannelPayload:
return update_Snapshot_Channel(iSnapshot, payload.ChannelPayload)
case *StreamEvent_DmChannelPayload:
Expand All @@ -182,7 +182,12 @@ func Update_Snapshot(iSnapshot *Snapshot, event *ParsedEvent, miniblockNum int64
}
}

func update_Snapshot_Space(iSnapshot *Snapshot, spacePayload *SpacePayload, eventNum int64) error {
func update_Snapshot_Space(
iSnapshot *Snapshot,
spacePayload *SpacePayload,
creatorAddress []byte,
eventNum int64,
) error {
snapshot := iSnapshot.Content.(*Snapshot_SpaceContent)
if snapshot == nil {
return RiverError(Err_INVALID_ARGUMENT, "blockheader snapshot is not a space snapshot")
Expand All @@ -199,6 +204,12 @@ func update_Snapshot_Space(iSnapshot *Snapshot, spacePayload *SpacePayload, even
}
snapshot.SpaceContent.Channels = insertChannel(snapshot.SpaceContent.Channels, channel)
return nil
case *SpacePayload_SpaceImage:
snapshot.SpaceContent.SpaceImage = &SpacePayload_SnappedSpaceImage{
Data: content.SpaceImage,
CreatorAddress: creatorAddress,
}
return nil
default:
return RiverError(Err_INVALID_ARGUMENT, "unknown space payload type %T", spacePayload.Content)
}
Expand Down Expand Up @@ -487,7 +498,6 @@ func update_Snapshot_Member(
}
snapshot.Pins = snapPins
return nil

default:
return RiverError(Err_INVALID_ARGUMENT, "unknown membership payload type %T", memberPayload.Content)
}
Expand Down
39 changes: 38 additions & 1 deletion core/node/events/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"google.golang.org/protobuf/proto"
)

const (
AES_GCM_DERIVED_ALGORITHM = "r.aes-256-gcm.derived"
)

func make_User_Inception(wallet *crypto.Wallet, streamId StreamId, t *testing.T) *ParsedEvent {
envelope, err := MakeEnvelopeWithPayload(
wallet,
Expand Down Expand Up @@ -84,6 +88,26 @@ func make_Space_Membership(
return parsed
}

func make_Space_Image(
wallet *crypto.Wallet,
ciphertext string,
prevMiniblockHash []byte,
t *testing.T,
) *ParsedEvent {
envelope, err := MakeEnvelopeWithPayload(
wallet,
Make_SpacePayload_SpaceImage(
ciphertext,
AES_GCM_DERIVED_ALGORITHM,
),
prevMiniblockHash,
)
assert.NoError(t, err)
parsed, err := ParseEvent(envelope)
assert.NoError(t, err)
return parsed
}

func make_Space_Username(wallet *crypto.Wallet, username string, prevHash []byte, t *testing.T) *ParsedEvent {
envelope, err := MakeEnvelopeWithPayload(
wallet,
Expand Down Expand Up @@ -197,7 +221,9 @@ func TestCloneAndUpdateSpaceSnapshot(t *testing.T) {
membership := make_Space_Membership(wallet, MembershipOp_SO_JOIN, userId, nil, t)
username := make_Space_Username(wallet, "bob", nil, t)
displayName := make_Space_DisplayName(wallet, "bobIsTheGreatest", nil, t)
events := []*ParsedEvent{membership, username, displayName}
imageCipertext := "space_image_ciphertext"
image := make_Space_Image(wallet, imageCipertext, nil, t)
events := []*ParsedEvent{membership, username, displayName, image}
for i, event := range events[:] {
err = Update_Snapshot(snapshot, event, 1, int64(3+i))
assert.NoError(t, err)
Expand Down Expand Up @@ -231,6 +257,17 @@ func TestCloneAndUpdateSpaceSnapshot(t *testing.T) {
int64(5),
member.DisplayName.EventNum,
)

assert.Equal(
t,
imageCipertext,
snapshot.Content.(*Snapshot_SpaceContent).SpaceContent.SpaceImage.Data.Ciphertext,
)
assert.Equal(
t,
AES_GCM_DERIVED_ALGORITHM,
snapshot.Content.(*Snapshot_SpaceContent).SpaceContent.SpaceImage.Data.Algorithm,
)
}

func TestUpdateSnapshotFailsIfInception(t *testing.T) {
Expand Down
Loading

0 comments on commit a29aa89

Please sign in to comment.