@notabene/javascript-sdk 2.0.0-next.9 → 2.0.1

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.
@@ -1,29 +1,78 @@
1
1
  import { ComponentMessage, HostMessage } from '../types';
2
2
 
3
- export type MessageCallback = (message: ComponentMessage) => void;
3
+ /**
4
+ * Callback function for handling component messages.
5
+ *
6
+ * @typeParam T - The type of data contained in the component message
7
+ * @param message - The message object containing the component data and type
8
+ * @public
9
+ */
4
10
 
5
- export class MessageEventManager {
6
- private listeners: Map<string, Set<MessageCallback>> = new Map();
11
+ export type MessageCallback<T> = (message: ComponentMessage<T>) => void;
12
+
13
+ /**
14
+ * Manages message event communication through MessagePorts.
15
+ *
16
+ * This class handles bidirectional communication between components using MessagePorts,
17
+ * allowing subscription to specific message types and dispatching messages to registered callbacks.
18
+ *
19
+ * @typeParam T - The type of data contained in messages received from components
20
+ * @typeParam O - The type of data contained in messages sent from the host
21
+ */
22
+
23
+ export class MessageEventManager<T, O> {
24
+ private listeners: Map<string, Set<MessageCallback<T>>> = new Map();
7
25
  private port?: MessagePort;
8
26
 
9
27
  constructor() {
10
28
  this.handleMessage = this.handleMessage.bind(this);
11
29
  }
12
30
 
31
+ /**
32
+ * Sets up the message port for communication.
33
+ *
34
+ * Initializes the MessagePort for receiving messages by setting up the message handler
35
+ * and starting the port.
36
+ *
37
+ * @param port - The MessagePort instance to use for communication
38
+ */
39
+
13
40
  setPort(port: MessagePort): void {
14
41
  this.port = port;
15
42
  this.port.onmessage = this.handleMessage;
16
43
  this.port.start();
17
44
  }
18
45
 
19
- on(messageType: string, callback: MessageCallback): void {
46
+ /**
47
+ * Registers a callback for a specific message type.
48
+ *
49
+ * When messages of the specified type are received, the callback will be executed
50
+ * with the message data.
51
+ *
52
+ * @param messageType - The type of message to listen for
53
+ * @param callback - The callback function to execute when matching messages are received
54
+
55
+ */
56
+
57
+ on(messageType: string, callback: MessageCallback<T>): () => void {
20
58
  if (!this.listeners.has(messageType)) {
21
59
  this.listeners.set(messageType, new Set());
22
60
  }
23
61
  this.listeners.get(messageType)!.add(callback);
62
+ return () => this.off(messageType, callback);
24
63
  }
25
64
 
26
- off(messageType: string, callback: MessageCallback): void {
65
+ /**
66
+ * Removes a callback for a specific message type.
67
+ *
68
+ * If the callback is the last one registered for the message type,
69
+ * the message type entry will be removed entirely.
70
+ *
71
+ * @param messageType - The type of message to remove listener from
72
+ * @param callback - The callback function to remove
73
+ */
74
+
75
+ off(messageType: string, callback: MessageCallback<T>): void {
27
76
  const callbacks = this.listeners.get(messageType);
28
77
  if (callbacks) {
29
78
  callbacks.delete(callback);
@@ -33,8 +82,17 @@ export class MessageEventManager {
33
82
  }
34
83
  }
35
84
 
36
- private handleMessage(event: MessageEvent): void {
37
- console.log('received message', event.data);
85
+ /**
86
+ * Internal message handler for processing received messages.
87
+ *
88
+ * Validates incoming messages and dispatches them to registered callbacks
89
+ * based on the message type.
90
+ *
91
+ * @param event - The message event containing the component message
92
+ */
93
+
94
+ private handleMessage(event: MessageEvent<ComponentMessage<T>>): void {
95
+ // console.log('received message', event.data);
38
96
  const message = event.data;
39
97
  if (typeof message === 'object' && message !== null && 'type' in message) {
40
98
  const messageType = message.type as string;
@@ -45,8 +103,11 @@ export class MessageEventManager {
45
103
  }
46
104
  }
47
105
 
48
- // Method to send messages through the port
49
- send(message: HostMessage): void {
106
+ /**
107
+ * Sends a message through the message port
108
+ * @param message The host message to send
109
+ */
110
+ send(message: HostMessage<T, O>): void {
50
111
  if (this.port) {
51
112
  this.port.postMessage(message);
52
113
  }
@@ -1,9 +1,14 @@
1
1
  import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { HMType, UpdateValue } from '../../types';
2
+ import {
3
+ HMType,
4
+ type TransactionOptions,
5
+ type UpdateValue,
6
+ type Withdrawal,
7
+ } from '../../types';
3
8
  import { MessageEventManager } from '../MessageEventManager';
4
9
 
5
10
  describe('MessageEventManager', () => {
6
- let messageEventManager: MessageEventManager;
11
+ let messageEventManager: MessageEventManager<Withdrawal, TransactionOptions>;
7
12
  let mockPort: MessagePort;
8
13
 
9
14
  beforeEach(() => {
@@ -13,7 +18,10 @@ describe('MessageEventManager', () => {
13
18
  start: vi.fn(),
14
19
  } as unknown as MessagePort;
15
20
 
16
- messageEventManager = new MessageEventManager();
21
+ messageEventManager = new MessageEventManager<
22
+ Withdrawal,
23
+ TransactionOptions
24
+ >();
17
25
  });
18
26
 
19
27
  describe('setPort', () => {
@@ -73,9 +81,9 @@ describe('MessageEventManager', () => {
73
81
  });
74
82
 
75
83
  it('should send messages through the port', () => {
76
- const message: UpdateValue = {
84
+ const message: UpdateValue<Withdrawal, TransactionOptions> = {
77
85
  type: HMType.UPDATE,
78
- value: { asset: 'USDC' },
86
+ value: { requestId: 'id' },
79
87
  };
80
88
  messageEventManager.send(message);
81
89
 
@@ -91,5 +99,21 @@ describe('MessageEventManager', () => {
91
99
 
92
100
  expect(handler).not.toHaveBeenCalled();
93
101
  });
102
+
103
+ it('on returns unsubscribe function', () => {
104
+ const messageType = 'customEvent';
105
+ const callback = vi.fn();
106
+ const unsubscribe = messageEventManager.on(messageType, callback);
107
+
108
+ // Verify it's a function
109
+ expect(typeof unsubscribe).toBe('function');
110
+
111
+ // Call it and verify the handler is removed
112
+ unsubscribe();
113
+
114
+ // Simulate a message - callback should not be called
115
+ mockPort.onmessage!({ data: { type: messageType } } as MessageEvent);
116
+ expect(callback).not.toHaveBeenCalled();
117
+ });
94
118
  });
95
119
  });
@@ -0,0 +1,112 @@
1
+ import { fc, test } from '@fast-check/vitest';
2
+ import { describe, expect } from 'vitest';
3
+ import { decodeFragmentToObject, encodeObjectToFragment } from '../urls';
4
+
5
+ describe('encodeObjectToFragment', () => {
6
+ test.prop([
7
+ fc.record({
8
+ key1: fc.string(),
9
+ key2: fc.string(),
10
+ key3: fc.string(),
11
+ }),
12
+ ])('Should correctly encode simple string values', (obj) => {
13
+ const encoded = encodeObjectToFragment(obj);
14
+ const parts = encoded.split('&');
15
+
16
+ return Object.entries(obj).every(([key, value]) => {
17
+ const part = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
18
+ return parts.includes(part);
19
+ });
20
+ });
21
+
22
+ test.prop([
23
+ fc.record({
24
+ str: fc.string(),
25
+ num: fc.float(),
26
+ bool: fc.boolean(),
27
+ }),
28
+ ])('Should handle different primitive types', (obj) => {
29
+ const encoded = encodeObjectToFragment(obj);
30
+ const parts = encoded.split('&');
31
+
32
+ return Object.entries(obj).every(([key, value]) => {
33
+ const part = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
34
+ return parts.includes(part);
35
+ });
36
+ });
37
+
38
+ test.prop([
39
+ fc.record({
40
+ obj: fc.object(),
41
+ arr: fc.array(fc.string()),
42
+ nested: fc.record({
43
+ a: fc.string(),
44
+ b: fc.integer(),
45
+ }),
46
+ }),
47
+ ])('Should properly encode nested objects', (obj) => {
48
+ const encoded = encodeObjectToFragment(obj);
49
+ const parts = encoded.split('&');
50
+
51
+ return Object.entries(obj).every(([key, value]) => {
52
+ const part = `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`;
53
+ return parts.includes(part);
54
+ });
55
+ });
56
+
57
+ test('Should skip undefined or null values', () => {
58
+ const obj = {
59
+ valid: 'value',
60
+ empty: undefined,
61
+ null: null,
62
+ falsy: 0,
63
+ };
64
+
65
+ const encoded = encodeObjectToFragment(obj);
66
+ expect(encoded).toBe('valid=value&falsy=0');
67
+ });
68
+
69
+ test('Should handle empty objects', () => {
70
+ const encoded = encodeObjectToFragment({});
71
+ expect(encoded).toBe('');
72
+ });
73
+ });
74
+
75
+ describe('decodeFragmentToObject', () => {
76
+ test.prop([
77
+ fc.record({
78
+ key1: fc.string(),
79
+ key2: fc.string(),
80
+ key3: fc.string(),
81
+ }),
82
+ ])('Should correctly decode URL fragment to object', (obj) => {
83
+ const fragment =
84
+ '#' +
85
+ Object.entries(obj)
86
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
87
+ .join('&');
88
+
89
+ const decoded = decodeFragmentToObject(fragment);
90
+ return Object.entries(obj).every(([k, v]) => decoded[k] === v);
91
+ });
92
+
93
+ test('Should handle empty fragment', () => {
94
+ const decoded = decodeFragmentToObject('#');
95
+ expect(decoded).toEqual({});
96
+ });
97
+
98
+ test('Should handle fragment without values', () => {
99
+ const decoded = decodeFragmentToObject('#key1=&key2=');
100
+ expect(decoded).toEqual({ key1: '', key2: '' });
101
+ });
102
+
103
+ test('Should decode special characters', () => {
104
+ const fragment = '#key1=%20space%20&key2=%3Dequals%3D&key3=%26ampersand%26';
105
+ const decoded = decodeFragmentToObject(fragment);
106
+ expect(decoded).toEqual({
107
+ key1: ' space ',
108
+ key2: '=equals=',
109
+ key3: '&ampersand&',
110
+ });
111
+ });
112
+ });
@@ -1,5 +1,36 @@
1
1
  import fc from 'fast-check';
2
- import type { CAIP10, CAIP19, CAIP2, CAIP220 } from '../types';
2
+ import type {
3
+ Agent,
4
+ BlockchainAddress,
5
+ CAIP10,
6
+ CAIP19,
7
+ CAIP2,
8
+ CAIP220,
9
+ CallbackOptions,
10
+ ConnectionOptions,
11
+ ConnectionRequest,
12
+ Counterparty,
13
+ CryptoCredential,
14
+ DepositRequest,
15
+ DepositRequestOptions,
16
+ DID,
17
+ FieldTypes,
18
+ ISOCurrency,
19
+ ISODate,
20
+ NaturalPerson,
21
+ ThresholdOptions,
22
+ TransactionAsset,
23
+ TransactionOptions,
24
+ TravelAddress,
25
+ VASPOptions,
26
+ Withdrawal,
27
+ } from '../types';
28
+ import {
29
+ AgentType,
30
+ PersonType,
31
+ ProofTypes,
32
+ ValidationSections,
33
+ } from '../types';
3
34
  import {
4
35
  CAIP10_MATCHER,
5
36
  CAIP19_MATCHER,
@@ -8,6 +39,7 @@ import {
8
39
  } from './caip';
9
40
 
10
41
  const LOCALE = new RegExp(/^[a-z]{2}(-[A-Z]{2})?$/);
42
+ const COMPONENT_MATCHER = /^[a-z0-9-_]+$/;
11
43
 
12
44
  // Arbitraries
13
45
  export const arbitraryCAIP10 = (): fc.Arbitrary<CAIP10> =>
@@ -24,3 +56,189 @@ export const arbitraryCAIP220 = (): fc.Arbitrary<CAIP220> =>
24
56
 
25
57
  export const arbitraryLocale = (): fc.Arbitrary<string> =>
26
58
  fc.stringMatching(LOCALE) as fc.Arbitrary<string>;
59
+
60
+ // Arbitraries
61
+ export const arbitraryComponent = (): fc.Arbitrary<string> =>
62
+ fc.stringMatching(COMPONENT_MATCHER);
63
+
64
+ export const abitraryCallbackOptions = (): fc.Arbitrary<CallbackOptions> =>
65
+ fc.record({
66
+ callback: fc.webUrl(),
67
+ redirectUri: fc.webUrl(),
68
+ });
69
+
70
+ export const arbitraryDID = (): fc.Arbitrary<DID> =>
71
+ fc.string().map((s) => `did:ethr:${s}` as DID);
72
+
73
+ export const arbitraryBlockchainAddress = (): fc.Arbitrary<BlockchainAddress> =>
74
+ fc.hexaString({ minLength: 40, maxLength: 40 }).map((s) => `0x${s}`);
75
+
76
+ export const arbitraryTransactionAsset = (): fc.Arbitrary<TransactionAsset> =>
77
+ fc.oneof(
78
+ fc.string(), // NotabeneAsset
79
+ arbitraryCAIP19(), // CAIP19
80
+ fc.string().map((s) => `DTI${s.slice(0, 5)}`), // DTI
81
+ );
82
+
83
+ export const arbitraryAgent = (): fc.Arbitrary<Agent> =>
84
+ fc.record({
85
+ did: arbitraryDID(),
86
+ type: fc.constantFrom(...Object.values(AgentType)),
87
+ logo: fc.option(fc.webUrl()),
88
+ url: fc.option(fc.webUrl()),
89
+ name: fc.option(fc.string()),
90
+ verified: fc.boolean(),
91
+ });
92
+
93
+ export const arbitraryCounterparty = (): fc.Arbitrary<Counterparty> =>
94
+ arbitraryNaturalPerson(); // For simplicity we'll just use NaturalPerson
95
+
96
+ export const arbitraryWithdrawal = (): fc.Arbitrary<Withdrawal> =>
97
+ fc.record({
98
+ agent: arbitraryAgent(),
99
+ counterparty: arbitraryCounterparty(),
100
+ asset: arbitraryTransactionAsset(),
101
+ amountDecimal: fc.float(),
102
+ destination: fc.oneof(arbitraryBlockchainAddress(), arbitraryCAIP10()),
103
+ });
104
+
105
+ export const arbitraryConnectionRequest = (): fc.Arbitrary<ConnectionRequest> =>
106
+ fc.record({
107
+ asset: arbitraryTransactionAsset(),
108
+ requestId: fc.option(fc.uuid()),
109
+ customer: fc.option(arbitraryCounterparty()),
110
+ });
111
+
112
+ // Helper for ISO date format
113
+ export const arbitraryISODate = (): fc.Arbitrary<ISODate> =>
114
+ fc.date().map((d) => {
115
+ const [year, month, day] = d.toISOString().split('T')[0].split('-');
116
+ return `${year}-${month}-${day}` as ISODate;
117
+ });
118
+
119
+ // Helper for ISO currency
120
+ export const arbitraryISOCurrency = (): fc.Arbitrary<ISOCurrency> =>
121
+ fc.constantFrom('USD', 'EUR', 'GBP', 'JPY');
122
+
123
+ export const arbitraryNaturalPerson = (): fc.Arbitrary<NaturalPerson> =>
124
+ fc.record({
125
+ type: fc.constant(PersonType.NATURAL),
126
+ name: fc.string(),
127
+ accountNumber: fc.option(fc.string()),
128
+ did: fc.option(arbitraryDID()),
129
+ verified: fc.option(fc.boolean()),
130
+ website: fc.option(fc.webUrl()),
131
+ phone: fc.option(fc.string()),
132
+ email: fc.option(fc.string()),
133
+ dateOfBirth: fc.option(arbitraryISODate()),
134
+ placeOfBirth: fc.option(fc.string()),
135
+ countryOfResidence: fc.option(fc.string()),
136
+ });
137
+
138
+ export const arbitraryTravelAddress = (): fc.Arbitrary<TravelAddress> =>
139
+ fc.string().map((s) => `ta${s}` as TravelAddress);
140
+
141
+ export const arbitraryCryptoCredential = (): fc.Arbitrary<CryptoCredential> =>
142
+ fc
143
+ .tuple(fc.string(), fc.string())
144
+ .map(([a, b]) => `${a}.${b}.mastercard` as CryptoCredential);
145
+
146
+ export const arbitraryDepositRequest = (): fc.Arbitrary<DepositRequest> =>
147
+ fc.record({
148
+ agent: arbitraryAgent(),
149
+ counterparty: arbitraryCounterparty(),
150
+ asset: arbitraryTransactionAsset(),
151
+ amountDecimal: fc.float(),
152
+ destination: fc.oneof(arbitraryBlockchainAddress(), arbitraryCAIP10()),
153
+ travelAddress: fc.option(arbitraryTravelAddress()),
154
+ cryptoCredential: fc.option(arbitraryCryptoCredential()),
155
+ });
156
+
157
+ // Options arbitraries
158
+ export const arbitraryThresholdOptions = (): fc.Arbitrary<ThresholdOptions> =>
159
+ fc.record({
160
+ threshold: fc.float(),
161
+ currency: arbitraryISOCurrency(),
162
+ proofTypes: fc.option(
163
+ fc.array(fc.constantFrom(...Object.values(ProofTypes))),
164
+ ),
165
+ });
166
+
167
+ export const arbitraryFieldTypes = (): fc.Arbitrary<FieldTypes> =>
168
+ fc.record({
169
+ naturalPerson: fc.option(
170
+ fc.dictionary(
171
+ fc.string(),
172
+ fc.oneof(
173
+ fc.boolean(),
174
+ fc.record({
175
+ optional: fc.boolean(),
176
+ transmit: fc.boolean(),
177
+ }),
178
+ ),
179
+ ),
180
+ ),
181
+ legalPerson: fc.option(
182
+ fc.dictionary(
183
+ fc.string(),
184
+ fc.oneof(
185
+ fc.boolean(),
186
+ fc.record({
187
+ optional: fc.boolean(),
188
+ transmit: fc.boolean(),
189
+ }),
190
+ ),
191
+ ),
192
+ ),
193
+ });
194
+
195
+ export const arbitraryVASPOptions = (): fc.Arbitrary<VASPOptions> =>
196
+ fc.record({
197
+ addUnknown: fc.option(fc.boolean()),
198
+ onlyActive: fc.option(fc.boolean()),
199
+ });
200
+
201
+ export const arbitraryTransactionOptions =
202
+ (): fc.Arbitrary<TransactionOptions> =>
203
+ fc.record({
204
+ proofs: fc.option(
205
+ fc.record({
206
+ microTransfer: fc.option(
207
+ fc.record({
208
+ destination: arbitraryBlockchainAddress(),
209
+ amountSubunits: fc.string(),
210
+ timeout: fc.option(fc.nat()),
211
+ }),
212
+ ),
213
+ fallbacks: fc.option(
214
+ fc.array(fc.constantFrom(...Object.values(ProofTypes))),
215
+ ),
216
+ deminimis: fc.option(arbitraryThresholdOptions()),
217
+ }),
218
+ ),
219
+ allowedAgentTypes: fc.option(
220
+ fc.array(fc.constantFrom(...Object.values(AgentType))),
221
+ ),
222
+ allowedCounterpartyTypes: fc.option(
223
+ fc.array(fc.constantFrom(...Object.values(PersonType))),
224
+ ),
225
+ fields: fc.option(arbitraryFieldTypes()),
226
+ vasps: fc.option(arbitraryVASPOptions()),
227
+ hide: fc.option(
228
+ fc.array(fc.constantFrom(...Object.values(ValidationSections))),
229
+ ),
230
+ });
231
+
232
+ export const arbitraryDepositRequestOptions =
233
+ (): fc.Arbitrary<DepositRequestOptions> =>
234
+ fc.record({
235
+ showQrCode: fc.option(fc.boolean()),
236
+ });
237
+
238
+ export const arbitraryConnectionOptions =
239
+ (): fc.Arbitrary<ConnectionOptions> => {
240
+ const transactionOptions = arbitraryTransactionOptions();
241
+ return fc.record({
242
+ proofs: transactionOptions.map((opt) => opt.proofs),
243
+ });
244
+ };
package/src/utils/urls.ts CHANGED
@@ -1,15 +1,40 @@
1
1
  export function encodeObjectToFragment(obj: any) {
2
2
  return Object.entries(obj)
3
3
  .map(([key, value]) => {
4
- if (!(key && value)) return undefined;
4
+ // Change condition to check for undefined/null specifically
5
+ if (value === undefined || value === null) return undefined;
5
6
  const encodedKey = encodeURIComponent(key);
6
7
  const encodedValue = encodeURIComponent(
7
- typeof value === 'object'
8
- ? JSON.stringify(value)
9
- : (value as string | number | boolean),
8
+ typeof value === 'object' ? JSON.stringify(value) : String(value), // Use String() to handle all primitive types
10
9
  );
11
10
  return `${encodedKey}=${encodedValue}`;
12
11
  })
13
- .filter((entry) => !!entry)
12
+ .filter((entry) => entry !== undefined)
14
13
  .join('&');
15
14
  }
15
+ /**
16
+ * Decodes a URL fragment into an object
17
+ *
18
+ * @param fragment - The URL fragment to decode
19
+ * @returns An object containing the decoded key-value pairs
20
+ * @public
21
+ */
22
+ export function decodeFragmentToObject(
23
+ fragment: string,
24
+ ): Record<string, string> {
25
+ const withoutHash = fragment.slice(1);
26
+ if (!withoutHash) return {}; // Return empty object for empty fragment
27
+
28
+ const pairs = withoutHash.split('&').filter(Boolean); // Filter out empty strings
29
+ return pairs.reduce(
30
+ (obj, pair) => {
31
+ const [key, value] = pair.split('=');
32
+ if (key) {
33
+ // Only add if key exists
34
+ obj[decodeURIComponent(key)] = value ? decodeURIComponent(value) : '';
35
+ }
36
+ return obj;
37
+ },
38
+ {} as Record<string, string>,
39
+ );
40
+ }