Skip to content

Commit a16adeb

Browse files
committed
adds client interoperability for apis
1 parent 66581f9 commit a16adeb

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { fetchActiveWallets, setup } from '../../api';
2+
import { getDeviceId } from '../utils/getters';
3+
import { setupTestClient } from '../utils/helpers';
4+
import { getStoredClient, setStoredClient } from '../utils/setup';
5+
6+
/**
7+
* This test is used to test the interoperability between the Class-based API and the Functional API.
8+
*/
9+
describe('client interop', () => {
10+
it('should setup the Client, then use that client data to', async () => {
11+
const client = setupTestClient();
12+
const isPaired = await client.connect(getDeviceId());
13+
expect(isPaired).toBe(true);
14+
15+
await setup({
16+
getStoredClient,
17+
setStoredClient,
18+
});
19+
20+
const activeWallets = await fetchActiveWallets();
21+
expect(activeWallets).toBeTruthy();
22+
});
23+
});

src/__test__/utils/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ProtocolConstants } from '../../protocol';
2323
import { getPathStr } from '../../shared/utilities';
2424
import { TypedTransaction } from '@ethereumjs/tx';
2525
import { getEnv } from './getters';
26+
import { setStoredClient } from './setup';
2627
const SIGHASH_ALL = 0x01;
2728
const secp256k1 = new EC('secp256k1');
2829
const ed25519 = new EdDSA('ed25519');
@@ -51,6 +52,7 @@ export function setupTestClient(
5152
name: env.APP_NAME || 'SDK Test',
5253
baseUrl: env.baseUrl || 'https://signing.gridpl.us',
5354
timeout: 120000,
55+
setStoredClient,
5456
};
5557

5658
// If the user passes a deviceID in the env, we assume they have previously

src/api/setup.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ import { buildLoadClientFn, buildSaveClientFn, queue } from './utilities';
88
* @prop {string} SetupParameters.deviceId - the device id of the client
99
* @prop {string} SetupParameters.password - the password of the client
1010
* @prop {string} SetupParameters.name - the name of the client
11+
* @prop {string} SetupParameters.appSecret - the app secret of the client
1112
* @prop {Function} SetupParameters.getStoredClient - a function that returns the stored client data
1213
* @prop {Function} SetupParameters.setStoredClient - a function that stores the client data
1314
*/
14-
type SetupParameters = {
15-
deviceId: string;
16-
password: string;
17-
name: string;
18-
getStoredClient: () => string;
19-
setStoredClient: (clientData: string | null) => void;
20-
};
15+
type SetupParameters =
16+
| {
17+
deviceId: string;
18+
password: string;
19+
name: string;
20+
appSecret?: string;
21+
getStoredClient: () => string;
22+
setStoredClient: (clientData: string | null) => void;
23+
}
24+
| {
25+
getStoredClient: () => string;
26+
setStoredClient: (clientData: string | null) => void;
27+
};
2128

2229
/**
2330
* `setup` initializes the Client and executes `connect()` if necessary. It returns a promise that
@@ -28,28 +35,29 @@ type SetupParameters = {
2835
* @param {string} SetupParameters.deviceId - the device id of the client
2936
* @param {string} SetupParameters.password - the password of the client
3037
* @param {string} SetupParameters.name - the name of the client
38+
* @param {string} SetupParameters.appSecret - the app secret of the client
3139
* @param {Function} SetupParameters.getStoredClient - a function that returns the stored client data
3240
* @param {Function} SetupParameters.setStoredClient - a function that stores the client data
3341
* @returns {Promise<boolean>} - a promise that resolves to a boolean that indicates whether the Client is paired to the application to which it's attempting to connect
3442
*
3543
*/
36-
export const setup = async ({
37-
deviceId,
38-
password,
39-
name,
40-
getStoredClient,
41-
setStoredClient,
42-
}: SetupParameters): Promise<boolean> => {
43-
if (!getStoredClient) throw new Error('Client data getter required');
44-
setSaveClient(buildSaveClientFn(setStoredClient));
44+
export const setup = async (params: SetupParameters): Promise<boolean> => {
45+
if (!params.getStoredClient) throw new Error('Client data getter required');
46+
setLoadClient(buildLoadClientFn(params.getStoredClient));
4547

46-
if (!setStoredClient) throw new Error('Client data setter required');
47-
setLoadClient(buildLoadClientFn(getStoredClient));
48+
if (!params.setStoredClient) throw new Error('Client data setter required');
49+
setSaveClient(buildSaveClientFn(params.setStoredClient));
4850

49-
if (deviceId && password && name) {
50-
const privKey = Utils.generateAppSecret(deviceId, password, name);
51-
const client = new Client({ deviceId, privKey, name });
52-
return client.connect(deviceId).then((isPaired) => {
51+
if ('deviceId' in params && 'password' in params && 'name' in params) {
52+
const privKey =
53+
params.appSecret ||
54+
Utils.generateAppSecret(params.deviceId, params.password, params.name);
55+
const client = new Client({
56+
deviceId: params.deviceId,
57+
privKey,
58+
name: params.name,
59+
});
60+
return client.connect(params.deviceId).then((isPaired) => {
5361
saveClient(client.getStateData());
5462
return isPaired;
5563
});

src/client.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { buildSaveClientFn } from './api/utilities';
12
import {
23
BASE_URL,
34
DEFAULT_ACTIVE_WALLETS,
@@ -56,6 +57,8 @@ export class Client {
5657
public activeWallets: ActiveWallets;
5758
/** A wrapper function for handling retries and injecting the {@link Client} class */
5859
private retryWrapper: (fn: any, params?: any) => Promise<any>;
60+
/** Function to set the stored client data */
61+
private setStoredClient: (clientData: string | null) => void;
5962

6063
/**
6164
* @param params - Parameters are passed as an object.
@@ -69,6 +72,7 @@ export class Client {
6972
retryCount,
7073
skipRetryOnWrongWallet,
7174
deviceId,
75+
setStoredClient,
7276
}: {
7377
/** The base URL of the signing server. */
7478
baseUrl?: string;
@@ -86,6 +90,8 @@ export class Client {
8690
skipRetryOnWrongWallet?: boolean;
8791
/** The ID of the connected Lattice */
8892
deviceId?: string;
93+
/** Function to set the stored client data */
94+
setStoredClient?: (clientData: string | null) => void;
8995
}) {
9096
this.name = name || 'Unknown';
9197
this.baseUrl = baseUrl || BASE_URL;
@@ -98,6 +104,9 @@ export class Client {
98104
this.privKey = privKey || randomBytes(32);
99105
this.key = getP256KeyPair(this.privKey);
100106
this.retryWrapper = buildRetryWrapper(this, this.retryCount);
107+
this.setStoredClient = setStoredClient
108+
? buildSaveClientFn(setStoredClient)
109+
: undefined;
101110

102111
/** The user may pass in state data to rehydrate a session that was previously cached */
103112
if (stateData) {
@@ -343,6 +352,10 @@ export class Client {
343352
if (isPaired !== undefined) this.isPaired = isPaired;
344353
if (fwVersion !== undefined) this.fwVersion = fwVersion;
345354
if (activeWallets !== undefined) this.activeWallets = activeWallets;
355+
356+
if (this.setStoredClient) {
357+
this.setStoredClient(this.getStateData());
358+
}
346359
}
347360

348361
/**

0 commit comments

Comments
 (0)