Skip to content

Commit

Permalink
Assume KeyPackages are always last resort.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bren2010 committed Jan 8, 2024
1 parent d54f08f commit d754306
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 290 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,5 @@ require (

// From node-go
replace github.com/ethereum/go-ethereum v1.10.17 => github.com/status-im/go-ethereum v1.10.4-status.2

replace github.com/xmtp/proto/v3 v3.36.1-0.20231219054634-2ff03b7d5090 => github.com/Bren2010/proto/v3 v3.0.0-20240104022019-f50c3c99cd1d
22 changes: 2 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxB
github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM=
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
github.com/Bren2010/proto/v3 v3.0.0-20240104022019-f50c3c99cd1d h1:0/4RpM5YodUyW0EMVlsYFTGop5Td97IiYbZqZTFQSuU=
github.com/Bren2010/proto/v3 v3.0.0-20240104022019-f50c3c99cd1d/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
Expand Down Expand Up @@ -94,7 +96,6 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/arrow/go/arrow v0.0.0-20191024131854-af6fa24be0db/go.mod h1:VTxUBvSJ3s3eHAg65PNgrsn5BtqCRPdmyXh6rAfdxN0=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -299,7 +300,6 @@ github.com/go-ldap/ldap/v3 v3.1.3/go.mod h1:3rbOH3jRS2u6jg2rJnKAMLE/xQyCKIveG2Sa
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
Expand Down Expand Up @@ -635,19 +635,16 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/json-iterator/go v0.0.0-20180612202835-f2b4162afba3/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jsternberg/zap-logfmt v1.0.0/go.mod h1:uvPs/4X51zdkcm5jXl5SYoN+4RK21K8mysFmDaM/h+o=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0=
github.com/karalabe/usb v0.0.2/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
Expand Down Expand Up @@ -810,7 +807,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
Expand Down Expand Up @@ -842,7 +838,6 @@ github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/n
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
Expand Down Expand Up @@ -974,7 +969,6 @@ github.com/retailnext/hllpp v1.0.1-0.20180308014038-101a6d2f8b52/go.mod h1:RDpi1
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
Expand Down Expand Up @@ -1154,18 +1148,6 @@ github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xmtp/go-msgio v0.2.1-0.20220510223757-25a701b79cd3 h1:wzUffJGCTBGXIDyNU+1UBu1fn2Nzo+OQzM1pLrheh58=
github.com/xmtp/go-msgio v0.2.1-0.20220510223757-25a701b79cd3/go.mod h1:bJREWk+NDnZYjgLQdAi8SUWuq/5pkMme4GqiffEhUF4=
github.com/xmtp/proto/v3 v3.29.1-0.20231019225839-328520e94f34 h1:rR10cJ5RTlw7OWdw5fnNF3WpByRybvGC3xmOmELd7JY=
github.com/xmtp/proto/v3 v3.29.1-0.20231019225839-328520e94f34/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xmtp/proto/v3 v3.29.1-0.20231023182354-832c8d572ed4 h1:Qc2ed8NrlosJnPMNxVriugcFB21d4V90HKZdO83yV2M=
github.com/xmtp/proto/v3 v3.29.1-0.20231023182354-832c8d572ed4/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xmtp/proto/v3 v3.29.1-0.20231025193535-9760f07c3401 h1:ctStMkU5570kEgpVnsklo7PUOuTridm8bkXcZ1i6sXI=
github.com/xmtp/proto/v3 v3.29.1-0.20231025193535-9760f07c3401/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xmtp/proto/v3 v3.29.1-0.20231025220423-87413e63f3ab h1:hWBftgxB7QWXDOOv1Wah6VZ6mwSFZX8e8rEGJQHm8zA=
github.com/xmtp/proto/v3 v3.29.1-0.20231025220423-87413e63f3ab/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xmtp/proto/v3 v3.32.1-0.20231026053711-5efc208e3135 h1:MpeptLshF0T8ikwmVQI1mnlp5fXkbyUc+9FpyzFdsbo=
github.com/xmtp/proto/v3 v3.32.1-0.20231026053711-5efc208e3135/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xmtp/proto/v3 v3.36.1-0.20231219054634-2ff03b7d5090 h1:+0KTgQiUfu5UxgLjP18VL4BtG6hJMJYL0n1mVXtf3Ss=
github.com/xmtp/proto/v3 v3.36.1-0.20231219054634-2ff03b7d5090/go.mod h1:NF2zAjtNpVIhS4tFG19g4L1tJcPZHm81oeDFXltmOiY=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/yoheimuta/go-protoparser/v4 v4.6.0 h1:uvz1e9/5Ihsm4Ku8AJeDImTpirKmIxubZdSn0QJNdnw=
github.com/yoheimuta/go-protoparser/v4 v4.6.0/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
Expand Down
54 changes: 24 additions & 30 deletions pkg/api/message/v3/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *Service) RegisterInstallation(ctx context.Context, req *proto.RegisterI
return nil, err
}

results, err := s.validationService.ValidateKeyPackages(ctx, [][]byte{req.LastResortKeyPackage.KeyPackageTlsSerialized})
results, err := s.validationService.ValidateKeyPackages(ctx, [][]byte{req.KeyPackage.KeyPackageTlsSerialized})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid identity: %s", err)
}
Expand All @@ -57,7 +57,7 @@ func (s *Service) RegisterInstallation(ctx context.Context, req *proto.RegisterI
accountAddress := results[0].AccountAddress
credentialIdentity := results[0].CredentialIdentity

if err = s.mlsStore.CreateInstallation(ctx, installationId, accountAddress, req.LastResortKeyPackage.KeyPackageTlsSerialized, credentialIdentity); err != nil {
if err = s.mlsStore.CreateInstallation(ctx, installationId, accountAddress, credentialIdentity, req.KeyPackage.KeyPackageTlsSerialized, results[0].Expiration); err != nil {
return nil, err
}

Expand All @@ -66,31 +66,31 @@ func (s *Service) RegisterInstallation(ctx context.Context, req *proto.RegisterI
}, nil
}

func (s *Service) ConsumeKeyPackages(ctx context.Context, req *proto.ConsumeKeyPackagesRequest) (*proto.ConsumeKeyPackagesResponse, error) {
func (s *Service) FetchKeyPackages(ctx context.Context, req *proto.FetchKeyPackagesRequest) (*proto.FetchKeyPackagesResponse, error) {
ids := req.InstallationIds
keyPackages, err := s.mlsStore.ConsumeKeyPackages(ctx, ids)
installations, err := s.mlsStore.FetchKeyPackages(ctx, ids)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to consume key packages: %s", err)
return nil, status.Errorf(codes.Internal, "failed to fetch key packages: %s", err)
}
keyPackageMap := make(map[string]int)
for idx, id := range ids {
keyPackageMap[string(id)] = idx
}

resPackages := make([]*proto.ConsumeKeyPackagesResponse_KeyPackage, len(keyPackages))
for _, keyPackage := range keyPackages {
resPackages := make([]*proto.FetchKeyPackagesResponse_KeyPackage, len(installations))
for _, installation := range installations {

idx, ok := keyPackageMap[string(keyPackage.InstallationId)]
idx, ok := keyPackageMap[string(installation.ID)]
if !ok {
return nil, status.Errorf(codes.Internal, "could not find key package for installation")
}

resPackages[idx] = &proto.ConsumeKeyPackagesResponse_KeyPackage{
KeyPackageTlsSerialized: keyPackage.Data,
resPackages[idx] = &proto.FetchKeyPackagesResponse_KeyPackage{
KeyPackageTlsSerialized: installation.KeyPackage,
}
}

return &proto.ConsumeKeyPackagesResponse{
return &proto.FetchKeyPackagesResponse{
KeyPackages: resPackages,
}, nil
}
Expand Down Expand Up @@ -166,28 +166,22 @@ func (s *Service) PublishWelcomes(ctx context.Context, req *proto.PublishWelcome
return &emptypb.Empty{}, nil
}

func (s *Service) UploadKeyPackages(ctx context.Context, req *proto.UploadKeyPackagesRequest) (res *emptypb.Empty, err error) {
if err = validateUploadKeyPackagesRequest(req); err != nil {
func (s *Service) UploadKeyPackage(ctx context.Context, req *proto.UploadKeyPackageRequest) (res *emptypb.Empty, err error) {
if err = validateUploadKeyPackageRequest(req); err != nil {
return nil, err
}
// Extract the key packages from the request
keyPackageBytes := make([][]byte, len(req.KeyPackages))
for i, keyPackage := range req.KeyPackages {
keyPackageBytes[i] = keyPackage.KeyPackageTlsSerialized
}
validationResults, err := s.validationService.ValidateKeyPackages(ctx, keyPackageBytes)
keyPackageBytes := req.KeyPackage.KeyPackageTlsSerialized

validationResults, err := s.validationService.ValidateKeyPackages(ctx, [][]byte{keyPackageBytes})
if err != nil {
// TODO: Differentiate between validation errors and internal errors
return nil, status.Errorf(codes.InvalidArgument, "invalid identity: %s", err)
}
installationId := validationResults[0].InstallationId
expiration := validationResults[0].Expiration

keyPackageModels := make([]*mlsstore.KeyPackage, len(validationResults))
for i, validationResult := range validationResults {
kp := mlsstore.NewKeyPackage(validationResult.InstallationId, keyPackageBytes[i], false)
keyPackageModels[i] = kp
}

if err = s.mlsStore.InsertKeyPackages(ctx, keyPackageModels); err != nil {
if err = s.mlsStore.UpdateKeyPackage(ctx, installationId, keyPackageBytes, expiration); err != nil {
return nil, status.Errorf(codes.Internal, "failed to insert key packages: %s", err)
}

Expand Down Expand Up @@ -275,15 +269,15 @@ func validatePublishWelcomesRequest(req *proto.PublishWelcomesRequest) error {
}

func validateRegisterInstallationRequest(req *proto.RegisterInstallationRequest) error {
if req == nil || req.LastResortKeyPackage == nil {
return status.Errorf(codes.InvalidArgument, "no last resort key package")
if req == nil || req.KeyPackage == nil {
return status.Errorf(codes.InvalidArgument, "no key package")
}
return nil
}

func validateUploadKeyPackagesRequest(req *proto.UploadKeyPackagesRequest) error {
if req == nil || len(req.KeyPackages) == 0 {
return status.Errorf(codes.InvalidArgument, "no key packages to upload")
func validateUploadKeyPackageRequest(req *proto.UploadKeyPackageRequest) error {
if req == nil || req.KeyPackage == nil {
return status.Errorf(codes.InvalidArgument, "no key package")
}
return nil
}
Expand Down
38 changes: 19 additions & 19 deletions pkg/api/message/v3/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (m *mockedMLSValidationService) mockValidateKeyPackages(installationId []by
InstallationId: installationId,
AccountAddress: accountAddress,
CredentialIdentity: []byte("test"),
Expiration: 0,
},
}, nil)
}
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestRegisterInstallation(t *testing.T) {
mlsValidationService.mockValidateKeyPackages(installationId, accountAddress)

res, err := svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
Expand All @@ -126,15 +127,15 @@ func TestRegisterInstallationError(t *testing.T) {
mlsValidationService.On("ValidateKeyPackages", ctx, mock.Anything).Return(nil, errors.New("error validating"))

res, err := svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
require.Error(t, err)
require.Nil(t, res)
}

func TestUploadKeyPackages(t *testing.T) {
func TestUploadKeyPackage(t *testing.T) {
ctx := context.Background()
svc, mlsDb, mlsValidationService, cleanup := newTestService(t, ctx)
defer cleanup()
Expand All @@ -145,28 +146,27 @@ func TestUploadKeyPackages(t *testing.T) {
mlsValidationService.mockValidateKeyPackages(installationId, accountAddress)

res, err := svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
require.NoError(t, err)
require.NotNil(t, res)

uploadRes, err := svc.UploadKeyPackages(ctx, &proto.UploadKeyPackagesRequest{
KeyPackages: []*proto.KeyPackageUpload{
{KeyPackageTlsSerialized: []byte("test2")},
uploadRes, err := svc.UploadKeyPackage(ctx, &proto.UploadKeyPackageRequest{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test2"),
},
})
require.NoError(t, err)
require.NotNil(t, uploadRes)

keyPackages := []mlsstore.KeyPackage{}
err = mlsDb.NewSelect().Model(&keyPackages).Where("installation_id = ?", installationId).Scan(ctx)
installation := &mlsstore.Installation{}
err = mlsDb.NewSelect().Model(installation).Where("id = ?", installationId).Scan(ctx)
require.NoError(t, err)
require.Len(t, keyPackages, 2)
}

func TestConsumeKeyPackages(t *testing.T) {
func TestFetchKeyPackages(t *testing.T) {
ctx := context.Background()
svc, _, mlsValidationService, cleanup := newTestService(t, ctx)
defer cleanup()
Expand All @@ -177,7 +177,7 @@ func TestConsumeKeyPackages(t *testing.T) {
mockCall := mlsValidationService.mockValidateKeyPackages(installationId1, accountAddress1)

res, err := svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
Expand All @@ -192,14 +192,14 @@ func TestConsumeKeyPackages(t *testing.T) {
mlsValidationService.mockValidateKeyPackages(installationId2, accountAddress2)

res, err = svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test2"),
},
})
require.NoError(t, err)
require.NotNil(t, res)

consumeRes, err := svc.ConsumeKeyPackages(ctx, &proto.ConsumeKeyPackagesRequest{
consumeRes, err := svc.FetchKeyPackages(ctx, &proto.FetchKeyPackagesRequest{
InstallationIds: [][]byte{installationId1, installationId2},
})
require.NoError(t, err)
Expand All @@ -209,7 +209,7 @@ func TestConsumeKeyPackages(t *testing.T) {
require.Equal(t, []byte("test2"), consumeRes.KeyPackages[1].KeyPackageTlsSerialized)

// Now do it with the installationIds reversed
consumeRes, err = svc.ConsumeKeyPackages(ctx, &proto.ConsumeKeyPackagesRequest{
consumeRes, err = svc.FetchKeyPackages(ctx, &proto.FetchKeyPackagesRequest{
InstallationIds: [][]byte{installationId2, installationId1},
})

Expand All @@ -221,12 +221,12 @@ func TestConsumeKeyPackages(t *testing.T) {
}

// Trying to consume key packages that don't exist should fail
func TestConsumeKeyPackagesFail(t *testing.T) {
func TestFetchKeyPackagesFail(t *testing.T) {
ctx := context.Background()
svc, _, _, cleanup := newTestService(t, ctx)
defer cleanup()

consumeRes, err := svc.ConsumeKeyPackages(ctx, &proto.ConsumeKeyPackagesRequest{
consumeRes, err := svc.FetchKeyPackages(ctx, &proto.FetchKeyPackagesRequest{
InstallationIds: [][]byte{test.RandomBytes(32)},
})
require.Error(t, err)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestGetIdentityUpdates(t *testing.T) {
mockCall := mlsValidationService.mockValidateKeyPackages(installationId, accountAddress)

_, err := svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
Expand All @@ -297,7 +297,7 @@ func TestGetIdentityUpdates(t *testing.T) {
mockCall.Unset()
mlsValidationService.mockValidateKeyPackages(test.RandomBytes(32), accountAddress)
_, err = svc.RegisterInstallation(ctx, &proto.RegisterInstallationRequest{
LastResortKeyPackage: &proto.KeyPackageUpload{
KeyPackage: &proto.KeyPackageUpload{
KeyPackageTlsSerialized: []byte("test"),
},
})
Expand Down
Loading

0 comments on commit d754306

Please sign in to comment.