@notabene/javascript-sdk 2.8.0-next.3 → 2.8.0-next.5

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 (126) hide show
  1. package/dist/cjs/notabene.cjs +1 -1
  2. package/dist/cjs/notabene.d.ts +58 -1
  3. package/dist/cjs/package.json +1 -1
  4. package/dist/esm/notabene.d.ts +58 -1
  5. package/dist/esm/notabene.js +282 -137
  6. package/dist/esm/package.json +1 -1
  7. package/dist/notabene.d.ts +58 -1
  8. package/dist/notabene.js +282 -137
  9. package/docs/_media/TransactionOptions.md +6 -6
  10. package/docs/ivms/types/enumerations/PayloadVersionCode.md +2 -2
  11. package/docs/ivms/types/interfaces/PayloadMetadata.md +2 -2
  12. package/docs/ivms/types/type-aliases/Address.md +1 -1
  13. package/docs/ivms/types/type-aliases/AddressTypeCode.md +1 -1
  14. package/docs/ivms/types/type-aliases/Beneficiary.md +1 -1
  15. package/docs/ivms/types/type-aliases/BeneficiaryVASP.md +1 -1
  16. package/docs/ivms/types/type-aliases/DateAndPlaceOfBirth.md +1 -1
  17. package/docs/ivms/types/type-aliases/ISOCountryCode.md +1 -1
  18. package/docs/ivms/types/type-aliases/ISODate.md +1 -1
  19. package/docs/ivms/types/type-aliases/IVMS101.md +1 -1
  20. package/docs/ivms/types/type-aliases/IntermediaryVASP.md +1 -1
  21. package/docs/ivms/types/type-aliases/LegalPerson.md +1 -1
  22. package/docs/ivms/types/type-aliases/LegalPersonName.md +1 -1
  23. package/docs/ivms/types/type-aliases/LegalPersonNameID.md +1 -1
  24. package/docs/ivms/types/type-aliases/LegalPersonNameTypeCode.md +1 -1
  25. package/docs/ivms/types/type-aliases/LocalLegalPersonNameID.md +1 -1
  26. package/docs/ivms/types/type-aliases/LocalNaturalPersonNameID.md +1 -1
  27. package/docs/ivms/types/type-aliases/NationalIdentification.md +1 -1
  28. package/docs/ivms/types/type-aliases/NationalIdentifierTypeCode.md +1 -1
  29. package/docs/ivms/types/type-aliases/NaturalPerson.md +1 -1
  30. package/docs/ivms/types/type-aliases/NaturalPersonName.md +1 -1
  31. package/docs/ivms/types/type-aliases/NaturalPersonNameID.md +1 -1
  32. package/docs/ivms/types/type-aliases/NaturalPersonNameTypeCode.md +1 -1
  33. package/docs/ivms/types/type-aliases/OriginatingVASP.md +1 -1
  34. package/docs/ivms/types/type-aliases/Originator.md +1 -1
  35. package/docs/ivms/types/type-aliases/Person.md +1 -1
  36. package/docs/ivms/types/type-aliases/TransferPath.md +1 -1
  37. package/docs/ivms/types/type-aliases/TransliterationMethodCode.md +1 -1
  38. package/docs/notabene/README.md +30 -0
  39. package/docs/notabene/classes/EmbeddedComponent.md +17 -17
  40. package/docs/notabene/classes/default.md +7 -7
  41. package/docs/notabene/functions/decodeFragmentToObject.md +1 -1
  42. package/docs/notabene/interfaces/NotabeneConfig.md +5 -5
  43. package/docs/notabene/type-aliases/MessageCallback.md +1 -1
  44. package/docs/types/README.md +2 -0
  45. package/docs/types/enumerations/AgentType.md +2 -2
  46. package/docs/types/enumerations/CMType.md +7 -7
  47. package/docs/types/enumerations/ErrorIdentifierCode.md +4 -4
  48. package/docs/types/enumerations/HMType.md +2 -2
  49. package/docs/types/enumerations/PersonType.md +3 -3
  50. package/docs/types/enumerations/ProofStatus.md +4 -4
  51. package/docs/types/enumerations/ProofTypes.md +36 -14
  52. package/docs/types/enumerations/Status.md +5 -5
  53. package/docs/types/enumerations/VASPSearchControl.md +2 -2
  54. package/docs/types/enumerations/ValidationSections.md +4 -4
  55. package/docs/types/interfaces/Agent.md +6 -6
  56. package/docs/types/interfaces/CallbackOptions.md +2 -2
  57. package/docs/types/interfaces/ComponentRequest.md +2 -2
  58. package/docs/types/interfaces/ComponentResponse.md +4 -4
  59. package/docs/types/interfaces/ConnectionRecord.md +7 -7
  60. package/docs/types/interfaces/ConnectionRequest.md +3 -3
  61. package/docs/types/interfaces/Counterparty.md +10 -10
  62. package/docs/types/interfaces/DeclarationProof.md +6 -6
  63. package/docs/types/interfaces/Deposit.md +25 -11
  64. package/docs/types/interfaces/DepositRequest.md +7 -7
  65. package/docs/types/interfaces/DepositRequestOptions.md +1 -1
  66. package/docs/types/interfaces/DepositTransaction.md +9 -9
  67. package/docs/types/interfaces/LegalPerson.md +13 -13
  68. package/docs/types/interfaces/MicroTransferProof.md +9 -9
  69. package/docs/types/interfaces/NaturalPerson.md +13 -13
  70. package/docs/types/interfaces/OwnershipProof.md +4 -4
  71. package/docs/types/interfaces/RefreshSource.md +27 -0
  72. package/docs/types/interfaces/Refreshable.md +22 -0
  73. package/docs/types/interfaces/ScreenshotProof.md +5 -5
  74. package/docs/types/interfaces/SignatureProof.md +23 -9
  75. package/docs/types/interfaces/ThresholdOptions.md +3 -3
  76. package/docs/types/interfaces/Transaction.md +8 -8
  77. package/docs/types/interfaces/TransactionOptions.md +6 -6
  78. package/docs/types/interfaces/TransactionResponse.md +9 -9
  79. package/docs/types/interfaces/VASP.md +9 -9
  80. package/docs/types/interfaces/Wallet.md +8 -8
  81. package/docs/types/interfaces/Withdrawal.md +24 -10
  82. package/docs/types/type-aliases/BlockchainAddress.md +1 -1
  83. package/docs/types/type-aliases/CAIP10.md +1 -1
  84. package/docs/types/type-aliases/CAIP19.md +1 -1
  85. package/docs/types/type-aliases/CAIP2.md +1 -1
  86. package/docs/types/type-aliases/CAIP220.md +1 -1
  87. package/docs/types/type-aliases/Cancel.md +1 -1
  88. package/docs/types/type-aliases/Completed.md +1 -1
  89. package/docs/types/type-aliases/ComponentMessage.md +1 -1
  90. package/docs/types/type-aliases/ConnectionOptions.md +1 -1
  91. package/docs/types/type-aliases/CryptoCredential.md +1 -1
  92. package/docs/types/type-aliases/DID.md +1 -1
  93. package/docs/types/type-aliases/DTI.md +1 -1
  94. package/docs/types/type-aliases/Destination.md +1 -1
  95. package/docs/types/type-aliases/Error.md +1 -1
  96. package/docs/types/type-aliases/FieldOptions.md +2 -2
  97. package/docs/types/type-aliases/FieldTypes.md +1 -1
  98. package/docs/types/type-aliases/HostMessage.md +1 -1
  99. package/docs/types/type-aliases/ISOCurrency.md +1 -1
  100. package/docs/types/type-aliases/InvalidValue.md +1 -1
  101. package/docs/types/type-aliases/LEI.md +1 -1
  102. package/docs/types/type-aliases/LegalPersonFieldName.md +1 -1
  103. package/docs/types/type-aliases/LegalPersonFields.md +1 -1
  104. package/docs/types/type-aliases/NaturalPersonFieldName.md +1 -1
  105. package/docs/types/type-aliases/NaturalPersonFields.md +1 -1
  106. package/docs/types/type-aliases/NotabeneAsset.md +1 -1
  107. package/docs/types/type-aliases/Ready.md +1 -1
  108. package/docs/types/type-aliases/ResizeRequest.md +1 -1
  109. package/docs/types/type-aliases/Source.md +1 -1
  110. package/docs/types/type-aliases/Theme.md +1 -1
  111. package/docs/types/type-aliases/TransactionAsset.md +1 -1
  112. package/docs/types/type-aliases/TravelAddress.md +1 -1
  113. package/docs/types/type-aliases/URI.md +1 -1
  114. package/docs/types/type-aliases/UUID.md +1 -1
  115. package/docs/types/type-aliases/UpdateValue.md +1 -1
  116. package/docs/types/type-aliases/V1Asset.md +1 -1
  117. package/docs/types/type-aliases/V1Transaction.md +1 -1
  118. package/docs/types/type-aliases/VASPOptions.md +1 -1
  119. package/docs/types/type-aliases/ValidationError.md +1 -1
  120. package/package.json +1 -1
  121. package/src/notabene.ts +7 -0
  122. package/src/types.ts +1 -0
  123. package/src/utils/__tests__/connections.test.ts +284 -0
  124. package/src/utils/__tests__/encryption.test.ts +79 -0
  125. package/src/utils/connections.ts +174 -0
  126. package/src/utils/encryption.ts +111 -0
@@ -0,0 +1,174 @@
1
+ import type { ComponentRequest, TransactionOptions, UUID } from '../types';
2
+ import { seal, unseal } from './encryption';
3
+
4
+ export type TransactionType = 'withdraw' | 'deposit';
5
+ export interface ConnectionData<T extends ComponentRequest> {
6
+ tx: T;
7
+ authToken?: string;
8
+ txOptions?: TransactionOptions;
9
+ }
10
+
11
+ export interface ConnectionMetadata {
12
+ nodeUrl?: string;
13
+ participants: string[];
14
+ transactionType: TransactionType;
15
+ }
16
+ export interface ConnectionResponse<T extends ComponentRequest> {
17
+ id: UUID;
18
+ version: number;
19
+ metadata: ConnectionMetadata;
20
+ data: ConnectionData<T>;
21
+ key: string;
22
+ }
23
+
24
+ /**
25
+ * Manages encrypted connections using Cloudflare Durable Objects
26
+ */
27
+ export class ConnectionManager {
28
+ private endpoint: string;
29
+
30
+ constructor(endpoint: string) {
31
+ this.endpoint = endpoint;
32
+ }
33
+
34
+ /**
35
+ * Creates a new encrypted connection
36
+ * @param data The component request data to encrypt and store
37
+ * @param participants Array of participant identifiers
38
+ * @returns Promise resolving to connection details including ID, version, and encryption key
39
+ */
40
+ async create<T extends ComponentRequest>(
41
+ data: ConnectionData<T>,
42
+ metadata: ConnectionMetadata,
43
+ ): Promise<ConnectionResponse<T>> {
44
+ // Encrypt the data
45
+ const sealed = await seal(data);
46
+
47
+ // Prepare the request body
48
+ const body = {
49
+ metadata,
50
+ sealed: sealed.ciphertext,
51
+ };
52
+
53
+ // Create the connection
54
+ const response = await fetch(this.endpoint, {
55
+ method: 'POST',
56
+ headers: {
57
+ 'Content-Type': 'application/json',
58
+ },
59
+ body: JSON.stringify(body),
60
+ });
61
+
62
+ if (!response.ok) {
63
+ throw new Error(`Failed to create connection: ${await response.text()}`);
64
+ }
65
+
66
+ const result = await response.json();
67
+
68
+ return {
69
+ id: result.id,
70
+ version: result.version,
71
+ metadata: metadata,
72
+ data: data,
73
+ key: sealed.key,
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Updates an existing connection with new encrypted data
79
+ * @param id Connection ID
80
+ * @param data New data to encrypt and store
81
+ * @param version Current version number
82
+ * @returns Promise resolving to updated connection details including new encryption key
83
+ */
84
+ async update<T extends ComponentRequest>(
85
+ id: UUID,
86
+ data: ConnectionData<T>,
87
+ version: number,
88
+ ): Promise<ConnectionResponse<T>> {
89
+ // Encrypt the new data
90
+ const sealed = await seal(data);
91
+
92
+ // Prepare the request body
93
+ const body = {
94
+ sealed: sealed.ciphertext,
95
+ version,
96
+ };
97
+
98
+ // Update the connection
99
+ const response = await fetch(`${this.endpoint}/${id}`, {
100
+ method: 'PATCH',
101
+ headers: {
102
+ 'Content-Type': 'application/json',
103
+ },
104
+ body: JSON.stringify(body),
105
+ });
106
+
107
+ if (!response.ok) {
108
+ throw new Error(`Failed to update connection: ${await response.text()}`);
109
+ }
110
+
111
+ const result = await response.json();
112
+
113
+ return {
114
+ id: result.id,
115
+ metadata: result.metadata,
116
+ version: result.version,
117
+ data: data,
118
+ key: sealed.key,
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Retrieves and decrypts connection data
124
+ * @param id Connection ID
125
+ * @param key Encryption key from previous create/update operation
126
+ * @returns Promise resolving to connection details including decrypted data
127
+ */
128
+ async get<T extends ComponentRequest>(
129
+ id: UUID,
130
+ key: string,
131
+ ): Promise<ConnectionResponse<T>> {
132
+ // Get the connection data
133
+ const response = await fetch(`${this.endpoint}/${id}`, {
134
+ method: 'GET',
135
+ });
136
+
137
+ if (!response.ok) {
138
+ throw new Error(`Failed to get connection: ${await response.text()}`);
139
+ }
140
+
141
+ const result = await response.json();
142
+
143
+ // Get the latest sealed data
144
+ const latestSealed = result.sealed[result.sealed.length - 1];
145
+
146
+ // Decrypt the data
147
+ const data = await unseal<ConnectionData<T>>({
148
+ ciphertext: latestSealed,
149
+ key,
150
+ });
151
+
152
+ return {
153
+ id: result.id,
154
+ version: result.version,
155
+ metadata: result.metadata,
156
+ data,
157
+ key,
158
+ };
159
+ }
160
+
161
+ /**
162
+ * Closes a connection
163
+ * @param id Connection ID
164
+ */
165
+ async close(id: UUID): Promise<void> {
166
+ const response = await fetch(`${this.endpoint}/${id}`, {
167
+ method: 'DELETE',
168
+ });
169
+
170
+ if (!response.ok) {
171
+ throw new Error(`Failed to close connection: ${await response.text()}`);
172
+ }
173
+ }
174
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Result of a sealing operation, containing ciphertext and encryption key.
3
+ */
4
+ export interface Sealed {
5
+ /** Base64-encoded ciphertext combined with initialization vector (IV) */
6
+ ciphertext: string;
7
+ /** Base64-encoded encryption key */
8
+ key: string;
9
+ }
10
+
11
+ /**
12
+ * Encrypts an object using AES-GCM encryption.
13
+ * @template T Type of data to encrypt
14
+ * @param data Data to encrypt
15
+ * @returns Promise resolving to a Sealed object containing ciphertext and key
16
+ */
17
+ export async function seal<T>(data: T): Promise<Sealed> {
18
+ // Convert the object to a JSON string
19
+ const plaintext = JSON.stringify(data);
20
+ const encoder = new TextEncoder();
21
+
22
+ // Generate a random encryption key and IV
23
+ const rawKey = crypto.getRandomValues(new Uint8Array(32)); // 256-bit key
24
+ const key = await crypto.subtle.importKey(
25
+ 'raw',
26
+ rawKey,
27
+ { name: 'AES-GCM' },
28
+ false,
29
+ ['encrypt'],
30
+ );
31
+
32
+ const iv = crypto.getRandomValues(new Uint8Array(12)); // 96-bit IV
33
+
34
+ // Encrypt the plaintext
35
+ const ciphertextBuffer = await crypto.subtle.encrypt(
36
+ { name: 'AES-GCM', iv: iv },
37
+ key,
38
+ encoder.encode(plaintext),
39
+ );
40
+
41
+ // Combine the IV and ciphertext, separated by `-`
42
+ const ciphertext = `${arrayBufferToBase64(iv)}-${arrayBufferToBase64(ciphertextBuffer)}`;
43
+
44
+ // Return the base64 encoded ciphertext and key
45
+ return {
46
+ ciphertext,
47
+ key: arrayBufferToBase64(rawKey),
48
+ };
49
+ }
50
+
51
+ /**
52
+ * Decrypts previously sealed object.
53
+ * @template T Type of data that was encrypted
54
+ * @param sealed Sealed object containing ciphertext and key
55
+ * @returns Promise resolving to the original decrypted data
56
+ */
57
+
58
+ export async function unseal<T>({ ciphertext, key }: Sealed): Promise<T> {
59
+ // Split the IV and the ciphertext
60
+ const [ivBase64, ciphertextBase64] = ciphertext.split('-');
61
+
62
+ // Decode the key, IV, and ciphertext
63
+ const rawKey = new Uint8Array(
64
+ atob(key)
65
+ .split('')
66
+ .map((c) => c.charCodeAt(0)),
67
+ );
68
+ const iv = base64ToUint8Array(ivBase64);
69
+ const ciphertextBuffer = base64ToUint8Array(ciphertextBase64);
70
+
71
+ // Import the key
72
+ const keyObj = await crypto.subtle.importKey(
73
+ 'raw',
74
+ rawKey,
75
+ { name: 'AES-GCM' },
76
+ false,
77
+ ['decrypt'],
78
+ );
79
+
80
+ // Decrypt the ciphertext
81
+ const decoder = new TextDecoder();
82
+ const plaintextBuffer = await crypto.subtle.decrypt(
83
+ { name: 'AES-GCM', iv: new Uint8Array(iv) },
84
+ keyObj,
85
+ ciphertextBuffer,
86
+ );
87
+
88
+ // Parse and return the original object
89
+ return JSON.parse(decoder.decode(plaintextBuffer));
90
+ }
91
+
92
+ // Utility function to convert ArrayBuffer to Base64
93
+ function arrayBufferToBase64(buffer: ArrayBuffer): string {
94
+ const uint8Array = new Uint8Array(buffer);
95
+ let binary = '';
96
+ for (let i = 0; i < uint8Array.length; i++) {
97
+ binary += String.fromCharCode(uint8Array[i]);
98
+ }
99
+ return btoa(binary);
100
+ }
101
+
102
+ // Utility function to convert Base64 to ArrayBuffer
103
+
104
+ function base64ToUint8Array(base64: string): Uint8Array {
105
+ const binary = atob(base64);
106
+ const buffer = new Uint8Array(binary.length);
107
+ for (let i = 0; i < binary.length; i++) {
108
+ buffer[i] = binary.charCodeAt(i);
109
+ }
110
+ return buffer;
111
+ }