@notabene/javascript-sdk 2.0.0-next.17 → 2.0.0-next.18
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 +19 -0
- package/dist/notabene.cjs +1 -1
- package/dist/notabene.d.ts +19 -13
- package/dist/notabene.js +23 -15
- package/package.json +1 -1
- package/src/components/EmbeddedComponent.ts +20 -8
- package/src/components/__tests__/EmbeddedComponent.test.ts +61 -16
- package/src/notabene.ts +11 -10
- package/src/types.ts +5 -2
- package/src/utils/MessageEventManager.ts +2 -2
- package/src/utils/__tests__/MessageEventManager.test.ts +12 -4
package/README.md
CHANGED
|
@@ -320,6 +320,25 @@ const options: TransactionOptions = {
|
|
|
320
320
|
const withdrawal = notabene.createWithdrawalAssist(tx, options);
|
|
321
321
|
```
|
|
322
322
|
|
|
323
|
+
The options can additionally be updated dynamically with the `update()` function.
|
|
324
|
+
|
|
325
|
+
```js
|
|
326
|
+
withdrawal.update({
|
|
327
|
+
asset: 'ETH',
|
|
328
|
+
destination: '0x8d12a197cb00d4747a1fe03395095ce2a5cc6819',
|
|
329
|
+
amountDecimal: 1.12,
|
|
330
|
+
}, {
|
|
331
|
+
proofs: {
|
|
332
|
+
microTransfer: {
|
|
333
|
+
destination: '0x...',
|
|
334
|
+
amountSubunits: '12344',
|
|
335
|
+
timeout: 86440,
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
|
|
323
342
|
### Common use cases
|
|
324
343
|
|
|
325
344
|
#### Only allow first party transactions
|
package/dist/notabene.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var g=Object.defineProperty;var v=(t,e,s)=>e in t?g(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var o=(t,e,s)=>v(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var d=(t=>(t.PRIVATE="WALLET",t.VASP="VASP",t))(d||{}),c=(t=>(t.NATURAL="natural",t.LEGAL="legal",t.SELF="self",t))(c||{}),m=(t=>(t.EMPTY="empty",t.VERIFY="verify",t.PENDING="pending",t.VERIFIED="verified",t.BANNED="banned",t))(m||{}),a=(t=>(t.COMPLETE="complete",t.RESIZE="resize",t.RESULT="result",t.READY="ready",t.INVALID="invalid",t.ERROR="error",t.CANCEL="cancel",t))(a||{}),u=(t=>(t.UPDATE="update",t.REQUEST_RESPONSE="requestResponse",t))(u||{}),p=(t=>(t.PENDING="pending",t.FAILED="rejected",t.FLAGGED="flagged",t.VERIFIED="verified",t))(p||{}),f=(t=>(t.SelfDeclaration="self-declaration",t.PersonalSignEIP191="eip-191",t.SIWE="siwe",t.PersonalSignEIP712="eip-712",t.PersonalSignBIP137="bip-137",t.PersonalSignXPUB="xpub",t.MicroTransfer="microtransfer",t.Screenshot="screenshot",t))(f||{});class
|
|
1
|
+
"use strict";var g=Object.defineProperty;var v=(t,e,s)=>e in t?g(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var o=(t,e,s)=>v(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var d=(t=>(t.PRIVATE="WALLET",t.VASP="VASP",t))(d||{}),c=(t=>(t.NATURAL="natural",t.LEGAL="legal",t.SELF="self",t))(c||{}),m=(t=>(t.EMPTY="empty",t.VERIFY="verify",t.PENDING="pending",t.VERIFIED="verified",t.BANNED="banned",t))(m||{}),a=(t=>(t.COMPLETE="complete",t.RESIZE="resize",t.RESULT="result",t.READY="ready",t.INVALID="invalid",t.ERROR="error",t.CANCEL="cancel",t))(a||{}),u=(t=>(t.UPDATE="update",t.REQUEST_RESPONSE="requestResponse",t))(u||{}),p=(t=>(t.PENDING="pending",t.FAILED="rejected",t.FLAGGED="flagged",t.VERIFIED="verified",t))(p||{}),f=(t=>(t.SelfDeclaration="self-declaration",t.PersonalSignEIP191="eip-191",t.SIWE="siwe",t.PersonalSignEIP712="eip-712",t.PersonalSignBIP137="bip-137",t.PersonalSignXPUB="xpub",t.MicroTransfer="microtransfer",t.Screenshot="screenshot",t))(f||{});class b{constructor(){o(this,"listeners",new Map);o(this,"port");this.handleMessage=this.handleMessage.bind(this)}setPort(e){this.port=e,this.port.onmessage=this.handleMessage,this.port.start()}on(e,s){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(s)}off(e,s){const r=this.listeners.get(e);r&&(r.delete(s),r.size===0&&this.listeners.delete(e))}handleMessage(e){const s=e.data;if(typeof s=="object"&&s!==null&&"type"in s){const r=s.type,i=this.listeners.get(r);i&&i.forEach(n=>n(s))}}send(e){this.port&&this.port.postMessage(e)}}class E{constructor(e,s,r){o(this,"_url");o(this,"_value");o(this,"_options");o(this,"_errors",[]);o(this,"iframe");o(this,"eventManager");o(this,"modal");this._url=e,this._value=s,this._options=r,this.eventManager=new b,this.on(a.INVALID,i=>{i.type===a.INVALID&&(this._errors=i.errors,this._value=i.value)}),this.on(a.RESIZE,i=>{i.type===a.RESIZE&&this.iframe&&(this.iframe.style.height=`${i.height}px`)})}get url(){return this._url}get value(){return this._value}get options(){return this._options}get errors(){return this._errors}open(){document.location.href=this.url}mount(e){const s=document.querySelector(e);if(!s)throw new Error(`parentID ${e} not found`);this.embed(s)}embed(e,s=!1){var r,i;this.removeEmbed(),this.iframe=document.createElement("iframe"),this.iframe.src=this.url+(s?"":"&embedded=true"),this.iframe.allow="web-share",this.iframe.style.width="100%",this.iframe.style.height="100px",this.iframe.style.border="none",this.iframe.style.overflow="hidden",e.appendChild(this.iframe),window.addEventListener("message",n=>{var h,l;n.source===((h=this.iframe)==null?void 0:h.contentWindow)&&((l=this.eventManager)==null||l.setPort(n.ports[0]))}),(i=(r=this.iframe)==null?void 0:r.contentWindow)==null||i.focus()}removeEmbed(){this.iframe&&this.iframe.remove()}send(e){this.eventManager.send(e)}on(e,s){this.eventManager.on(e,s)}off(e,s){this.eventManager.off(e,s)}update(e,s){this._value=e,s&&(this._options=s),this.send({type:u.UPDATE,value:e,options:this._options})}completion(){return new Promise((e,s)=>{this.on(a.COMPLETE,r=>{e(r.response)}),this.on("error",r=>{s(new Error(r.message))})})}openModal(){return this.modal&&this.closeModal(),this.modal=document.createElement("dialog"),this.modal.style.border="none",this.modal.style.backgroundColor="white",this.modal.style.maxWidth="100vw",this.modal.style.maxHeight="100vh",this.modal.style.width="600px",this.modal.style.height="600px",document.body.appendChild(this.modal),this.embed(this.modal,!0),this.on(a.CANCEL,()=>{this.closeModal()}),this.on(a.COMPLETE,()=>{this.closeModal()}),this.modal.showModal(),this.modal.addEventListener("click",()=>{this.closeModal()}),this.completion()}closeModal(){var e;this.modal&&((e=this.modal)==null||e.close(),this.modal.remove(),this.modal=void 0)}}function w(t){return Object.entries(t).map(([e,s])=>{if(!(e&&s))return;const r=encodeURIComponent(e),i=encodeURIComponent(typeof s=="object"?JSON.stringify(s):s);return`${r}=${i}`}).filter(e=>!!e).join("&")}class I{constructor(e){o(this,"nodeUrl");o(this,"authToken");o(this,"uxUrl");o(this,"theme");o(this,"locale");this.uxUrl=e.uxUrl||"https://connect.notabene.id",this.nodeUrl=e.nodeUrl,this.authToken=e.authToken,this.theme=e.theme,this.locale=e.locale}componentUrl(e,s,r,i){const n=new URL(this.uxUrl);n.pathname=e;const h=w({authToken:this.authToken,value:s,configuration:r});return n.hash=h,this.nodeUrl&&n.searchParams.set("nodeUrl",this.nodeUrl),this.theme&&n.searchParams.set("theme",JSON.stringify(this.theme)),this.locale&&n.searchParams.set("locale",this.locale),i&&(i.callback&&n.searchParams.set("callback_url",i.callback),i.redirectUri&&n.searchParams.set("redirect_uri",i.redirectUri)),n.toString()}createComponent(e,s,r,i){return new E(this.componentUrl(e,s,r,i),s,r)}createWithdrawalAssist(e,s,r){return this.createComponent("withdrawal-assist",e,s,r)}createWalletAssist(e,s,r){return this.createComponent("wallet-assist",e,s,r)}createDepositAssist(e,s,r){return this.createComponent("deposit-assist",e,s,r)}createConnectWallet(e,s,r){return this.createComponent("connect",e,s,r)}createDepositRequest(e,s,r){return this.createComponent("deposit-request",e,s,r)}decodeFragmentToObject(e){return e.slice(1).split("&").reduce((r,i)=>{const[n,h]=i.split("=");return r[decodeURIComponent(n)]=decodeURIComponent(h),r},{})}}exports.AgentType=d;exports.CMType=a;exports.EmbeddedComponent=E;exports.PersonType=c;exports.ProofStatus=p;exports.ProofTypes=f;exports.Status=m;exports.default=I;
|
package/dist/notabene.d.ts
CHANGED
|
@@ -293,9 +293,10 @@ declare type DTI = string;
|
|
|
293
293
|
* An embedded Notabene component
|
|
294
294
|
* @public
|
|
295
295
|
*/
|
|
296
|
-
export declare class EmbeddedComponent<V> {
|
|
296
|
+
export declare class EmbeddedComponent<V, O> {
|
|
297
297
|
private _url;
|
|
298
298
|
private _value;
|
|
299
|
+
private _options?;
|
|
299
300
|
private _errors;
|
|
300
301
|
private iframe?;
|
|
301
302
|
private eventManager;
|
|
@@ -305,7 +306,7 @@ export declare class EmbeddedComponent<V> {
|
|
|
305
306
|
* @param url - The URL of the embedded component
|
|
306
307
|
* @param value - The initial transaction value
|
|
307
308
|
*/
|
|
308
|
-
constructor(url: string, value: Partial<V
|
|
309
|
+
constructor(url: string, value: Partial<V>, options?: O);
|
|
309
310
|
/**
|
|
310
311
|
* Gets the URL of the embedded component
|
|
311
312
|
* @returns The URL of the embedded component
|
|
@@ -316,6 +317,7 @@ export declare class EmbeddedComponent<V> {
|
|
|
316
317
|
* @returns The current transaction value
|
|
317
318
|
*/
|
|
318
319
|
get value(): Partial<V>;
|
|
320
|
+
get options(): O | undefined;
|
|
319
321
|
get errors(): ValidationError[];
|
|
320
322
|
/**
|
|
321
323
|
* Opens the component URL in the current window
|
|
@@ -332,11 +334,12 @@ export declare class EmbeddedComponent<V> {
|
|
|
332
334
|
* @param parent - The parent element to embed the component into
|
|
333
335
|
*/
|
|
334
336
|
embed(parent: Element, modal?: boolean): void;
|
|
337
|
+
removeEmbed(): void;
|
|
335
338
|
/**
|
|
336
339
|
* Sends a message to the embedded component
|
|
337
340
|
* @param message - The message to send
|
|
338
341
|
*/
|
|
339
|
-
send(message: HostMessage<V>): void;
|
|
342
|
+
send(message: HostMessage<V, O>): void;
|
|
340
343
|
/**
|
|
341
344
|
* Adds an event listener for a specific message type
|
|
342
345
|
* @param messageType - The type of message to listen for
|
|
@@ -353,7 +356,7 @@ export declare class EmbeddedComponent<V> {
|
|
|
353
356
|
* Updates the transaction value and sends an update message to the component
|
|
354
357
|
* @param value - The new transaction value
|
|
355
358
|
*/
|
|
356
|
-
update(value: Partial<V
|
|
359
|
+
update(value: Partial<V>, options?: O): void;
|
|
357
360
|
/**
|
|
358
361
|
* Waits for the component to complete and returns the transaction response
|
|
359
362
|
* @returns A promise that resolves with the transaction response
|
|
@@ -407,7 +410,7 @@ declare const enum HMType {
|
|
|
407
410
|
* Host Messages
|
|
408
411
|
* @public
|
|
409
412
|
*/
|
|
410
|
-
export declare type HostMessage<T> = UpdateValue<T>;
|
|
413
|
+
export declare type HostMessage<T, O> = UpdateValue<T, O>;
|
|
411
414
|
|
|
412
415
|
/**
|
|
413
416
|
* Intermediary VASP
|
|
@@ -703,7 +706,7 @@ declare class Notabene {
|
|
|
703
706
|
* @returns component URL
|
|
704
707
|
* @internal
|
|
705
708
|
*/
|
|
706
|
-
componentUrl<V>(path: string, value: V, configuration?:
|
|
709
|
+
componentUrl<V, O>(path: string, value: V, configuration?: O, callbacks?: CallbackOptions): string;
|
|
707
710
|
/**
|
|
708
711
|
* Creates a new embedded component
|
|
709
712
|
*
|
|
@@ -714,7 +717,7 @@ declare class Notabene {
|
|
|
714
717
|
* @returns A new EmbeddedComponent instance
|
|
715
718
|
* @internal
|
|
716
719
|
*/
|
|
717
|
-
createComponent<V>(path: string, value: Partial<V>, options?:
|
|
720
|
+
createComponent<V, O>(path: string, value: Partial<V>, options?: O, callbacks?: CallbackOptions): EmbeddedComponent<V, O>;
|
|
718
721
|
/**
|
|
719
722
|
* Creates a withdrawal assist component
|
|
720
723
|
*
|
|
@@ -723,7 +726,7 @@ declare class Notabene {
|
|
|
723
726
|
* @param callbacks - Optional callback configuration
|
|
724
727
|
* @returns A new EmbeddedComponent instance for withdrawal assistance
|
|
725
728
|
*/
|
|
726
|
-
createWithdrawalAssist(value: Partial<Withdrawal>, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<Withdrawal>;
|
|
729
|
+
createWithdrawalAssist(value: Partial<Withdrawal>, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
727
730
|
/**
|
|
728
731
|
* Creates a wallet assist component
|
|
729
732
|
*
|
|
@@ -733,7 +736,7 @@ declare class Notabene {
|
|
|
733
736
|
* @returns A new EmbeddedComponent instance for withdrawal assistance
|
|
734
737
|
* @alpha
|
|
735
738
|
*/
|
|
736
|
-
createWalletAssist(value: Partial<Withdrawal>, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<Withdrawal>;
|
|
739
|
+
createWalletAssist(value: Partial<Withdrawal>, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
737
740
|
/**
|
|
738
741
|
* Creates a deposit assist component
|
|
739
742
|
*
|
|
@@ -743,7 +746,7 @@ declare class Notabene {
|
|
|
743
746
|
* @returns A new EmbeddedComponent instance for deposit assistance
|
|
744
747
|
* @alpha
|
|
745
748
|
*/
|
|
746
|
-
createDepositAssist(value: Partial<Deposit>, options?: any, callbacks?: CallbackOptions): EmbeddedComponent<Deposit>;
|
|
749
|
+
createDepositAssist(value: Partial<Deposit>, options?: any, callbacks?: CallbackOptions): EmbeddedComponent<Deposit, any>;
|
|
747
750
|
/**
|
|
748
751
|
* Creates a connect component
|
|
749
752
|
*
|
|
@@ -753,7 +756,7 @@ declare class Notabene {
|
|
|
753
756
|
* @returns A new EmbeddedComponent instance for connection
|
|
754
757
|
* @alpha
|
|
755
758
|
*/
|
|
756
|
-
createConnectWallet(value: ConnectionRequest, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<ConnectionRequest>;
|
|
759
|
+
createConnectWallet(value: ConnectionRequest, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<ConnectionRequest, TransactionOptions>;
|
|
757
760
|
/**
|
|
758
761
|
* Creates a deposit request component
|
|
759
762
|
*
|
|
@@ -763,7 +766,7 @@ declare class Notabene {
|
|
|
763
766
|
* @returns A new EmbeddedComponent instance for deposit requests
|
|
764
767
|
* @public
|
|
765
768
|
*/
|
|
766
|
-
createDepositRequest(value: DepositRequest, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<DepositRequest>;
|
|
769
|
+
createDepositRequest(value: DepositRequest, options?: TransactionOptions, callbacks?: CallbackOptions): EmbeddedComponent<DepositRequest, TransactionOptions>;
|
|
767
770
|
/**
|
|
768
771
|
* Decodes a URL fragment into an object
|
|
769
772
|
*
|
|
@@ -1069,11 +1072,14 @@ declare type TravelAddress = `ta${string}`;
|
|
|
1069
1072
|
|
|
1070
1073
|
/**
|
|
1071
1074
|
* Represents an update value host message. This is sent from Host to component.
|
|
1075
|
+
* @typeParam T - The overall Value type being sent
|
|
1076
|
+
* @typeParam O - The Option type being sent
|
|
1072
1077
|
* @public
|
|
1073
1078
|
*/
|
|
1074
|
-
declare type UpdateValue<T> = {
|
|
1079
|
+
declare type UpdateValue<T, O> = {
|
|
1075
1080
|
type: HMType.UPDATE;
|
|
1076
1081
|
value: Partial<T>;
|
|
1082
|
+
options?: O;
|
|
1077
1083
|
};
|
|
1078
1084
|
|
|
1079
1085
|
/**
|
package/dist/notabene.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var c = Object.defineProperty;
|
|
2
2
|
var m = (t, e, s) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
|
|
3
3
|
var o = (t, e, s) => m(t, typeof e != "symbol" ? e + "" : e, s);
|
|
4
|
-
var u = /* @__PURE__ */ ((t) => (t.PRIVATE = "WALLET", t.VASP = "VASP", t))(u || {}),
|
|
4
|
+
var u = /* @__PURE__ */ ((t) => (t.PRIVATE = "WALLET", t.VASP = "VASP", t))(u || {}), f = /* @__PURE__ */ ((t) => (t.NATURAL = "natural", t.LEGAL = "legal", t.SELF = "self", t))(f || {}), p = /* @__PURE__ */ ((t) => (t.EMPTY = "empty", t.VERIFY = "verify", t.PENDING = "pending", t.VERIFIED = "verified", t.BANNED = "banned", t))(p || {}), a = /* @__PURE__ */ ((t) => (t.COMPLETE = "complete", t.RESIZE = "resize", t.RESULT = "result", t.READY = "ready", t.INVALID = "invalid", t.ERROR = "error", t.CANCEL = "cancel", t))(a || {}), d = /* @__PURE__ */ ((t) => (t.UPDATE = "update", t.REQUEST_RESPONSE = "requestResponse", t))(d || {}), E = /* @__PURE__ */ ((t) => (t.PENDING = "pending", t.FAILED = "rejected", t.FLAGGED = "flagged", t.VERIFIED = "verified", t))(E || {}), g = /* @__PURE__ */ ((t) => (t.SelfDeclaration = "self-declaration", t.PersonalSignEIP191 = "eip-191", t.SIWE = "siwe", t.PersonalSignEIP712 = "eip-712", t.PersonalSignBIP137 = "bip-137", t.PersonalSignXPUB = "xpub", t.MicroTransfer = "microtransfer", t.Screenshot = "screenshot", t))(g || {});
|
|
5
5
|
class v {
|
|
6
6
|
constructor() {
|
|
7
7
|
o(this, "listeners", /* @__PURE__ */ new Map());
|
|
@@ -36,17 +36,18 @@ class w {
|
|
|
36
36
|
* @param url - The URL of the embedded component
|
|
37
37
|
* @param value - The initial transaction value
|
|
38
38
|
*/
|
|
39
|
-
constructor(e, s) {
|
|
39
|
+
constructor(e, s, r) {
|
|
40
40
|
o(this, "_url");
|
|
41
41
|
o(this, "_value");
|
|
42
|
+
o(this, "_options");
|
|
42
43
|
o(this, "_errors", []);
|
|
43
44
|
o(this, "iframe");
|
|
44
45
|
o(this, "eventManager");
|
|
45
46
|
o(this, "modal");
|
|
46
|
-
this._url = e, this._value = s, this.eventManager = new v(), this.on(a.INVALID, (
|
|
47
|
-
|
|
48
|
-
}), this.on(a.RESIZE, (
|
|
49
|
-
|
|
47
|
+
this._url = e, this._value = s, this._options = r, this.eventManager = new v(), this.on(a.INVALID, (i) => {
|
|
48
|
+
i.type === a.INVALID && (this._errors = i.errors, this._value = i.value);
|
|
49
|
+
}), this.on(a.RESIZE, (i) => {
|
|
50
|
+
i.type === a.RESIZE && this.iframe && (this.iframe.style.height = `${i.height}px`);
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
53
|
/**
|
|
@@ -63,6 +64,9 @@ class w {
|
|
|
63
64
|
get value() {
|
|
64
65
|
return this._value;
|
|
65
66
|
}
|
|
67
|
+
get options() {
|
|
68
|
+
return this._options;
|
|
69
|
+
}
|
|
66
70
|
get errors() {
|
|
67
71
|
return this._errors;
|
|
68
72
|
}
|
|
@@ -88,11 +92,14 @@ class w {
|
|
|
88
92
|
*/
|
|
89
93
|
embed(e, s = !1) {
|
|
90
94
|
var r, i;
|
|
91
|
-
this.iframe = document.createElement("iframe"), this.iframe.src = this.url + (s ? "" : "&embedded=true"), this.iframe.allow = "web-share", this.iframe.style.width = "100%", this.iframe.style.height = "100px", this.iframe.style.border = "none", this.iframe.style.overflow = "hidden", e.appendChild(this.iframe), window.addEventListener("message", (n) => {
|
|
95
|
+
this.removeEmbed(), this.iframe = document.createElement("iframe"), this.iframe.src = this.url + (s ? "" : "&embedded=true"), this.iframe.allow = "web-share", this.iframe.style.width = "100%", this.iframe.style.height = "100px", this.iframe.style.border = "none", this.iframe.style.overflow = "hidden", e.appendChild(this.iframe), window.addEventListener("message", (n) => {
|
|
92
96
|
var h, l;
|
|
93
97
|
n.source === ((h = this.iframe) == null ? void 0 : h.contentWindow) && ((l = this.eventManager) == null || l.setPort(n.ports[0]));
|
|
94
98
|
}), (i = (r = this.iframe) == null ? void 0 : r.contentWindow) == null || i.focus();
|
|
95
99
|
}
|
|
100
|
+
removeEmbed() {
|
|
101
|
+
this.iframe && this.iframe.remove();
|
|
102
|
+
}
|
|
96
103
|
/**
|
|
97
104
|
* Sends a message to the embedded component
|
|
98
105
|
* @param message - The message to send
|
|
@@ -120,8 +127,8 @@ class w {
|
|
|
120
127
|
* Updates the transaction value and sends an update message to the component
|
|
121
128
|
* @param value - The new transaction value
|
|
122
129
|
*/
|
|
123
|
-
update(e) {
|
|
124
|
-
this._value = e, this.send({ type: d.UPDATE, value: e });
|
|
130
|
+
update(e, s) {
|
|
131
|
+
this._value = e, s && (this._options = s), this.send({ type: d.UPDATE, value: e, options: this._options });
|
|
125
132
|
}
|
|
126
133
|
/**
|
|
127
134
|
* Waits for the component to complete and returns the transaction response
|
|
@@ -166,7 +173,7 @@ function I(t) {
|
|
|
166
173
|
return `${r}=${i}`;
|
|
167
174
|
}).filter((e) => !!e).join("&");
|
|
168
175
|
}
|
|
169
|
-
class
|
|
176
|
+
class b {
|
|
170
177
|
/**
|
|
171
178
|
* Creates a new instance of the Notabene SDK
|
|
172
179
|
*
|
|
@@ -213,7 +220,8 @@ class R {
|
|
|
213
220
|
createComponent(e, s, r, i) {
|
|
214
221
|
return new w(
|
|
215
222
|
this.componentUrl(e, s, r, i),
|
|
216
|
-
s
|
|
223
|
+
s,
|
|
224
|
+
r
|
|
217
225
|
);
|
|
218
226
|
}
|
|
219
227
|
/**
|
|
@@ -320,9 +328,9 @@ export {
|
|
|
320
328
|
u as AgentType,
|
|
321
329
|
a as CMType,
|
|
322
330
|
w as EmbeddedComponent,
|
|
323
|
-
|
|
324
|
-
|
|
331
|
+
f as PersonType,
|
|
332
|
+
E as ProofStatus,
|
|
325
333
|
g as ProofTypes,
|
|
326
|
-
|
|
327
|
-
|
|
334
|
+
p as Status,
|
|
335
|
+
b as default
|
|
328
336
|
};
|
package/package.json
CHANGED
|
@@ -15,13 +15,14 @@ import {
|
|
|
15
15
|
* @public
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
export default class EmbeddedComponent<V> {
|
|
18
|
+
export default class EmbeddedComponent<V, O> {
|
|
19
19
|
private _url: string;
|
|
20
20
|
private _value: Partial<V>;
|
|
21
|
+
private _options?: O;
|
|
21
22
|
private _errors: ValidationError[] = [];
|
|
22
23
|
|
|
23
24
|
private iframe?: HTMLIFrameElement;
|
|
24
|
-
private eventManager: MessageEventManager<V>;
|
|
25
|
+
private eventManager: MessageEventManager<V, O>;
|
|
25
26
|
private modal?: HTMLDialogElement;
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -30,10 +31,11 @@ export default class EmbeddedComponent<V> {
|
|
|
30
31
|
* @param value - The initial transaction value
|
|
31
32
|
*/
|
|
32
33
|
|
|
33
|
-
constructor(url: string, value: Partial<V
|
|
34
|
+
constructor(url: string, value: Partial<V>, options?: O) {
|
|
34
35
|
this._url = url;
|
|
35
36
|
this._value = value;
|
|
36
|
-
this.
|
|
37
|
+
this._options = options;
|
|
38
|
+
this.eventManager = new MessageEventManager<V, O>();
|
|
37
39
|
this.on(CMType.INVALID, (message) => {
|
|
38
40
|
if (message.type === CMType.INVALID) {
|
|
39
41
|
this._errors = message.errors;
|
|
@@ -63,6 +65,10 @@ export default class EmbeddedComponent<V> {
|
|
|
63
65
|
return this._value;
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
public get options(): O | undefined {
|
|
69
|
+
return this._options;
|
|
70
|
+
}
|
|
71
|
+
|
|
66
72
|
public get errors(): ValidationError[] {
|
|
67
73
|
return this._errors;
|
|
68
74
|
}
|
|
@@ -86,13 +92,14 @@ export default class EmbeddedComponent<V> {
|
|
|
86
92
|
if (!parent) throw new Error(`parentID ${parentId} not found`);
|
|
87
93
|
this.embed(parent);
|
|
88
94
|
}
|
|
95
|
+
|
|
89
96
|
/**
|
|
90
97
|
* Embeds the component into a parent element
|
|
91
98
|
* @param parent - The parent element to embed the component into
|
|
92
99
|
*/
|
|
93
|
-
|
|
94
100
|
embed(parent: Element, modal: boolean = false) {
|
|
95
101
|
// console.log(this.url);
|
|
102
|
+
this.removeEmbed();
|
|
96
103
|
this.iframe = document.createElement('iframe');
|
|
97
104
|
this.iframe.src = this.url + (!modal ? '&embedded=true' : '');
|
|
98
105
|
this.iframe.allow = 'web-share';
|
|
@@ -115,11 +122,15 @@ export default class EmbeddedComponent<V> {
|
|
|
115
122
|
this.iframe?.contentWindow?.focus();
|
|
116
123
|
}
|
|
117
124
|
|
|
125
|
+
removeEmbed() {
|
|
126
|
+
if (this.iframe) this.iframe.remove();
|
|
127
|
+
}
|
|
128
|
+
|
|
118
129
|
/**
|
|
119
130
|
* Sends a message to the embedded component
|
|
120
131
|
* @param message - The message to send
|
|
121
132
|
*/
|
|
122
|
-
send(message: HostMessage<V>): void {
|
|
133
|
+
send(message: HostMessage<V, O>): void {
|
|
123
134
|
this.eventManager.send(message);
|
|
124
135
|
}
|
|
125
136
|
|
|
@@ -147,9 +158,10 @@ export default class EmbeddedComponent<V> {
|
|
|
147
158
|
* Updates the transaction value and sends an update message to the component
|
|
148
159
|
* @param value - The new transaction value
|
|
149
160
|
*/
|
|
150
|
-
update(value: Partial<V
|
|
161
|
+
update(value: Partial<V>, options?: O) {
|
|
151
162
|
this._value = value;
|
|
152
|
-
|
|
163
|
+
if (options) this._options = options;
|
|
164
|
+
this.send({ type: HMType.UPDATE, value, options: this._options });
|
|
153
165
|
}
|
|
154
166
|
|
|
155
167
|
/**
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
2
2
|
import {
|
|
3
|
+
AgentType,
|
|
3
4
|
CMType,
|
|
4
5
|
HMType,
|
|
5
|
-
HostMessage,
|
|
6
|
-
Transaction,
|
|
7
|
-
|
|
6
|
+
type HostMessage,
|
|
7
|
+
type Transaction,
|
|
8
|
+
type TransactionOptions,
|
|
9
|
+
type Withdrawal,
|
|
8
10
|
} from '../../types';
|
|
9
11
|
import EmbeddedComponent from '../EmbeddedComponent';
|
|
10
12
|
|
|
@@ -14,9 +16,9 @@ const value: Partial<Transaction> = { amountDecimal: 100 };
|
|
|
14
16
|
|
|
15
17
|
describe('EmbeddedComponent', () => {
|
|
16
18
|
describe('creation', () => {
|
|
17
|
-
let component: EmbeddedComponent<Withdrawal>;
|
|
19
|
+
let component: EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
18
20
|
beforeEach(() => {
|
|
19
|
-
component = new EmbeddedComponent<Withdrawal>(url, {
|
|
21
|
+
component = new EmbeddedComponent<Withdrawal, TransactionOptions>(url, {
|
|
20
22
|
amountDecimal: 100,
|
|
21
23
|
});
|
|
22
24
|
});
|
|
@@ -68,14 +70,20 @@ describe('EmbeddedComponent', () => {
|
|
|
68
70
|
});
|
|
69
71
|
|
|
70
72
|
test('mount', () => {
|
|
71
|
-
const component = new EmbeddedComponent<Withdrawal>(
|
|
73
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
74
|
+
url,
|
|
75
|
+
value,
|
|
76
|
+
);
|
|
72
77
|
component.mount(parentId);
|
|
73
78
|
expect(iframe?.src).toBe(url + '&embedded=true');
|
|
74
79
|
expect(parentElement.appendChild).toHaveBeenCalledWith(iframe);
|
|
75
80
|
});
|
|
76
81
|
|
|
77
82
|
test('on', () => {
|
|
78
|
-
const component = new EmbeddedComponent<Withdrawal>(
|
|
83
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
84
|
+
url,
|
|
85
|
+
value,
|
|
86
|
+
);
|
|
79
87
|
const messageType = 'customEvent';
|
|
80
88
|
const callback = vi.fn();
|
|
81
89
|
component.on(messageType, callback);
|
|
@@ -86,7 +94,10 @@ describe('EmbeddedComponent', () => {
|
|
|
86
94
|
});
|
|
87
95
|
|
|
88
96
|
test('off', () => {
|
|
89
|
-
const component = new EmbeddedComponent<Withdrawal>(
|
|
97
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
98
|
+
url,
|
|
99
|
+
value,
|
|
100
|
+
);
|
|
90
101
|
const messageType = 'customEvent';
|
|
91
102
|
const callback = vi.fn();
|
|
92
103
|
component.on(messageType, callback);
|
|
@@ -104,7 +115,8 @@ describe('EmbeddedComponent', () => {
|
|
|
104
115
|
let messageHandler: (event: MessageEvent) => void;
|
|
105
116
|
let port: MessagePort;
|
|
106
117
|
let handshake: MessageEvent;
|
|
107
|
-
let component: EmbeddedComponent<Withdrawal>;
|
|
118
|
+
let component: EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
119
|
+
let options: TransactionOptions;
|
|
108
120
|
|
|
109
121
|
beforeEach(() => {
|
|
110
122
|
port = {
|
|
@@ -121,7 +133,12 @@ describe('EmbeddedComponent', () => {
|
|
|
121
133
|
.mockImplementation((event, handler) => {
|
|
122
134
|
messageHandler = handler;
|
|
123
135
|
});
|
|
124
|
-
|
|
136
|
+
options = { allowedAgentTypes: [AgentType.PRIVATE, AgentType.VASP] };
|
|
137
|
+
component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
138
|
+
url,
|
|
139
|
+
value,
|
|
140
|
+
options,
|
|
141
|
+
);
|
|
125
142
|
component.mount(parentId);
|
|
126
143
|
|
|
127
144
|
expect(messageHandler).toBeDefined();
|
|
@@ -129,23 +146,51 @@ describe('EmbeddedComponent', () => {
|
|
|
129
146
|
});
|
|
130
147
|
|
|
131
148
|
test('send', () => {
|
|
132
|
-
const message: HostMessage<Withdrawal> = {
|
|
149
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
133
150
|
type: HMType.UPDATE,
|
|
134
151
|
value: {
|
|
135
152
|
requestId: 'id',
|
|
136
153
|
},
|
|
154
|
+
options: { allowedAgentTypes: [AgentType.PRIVATE] },
|
|
137
155
|
};
|
|
138
156
|
component.send(message);
|
|
139
157
|
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
140
158
|
});
|
|
141
159
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
160
|
+
describe('update', () => {
|
|
161
|
+
test('with out options', () => {
|
|
162
|
+
const newValue: Partial<Transaction> = { amountDecimal: 200 };
|
|
145
163
|
|
|
146
|
-
|
|
147
|
-
|
|
164
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
165
|
+
type: HMType.UPDATE,
|
|
166
|
+
value: newValue,
|
|
167
|
+
options,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
component.update(newValue);
|
|
171
|
+
|
|
172
|
+
expect(component.value).toBe(newValue);
|
|
173
|
+
expect(component.options).toBe(options);
|
|
174
|
+
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('with options', () => {
|
|
178
|
+
const newValue: Partial<Transaction> = { amountDecimal: 200 };
|
|
179
|
+
const newOptions = { allowedAgentTypes: [AgentType.PRIVATE] };
|
|
148
180
|
|
|
181
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
182
|
+
type: HMType.UPDATE,
|
|
183
|
+
value: newValue,
|
|
184
|
+
options: newOptions,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
component.update(newValue, newOptions);
|
|
188
|
+
|
|
189
|
+
expect(component.value).toBe(newValue);
|
|
190
|
+
expect(component.options).toBe(newOptions);
|
|
191
|
+
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
149
194
|
// TODO not sure this actually does what I want it to do
|
|
150
195
|
test('completion', async () => {
|
|
151
196
|
const response = { valid: true, value: 'completed' };
|
package/src/notabene.ts
CHANGED
|
@@ -139,10 +139,10 @@ export default class Notabene {
|
|
|
139
139
|
* @returns component URL
|
|
140
140
|
* @internal
|
|
141
141
|
*/
|
|
142
|
-
componentUrl<V>(
|
|
142
|
+
componentUrl<V, O>(
|
|
143
143
|
path: string,
|
|
144
144
|
value: V,
|
|
145
|
-
configuration?:
|
|
145
|
+
configuration?: O,
|
|
146
146
|
callbacks?: CallbackOptions,
|
|
147
147
|
): string {
|
|
148
148
|
const url = new URL(this.uxUrl);
|
|
@@ -181,15 +181,16 @@ export default class Notabene {
|
|
|
181
181
|
* @returns A new EmbeddedComponent instance
|
|
182
182
|
* @internal
|
|
183
183
|
*/
|
|
184
|
-
createComponent<V>(
|
|
184
|
+
createComponent<V, O>(
|
|
185
185
|
path: string,
|
|
186
186
|
value: Partial<V>,
|
|
187
|
-
options?:
|
|
187
|
+
options?: O,
|
|
188
188
|
callbacks?: CallbackOptions,
|
|
189
189
|
) {
|
|
190
|
-
return new EmbeddedComponent<V>(
|
|
190
|
+
return new EmbeddedComponent<V, O>(
|
|
191
191
|
this.componentUrl(path, value, options, callbacks),
|
|
192
192
|
value,
|
|
193
|
+
options,
|
|
193
194
|
);
|
|
194
195
|
}
|
|
195
196
|
|
|
@@ -206,7 +207,7 @@ export default class Notabene {
|
|
|
206
207
|
options?: TransactionOptions,
|
|
207
208
|
callbacks?: CallbackOptions,
|
|
208
209
|
) {
|
|
209
|
-
return this.createComponent<Withdrawal>(
|
|
210
|
+
return this.createComponent<Withdrawal, TransactionOptions>(
|
|
210
211
|
'withdrawal-assist',
|
|
211
212
|
value,
|
|
212
213
|
options,
|
|
@@ -228,7 +229,7 @@ export default class Notabene {
|
|
|
228
229
|
options?: TransactionOptions,
|
|
229
230
|
callbacks?: CallbackOptions,
|
|
230
231
|
) {
|
|
231
|
-
return this.createComponent<Withdrawal>(
|
|
232
|
+
return this.createComponent<Withdrawal, TransactionOptions>(
|
|
232
233
|
'wallet-assist',
|
|
233
234
|
value,
|
|
234
235
|
options,
|
|
@@ -250,7 +251,7 @@ export default class Notabene {
|
|
|
250
251
|
options?: any,
|
|
251
252
|
callbacks?: CallbackOptions,
|
|
252
253
|
) {
|
|
253
|
-
return this.createComponent<Deposit>(
|
|
254
|
+
return this.createComponent<Deposit, any>(
|
|
254
255
|
'deposit-assist',
|
|
255
256
|
value,
|
|
256
257
|
options,
|
|
@@ -272,7 +273,7 @@ export default class Notabene {
|
|
|
272
273
|
options?: TransactionOptions,
|
|
273
274
|
callbacks?: CallbackOptions,
|
|
274
275
|
) {
|
|
275
|
-
return this.createComponent<ConnectionRequest>(
|
|
276
|
+
return this.createComponent<ConnectionRequest, TransactionOptions>(
|
|
276
277
|
'connect',
|
|
277
278
|
value,
|
|
278
279
|
options,
|
|
@@ -294,7 +295,7 @@ export default class Notabene {
|
|
|
294
295
|
options?: TransactionOptions,
|
|
295
296
|
callbacks?: CallbackOptions,
|
|
296
297
|
) {
|
|
297
|
-
return this.createComponent<DepositRequest>(
|
|
298
|
+
return this.createComponent<DepositRequest, TransactionOptions>(
|
|
298
299
|
'deposit-request',
|
|
299
300
|
value,
|
|
300
301
|
options,
|
package/src/types.ts
CHANGED
|
@@ -545,18 +545,21 @@ export const enum HMType {
|
|
|
545
545
|
|
|
546
546
|
/**
|
|
547
547
|
* Represents an update value host message. This is sent from Host to component.
|
|
548
|
+
* @typeParam T - The overall Value type being sent
|
|
549
|
+
* @typeParam O - The Option type being sent
|
|
548
550
|
* @public
|
|
549
551
|
*/
|
|
550
|
-
export type UpdateValue<T> = {
|
|
552
|
+
export type UpdateValue<T, O> = {
|
|
551
553
|
type: HMType.UPDATE;
|
|
552
554
|
value: Partial<T>;
|
|
555
|
+
options?: O;
|
|
553
556
|
};
|
|
554
557
|
|
|
555
558
|
/**
|
|
556
559
|
* Host Messages
|
|
557
560
|
* @public
|
|
558
561
|
*/
|
|
559
|
-
export type HostMessage<T> = UpdateValue<T>;
|
|
562
|
+
export type HostMessage<T, O> = UpdateValue<T, O>;
|
|
560
563
|
|
|
561
564
|
/**
|
|
562
565
|
* Options for callback and redirect URIs
|
|
@@ -2,7 +2,7 @@ import { ComponentMessage, HostMessage } from '../types';
|
|
|
2
2
|
|
|
3
3
|
export type MessageCallback<T> = (message: ComponentMessage<T>) => void;
|
|
4
4
|
|
|
5
|
-
export class MessageEventManager<T> {
|
|
5
|
+
export class MessageEventManager<T, O> {
|
|
6
6
|
private listeners: Map<string, Set<MessageCallback<T>>> = new Map();
|
|
7
7
|
private port?: MessagePort;
|
|
8
8
|
|
|
@@ -46,7 +46,7 @@ export class MessageEventManager<T> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// Method to send messages through the port
|
|
49
|
-
send(message: HostMessage<T>): void {
|
|
49
|
+
send(message: HostMessage<T, O>): void {
|
|
50
50
|
if (this.port) {
|
|
51
51
|
this.port.postMessage(message);
|
|
52
52
|
}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import {
|
|
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<Withdrawal>;
|
|
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,7 +81,7 @@ describe('MessageEventManager', () => {
|
|
|
73
81
|
});
|
|
74
82
|
|
|
75
83
|
it('should send messages through the port', () => {
|
|
76
|
-
const message: UpdateValue<Withdrawal> = {
|
|
84
|
+
const message: UpdateValue<Withdrawal, TransactionOptions> = {
|
|
77
85
|
type: HMType.UPDATE,
|
|
78
86
|
value: { requestId: 'id' },
|
|
79
87
|
};
|