@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.
- package/README.md +50 -0
- package/dist/address-avatar.d.ts +67 -0
- package/dist/address-avatar.d.ts.map +1 -0
- package/dist/address-avatar.js +130 -0
- package/dist/address-avatar.js.map +1 -0
- package/dist/addressbook.d.ts +289 -0
- package/dist/addressbook.d.ts.map +1 -0
- package/dist/addressbook.js +82 -0
- package/dist/addressbook.js.map +1 -0
- package/dist/enrichment.d.ts +88 -0
- package/dist/enrichment.d.ts.map +1 -0
- package/dist/enrichment.js +47 -0
- package/dist/enrichment.js.map +1 -0
- package/dist/hedera-system-accounts.d.ts +49 -0
- package/dist/hedera-system-accounts.d.ts.map +1 -0
- package/dist/hedera-system-accounts.js +56 -0
- package/dist/hedera-system-accounts.js.map +1 -0
- package/dist/identity.d.ts +94 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +190 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/ledger.d.ts +848 -0
- package/dist/ledger.d.ts.map +1 -0
- package/dist/ledger.js +152 -0
- package/dist/ledger.js.map +1 -0
- package/dist/messages.d.ts +726 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +252 -0
- package/dist/messages.js.map +1 -0
- package/dist/multisig-account.d.ts +419 -0
- package/dist/multisig-account.d.ts.map +1 -0
- package/dist/multisig-account.js +201 -0
- package/dist/multisig-account.js.map +1 -0
- package/dist/private-key-export.d.ts +112 -0
- package/dist/private-key-export.d.ts.map +1 -0
- package/dist/private-key-export.js +11 -0
- package/dist/private-key-export.js.map +1 -0
- package/dist/rpc.d.ts +872 -0
- package/dist/rpc.d.ts.map +1 -0
- package/dist/rpc.js +143 -0
- package/dist/rpc.js.map +1 -0
- package/dist/transaction-approval.d.ts +895 -0
- package/dist/transaction-approval.d.ts.map +1 -0
- package/dist/transaction-approval.js +11 -0
- package/dist/transaction-approval.js.map +1 -0
- package/dist/transaction-parser.d.ts +93 -0
- package/dist/transaction-parser.d.ts.map +1 -0
- package/dist/transaction-parser.js +11 -0
- package/dist/transaction-parser.js.map +1 -0
- package/dist/vault.d.ts +673 -0
- package/dist/vault.d.ts.map +1 -0
- package/dist/vault.js +188 -0
- package/dist/vault.js.map +1 -0
- package/dist/widget.types.d.ts +323 -0
- package/dist/widget.types.d.ts.map +1 -0
- package/dist/widget.types.js +140 -0
- package/dist/widget.types.js.map +1 -0
- package/package.json +43 -0
- package/src/address-avatar.ts +181 -0
- package/src/addressbook.spec.ts +220 -0
- package/src/addressbook.ts +115 -0
- package/src/enrichment.ts +97 -0
- package/src/hedera-system-accounts.ts +60 -0
- package/src/identity.spec.ts +243 -0
- package/src/identity.ts +244 -0
- package/src/index.ts +197 -0
- package/src/ledger.spec.ts +366 -0
- package/src/ledger.ts +567 -0
- package/src/messages.ts +381 -0
- package/src/multisig-account.spec.ts +362 -0
- package/src/multisig-account.ts +310 -0
- package/src/private-key-export.ts +117 -0
- package/src/rpc.spec.ts +396 -0
- package/src/rpc.ts +257 -0
- package/src/transaction-approval.ts +1033 -0
- package/src/transaction-parser.ts +98 -0
- package/src/vault.spec.ts +268 -0
- package/src/vault.ts +256 -0
- package/src/widget.types.spec.ts +337 -0
- package/src/widget.types.ts +501 -0
- package/tsconfig.build.json +17 -0
- package/tsconfig.json +11 -0
- package/vitest.config.ts +36 -0
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hsuite/native-connect-types",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -p tsconfig.build.json",
|
|
10
|
+
"watch": "tsc -p tsconfig.build.json --watch",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:watch": "vitest --watch",
|
|
13
|
+
"test:coverage": "vitest run --coverage",
|
|
14
|
+
"docs": "compodoc -p tsconfig.build.json -c ../../compodoc.base.json -d dist/docs --name 'Shared Types API' --includes docs"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./messages": {
|
|
23
|
+
"types": "./dist/messages.d.ts",
|
|
24
|
+
"import": "./dist/messages.js"
|
|
25
|
+
},
|
|
26
|
+
"./rpc": {
|
|
27
|
+
"types": "./dist/rpc.d.ts",
|
|
28
|
+
"import": "./dist/rpc.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"zod": "^3.23.8"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@compodoc/compodoc": "^1.1.31",
|
|
36
|
+
"@vitest/coverage-v8": "^1.6.0",
|
|
37
|
+
"typescript": "^5.8.2",
|
|
38
|
+
"vitest": "^1.6.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
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
|
+
* Deterministic "marble" avatar generator for wallet addresses.
|
|
13
|
+
*
|
|
14
|
+
* Visual model is the Marble variant from boring-avatars
|
|
15
|
+
* (https://boringavatars.com — MIT licensed): three colors blended via
|
|
16
|
+
* gaussian-blurred organic shapes, all clipped to a circle. The path data
|
|
17
|
+
* and transform algorithm are ported verbatim; only the color palette is
|
|
18
|
+
* re-tinted to the HSuite brand so account avatars stay coherent with the
|
|
19
|
+
* rest of the wallet (purple-cyan family) instead of injecting unrelated
|
|
20
|
+
* cyber-neon colors per address.
|
|
21
|
+
*
|
|
22
|
+
* Replaces the previous emoji-based avatar (💎🚀🛸 etc.) which:
|
|
23
|
+
* - rendered differently on every OS (Apple emoji vs Windows Segoe etc.)
|
|
24
|
+
* - carried "degen NFT 2021" cultural baggage that clashed with the
|
|
25
|
+
* wallet's data-terminal aesthetic
|
|
26
|
+
* - had only 30 × 15 = 450 unique combinations
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Per-shape transform derived from the address hash. The two non-base
|
|
31
|
+
* shapes get independent translate / rotate / scale so the marble
|
|
32
|
+
* composition feels organic rather than repetitive.
|
|
33
|
+
*/
|
|
34
|
+
export interface MarbleShape {
|
|
35
|
+
translateX: number;
|
|
36
|
+
translateY: number;
|
|
37
|
+
rotate: number;
|
|
38
|
+
scale: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Resolved avatar data — three colors plus the transforms for the two
|
|
43
|
+
* blurred shapes that float over the base color.
|
|
44
|
+
*/
|
|
45
|
+
export interface MarbleAvatar {
|
|
46
|
+
/** Base color, then two overlay shape colors. */
|
|
47
|
+
colors: [string, string, string];
|
|
48
|
+
/** Transforms for the two overlay shapes (base has no transform). */
|
|
49
|
+
shapes: [MarbleShape, MarbleShape];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Curated HSuite brand palette. All colors live in the purple-cyan family
|
|
54
|
+
* so any combination feels on-brand. Order matters only for determinism;
|
|
55
|
+
* the hash function reads from the array uniformly.
|
|
56
|
+
*/
|
|
57
|
+
const HSUITE_PALETTE = [
|
|
58
|
+
'#8B7FFF', // primary purple
|
|
59
|
+
'#00D9FF', // secondary cyan
|
|
60
|
+
'#A855F7', // accent purple
|
|
61
|
+
'#7C3AED', // deep purple
|
|
62
|
+
'#06B6D4', // teal cyan
|
|
63
|
+
'#3B82F6', // electric blue
|
|
64
|
+
'#22D3EE', // cyan
|
|
65
|
+
'#6366F1', // indigo
|
|
66
|
+
] as const;
|
|
67
|
+
|
|
68
|
+
/** Reference SVG viewport size — boring-avatars marble is built around 80x80. */
|
|
69
|
+
const SIZE = 80;
|
|
70
|
+
const ELEMENTS = 3;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Same string-hash as the previous emoji implementation (RainbowKit's
|
|
74
|
+
* algorithm), kept so existing addresses don't dramatically reshuffle
|
|
75
|
+
* their avatars on rollout — they just get the new visual treatment.
|
|
76
|
+
* @param str
|
|
77
|
+
*/
|
|
78
|
+
function hashCode(str: string): number {
|
|
79
|
+
let hash = 0;
|
|
80
|
+
for (let i = 0; i < str.length; i++) {
|
|
81
|
+
const chr = str.charCodeAt(i);
|
|
82
|
+
hash = (hash << 5) - hash + chr;
|
|
83
|
+
hash |= 0;
|
|
84
|
+
}
|
|
85
|
+
return Math.abs(hash);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Extract the n-th decimal digit of `num`.
|
|
90
|
+
* @param num
|
|
91
|
+
* @param ntn
|
|
92
|
+
*/
|
|
93
|
+
function getDigit(num: number, ntn: number): number {
|
|
94
|
+
return Math.floor((num / Math.pow(10, ntn)) % 10);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Bounded random unit derived from the hash. `range` controls magnitude;
|
|
99
|
+
* `index` (when provided) flips the sign on odd digits — gives the marble
|
|
100
|
+
* shapes asymmetric drift instead of always-positive offsets.
|
|
101
|
+
* @param num
|
|
102
|
+
* @param range
|
|
103
|
+
* @param index
|
|
104
|
+
*/
|
|
105
|
+
function getUnit(num: number, range: number, index?: number): number {
|
|
106
|
+
const value = num % range;
|
|
107
|
+
if (index !== undefined && getDigit(num, index) % 2 === 0) {
|
|
108
|
+
return -value;
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Pick the n-th palette entry (mod length) for deterministic color selection.
|
|
115
|
+
* @param num
|
|
116
|
+
* @param palette
|
|
117
|
+
*/
|
|
118
|
+
function pickColor(num: number, palette: readonly string[]): string {
|
|
119
|
+
return palette[num % palette.length];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Generate the marble avatar parameters for a given address.
|
|
124
|
+
*
|
|
125
|
+
* Deterministic: same address (case-insensitive) always returns the same
|
|
126
|
+
* avatar. Pass a different `palette` to tint avatars for non-HSuite
|
|
127
|
+
* contexts (e.g., a partner-branded variant).
|
|
128
|
+
* @param address
|
|
129
|
+
* @param palette
|
|
130
|
+
*/
|
|
131
|
+
export function avatarForAddress(address: string, palette?: readonly string[]): MarbleAvatar {
|
|
132
|
+
// Fall back to the HSuite default if no palette is supplied or the caller
|
|
133
|
+
// accidentally passed an empty array (defensive — modulo into a 0-length
|
|
134
|
+
// array yields NaN, which would crash color lookup downstream).
|
|
135
|
+
const colors = palette && palette.length > 0 ? palette : HSUITE_PALETTE;
|
|
136
|
+
const seed = hashCode(address.toLowerCase());
|
|
137
|
+
|
|
138
|
+
const props = Array.from({ length: ELEMENTS }, (_unused, i) => {
|
|
139
|
+
const n = seed * (i + 1);
|
|
140
|
+
return {
|
|
141
|
+
color: pickColor(seed + i, colors),
|
|
142
|
+
translateX: getUnit(n, SIZE / 10, 1),
|
|
143
|
+
translateY: getUnit(n, SIZE / 10, 2),
|
|
144
|
+
scale: 1.2 + getUnit(n, SIZE / 20) / 10,
|
|
145
|
+
rotate: getUnit(n, 360, 1),
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
colors: [props[0].color, props[1].color, props[2].color],
|
|
151
|
+
shapes: [
|
|
152
|
+
{
|
|
153
|
+
translateX: props[1].translateX,
|
|
154
|
+
translateY: props[1].translateY,
|
|
155
|
+
rotate: props[1].rotate,
|
|
156
|
+
scale: props[2].scale,
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
translateX: props[2].translateX,
|
|
160
|
+
translateY: props[2].translateY,
|
|
161
|
+
rotate: props[2].rotate,
|
|
162
|
+
scale: props[2].scale,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Build the SVG `transform` attribute string for a marble shape. Splits
|
|
170
|
+
* the avatar viewport size in half so rotation pivots from the center.
|
|
171
|
+
* @param shape
|
|
172
|
+
* @param size
|
|
173
|
+
*/
|
|
174
|
+
export function marbleShapeTransform(shape: MarbleShape, size = SIZE): string {
|
|
175
|
+
const cx = size / 2;
|
|
176
|
+
const cy = size / 2;
|
|
177
|
+
return `translate(${shape.translateX} ${shape.translateY}) rotate(${shape.rotate} ${cx} ${cy}) scale(${shape.scale})`;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Default avatar viewport size for use in templates. */
|
|
181
|
+
export const MARBLE_AVATAR_SIZE = SIZE;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Unit tests for Address Book type schemas
|
|
3
|
+
* Tests Zod schema validation
|
|
4
|
+
*/
|
|
5
|
+
import { describe, it, expect } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
ContactAddressSchema,
|
|
8
|
+
AddressBookContactSchema,
|
|
9
|
+
AddressBookSchema,
|
|
10
|
+
} from './addressbook.js';
|
|
11
|
+
|
|
12
|
+
describe('ContactAddressSchema', () => {
|
|
13
|
+
it('should accept valid address', () => {
|
|
14
|
+
const address = {
|
|
15
|
+
address: '0.0.12345',
|
|
16
|
+
ledgerId: 'hedera',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const result = ContactAddressSchema.parse(address);
|
|
20
|
+
expect(result.address).toBe('0.0.12345');
|
|
21
|
+
expect(result.ledgerId).toBe('hedera');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should accept address with optional fields', () => {
|
|
25
|
+
const address = {
|
|
26
|
+
address: 'rTestAddress123',
|
|
27
|
+
ledgerId: 'xrpl',
|
|
28
|
+
networkId: 'xrpl:testnet',
|
|
29
|
+
label: 'Main Account',
|
|
30
|
+
isPrimary: true,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const result = ContactAddressSchema.parse(address);
|
|
34
|
+
expect(result.label).toBe('Main Account');
|
|
35
|
+
expect(result.isPrimary).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should reject label exceeding 50 characters', () => {
|
|
39
|
+
const address = {
|
|
40
|
+
address: '0.0.12345',
|
|
41
|
+
ledgerId: 'hedera',
|
|
42
|
+
label: 'A'.repeat(51),
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
expect(() => ContactAddressSchema.parse(address)).toThrow();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('AddressBookContactSchema', () => {
|
|
50
|
+
it('should accept valid contact', () => {
|
|
51
|
+
const contact = {
|
|
52
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
53
|
+
name: 'Alice',
|
|
54
|
+
addresses: [
|
|
55
|
+
{ address: '0.0.12345', ledgerId: 'hedera' },
|
|
56
|
+
],
|
|
57
|
+
createdAt: Date.now(),
|
|
58
|
+
updatedAt: Date.now(),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const result = AddressBookContactSchema.parse(contact);
|
|
62
|
+
expect(result.name).toBe('Alice');
|
|
63
|
+
expect(result.addresses).toHaveLength(1);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should accept contact with multiple addresses', () => {
|
|
67
|
+
const contact = {
|
|
68
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
69
|
+
name: 'Bob',
|
|
70
|
+
addresses: [
|
|
71
|
+
{ address: '0.0.12345', ledgerId: 'hedera', isPrimary: true },
|
|
72
|
+
{ address: 'rBobXrplAddr', ledgerId: 'xrpl' },
|
|
73
|
+
],
|
|
74
|
+
createdAt: Date.now(),
|
|
75
|
+
updatedAt: Date.now(),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const result = AddressBookContactSchema.parse(contact);
|
|
79
|
+
expect(result.addresses).toHaveLength(2);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should accept contact with all optional fields', () => {
|
|
83
|
+
const contact = {
|
|
84
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
85
|
+
name: 'Charlie',
|
|
86
|
+
addresses: [{ address: '0.0.99999', ledgerId: 'hedera' }],
|
|
87
|
+
notes: 'Test contact',
|
|
88
|
+
tags: ['friend', 'work'],
|
|
89
|
+
avatar: 'https://example.com/avatar.png',
|
|
90
|
+
createdAt: Date.now(),
|
|
91
|
+
updatedAt: Date.now(),
|
|
92
|
+
metadata: { customField: 'value' },
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const result = AddressBookContactSchema.parse(contact);
|
|
96
|
+
expect(result.notes).toBe('Test contact');
|
|
97
|
+
expect(result.tags).toContain('friend');
|
|
98
|
+
expect(result.avatar).toBe('https://example.com/avatar.png');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should reject empty name', () => {
|
|
102
|
+
const contact = {
|
|
103
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
104
|
+
name: '',
|
|
105
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
106
|
+
createdAt: Date.now(),
|
|
107
|
+
updatedAt: Date.now(),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should reject name exceeding 100 characters', () => {
|
|
114
|
+
const contact = {
|
|
115
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
116
|
+
name: 'A'.repeat(101),
|
|
117
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
118
|
+
createdAt: Date.now(),
|
|
119
|
+
updatedAt: Date.now(),
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should reject notes exceeding 500 characters', () => {
|
|
126
|
+
const contact = {
|
|
127
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
128
|
+
name: 'Test',
|
|
129
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
130
|
+
notes: 'A'.repeat(501),
|
|
131
|
+
createdAt: Date.now(),
|
|
132
|
+
updatedAt: Date.now(),
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should reject empty addresses array', () => {
|
|
139
|
+
const contact = {
|
|
140
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
141
|
+
name: 'Test',
|
|
142
|
+
addresses: [],
|
|
143
|
+
createdAt: Date.now(),
|
|
144
|
+
updatedAt: Date.now(),
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should reject invalid UUID', () => {
|
|
151
|
+
const contact = {
|
|
152
|
+
id: 'not-a-uuid',
|
|
153
|
+
name: 'Test',
|
|
154
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
155
|
+
createdAt: Date.now(),
|
|
156
|
+
updatedAt: Date.now(),
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should reject invalid avatar URL', () => {
|
|
163
|
+
const contact = {
|
|
164
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
165
|
+
name: 'Test',
|
|
166
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
167
|
+
avatar: 'not-a-url',
|
|
168
|
+
createdAt: Date.now(),
|
|
169
|
+
updatedAt: Date.now(),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
expect(() => AddressBookContactSchema.parse(contact)).toThrow();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('AddressBookSchema', () => {
|
|
177
|
+
it('should accept valid address book', () => {
|
|
178
|
+
const book = {
|
|
179
|
+
version: '2.0' as const,
|
|
180
|
+
contacts: [
|
|
181
|
+
{
|
|
182
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
183
|
+
name: 'Alice',
|
|
184
|
+
addresses: [{ address: '0.0.12345', ledgerId: 'hedera' }],
|
|
185
|
+
createdAt: Date.now(),
|
|
186
|
+
updatedAt: Date.now(),
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
createdAt: Date.now(),
|
|
190
|
+
updatedAt: Date.now(),
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const result = AddressBookSchema.parse(book);
|
|
194
|
+
expect(result.version).toBe('2.0');
|
|
195
|
+
expect(result.contacts).toHaveLength(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should accept empty contacts array', () => {
|
|
199
|
+
const book = {
|
|
200
|
+
version: '2.0' as const,
|
|
201
|
+
contacts: [],
|
|
202
|
+
createdAt: Date.now(),
|
|
203
|
+
updatedAt: Date.now(),
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const result = AddressBookSchema.parse(book);
|
|
207
|
+
expect(result.contacts).toHaveLength(0);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should reject wrong version', () => {
|
|
211
|
+
const book = {
|
|
212
|
+
version: '1.0',
|
|
213
|
+
contacts: [],
|
|
214
|
+
createdAt: Date.now(),
|
|
215
|
+
updatedAt: Date.now(),
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
expect(() => AddressBookSchema.parse(book)).toThrow();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
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 Address book types for contact management across ledgers.
|
|
13
|
+
*
|
|
14
|
+
* This module defines the data structures for the wallet's address book,
|
|
15
|
+
* which stores contacts with multiple blockchain addresses across different
|
|
16
|
+
* ledgers (Hedera, XRPL, etc.).
|
|
17
|
+
*
|
|
18
|
+
* Features:
|
|
19
|
+
* - Multiple addresses per contact (cross-chain support)
|
|
20
|
+
* - Primary address designation per ledger
|
|
21
|
+
* - Tags and notes for organization
|
|
22
|
+
* - Search and filter capabilities
|
|
23
|
+
*
|
|
24
|
+
* @module types/addressbook
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { z } from 'zod';
|
|
28
|
+
import { LedgerIdSchema, NetworkIdSchema } from './ledger.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @description Schema for a single address entry within a contact
|
|
32
|
+
* Represents one blockchain address for a contact (contacts can have multiple addresses)
|
|
33
|
+
*/
|
|
34
|
+
export const ContactAddressSchema = z.object({
|
|
35
|
+
/** Blockchain address */
|
|
36
|
+
address: z.string(),
|
|
37
|
+
/** Ledger/blockchain identifier (hedera, xrpl, etc.) */
|
|
38
|
+
ledgerId: LedgerIdSchema,
|
|
39
|
+
/** Network identifier (mainnet, testnet, etc.) */
|
|
40
|
+
networkId: NetworkIdSchema.optional(),
|
|
41
|
+
/** Optional label for this specific address (e.g., "Main Account", "Savings") */
|
|
42
|
+
label: z.string().max(50).optional(),
|
|
43
|
+
/** Whether this is the primary/default address for this ledger */
|
|
44
|
+
isPrimary: z.boolean().optional(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @description Type representing a contact address
|
|
49
|
+
*/
|
|
50
|
+
export type ContactAddress = z.infer<typeof ContactAddressSchema>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @description Schema for address book contact entry
|
|
54
|
+
* A contact represents a person/entity with one or more blockchain addresses.
|
|
55
|
+
* Supports multiple addresses across different ledgers (Hedera, XRPL, etc.)
|
|
56
|
+
*/
|
|
57
|
+
export const AddressBookContactSchema = z.object({
|
|
58
|
+
/** Unique identifier for the contact */
|
|
59
|
+
id: z.string().uuid(),
|
|
60
|
+
/** Display name for the contact */
|
|
61
|
+
name: z.string().min(1).max(100),
|
|
62
|
+
/** Array of blockchain addresses for this contact */
|
|
63
|
+
addresses: z.array(ContactAddressSchema).min(1),
|
|
64
|
+
/** Optional notes or description */
|
|
65
|
+
notes: z.string().max(500).optional(),
|
|
66
|
+
/** Optional tags for categorization */
|
|
67
|
+
tags: z.array(z.string()).optional(),
|
|
68
|
+
/** Optional avatar/icon URL */
|
|
69
|
+
avatar: z.string().url().optional(),
|
|
70
|
+
/** When the contact was added */
|
|
71
|
+
createdAt: z.number(),
|
|
72
|
+
/** Last time the contact was modified */
|
|
73
|
+
updatedAt: z.number(),
|
|
74
|
+
/** Additional metadata */
|
|
75
|
+
metadata: z.record(z.unknown()).optional(),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @description Type representing an address book contact
|
|
80
|
+
*/
|
|
81
|
+
export type AddressBookContact = z.infer<typeof AddressBookContactSchema>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @description Schema for address book storage
|
|
85
|
+
* The address book contains all saved contacts
|
|
86
|
+
*/
|
|
87
|
+
export const AddressBookSchema = z.object({
|
|
88
|
+
/** Version for schema evolution */
|
|
89
|
+
version: z.literal('2.0'),
|
|
90
|
+
/** All contacts in the address book */
|
|
91
|
+
contacts: z.array(AddressBookContactSchema),
|
|
92
|
+
/** When the address book was created */
|
|
93
|
+
createdAt: z.number(),
|
|
94
|
+
/** Last time unknown contact was modified */
|
|
95
|
+
updatedAt: z.number(),
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @description Type representing the address book
|
|
100
|
+
*/
|
|
101
|
+
export type AddressBook = z.infer<typeof AddressBookSchema>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @description Filter options for searching contacts
|
|
105
|
+
*/
|
|
106
|
+
export interface AddressBookFilter {
|
|
107
|
+
/** Filter by ledger ID */
|
|
108
|
+
ledgerId?: string;
|
|
109
|
+
/** Filter by network ID */
|
|
110
|
+
networkId?: string;
|
|
111
|
+
/** Filter by tag */
|
|
112
|
+
tag?: string;
|
|
113
|
+
/** Search query for name or address */
|
|
114
|
+
query?: string;
|
|
115
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
* @description Result wrapper for async-enriched values.
|
|
13
|
+
*
|
|
14
|
+
* The wallet's approval UI surfaces values that are derived from network
|
|
15
|
+
* sources (Mirror Node token metadata, NFT thumbnails, account labels,
|
|
16
|
+
* fee schedules, etc). These fetches can fail; they can return stale
|
|
17
|
+
* cached data; they can be in-flight when the consumer needs to render.
|
|
18
|
+
*
|
|
19
|
+
* Wrapping every enriched value in a {@link EnrichmentResult} forces every
|
|
20
|
+
* consumer to handle all four states explicitly, instead of getting
|
|
21
|
+
* surprised by undefined data mid-render:
|
|
22
|
+
*
|
|
23
|
+
* - `fresh` — the value was just fetched and is current.
|
|
24
|
+
* - `cached` — the value is from a previous fetch; may be stale.
|
|
25
|
+
* - `missing` — the cache has no entry yet; a fetch is fired but the
|
|
26
|
+
* caller gets a safe-default value to render immediately.
|
|
27
|
+
* - `error` — fetch failed (timeout, 4xx, 5xx). Safe-default value
|
|
28
|
+
* still present so the UI degrades gracefully.
|
|
29
|
+
*
|
|
30
|
+
* The wrapper always carries a `value` — never `undefined`. Pipes and
|
|
31
|
+
* templates can render unconditionally; the `status` drives any optional
|
|
32
|
+
* "verified / pending / error" decoration.
|
|
33
|
+
*
|
|
34
|
+
* @typeParam T - The enriched payload type (e.g., token metadata).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const result = tokenCache.get(tokenId, networkId);
|
|
39
|
+
* switch (result.status) {
|
|
40
|
+
* case 'fresh':
|
|
41
|
+
* case 'cached':
|
|
42
|
+
* return `${result.value.symbol} (${result.value.decimals} decimals)`;
|
|
43
|
+
* case 'missing':
|
|
44
|
+
* return `${tokenId} (loading…)`;
|
|
45
|
+
* case 'error':
|
|
46
|
+
* return `${tokenId} (unverified)`;
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export interface EnrichmentResult<T> {
|
|
51
|
+
/** The enriched value. Always defined; for non-fresh statuses, a safe default. */
|
|
52
|
+
value: T;
|
|
53
|
+
/** Current state of the enrichment for this entry. */
|
|
54
|
+
status: 'fresh' | 'cached' | 'missing' | 'error';
|
|
55
|
+
/** Unix ms when the value was last fetched. Undefined for `missing` entries. */
|
|
56
|
+
fetchedAt?: number;
|
|
57
|
+
/** Optional error message when `status === 'error'`. */
|
|
58
|
+
error?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Factory helpers for {@link EnrichmentResult}. Use these in cache
|
|
63
|
+
* services and tests to keep construction consistent across the
|
|
64
|
+
* codebase — never construct the object literally.
|
|
65
|
+
*/
|
|
66
|
+
export const enrichmentResult = {
|
|
67
|
+
/**
|
|
68
|
+
* Returns a `fresh` result with the current timestamp.
|
|
69
|
+
* @param value
|
|
70
|
+
*/
|
|
71
|
+
fresh<T>(value: T): EnrichmentResult<T> {
|
|
72
|
+
return { value, status: 'fresh', fetchedAt: Date.now() };
|
|
73
|
+
},
|
|
74
|
+
/**
|
|
75
|
+
* Returns a `cached` result; supply the original fetch timestamp.
|
|
76
|
+
* @param value
|
|
77
|
+
* @param fetchedAt
|
|
78
|
+
*/
|
|
79
|
+
cached<T>(value: T, fetchedAt: number): EnrichmentResult<T> {
|
|
80
|
+
return { value, status: 'cached', fetchedAt };
|
|
81
|
+
},
|
|
82
|
+
/**
|
|
83
|
+
* Returns a `missing` result with the caller-supplied safe default.
|
|
84
|
+
* @param safeDefault
|
|
85
|
+
*/
|
|
86
|
+
missing<T>(safeDefault: T): EnrichmentResult<T> {
|
|
87
|
+
return { value: safeDefault, status: 'missing' };
|
|
88
|
+
},
|
|
89
|
+
/**
|
|
90
|
+
* Returns an `error` result with the caller-supplied safe default + message.
|
|
91
|
+
* @param safeDefault
|
|
92
|
+
* @param message
|
|
93
|
+
*/
|
|
94
|
+
error<T>(safeDefault: T, message: string): EnrichmentResult<T> {
|
|
95
|
+
return { value: safeDefault, status: 'error', error: message };
|
|
96
|
+
},
|
|
97
|
+
} as const;
|