@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,60 @@
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
+ * Hedera node accounts that receive node-fee debits.
13
+ *
14
+ * @description
15
+ * Hedera mainnet has ~32 active consensus nodes whose account IDs run from
16
+ * `0.0.3` through `0.0.34`. Every transaction's node-fee component is paid
17
+ * to whichever node submitted the transaction — so any of these account IDs
18
+ * may appear as a positive-amount transfer in a transaction's HBAR transfer
19
+ * list.
20
+ *
21
+ * Filter against this set when you need to distinguish "node fee" debits
22
+ * from genuine user transfers.
23
+ */
24
+ export const HEDERA_NODE_ACCOUNTS: ReadonlySet<string> = new Set(
25
+ Array.from({ length: 32 }, (_, i) => `0.0.${i + 3}`),
26
+ );
27
+
28
+ /**
29
+ * Hedera fee/reward collector accounts (non-node).
30
+ *
31
+ * @description
32
+ * - `0.0.98` — main Hedera fee collection treasury.
33
+ * - `0.0.800` — staking reward distribution account.
34
+ * - `0.0.801` — node reward distribution account.
35
+ */
36
+ export const HEDERA_FEE_COLLECTOR_ACCOUNTS: ReadonlySet<string> = new Set([
37
+ '0.0.98',
38
+ '0.0.800',
39
+ '0.0.801',
40
+ ]);
41
+
42
+ /**
43
+ * Union of all Hedera system accounts: nodes + explicit fee/reward collectors.
44
+ *
45
+ * @description
46
+ * Canonical set used to filter system-level transfers out of user-facing
47
+ * displays (transaction history, transfer breakdowns, etc.). These accounts
48
+ * receive node fees, Hedera fees, and staking/node rewards — they appear in
49
+ * every transaction's transfer list but are noise from a wallet UX
50
+ * perspective.
51
+ *
52
+ * Cross-package consumers (the wallet UI, the ledger-hedera adapter) MUST
53
+ * import this set from `@hsuite/native-connect-types` so the filter stays in
54
+ * lock-step with the parser. Any new system account must be added here, not
55
+ * forked locally.
56
+ */
57
+ export const HEDERA_SYSTEM_ACCOUNTS: ReadonlySet<string> = new Set([
58
+ ...HEDERA_NODE_ACCOUNTS,
59
+ ...HEDERA_FEE_COLLECTOR_ACCOUNTS,
60
+ ]);
@@ -0,0 +1,243 @@
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 RED-phase tests for the shared `MemberIdentity` matcher.
13
+ *
14
+ * The matcher is the cross-package contract that ties the SDK's
15
+ * `canonicalMemberId(member)` output (the string keys populating
16
+ * `proposal.progress.members`) to the UI's pilot-filter logic. If the two
17
+ * normalizations drift, the UI silently fails to filter the pilot and
18
+ * presents a wrong "X of Y" count to the user (see
19
+ * `sign-progress-1of1-bug.md` for the live incident this guards).
20
+ *
21
+ * These tests pin the matcher to a small, exhaustive set of cross-ledger
22
+ * shapes (XRPL secp256k1 / Ed25519 / rAddress, Hedera Ed25519 in raw and
23
+ * DER form, ECDSA DER) and ensure:
24
+ *
25
+ * 1. Both directions match — given an identity with address+publicKey,
26
+ * a canonical id derived from EITHER field matches the identity.
27
+ * 2. Wrapped variants (XRPL `ed`, Ed25519 DER, ECDSA DER, `0x` prefix,
28
+ * upper/lower case) all normalize to the same canonical id.
29
+ * 3. Identities with neither field are rejected.
30
+ * 4. Distinct keys do not collide after normalization.
31
+ */
32
+
33
+ import { describe, it, expect } from 'vitest';
34
+ import { type MemberIdentity, normalizeIdentityKey, matchesMemberIdentity } from './identity.js';
35
+
36
+ describe('normalizeIdentityKey', () => {
37
+ it('lower-cases ASCII hex', () => {
38
+ expect(normalizeIdentityKey('ABCDEF0123456789')).toBe('abcdef0123456789');
39
+ });
40
+
41
+ it('strips a leading "0x" prefix (mixed case)', () => {
42
+ expect(normalizeIdentityKey('0xDEADBEEF')).toBe('deadbeef');
43
+ expect(normalizeIdentityKey('0Xdeadbeef')).toBe('deadbeef');
44
+ });
45
+
46
+ it('strips XRPL Ed25519 "ed" byte prefix for 33-byte keys (66 hex chars)', () => {
47
+ const raw32 = '11'.repeat(32);
48
+ expect(normalizeIdentityKey(`ed${raw32}`)).toBe(raw32);
49
+ });
50
+
51
+ it('does NOT strip "ed" when length is wrong (defensive: never rewrite ambiguous keys)', () => {
52
+ // A 64-char hex that happens to start with 'ed' is NOT the XRPL prefix.
53
+ const sneaky = 'ed' + '00'.repeat(31);
54
+ expect(sneaky.length).toBe(64);
55
+ expect(normalizeIdentityKey(sneaky)).toBe(sneaky);
56
+ });
57
+
58
+ it('strips Ed25519 DER prefix when total length is exactly 88 hex chars', () => {
59
+ const der = '302a300506032b6570032100';
60
+ const raw32 = 'aa'.repeat(32);
61
+ expect(normalizeIdentityKey(`${der}${raw32}`)).toBe(raw32);
62
+ });
63
+
64
+ it('strips ECDSA secp256k1 uncompressed DER prefix when total length is exactly 176 hex chars', () => {
65
+ const der = '3056301006072a8648ce3d020106052b8104000a034200';
66
+ const raw65 = 'bb'.repeat(65);
67
+ expect(normalizeIdentityKey(`${der}${raw65}`)).toBe(raw65);
68
+ });
69
+
70
+ it('leaves XRPL rAddresses unchanged except for lowercasing', () => {
71
+ expect(normalizeIdentityKey('rEQnA3cVcJXmH4czsiS')).toBe('reqna3cvcjxmh4czsis');
72
+ });
73
+
74
+ it('leaves Hedera account IDs unchanged except for lowercasing', () => {
75
+ expect(normalizeIdentityKey('0.0.12345')).toBe('0.0.12345');
76
+ });
77
+
78
+ it('returns empty string for empty input (defensive)', () => {
79
+ expect(normalizeIdentityKey('')).toBe('');
80
+ });
81
+
82
+ it('is idempotent — normalizing the output again is a no-op', () => {
83
+ const inputs = [
84
+ '0xDEADBEEF',
85
+ 'edAA'.padEnd(66, 'B'),
86
+ '302a300506032b6570032100' + 'cc'.repeat(32),
87
+ '0.0.12345',
88
+ 'rEQnA3cVcJXmH4czsiS',
89
+ ];
90
+ for (const input of inputs) {
91
+ const once = normalizeIdentityKey(input);
92
+ const twice = normalizeIdentityKey(once);
93
+ expect(twice).toBe(once);
94
+ }
95
+ });
96
+ });
97
+
98
+ describe('matchesMemberIdentity', () => {
99
+ const xrplPubKey = '02c9742b4c3e6fc9245280eb3cb0296ed089bafd838cf64780674e67fe04b75180';
100
+ const xrplRAddress = 'rEQnA3cVcJXmH4czsiS';
101
+ const hederaPubKeyRaw = '11'.repeat(32);
102
+ const hederaPubKeyDer = '302a300506032b6570032100' + hederaPubKeyRaw;
103
+ const hederaAccountId = '0.0.987654';
104
+
105
+ describe('happy paths — canonical id derived from publicKey', () => {
106
+ it('matches an identity carrying the same publicKey (XRPL secp256k1)', () => {
107
+ const canonical = normalizeIdentityKey(xrplPubKey);
108
+ const identity: MemberIdentity = { publicKey: xrplPubKey };
109
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
110
+ });
111
+
112
+ it('matches an identity carrying ONLY the rAddress when canonical=publicKey AND identity carries both', () => {
113
+ // SDK populated `members` with publicKey-derived canonical id (XRPL signer
114
+ // with pubkey present). Wallet's pilot identity carries BOTH rAddress
115
+ // and publicKey — matcher must hit the publicKey arm and succeed.
116
+ const canonical = normalizeIdentityKey(xrplPubKey);
117
+ const identity: MemberIdentity = {
118
+ address: xrplRAddress,
119
+ publicKey: xrplPubKey,
120
+ };
121
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
122
+ });
123
+
124
+ it('matches an identity carrying ONLY the rAddress when canonical=rAddress (XRPL keyless legacy member)', () => {
125
+ // Some test fixtures / older XRPL invitations omit publicKey on the
126
+ // member entry — canonical id falls back to address.
127
+ const canonical = normalizeIdentityKey(xrplRAddress);
128
+ const identity: MemberIdentity = { address: xrplRAddress };
129
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
130
+ });
131
+
132
+ it('cross-matches an identity carrying ONLY the address when canonical was derived from publicKey', () => {
133
+ // SDK derived canonical from publicKey (most XRPL multisigs). Wallet
134
+ // built pilotIdentity from a local account that only has address.
135
+ // The matcher must NOT silently miss — this is the XRPL B2 case.
136
+ const canonical = normalizeIdentityKey(xrplPubKey);
137
+ const identity: MemberIdentity = { address: xrplPubKey }; // intentional
138
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
139
+ });
140
+
141
+ it('matches Hedera raw 32-byte pubkey identity against canonical from DER form', () => {
142
+ const canonical = normalizeIdentityKey(hederaPubKeyDer);
143
+ const identity: MemberIdentity = { publicKey: hederaPubKeyRaw };
144
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
145
+ });
146
+
147
+ it('matches Hedera DER-wrapped pubkey identity against canonical from raw form', () => {
148
+ const canonical = normalizeIdentityKey(hederaPubKeyRaw);
149
+ const identity: MemberIdentity = { publicKey: hederaPubKeyDer };
150
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
151
+ });
152
+
153
+ it('matches Hedera account id (no pubkey) when canonical=address', () => {
154
+ const canonical = normalizeIdentityKey(hederaAccountId);
155
+ const identity: MemberIdentity = { address: hederaAccountId };
156
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
157
+ });
158
+
159
+ it('matches when both address and publicKey are present and EITHER would match', () => {
160
+ const canonical = normalizeIdentityKey(xrplPubKey);
161
+ const identity: MemberIdentity = {
162
+ address: xrplRAddress,
163
+ publicKey: xrplPubKey,
164
+ };
165
+ expect(matchesMemberIdentity(canonical, identity)).toBe(true);
166
+ });
167
+ });
168
+
169
+ describe('negative paths', () => {
170
+ it('rejects an identity with neither address nor publicKey', () => {
171
+ expect(matchesMemberIdentity('any', {})).toBe(false);
172
+ });
173
+
174
+ it('rejects an identity with empty-string fields (no useful information)', () => {
175
+ expect(matchesMemberIdentity('any', { address: '', publicKey: '' })).toBe(false);
176
+ });
177
+
178
+ it('rejects when canonical id is empty', () => {
179
+ expect(matchesMemberIdentity('', { publicKey: xrplPubKey })).toBe(false);
180
+ });
181
+
182
+ it('rejects two distinct keys that differ only after normalization', () => {
183
+ const other = '02' + 'cc'.repeat(32); // different XRPL secp256k1 pubkey
184
+ const canonical = normalizeIdentityKey(xrplPubKey);
185
+ expect(matchesMemberIdentity(canonical, { publicKey: other })).toBe(false);
186
+ expect(matchesMemberIdentity(canonical, { address: other })).toBe(false);
187
+ });
188
+
189
+ it('does not silently match a PKCS#8 private-key prefix against an arbitrary canonical id', () => {
190
+ // Defensive: even if a private key sneaks into MemberIdentity (it
191
+ // never should), the normalize path must not collapse it to a value
192
+ // that collides with anyone's pubkey. The simplest guarantee is:
193
+ // PKCS#8 bytes don't share a prefix with raw Ed25519 keys, so the
194
+ // normalized result is still a different string.
195
+ const pkcs8 = '302e020100300506032b657004220420' + '00'.repeat(32);
196
+ const canonical = normalizeIdentityKey(hederaPubKeyRaw);
197
+ expect(matchesMemberIdentity(canonical, { publicKey: pkcs8 })).toBe(false);
198
+ });
199
+
200
+ it('is case-insensitive: address fields differing only in case still match', () => {
201
+ const upper = 'rEQnA3cVcJXmH4czsiS'.toUpperCase();
202
+ const canonical = normalizeIdentityKey('rEQnA3cVcJXmH4czsiS');
203
+ expect(matchesMemberIdentity(canonical, { address: upper })).toBe(true);
204
+ });
205
+ });
206
+
207
+ describe('symmetry', () => {
208
+ // Property: for every member that the SDK might canonicalize, an
209
+ // identity carrying ALL of (address, publicKey) the SDK saw must match
210
+ // the canonical id derived from EITHER side. This is the cross-key
211
+ // invariant the wallet relies on for B2 immunity.
212
+ const cases: Array<{ name: string; address: string; publicKey: string }> = [
213
+ {
214
+ name: 'XRPL with secp256k1 pubkey',
215
+ address: xrplRAddress,
216
+ publicKey: xrplPubKey,
217
+ },
218
+ {
219
+ name: 'Hedera raw Ed25519',
220
+ address: hederaAccountId,
221
+ publicKey: hederaPubKeyRaw,
222
+ },
223
+ {
224
+ name: 'Hedera DER-wrapped Ed25519',
225
+ address: hederaAccountId,
226
+ publicKey: hederaPubKeyDer,
227
+ },
228
+ ];
229
+
230
+ for (const c of cases) {
231
+ it(`symmetry holds for ${c.name}`, () => {
232
+ const idFromPubkey = normalizeIdentityKey(c.publicKey);
233
+ const idFromAddress = normalizeIdentityKey(c.address);
234
+ const identity: MemberIdentity = {
235
+ address: c.address,
236
+ publicKey: c.publicKey,
237
+ };
238
+ expect(matchesMemberIdentity(idFromPubkey, identity)).toBe(true);
239
+ expect(matchesMemberIdentity(idFromAddress, identity)).toBe(true);
240
+ });
241
+ }
242
+ });
243
+ });
@@ -0,0 +1,244 @@
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 Cross-package member identity contract for multisig flows.
13
+ * @module @hsuite/native-connect-types/identity
14
+ *
15
+ * @description
16
+ * Defines the small, pure surface that ties the SDK's
17
+ * `canonicalMemberId(member)` output (the strings the SDK uses as keys in
18
+ * `proposal.progress.members`) to the wallet UI's pilot-filter logic.
19
+ *
20
+ * ## Why this lives in `wallet-connect-types`
21
+ *
22
+ * The SDK owns the canonical id derivation (see
23
+ * `packages/native-wallet-sdk/src/multisig/member-identity.ts`). The UI
24
+ * package owns the rendering of "X of Y member signatures" and needs to
25
+ * compare a wallet-resolved "this is the pilot" identity against the
26
+ * SDK-populated `members[]` strings. Putting the matcher in a heavyweight
27
+ * package (SDK) would either drag SDK runtime deps into the UI bundle or
28
+ * force a duplicate normalize implementation in the UI — both DRY
29
+ * violations. `wallet-connect-types` is the no-runtime-deps shared
30
+ * boundary both packages already depend on, so the matcher lives here.
31
+ *
32
+ * The SDK re-exports {@link MemberIdentity} and {@link matchesMemberIdentity}
33
+ * from its own `multisig/index.ts` so SDK consumers can also pick them up
34
+ * via `@hsuite/native-connect-sdk` without learning a second package name.
35
+ *
36
+ * ## The bug this prevents
37
+ *
38
+ * Before this contract existed, the wallet UI compared the pilot's
39
+ * rAddress (e.g. `rEQn…`) against `progress.members[0] = '02c9742b…'`
40
+ * (XRPL secp256k1 pubkey, populated by the SDK via `canonicalMemberId`
41
+ * which prefers `publicKey`). The `!==` filter was a no-op on XRPL and
42
+ * the UI rendered "1 of 1 — Ready! You can now sign & submit." for a
43
+ * 2-of-2 multisig where no cosigner had signed. See the live incident at
44
+ * `multisig-update:.multisig-p2p/evidence/sign-progress-1of1-bug.md`.
45
+ *
46
+ * ## Normalization algorithm
47
+ *
48
+ * The same algorithm the SDK uses for its canonical id derivation, minus
49
+ * the SDK-specific PKCS#8 private-key leak detection (which is a separate
50
+ * security concern handled at key-loading boundaries, not comparison
51
+ * boundaries). Strips, in order:
52
+ *
53
+ * 1. `0x` / `0X` hex prefix.
54
+ * 2. Case (lowercased).
55
+ * 3. XRPL Ed25519 `ed` byte prefix when length is exactly 66 hex chars
56
+ * (33 bytes — XRPL's wire encoding for Ed25519 pubkeys).
57
+ * 4. Ed25519 DER wrapper (`302a300506032b6570032100` + 32 raw bytes)
58
+ * when length is exactly 88 hex chars.
59
+ * 5. ECDSA secp256k1 uncompressed DER wrapper
60
+ * (`3056301006072a8648ce3d020106052b8104000a034200` + 65 raw bytes)
61
+ * when length is exactly 176 hex chars.
62
+ *
63
+ * Anything not matching one of those wrappers is returned unchanged
64
+ * (lowercase). Two genuinely different keys remain different after
65
+ * normalization — the function only strips wrappers whose semantics are
66
+ * "same key in a different encoding", never rewrites key material.
67
+ */
68
+
69
+ // ============================================================================
70
+ // Wire-format prefixes
71
+ // ============================================================================
72
+
73
+ /**
74
+ * Ed25519 DER-wrapped public key prefix.
75
+ *
76
+ * Hex representation: `302a300506032b6570032100` (12 bytes / 24 hex chars).
77
+ * Followed by 32 bytes (64 hex chars) of raw key → total 88 hex chars.
78
+ */
79
+ const ED25519_DER_PREFIX = '302a300506032b6570032100';
80
+
81
+ /**
82
+ * ECDSA secp256k1 uncompressed DER public key prefix.
83
+ *
84
+ * Hex representation: `3056301006072a8648ce3d020106052b8104000a034200`
85
+ * (23 bytes / 46 hex chars). Followed by 65 bytes (130 hex chars) of
86
+ * uncompressed key → total 176 hex chars.
87
+ */
88
+ const ECDSA_DER_PREFIX = '3056301006072a8648ce3d020106052b8104000a034200';
89
+
90
+ /** Hex length (chars) of an XRPL-encoded Ed25519 pubkey (1 byte `ed` prefix + 32 raw bytes). */
91
+ const XRPL_ED25519_HEX_LEN = 66;
92
+
93
+ /** Hex length (chars) of an Ed25519 DER-wrapped pubkey (24 prefix + 64 raw). */
94
+ const ED25519_DER_HEX_LEN = 88;
95
+
96
+ /** Hex length (chars) of an ECDSA secp256k1 uncompressed DER pubkey (46 prefix + 130 raw). */
97
+ const ECDSA_DER_HEX_LEN = 176;
98
+
99
+ // ============================================================================
100
+ // Types
101
+ // ============================================================================
102
+
103
+ /**
104
+ * A reference to a party member, carrying either the member's on-chain
105
+ * address (Hedera account id, XRPL rAddress, etc.) or the member's
106
+ * cryptographic public key, or both.
107
+ *
108
+ * Both fields are optional because different ledgers and different
109
+ * lookup paths surface different combinations:
110
+ *
111
+ * - **Hedera multisig signers** typically carry only a `publicKey` (the
112
+ * account is materialized from the key).
113
+ * - **XRPL multisig signers** carry both an `address` (the rAddress, the
114
+ * on-chain identity) and a `publicKey` (the secp256k1 / Ed25519 pubkey
115
+ * used to verify signatures).
116
+ * - **Wallet-local signers** carry an `address` (the local account's
117
+ * primary identifier) and usually a `publicKey` derived at key-import
118
+ * time.
119
+ *
120
+ * The matcher {@link matchesMemberIdentity} accepts any of these
121
+ * combinations and resolves them against a canonical id string by trying
122
+ * each populated field independently.
123
+ *
124
+ * Empty-string fields are treated as absent — they carry no useful
125
+ * information and must not collapse to a normalized empty string that
126
+ * happens to match another empty-string identity.
127
+ */
128
+ export interface MemberIdentity {
129
+ /** On-chain address for the member, if known. */
130
+ address?: string;
131
+ /** Public key for the member (any supported wire format), if known. */
132
+ publicKey?: string;
133
+ }
134
+
135
+ // ============================================================================
136
+ // Normalization
137
+ // ============================================================================
138
+
139
+ /**
140
+ * Normalize a member-identity field (address OR publicKey) to its raw
141
+ * canonical hex / lowercase form so two callers with the same underlying
142
+ * key in different wire formats compare equal.
143
+ *
144
+ * @param value - Hex pubkey (with or without DER / XRPL / `0x` wrappers)
145
+ * or an on-chain address (Hedera account id, XRPL rAddress, anything
146
+ * else opaque). Empty string is accepted and returns empty string.
147
+ * @returns Normalized lowercase string. Returns empty string only for
148
+ * empty input. Two distinct keys remain distinct after normalization;
149
+ * the function only strips wrappers whose semantics are "this is the
150
+ * same key in a different encoding."
151
+ */
152
+ export function normalizeIdentityKey(value: string): string {
153
+ if (value.length === 0) return '';
154
+
155
+ // Strip 0x / 0X prefix and lowercase. After this, every subsequent
156
+ // length check uses the prefix-stripped hex length.
157
+ let hex = value.replace(/^0x/i, '').toLowerCase();
158
+
159
+ // XRPL Ed25519 keys are encoded as a single `ed` byte (2 hex chars)
160
+ // followed by 32 raw bytes (64 hex chars). Strip the prefix ONLY when
161
+ // the total length matches — a plain 64-char hex that happens to start
162
+ // with `ed` is a different key and must not be silently rewritten.
163
+ if (hex.startsWith('ed') && hex.length === XRPL_ED25519_HEX_LEN) {
164
+ hex = hex.slice(2);
165
+ }
166
+
167
+ // Ed25519 DER wrapper: full DER-encoded SubjectPublicKeyInfo, total
168
+ // length is exactly 88 hex chars (24 prefix + 64 raw).
169
+ if (hex.startsWith(ED25519_DER_PREFIX) && hex.length === ED25519_DER_HEX_LEN) {
170
+ hex = hex.slice(ED25519_DER_PREFIX.length);
171
+ }
172
+
173
+ // ECDSA secp256k1 uncompressed DER wrapper: total length is exactly
174
+ // 176 hex chars (46 prefix + 130 raw). The compressed form (66 chars,
175
+ // `02`/`03` prefix) is already canonical and not unwrapped.
176
+ if (hex.startsWith(ECDSA_DER_PREFIX) && hex.length === ECDSA_DER_HEX_LEN) {
177
+ hex = hex.slice(ECDSA_DER_PREFIX.length);
178
+ }
179
+
180
+ return hex;
181
+ }
182
+
183
+ // ============================================================================
184
+ // Matcher
185
+ // ============================================================================
186
+
187
+ /**
188
+ * Does the given `canonicalId` (an SDK-derived signature-store key)
189
+ * refer to the given `identity`?
190
+ *
191
+ * Tries each populated identity field independently against the
192
+ * canonical id, normalized through {@link normalizeIdentityKey}. Returns
193
+ * true if EITHER the `publicKey` field or the `address` field — after
194
+ * normalization — equals the canonical id.
195
+ *
196
+ * ## Why "either" is correct
197
+ *
198
+ * The SDK's `canonicalMemberId(member)` prefers `member.publicKey` when
199
+ * present, falling back to `member.address`. Wallets that resolve "this
200
+ * is the local pilot" might end up with an identity carrying only the
201
+ * address (e.g. legacy XRPL invitations) or only the publicKey (Hedera
202
+ * raw key import), or both. The matcher must succeed in all three cases
203
+ * — otherwise a legitimate filter silently no-ops and the UI lies about
204
+ * signature progress.
205
+ *
206
+ * ## Empty / unusable identity guard
207
+ *
208
+ * Returns false when:
209
+ *
210
+ * - The canonical id is empty (defensive — an empty canonical id should
211
+ * never appear in practice, but guarding here prevents a stray empty
212
+ * entry from matching every empty identity).
213
+ * - The identity has neither field populated.
214
+ * - The identity has only empty-string fields.
215
+ *
216
+ * This guarantees that "I don't know who the pilot is" never silently
217
+ * matches arbitrary members.
218
+ *
219
+ * @param canonicalId - SDK-derived signature-store key (output of
220
+ * `canonicalMemberId(member)` on the SDK side).
221
+ * @param identity - Wallet-resolved identity (address and/or publicKey)
222
+ * to test against the canonical id.
223
+ * @returns True if the identity refers to the canonical id, false otherwise.
224
+ */
225
+ export function matchesMemberIdentity(canonicalId: string, identity: MemberIdentity): boolean {
226
+ const target = normalizeIdentityKey(canonicalId);
227
+ if (target.length === 0) return false;
228
+
229
+ const candidatePubKey =
230
+ identity.publicKey && identity.publicKey.length > 0
231
+ ? normalizeIdentityKey(identity.publicKey)
232
+ : '';
233
+ if (candidatePubKey.length > 0 && candidatePubKey === target) {
234
+ return true;
235
+ }
236
+
237
+ const candidateAddress =
238
+ identity.address && identity.address.length > 0 ? normalizeIdentityKey(identity.address) : '';
239
+ if (candidateAddress.length > 0 && candidateAddress === target) {
240
+ return true;
241
+ }
242
+
243
+ return false;
244
+ }