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

feat: added inheritance server session management #118

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/short-ghosts-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cypherock/sdk-core': patch
---

added inheritance session management
8 changes: 3 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ jobs:

steps:
- name: checkout code repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: next-8
uses: pnpm/action-setup@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ jobs:

steps:
- name: Checkout Commit
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
with:
version: next-8
uses: pnpm/action-setup@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: 'pnpm'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@cypherock/prettier-config": "workspace:*",
"husky": "^8.0.0",
"prettier": "^3.2.4",
"turbo": "latest"
"turbo": "^1.13.4"
},
"engines": {
"node": ">=18.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: '.',
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '/dist/'],
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '/__helpers__/', '/dist/'],
testMatch: [
'**/tests/**/*.[jt]s?(x)',
'**/__tests__/**/*.[jt]s?(x)',
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"@cypherock/sdk-interfaces": "workspace:^0.0.15",
"@cypherock/sdk-utils": "workspace:^0.0.18",
"axios": "^1.3.4",
"compare-versions": "6.0.0-rc.1",
"protobufjs": "^7.2.2",
"uuid": "^9.0.0"
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/__mocks__/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IDeviceConnection } from '@cypherock/sdk-interfaces';
import { jest } from '@jest/globals';
import { Status } from '../encoders/types';
import { PacketVersion } from '../utils';

type SendCommandType = (params: {
connection: IDeviceConnection;
rawData?: string;
protoData?: string;
version: PacketVersion;
sequenceNumber: number;
maxTries?: number;
timeout?: number;
}) => Promise<void>;

type WaitForResultType = (params: {
connection: IDeviceConnection;
sequenceNumber: number;
appletId: number;
onStatus?: (status: Status) => void;
version: PacketVersion;
options?: { interval?: number; timeout?: number; maxTries?: number };
allowCoreData?: boolean;
}) => Promise<Uint8Array>;

export const sendCommand: jest.Mock<SendCommandType> =
jest.fn<SendCommandType>();

export const waitForResult: jest.Mock<WaitForResultType> =
jest.fn<WaitForResultType>();

jest.mock('../operations/helpers/sendCommand', () => {
const originalModule: any = jest.requireActual(
'../operations/helpers/sendCommand',
);

return {
__esModule: true,
...originalModule,
sendCommand,
};
});

jest.mock('../operations/proto/waitForResult', () => {
const originalModule: any = jest.requireActual(
'../operations/proto/waitForResult',
);

return {
__esModule: true,
...originalModule,
waitForResult,
};
});
26 changes: 26 additions & 0 deletions packages/core/src/__mocks__/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { jest } from '@jest/globals';
import {
IInitiateServerSessionParams,
IInitiateServerSessionResult,
} from '../services/types';

type InitiateServerSessionType = (
params: IInitiateServerSessionParams,
) => Promise<IInitiateServerSessionResult>;
type StartServerSessionType = (params: { sessionId: string }) => Promise<void>;

export const initiateServerSession: jest.Mock<InitiateServerSessionType> =
jest.fn<InitiateServerSessionType>();
export const startServerSession: jest.Mock<StartServerSessionType> =
jest.fn<StartServerSessionType>();

jest.mock('../services', () => {
const originalModule: any = jest.requireActual('../services');

return {
__esModule: true,
...originalModule,
initiateServerSession,
startServerSession,
};
});
97 changes: 97 additions & 0 deletions packages/core/src/commands/closeSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
DeviceAppError,
DeviceAppErrorType,
IDeviceConnection,
} from '@cypherock/sdk-interfaces';
import { assert, uint8ArrayToHex } from '@cypherock/sdk-utils';
import { DeepPartial, Msg } from '../encoders/proto/generated/core';
import { SessionCloseResponse } from '../encoders/proto/generated/session';
import { ISessionCloseResponse } from '../encoders/proto/generated/types';
import { Status } from '../encoders/types';
import { sendCommand } from '../operations/helpers';
import { waitForResult } from '../operations/proto';
import {
assertOrThrowInvalidResult,
PacketVersionMap,
parseCommonError,
} from '../utils';

export interface ICloseSessionParams {
connection: IDeviceConnection;
onStatus?: (status: Status) => void;
options?: { interval?: number; timeout?: number; maxTries?: number };
getSequenceNumber: () => Promise<number>;
getNewSequenceNumber: () => Promise<number>;
}

const sendSessionCommand = async (
params: ICloseSessionParams,
data: DeepPartial<Msg['sessionClose']>,
) => {
const { maxTries, timeout } = params.options ?? {};

const msgData = uint8ArrayToHex(
Msg.encode(Msg.create({ sessionClose: data })).finish(),
);

await sendCommand({
connection: params.connection,
protoData: msgData,
rawData: '',
version: PacketVersionMap.v3,
maxTries,
sequenceNumber: await params.getNewSequenceNumber(),
timeout,
});
};

const waitForSessionResult = async (
params: ICloseSessionParams,
): Promise<ISessionCloseResponse> => {
const { connection, onStatus, options } = params;

const version = PacketVersionMap.v3;

const result = await waitForResult({
connection,
appletId: 0,
sequenceNumber: await params.getSequenceNumber(),
version,
allowCoreData: true,
onStatus,
options,
});

let msg: Msg;
try {
msg = Msg.decode(result);
} catch (error) {
throw new DeviceAppError(DeviceAppErrorType.INVALID_MSG_FROM_DEVICE);
}

const response: SessionCloseResponse | undefined = msg.sessionClose?.response;

assertOrThrowInvalidResult(response);

if (response.commonError) {
parseCommonError(response.commonError);
}

return response;
};

export const closeSession = async (
params: ICloseSessionParams,
): Promise<void> => {
assert(params.connection, 'Invalid connection');
assert(params.getNewSequenceNumber, 'Invalid getNewSequenceNumber');

await sendSessionCommand(params, {
request: {
clear: {},
},
});

const { clear } = await waitForSessionResult(params);
assertOrThrowInvalidResult(clear);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import {
IDeviceConnection,
} from '@cypherock/sdk-interfaces';
import { assert, uint8ArrayToHex } from '@cypherock/sdk-utils';
import { Msg, Status } from '../../encoders/proto/generated/core';
import { IAppVersionResultResponse } from '../../encoders/proto/generated/types';
import { AppVersionResponse } from '../../encoders/proto/generated/version';
import { Msg } from '../encoders/proto/generated/core';
import { IAppVersionResultResponse } from '../encoders/proto/generated/types';
import { AppVersionResponse } from '../encoders/proto/generated/version';
import { Status } from '../encoders/types';
import { sendCommand } from '../operations/helpers';
import { waitForResult } from '../operations/proto';
import {
assertOrThrowInvalidResult,
PacketVersionMap,
parseCommonError,
} from '../../utils';
import { sendCommand } from '../helpers';
import { waitForResult } from './waitForResult';
} from '../utils';

export interface IGetAppVersionsParams {
connection: IDeviceConnection;
Expand Down Expand Up @@ -60,13 +61,17 @@ export const getAppVersions = async ({
options,
});

let response: AppVersionResponse;
let msg: Msg;
try {
response = AppVersionResponse.decode(result);
msg = Msg.decode(result);
} catch (error) {
throw new DeviceAppError(DeviceAppErrorType.INVALID_MSG_FROM_DEVICE);
}

const response: AppVersionResponse | undefined = msg.appVersion?.response;

assertOrThrowInvalidResult(response);

if (response.commonError) {
parseCommonError(response.commonError);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './getAppVersions';
export * from './startSession';
export * from './closeSession';
Loading
Loading