@ledgerhq/wallet-api-simulator 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,131 @@
1
+ <p align="center">
2
+ <img src="https://user-images.githubusercontent.com/9203826/154288895-670f5c23-81a1-4307-a080-1af83f7f8356.svg" align="center" alt="Ledger" />
3
+ <h2 align="center">WalletAPI Simulator</h2>
4
+ <p align="center">Test and develop your application or service without <a href="https://www.ledger.com/ledger-live">Ledger Live</a></p>
5
+ </p>
6
+ <p align="center">
7
+ <p align="center">
8
+ <a href="https://www.npmjs.com/package/@ledgerhq/wallet-api-simulator?activeTab=versions">
9
+ <img src="https://img.shields.io/npm/v/@ledgerhq/wallet-api-simulator.svg?style=flat-square" />
10
+ </a>
11
+ <a href="https://opensource.org/licenses/Apache-2.0">
12
+ <img alt="License" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg" />
13
+ </a>
14
+ <a href="https://github.com/LedgerHQ/wallet-api/actions">
15
+ <img alt="Tests Passing" src="https://github.com/LedgerHQ/wallet-api/workflows/Release/badge.svg" />
16
+ </a>
17
+ <a href="https://codecov.io/gh/LedgerHQ/wallet-api">
18
+ <img src="https://codecov.io/gh/LedgerHQ/wallet-api/branch/main/graph/badge.svg" />
19
+ </a>
20
+ <a href="https://github.com/LedgerHQ/wallet-api/issues">
21
+ <img alt="Issues" src="https://img.shields.io/github/issues/LedgerHQ/wallet-api?color=0088ff" />
22
+ </a>
23
+ <a href="https://github.com/LedgerHQ/wallet-api/pulls">
24
+ <img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/LedgerHQ/wallet-api?color=0088ff" />
25
+ </a>
26
+ <a href="https://discord.gg/y6nZhxv2bC">
27
+ <img alt="Discord" src="https://img.shields.io/discord/885256081289379850?color=1C1CE1&label=Ledger%20%7C%20Discord%20%F0%9F%91%8B%20&style=flat-square" />
28
+ </a>
29
+
30
+
31
+ </p>
32
+
33
+ <p align="center">
34
+ <a href="https://developers.ledger.com/docs/live-app/start-here/">Full documentation</a>
35
+ ·
36
+ <a href="https://github.com/LedgerHQ/wallet-api/issues/new/choose">Report Bug</a>
37
+ ·
38
+ <a href="https://github.com/LedgerHQ/wallet-api/issues/new/choose">Request Feature</a>
39
+ </p>
40
+ </p>
41
+
42
+ ## Overview
43
+
44
+ The Ledger Wallet Simulator is a comprehensive tool designed to emulate the behavior of a Ledger device simulating responses and interactions with the wallet-API. It aids developers in testing and interacting with Ledger-related applications without requiring a physical Ledger device or necessitating the run of your application inside the WebView of the LedgerLive software. This documentation will guide you through the process of setting up and using the simulator.
45
+
46
+ By following this documentation, you'll be equipped to install, set up, and make the most out of this simulator. If any issues arise or further customization is needed, consider diving deeper into the source code or consulting the official documentation.
47
+
48
+ ## Table of Contents
49
+
50
+ - [Ledger Wallet Simulator Documentation](#ledger-wallet-simulator-documentation)
51
+ - [Overview](#overview)
52
+ - [Table of Contents](#table-of-contents)
53
+ - [Installation](#installation)
54
+ - [Getting Started](#getting-started)
55
+ - [1. **Setting Up Transport**:](#1-setting-up-transport)
56
+ - [2. **Creating Client**:](#2-creating-client)
57
+ - [Working with Profiles](#working-with-profiles)
58
+ - [Using the Simulator](#using-the-simulator)
59
+
60
+ ---
61
+
62
+ ## Installation
63
+
64
+ To install the Ledger Wallet Simulator, you'll first need to include the necessary dependencies in your project.
65
+
66
+ ```sh
67
+ npm install @ledgerhq/wallet-api-client
68
+ ```
69
+
70
+ Then, install the simulator package:
71
+
72
+ ```sh
73
+ npm install @ledgerHQ/simulator
74
+ ```
75
+
76
+ ---
77
+
78
+ ## Getting Started
79
+
80
+ ### 1. **Setting Up Transport**:
81
+
82
+ Begin by setting up the transport using the `getSimulatorTransport` function.
83
+
84
+ ```typescript
85
+ import { getSimulatorTransport, profiles } from "@ledgerHQ/simulator";
86
+
87
+ const transport = getSimulatorTransport(profiles.STANDARD);
88
+ transport.onMessage = handleMessageFunction; // Replace with your message handler.
89
+ ```
90
+
91
+ ### 2. **Creating Client**:
92
+
93
+ Create a `createClient` function that return a new client. The provided simulated transport should be passed to the `WalletAPIClient()` as a parameter to effectively interact with the `WalletAPIClient`. Normally, `WalletAPIClient` only works with real transport, but the simulator provides a simulated transport for this purpose.
94
+
95
+ Here's a basic form:
96
+
97
+ ```typescript
98
+ function createClient() {
99
+ // insert the transport created above
100
+ return new WalletAPIClient(transport);
101
+ }
102
+ ```
103
+
104
+ ---
105
+
106
+ ## Working with Profiles
107
+
108
+ The simulator comes with predefined profiles (`STANDARD` and `DEVICE`), which determine the behavior and data of the simulator. You can easily extend these profiles by spreading inside the profile object:
109
+
110
+ ```typescript
111
+ const extendedProfile = {
112
+ ...profiles.STANDARD,
113
+ yourInfo: "yourValue", // Add any custom information here.
114
+ };
115
+ ```
116
+
117
+ ---
118
+
119
+ ## Using the Simulator
120
+
121
+ The primary use case for the simulator is to emulate a Ledger device's behavior for development and testing purposes. Here's a basic example:
122
+
123
+ ```typescript
124
+ // Create a client
125
+ const client = createClient();
126
+
127
+ // List accounts
128
+ const response = await client.transaction.list();
129
+
130
+ console.log(response);
131
+ ```
package/lib/helpers.d.ts CHANGED
@@ -1,4 +1,11 @@
1
- import type { WalletAPIServer } from "@ledgerhq/wallet-api-server";
1
+ import type { WalletAPIServer, WalletHandlers } from "@ledgerhq/wallet-api-server";
2
2
  import type { SimulatorProfile } from "./types";
3
3
  export declare function applyProfile(serverInstance: WalletAPIServer, profile: SimulatorProfile): void;
4
+ type MockedResponse<K extends keyof WalletHandlers, H extends WalletHandlers[K] = WalletHandlers[K]> = ReturnType<H> | H;
5
+ export declare function declarativeHandler<K extends keyof WalletHandlers>(mocks: MockedResponse<K>[]): WalletHandlers[K];
6
+ export type MockedHandlers = {
7
+ [K in keyof Partial<WalletHandlers>]: MockedResponse<K>[];
8
+ };
9
+ export declare function declarativeHandlers(mocks: MockedHandlers): Partial<WalletHandlers>;
10
+ export {};
4
11
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,YAAY,CAC1B,cAAc,EAAE,eAAe,EAC/B,OAAO,EAAE,gBAAgB,QAM1B"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,YAAY,CAC1B,cAAc,EAAE,eAAe,EAC/B,OAAO,EAAE,gBAAgB,QAM1B;AAED,KAAK,cAAc,CACjB,CAAC,SAAS,MAAM,cAAc,EAC9B,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,IAC7C,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEtB,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,cAAc,EAC/D,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,GACzB,cAAc,CAAC,CAAC,CAAC,CAuBnB;AAED,MAAM,MAAM,cAAc,GAAG;KAC1B,CAAC,IAAI,MAAM,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,EAAE;CAC1D,CAAC;AAEF,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,cAAc,CAAC,CAUzB"}
package/lib/helpers.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyProfile = void 0;
3
+ exports.declarativeHandlers = exports.declarativeHandler = exports.applyProfile = void 0;
4
4
  function applyProfile(serverInstance, profile) {
5
5
  serverInstance.setAccounts(profile.accounts);
6
6
  serverInstance.setCurrencies(profile.currencies);
@@ -8,4 +8,34 @@ function applyProfile(serverInstance, profile) {
8
8
  serverInstance.setHandlers(profile.methods);
9
9
  }
10
10
  exports.applyProfile = applyProfile;
11
+ function declarativeHandler(mocks) {
12
+ let numCalls = 0;
13
+ // @ts-expect-error: issue with types
14
+ return (...args) => {
15
+ // Finding the mock matching with the number of calls
16
+ // Or fallback to the first mock
17
+ const mock = numCalls > mocks.length ? mocks[0] : mocks[numCalls];
18
+ numCalls += 1;
19
+ if (!mock) {
20
+ return Promise.reject(new Error("No mock object found"));
21
+ }
22
+ if (typeof mock === "function") {
23
+ // @ts-expect-error: issue with types
24
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
25
+ return mock(...args);
26
+ }
27
+ return mock;
28
+ };
29
+ }
30
+ exports.declarativeHandler = declarativeHandler;
31
+ function declarativeHandlers(mocks) {
32
+ const handlers = {};
33
+ for (const key in mocks) {
34
+ // @ts-expect-error: issue with types
35
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
36
+ handlers[key] = declarativeHandler(mocks[key]);
37
+ }
38
+ return handlers;
39
+ }
40
+ exports.declarativeHandlers = declarativeHandlers;
11
41
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAGA,SAAgB,YAAY,CAC1B,cAA+B,EAC/B,OAAyB;IAEzB,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,cAAc,CAAC,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACjD,cAAc,CAAC,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACnD,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AAC9C,CAAC;AARD,oCAQC"}
1
+ {"version":3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AAG,CAAC,CAAC,CAAC;KAChD;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAZD,kDAYC"}
@@ -1,5 +1,6 @@
1
1
  import type { Transport } from "@ledgerhq/wallet-api-core";
2
2
  import { CustomHandlers } from "@ledgerhq/wallet-api-server";
3
3
  import type { SimulatorProfile } from "./types";
4
+ export { declarativeHandlers } from "./helpers";
4
5
  export declare function getSimulatorTransport(profile: SimulatorProfile, customHandlers?: CustomHandlers): Transport;
5
6
  //# sourceMappingURL=transport.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EACL,cAAc,EAGf,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,gBAAgB,EACzB,cAAc,CAAC,EAAE,cAAc,GAC9B,SAAS,CAkCX"}
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EACL,cAAc,EAGf,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,gBAAgB,EACzB,cAAc,CAAC,EAAE,cAAc,GAC9B,SAAS,CAuCX"}
package/lib/transport.js CHANGED
@@ -1,27 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSimulatorTransport = void 0;
3
+ exports.getSimulatorTransport = exports.declarativeHandlers = void 0;
4
4
  const wallet_api_server_1 = require("@ledgerhq/wallet-api-server");
5
5
  const helpers_1 = require("./helpers");
6
+ var helpers_2 = require("./helpers");
7
+ Object.defineProperty(exports, "declarativeHandlers", { enumerable: true, get: function () { return helpers_2.declarativeHandlers; } });
6
8
  function getSimulatorTransport(profile, customHandlers) {
7
- // eslint-disable-next-line prefer-const
8
- let clientTransport;
9
9
  const serverTransport = {
10
10
  onMessage: undefined,
11
11
  send: (payload) => {
12
- console.log("wallet -> app", payload);
13
- if (clientTransport?.onMessage) {
14
- clientTransport.onMessage(payload);
15
- }
12
+ console.info("wallet -> app", payload);
13
+ // Using setTimeout to simulate async call (Do we want to keep this sync ?)
14
+ // It also avoids an act warning when using RTL to test components
15
+ setTimeout(() => {
16
+ if (clientTransport.onMessage) {
17
+ clientTransport.onMessage(payload);
18
+ }
19
+ }, 0);
16
20
  },
17
21
  };
18
- clientTransport = {
22
+ const clientTransport = {
19
23
  onMessage: undefined,
20
24
  send: (payload) => {
21
- console.log("app -> wallet", payload);
22
- if (serverTransport?.onMessage) {
23
- serverTransport.onMessage(payload);
24
- }
25
+ console.info("app -> wallet", payload);
26
+ // Using setTimeout to simulate async call (Do we want to keep this sync ?)
27
+ // It also avoids an act warning when using RTL to test components
28
+ setTimeout(() => {
29
+ if (serverTransport.onMessage) {
30
+ serverTransport.onMessage(payload);
31
+ }
32
+ }, 0);
25
33
  },
26
34
  };
27
35
  const serverInstance = new wallet_api_server_1.WalletAPIServer(serverTransport, profile.config, wallet_api_server_1.defaultLogger, customHandlers);
@@ -1 +1 @@
1
- {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":";;;AACA,mEAIqC;AACrC,uCAAyC;AAGzC,SAAgB,qBAAqB,CACnC,OAAyB,EACzB,cAA+B;IAE/B,wCAAwC;IACxC,IAAI,eAAsC,CAAC;IAE3C,MAAM,eAAe,GAAc;QACjC,SAAS,EAAE,SAAS;QACpB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAChB,OAAO,C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;QACpB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAChB,OAAO,CAAC,IAAI,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;YACvC,2EAA2E;YAC3E,kEAAkE;YAClE,UAAU,CAAC,GAAG,EAAE;gBACd,IAAI,eAAe,CAAC,SAAS,EAAE;oBAC7B,eAAe,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;iBACpC;YACH,CAAC,EAAE,CAAC,CAAC,CAAC;QACR,CAAC;KACF,CAAC;IAEF,MAAM,cAAc,GAAG,IAAI,mCAAe,CACxC,eAAe,EACf,OAAO,CAAC,MAAM,EACd,iCAAa,EACb,cAAc,CACf,CAAC;IAEF,IAAA,sBAAY,EAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAEtC,OAAO,eAAe,CAAC;AACzB,CAAC;AA1CD,sDA0CC"}
@@ -1,4 +1,11 @@
1
- import type { WalletAPIServer } from "@ledgerhq/wallet-api-server";
1
+ import type { WalletAPIServer, WalletHandlers } from "@ledgerhq/wallet-api-server";
2
2
  import type { SimulatorProfile } from "./types";
3
3
  export declare function applyProfile(serverInstance: WalletAPIServer, profile: SimulatorProfile): void;
4
+ type MockedResponse<K extends keyof WalletHandlers, H extends WalletHandlers[K] = WalletHandlers[K]> = ReturnType<H> | H;
5
+ export declare function declarativeHandler<K extends keyof WalletHandlers>(mocks: MockedResponse<K>[]): WalletHandlers[K];
6
+ export type MockedHandlers = {
7
+ [K in keyof Partial<WalletHandlers>]: MockedResponse<K>[];
8
+ };
9
+ export declare function declarativeHandlers(mocks: MockedHandlers): Partial<WalletHandlers>;
10
+ export {};
4
11
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,YAAY,CAC1B,cAAc,EAAE,eAAe,EAC/B,OAAO,EAAE,gBAAgB,QAM1B"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACf,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,YAAY,CAC1B,cAAc,EAAE,eAAe,EAC/B,OAAO,EAAE,gBAAgB,QAM1B;AAED,KAAK,cAAc,CACjB,CAAC,SAAS,MAAM,cAAc,EAC9B,CAAC,SAAS,cAAc,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,IAC7C,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEtB,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,cAAc,EAC/D,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,GACzB,cAAc,CAAC,CAAC,CAAC,CAuBnB;AAED,MAAM,MAAM,cAAc,GAAG;KAC1B,CAAC,IAAI,MAAM,OAAO,CAAC,cAAc,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,EAAE;CAC1D,CAAC;AAEF,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,cAAc,GACpB,OAAO,CAAC,cAAc,CAAC,CAUzB"}
package/lib-es/helpers.js CHANGED
@@ -4,4 +4,32 @@ export function applyProfile(serverInstance, profile) {
4
4
  serverInstance.setPermissions(profile.permissions);
5
5
  serverInstance.setHandlers(profile.methods);
6
6
  }
7
+ export function declarativeHandler(mocks) {
8
+ let numCalls = 0;
9
+ // @ts-expect-error: issue with types
10
+ return (...args) => {
11
+ // Finding the mock matching with the number of calls
12
+ // Or fallback to the first mock
13
+ const mock = numCalls > mocks.length ? mocks[0] : mocks[numCalls];
14
+ numCalls += 1;
15
+ if (!mock) {
16
+ return Promise.reject(new Error("No mock object found"));
17
+ }
18
+ if (typeof mock === "function") {
19
+ // @ts-expect-error: issue with types
20
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
21
+ return mock(...args);
22
+ }
23
+ return mock;
24
+ };
25
+ }
26
+ export function declarativeHandlers(mocks) {
27
+ const handlers = {};
28
+ for (const key in mocks) {
29
+ // @ts-expect-error: issue with types
30
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
31
+ handlers[key] = declarativeHandler(mocks[key]);
32
+ }
33
+ return handlers;
34
+ }
7
35
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,YAAY,CAC1B,cAA+B,EAC/B,OAAyB;IAEzB,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,cAAc,CAAC,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACjD,cAAc,CAAC,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACnD,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AAC9C,CAAC"}
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,YAAY,CAC1B,cAA+B,EAC/B,OAAyB;IAEzB,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC7C,cAAc,CAAC,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACjD,cAAc,CAAC,cAAc,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACnD,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AAC9C,CAAC;AAOD,MAAM,UAAU,kBAAkB,CAChC,KAA0B;IAE1B,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,qCAAqC;IACrC,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE;QACjB,qDAAqD;QACrD,gCAAgC;QAChC,MAAM,IAAI,GAAG,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAElE,QAAQ,IAAI,CAAC,CAAC;QAEd,IAAI,CAAC,IAAI,EAAE;YACT,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;SAC1D;QAED,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;YAC9B,qCAAqC;YACrC,iEAAiE;YACjE,OAAO,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;SACtB;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC;AAMD,MAAM,UAAU,mBAAmB,CACjC,KAAqB;IAErB,MAAM,QAAQ,GAAG,EAAE,CAAC;IAEpB,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE;QACvB,qCAAqC;QACrC,iEAAiE;QACjE,QAAQ,CAAC,GAAG,CAAC,GAAG,kBAAkB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;KAChD;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import type { Transport } from "@ledgerhq/wallet-api-core";
2
2
  import { CustomHandlers } from "@ledgerhq/wallet-api-server";
3
3
  import type { SimulatorProfile } from "./types";
4
+ export { declarativeHandlers } from "./helpers";
4
5
  export declare function getSimulatorTransport(profile: SimulatorProfile, customHandlers?: CustomHandlers): Transport;
5
6
  //# sourceMappingURL=transport.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EACL,cAAc,EAGf,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,gBAAgB,EACzB,cAAc,CAAC,EAAE,cAAc,GAC9B,SAAS,CAkCX"}
1
+ {"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EACL,cAAc,EAGf,MAAM,6BAA6B,CAAC;AAErC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,gBAAgB,EACzB,cAAc,CAAC,EAAE,cAAc,GAC9B,SAAS,CAuCX"}
@@ -1,24 +1,31 @@
1
1
  import { WalletAPIServer, defaultLogger, } from "@ledgerhq/wallet-api-server";
2
2
  import { applyProfile } from "./helpers";
3
+ export { declarativeHandlers } from "./helpers";
3
4
  export function getSimulatorTransport(profile, customHandlers) {
4
- // eslint-disable-next-line prefer-const
5
- let clientTransport;
6
5
  const serverTransport = {
7
6
  onMessage: undefined,
8
7
  send: (payload) => {
9
- console.log("wallet -> app", payload);
10
- if (clientTransport?.onMessage) {
11
- clientTransport.onMessage(payload);
12
- }
8
+ console.info("wallet -> app", payload);
9
+ // Using setTimeout to simulate async call (Do we want to keep this sync ?)
10
+ // It also avoids an act warning when using RTL to test components
11
+ setTimeout(() => {
12
+ if (clientTransport.onMessage) {
13
+ clientTransport.onMessage(payload);
14
+ }
15
+ }, 0);
13
16
  },
14
17
  };
15
- clientTransport = {
18
+ const clientTransport = {
16
19
  onMessage: undefined,
17
20
  send: (payload) => {
18
- console.log("app -> wallet", payload);
19
- if (serverTransport?.onMessage) {
20
- serverTransport.onMessage(payload);
21
- }
21
+ console.info("app -> wallet", payload);
22
+ // Using setTimeout to simulate async call (Do we want to keep this sync ?)
23
+ // It also avoids an act warning when using RTL to test components
24
+ setTimeout(() => {
25
+ if (serverTransport.onMessage) {
26
+ serverTransport.onMessage(payload);
27
+ }
28
+ }, 0);
22
29
  },
23
30
  };
24
31
  const serverInstance = new WalletAPIServer(serverTransport, profile.config, defaultLogger, customHandlers);
@@ -1 +1 @@
1
- {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,eAAe,EACf,aAAa,GACd,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAGzC,MAAM,UAAU,qBAAqB,CACnC,OAAyB,EACzB,cAA+B;IAE/B,wCAAwC;IACxC,IAAI,eAAsC,CAAC;IAE3C,MAAM,eAAe,GAAc;QACjC,SAAS,EAAE,SAAS;QACpB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAChB,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;YACtC,IAAI,eAAe,EAAE,SAAS,EAAE;gBAC9B,eAAe,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;aACpC;QACH,CAAC;KACF,CAAC;IAEF,eAAe,GAAG;QAChB,SAAS,EAAE,SAAS;QACpB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAChB,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;YACtC,IAAI,eAAe,EAAE,SAAS,EAAE;gBAC9B,eAAe,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;aACpC;QACH,CAAC;KACF,CAAC;IAEF,MAAM,cAAc,GAAG,IAAI,eAAe,CACxC,eAAe,EACf,OAAO,CAAC,MAAM,EACd,aAAa,EACb,cAAc,CACf,CAAC;IAEF,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAEtC,OAAO,eAAe,CAAC;AACzB,CAAC"}
1
+ {"version":3,"file":"transport.js","s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-api-simulator",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "license": "MIT",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib-es/index.js",
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "devDependencies": {
13
13
  "@types/jest": "^29.5.4",
14
- "@types/node": "^20.5.9",
14
+ "@types/node": "^20.8.7",
15
15
  "@types/ws": "^8.5.5",
16
16
  "bignumber.js": "^9.1.2",
17
17
  "eslint": "^8.48.0",
@@ -27,9 +27,9 @@
27
27
  "@ledgerhq/hw-transport-http": "^6.28.3",
28
28
  "rxjs": "^7.8.1",
29
29
  "ws": "^8.13.0",
30
- "@ledgerhq/wallet-api-client": "1.2.0",
31
- "@ledgerhq/wallet-api-core": "1.3.0",
32
- "@ledgerhq/wallet-api-server": "1.3.0"
30
+ "@ledgerhq/wallet-api-client": "1.3.0",
31
+ "@ledgerhq/wallet-api-core": "1.4.0",
32
+ "@ledgerhq/wallet-api-server": "1.4.0"
33
33
  },
34
34
  "scripts": {
35
35
  "format:check": "prettier --check \"src\"",