Skip to content

Commit

Permalink
refactor: session integration
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadjsr21 committed Sep 21, 2024
1 parent 9f46e05 commit 0b857b9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5,152 deletions.
98 changes: 49 additions & 49 deletions apps/node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import DeviceConnection, {
updateLogger as updateLoggerHid,
updateLogger as updateLoggerHid,
} from '@cypherock/sdk-hw-hid';
import DeviceConnectionSerialport from '@cypherock/sdk-hw-serialport';
import { ManagerApp, updateLogger } from '@cypherock/sdk-app-manager';
import { SDK, updateLogger as updateLoggerCore } from '@cypherock/sdk-core';
import { updateLogger as updateLoggerCore } from '@cypherock/sdk-core';
import { IDeviceConnection } from '@cypherock/sdk-interfaces';
import * as bitcoinJsLib from 'bitcoinjs-lib';
import * as nearApiJs from 'near-api-js';
Expand All @@ -13,71 +13,71 @@ import { setEthersLib } from '@cypherock/sdk-app-evm';
import { setNearApiJs } from '@cypherock/sdk-app-near';
import { setSolanaWeb3 } from '@cypherock/sdk-app-solana';
import { ethers } from 'ethers';
import { createServiceLogger } from './logger';
import { InheritanceApp } from '@cypherock/sdk-app-inheritance';
import { createServiceLogger } from './logger';

const run = async () => {
updateLogger(createServiceLogger);
updateLoggerCore(createServiceLogger);
updateLoggerHid(createServiceLogger);
updateLogger(createServiceLogger);
updateLoggerCore(createServiceLogger);
updateLoggerHid(createServiceLogger);

setBitcoinJSLib(bitcoinJsLib);
setEthersLib(ethers);
setNearApiJs(nearApiJs);
setSolanaWeb3(solanaWeb3);
setBitcoinJSLib(bitcoinJsLib);
setEthersLib(ethers);
setNearApiJs(nearApiJs);
setSolanaWeb3(solanaWeb3);

let connection: IDeviceConnection;
let connection: IDeviceConnection;

try {
connection = await DeviceConnection.create();
} catch (error) {
connection = await DeviceConnectionSerialport.create();
}
try {
connection = await DeviceConnection.create();
} catch (error) {
connection = await DeviceConnectionSerialport.create();
}

console.log('started');
console.log('started');

const managerApp = await ManagerApp.create(connection);
const managerApp = await ManagerApp.create(connection);

const { walletList } = await managerApp.getWallets();
const { walletList } = await managerApp.getWallets();

const inheritanceApp = await InheritanceApp.create(connection);
const inheritanceApp = await InheritanceApp.create(connection);

setTimeout(async () => {
//console.log('aborting');
//await inheritanceApp.abort();
//console.log('aborted');
}, 3000);
setTimeout(async () => {
// console.log('aborting');
// await inheritanceApp.abort();
// console.log('aborted');
}, 3000);

//console.log(walletList[1].id.join(','));
const walletId = walletList[1].id;
// console.log(walletList[1].id.join(','));
const walletId = walletList[1].id;

//await inheritanceApp.authWallet({
// challenge: walletId,
// walletId: walletId,
// withPublicKey: true,
// type: 'wallet-based',
//});
// await inheritanceApp.authWallet({
// challenge: walletId,
// walletId: walletId,
// withPublicKey: true,
// type: 'wallet-based',
// });

await inheritanceApp.testSessionStart();
await inheritanceApp.startSession();

const thing = await inheritanceApp.encryptMessagesWithPin({
walletId: walletId,
messages: {
2: {
value: 'test value',
verifyOnDevice: true,
},
},
});
const thing = await inheritanceApp.encryptMessagesWithPin({
walletId,
messages: {
2: {
value: 'test value',
verifyOnDevice: true,
},
},
});

const output = await inheritanceApp.decryptMessagesWithPin({
walletId: walletId,
encryptedData: thing.encryptedPacket,
});
const output = await inheritanceApp.decryptMessagesWithPin({
walletId,
encryptedData: thing.encryptedPacket,
});

await inheritanceApp.testSessionStop();
await inheritanceApp.closeSession();

console.log(JSON.stringify(output));
console.log(JSON.stringify(output));
};

run();
4 changes: 2 additions & 2 deletions packages/app-inheritance/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export class InheritanceApp {
return this.sdk.runOperation(() => operations.authWallet(this.sdk, params));
}

public async testSessionStart() {
public async startSession() {
return this.sdk.runOperation(() => this.sdk.startSession());
}

public async testSessionStop() {
public async closeSession() {
return this.sdk.runOperation(() => this.sdk.closeSession());
}

Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/commands/startSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export interface IStartSessionParams {
getNewSequenceNumber: () => Promise<number>;
}

export interface IStartSessionResult {
sessionId: string;
sessionAge: number;
}

const sendSessionCommand = async (
params: IStartSessionParams,
data: DeepPartial<Msg['sessionStart']>,
Expand Down Expand Up @@ -83,7 +88,7 @@ const waitForSessionResult = async (

export const startSession = async (
params: IStartSessionParams,
): Promise<string> => {
): Promise<IStartSessionResult> => {
assert(params.connection, 'Invalid connection');
assert(params.getNewSequenceNumber, 'Invalid getNewSequenceNumber');

Expand Down Expand Up @@ -115,5 +120,11 @@ export const startSession = async (
const { confirmationStart } = await waitForSessionResult(params);
assertOrThrowInvalidResult(confirmationStart);

return '';
assert(serverInitiateResponse.sessionId, 'Invalid session ID from server');
assert(serverInitiateResponse.sessionAge, 'Invalid session age from server');

return {
sessionId: serverInitiateResponse.sessionId,
sessionAge: serverInitiateResponse.sessionAge,
};
};
2 changes: 1 addition & 1 deletion packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export class SDK implements ISDK {
public async startSession(
onStatus?: commands.IStartSessionParams['onStatus'],
options?: commands.IStartSessionParams['options'],
): Promise<string> {
): Promise<commands.IStartSessionResult> {
await this.validateNotInBootloaderMode();
assert(
this.packetVersion,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IInitiateServerSessionResult {
publicKey?: string;
sessionAge?: number;
signature?: string;
sessionId?: string;
}
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export interface ISDK {
startSession(
onStatus?: commands.IStartSessionParams['onStatus'],
options?: commands.IStartSessionParams['options'],
): Promise<string>;
): Promise<commands.IStartSessionResult>;
closeSession(
onStatus?: commands.ICloseSessionParams['onStatus'],
options?: commands.ICloseSessionParams['options'],
Expand Down
Loading

0 comments on commit 0b857b9

Please sign in to comment.