adapters.ts raw
1 /**
2 * Adapter functions for gradual migration from legacy code to Identity domain objects.
3 */
4
5 import { Account } from './Account'
6 import { SignerType, SignerTypeValue } from './SignerType'
7
8 /**
9 * Legacy TAccount type for reference
10 */
11 type LegacyAccount = {
12 pubkey: string
13 signerType: SignerTypeValue
14 ncryptsec?: string
15 nsec?: string
16 npub?: string
17 }
18
19 /**
20 * Legacy TAccountPointer type for reference
21 */
22 type LegacyAccountPointer = {
23 pubkey: string
24 signerType: SignerTypeValue
25 }
26
27 // ============================================================================
28 // Account Adapters
29 // ============================================================================
30
31 /**
32 * Convert a legacy TAccount to an Account domain object
33 */
34 export const toAccount = (legacy: LegacyAccount): Account | null => {
35 return Account.fromLegacy(legacy)
36 }
37
38 /**
39 * Convert an Account domain object to legacy TAccount format
40 */
41 export const fromAccount = (account: Account): LegacyAccount => {
42 return account.toLegacy()
43 }
44
45 /**
46 * Convert multiple legacy accounts to domain objects
47 */
48 export const toAccounts = (legacyAccounts: LegacyAccount[]): Account[] => {
49 return legacyAccounts.map((a) => toAccount(a)).filter((a): a is Account => a !== null)
50 }
51
52 /**
53 * Convert multiple domain accounts to legacy format
54 */
55 export const fromAccounts = (accounts: Account[]): LegacyAccount[] => {
56 return accounts.map((a) => fromAccount(a))
57 }
58
59 /**
60 * Check if two account pointers are the same
61 */
62 export const isSameAccount = (
63 a: LegacyAccountPointer | null,
64 b: LegacyAccountPointer | null
65 ): boolean => {
66 if (!a || !b) return false
67 return a.pubkey === b.pubkey && a.signerType === b.signerType
68 }
69
70 /**
71 * Check if two accounts have the same pubkey
72 */
73 export const isSamePubkey = (
74 a: LegacyAccountPointer | null,
75 b: LegacyAccountPointer | null
76 ): boolean => {
77 if (!a || !b) return false
78 return a.pubkey === b.pubkey
79 }
80
81 // ============================================================================
82 // SignerType Adapters
83 // ============================================================================
84
85 /**
86 * Convert a string to a SignerType domain object
87 */
88 export const toSignerType = (value: string): SignerType | null => {
89 return SignerType.tryFromString(value)
90 }
91
92 /**
93 * Convert a SignerType to its string value
94 */
95 export const fromSignerType = (signerType: SignerType): SignerTypeValue => {
96 return signerType.value
97 }
98
99 /**
100 * Check if a signer type can sign events
101 */
102 export const canSign = (signerType: string): boolean => {
103 const type = SignerType.tryFromString(signerType)
104 return type ? type.canSign : false
105 }
106
107 /**
108 * Check if a signer type is view-only
109 */
110 export const isViewOnly = (signerType: string): boolean => {
111 const type = SignerType.tryFromString(signerType)
112 return type ? type.isViewOnly : false
113 }
114
115 /**
116 * Get the display name for a signer type
117 */
118 export const getSignerTypeDisplayName = (signerType: string): string => {
119 const type = SignerType.tryFromString(signerType)
120 return type ? type.displayName : 'Unknown'
121 }
122
123 // ============================================================================
124 // Account List Helpers
125 // ============================================================================
126
127 /**
128 * Find an account in a list by pubkey and signer type
129 */
130 export const findAccount = (
131 accounts: Account[],
132 pubkey: string,
133 signerType: string
134 ): Account | undefined => {
135 return accounts.find(
136 (a) => a.pubkey.hex === pubkey && a.signerType.value === signerType
137 )
138 }
139
140 /**
141 * Find all accounts with a specific pubkey
142 */
143 export const findAccountsByPubkey = (accounts: Account[], pubkey: string): Account[] => {
144 return accounts.filter((a) => a.pubkey.hex === pubkey)
145 }
146
147 /**
148 * Remove an account from a list
149 */
150 export const removeAccount = (
151 accounts: Account[],
152 pubkey: string,
153 signerType: string
154 ): Account[] => {
155 return accounts.filter(
156 (a) => !(a.pubkey.hex === pubkey && a.signerType.value === signerType)
157 )
158 }
159
160 /**
161 * Add or update an account in a list
162 */
163 export const upsertAccount = (accounts: Account[], account: Account): Account[] => {
164 const existing = accounts.findIndex((a) => a.equals(account))
165 if (existing >= 0) {
166 const result = [...accounts]
167 result[existing] = account
168 return result
169 }
170 return [...accounts, account]
171 }
172