Skip to content

Commit

Permalink
maint: Update Status to be an enum like sdk-js, and export more types
Browse files Browse the repository at this point in the history
We can also export AttachResult and LogRecord from the same file,
while we're fixing the types.
Satus is the only one not exported as a type, because it's _also_
a value, so the export must not be removed when compiling the TS
(this is also what we do in sdk-js)
  • Loading branch information
tux3 committed Nov 29, 2024
1 parent ef60cc1 commit 37a177a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 48 deletions.
9 changes: 7 additions & 2 deletions example/src/test_basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Tanker, prehashPassword, errors } from '@tanker/client-react-native';
import {
Tanker,
Status,
prehashPassword,
errors,
} from '@tanker/client-react-native';
import { expect, describe, beforeEach, afterEach, it } from './framework';
import { createTanker, clearTankerDataDirs } from './tests';
import {
Expand Down Expand Up @@ -30,7 +35,7 @@ export const basicTests = () => {
});

it('has a status', async () => {
expect(tanker.status).to.equal(Tanker.statuses.STOPPED);
expect(tanker.status).to.equal(Status.STOPPED);
});

it('cannot create Tanker with a bad appId', async () => {
Expand Down
16 changes: 8 additions & 8 deletions example/src/test_tanker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tanker, setLogHandler } from '@tanker/client-react-native';
import { Tanker, Status, setLogHandler } from '@tanker/client-react-native';
import { expect, describe, beforeEach, afterEach, it } from './framework';
import {
createIdentity,
Expand All @@ -23,18 +23,18 @@ export const tankerTests = () => {
});

it('can start and stop', async () => {
expect(tanker.status).eq(Tanker.statuses.STOPPED);
expect(tanker.status).eq(Status.STOPPED);
await tanker.start(identity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_REGISTRATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_REGISTRATION_NEEDED);
await tanker.stop();
expect(tanker.status).eq(Tanker.statuses.STOPPED);
expect(tanker.status).eq(Status.STOPPED);
});

it('can reuse the Tanker object after stop', async () => {
await tanker.start(identity);
await tanker.stop();
await tanker.start(identity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_REGISTRATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_REGISTRATION_NEEDED);
});

it('calls the log handler', async () => {
Expand Down Expand Up @@ -135,7 +135,7 @@ export const tankerTests = () => {
const email = '[email protected]';
const provIdentity = await createProvisionalIdentity(email);
const result = await tanker.attachProvisionalIdentity(provIdentity);
expect(result.status).eq(Tanker.statuses.IDENTITY_VERIFICATION_NEEDED);
expect(result.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);
expect(result.verificationMethod).deep.eq({
type: 'email',
email,
Expand Down Expand Up @@ -165,7 +165,7 @@ export const tankerTests = () => {
await other.start(await createIdentity());
await other.registerIdentity({ passphrase: 'otherpass' });
await other.attachProvisionalIdentity(provIdentity);
expect(result.status).eq(Tanker.statuses.IDENTITY_VERIFICATION_NEEDED);
expect(result.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);
const verificationCode2 = await getEmailVerificationCode(email);
await expect(
other.verifyProvisionalIdentity({
Expand All @@ -191,7 +191,7 @@ export const tankerTests = () => {
const verificationCode = await getEmailVerificationCode(email);
await tanker.registerIdentity({ email, verificationCode });
const result = await tanker.attachProvisionalIdentity(provIdentity);
expect(result.status).eq(Tanker.statuses.READY);
expect(result.status).eq(Status.READY);
expect(result.verificationMethod).is.undefined;
});
});
Expand Down
48 changes: 23 additions & 25 deletions example/src/test_verify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tanker } from '@tanker/client-react-native';
import { Tanker, Status } from '@tanker/client-react-native';
import { expect, describe, beforeEach, afterEach, it } from './framework';
import {
setAppOidcConfig,
Expand Down Expand Up @@ -45,23 +45,23 @@ export const verifyTests = () => {

it('can validate a new device with a verification key', async () => {
await tanker.start(identity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_REGISTRATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_REGISTRATION_NEEDED);

const verifKey = await tanker.generateVerificationKey();
expect(verifKey).is.not.empty;
await tanker.registerIdentity({
verificationKey: verifKey,
});
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);
await tanker.stop();

const tanker2 = await createTanker();
await tanker2.start(identity);
expect(tanker2.status).eq(Tanker.statuses.IDENTITY_VERIFICATION_NEEDED);
expect(tanker2.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);
await tanker2.verifyIdentity({
verificationKey: verifKey,
});
expect(tanker2.status).eq(Tanker.statuses.READY);
expect(tanker2.status).eq(Status.READY);
await tanker2.stop();
});

Expand Down Expand Up @@ -96,32 +96,30 @@ export const verifyTests = () => {
it('can use registerIdentity to open a session with a passphrase', async () => {
await tanker.start(identity);
await tanker.registerIdentity({ passphrase: 'foo' });
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);
});

it('can use registerIdentity to open a session with a phone number', async () => {
const phoneNumber = '+33639982233';
const verificationCode = await getSMSVerificationCode(phoneNumber);

await tanker.start(identity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_REGISTRATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_REGISTRATION_NEEDED);
await tanker.registerIdentity({ phoneNumber, verificationCode });
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);
});

it('can use verifyIdentity to open a session', async () => {
await tanker.start(identity);
await tanker.registerIdentity({ passphrase: 'foo' });
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);

let secondDevice = await createTanker();
await secondDevice.start(identity);
expect(secondDevice.status).eq(
Tanker.statuses.IDENTITY_VERIFICATION_NEEDED
);
expect(secondDevice.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);

await secondDevice.verifyIdentity({ passphrase: 'foo' });
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);
await secondDevice.stop();
});

Expand All @@ -136,7 +134,7 @@ export const verifyTests = () => {
let secondDevice = await createTanker();
await secondDevice.start(identity);
await secondDevice.verifyIdentity(pass2);
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);
await secondDevice.stop();
});

Expand Down Expand Up @@ -255,7 +253,7 @@ export const verifyTests = () => {

const verificationCode = await getEmailVerificationCode(email);
await secondDevice.verifyIdentity({ email, verificationCode });
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);

expect(await secondDevice.getVerificationMethods()).to.have.deep.members([
{
Expand Down Expand Up @@ -294,7 +292,7 @@ export const verifyTests = () => {

const verificationCode = await getSMSVerificationCode(phoneNumber);
await secondDevice.verifyIdentity({ phoneNumber, verificationCode });
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);

expect(await secondDevice.getVerificationMethods()).to.have.deep.members([
{
Expand Down Expand Up @@ -346,7 +344,7 @@ export const verifyTests = () => {
await secondDevice.verifyIdentity({
oidcIdToken: oidcToken,
});
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);

await secondDevice.stop();
await setAppOidcConfig(undefined); // Cleanup
Expand All @@ -361,7 +359,7 @@ export const verifyTests = () => {
let secondDevice = await createTanker();
await secondDevice.start(identity);
await secondDevice.verifyIdentity({ e2ePassphrase });
expect(secondDevice.status).eq(Tanker.statuses.READY);
expect(secondDevice.status).eq(Status.READY);
await secondDevice.stop();
});

Expand Down Expand Up @@ -439,7 +437,7 @@ export const verifyTests = () => {
{ passphrase: 'foo' },
{ withSessionToken: true }
);
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);
expect(token).is.not.empty;
// @ts-ignore is.not.empty checks that the token is not undefined
const tokenData = base64.decode(token);
Expand Down Expand Up @@ -508,17 +506,17 @@ export const verifyTests = () => {
await tanker.start(martineIdentity);
await tanker.setOidcTestNonce(await tanker.createOidcNonce());

expect(tanker.status).eq(Tanker.statuses.IDENTITY_REGISTRATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_REGISTRATION_NEEDED);
await tanker.registerIdentity({ oidcIdToken: oidcToken });
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);
await tanker.stop();

tanker = await createTanker();
await tanker.setOidcTestNonce(await tanker.createOidcNonce());
await tanker.start(martineIdentity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_VERIFICATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);
await tanker.verifyIdentity({ oidcIdToken: oidcToken });
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);

await setAppOidcConfig(undefined); // Cleanup
});
Expand Down Expand Up @@ -549,9 +547,9 @@ export const verifyTests = () => {

tanker = await createTanker();
await tanker.start(identity);
expect(tanker.status).eq(Tanker.statuses.IDENTITY_VERIFICATION_NEEDED);
expect(tanker.status).eq(Status.IDENTITY_VERIFICATION_NEEDED);
await tanker.verifyIdentity(verif2);
expect(tanker.status).eq(Tanker.statuses.READY);
expect(tanker.status).eq(Status.READY);

await setAppOidcConfig(undefined); // Cleanup
});
Expand Down
15 changes: 11 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Native } from './native';
export type { Status, TankerOptions, NativeTanker } from './types';
export { Tanker } from './nativeWrapper';
export { Padding } from './encryptionOptions';
import { bridgeAsyncExceptions } from './errors';
export { errors } from './errors';
import type { LogRecord } from './types';
import {
type EmitterSubscription,
NativeEventEmitter,
NativeModules,
} from 'react-native';

export { Status } from './types';
export type {
AttachResult,
LogRecord,
NativeTanker,
TankerOptions,
} from './types';
export { Tanker } from './nativeWrapper';
export { Padding } from './encryptionOptions';
export { errors } from './errors';

let EVENT_LISTENER: EmitterSubscription | null = null;

export async function prehashPassword(password: string): Promise<string> {
Expand Down
25 changes: 16 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ export type TankerOptions = {
url?: string;
};

const statusDefs = [
// NOTE: This cannot be a const enum for compat with TS verbatimModuleSyntax
export enum Status {
'STOPPED' = 0,
'READY' = 1,
'IDENTITY_REGISTRATION_NEEDED' = 2,
'IDENTITY_VERIFICATION_NEEDED' = 3,
}

const statusDefs: Array<{ name: keyof typeof Status }> = [
/* 0 */ { name: 'STOPPED' },
/* 1 */ { name: 'READY' },
/* 2 */ { name: 'IDENTITY_REGISTRATION_NEEDED' },
/* 3 */ { name: 'IDENTITY_VERIFICATION_NEEDED' },
];

type Statuses = { [name: string]: number };
export const statuses: Statuses = (() => {
const h: Statuses = {};
statusDefs.forEach((def, index) => {
h[def.name] = index;
export const statuses = (() => {
const h: Partial<Record<keyof typeof Status, Status>> = {};

statusDefs.forEach((status, index) => {
h[status.name] = index as Status;
});
return h;
})();

export type Status = number;
return h as Record<keyof typeof Status, Status>;
})();

export type AttachResult = {
status: Status;
Expand Down

0 comments on commit 37a177a

Please sign in to comment.