@frak-labs/core-sdk 0.1.0-beta.afa252b0 → 0.1.0-beta.dd44738a
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/cdn/bundle.js +3 -3
- package/dist/bundle.cjs +1 -1
- package/dist/bundle.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +4 -2
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.ts +71 -0
- package/src/clients/transports/iframeLifecycleManager.ts +88 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +103 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.ts +94 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +70 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.ts +152 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.ts +142 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.ts +26 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplayModalParamsType,
|
|
3
|
+
FinalActionType,
|
|
4
|
+
FinalModalStepType,
|
|
5
|
+
FrakClient,
|
|
6
|
+
LoginModalStepType,
|
|
7
|
+
ModalRpcMetadata,
|
|
8
|
+
ModalRpcStepsResultType,
|
|
9
|
+
ModalStepTypes,
|
|
10
|
+
OpenInteractionSessionModalStepType,
|
|
11
|
+
SendTransactionModalStepType,
|
|
12
|
+
} from "../../types";
|
|
13
|
+
import { displayModal } from "../displayModal";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represent the type of the modal step builder
|
|
17
|
+
*/
|
|
18
|
+
export type ModalStepBuilder<
|
|
19
|
+
Steps extends ModalStepTypes[] = ModalStepTypes[],
|
|
20
|
+
> = {
|
|
21
|
+
/**
|
|
22
|
+
* The current modal params
|
|
23
|
+
*/
|
|
24
|
+
params: DisplayModalParamsType<Steps>;
|
|
25
|
+
/**
|
|
26
|
+
* Add a send transaction step to the modal
|
|
27
|
+
*/
|
|
28
|
+
sendTx: (
|
|
29
|
+
options: SendTransactionModalStepType["params"]
|
|
30
|
+
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
|
|
31
|
+
/**
|
|
32
|
+
* Add a final step of type reward to the modal
|
|
33
|
+
*/
|
|
34
|
+
reward: (
|
|
35
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
36
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
37
|
+
/**
|
|
38
|
+
* Add a final step of type sharing to the modal
|
|
39
|
+
*/
|
|
40
|
+
sharing: (
|
|
41
|
+
sharingOptions?: Extract<
|
|
42
|
+
FinalActionType,
|
|
43
|
+
{ key: "sharing" }
|
|
44
|
+
>["options"],
|
|
45
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
46
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
47
|
+
/**
|
|
48
|
+
* Display the modal
|
|
49
|
+
* @param metadataOverride - Function returning optional metadata to override the current modal metadata
|
|
50
|
+
*/
|
|
51
|
+
display: (
|
|
52
|
+
metadataOverride?: (
|
|
53
|
+
current?: ModalRpcMetadata
|
|
54
|
+
) => ModalRpcMetadata | undefined
|
|
55
|
+
) => Promise<ModalRpcStepsResultType<Steps>>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Represent the output type of the modal builder
|
|
60
|
+
*/
|
|
61
|
+
export type ModalBuilder = ModalStepBuilder<
|
|
62
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
63
|
+
>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Helper to craft Frak modal, and share a base initial config
|
|
67
|
+
* @param client - The current Frak Client
|
|
68
|
+
* @param args
|
|
69
|
+
* @param args.metadata - Common modal metadata (customisation, language etc)
|
|
70
|
+
* @param args.login - Login step parameters
|
|
71
|
+
* @param args.openSession - Open session step parameters
|
|
72
|
+
*
|
|
73
|
+
* @description This function will create a modal builder with the provided metadata, login and open session parameters.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Here is an example of how to use the `modalBuilder` to create and display a sharing modal:
|
|
77
|
+
*
|
|
78
|
+
* ```js
|
|
79
|
+
* // Create the modal builder
|
|
80
|
+
* const modalBuilder = window.FrakSDK.modalBuilder(frakClient, baseModalConfig);
|
|
81
|
+
*
|
|
82
|
+
* // Configure the information to be shared via the sharing link
|
|
83
|
+
* const sharingConfig = {
|
|
84
|
+
* popupTitle: "Share this with your friends",
|
|
85
|
+
* text: "Discover our product!",
|
|
86
|
+
* link: window.location.href,
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* // Display the sharing modal
|
|
90
|
+
* function modalShare() {
|
|
91
|
+
* modalBuilder.sharing(sharingConfig).display();
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @see {@link ModalStepTypes} for more info about each modal step types and their parameters
|
|
96
|
+
* @see {@link ModalRpcMetadata} for more info about the metadata that can be passed to the modal
|
|
97
|
+
* @see {@link ModalRpcStepsResultType} for more info about the result of each modal steps
|
|
98
|
+
* @see {@link displayModal} for more info about how the modal is displayed
|
|
99
|
+
*/
|
|
100
|
+
export function modalBuilder(
|
|
101
|
+
client: FrakClient,
|
|
102
|
+
{
|
|
103
|
+
metadata,
|
|
104
|
+
login,
|
|
105
|
+
openSession,
|
|
106
|
+
}: {
|
|
107
|
+
metadata?: ModalRpcMetadata;
|
|
108
|
+
login?: LoginModalStepType["params"];
|
|
109
|
+
openSession?: OpenInteractionSessionModalStepType["params"];
|
|
110
|
+
}
|
|
111
|
+
): ModalBuilder {
|
|
112
|
+
// Build the initial modal params
|
|
113
|
+
const baseParams: DisplayModalParamsType<
|
|
114
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
115
|
+
> = {
|
|
116
|
+
steps: {
|
|
117
|
+
login: login ?? {},
|
|
118
|
+
openSession: openSession ?? {},
|
|
119
|
+
},
|
|
120
|
+
metadata,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Return the step builder
|
|
124
|
+
return modalStepsBuilder(client, baseParams);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Modal step builder, allowing to add new steps to the modal, and to build and display it
|
|
129
|
+
*/
|
|
130
|
+
function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
|
|
131
|
+
client: FrakClient,
|
|
132
|
+
params: DisplayModalParamsType<CurrentSteps>
|
|
133
|
+
): ModalStepBuilder<CurrentSteps> {
|
|
134
|
+
// Function add the send tx step
|
|
135
|
+
function sendTx(options: SendTransactionModalStepType["params"]) {
|
|
136
|
+
return modalStepsBuilder<
|
|
137
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
138
|
+
>(client, {
|
|
139
|
+
...params,
|
|
140
|
+
steps: {
|
|
141
|
+
...params.steps,
|
|
142
|
+
sendTransaction: options,
|
|
143
|
+
},
|
|
144
|
+
} as DisplayModalParamsType<
|
|
145
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
146
|
+
>);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Function to add a reward step at the end
|
|
150
|
+
function reward(options?: Omit<FinalModalStepType["params"], "action">) {
|
|
151
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
152
|
+
client,
|
|
153
|
+
{
|
|
154
|
+
...params,
|
|
155
|
+
steps: {
|
|
156
|
+
...params.steps,
|
|
157
|
+
final: {
|
|
158
|
+
...options,
|
|
159
|
+
action: { key: "reward" },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Function to add sharing step at the end
|
|
167
|
+
function sharing(
|
|
168
|
+
sharingOptions?: Extract<
|
|
169
|
+
FinalActionType,
|
|
170
|
+
{ key: "sharing" }
|
|
171
|
+
>["options"],
|
|
172
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
173
|
+
) {
|
|
174
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
175
|
+
client,
|
|
176
|
+
{
|
|
177
|
+
...params,
|
|
178
|
+
steps: {
|
|
179
|
+
...params.steps,
|
|
180
|
+
final: {
|
|
181
|
+
...options,
|
|
182
|
+
action: { key: "sharing", options: sharingOptions },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Function to display it
|
|
190
|
+
async function display(
|
|
191
|
+
metadataOverride?: (
|
|
192
|
+
current?: ModalRpcMetadata
|
|
193
|
+
) => ModalRpcMetadata | undefined
|
|
194
|
+
) {
|
|
195
|
+
// If we have a metadata override, apply it
|
|
196
|
+
if (metadataOverride) {
|
|
197
|
+
params.metadata = metadataOverride(params.metadata ?? {});
|
|
198
|
+
}
|
|
199
|
+
return await displayModal(client, params);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
// Access current modal params
|
|
204
|
+
params,
|
|
205
|
+
// Function to add new steps
|
|
206
|
+
sendTx,
|
|
207
|
+
reward,
|
|
208
|
+
sharing,
|
|
209
|
+
// Display the modal
|
|
210
|
+
display,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FrakClient,
|
|
3
|
+
ModalRpcMetadata,
|
|
4
|
+
SendTransactionModalStepType,
|
|
5
|
+
SendTransactionReturnType,
|
|
6
|
+
} from "../../types";
|
|
7
|
+
import { displayModal } from "../displayModal";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parameters to directly show a modal used to send a transaction
|
|
11
|
+
* @inline
|
|
12
|
+
*/
|
|
13
|
+
export type SendTransactionParams = {
|
|
14
|
+
/**
|
|
15
|
+
* The transaction to be sent (either a single tx or multiple ones)
|
|
16
|
+
*/
|
|
17
|
+
tx: SendTransactionModalStepType["params"]["tx"];
|
|
18
|
+
/**
|
|
19
|
+
* Custom metadata to be passed to the modal
|
|
20
|
+
*/
|
|
21
|
+
metadata?: ModalRpcMetadata;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Function used to send a user transaction, simple wrapper around the displayModal function to ease the send transaction process
|
|
26
|
+
* @param client - The current Frak Client
|
|
27
|
+
* @param args - The parameters
|
|
28
|
+
* @returns The hash of the transaction that was sent in a promise
|
|
29
|
+
*
|
|
30
|
+
* @description This function will display a modal to the user with the provided transaction and metadata.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const { hash } = await sendTransaction(frakConfig, {
|
|
34
|
+
* tx: {
|
|
35
|
+
* to: "0xdeadbeef",
|
|
36
|
+
* value: toHex(100n),
|
|
37
|
+
* },
|
|
38
|
+
* metadata: {
|
|
39
|
+
* header: {
|
|
40
|
+
* title: "Sending eth",
|
|
41
|
+
* },
|
|
42
|
+
* context: "Send 100wei to 0xdeadbeef",
|
|
43
|
+
* },
|
|
44
|
+
* });
|
|
45
|
+
* console.log("Transaction hash:", hash);
|
|
46
|
+
*/
|
|
47
|
+
export async function sendTransaction(
|
|
48
|
+
client: FrakClient,
|
|
49
|
+
{ tx, metadata }: SendTransactionParams
|
|
50
|
+
): Promise<SendTransactionReturnType> {
|
|
51
|
+
// Trigger a modal with login options
|
|
52
|
+
const result = await displayModal(client, {
|
|
53
|
+
metadata,
|
|
54
|
+
steps: {
|
|
55
|
+
login: {},
|
|
56
|
+
sendTransaction: { tx },
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Return the tx result only
|
|
61
|
+
return result.sendTransaction;
|
|
62
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { generateSiweNonce } from "viem/siwe";
|
|
2
|
+
import type {
|
|
3
|
+
FrakClient,
|
|
4
|
+
ModalRpcMetadata,
|
|
5
|
+
SiweAuthenticateReturnType,
|
|
6
|
+
SiweAuthenticationParams,
|
|
7
|
+
} from "../../types";
|
|
8
|
+
import { displayModal } from "../displayModal";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parameter used to directly show a modal used to authenticate with SIWE
|
|
12
|
+
* @inline
|
|
13
|
+
*/
|
|
14
|
+
export type SiweAuthenticateModalParams = {
|
|
15
|
+
/**
|
|
16
|
+
* Partial SIWE params, since we can rebuild them from the SDK if they are empty
|
|
17
|
+
*
|
|
18
|
+
* If no parameters provider, some fields will be recomputed from the current configuration and environment.
|
|
19
|
+
* - `statement` will be set to a default value
|
|
20
|
+
* - `nonce` will be generated
|
|
21
|
+
* - `uri` will be set to the current domain
|
|
22
|
+
* - `version` will be set to "1"
|
|
23
|
+
* - `domain` will be set to the current window domain
|
|
24
|
+
*
|
|
25
|
+
* @default {}
|
|
26
|
+
*/
|
|
27
|
+
siwe?: Partial<SiweAuthenticationParams>;
|
|
28
|
+
/**
|
|
29
|
+
* Custom metadata to be passed to the modal
|
|
30
|
+
*/
|
|
31
|
+
metadata?: ModalRpcMetadata;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Function used to launch a siwe authentication
|
|
36
|
+
* @param client - The current Frak Client
|
|
37
|
+
* @param args - The parameters
|
|
38
|
+
* @returns The SIWE authentication result (message + signature) in a promise
|
|
39
|
+
*
|
|
40
|
+
* @description This function will display a modal to the user with the provided SIWE parameters and metadata.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* import { siweAuthenticate } from "@frak-labs/core-sdk/actions";
|
|
44
|
+
* import { parseSiweMessage } from "viem/siwe";
|
|
45
|
+
*
|
|
46
|
+
* const { signature, message } = await siweAuthenticate(frakConfig, {
|
|
47
|
+
* siwe: {
|
|
48
|
+
* statement: "Sign in to My App",
|
|
49
|
+
* domain: "my-app.com",
|
|
50
|
+
* expirationTimeTimestamp: Date.now() + 1000 * 60 * 5,
|
|
51
|
+
* },
|
|
52
|
+
* metadata: {
|
|
53
|
+
* header: {
|
|
54
|
+
* title: "Sign in",
|
|
55
|
+
* },
|
|
56
|
+
* context: "Sign in to My App",
|
|
57
|
+
* },
|
|
58
|
+
* });
|
|
59
|
+
* console.log("Parsed final message:", parseSiweMessage(message));
|
|
60
|
+
* console.log("Siwe signature:", signature);
|
|
61
|
+
*/
|
|
62
|
+
export async function siweAuthenticate(
|
|
63
|
+
client: FrakClient,
|
|
64
|
+
{ siwe, metadata }: SiweAuthenticateModalParams
|
|
65
|
+
): Promise<SiweAuthenticateReturnType> {
|
|
66
|
+
const effectiveDomain = client.config?.domain ?? window.location.host;
|
|
67
|
+
const realStatement =
|
|
68
|
+
siwe?.statement ??
|
|
69
|
+
`I confirm that I want to use my Frak wallet on: ${client.config.metadata.name}`;
|
|
70
|
+
|
|
71
|
+
// Fill up the siwe request params
|
|
72
|
+
const builtSiwe: SiweAuthenticationParams = {
|
|
73
|
+
...siwe,
|
|
74
|
+
statement: realStatement,
|
|
75
|
+
nonce: siwe?.nonce ?? generateSiweNonce(),
|
|
76
|
+
uri: siwe?.uri ?? `https://${effectiveDomain}`,
|
|
77
|
+
version: siwe?.version ?? "1",
|
|
78
|
+
domain: effectiveDomain,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Trigger a modal with login options
|
|
82
|
+
const result = await displayModal(client, {
|
|
83
|
+
metadata,
|
|
84
|
+
steps: {
|
|
85
|
+
login: {},
|
|
86
|
+
siweAuthenticate: {
|
|
87
|
+
siwe: builtSiwe,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// Return the SIWE result only
|
|
93
|
+
return result.siweAuthenticate;
|
|
94
|
+
}
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FrakRpcError,
|
|
3
|
+
type RpcMessage,
|
|
4
|
+
type RpcResponse,
|
|
5
|
+
} from "@frak-labs/frame-connector";
|
|
6
|
+
import type { FrakWalletSdkConfig } from "../types";
|
|
7
|
+
|
|
8
|
+
type IframeStatus = {
|
|
9
|
+
loading: boolean;
|
|
10
|
+
url: string | null;
|
|
11
|
+
readyState: number;
|
|
12
|
+
contentWindow: boolean;
|
|
13
|
+
isConnected: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type DebugInfo = {
|
|
17
|
+
timestamp: string;
|
|
18
|
+
encodedUrl: string;
|
|
19
|
+
navigatorInfo: string;
|
|
20
|
+
encodedConfig: string;
|
|
21
|
+
iframeStatus: string;
|
|
22
|
+
lastRequest: string;
|
|
23
|
+
lastResponse: string;
|
|
24
|
+
clientStatus: string;
|
|
25
|
+
error: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type NavigatorInfo = {
|
|
29
|
+
userAgent: string;
|
|
30
|
+
language: string;
|
|
31
|
+
onLine: boolean;
|
|
32
|
+
screenWidth: number;
|
|
33
|
+
screenHeight: number;
|
|
34
|
+
pixelRatio: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** @ignore */
|
|
38
|
+
export class DebugInfoGatherer {
|
|
39
|
+
private config?: FrakWalletSdkConfig;
|
|
40
|
+
private iframe?: HTMLIFrameElement;
|
|
41
|
+
private isSetupDone = false;
|
|
42
|
+
private lastResponse: null | {
|
|
43
|
+
message: RpcMessage;
|
|
44
|
+
response: RpcResponse;
|
|
45
|
+
timestamp: number;
|
|
46
|
+
} = null;
|
|
47
|
+
private lastRequest: null | {
|
|
48
|
+
event: RpcMessage;
|
|
49
|
+
timestamp: number;
|
|
50
|
+
} = null;
|
|
51
|
+
|
|
52
|
+
constructor(config?: FrakWalletSdkConfig, iframe?: HTMLIFrameElement) {
|
|
53
|
+
this.config = config;
|
|
54
|
+
this.iframe = iframe;
|
|
55
|
+
this.lastRequest = null;
|
|
56
|
+
this.lastResponse = null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Update communication logs
|
|
60
|
+
public setLastResponse(message: RpcMessage, response: RpcResponse) {
|
|
61
|
+
this.lastResponse = {
|
|
62
|
+
message,
|
|
63
|
+
response,
|
|
64
|
+
timestamp: Date.now(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
public setLastRequest(event: RpcMessage) {
|
|
68
|
+
this.lastRequest = { event, timestamp: Date.now() };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Update connection status
|
|
72
|
+
public updateSetupStatus(status: boolean) {
|
|
73
|
+
this.isSetupDone = status;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private base64Encode(data: object): string {
|
|
77
|
+
try {
|
|
78
|
+
return btoa(JSON.stringify(data));
|
|
79
|
+
} catch (err) {
|
|
80
|
+
console.warn("Failed to encode debug data", err);
|
|
81
|
+
return btoa("Failed to encode data");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Extract information from the iframe status
|
|
87
|
+
*/
|
|
88
|
+
private getIframeStatus(): IframeStatus | null {
|
|
89
|
+
if (!this.iframe) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
loading: this.iframe.hasAttribute("loading"),
|
|
94
|
+
url: this.iframe.src,
|
|
95
|
+
readyState: this.iframe.contentDocument?.readyState
|
|
96
|
+
? this.iframe.contentDocument.readyState === "complete"
|
|
97
|
+
? 1
|
|
98
|
+
: 0
|
|
99
|
+
: -1,
|
|
100
|
+
contentWindow: !!this.iframe.contentWindow,
|
|
101
|
+
isConnected: this.iframe.isConnected,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private getNavigatorInfo(): NavigatorInfo | null {
|
|
106
|
+
if (!navigator) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
userAgent: navigator.userAgent,
|
|
111
|
+
language: navigator.language,
|
|
112
|
+
onLine: navigator.onLine,
|
|
113
|
+
screenWidth: window.screen.width,
|
|
114
|
+
screenHeight: window.screen.height,
|
|
115
|
+
pixelRatio: window.devicePixelRatio,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private gatherDebugInfo(error: Error | unknown): DebugInfo {
|
|
120
|
+
const iframeStatus = this.getIframeStatus();
|
|
121
|
+
const navigatorInfo = this.getNavigatorInfo();
|
|
122
|
+
|
|
123
|
+
// Format the error in a readable format
|
|
124
|
+
let formattedError = "Unknown";
|
|
125
|
+
if (error instanceof FrakRpcError) {
|
|
126
|
+
formattedError = `FrakRpcError: ${error.code} '${error.message}'`;
|
|
127
|
+
} else if (error instanceof Error) {
|
|
128
|
+
formattedError = error.message;
|
|
129
|
+
} else if (typeof error === "string") {
|
|
130
|
+
formattedError = error;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Craft the debug info
|
|
134
|
+
const debugInfo: DebugInfo = {
|
|
135
|
+
timestamp: new Date().toISOString(),
|
|
136
|
+
encodedUrl: btoa(window.location.href),
|
|
137
|
+
encodedConfig: this.config
|
|
138
|
+
? this.base64Encode(this.config)
|
|
139
|
+
: "no-config",
|
|
140
|
+
navigatorInfo: navigatorInfo
|
|
141
|
+
? this.base64Encode(navigatorInfo)
|
|
142
|
+
: "no-navigator",
|
|
143
|
+
iframeStatus: iframeStatus
|
|
144
|
+
? this.base64Encode(iframeStatus)
|
|
145
|
+
: "not-iframe",
|
|
146
|
+
lastRequest: this.lastRequest
|
|
147
|
+
? this.base64Encode(this.lastRequest)
|
|
148
|
+
: "No Frak request logged",
|
|
149
|
+
lastResponse: this.lastResponse
|
|
150
|
+
? this.base64Encode(this.lastResponse)
|
|
151
|
+
: "No Frak response logged",
|
|
152
|
+
clientStatus: this.isSetupDone ? "setup" : "not-setup",
|
|
153
|
+
error: formattedError,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
return debugInfo;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public static empty(): DebugInfoGatherer {
|
|
160
|
+
return new DebugInfoGatherer();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Format Frak debug information
|
|
165
|
+
*/
|
|
166
|
+
public formatDebugInfo(error: Error | unknown | string): string {
|
|
167
|
+
const debugInfo = this.gatherDebugInfo(error);
|
|
168
|
+
return `
|
|
169
|
+
Debug Information:
|
|
170
|
+
-----------------
|
|
171
|
+
Timestamp: ${debugInfo.timestamp}
|
|
172
|
+
URL: ${debugInfo.encodedUrl}
|
|
173
|
+
Config: ${debugInfo.encodedConfig}
|
|
174
|
+
Navigator Info: ${debugInfo.navigatorInfo}
|
|
175
|
+
IFrame Status: ${debugInfo.iframeStatus}
|
|
176
|
+
Last Request: ${debugInfo.lastRequest}
|
|
177
|
+
Last Response: ${debugInfo.lastResponse}
|
|
178
|
+
Client Status: ${debugInfo.clientStatus}
|
|
179
|
+
Error: ${debugInfo.error}
|
|
180
|
+
`.trim();
|
|
181
|
+
}
|
|
182
|
+
}
|