@notabene/javascript-sdk 1.32.0 → 2.0.0-next.10

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/src/notabene.ts CHANGED
@@ -1,308 +1,186 @@
1
- // @ts-ignore
2
- import { dom, node } from 'jsx-pragmatic';
3
- import {
4
- FieldsProps,
5
- NonCustodialDeclarationType,
1
+ import EmbeddedComponent from './components/EmbeddedComponent';
2
+ import type {
3
+ Agent,
4
+ AgentType,
5
+ CallbackOptions,
6
+ ComponentMessage,
7
+ Counterparty,
8
+ DeclarationProof,
9
+ Deposit,
10
+ DepositRequest,
11
+ Destination,
12
+ HostMessage,
13
+ IVMS101,
14
+ MicroTransferProof,
15
+ OwnershipProof,
16
+ PersonType,
17
+ ProofStatus,
18
+ ProofTypes,
19
+ ScreenshotProof,
20
+ SignatureProof,
21
+ Status,
6
22
  Theme,
7
23
  Transaction,
8
- TransactionTypeAllowed,
9
- WidgetType,
10
- Fallback,
11
- OptInFeature,
24
+ TransactionAsset,
25
+ TransactionOptions,
26
+ VASP,
27
+ Wallet,
28
+ Withdrawal,
12
29
  } from './types';
13
- // @ts-ignore
14
- import zoid, { destroy } from 'zoid';
15
- import { popupProps, widgetProps } from './zoidComponentProps';
16
-
17
- interface NotabeneConfig {
18
- widget: string;
19
- container: string;
20
- authToken: string;
21
- vaspDID: string;
22
- onValidStateChange(isValid: boolean): void;
23
- onError(err: any): void;
30
+ import { CMType, TransactionResponse } from './types';
31
+ import { encodeObjectToFragment } from './utils/urls';
32
+
33
+ export type {
34
+ CMType,
35
+ ComponentMessage,
36
+ EmbeddedComponent,
37
+ HostMessage,
38
+ Theme,
39
+ Counterparty,
40
+ Withdrawal,
41
+ Deposit,
42
+ Transaction,
43
+ DepositRequest,
44
+ TransactionResponse,
45
+ Destination as destination,
46
+ TransactionAsset,
47
+ SignatureProof,
48
+ ScreenshotProof,
49
+ MicroTransferProof,
50
+ DeclarationProof,
51
+ Notabene,
52
+ ProofStatus,
53
+ ProofTypes,
54
+ IVMS101,
55
+ PersonType,
56
+ Status,
57
+ VASP,
58
+ Wallet,
59
+ Agent,
60
+ AgentType,
61
+ OwnershipProof,
62
+ TransactionOptions,
63
+ };
64
+
65
+ /**
66
+ * Configuration for the Notabene SDK
67
+ *
68
+ * @public
69
+ */
70
+ export interface NotabeneConfig {
71
+ nodeUrl?: string;
72
+ authToken?: string;
73
+ uxUrl?: string;
24
74
  theme?: Theme;
25
- dictionary?: { [key: string]: string };
26
- transactionTypeAllowed?: TransactionTypeAllowed;
27
- nonCustodialDeclarationType?: NonCustodialDeclarationType;
28
- beneficiaryDetails?: string[]; // @deprecated beneficiaryDetails - do not delete yet
29
- fieldsProps?: FieldsProps;
30
- fallbacks?: Fallback[];
31
- optInFeatures?: OptInFeature[];
75
+ locale?: string;
32
76
  }
33
77
 
78
+ /**
79
+ * Primary constructor for Notabene UX elements
80
+ *
81
+ * @public
82
+ */
83
+
34
84
  export default class Notabene {
35
- private widget: zoid.ZoidComponent;
36
- private popup: zoid.ZoidComponent;
37
- private config: NotabeneConfig;
38
- private tx: Transaction | undefined;
39
- private error?: Error;
40
- private widgetRendered: boolean;
41
- private popupRendered: boolean;
42
- private shouldBeDestroyed: boolean;
85
+ private nodeUrl?: string;
86
+ private authToken?: string;
87
+ private uxUrl: string;
88
+ private theme?: Theme;
89
+ private locale?: string;
43
90
 
44
91
  constructor(config: NotabeneConfig) {
45
- this.config = config;
46
- this.config.nonCustodialDeclarationType =
47
- config.nonCustodialDeclarationType || 'SIGNATURE';
48
- this.config.transactionTypeAllowed = config.transactionTypeAllowed || 'ALL';
49
- this.widgetRendered = false;
50
- this.shouldBeDestroyed = false;
51
- this.popupRendered = false;
52
- this.config.fallbacks = this.config.fallbacks || [
53
- { flow: 'WALLET_NOT_SUPPORTED', action: 'DECLARATION' },
54
- ];
55
- }
56
-
57
- /**
58
- * Renders a new widget
59
- * @param type - the type of widget to render
60
- */
61
- public async renderWidget(
62
- type: WidgetType = WidgetType.WITHDRAWAL
63
- ): Promise<void> {
64
- this._renderWidget(type);
92
+ this.uxUrl = config.uxUrl || 'https://connect.notabene.id';
93
+ this.nodeUrl = config.nodeUrl;
94
+ this.authToken = config.authToken;
95
+ this.theme = config.theme;
96
+ this.locale = config.locale;
65
97
  }
66
98
 
67
- private async _renderWidget(type: WidgetType): Promise<void> {
68
- if (this.widgetRendered) {
69
- console.error(
70
- 'Notabene: Widget already rendered, please destroy it first'
71
- );
72
- this.config.onError(
73
- new Error('Widget already rendered, please destroy it first')
74
- );
75
- return;
76
- }
77
-
78
- this.widget = await this.buildWidget();
79
- this.popup = await this.buildPopup();
80
- this.widget.updateProps({
81
- transaction: this.tx,
82
- type,
83
- });
84
- this.popup.updateProps({
85
- transaction: this.tx,
86
- type,
99
+ public componentUrl(
100
+ path: string,
101
+ value: Partial<Transaction>,
102
+ configuration?: TransactionOptions,
103
+ callbacks?: CallbackOptions,
104
+ ): string {
105
+ const url = new URL(this.uxUrl);
106
+ url.pathname = path;
107
+
108
+ const hash = encodeObjectToFragment({
109
+ authToken: this.authToken,
110
+ value,
111
+ configuration,
87
112
  });
88
- this.popup.hide();
89
- await this.widget.render(this.config.container);
90
- await this.renderPopup();
91
- }
92
-
93
- private async renderPopup(): Promise<void> {
94
- if (!this.popup) return;
113
+ url.hash = hash;
114
+ if (this.nodeUrl) url.searchParams.set('nodeUrl', this.nodeUrl);
95
115
 
96
- if (!this.popupRendered) {
97
- await this.popup.render(this.config.container);
98
- this.popupRendered = true;
116
+ if (this.theme) {
117
+ url.searchParams.set('theme', JSON.stringify(this.theme));
99
118
  }
100
- }
101
-
102
- public async destroyWidget(): Promise<void> {
103
- if (!this.widgetRendered || !this.popupRendered) {
104
- this.shouldBeDestroyed = true;
105
- return;
119
+ if (this.locale) {
120
+ url.searchParams.set('locale', this.locale);
106
121
  }
107
- await destroy();
108
- this.shouldBeDestroyed = false;
109
- this.widgetRendered = false;
110
- this.popupRendered = false;
111
- }
112
-
113
- public setTransaction(tx: Transaction) {
114
- this.tx = tx;
115
- if (this.widgetRendered) {
116
- this.widget.updateProps({
117
- transaction: tx,
118
- });
119
- this.popup.updateProps({
120
- transaction: tx,
121
- });
122
+ if (callbacks) {
123
+ if (callbacks.callback)
124
+ url.searchParams.set('callback_url', callbacks.callback);
125
+ if (callbacks.redirectUri)
126
+ url.searchParams.set('redirect_uri', callbacks.redirectUri);
122
127
  }
128
+ return url.toString();
123
129
  }
124
130
 
125
- private cleanUpDestroyedComponents() {
126
- if (!this.shouldBeDestroyed) return;
127
- this.destroyWidget();
131
+ public createComponent(
132
+ path: string,
133
+ value: Partial<Transaction>,
134
+ options?: TransactionOptions,
135
+ callbacks?: CallbackOptions,
136
+ ) {
137
+ return new EmbeddedComponent(
138
+ this.componentUrl(path, value, options, callbacks),
139
+ value,
140
+ );
128
141
  }
129
142
 
130
- private getWidgetUrl(): string {
131
- const url = new URL(this.config.widget);
132
- url.hash = `authToken=${this.config.authToken}`;
133
- return url.toString();
143
+ public createWithdrawalAssist(
144
+ value: Partial<Withdrawal>,
145
+ options?: TransactionOptions,
146
+ callbacks?: CallbackOptions,
147
+ ) {
148
+ return this.createComponent('withdrawal-assist', value, options, callbacks);
134
149
  }
135
150
 
136
- private getAdditionalInfoUrl(): string {
137
- const url = new URL(this.config.widget);
138
- url.search = 'popup=true';
139
- url.hash = `authToken=${this.config.authToken}`;
140
- return url.toString();
151
+ public createDepositAssist(
152
+ value: Partial<Deposit>,
153
+ options?: any,
154
+ callbacks?: CallbackOptions,
155
+ ) {
156
+ return this.createComponent('deposit-assist', value, options, callbacks);
141
157
  }
142
158
 
143
- private handleError(err: any): void {
144
- this.error = err;
145
- if (this.widget && this.popup) {
146
- this.widget.updateProps({
147
- error: err,
148
- });
149
- this.popup.updateProps({
150
- error: err,
151
- });
152
- }
153
- if (!err || !this.config.onError) return;
154
- this.config.onError(err);
159
+ public createConnect(
160
+ value: Partial<Withdrawal>,
161
+ options?: TransactionOptions,
162
+ callbacks?: CallbackOptions,
163
+ ) {
164
+ return this.createComponent('connect', value, options, callbacks);
155
165
  }
156
166
 
157
- private buildWidget(): zoid.ZoidComponent {
158
- if (!this.config.authToken) {
159
- console.error('Notabene: authToken is required');
160
- return;
161
- }
162
-
163
- const builtWidget = zoid.create({
164
- tag: 'widget',
165
- url: this.getWidgetUrl(),
166
- dimensions: {
167
- width: '100%',
168
- height: '280px',
169
- },
170
- props: widgetProps,
171
- })({
172
- vaspDID: this.config.vaspDID,
173
- transaction: {},
174
- theme: this.config.theme,
175
- dictionary: this.config.dictionary,
176
- transactionTypeAllowed: this.config.transactionTypeAllowed,
177
- nonCustodialDeclarationType: this.config.nonCustodialDeclarationType,
178
- beneficiaryDetails: this.config.beneficiaryDetails,
179
- fieldsProps: this.config.fieldsProps,
180
- fallbacks: this.config.fallbacks,
181
- optInFeatures: this.config.optInFeatures,
182
- onRequireAdditionalInformation: () => {
183
- this.popup.updateProps({
184
- popupOpen: true,
185
- });
186
- this.popup.show();
187
- },
188
- onValidStateChange: (isValid: boolean) => {
189
- this.config.onValidStateChange(isValid);
190
- },
191
- error: this.error,
192
- onError: (err: any) => {
193
- this.handleError(err);
194
- console.debug('Notabene: Error handler called');
195
- },
196
- storeTransaction: (tx: Transaction) => {
197
- this.setTransaction(tx);
198
- },
199
- });
200
-
201
- builtWidget.event.on(zoid.EVENT.RENDERED, () => {
202
- this.widgetRendered = true;
203
- this.cleanUpDestroyedComponents();
204
- });
205
-
206
- return builtWidget;
167
+ public createDepositRequest(
168
+ value: Partial<DepositRequest>,
169
+ options?: TransactionOptions,
170
+ callbacks?: CallbackOptions,
171
+ ) {
172
+ return this.createComponent('deposit-request', value, options, callbacks);
207
173
  }
208
174
 
209
- private buildPopup(): zoid.ZoidComponent {
210
- if (!this.config.authToken) {
211
- console.error('Notabene: authToken is required');
212
- return;
213
- }
214
-
215
- const builtPopup = zoid.create({
216
- tag: 'popup',
217
- url: this.getAdditionalInfoUrl(),
218
- children: () => {
219
- return {
220
- Transaction: this.tx,
221
- };
222
- },
223
- dimensions: {
224
- width: '100%',
225
- height: '100%',
175
+ public decodeFragmentToObject(fragment: string): Record<string, string> {
176
+ const pairs = fragment.slice(1).split('&');
177
+ return pairs.reduce(
178
+ (obj, pair) => {
179
+ const [key, value] = pair.split('=');
180
+ obj[decodeURIComponent(key)] = decodeURIComponent(value);
181
+ return obj;
226
182
  },
227
- props: popupProps,
228
- containerTemplate: ({ uid, doc, frame, prerenderFrame }: any) => {
229
- return node(
230
- 'div',
231
- { id: uid, class: 'nb-container' },
232
- node(
233
- 'style',
234
- null,
235
- `
236
- #${uid}.container {
237
- border: 5px solid red;
238
- }
239
- .nb-overlay {
240
- position: fixed;
241
- width: 100vw;
242
- height: 100vh;
243
- top: 0;
244
- left: 0;
245
- display: flex;
246
- align-items: center;
247
- justify-content: center;
248
- background-color: rgba(0,0,0,0.5);
249
- }
250
- .nb-popup {
251
- width: 100%;
252
- height: 100%;
253
- }
254
- .nb-preloader {
255
- display: none;
256
- }
257
- `
258
- ),
259
- node(
260
- 'div',
261
- { class: 'nb-overlay' },
262
- node('node', { el: frame, class: 'nb-popup' }),
263
- node('node', { el: prerenderFrame, class: 'nb-preloader' })
264
- )
265
- ).render(dom({ doc }));
266
- },
267
- })({
268
- vaspDID: this.config.vaspDID,
269
- transaction: this.tx,
270
- beneficiaryDetails: this.config.beneficiaryDetails,
271
- fieldsProps: this.config.fieldsProps,
272
- storeTransaction: (tx: Transaction) => {
273
- this.setTransaction(tx);
274
- console.debug('Notabene: Transaction stored');
275
- },
276
- close: () => {
277
- this.popup.close();
278
- console.debug('Notabene: Popup closing');
279
- },
280
- setPopupOpen: (isOpen: boolean) => {
281
- this.popup.updateProps({
282
- popupOpen: isOpen,
283
- });
284
- },
285
- error: this.error,
286
- onError: (err: any) => {
287
- this.handleError(err);
288
- console.debug('Notabene: Error handler called');
289
- },
290
- popupOpen: false,
291
- theme: this.config.theme,
292
- dictionary: this.config.dictionary,
293
- transactionTypeAllowed: this.config.transactionTypeAllowed,
294
- nonCustodialDeclarationType: this.config.nonCustodialDeclarationType,
295
- fallbacks: this.config.fallbacks,
296
- optInFeatures: this.config.optInFeatures,
297
- });
298
-
299
- builtPopup.event.on(zoid.EVENT.RENDERED, () => {
300
- this.popupRendered = true;
301
- this.cleanUpDestroyedComponents();
302
- });
303
-
304
- return builtPopup;
183
+ {} as Record<string, string>,
184
+ );
305
185
  }
306
186
  }
307
-
308
- if (window) window.Notabene = Notabene;