@hsuite/native-connect-client 1.0.0
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 +51 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/coverage-summary.json +10 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +161 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +161 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/handlers/index.html +146 -0
- package/coverage/lcov-report/src/handlers/session-handler.ts.html +223 -0
- package/coverage/lcov-report/src/handlers/signing-handler.ts.html +217 -0
- package/coverage/lcov-report/src/handlers/wallet-handler.ts.html +193 -0
- package/coverage/lcov-report/src/harness/index.html +116 -0
- package/coverage/lcov-report/src/harness/signing-harness.ts.html +352 -0
- package/coverage/lcov-report/src/index.html +146 -0
- package/coverage/lcov-report/src/memory-transport.ts.html +358 -0
- package/coverage/lcov-report/src/protocol/e2e-harness.ts.html +568 -0
- package/coverage/lcov-report/src/protocol/index.html +116 -0
- package/coverage/lcov-report/src/query-client.ts.html +277 -0
- package/coverage/lcov-report/src/reference-client.ts.html +691 -0
- package/coverage/lcov.info +981 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/handlers/index.html +146 -0
- package/coverage/src/handlers/session-handler.ts.html +223 -0
- package/coverage/src/handlers/signing-handler.ts.html +217 -0
- package/coverage/src/handlers/wallet-handler.ts.html +193 -0
- package/coverage/src/harness/index.html +116 -0
- package/coverage/src/harness/signing-harness.ts.html +352 -0
- package/coverage/src/index.html +146 -0
- package/coverage/src/memory-transport.ts.html +358 -0
- package/coverage/src/protocol/e2e-harness.ts.html +568 -0
- package/coverage/src/protocol/index.html +116 -0
- package/coverage/src/query-client.ts.html +277 -0
- package/coverage/src/reference-client.ts.html +691 -0
- package/dist/handlers/session-handler.d.ts +22 -0
- package/dist/handlers/session-handler.d.ts.map +1 -0
- package/dist/handlers/session-handler.js +41 -0
- package/dist/handlers/session-handler.js.map +1 -0
- package/dist/handlers/signing-handler.d.ts +22 -0
- package/dist/handlers/signing-handler.d.ts.map +1 -0
- package/dist/handlers/signing-handler.js +39 -0
- package/dist/handlers/signing-handler.js.map +1 -0
- package/dist/handlers/wallet-handler.d.ts +22 -0
- package/dist/handlers/wallet-handler.d.ts.map +1 -0
- package/dist/handlers/wallet-handler.js +32 -0
- package/dist/handlers/wallet-handler.js.map +1 -0
- package/dist/harness/signing-harness.d.ts +47 -0
- package/dist/harness/signing-harness.d.ts.map +1 -0
- package/dist/harness/signing-harness.js +79 -0
- package/dist/harness/signing-harness.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/memory-transport-options.d.ts +27 -0
- package/dist/memory-transport-options.d.ts.map +1 -0
- package/dist/memory-transport-options.js +11 -0
- package/dist/memory-transport-options.js.map +1 -0
- package/dist/memory-transport.d.ts +55 -0
- package/dist/memory-transport.d.ts.map +1 -0
- package/dist/memory-transport.js +81 -0
- package/dist/memory-transport.js.map +1 -0
- package/dist/protocol/e2e-harness.d.ts +65 -0
- package/dist/protocol/e2e-harness.d.ts.map +1 -0
- package/dist/protocol/e2e-harness.js +130 -0
- package/dist/protocol/e2e-harness.js.map +1 -0
- package/dist/protocol/index.d.ts +22 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +22 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/query-client.d.ts +41 -0
- package/dist/query-client.d.ts.map +1 -0
- package/dist/query-client.js +52 -0
- package/dist/query-client.js.map +1 -0
- package/dist/reference-client.d.ts +100 -0
- package/dist/reference-client.d.ts.map +1 -0
- package/dist/reference-client.js +149 -0
- package/dist/reference-client.js.map +1 -0
- package/package.json +46 -0
- package/src/handlers/session-handler.ts +46 -0
- package/src/handlers/signing-handler.ts +44 -0
- package/src/handlers/wallet-handler.ts +36 -0
- package/src/harness/signing-harness.ts +89 -0
- package/src/index.ts +24 -0
- package/src/memory-transport-options.ts +28 -0
- package/src/memory-transport.ts +91 -0
- package/src/protocol/e2e-harness.ts +161 -0
- package/src/protocol/index.ts +23 -0
- package/src/query-client.ts +64 -0
- package/src/reference-client.spec.ts +408 -0
- package/src/reference-client.ts +202 -0
- package/tests/e2e-harness.spec.ts +64 -0
- package/tests/realnet-session.spec.ts +303 -0
- package/tests/signing-harness.spec.ts +41 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +17 -0
- package/vitest.config.ts +35 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file End-to-end test harness with full wallet SDK integration.
|
|
12
|
+
* Provides complete WalletCore, ledger registration, and session management
|
|
13
|
+
* for comprehensive integration testing.
|
|
14
|
+
*
|
|
15
|
+
* @module @hsuite/native-connect-client/protocol/e2e-harness
|
|
16
|
+
*/
|
|
17
|
+
import { ReferenceClient } from '../reference-client';
|
|
18
|
+
import { MemoryTransport } from '../memory-transport';
|
|
19
|
+
import { WalletCore } from '@hsuite/native-connect-sdk';
|
|
20
|
+
import type { LedgerAdapter, ChannelInvite } from '@hsuite/native-connect-sdk';
|
|
21
|
+
/**
|
|
22
|
+
* @description Configuration options for end-to-end test harness
|
|
23
|
+
* @property {LedgerAdapter} ledgerAdapter - Ledger adapter to register
|
|
24
|
+
* @property {object} [session] - Optional session configuration
|
|
25
|
+
*/
|
|
26
|
+
export interface HarnessOptions {
|
|
27
|
+
ledgerAdapter: LedgerAdapter;
|
|
28
|
+
session?: {
|
|
29
|
+
/** Session request - ChannelInvite object */
|
|
30
|
+
request: ChannelInvite;
|
|
31
|
+
approval?: Parameters<WalletCore['approveSession']>[1];
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @class EndToEndHarness
|
|
36
|
+
* @description End-to-end test harness with full wallet SDK integration.
|
|
37
|
+
*
|
|
38
|
+
* Provides:
|
|
39
|
+
* - Complete WalletCore instance with ledger registration
|
|
40
|
+
* - Reference client for RPC communication
|
|
41
|
+
* - In-memory transport for isolated testing
|
|
42
|
+
* - Session establishment utilities
|
|
43
|
+
*/
|
|
44
|
+
export declare class EndToEndHarness {
|
|
45
|
+
private readonly options;
|
|
46
|
+
readonly client: ReferenceClient;
|
|
47
|
+
readonly wallet: WalletCore;
|
|
48
|
+
readonly transport: MemoryTransport;
|
|
49
|
+
constructor(options: HarnessOptions);
|
|
50
|
+
/**
|
|
51
|
+
* @description Establish test session with wallet
|
|
52
|
+
* @returns Session identifier
|
|
53
|
+
*/
|
|
54
|
+
establishSession(): Promise<string>;
|
|
55
|
+
/**
|
|
56
|
+
* @description Create a default channel invite for testing
|
|
57
|
+
* @returns ChannelInvite object
|
|
58
|
+
*/
|
|
59
|
+
private createDefaultInvite;
|
|
60
|
+
/**
|
|
61
|
+
* @description Cleanup harness resources
|
|
62
|
+
*/
|
|
63
|
+
teardown(): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=e2e-harness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"e2e-harness.d.ts","sourceRoot":"","sources":["../../src/protocol/e2e-harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EACL,UAAU,EAMX,MAAM,4BAA4B,CAAC;AAEpC,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE/E;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,aAAa,EAAE,aAAa,CAAC;IAC7B,OAAO,CAAC,EAAE;QACR,6CAA6C;QAC7C,OAAO,EAAE,aAAa,CAAC;QACvB,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACxD,CAAC;CACH;AAED;;;;;;;;;GASG;AACH,qBAAa,eAAe;IAKd,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJpC,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC;gBAEP,OAAO,EAAE,cAAc;IAyDpD;;;OAGG;IACG,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC;IAazC;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAe3B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAIhC"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file End-to-end test harness with full wallet SDK integration.
|
|
12
|
+
* Provides complete WalletCore, ledger registration, and session management
|
|
13
|
+
* for comprehensive integration testing.
|
|
14
|
+
*
|
|
15
|
+
* @module @hsuite/native-connect-client/protocol/e2e-harness
|
|
16
|
+
*/
|
|
17
|
+
import { ReferenceClient } from '../reference-client';
|
|
18
|
+
import { MemoryTransport } from '../memory-transport';
|
|
19
|
+
import { WalletCore, LedgerRegistry, SessionManager, KeyVaultService, CryptoService, createChannelInvite, } from '@hsuite/native-connect-sdk';
|
|
20
|
+
import { base64 } from '@scure/base';
|
|
21
|
+
/**
|
|
22
|
+
* @class EndToEndHarness
|
|
23
|
+
* @description End-to-end test harness with full wallet SDK integration.
|
|
24
|
+
*
|
|
25
|
+
* Provides:
|
|
26
|
+
* - Complete WalletCore instance with ledger registration
|
|
27
|
+
* - Reference client for RPC communication
|
|
28
|
+
* - In-memory transport for isolated testing
|
|
29
|
+
* - Session establishment utilities
|
|
30
|
+
*/
|
|
31
|
+
export class EndToEndHarness {
|
|
32
|
+
options;
|
|
33
|
+
client;
|
|
34
|
+
wallet;
|
|
35
|
+
transport;
|
|
36
|
+
constructor(options) {
|
|
37
|
+
this.options = options;
|
|
38
|
+
this.transport = new MemoryTransport();
|
|
39
|
+
const registry = new LedgerRegistry({
|
|
40
|
+
context: {
|
|
41
|
+
secureRandom: (length) => crypto.getRandomValues(new Uint8Array(length)),
|
|
42
|
+
logger: () => undefined,
|
|
43
|
+
httpClient: {
|
|
44
|
+
async get() {
|
|
45
|
+
return {};
|
|
46
|
+
},
|
|
47
|
+
async post() {
|
|
48
|
+
return {};
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
const sessionManager = new SessionManager();
|
|
54
|
+
const keyVault = new KeyVaultService({
|
|
55
|
+
storage: {
|
|
56
|
+
async store() { },
|
|
57
|
+
async retrieve() {
|
|
58
|
+
return undefined;
|
|
59
|
+
},
|
|
60
|
+
async remove() { },
|
|
61
|
+
async list() {
|
|
62
|
+
return [];
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
encoder: {
|
|
66
|
+
async encrypt(data) {
|
|
67
|
+
return base64.encode(data);
|
|
68
|
+
},
|
|
69
|
+
async decrypt(payload) {
|
|
70
|
+
return base64.decode(payload);
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
this.wallet = new WalletCore({
|
|
75
|
+
registry,
|
|
76
|
+
sessionManager,
|
|
77
|
+
keyVault,
|
|
78
|
+
crypto: new CryptoService(),
|
|
79
|
+
secureMemory: {
|
|
80
|
+
async store() { },
|
|
81
|
+
async retrieve() {
|
|
82
|
+
return undefined;
|
|
83
|
+
},
|
|
84
|
+
async remove() { },
|
|
85
|
+
async clear() { },
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
void this.wallet.registerLedger(options.ledgerAdapter);
|
|
89
|
+
this.client = new ReferenceClient({ transport: this.transport });
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @description Establish test session with wallet
|
|
93
|
+
* @returns Session identifier
|
|
94
|
+
*/
|
|
95
|
+
async establishSession() {
|
|
96
|
+
// Use provided request or create a default invite
|
|
97
|
+
const request = this.options.session?.request ?? this.createDefaultInvite();
|
|
98
|
+
const sessionId = await this.wallet.createSession(request);
|
|
99
|
+
if (this.options.session?.approval) {
|
|
100
|
+
await this.wallet.approveSession(sessionId, this.options.session.approval);
|
|
101
|
+
}
|
|
102
|
+
return sessionId;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @description Create a default channel invite for testing
|
|
106
|
+
* @returns ChannelInvite object
|
|
107
|
+
*/
|
|
108
|
+
createDefaultInvite() {
|
|
109
|
+
return createChannelInvite({
|
|
110
|
+
type: 'session',
|
|
111
|
+
app: {
|
|
112
|
+
id: 'test-app',
|
|
113
|
+
name: 'Test App',
|
|
114
|
+
},
|
|
115
|
+
context: {
|
|
116
|
+
ledgerId: this.options.ledgerAdapter.manifest.metadata.id,
|
|
117
|
+
networkId: this.options.ledgerAdapter.manifest.metadata.defaultNetworkId,
|
|
118
|
+
},
|
|
119
|
+
permissions: [],
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @description Cleanup harness resources
|
|
124
|
+
*/
|
|
125
|
+
async teardown() {
|
|
126
|
+
this.client.destroy();
|
|
127
|
+
await this.transport.drain();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=e2e-harness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"e2e-harness.js","sourceRoot":"","sources":["../../src/protocol/e2e-harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EACL,UAAU,EACV,cAAc,EACd,cAAc,EACd,eAAe,EACf,aAAa,EACb,mBAAmB,GACpB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAiBrC;;;;;;;;;GASG;AACH,MAAM,OAAO,eAAe;IAKG;IAJpB,MAAM,CAAkB;IACxB,MAAM,CAAa;IACnB,SAAS,CAAkB;IAEpC,YAA6B,OAAuB;QAAvB,YAAO,GAAP,OAAO,CAAgB;QAClD,IAAI,CAAC,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;QACvC,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAC;YAClC,OAAO,EAAE;gBACP,YAAY,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;gBAChF,MAAM,EAAE,GAAG,EAAE,CAAC,SAAS;gBACvB,UAAU,EAA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6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file Handler registration utilities and protocol exports for reference client.
|
|
12
|
+
* Re-exports all handler registration functions for convenient access.
|
|
13
|
+
*
|
|
14
|
+
* @module @hsuite/native-connect-client/protocol
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @description Handler registration utilities for reference client
|
|
18
|
+
*/
|
|
19
|
+
export { registerSessionHandlers } from '../handlers/session-handler';
|
|
20
|
+
export { registerSigningHandlers } from '../handlers/signing-handler';
|
|
21
|
+
export { registerWalletHandlers } from '../handlers/wallet-handler';
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;;GAEG;AACH,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file Handler registration utilities and protocol exports for reference client.
|
|
12
|
+
* Re-exports all handler registration functions for convenient access.
|
|
13
|
+
*
|
|
14
|
+
* @module @hsuite/native-connect-client/protocol
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @description Handler registration utilities for reference client
|
|
18
|
+
*/
|
|
19
|
+
export { registerSessionHandlers } from '../handlers/session-handler';
|
|
20
|
+
export { registerSigningHandlers } from '../handlers/signing-handler';
|
|
21
|
+
export { registerWalletHandlers } from '../handlers/wallet-handler';
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;;GAEG;AACH,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file High-level query client for common wallet RPC methods.
|
|
12
|
+
* Provides convenience methods wrapping ReferenceClient for typical
|
|
13
|
+
* wallet operations like account discovery and configuration retrieval.
|
|
14
|
+
*
|
|
15
|
+
* @module @hsuite/native-connect-client/query-client
|
|
16
|
+
*/
|
|
17
|
+
import { ReferenceClient } from './reference-client';
|
|
18
|
+
import type { RpcResponse } from '@hsuite/native-connect-types';
|
|
19
|
+
/**
|
|
20
|
+
* @class QueryClient
|
|
21
|
+
* @description High-level query client for common wallet RPC methods.
|
|
22
|
+
*
|
|
23
|
+
* Provides convenience methods for:
|
|
24
|
+
* - Account discovery via `wallet/accounts` RPC method
|
|
25
|
+
* - Configuration retrieval via `wallet/get-config` RPC method
|
|
26
|
+
*/
|
|
27
|
+
export declare class QueryClient {
|
|
28
|
+
private readonly client;
|
|
29
|
+
constructor(client: ReferenceClient);
|
|
30
|
+
/**
|
|
31
|
+
* @description Retrieve wallet accounts
|
|
32
|
+
* @returns RPC response containing account list
|
|
33
|
+
*/
|
|
34
|
+
getAccounts(): Promise<RpcResponse>;
|
|
35
|
+
/**
|
|
36
|
+
* @description Fetch wallet configuration metadata
|
|
37
|
+
* @returns RPC response containing configuration data
|
|
38
|
+
*/
|
|
39
|
+
getConfig(): Promise<RpcResponse>;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=query-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-client.d.ts","sourceRoot":"","sources":["../src/query-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE5E;;;;;;;GAOG;AACH,qBAAa,WAAW;IACV,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,eAAe;IAEpD;;;OAGG;IACG,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAYzC;;;OAGG;IACG,SAAS,IAAI,OAAO,CAAC,WAAW,CAAC;CAWxC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @class QueryClient
|
|
12
|
+
* @description High-level query client for common wallet RPC methods.
|
|
13
|
+
*
|
|
14
|
+
* Provides convenience methods for:
|
|
15
|
+
* - Account discovery via `wallet/accounts` RPC method
|
|
16
|
+
* - Configuration retrieval via `wallet/get-config` RPC method
|
|
17
|
+
*/
|
|
18
|
+
export class QueryClient {
|
|
19
|
+
client;
|
|
20
|
+
constructor(client) {
|
|
21
|
+
this.client = client;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @description Retrieve wallet accounts
|
|
25
|
+
* @returns RPC response containing account list
|
|
26
|
+
*/
|
|
27
|
+
async getAccounts() {
|
|
28
|
+
const request = {
|
|
29
|
+
id: crypto.randomUUID(),
|
|
30
|
+
jsonrpc: '2.0',
|
|
31
|
+
method: 'wallet/accounts',
|
|
32
|
+
params: {},
|
|
33
|
+
timestamp: Date.now(),
|
|
34
|
+
};
|
|
35
|
+
return this.client.send(request);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @description Fetch wallet configuration metadata
|
|
39
|
+
* @returns RPC response containing configuration data
|
|
40
|
+
*/
|
|
41
|
+
async getConfig() {
|
|
42
|
+
const request = {
|
|
43
|
+
id: crypto.randomUUID(),
|
|
44
|
+
jsonrpc: '2.0',
|
|
45
|
+
method: 'wallet/get-config',
|
|
46
|
+
params: {},
|
|
47
|
+
timestamp: Date.now(),
|
|
48
|
+
};
|
|
49
|
+
return this.client.send(request);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=query-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-client.js","sourceRoot":"","sources":["../src/query-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAaH;;;;;;;GAOG;AACH,MAAM,OAAO,WAAW;IACO;IAA7B,YAA6B,MAAuB;QAAvB,WAAM,GAAN,MAAM,CAAiB;IAAG,CAAC;IAExD;;;OAGG;IACH,KAAK,CAAC,WAAW;QACf,MAAM,OAAO,GAAe;YAC1B,EAAE,EAAE,MAAM,CAAC,UAAU,EAAE;YACvB,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,iBAAiB;YACzB,MAAM,EAAE,EAAE;YACV,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC;QAEF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,OAAO,GAAe;YAC1B,EAAE,EAAE,MAAM,CAAC,UAAU,EAAE;YACvB,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,mBAAmB;YAC3B,MAAM,EAAE,EAAE;YACV,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACtB,CAAC;QAEF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file Reference client implementation for dApp developers.
|
|
12
|
+
* Provides a complete RPC client with bidirectional message routing,
|
|
13
|
+
* timeout protection, and event-driven lifecycle hooks.
|
|
14
|
+
*
|
|
15
|
+
* @module @hsuite/native-connect-client/reference-client
|
|
16
|
+
*/
|
|
17
|
+
import EventEmitter from 'eventemitter3';
|
|
18
|
+
import { RpcServer, RpcRouter } from '@hsuite/native-connect-sdk';
|
|
19
|
+
import type { RpcRequest, RpcResponse, RpcEnvelope } from '@hsuite/native-connect-types';
|
|
20
|
+
/**
|
|
21
|
+
* @description Transport abstraction for RPC message exchange
|
|
22
|
+
* @property {function} send - Send RPC envelope to peer
|
|
23
|
+
* @property {function} onMessage - Register listener for incoming messages
|
|
24
|
+
* @property {function} [offMessage] - Unregister message listener
|
|
25
|
+
* @property {function} [onServerMessage] - Register listener for server-originated messages
|
|
26
|
+
* @property {function} [offServerMessage] - Unregister server message listener
|
|
27
|
+
*/
|
|
28
|
+
export interface ReferenceClientTransport {
|
|
29
|
+
send(message: RpcEnvelope, from?: 'client' | 'server'): Promise<void>;
|
|
30
|
+
onMessage(listener: (message: RpcEnvelope) => void): void;
|
|
31
|
+
offMessage?(listener: (message: RpcEnvelope) => void): void;
|
|
32
|
+
onServerMessage?(listener: (message: RpcEnvelope) => void): void;
|
|
33
|
+
offServerMessage?(listener: (message: RpcEnvelope) => void): void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @description Configuration for reference client instantiation
|
|
37
|
+
* @property {ReferenceClientTransport} transport - Transport layer for message exchange
|
|
38
|
+
* @property {number} [timeoutMs] - Request timeout in milliseconds
|
|
39
|
+
* @property {RpcServer} [server] - Optional RPC server for bidirectional communication
|
|
40
|
+
* @property {function} [onResponse] - Callback invoked on response receipt
|
|
41
|
+
*/
|
|
42
|
+
export interface ReferenceClientOptions {
|
|
43
|
+
transport: ReferenceClientTransport;
|
|
44
|
+
timeoutMs?: number;
|
|
45
|
+
server?: RpcServer;
|
|
46
|
+
onResponse?(response: RpcResponse): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @description Events emitted by reference client
|
|
50
|
+
*/
|
|
51
|
+
export type ReferenceClientEvent = 'request' | 'response' | 'response:error' | 'error';
|
|
52
|
+
/**
|
|
53
|
+
* @class ReferenceClient
|
|
54
|
+
* @augments EventEmitter
|
|
55
|
+
* @description Developer-facing RPC client for HSuite SDK integration testing.
|
|
56
|
+
*
|
|
57
|
+
* This is a reference implementation designed for dApp developers to understand
|
|
58
|
+
* and test wallet communication patterns. It demonstrates the complete RPC
|
|
59
|
+
* request/response lifecycle with proper timeout handling and error recovery.
|
|
60
|
+
*
|
|
61
|
+
* Features:
|
|
62
|
+
* - Request/response RPC communication with timeout protection
|
|
63
|
+
* - Bidirectional message routing for client-server scenarios
|
|
64
|
+
* - Event emission for request/response lifecycle hooks
|
|
65
|
+
* - Handler registration for incoming RPC methods
|
|
66
|
+
*/
|
|
67
|
+
export declare class ReferenceClient extends EventEmitter<ReferenceClientEvent | string> {
|
|
68
|
+
private readonly options;
|
|
69
|
+
private readonly rpc;
|
|
70
|
+
private readonly codec;
|
|
71
|
+
private readonly pending;
|
|
72
|
+
private readonly timeoutMs;
|
|
73
|
+
private readonly messageListener;
|
|
74
|
+
private readonly server?;
|
|
75
|
+
private readonly router;
|
|
76
|
+
private readonly logger;
|
|
77
|
+
constructor(options: ReferenceClientOptions);
|
|
78
|
+
/**
|
|
79
|
+
* @description Send RPC request and await response with timeout
|
|
80
|
+
* @param request - RPC request payload
|
|
81
|
+
* @returns Promise resolving to RPC response
|
|
82
|
+
* @throws Error if request times out
|
|
83
|
+
*/
|
|
84
|
+
send<T = unknown>(request: RpcRequest): Promise<RpcResponse<T>>;
|
|
85
|
+
/**
|
|
86
|
+
* @description Register handler for incoming RPC method
|
|
87
|
+
* @param method - RPC method name
|
|
88
|
+
* @param handler - Handler function returning response data
|
|
89
|
+
*/
|
|
90
|
+
register(method: string, handler: Parameters<typeof RpcRouter.prototype.register>[1]): void;
|
|
91
|
+
/**
|
|
92
|
+
* @description Dispose client and cleanup listeners
|
|
93
|
+
*/
|
|
94
|
+
destroy(): void;
|
|
95
|
+
private handleEnvelope;
|
|
96
|
+
private isRequest;
|
|
97
|
+
private isError;
|
|
98
|
+
private isSuccess;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=reference-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-client.d.ts","sourceRoot":"","sources":["../src/reference-client.ts"],"names":[],"mappings":"AAAA;;;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C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkB;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+D;IACvF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAkC;IAClE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAY;IACpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAC1C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0D;gBAEpD,OAAO,EAAE,sBAAsB;IAc5D;;;;;OAKG;IACG,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAqBrE;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;IAI3F;;OAEG;IACH,OAAO,IAAI,IAAI;YAKD,cAAc;IAiD5B,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,SAAS;CAGlB"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HSuite Native Connect
|
|
3
|
+
* Copyright 2024-2025 HSuite (https://hsuite.finance)
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
6
|
+
*
|
|
7
|
+
* This file is part of HSuite Native Connect. For commercial licensing,
|
|
8
|
+
* visit https://hsuite.finance/licensing
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @file Reference client implementation for dApp developers.
|
|
12
|
+
* Provides a complete RPC client with bidirectional message routing,
|
|
13
|
+
* timeout protection, and event-driven lifecycle hooks.
|
|
14
|
+
*
|
|
15
|
+
* @module @hsuite/native-connect-client/reference-client
|
|
16
|
+
*/
|
|
17
|
+
import EventEmitter from 'eventemitter3';
|
|
18
|
+
import { RpcClient, RpcCodec, RpcRouter, getLogger } from '@hsuite/native-connect-sdk';
|
|
19
|
+
import { RpcRequestSchema, RpcErrorSchema, RpcSuccessSchema } from '@hsuite/native-connect-types';
|
|
20
|
+
/**
|
|
21
|
+
* @class ReferenceClient
|
|
22
|
+
* @augments EventEmitter
|
|
23
|
+
* @description Developer-facing RPC client for HSuite SDK integration testing.
|
|
24
|
+
*
|
|
25
|
+
* This is a reference implementation designed for dApp developers to understand
|
|
26
|
+
* and test wallet communication patterns. It demonstrates the complete RPC
|
|
27
|
+
* request/response lifecycle with proper timeout handling and error recovery.
|
|
28
|
+
*
|
|
29
|
+
* Features:
|
|
30
|
+
* - Request/response RPC communication with timeout protection
|
|
31
|
+
* - Bidirectional message routing for client-server scenarios
|
|
32
|
+
* - Event emission for request/response lifecycle hooks
|
|
33
|
+
* - Handler registration for incoming RPC methods
|
|
34
|
+
*/
|
|
35
|
+
export class ReferenceClient extends EventEmitter {
|
|
36
|
+
options;
|
|
37
|
+
rpc;
|
|
38
|
+
codec = new RpcCodec();
|
|
39
|
+
pending = new Map();
|
|
40
|
+
timeoutMs;
|
|
41
|
+
messageListener;
|
|
42
|
+
server;
|
|
43
|
+
router = new RpcRouter();
|
|
44
|
+
logger = getLogger().scoped?.('ReferenceClient') ?? getLogger();
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super();
|
|
47
|
+
this.options = options;
|
|
48
|
+
this.timeoutMs = options.timeoutMs ?? 5000;
|
|
49
|
+
this.rpc = new RpcClient({
|
|
50
|
+
transport: options.transport,
|
|
51
|
+
codec: this.codec,
|
|
52
|
+
timeoutMs: this.timeoutMs,
|
|
53
|
+
});
|
|
54
|
+
this.server = options.server;
|
|
55
|
+
this.messageListener = (envelope) => this.handleEnvelope(envelope);
|
|
56
|
+
options.transport.onMessage(this.messageListener);
|
|
57
|
+
options.transport.onServerMessage?.((envelope) => this.handleEnvelope(envelope));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @description Send RPC request and await response with timeout
|
|
61
|
+
* @param request - RPC request payload
|
|
62
|
+
* @returns Promise resolving to RPC response
|
|
63
|
+
* @throws Error if request times out
|
|
64
|
+
*/
|
|
65
|
+
async send(request) {
|
|
66
|
+
const id = request.id ?? crypto.randomUUID();
|
|
67
|
+
const finalRequest = { ...request, id, timestamp: request.timestamp ?? Date.now() };
|
|
68
|
+
this.logger.debug('Sending RPC request', { method: finalRequest.method, id: String(id) });
|
|
69
|
+
const promise = new Promise((resolve, reject) => {
|
|
70
|
+
const timeout = setTimeout(() => {
|
|
71
|
+
this.pending.delete(id);
|
|
72
|
+
reject(new Error(`RPC request ${String(id)} timed out after ${this.timeoutMs}ms`));
|
|
73
|
+
}, this.timeoutMs);
|
|
74
|
+
this.pending.set(id, (response) => {
|
|
75
|
+
clearTimeout(timeout);
|
|
76
|
+
this.logger.debug('RPC response received', { id: String(id), method: finalRequest.method });
|
|
77
|
+
resolve(response);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
await this.rpc.send(finalRequest);
|
|
81
|
+
return promise;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @description Register handler for incoming RPC method
|
|
85
|
+
* @param method - RPC method name
|
|
86
|
+
* @param handler - Handler function returning response data
|
|
87
|
+
*/
|
|
88
|
+
register(method, handler) {
|
|
89
|
+
this.router.register(method, handler);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @description Dispose client and cleanup listeners
|
|
93
|
+
*/
|
|
94
|
+
destroy() {
|
|
95
|
+
this.pending.clear();
|
|
96
|
+
this.options.transport.offMessage?.(this.messageListener);
|
|
97
|
+
}
|
|
98
|
+
async handleEnvelope(envelope) {
|
|
99
|
+
try {
|
|
100
|
+
const decoded = await this.codec.decode(envelope);
|
|
101
|
+
const payload = decoded.payload;
|
|
102
|
+
if (this.isRequest(payload)) {
|
|
103
|
+
const handled = this.server ? await this.server.handle(decoded) : undefined;
|
|
104
|
+
if (!handled) {
|
|
105
|
+
const response = await this.router.handle(payload);
|
|
106
|
+
const responseEnvelope = await this.codec.encodeResponse(response);
|
|
107
|
+
await this.options.transport.send(responseEnvelope, 'server');
|
|
108
|
+
this.logger.debug('RPC response sent from local router', {
|
|
109
|
+
method: payload.method,
|
|
110
|
+
id: String(payload.id),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
this.emit('request', payload);
|
|
114
|
+
this.logger.info('RPC request handled locally', {
|
|
115
|
+
method: payload.method,
|
|
116
|
+
id: String(payload.id),
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const result = payload;
|
|
121
|
+
const id = typeof result.id === 'string' || typeof result.id === 'number' ? result.id : 'unknown';
|
|
122
|
+
const callback = this.pending.get(id);
|
|
123
|
+
if (callback) {
|
|
124
|
+
callback(result);
|
|
125
|
+
this.pending.delete(id);
|
|
126
|
+
}
|
|
127
|
+
this.options.onResponse?.(result);
|
|
128
|
+
this.emit(this.isError(result) ? 'response:error' : 'response', result);
|
|
129
|
+
this.logger.info('RPC response processed', {
|
|
130
|
+
id: String(result.id),
|
|
131
|
+
status: this.isError(result) ? 'error' : 'success',
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
this.emit('error', error);
|
|
136
|
+
this.logger.error('RPC envelope handling failed', { error });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
isRequest(payload) {
|
|
140
|
+
return RpcRequestSchema.safeParse(payload).success;
|
|
141
|
+
}
|
|
142
|
+
isError(payload) {
|
|
143
|
+
return RpcErrorSchema.safeParse(payload).success;
|
|
144
|
+
}
|
|
145
|
+
isSuccess(payload) {
|
|
146
|
+
return RpcSuccessSchema.safeParse(payload).success;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=reference-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-client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