@hsuite/native-connect-types 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.
Files changed (87) hide show
  1. package/README.md +50 -0
  2. package/dist/address-avatar.d.ts +67 -0
  3. package/dist/address-avatar.d.ts.map +1 -0
  4. package/dist/address-avatar.js +130 -0
  5. package/dist/address-avatar.js.map +1 -0
  6. package/dist/addressbook.d.ts +289 -0
  7. package/dist/addressbook.d.ts.map +1 -0
  8. package/dist/addressbook.js +82 -0
  9. package/dist/addressbook.js.map +1 -0
  10. package/dist/enrichment.d.ts +88 -0
  11. package/dist/enrichment.d.ts.map +1 -0
  12. package/dist/enrichment.js +47 -0
  13. package/dist/enrichment.js.map +1 -0
  14. package/dist/hedera-system-accounts.d.ts +49 -0
  15. package/dist/hedera-system-accounts.d.ts.map +1 -0
  16. package/dist/hedera-system-accounts.js +56 -0
  17. package/dist/hedera-system-accounts.js.map +1 -0
  18. package/dist/identity.d.ts +94 -0
  19. package/dist/identity.d.ts.map +1 -0
  20. package/dist/identity.js +190 -0
  21. package/dist/identity.js.map +1 -0
  22. package/dist/index.d.ts +43 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +52 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/ledger.d.ts +848 -0
  27. package/dist/ledger.d.ts.map +1 -0
  28. package/dist/ledger.js +152 -0
  29. package/dist/ledger.js.map +1 -0
  30. package/dist/messages.d.ts +726 -0
  31. package/dist/messages.d.ts.map +1 -0
  32. package/dist/messages.js +252 -0
  33. package/dist/messages.js.map +1 -0
  34. package/dist/multisig-account.d.ts +419 -0
  35. package/dist/multisig-account.d.ts.map +1 -0
  36. package/dist/multisig-account.js +201 -0
  37. package/dist/multisig-account.js.map +1 -0
  38. package/dist/private-key-export.d.ts +112 -0
  39. package/dist/private-key-export.d.ts.map +1 -0
  40. package/dist/private-key-export.js +11 -0
  41. package/dist/private-key-export.js.map +1 -0
  42. package/dist/rpc.d.ts +872 -0
  43. package/dist/rpc.d.ts.map +1 -0
  44. package/dist/rpc.js +143 -0
  45. package/dist/rpc.js.map +1 -0
  46. package/dist/transaction-approval.d.ts +895 -0
  47. package/dist/transaction-approval.d.ts.map +1 -0
  48. package/dist/transaction-approval.js +11 -0
  49. package/dist/transaction-approval.js.map +1 -0
  50. package/dist/transaction-parser.d.ts +93 -0
  51. package/dist/transaction-parser.d.ts.map +1 -0
  52. package/dist/transaction-parser.js +11 -0
  53. package/dist/transaction-parser.js.map +1 -0
  54. package/dist/vault.d.ts +673 -0
  55. package/dist/vault.d.ts.map +1 -0
  56. package/dist/vault.js +188 -0
  57. package/dist/vault.js.map +1 -0
  58. package/dist/widget.types.d.ts +323 -0
  59. package/dist/widget.types.d.ts.map +1 -0
  60. package/dist/widget.types.js +140 -0
  61. package/dist/widget.types.js.map +1 -0
  62. package/package.json +43 -0
  63. package/src/address-avatar.ts +181 -0
  64. package/src/addressbook.spec.ts +220 -0
  65. package/src/addressbook.ts +115 -0
  66. package/src/enrichment.ts +97 -0
  67. package/src/hedera-system-accounts.ts +60 -0
  68. package/src/identity.spec.ts +243 -0
  69. package/src/identity.ts +244 -0
  70. package/src/index.ts +197 -0
  71. package/src/ledger.spec.ts +366 -0
  72. package/src/ledger.ts +567 -0
  73. package/src/messages.ts +381 -0
  74. package/src/multisig-account.spec.ts +362 -0
  75. package/src/multisig-account.ts +310 -0
  76. package/src/private-key-export.ts +117 -0
  77. package/src/rpc.spec.ts +396 -0
  78. package/src/rpc.ts +257 -0
  79. package/src/transaction-approval.ts +1033 -0
  80. package/src/transaction-parser.ts +98 -0
  81. package/src/vault.spec.ts +268 -0
  82. package/src/vault.ts +256 -0
  83. package/src/widget.types.spec.ts +337 -0
  84. package/src/widget.types.ts +501 -0
  85. package/tsconfig.build.json +17 -0
  86. package/tsconfig.json +11 -0
  87. package/vitest.config.ts +36 -0
@@ -0,0 +1,381 @@
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
+ /**
12
+ * @file Protocol message types for dApp-wallet communication.
13
+ *
14
+ * This module defines the core message schemas and types used in the HSuite
15
+ * Channel Protocol (hsc:) for secure communication between dApps and wallets.
16
+ *
17
+ * Message Flow:
18
+ * 1. dApp sends {@link SessionRequest} to initiate connection
19
+ * 2. Wallet responds with {@link SessionApproval} (approve/reject)
20
+ * 3. dApp sends {@link SignPayload} for transaction signing
21
+ * 4. Wallet returns {@link SignResult} with signature
22
+ *
23
+ * All messages are wrapped in {@link MessageEnvelope} for transport with
24
+ * versioning, timestamps, and optional signatures.
25
+ *
26
+ * @module types/messages
27
+ */
28
+
29
+ import { z } from 'zod';
30
+
31
+ import {
32
+ DerivationPathSchema,
33
+ LedgerAccountSchema,
34
+ LedgerIdSchema,
35
+ NetworkIdSchema,
36
+ } from './ledger.js';
37
+
38
+ /**
39
+ * @description Schema for dApp session request payload
40
+ */
41
+ export const SessionRequestSchema = z.object({
42
+ appId: z.string().min(1),
43
+ appName: z.string().min(1),
44
+ appIcon: z.string().url().optional(),
45
+ ledgerId: LedgerIdSchema,
46
+ networkId: NetworkIdSchema,
47
+ purpose: z.array(z.string()).default([]),
48
+ permissions: z.array(z.string()).default([]),
49
+ nonce: z.string().uuid(),
50
+ timestamp: z.number().int().positive(),
51
+ metadata: z.record(z.unknown()).optional(),
52
+ });
53
+
54
+ /**
55
+ * @description Type representing session request payload from dApp
56
+ */
57
+ export type SessionRequest = z.infer<typeof SessionRequestSchema>;
58
+
59
+ /**
60
+ * @description Schema for wallet's session approval/rejection response
61
+ */
62
+ export const SessionApprovalSchema = SessionRequestSchema.extend({
63
+ approved: z.boolean(),
64
+ accounts: z.array(LedgerAccountSchema).default([]),
65
+ expiration: z.number().optional(),
66
+ signature: z.string().optional(),
67
+ metadata: z.record(z.unknown()).optional(),
68
+ });
69
+
70
+ /**
71
+ * @description Type representing session approval response
72
+ */
73
+ export type SessionApproval = z.infer<typeof SessionApprovalSchema>;
74
+
75
+ /**
76
+ * @description Schema for batch inner transaction
77
+ */
78
+ export const BatchInnerTransactionSchema = z.object({
79
+ payload: z.union([z.string(), z.instanceof(Uint8Array)]),
80
+ description: z.string().optional(),
81
+ metadata: z.record(z.unknown()).optional(),
82
+ });
83
+
84
+ /**
85
+ * @description Type representing batch inner transaction
86
+ */
87
+ export type BatchInnerTransaction = z.infer<typeof BatchInnerTransactionSchema>;
88
+
89
+ /**
90
+ * @description Schema for signing request payload
91
+ * Can be either a single transaction or a batch of transactions
92
+ */
93
+ export const SignPayloadSchema = z.object({
94
+ ledgerId: LedgerIdSchema,
95
+ networkId: NetworkIdSchema,
96
+ accountAddress: z.string().min(1),
97
+ payload: z.union([z.string(), z.instanceof(Uint8Array)]),
98
+ derivationPath: DerivationPathSchema.optional(),
99
+ context: z.record(z.unknown()).optional(),
100
+ nonce: z.string().uuid().optional(),
101
+ // Batch transaction support
102
+ isBatch: z.boolean().optional(),
103
+ batchKey: z.string().optional(), // Public key for batch transactions
104
+ innerTransactions: z.array(BatchInnerTransactionSchema).optional(),
105
+ });
106
+
107
+ /**
108
+ * @description Type representing signing request
109
+ */
110
+ export type SignPayload = z.infer<typeof SignPayloadSchema>;
111
+
112
+ /**
113
+ * @description Schema for signed inner transaction result
114
+ */
115
+ export const SignedInnerTransactionSchema = z.object({
116
+ signedPayload: z.string(), // Base64 encoded signed transaction
117
+ transactionId: z.string().optional(),
118
+ });
119
+
120
+ /**
121
+ * @description Type representing signed inner transaction
122
+ */
123
+ export type SignedInnerTransaction = z.infer<typeof SignedInnerTransactionSchema>;
124
+
125
+ /**
126
+ * @description Schema for signing operation result
127
+ * For single transactions: returns signature
128
+ * For batch transactions (sign mode): returns array of signed inner transactions
129
+ * For batch transactions (signAndExecute mode): returns transaction result
130
+ */
131
+ export const SignResultSchema = z.object({
132
+ signature: z.string().optional(),
133
+ /** Full signed transaction bytes (hex-encoded), suitable for submission via submitTransaction */
134
+ signedPayload: z.string().optional(),
135
+ publicKey: z.string(),
136
+ payloadHash: z.string().optional(),
137
+ algorithm: z.string(),
138
+ // Batch transaction results (sign mode - returns signed inner txs)
139
+ signedInnerTransactions: z.array(SignedInnerTransactionSchema).optional(),
140
+ // Batch transaction results (signAndExecute mode - returns tx result)
141
+ transactionId: z.string().optional(),
142
+ transactionHash: z.string().optional(),
143
+ status: z.string().optional(),
144
+ });
145
+
146
+ /**
147
+ * @description Type representing signature result
148
+ */
149
+ export type SignResult = z.infer<typeof SignResultSchema>;
150
+
151
+ /**
152
+ * @description Payload for arbitrary message signing (ledger/signMessage RPC).
153
+ *
154
+ * Distinct from SignPayload (which carries transaction protobuf). Used by the
155
+ * §21.3 message-signing flow end-to-end: dApp → ChannelClient → wallet →
156
+ * adapter.signMessage() → domain-separated Ed25519/secp256k1 signing.
157
+ */
158
+ export interface SignMessagePayload {
159
+ /** The message to sign — UTF-8 string or base64-encoded bytes depending on `encoding`. */
160
+ message: string;
161
+ /** How to decode `message` into bytes before domain separation + signing. */
162
+ encoding: 'utf-8' | 'base64';
163
+ /** Account address that will sign (selects the correct private key from the vault). */
164
+ accountAddress: string;
165
+ /**
166
+ * CAIP-2 chain identifier embedded verbatim in the signed blob.
167
+ * Example: "hedera:testnet", "xrpl:mainnet". Prevents cross-network replay:
168
+ * a signature over "hedera:testnet" + msg cannot be verified against
169
+ * "hedera:mainnet" + msg because the signed bytes differ.
170
+ *
171
+ * Must match `/^[a-z0-9]+:[a-z0-9]+$/` (CAIP-2 lower-kebab-case shape).
172
+ */
173
+ caipChainId: string;
174
+ }
175
+
176
+ /**
177
+ * @description Schema for message-signing result (§21.3).
178
+ *
179
+ * Separate from SignResultSchema because (a) message signing is semantically
180
+ * distinct from transaction signing — different security surface, different
181
+ * audit trail — and (b) it returns a narrower, always-required field set
182
+ * (signature, publicKey, algorithm) plus message-specific metadata
183
+ * (encoding, caipChainId, messageLength) that auditors and on-chain
184
+ * verifiers use to reconstruct the exact signed blob.
185
+ *
186
+ * The `algorithm` field is an enum (not free-form string) to make off-wallet
187
+ * verification deterministic. Consumers feed `signature`, `publicKey`,
188
+ * `algorithm`, `encoding`, `caipChainId`, and the original `message` back
189
+ * into `buildDomainSeparatedMessage` + the matching crypto primitive to
190
+ * verify the signature.
191
+ */
192
+ export const MessageSignResultSchema = z.object({
193
+ /** Raw signature as hex (no 0x prefix). 128 chars for Ed25519, variable for ECDSA/secp256k1 DER. */
194
+ signature: z.string(),
195
+ /** Raw public key as hex (no 0x prefix). 64 chars for Ed25519, 66 chars for compressed secp256k1. */
196
+ publicKey: z.string(),
197
+ /** Signing algorithm — enum for deterministic verification. */
198
+ algorithm: z.enum(['ed25519', 'ecdsa_secp256k1', 'secp256k1']),
199
+ /** Encoding used to decode the original `message` into bytes. */
200
+ encoding: z.enum(['utf-8', 'base64']),
201
+ /**
202
+ * CAIP-2 chain identifier embedded in the signed blob. Mirrors the
203
+ * `caipChainId` from SignMessagePayload so verifiers can reconstruct
204
+ * the exact signed bytes without needing the original payload.
205
+ */
206
+ caipChainId: z.string(),
207
+ /**
208
+ * Length of the decoded message in bytes (NOT the original string length).
209
+ * For UTF-8 messages this may exceed the character count (multi-byte
210
+ * codepoints). Used to prevent truncation attacks and for audit trail.
211
+ */
212
+ messageLength: z.number().int().nonnegative(),
213
+ });
214
+
215
+ /**
216
+ * @description Type representing message signature result
217
+ */
218
+ export type MessageSignResult = z.infer<typeof MessageSignResultSchema>;
219
+
220
+ /**
221
+ * @description Schema for transaction submission payload
222
+ */
223
+ export const TransactionSubmissionSchema = z.object({
224
+ ledgerId: LedgerIdSchema,
225
+ networkId: NetworkIdSchema,
226
+ transactionBlob: z.instanceof(Uint8Array),
227
+ context: z.record(z.unknown()).optional(),
228
+ });
229
+
230
+ /**
231
+ * @description Type representing transaction submission request
232
+ */
233
+ export type TransactionSubmission = z.infer<typeof TransactionSubmissionSchema>;
234
+
235
+ /**
236
+ * @description Schema for message envelope wrapper
237
+ */
238
+ export const MessageEnvelopeSchema = z.object({
239
+ id: z.string().uuid(),
240
+ topic: z.string(),
241
+ createdAt: z.number(),
242
+ payload: z.unknown(),
243
+ version: z.string().default('1.0.0'),
244
+ signature: z.string().optional(),
245
+ });
246
+
247
+ /**
248
+ * @description Type representing message envelope with generic payload
249
+ */
250
+ export type MessageEnvelope<TPayload = unknown> = z.infer<typeof MessageEnvelopeSchema> & {
251
+ payload: TPayload;
252
+ };
253
+
254
+ /**
255
+ * @description Schema for RPC method names
256
+ */
257
+ export const RpcMethodSchema = z.enum([
258
+ 'hsuite.session.request',
259
+ 'hsuite.session.approve',
260
+ 'hsuite.session.terminate',
261
+ 'hsuite.ledger.sign',
262
+ 'hsuite.ledger.submit',
263
+ 'hsuite.wallet.accounts',
264
+ ]);
265
+
266
+ /**
267
+ * @description Type representing RPC method
268
+ */
269
+ export type RpcMethod = z.infer<typeof RpcMethodSchema>;
270
+
271
+ /**
272
+ * @description Schema for RPC request
273
+ */
274
+ export const RpcRequestSchema = z.object({
275
+ id: z.string().uuid(),
276
+ method: RpcMethodSchema,
277
+ params: z.unknown(),
278
+ origin: z.string().optional(),
279
+ timestamp: z.number(),
280
+ });
281
+
282
+ /**
283
+ * @description Type representing RPC request
284
+ */
285
+ export type RpcRequest = z.infer<typeof RpcRequestSchema>;
286
+
287
+ /**
288
+ * @description Schema for successful RPC response
289
+ */
290
+ export const RpcSuccessSchema = z.object({
291
+ id: z.string().uuid(),
292
+ result: z.unknown(),
293
+ timestamp: z.number(),
294
+ });
295
+
296
+ /**
297
+ * @description Schema for failed RPC response
298
+ */
299
+ export const RpcErrorSchema = z.object({
300
+ id: z.string().uuid(),
301
+ error: z.object({
302
+ code: z.number(),
303
+ message: z.string(),
304
+ data: z.unknown().optional(),
305
+ }),
306
+ timestamp: z.number(),
307
+ });
308
+
309
+ /**
310
+ * @description Type representing RPC response (success or error)
311
+ */
312
+ export type RpcResponse = z.infer<typeof RpcSuccessSchema> | z.infer<typeof RpcErrorSchema>;
313
+
314
+ // ========== WebRTC P2P Types (Used for P2P upgrade signaling) ==========
315
+
316
+ /**
317
+ * @description Schema for ICE server configuration
318
+ * Used by SessionTransportMux for P2P WebRTC upgrade after Nostr signaling.
319
+ */
320
+ export const IceServerSchema = z.object({
321
+ urls: z.union([z.string(), z.array(z.string())]),
322
+ username: z.string().optional(),
323
+ credential: z.string().optional(),
324
+ credentialType: z.enum(['password', 'oauth']).optional(),
325
+ });
326
+
327
+ /**
328
+ * @description Type representing ICE server configuration
329
+ */
330
+ export type IceServer = z.infer<typeof IceServerSchema>;
331
+
332
+ /**
333
+ * @description Schema for WebRTC SDP offer
334
+ * Used internally by SessionTransportMux for P2P upgrade signaling.
335
+ */
336
+ export const WebRtcOfferSchema = z.object({
337
+ /** Protocol version (currently 1) */
338
+ v: z.literal(1),
339
+ /** Compressed SDP offer (base64url encoded) */
340
+ sdp: z.string().min(1),
341
+ /** ICE server configuration for NAT traversal */
342
+ ice: z.array(IceServerSchema),
343
+ /** Optional session request payload for wallet */
344
+ session: SessionRequestSchema.optional(),
345
+ });
346
+
347
+ /**
348
+ * @description Type representing WebRTC SDP offer
349
+ */
350
+ export type WebRtcOffer = z.infer<typeof WebRtcOfferSchema>;
351
+
352
+ /**
353
+ * @description Schema for WebRTC SDP answer
354
+ */
355
+ export const WebRtcAnswerSchema = z.object({
356
+ /** Protocol version (must match offer version) */
357
+ v: z.literal(1),
358
+ /** Compressed SDP answer (base64url encoded) */
359
+ sdp: z.string().min(1),
360
+ });
361
+
362
+ /**
363
+ * @description Type representing WebRTC SDP answer
364
+ */
365
+ export type WebRtcAnswer = z.infer<typeof WebRtcAnswerSchema>;
366
+
367
+ /**
368
+ * @description Current protocol version for WebRTC P2P upgrade
369
+ */
370
+ export const WEBRTC_PROTOCOL_VERSION = 1 as const;
371
+
372
+ // ========== HSuite Channel Protocol (hsc:) ==========
373
+
374
+ /**
375
+ * @description Protocol prefix for HSuite Channel invites
376
+ * Format: hsc:connect?i=<base64url-encoded-invite>
377
+ *
378
+ * This is the recommended protocol for all wallet connections.
379
+ * Uses Nostr for initial signaling with automatic P2P WebRTC upgrade.
380
+ */
381
+ export const HSUITE_CHANNEL_PROTOCOL_PREFIX = 'hsc:' as const;