Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use capture timestamp instead of the timestamp on getCurrentPosition. #456

Merged
merged 1 commit into from
Jan 15, 2021
Merged
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
5 changes: 4 additions & 1 deletion src/app/shared/services/capture/capture.service.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,10 @@ export class CaptureService {
this._collectingOldProofHashes$.next(
this._collectingOldProofHashes$.value.add(getOldProof(proof).hash)
);
const collected = await this.collectorService.run(await proof.getAssets());
const collected = await this.collectorService.run(
await proof.getAssets(),
proof.timestamp
);
const newCollectingOldProofHashes = this._collectingOldProofHashes$.value;
newCollectingOldProofHashes.delete(getOldProof(proof).hash);
this._collectingOldProofHashes$.next(newCollectingOldProofHashes);
10 changes: 5 additions & 5 deletions src/app/shared/services/collector/collector.service.spec.ts
Original file line number Diff line number Diff line change
@@ -27,15 +27,15 @@ describe('CollectorService', () => {
it('should be created', () => expect(service).toBeTruthy());

it('should get the stored proof after run', async () => {
const proof = await service.run(ASSETS);
const proof = await service.run(ASSETS, Date.now());
expect(await proof.getAssets()).toEqual(ASSETS);
});

it('should remove added truth providers', async () => {
service.addFactsProvider(mockFactsProvider);
service.removeFactsProvider(mockFactsProvider);

const proof = await service.run(ASSETS);
const proof = await service.run(ASSETS, Date.now());

expect(proof.truth.providers).toEqual({});
});
@@ -44,20 +44,20 @@ describe('CollectorService', () => {
service.addSignatureProvider(mockSignatureProvider);
service.removeSignatureProvider(mockSignatureProvider);

const proof = await service.run(ASSETS);
const proof = await service.run(ASSETS, Date.now());

expect(proof.signatures).toEqual({});
});

it('should get the stored proof with provided facts', async () => {
service.addFactsProvider(mockFactsProvider);
const proof = await service.run(ASSETS);
const proof = await service.run(ASSETS, Date.now());
expect(proof.truth.providers).toEqual({ [mockFactsProvider.id]: FACTS });
});

it('should get the stored proof with provided signature', async () => {
service.addSignatureProvider(mockSignatureProvider);
const proof = await service.run(ASSETS);
const proof = await service.run(ASSETS, Date.now());
expect(proof.signatures).toEqual({ [mockSignatureProvider.id]: SIGNATURE });
});
});
11 changes: 7 additions & 4 deletions src/app/shared/services/collector/collector.service.ts
Original file line number Diff line number Diff line change
@@ -20,22 +20,25 @@ export class CollectorService {

constructor(private readonly imageStore: ImageStore) {}

async run(assets: Assets) {
const truth = await this.collectTruth(assets);
async run(assets: Assets, capturedTimestamp: number) {
const truth = await this.collectTruth(assets, capturedTimestamp);
const signatures = await this.signTargets({ assets, truth });
const proof = await Proof.from(this.imageStore, assets, truth, signatures);
proof.isCollected = true;
return proof;
}

private async collectTruth(assets: Assets): Promise<Truth> {
private async collectTruth(
assets: Assets,
capturedTimestamp: number
): Promise<Truth> {
return {
timestamp: Date.now(),
providers: Object.fromEntries(
await Promise.all(
[...this.factsProviders].map(async provider => [
provider.id,
await provider.provide(assets),
await provider.provide(assets, capturedTimestamp),
])
)
),
Original file line number Diff line number Diff line change
@@ -31,9 +31,12 @@ export class CapacitorFactsProvider implements FactsProvider {
private readonly translocoService: TranslocoService
) {}

async provide(_: Assets): Promise<Facts> {
async provide(
_: Assets,
capturedTimestamp: number = Date.now()
): Promise<Facts> {
const deviceInfo = await this.collectDeviceInfo();
const locationInfo = await this.collectLocationInfo();
const locationInfo = await this.collectLocationInfo(capturedTimestamp);
return {
[DefaultFactId.DEVICE_NAME]: deviceInfo?.model,
[DefaultFactId.GEOLOCATION_LATITUDE]: locationInfo?.coords.latitude,
@@ -65,7 +68,7 @@ export class CapacitorFactsProvider implements FactsProvider {
return { ...(await Device.getInfo()), ...(await Device.getBatteryInfo()) };
}

private async collectLocationInfo() {
private async collectLocationInfo(capturedTimestamp: number) {
const defaultGeolocationAge = 30000;
const defaultGeolocationTimeout = 20000;
const isLocationInfoCollectionEnabled = await this.isGeolocationInfoCollectionEnabled();
@@ -75,6 +78,7 @@ export class CapacitorFactsProvider implements FactsProvider {

return this.geolocationService
.getCurrentPosition({
capturedTimestamp,
enableHighAccuracy: true,
maximumAge: defaultGeolocationAge,
timeout: defaultGeolocationTimeout,
2 changes: 1 addition & 1 deletion src/app/shared/services/collector/facts/facts-provider.ts
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@ import { Assets, Facts } from '../../repositories/proof/proof';

export interface FactsProvider {
readonly id: string;
provide(assets: Assets): Promise<Facts>;
provide(assets: Assets, capturedTimestamp?: number): Promise<Facts>;
}
28 changes: 21 additions & 7 deletions src/app/shared/services/geolocation/geolocation.service.ts
Original file line number Diff line number Diff line change
@@ -11,41 +11,53 @@ export class GeolocationService {
// Cache the current position manually due to the issue of Capacitor
// geolocation plugin: https://github.com/ionic-team/capacitor/issues/3304
private currentPositionCache?: Position;
private lastRequestTimestamp?: number;
private lastCaptureTimestamp?: number;

constructor(
@Inject(GEOLOCATION_PLUGIN)
private readonly geolocationPlugin: GeolocationPlugin
) {}

async getCurrentPosition(
{ enableHighAccuracy, timeout, maximumAge }: GetCurrentPositionOptions = {
{
capturedTimestamp,
enableHighAccuracy,
timeout,
maximumAge,
}: GetCurrentPositionOptions = {
enableHighAccuracy: true,
timeout: undefined,
maximumAge: undefined,
}
): Promise<Position | undefined> {
const result = await this._getCurrentPosition({
capturedTimestamp,
enableHighAccuracy,
timeout,
maximumAge,
});
this.lastRequestTimestamp = Date.now();
this.lastCaptureTimestamp = capturedTimestamp;
return result;
}

private async _getCurrentPosition(
{ enableHighAccuracy, timeout, maximumAge }: GetCurrentPositionOptions = {
{
capturedTimestamp,
enableHighAccuracy,
timeout,
maximumAge,
}: GetCurrentPositionOptions = {
enableHighAccuracy: true,
timeout: undefined,
maximumAge: undefined,
}
): Promise<Position | undefined> {
if (
capturedTimestamp &&
maximumAge &&
maximumAge > 0 &&
this.currentPositionCache &&
Date.now() - this.currentPositionCache.timestamp < maximumAge
capturedTimestamp - this.currentPositionCache.timestamp < maximumAge
) {
return this.currentPositionCache;
}
@@ -74,9 +86,10 @@ export class GeolocationService {
}

if (
this.lastRequestTimestamp &&
capturedTimestamp &&
this.lastCaptureTimestamp &&
maximumAge &&
Date.now() - this.lastRequestTimestamp < maximumAge
capturedTimestamp - this.lastCaptureTimestamp < maximumAge
) {
return this.currentPositionCache;
}
@@ -94,6 +107,7 @@ async function getTimer(timeout: number) {
}

export interface GetCurrentPositionOptions {
readonly capturedTimestamp?: number;
readonly enableHighAccuracy?: boolean;
readonly timeout?: number;
readonly maximumAge?: number;