Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions internal/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import (
"github.com/DIMO-Network/attestation-api/internal/client/identity"
"github.com/DIMO-Network/attestation-api/internal/client/telemetryapi"
"github.com/DIMO-Network/attestation-api/internal/client/tokencache"
"github.com/DIMO-Network/attestation-api/internal/client/vinvalidator"
"github.com/DIMO-Network/attestation-api/internal/config"
"github.com/DIMO-Network/attestation-api/internal/controllers/httphandlers"
"github.com/DIMO-Network/attestation-api/internal/controllers/rpc"
ddgrpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

// createControllers creates a new controllers with the given settings.
Expand All @@ -35,13 +31,6 @@ func createControllers(logger *zerolog.Logger, settings *config.Settings) (*http
return nil, nil, fmt.Errorf("failed to decode private key: %w", err)
}

// Initialize device definition API client
deviceDefGRPCClient, err := deviceDefAPIClientFromSettings(settings)
if err != nil {
return nil, nil, fmt.Errorf("failed to create device definition API client: %w", err)
}
vinValidateSerivce := vinvalidator.New(deviceDefGRPCClient)

// Initialize fingerprint repository
fingerprintRepo := fingerprint.New(fetchAPIClient)

Expand Down Expand Up @@ -75,7 +64,7 @@ func createControllers(logger *zerolog.Logger, settings *config.Settings) (*http
}

// Initialize VC service using the initialized services
vinvcService := vinvc.NewService(logger, vcRepo, identityAPI, fingerprintRepo, vinValidateSerivce, settings, privateKey)
vinvcService := vinvc.NewService(logger, vcRepo, identityAPI, fingerprintRepo, settings, privateKey)

// Initialize VehiclePositionVC service
vehiclePositionService := vehiclepositionvc.NewService(vcRepo, identityAPI, telemetryAPI, settings, privateKey)
Expand All @@ -101,14 +90,4 @@ func createControllers(logger *zerolog.Logger, settings *config.Settings) (*http
server := rpc.NewServer(vinvcService, settings)

return ctrl, server, nil

}

func deviceDefAPIClientFromSettings(settings *config.Settings) (ddgrpc.VinDecoderServiceClient, error) {
conn, err := grpc.NewClient(settings.DefinitionsGRPCAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("failed to create gRPC client: %w", err)
}
definitionsClient := ddgrpc.NewVinDecoderServiceClient(conn)
return definitionsClient, nil
}
5 changes: 0 additions & 5 deletions internal/attestation/vinvc/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,3 @@ type IdentityAPI interface {
type FingerprintRepo interface {
GetLatestFingerprintMessages(ctx context.Context, vehicle cloudevent.ERC721DID, pairedDeviceAddr models.PairedDevice) (*models.DecodedFingerprintData, error)
}

// VINAPI defines the interface for VIN validation.
type VINAPI interface {
DecodeVIN(ctx context.Context, vin, countryCode string) (string, error)
}
39 changes: 0 additions & 39 deletions internal/attestation/vinvc/interfaces_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions internal/attestation/vinvc/vinvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type Service struct {
vcRepo VCRepo
identityAPI IdentityAPI
fingerprintRepo FingerprintRepo
vinAPI VINAPI
vehicleNFTAddress string
chainID uint64
VINVCDataVersion string
Expand All @@ -46,7 +45,6 @@ func NewService(
vcRepo VCRepo,
identityService IdentityAPI,
fingerprintService FingerprintRepo,
vinService VINAPI,
settings *config.Settings,
privateKey *ecdsa.PrivateKey,
) *Service {
Expand All @@ -56,7 +54,6 @@ func NewService(
vcRepo: vcRepo,
identityAPI: identityService,
fingerprintRepo: fingerprintService,
vinAPI: vinService,
vehicleNFTAddress: settings.VehicleNFTAddress,
chainID: uint64(settings.DIMORegistryChainID),
privateKey: privateKey,
Expand Down Expand Up @@ -155,15 +152,6 @@ func (v *Service) getValidFingerPrint(ctx context.Context, vehicleInfo *models.V
if (latestFP == nil || latestFP.VIN == "") && fingerprintErr != nil {
return nil, fingerprintErr
}
decodedNameSlug, err := v.vinAPI.DecodeVIN(ctx, latestFP.VIN, countryCode)
if err != nil {
return nil, richerrors.Error{Err: err, ExternalMsg: "Server failed to decode VIN", Code: http.StatusInternalServerError}
}
if decodedNameSlug != vehicleInfo.NameSlug {
message := fmt.Sprintf("Invalid VIN Decoding expected: %s got: %s", vehicleInfo.NameSlug, decodedNameSlug)
err := fmt.Errorf("decodedNameSlug: %s != identityNameSlug: %s vin = %s", decodedNameSlug, vehicleInfo.NameSlug, latestFP.VIN)
return nil, richerrors.Error{Err: err, ExternalMsg: message, Code: http.StatusBadRequest}
}

return latestFP, nil
}
Expand Down
80 changes: 1 addition & 79 deletions internal/attestation/vinvc/vinvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Mocks struct {
vcRepo *MockVCRepo
identityAPI *MockIdentityAPI
fingerprintRepo *MockFingerprintRepo
vinAPI *MockVINAPI
}

func TestVCController_GetVINVC(t *testing.T) {
Expand Down Expand Up @@ -104,7 +103,6 @@ func TestVCController_GetVINVC(t *testing.T) {
VIN: "1HGCM82633A123456",
}
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, pariedDevice).Return(&validFP, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, validFP.VIN, "").Return(vehicleInfo.NameSlug, nil)

// Create a matcher to verify the expected VIN subject
expectedVINSubject := types.VINSubject{
Expand Down Expand Up @@ -213,7 +211,6 @@ func TestVCController_GetVINVC(t *testing.T) {

mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, device1).Return(&validFPEarliest, nil)
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, device2).Return(&validFPLatest, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, validFPLatest.VIN, "").Return(vehicleInfo.NameSlug, nil)

// Create a matcher to verify the expected VIN subject (should use the latest fingerprint)
expectedVINSubject := types.VINSubject{
Expand All @@ -228,78 +225,6 @@ func TestVCController_GetVINVC(t *testing.T) {
mocks.vcRepo.EXPECT().UploadAttestation(ctxType, matchVINSubject(expectedVINSubject)).Return(nil)
},
},
{
name: "failed to decode VIN from fingerprint message",
tokenID: 129,
setupMocks: func(mocks Mocks) {
tokenID := new(big.Int).SetInt64(129)
pariedDevice := models.PairedDevice{
Type: models.DeviceTypeAftermarket,
DID: cloudevent.ERC721DID{
ChainID: polygonChainID,
TokenID: tokenID10,
ContractAddress: common.HexToAddress(defaultNFTAddress),
},
}
vehicleInfo := &models.VehicleInfo{
PairedDevices: []models.PairedDevice{pariedDevice},
NameSlug: defaultNameSlug,
DID: cloudevent.ERC721DID{
ChainID: polygonChainID,
TokenID: tokenID,
ContractAddress: common.HexToAddress(defaultNFTAddress),
},
}
mocks.identityAPI.EXPECT().GetVehicleInfo(ctxType, vehicleInfo.DID).Return(vehicleInfo, nil)
invalidFP := models.DecodedFingerprintData{
CloudEventHeader: cloudevent.CloudEventHeader{
Source: "0x123",
Time: time.Now(),
Producer: pariedDevice.DID.String(),
},
VIN: "INVALIDVIN",
}
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, pariedDevice).Return(&invalidFP, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, "INVALIDVIN", "").Return("", errors.New("invalid VIN"))
},
expectedErrror: true,
},
{
name: "invalid VIN from fingerprint message",
tokenID: 130,
setupMocks: func(mocks Mocks) {
tokenID := big.NewInt(130)
pariedDevice := models.PairedDevice{
Type: models.DeviceTypeAftermarket,
DID: cloudevent.ERC721DID{
ChainID: polygonChainID,
TokenID: tokenID10,
ContractAddress: common.HexToAddress(defaultNFTAddress),
},
}
vehicleInfo := &models.VehicleInfo{
PairedDevices: []models.PairedDevice{pariedDevice},
NameSlug: defaultNameSlug,
DID: cloudevent.ERC721DID{
ChainID: polygonChainID,
TokenID: tokenID,
ContractAddress: common.HexToAddress(defaultNFTAddress),
},
}
mocks.identityAPI.EXPECT().GetVehicleInfo(ctxType, vehicleInfo.DID).Return(vehicleInfo, nil)
validFP := models.DecodedFingerprintData{
CloudEventHeader: cloudevent.CloudEventHeader{
Source: "0x123",
Time: time.Now(),
Producer: pariedDevice.DID.String(),
},
VIN: "1HGCM82633A123456",
}
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, pariedDevice).Return(&validFP, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, "1HGCM82633A123456", "").Return("bad_name", nil)
},
expectedErrror: true,
},
{
name: "error on generate and store VC",
tokenID: 131,
Expand Down Expand Up @@ -332,7 +257,6 @@ func TestVCController_GetVINVC(t *testing.T) {
VIN: "1HGCM82633A123456",
}
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, pariedDevice).Return(&validFP, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, validFP.VIN, "").Return(vehicleInfo.NameSlug, nil)

// Create a matcher to verify the expected VIN subject
expectedVINSubject := types.VINSubject{
Expand Down Expand Up @@ -380,7 +304,6 @@ func TestVCController_GetVINVC(t *testing.T) {
VIN: "1HGCM82633A123456",
}
mocks.fingerprintRepo.EXPECT().GetLatestFingerprintMessages(ctxType, vehicleInfo.DID, pariedDevice).Return(&validFP, nil)
mocks.vinAPI.EXPECT().DecodeVIN(ctxType, validFP.VIN, "").Return(vehicleInfo.NameSlug, nil)

// Create a matcher to verify the expected VIN subject
expectedVINSubject := types.VINSubject{
Expand All @@ -406,7 +329,6 @@ func TestVCController_GetVINVC(t *testing.T) {
vcRepo: NewMockVCRepo(ctrl),
identityAPI: NewMockIdentityAPI(ctrl),
fingerprintRepo: NewMockFingerprintRepo(ctrl),
vinAPI: NewMockVINAPI(ctrl),
}

// Set up the mocks as defined in the test case
Expand All @@ -424,7 +346,7 @@ func TestVCController_GetVINVC(t *testing.T) {
// Create a new VCController instance for this test
vcController := vinvc.NewService(&logger,
mocks.vcRepo, mocks.identityAPI,
mocks.fingerprintRepo, mocks.vinAPI,
mocks.fingerprintRepo,
settings, pk,
)

Expand Down