-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Create validation service
Showing
8 changed files
with
548 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package mlsvalidate | ||
|
||
import ( | ||
"context" | ||
|
||
identity_proto "github.com/xmtp/xmtpd/pkg/proto/identity" | ||
associations "github.com/xmtp/xmtpd/pkg/proto/identity/associations" | ||
mlsv1 "github.com/xmtp/xmtpd/pkg/proto/mls/api/v1" | ||
) | ||
|
||
type KeyPackageValidationResult struct { | ||
InstallationKey []byte | ||
Credential *identity_proto.MlsCredential | ||
Expiration uint64 | ||
} | ||
|
||
type GroupMessageValidationResult struct { | ||
GroupId string | ||
} | ||
|
||
type AssociationStateResult struct { | ||
AssociationState *associations.AssociationState `protobuf:"bytes,1,opt,name=association_state,json=associationState,proto3" json:"association_state,omitempty"` | ||
StateDiff *associations.AssociationStateDiff `protobuf:"bytes,2,opt,name=state_diff,json=stateDiff,proto3" json:"state_diff,omitempty"` | ||
} | ||
|
||
type MLSValidationService interface { | ||
ValidateKeyPackages( | ||
ctx context.Context, | ||
keyPackages [][]byte, | ||
) ([]KeyPackageValidationResult, error) | ||
ValidateGroupMessages( | ||
ctx context.Context, | ||
groupMessages []*mlsv1.GroupMessageInput, | ||
) ([]GroupMessageValidationResult, error) | ||
GetAssociationState( | ||
ctx context.Context, | ||
oldUpdates []*associations.IdentityUpdate, | ||
newUpdates []*associations.IdentityUpdate, | ||
) (*AssociationStateResult, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package mlsvalidate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/xmtp/xmtpd/pkg/config" | ||
associations "github.com/xmtp/xmtpd/pkg/proto/identity/associations" | ||
mlsv1 "github.com/xmtp/xmtpd/pkg/proto/mls/api/v1" | ||
svc "github.com/xmtp/xmtpd/pkg/proto/mls_validation/v1" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
type MLSValidationServiceImpl struct { | ||
grpcClient svc.ValidationApiClient | ||
} | ||
|
||
func NewMlsValidationService( | ||
ctx context.Context, | ||
cfg config.MlsValidationOptions, | ||
) (*MLSValidationServiceImpl, error) { | ||
conn, err := grpc.NewClient( | ||
cfg.GrpcAddress, | ||
grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
conn.Close() | ||
}() | ||
|
||
return &MLSValidationServiceImpl{ | ||
grpcClient: svc.NewValidationApiClient(conn), | ||
}, nil | ||
} | ||
|
||
func (s *MLSValidationServiceImpl) GetAssociationState( | ||
ctx context.Context, | ||
oldUpdates []*associations.IdentityUpdate, | ||
newUpdates []*associations.IdentityUpdate, | ||
) (*AssociationStateResult, error) { | ||
req := &svc.GetAssociationStateRequest{ | ||
OldUpdates: oldUpdates, | ||
NewUpdates: newUpdates, | ||
} | ||
response, err := s.grpcClient.GetAssociationState(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AssociationStateResult{ | ||
AssociationState: response.GetAssociationState(), | ||
StateDiff: response.GetStateDiff(), | ||
}, nil | ||
} | ||
|
||
func (s *MLSValidationServiceImpl) ValidateKeyPackages( | ||
ctx context.Context, | ||
keyPackages [][]byte, | ||
) ([]KeyPackageValidationResult, error) { | ||
req := makeValidateKeyPackageRequest(keyPackages) | ||
|
||
response, err := s.grpcClient.ValidateInboxIdKeyPackages(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out := make([]KeyPackageValidationResult, len(response.Responses)) | ||
for i, response := range response.Responses { | ||
if !response.IsOk { | ||
return nil, fmt.Errorf("validation failed with error %s", response.ErrorMessage) | ||
} | ||
out[i] = KeyPackageValidationResult{ | ||
InstallationKey: response.InstallationPublicKey, | ||
Credential: nil, | ||
Expiration: response.Expiration, | ||
} | ||
} | ||
return out, nil | ||
} | ||
|
||
func makeValidateKeyPackageRequest( | ||
keyPackageBytes [][]byte, | ||
) *svc.ValidateInboxIdKeyPackagesRequest { | ||
keyPackageRequests := make( | ||
[]*svc.ValidateInboxIdKeyPackagesRequest_KeyPackage, | ||
len(keyPackageBytes), | ||
) | ||
for i, keyPackage := range keyPackageBytes { | ||
keyPackageRequests[i] = &svc.ValidateInboxIdKeyPackagesRequest_KeyPackage{ | ||
KeyPackageBytesTlsSerialized: keyPackage, | ||
IsInboxIdCredential: true, | ||
} | ||
} | ||
return &svc.ValidateInboxIdKeyPackagesRequest{ | ||
KeyPackages: keyPackageRequests, | ||
} | ||
} | ||
|
||
func (s *MLSValidationServiceImpl) ValidateGroupMessages( | ||
ctx context.Context, | ||
groupMessages []*mlsv1.GroupMessageInput, | ||
) ([]GroupMessageValidationResult, error) { | ||
req := makeValidateGroupMessagesRequest(groupMessages) | ||
|
||
response, err := s.grpcClient.ValidateGroupMessages(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out := make([]GroupMessageValidationResult, len(response.Responses)) | ||
for i, response := range response.Responses { | ||
if !response.IsOk { | ||
return nil, fmt.Errorf("validation failed with error %s", response.ErrorMessage) | ||
} | ||
out[i] = GroupMessageValidationResult{ | ||
GroupId: response.GroupId, | ||
} | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func makeValidateGroupMessagesRequest( | ||
groupMessages []*mlsv1.GroupMessageInput, | ||
) *svc.ValidateGroupMessagesRequest { | ||
groupMessageRequests := make( | ||
[]*svc.ValidateGroupMessagesRequest_GroupMessage, | ||
len(groupMessages), | ||
) | ||
for i, groupMessage := range groupMessages { | ||
groupMessageRequests[i] = &svc.ValidateGroupMessagesRequest_GroupMessage{ | ||
GroupMessageBytesTlsSerialized: groupMessage.GetV1().Data, | ||
} | ||
} | ||
return &svc.ValidateGroupMessagesRequest{ | ||
GroupMessages: groupMessageRequests, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package mlsvalidate | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/mocks" | ||
"github.com/xmtp/xmtpd/pkg/proto/identity/associations" | ||
proto "github.com/xmtp/xmtpd/pkg/proto/mls_validation/v1" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
) | ||
|
||
func TestValidateKeyPackages(t *testing.T) { | ||
apiClient := mocks.NewMockValidationApiClient(t) | ||
svc := &MLSValidationServiceImpl{ | ||
grpcClient: apiClient, | ||
} | ||
|
||
mockResponse := proto.ValidateInboxIdKeyPackagesResponse_Response{ | ||
IsOk: true, | ||
InstallationPublicKey: testutils.RandomBytes(32), | ||
Credential: nil, | ||
Expiration: 1, | ||
} | ||
|
||
apiClient.EXPECT(). | ||
ValidateInboxIdKeyPackages(mock.Anything, mock.Anything). | ||
Times(1). | ||
Return(&proto.ValidateInboxIdKeyPackagesResponse{ | ||
Responses: []*proto.ValidateInboxIdKeyPackagesResponse_Response{&mockResponse}}, | ||
nil, | ||
) | ||
|
||
res, err := svc.ValidateKeyPackages(context.Background(), [][]byte{testutils.RandomBytes(32)}) | ||
require.NoError(t, err) | ||
require.Len(t, res, 1) | ||
require.Equal(t, mockResponse.InstallationPublicKey, res[0].InstallationKey) | ||
require.Nil(t, res[0].Credential) | ||
} | ||
|
||
func TestGetAssociationState(t *testing.T) { | ||
apiClient := mocks.NewMockValidationApiClient(t) | ||
svc := &MLSValidationServiceImpl{ | ||
grpcClient: apiClient, | ||
} | ||
|
||
inboxId := testutils.RandomInboxId() | ||
address := testutils.RandomAddress().String() | ||
|
||
mockResponse := proto.GetAssociationStateResponse{ | ||
AssociationState: &associations.AssociationState{ | ||
InboxId: inboxId, | ||
}, | ||
StateDiff: &associations.AssociationStateDiff{ | ||
NewMembers: []*associations.MemberIdentifier{{ | ||
Kind: &associations.MemberIdentifier_Address{Address: address}, | ||
}}, | ||
}, | ||
} | ||
|
||
apiClient.EXPECT(). | ||
GetAssociationState(mock.Anything, mock.Anything). | ||
Times(1). | ||
Return(&mockResponse, nil) | ||
|
||
res, err := svc.GetAssociationState( | ||
context.Background(), | ||
[]*associations.IdentityUpdate{}, | ||
[]*associations.IdentityUpdate{}, | ||
) | ||
require.NoError(t, err) | ||
require.Equal(t, inboxId, res.AssociationState.InboxId) | ||
require.Equal(t, address, res.StateDiff.NewMembers[0].GetAddress()) | ||
} |
Oops, something went wrong.