@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,362 @@
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
+
8
+ import { describe, it, expect } from 'vitest';
9
+ import {
10
+ AccountTypeSchema,
11
+ MultisigSignerSchema,
12
+ MultisigInfoSchema,
13
+ UnifiedAccountMetadataSchema,
14
+ isMultisigAccount,
15
+ getMultisigInfo,
16
+ createMultisigMetadata,
17
+ convertSinglesigToMultisig,
18
+ type MultisigSigner,
19
+ type MultisigAccountConfig,
20
+ type MultisigInfo,
21
+ } from './multisig-account';
22
+
23
+ describe('Multisig Account Types', () => {
24
+ describe('AccountTypeSchema', () => {
25
+ it('should validate singlesig type', () => {
26
+ expect(AccountTypeSchema.parse('singlesig')).toBe('singlesig');
27
+ });
28
+
29
+ it('should validate multisig type', () => {
30
+ expect(AccountTypeSchema.parse('multisig')).toBe('multisig');
31
+ });
32
+
33
+ it('should reject invalid account types', () => {
34
+ expect(() => AccountTypeSchema.parse('invalid')).toThrow();
35
+ expect(() => AccountTypeSchema.parse('')).toThrow();
36
+ expect(() => AccountTypeSchema.parse(123)).toThrow();
37
+ });
38
+ });
39
+
40
+ describe('MultisigSignerSchema', () => {
41
+ it('should validate a valid signer', () => {
42
+ const signer = {
43
+ label: 'Alice',
44
+ publicKey: 'abc123',
45
+ };
46
+ const result = MultisigSignerSchema.parse(signer);
47
+ expect(result.label).toBe('Alice');
48
+ expect(result.publicKey).toBe('abc123');
49
+ expect(result.weight).toBe(1); // default
50
+ });
51
+
52
+ it('should validate signer with all fields', () => {
53
+ const signer = {
54
+ label: 'Bob',
55
+ publicKey: 'def456',
56
+ accountAddress: 'rBob123',
57
+ weight: 2,
58
+ isSelf: true,
59
+ };
60
+ const result = MultisigSignerSchema.parse(signer);
61
+ expect(result).toEqual(signer);
62
+ });
63
+
64
+ it('should reject signer with empty label', () => {
65
+ expect(() =>
66
+ MultisigSignerSchema.parse({
67
+ label: '',
68
+ publicKey: 'abc',
69
+ }),
70
+ ).toThrow();
71
+ });
72
+
73
+ it('should reject signer with empty publicKey', () => {
74
+ expect(() =>
75
+ MultisigSignerSchema.parse({
76
+ label: 'Alice',
77
+ publicKey: '',
78
+ }),
79
+ ).toThrow();
80
+ });
81
+
82
+ it('should reject signer with non-positive weight', () => {
83
+ expect(() =>
84
+ MultisigSignerSchema.parse({
85
+ label: 'Alice',
86
+ publicKey: 'abc',
87
+ weight: 0,
88
+ }),
89
+ ).toThrow();
90
+ expect(() =>
91
+ MultisigSignerSchema.parse({
92
+ label: 'Alice',
93
+ publicKey: 'abc',
94
+ weight: -1,
95
+ }),
96
+ ).toThrow();
97
+ });
98
+ });
99
+
100
+ describe('MultisigInfoSchema', () => {
101
+ it('should validate basic multisig info', () => {
102
+ const info = {
103
+ threshold: 2,
104
+ signers: [
105
+ { label: 'Alice', publicKey: 'abc' },
106
+ { label: 'Bob', publicKey: 'def' },
107
+ ],
108
+ };
109
+ const result = MultisigInfoSchema.parse(info);
110
+ expect(result.threshold).toBe(2);
111
+ expect(result.signers).toHaveLength(2);
112
+ });
113
+
114
+ it('should validate multisig info with all optional fields', () => {
115
+ const info = {
116
+ threshold: 2,
117
+ signers: [{ label: 'Alice', publicKey: 'abc', isSelf: true, weight: 1 }],
118
+ localSignerAddress: 'rAlice123',
119
+ name: 'Treasury 2-of-3',
120
+ originalConfigId: 'config-uuid',
121
+ convertedAt: 1234567890,
122
+ originalSinglesigAddress: 'rOriginal',
123
+ };
124
+ const result = MultisigInfoSchema.parse(info);
125
+ expect(result).toEqual(info);
126
+ });
127
+
128
+ it('should reject non-positive threshold', () => {
129
+ expect(() =>
130
+ MultisigInfoSchema.parse({
131
+ threshold: 0,
132
+ signers: [{ label: 'Alice', publicKey: 'abc' }],
133
+ }),
134
+ ).toThrow();
135
+ });
136
+
137
+ it('should reject non-integer threshold', () => {
138
+ expect(() =>
139
+ MultisigInfoSchema.parse({
140
+ threshold: 1.5,
141
+ signers: [{ label: 'Alice', publicKey: 'abc' }],
142
+ }),
143
+ ).toThrow();
144
+ });
145
+ });
146
+
147
+ describe('UnifiedAccountMetadataSchema', () => {
148
+ it('should validate singlesig account metadata', () => {
149
+ const metadata = {
150
+ accountType: 'singlesig' as const,
151
+ label: 'My Account',
152
+ };
153
+ const result = UnifiedAccountMetadataSchema.parse(metadata);
154
+ expect(result.accountType).toBe('singlesig');
155
+ expect(result.label).toBe('My Account');
156
+ });
157
+
158
+ it('should default accountType to singlesig', () => {
159
+ const metadata = {};
160
+ const result = UnifiedAccountMetadataSchema.parse(metadata);
161
+ expect(result.accountType).toBe('singlesig');
162
+ });
163
+
164
+ it('should validate multisig account metadata', () => {
165
+ const metadata = {
166
+ accountType: 'multisig' as const,
167
+ multisig: {
168
+ threshold: 2,
169
+ signers: [
170
+ { label: 'Alice', publicKey: 'abc' },
171
+ { label: 'Bob', publicKey: 'def' },
172
+ ],
173
+ },
174
+ label: 'Treasury',
175
+ };
176
+ const result = UnifiedAccountMetadataSchema.parse(metadata);
177
+ expect(result.accountType).toBe('multisig');
178
+ expect(result.multisig?.threshold).toBe(2);
179
+ });
180
+
181
+ it('should validate all optional fields', () => {
182
+ const metadata = {
183
+ accountType: 'singlesig' as const,
184
+ label: 'Test',
185
+ imported: true,
186
+ activated: true,
187
+ hidden: false,
188
+ tags: ['treasury', 'main'],
189
+ notes: 'Main treasury account',
190
+ createdAt: 1234567890,
191
+ lastUsedAt: 1234567899,
192
+ };
193
+ const result = UnifiedAccountMetadataSchema.parse(metadata);
194
+ expect(result).toEqual(metadata);
195
+ });
196
+ });
197
+
198
+ describe('isMultisigAccount', () => {
199
+ it('should return true for multisig account', () => {
200
+ const metadata = {
201
+ accountType: 'multisig',
202
+ multisig: {
203
+ threshold: 2,
204
+ signers: [],
205
+ },
206
+ };
207
+ expect(isMultisigAccount(metadata)).toBe(true);
208
+ });
209
+
210
+ it('should return false for singlesig account', () => {
211
+ const metadata = {
212
+ accountType: 'singlesig',
213
+ };
214
+ expect(isMultisigAccount(metadata)).toBe(false);
215
+ });
216
+
217
+ it('should return false if multisig field is missing', () => {
218
+ const metadata = {
219
+ accountType: 'multisig',
220
+ };
221
+ expect(isMultisigAccount(metadata)).toBe(false);
222
+ });
223
+
224
+ it('should return false for undefined metadata', () => {
225
+ expect(isMultisigAccount(undefined)).toBe(false);
226
+ });
227
+
228
+ it('should return false for empty metadata', () => {
229
+ expect(isMultisigAccount({})).toBe(false);
230
+ });
231
+ });
232
+
233
+ describe('getMultisigInfo', () => {
234
+ it('should return multisig info for multisig account', () => {
235
+ const multisigInfo: MultisigInfo = {
236
+ threshold: 2,
237
+ signers: [{ label: 'Alice', publicKey: 'abc' }],
238
+ };
239
+ const metadata = {
240
+ accountType: 'multisig',
241
+ multisig: multisigInfo,
242
+ };
243
+ expect(getMultisigInfo(metadata)).toEqual(multisigInfo);
244
+ });
245
+
246
+ it('should return undefined for singlesig account', () => {
247
+ const metadata = {
248
+ accountType: 'singlesig',
249
+ };
250
+ expect(getMultisigInfo(metadata)).toBeUndefined();
251
+ });
252
+
253
+ it('should return undefined for undefined metadata', () => {
254
+ expect(getMultisigInfo(undefined)).toBeUndefined();
255
+ });
256
+ });
257
+
258
+ describe('createMultisigMetadata', () => {
259
+ it('should create metadata from config', () => {
260
+ const config: MultisigAccountConfig = {
261
+ id: 'config-123',
262
+ name: 'Treasury 2-of-3',
263
+ ledgerId: 'xrpl',
264
+ networkId: 'xrpl:mainnet',
265
+ threshold: 2,
266
+ signers: [
267
+ { label: 'Alice', publicKey: 'abc', isSelf: true },
268
+ { label: 'Bob', publicKey: 'def' },
269
+ { label: 'Charlie', publicKey: 'ghi' },
270
+ ],
271
+ createdAt: 1234567890,
272
+ updatedAt: 1234567890,
273
+ };
274
+
275
+ const result = createMultisigMetadata(config, 'rAlice123');
276
+
277
+ expect(result.accountType).toBe('multisig');
278
+ expect(result.multisig?.threshold).toBe(2);
279
+ expect(result.multisig?.signers).toHaveLength(3);
280
+ expect(result.multisig?.localSignerAddress).toBe('rAlice123');
281
+ expect(result.multisig?.name).toBe('Treasury 2-of-3');
282
+ expect(result.multisig?.originalConfigId).toBe('config-123');
283
+ expect(result.label).toBe('Treasury 2-of-3');
284
+ expect(result.createdAt).toBe(1234567890);
285
+ });
286
+
287
+ it('should create metadata without local signer address', () => {
288
+ const config: MultisigAccountConfig = {
289
+ id: 'config-456',
290
+ name: 'Test Multisig',
291
+ ledgerId: 'hedera',
292
+ networkId: 'hedera:testnet',
293
+ threshold: 1,
294
+ signers: [{ label: 'Solo', publicKey: 'xyz' }],
295
+ createdAt: 9999999999,
296
+ updatedAt: 9999999999,
297
+ };
298
+
299
+ const result = createMultisigMetadata(config);
300
+
301
+ expect(result.multisig?.localSignerAddress).toBeUndefined();
302
+ });
303
+ });
304
+
305
+ describe('convertSinglesigToMultisig', () => {
306
+ it('should convert singlesig metadata to multisig', () => {
307
+ const existingMetadata = {
308
+ accountType: 'singlesig',
309
+ label: 'My Account',
310
+ address: 'rOriginal123',
311
+ createdAt: 1000000000,
312
+ };
313
+
314
+ const multisigConfig = {
315
+ threshold: 2,
316
+ signers: [
317
+ { label: 'Alice', publicKey: 'abc', isSelf: true } as MultisigSigner,
318
+ { label: 'Bob', publicKey: 'def' } as MultisigSigner,
319
+ ],
320
+ name: 'Converted Multisig',
321
+ };
322
+
323
+ const result = convertSinglesigToMultisig(existingMetadata, multisigConfig, 'rAlice');
324
+
325
+ expect(result.accountType).toBe('multisig');
326
+ expect(result.multisig?.threshold).toBe(2);
327
+ expect(result.multisig?.signers).toHaveLength(2);
328
+ expect(result.multisig?.localSignerAddress).toBe('rAlice');
329
+ expect(result.multisig?.name).toBe('Converted Multisig');
330
+ expect(result.multisig?.originalSinglesigAddress).toBe('rOriginal123');
331
+ expect(result.multisig?.convertedAt).toBeDefined();
332
+ expect(result.label).toBe('Converted Multisig');
333
+ // Should preserve existing fields
334
+ expect(result.createdAt).toBe(1000000000);
335
+ });
336
+
337
+ it('should preserve existing label if no name provided', () => {
338
+ const existingMetadata = {
339
+ label: 'Original Label',
340
+ };
341
+
342
+ const result = convertSinglesigToMultisig(
343
+ existingMetadata,
344
+ { threshold: 1, signers: [{ label: 'Solo', publicKey: 'xyz' }] },
345
+ 'rSolo',
346
+ );
347
+
348
+ expect(result.label).toBe('Original Label');
349
+ });
350
+
351
+ it('should handle undefined existing metadata', () => {
352
+ const result = convertSinglesigToMultisig(
353
+ undefined,
354
+ { threshold: 2, signers: [{ label: 'A', publicKey: 'a' }] },
355
+ 'rA',
356
+ );
357
+
358
+ expect(result.accountType).toBe('multisig');
359
+ expect(result.multisig?.originalSinglesigAddress).toBeUndefined();
360
+ });
361
+ });
362
+ });
@@ -0,0 +1,310 @@
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
+ * @fileoverview Unified account management for singlesig and multisig accounts.
13
+ *
14
+ * This module provides a unified data model where both singlesig and multisig
15
+ * accounts are stored together in the KeyVault. This design enables:
16
+ *
17
+ * - **Single source of truth** - All accounts in one storage layer
18
+ * - **Seamless transformation** - Singlesig to multisig upgrade (especially XRPL)
19
+ * - **Unified history** - Transaction history across all account types
20
+ * - **Consistent UI** - Same treatment for all accounts
21
+ *
22
+ * Account Type Discrimination:
23
+ * - Check `metadata.accountType` for 'singlesig' or 'multisig'
24
+ * - Use {@link isMultisigAccount} for type-safe checking
25
+ * - Use {@link getMultisigInfo} to extract multisig configuration
26
+ *
27
+ * @module types/multisig-account
28
+ *
29
+ * Type definitions for unified account management (singlesig and multisig).
30
+ *
31
+ * This module provides a unified account model where both singlesig and multisig
32
+ * accounts are stored in the same storage layer (KeyVault). This enables:
33
+ * - Single source of truth for all account data
34
+ * - Seamless singlesig→multisig transformation (especially for XRPL)
35
+ * - Unified transaction history across all account types
36
+ * - Consistent UI treatment for all accounts
37
+ */
38
+
39
+ import { z } from 'zod';
40
+
41
+ // ============================================================================
42
+ // Account Type Discriminator
43
+ // ============================================================================
44
+
45
+ /**
46
+ * AccountType
47
+ *
48
+ * Discriminator for account types. Used to determine signing behavior
49
+ * and UI rendering for an account.
50
+ *
51
+ * - 'singlesig': Standard account with single private key control
52
+ * - 'multisig': Account requiring M-of-N signatures for transactions
53
+ */
54
+ export type AccountType = 'singlesig' | 'multisig';
55
+
56
+ /**
57
+ * AccountTypeSchema
58
+ *
59
+ * Zod schema for AccountType validation.
60
+ */
61
+ export const AccountTypeSchema = z.enum(['singlesig', 'multisig']);
62
+
63
+ // ============================================================================
64
+ // Multisig Signer
65
+ // ============================================================================
66
+
67
+ /**
68
+ * MultisigSigner
69
+ *
70
+ * Represents a single signer in a multisig account configuration.
71
+ */
72
+ /**
73
+ * MultisigSignerSchema
74
+ *
75
+ * Zod schema for MultisigSigner validation.
76
+ */
77
+ export const MultisigSignerSchema = z.object({
78
+ /** Human-readable label for this signer */
79
+ label: z.string().min(1),
80
+ /** Public key in hex or base64 format (ledger-specific) */
81
+ publicKey: z.string().min(1),
82
+ /** On-chain account address for this signer (if applicable) */
83
+ accountAddress: z.string().optional(),
84
+ /** Optional weight for weighted multisig (default: 1) */
85
+ weight: z.number().positive().default(1),
86
+ /** Whether this signer is "self" (controlled by this wallet instance) */
87
+ isSelf: z.boolean().optional(),
88
+ });
89
+
90
+ export interface MultisigSigner {
91
+ /** Human-readable label for this signer (e.g., "Alice", "Treasury Hot Wallet") */
92
+ label: string;
93
+ /** Public key in hex or base64 format (ledger-specific) */
94
+ publicKey: string;
95
+ /** On-chain account address for this signer (if applicable) */
96
+ accountAddress?: string;
97
+ /** Optional weight for weighted multisig (default: 1) */
98
+ weight?: number;
99
+ /** Whether this signer is "self" (controlled by this wallet instance) */
100
+ isSelf?: boolean;
101
+ }
102
+
103
+ /**
104
+ * MultisigAccountConfig
105
+ *
106
+ * Configuration for a multisig account. Stored locally and used to create parties.
107
+ */
108
+ export interface MultisigAccountConfig {
109
+ /** Unique identifier for this multisig account configuration */
110
+ id: string;
111
+ /** Human-readable name (e.g., "Treasury 2-of-3", "DAO Operations") */
112
+ name: string;
113
+ /** Ledger this multisig operates on */
114
+ ledgerId: string;
115
+ /** Network ID (e.g., "hedera:mainnet", "xrpl:mainnet") */
116
+ networkId: string;
117
+ /** Number of signatures required to execute transactions */
118
+ threshold: number;
119
+ /** List of all signers (including self) */
120
+ signers: MultisigSigner[];
121
+ /** The on-chain multisig account address (if already created on ledger) */
122
+ accountAddress?: string;
123
+ /** Optional metadata (tags, notes, etc.) */
124
+ metadata?: Record<string, unknown>;
125
+ /** Creation timestamp */
126
+ createdAt: number;
127
+ /** Last updated timestamp */
128
+ updatedAt: number;
129
+ }
130
+
131
+ /**
132
+ * MultisigAccountStatus
133
+ *
134
+ * Runtime status of a multisig account and its associated party.
135
+ */
136
+ export interface MultisigAccountStatus {
137
+ /** The account configuration ID */
138
+ accountId: string;
139
+ /** Whether there's an active party for this account */
140
+ hasActiveParty: boolean;
141
+ /** Active party ID (if any) */
142
+ activePartyId?: string;
143
+ /** Number of signers currently online/connected */
144
+ connectedSigners?: number;
145
+ /** Last activity timestamp */
146
+ lastActivityAt?: number;
147
+ }
148
+
149
+ // ============================================================================
150
+ // Unified Account Metadata Extensions
151
+ // ============================================================================
152
+
153
+ /**
154
+ * MultisigInfoSchema
155
+ *
156
+ * Zod schema for multisig configuration stored in VaultKey.metadata.
157
+ * This allows singlesig accounts to be "upgraded" to multisig without
158
+ * moving them to a separate storage system.
159
+ */
160
+ export const MultisigInfoSchema = z.object({
161
+ /** Number of signatures required to execute transactions */
162
+ threshold: z.number().int().positive(),
163
+ /** List of all signers (including self) */
164
+ signers: z.array(MultisigSignerSchema),
165
+ /** The address of the local signer (this wallet's signing account) */
166
+ localSignerAddress: z.string().optional(),
167
+ /** Name for this multisig configuration (e.g., "Treasury 2-of-3") */
168
+ name: z.string().optional(),
169
+ /** ID of the original MultisigAccountConfig */
170
+ originalConfigId: z.string().optional(),
171
+ /** When this account was converted to multisig */
172
+ convertedAt: z.number().optional(),
173
+ /** The singlesig account address that was converted (for XRPL transformation) */
174
+ originalSinglesigAddress: z.string().optional(),
175
+ });
176
+
177
+ /**
178
+ * MultisigInfo
179
+ *
180
+ * Multisig configuration that can be embedded in VaultKey.metadata.
181
+ * This unifies storage of singlesig and multisig accounts.
182
+ */
183
+ export type MultisigInfo = z.infer<typeof MultisigInfoSchema>;
184
+
185
+ /**
186
+ * UnifiedAccountMetadataSchema
187
+ *
188
+ * Schema for account metadata that supports both singlesig and multisig accounts.
189
+ * Stored in VaultKey.metadata for unified account management.
190
+ */
191
+ export const UnifiedAccountMetadataSchema = z.object({
192
+ /** Account type discriminator */
193
+ accountType: AccountTypeSchema.default('singlesig'),
194
+ /** Multisig configuration (only present for multisig accounts) */
195
+ multisig: MultisigInfoSchema.optional(),
196
+ /** User-defined label for the account */
197
+ label: z.string().optional(),
198
+ /** Whether this is an imported account (not derived from seed) */
199
+ imported: z.boolean().optional(),
200
+ /** Whether the account has been activated on-chain (for Hedera) */
201
+ activated: z.boolean().optional(),
202
+ /** Whether this account is hidden from the UI */
203
+ hidden: z.boolean().optional(),
204
+ /** Tags for organization */
205
+ tags: z.array(z.string()).optional(),
206
+ /** Notes about this account */
207
+ notes: z.string().optional(),
208
+ /** When the account was created in this wallet */
209
+ createdAt: z.number().optional(),
210
+ /** Last time the account was used for signing */
211
+ lastUsedAt: z.number().optional(),
212
+ });
213
+
214
+ /**
215
+ * UnifiedAccountMetadata
216
+ *
217
+ * Metadata structure for unified account management.
218
+ */
219
+ export type UnifiedAccountMetadata = z.infer<typeof UnifiedAccountMetadataSchema>;
220
+
221
+ // ============================================================================
222
+ // Utility Functions
223
+ // ============================================================================
224
+
225
+ /**
226
+ * isMultisigAccount
227
+ *
228
+ * Type guard to check if an account is a multisig account.
229
+ *
230
+ * @param metadata - Account metadata to check
231
+ * @returns true if the account is multisig
232
+ */
233
+ export function isMultisigAccount(metadata?: Record<string, unknown>): boolean {
234
+ return metadata?.['accountType'] === 'multisig' && !!metadata?.['multisig'];
235
+ }
236
+
237
+ /**
238
+ * getMultisigInfo
239
+ *
240
+ * Extract MultisigInfo from account metadata.
241
+ *
242
+ * @param metadata - Account metadata
243
+ * @returns MultisigInfo if present, undefined otherwise
244
+ */
245
+ export function getMultisigInfo(metadata?: Record<string, unknown>): MultisigInfo | undefined {
246
+ if (!isMultisigAccount(metadata)) {
247
+ return undefined;
248
+ }
249
+ return metadata?.['multisig'] as MultisigInfo;
250
+ }
251
+
252
+ /**
253
+ * createMultisigMetadata
254
+ *
255
+ * Create unified account metadata for a multisig account.
256
+ *
257
+ * @param config - MultisigAccountConfig to convert
258
+ * @param localSignerAddress - Address of the local signer
259
+ * @returns UnifiedAccountMetadata for storage in VaultKey
260
+ */
261
+ export function createMultisigMetadata(
262
+ config: MultisigAccountConfig,
263
+ localSignerAddress?: string,
264
+ ): UnifiedAccountMetadata {
265
+ return {
266
+ accountType: 'multisig',
267
+ multisig: {
268
+ threshold: config.threshold,
269
+ signers: config.signers,
270
+ localSignerAddress,
271
+ name: config.name,
272
+ originalConfigId: config.id,
273
+ },
274
+ label: config.name,
275
+ createdAt: config.createdAt,
276
+ };
277
+ }
278
+
279
+ /**
280
+ * convertSinglesigToMultisig
281
+ *
282
+ * Update account metadata to convert a singlesig account to multisig.
283
+ * Used when an XRPL account is converted to multisig on-chain.
284
+ *
285
+ * @param existingMetadata - Current account metadata
286
+ * @param multisigConfig - Multisig configuration to apply
287
+ * @param localSignerAddress - Address of the local signer
288
+ * @returns Updated metadata with multisig configuration
289
+ */
290
+ export function convertSinglesigToMultisig(
291
+ existingMetadata: Record<string, unknown> | undefined,
292
+ multisigConfig: { threshold: number; signers: MultisigSigner[]; name?: string },
293
+ localSignerAddress?: string,
294
+ ): UnifiedAccountMetadata {
295
+ const originalAddress = (existingMetadata?.['address'] as string) || undefined;
296
+
297
+ return {
298
+ ...existingMetadata,
299
+ accountType: 'multisig',
300
+ multisig: {
301
+ threshold: multisigConfig.threshold,
302
+ signers: multisigConfig.signers,
303
+ localSignerAddress,
304
+ name: multisigConfig.name,
305
+ convertedAt: Date.now(),
306
+ originalSinglesigAddress: originalAddress,
307
+ },
308
+ label: multisigConfig.name || (existingMetadata?.['label'] as string),
309
+ };
310
+ }