@tomo-inc/embedded-wallet-providers 0.0.3 → 0.0.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.
- package/README.md +47 -24
- package/dist/index.cjs +352 -0
- package/dist/index.d.cts +82 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.js +350 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
# embedded-wallet-providers
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
4
6
|
pnpm add @tomo-inc/embedded-wallet-providers
|
|
5
7
|
```
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
---
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## 1. Embedded Wallet
|
|
12
|
+
|
|
13
|
+
### 1.1 Init
|
|
10
14
|
|
|
11
15
|
```typescript
|
|
12
16
|
const config = {
|
|
@@ -17,22 +21,25 @@ const config = {
|
|
|
17
21
|
xClientId: "******",
|
|
18
22
|
googleClientId: "******",
|
|
19
23
|
|
|
20
|
-
walletBaseUrl: "https://******", //
|
|
24
|
+
walletBaseUrl: "https://******", // embedded-wallet link
|
|
21
25
|
};
|
|
22
26
|
|
|
23
27
|
embeddedWallet = EmbeddedWallet.getInstance();
|
|
28
|
+
|
|
24
29
|
const { isAvailable, message, connectedInfo } =
|
|
25
30
|
await embeddedWallet.init(config);
|
|
26
31
|
|
|
27
32
|
if ((isAvailable = false)) {
|
|
28
|
-
//
|
|
29
|
-
//wallet login
|
|
33
|
+
// oidc auth
|
|
34
|
+
// wallet login
|
|
30
35
|
}
|
|
31
36
|
|
|
32
|
-
embeddedWallet.setWalletZIndex(999); //default
|
|
37
|
+
embeddedWallet.setWalletZIndex(999); // default
|
|
33
38
|
```
|
|
34
39
|
|
|
35
|
-
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### 1.2 OIDC Auth
|
|
36
43
|
|
|
37
44
|
```typescript
|
|
38
45
|
const oidcToken = await embeddedWallet.loginByGoogle();
|
|
@@ -40,12 +47,15 @@ const oidcToken = await embeddedWallet.loginByX();
|
|
|
40
47
|
const oidcToken = await embeddedWallet.loginByEmail({ email });
|
|
41
48
|
```
|
|
42
49
|
|
|
43
|
-
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
### 1.3 Wallet Login
|
|
44
53
|
|
|
45
|
-
|
|
54
|
+
Same response as `init`:
|
|
46
55
|
|
|
47
56
|
```typescript
|
|
48
57
|
const { isAvailable, connectedInfo } = await embeddedWallet.login(oidcToken);
|
|
58
|
+
|
|
49
59
|
connectInfo = {
|
|
50
60
|
evmProvider: {
|
|
51
61
|
connected: true;
|
|
@@ -54,10 +64,12 @@ connectInfo = {
|
|
|
54
64
|
solanaProvider: {
|
|
55
65
|
connected: false;
|
|
56
66
|
}
|
|
57
|
-
}
|
|
67
|
+
};
|
|
58
68
|
```
|
|
59
69
|
|
|
60
|
-
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 2. Wallet Brand Info
|
|
61
73
|
|
|
62
74
|
```typescript
|
|
63
75
|
embeddedWallet.info = {
|
|
@@ -72,23 +84,28 @@ embeddedWallet.info = {
|
|
|
72
84
|
};
|
|
73
85
|
```
|
|
74
86
|
|
|
75
|
-
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## 3. Wallet API
|
|
76
90
|
|
|
77
91
|
```typescript
|
|
78
|
-
embeddedWallet.open("setting"); //mfa config
|
|
92
|
+
embeddedWallet.open("setting"); // mfa config
|
|
79
93
|
embeddedWallet.open("mnemonicExport");
|
|
80
94
|
embeddedWallet.logout();
|
|
81
95
|
```
|
|
82
96
|
|
|
83
|
-
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 4. Providers
|
|
84
100
|
|
|
85
|
-
> isAvailable = true
|
|
101
|
+
> `isAvailable = true`
|
|
86
102
|
|
|
87
|
-
### 4.1
|
|
103
|
+
### 4.1 EVM
|
|
88
104
|
|
|
89
105
|
```typescript
|
|
90
106
|
const evmProvider = embeddedWallet.evmProvider;
|
|
91
107
|
|
|
108
|
+
// methods
|
|
92
109
|
connect;
|
|
93
110
|
disconnect;
|
|
94
111
|
getChainId;
|
|
@@ -99,24 +116,30 @@ eth_signTransaction;
|
|
|
99
116
|
sendTransaction;
|
|
100
117
|
```
|
|
101
118
|
|
|
102
|
-
|
|
119
|
+
API docs: https://qsg07xytt12z.sg.larksuite.com/wiki/XLq9wrNRLiAXANkCj4QlERCxg7f
|
|
103
120
|
|
|
104
|
-
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
### 4.2 Solana (todo)
|
|
105
124
|
|
|
106
125
|
```typescript
|
|
107
126
|
const solanaProvider = embeddedWallet.solanaProvider;
|
|
108
127
|
```
|
|
109
128
|
|
|
110
|
-
|
|
129
|
+
API docs: https://qsg07xytt12z.sg.larksuite.com/wiki/LyVnwBkzaibRDEkUfXclabRmg6g
|
|
130
|
+
|
|
131
|
+
---
|
|
111
132
|
|
|
112
|
-
### 4.3
|
|
133
|
+
### 4.3 Dogecoin (todo)
|
|
113
134
|
|
|
114
135
|
```typescript
|
|
115
136
|
const dogecoinProvider = embeddedWallet.dogecoinProvider;
|
|
116
137
|
```
|
|
117
138
|
|
|
118
|
-
|
|
139
|
+
API docs: https://qsg07xytt12z.sg.larksuite.com/wiki/FKuOwiSN7iNHHNkA4zjl4S5JgLb
|
|
140
|
+
|
|
141
|
+
---
|
|
119
142
|
|
|
120
|
-
## 5.
|
|
143
|
+
## 5. Demo
|
|
121
144
|
|
|
122
|
-
https://github.com/tomo-inc/tomo-wallet/tree/tomo-sdk-design/examples/wallet-sdk-demo/src/embedded-wallet-providers
|
|
145
|
+
Demo repo: https://github.com/tomo-inc/tomo-wallet/tree/tomo-sdk-design/examples/wallet-sdk-demo/src/embedded-wallet-providers
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var injectProviders = require('@tomo-inc/inject-providers');
|
|
4
|
+
var walletUtils = require('@tomo-inc/wallet-utils');
|
|
5
|
+
var oidcAuth = require('@tomo-inc/oidc-auth');
|
|
6
|
+
|
|
7
|
+
// src/embedded-wallet.ts
|
|
8
|
+
var evmDappPopups = {
|
|
9
|
+
connect: "/dapp/connect",
|
|
10
|
+
eth_requestAccounts: "/dapp/connect",
|
|
11
|
+
wallet_addEthereumChain: "/dapp-evm/network-add",
|
|
12
|
+
wallet_watchAsset: "/dapp-evm/token-add",
|
|
13
|
+
personal_sign: "/dapp-evm/message-sign",
|
|
14
|
+
eth_encrypt: "/dapp-evm/message-sign",
|
|
15
|
+
eth_decrypt: "/dapp-evm/message-unsign",
|
|
16
|
+
eth_signTypedData_v4: "/dapp-evm/message-sign",
|
|
17
|
+
eth_signTransaction: "/dapp-evm/tx-sign",
|
|
18
|
+
eth_sendTransaction: "/dapp-evm/tx-send"
|
|
19
|
+
};
|
|
20
|
+
var dogeDappPopups = {
|
|
21
|
+
connect: "/dapp/connect",
|
|
22
|
+
requestAccounts: "/dapp/connect",
|
|
23
|
+
getPublicKey: "/dapp-doge/publickey",
|
|
24
|
+
switchNetwork: "/dapp-doge/network-switch",
|
|
25
|
+
signMessage: "/dapp-doge/message-sign",
|
|
26
|
+
requestSignedMessage: "/dapp-doge/message-sign",
|
|
27
|
+
requestDecryptedMessage: "/dapp-doge/message-unsign",
|
|
28
|
+
signPsbt: "/dapp-doge/psbt-sign",
|
|
29
|
+
requestPsbt: "/dapp-doge/psbt-sign",
|
|
30
|
+
signPsbts: "/dapp-doge/psbts-sign",
|
|
31
|
+
requestPsbts: "/dapp-doge/psbts-sign",
|
|
32
|
+
requestTransaction: "/dapp-doge/tx-send",
|
|
33
|
+
requestAvailableDRC20Transaction: "/dapp-doge/drc20-inscribe",
|
|
34
|
+
requestInscriptionTransaction: "/dapp-doge/drc20-tx",
|
|
35
|
+
requestDunesTransaction: "/dapp-doge/dunes-tx"
|
|
36
|
+
};
|
|
37
|
+
var solanaDappPopups = {
|
|
38
|
+
connect: "/dapp/connect",
|
|
39
|
+
requestAccounts: "/dapp/connect",
|
|
40
|
+
getPublicKey: "/dapp-solana/publickey",
|
|
41
|
+
signMessage: "/dapp-solana/message-sign",
|
|
42
|
+
signIn: "/dapp-solana/message-sign",
|
|
43
|
+
signTransaction: "/dapp-solana/tx-send",
|
|
44
|
+
signAllTransactions: "/dapp-solana/tx-send",
|
|
45
|
+
signAndSendTransaction: "/dapp-solana/tx-send",
|
|
46
|
+
signAndSendAllTransactions: "/dapp-solana/tx-send",
|
|
47
|
+
sendSolana: "/dapp-solana/tx-send",
|
|
48
|
+
sendToken: "/dapp-solana/tx-send"
|
|
49
|
+
};
|
|
50
|
+
var tronDappPopups = {
|
|
51
|
+
connect: "/dapp/connect",
|
|
52
|
+
tron_requestAccounts: "/dapp/connect",
|
|
53
|
+
signMessage: "/dapp-tron/message-sign",
|
|
54
|
+
wallet_watchAsset: "/dapp-tron/token-add",
|
|
55
|
+
signTransaction: "/dapp-tron/tx-send",
|
|
56
|
+
sendRawTransaction: "/dapp-tron/tx-send",
|
|
57
|
+
sendTransaction: "/dapp-tron/tx-send",
|
|
58
|
+
sendToken: "/dapp-tron/tx-send"
|
|
59
|
+
};
|
|
60
|
+
var btcDappPopups = {
|
|
61
|
+
connect: "/dapp/connect",
|
|
62
|
+
getPublicKey: "/dapp/connect",
|
|
63
|
+
requestAccounts: "/dapp/connect",
|
|
64
|
+
getBalance: "/dapp/connect",
|
|
65
|
+
switchNetwork: "/dapp-btc/network-switch",
|
|
66
|
+
switchChain: "/dapp-btc/network-switch",
|
|
67
|
+
signMessage: "/dapp-btc/message-sign",
|
|
68
|
+
requestDecryptedMessage: "/dapp-btc/message-unsign",
|
|
69
|
+
pushPsbt: "/dapp-btc/psbt-sign",
|
|
70
|
+
signPsbt: "/dapp-btc/psbt-sign",
|
|
71
|
+
signPsbts: "/dapp-btc/psbts-sign",
|
|
72
|
+
sendBitcoin: "/dapp-btc/tx-send"
|
|
73
|
+
};
|
|
74
|
+
var shopDappPopups = {
|
|
75
|
+
connect: "/dapp-shop/dialog",
|
|
76
|
+
welcomeDialog: "/dapp-shop/dialog",
|
|
77
|
+
addToCartDialog: "/dapp-shop/dialog",
|
|
78
|
+
rewardEstimatedDialog: "/dapp-shop/dialog",
|
|
79
|
+
rewardClaimedDialog: "/dapp-shop/dialog"
|
|
80
|
+
};
|
|
81
|
+
var dappPopups = {
|
|
82
|
+
[walletUtils.ChainTypes.EVM]: evmDappPopups,
|
|
83
|
+
[walletUtils.ChainTypes.DOGE]: dogeDappPopups,
|
|
84
|
+
[walletUtils.ChainTypes.SOL]: solanaDappPopups,
|
|
85
|
+
[walletUtils.ChainTypes.TRON]: tronDappPopups,
|
|
86
|
+
[walletUtils.ChainTypes.BTC]: btcDappPopups,
|
|
87
|
+
shop: shopDappPopups
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/hub.ts
|
|
91
|
+
var notPorivderAPIs = {
|
|
92
|
+
keepAlive: true,
|
|
93
|
+
wallet_getProviderState: true,
|
|
94
|
+
wallet_sendDomainMetadata: true
|
|
95
|
+
};
|
|
96
|
+
var sendRequest = async (chainType, { method, params, dappInfo }) => {
|
|
97
|
+
var _a;
|
|
98
|
+
if (!chainType || !method) {
|
|
99
|
+
throw new Error("chainType or method is not allowed to be empty");
|
|
100
|
+
}
|
|
101
|
+
if (notPorivderAPIs[method]) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const embeddedWallet = EmbeddedWallet.getInstance();
|
|
105
|
+
const walletOrigin = embeddedWallet.walletOrigin;
|
|
106
|
+
const walletIframe = embeddedWallet == null ? void 0 : embeddedWallet.walletIframe;
|
|
107
|
+
if (!walletOrigin || !walletIframe) {
|
|
108
|
+
console.error("walletOrigin is not set", { chainType, method, params, dappInfo });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const popupRoutes = dappPopups[chainType];
|
|
112
|
+
const isNeedApprove = popupRoutes == null ? void 0 : popupRoutes[method];
|
|
113
|
+
if (isNeedApprove) {
|
|
114
|
+
embeddedWallet.popup();
|
|
115
|
+
}
|
|
116
|
+
const data = {
|
|
117
|
+
chainType,
|
|
118
|
+
type: "dapp-request",
|
|
119
|
+
data: { method, params, dappInfo }
|
|
120
|
+
};
|
|
121
|
+
(_a = walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, walletOrigin);
|
|
122
|
+
console.log("sendRequest", data, walletOrigin);
|
|
123
|
+
};
|
|
124
|
+
var onResponse = async (requestParams) => {
|
|
125
|
+
return new Promise((resolve, reject) => {
|
|
126
|
+
const receiveResponse = ({ origin, data }) => {
|
|
127
|
+
if ((data == null ? void 0 : data.type) !== "dapp-response") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const embeddedWallet = EmbeddedWallet.getInstance();
|
|
131
|
+
if (origin === embeddedWallet.walletOrigin && requestParams.method === (data == null ? void 0 : data.method)) {
|
|
132
|
+
window.removeEventListener("message", receiveResponse);
|
|
133
|
+
console.log("onResponse", data);
|
|
134
|
+
if (data == null ? void 0 : data.success) {
|
|
135
|
+
resolve(data);
|
|
136
|
+
} else {
|
|
137
|
+
reject(data.data);
|
|
138
|
+
}
|
|
139
|
+
embeddedWallet.close();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
window.addEventListener("message", receiveResponse);
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
var EmbeddedWallet = class _EmbeddedWallet {
|
|
146
|
+
constructor() {
|
|
147
|
+
this.isAvailable = false;
|
|
148
|
+
this.config = null;
|
|
149
|
+
this.info = null;
|
|
150
|
+
this.btcProvider = null;
|
|
151
|
+
this.dogecoinProvider = null;
|
|
152
|
+
this.evmProvider = null;
|
|
153
|
+
this.solanaProvider = null;
|
|
154
|
+
this.tronProvider = null;
|
|
155
|
+
this.walletIframe = null;
|
|
156
|
+
this.walletOrigin = "";
|
|
157
|
+
this.maskZIndex = 999;
|
|
158
|
+
this.loginByGoogle = null;
|
|
159
|
+
this.loginByX = null;
|
|
160
|
+
this.walletCloseHandler = ({ origin, data }) => {
|
|
161
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
162
|
+
this.close();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
static getInstance() {
|
|
167
|
+
if (!_EmbeddedWallet.instance) {
|
|
168
|
+
_EmbeddedWallet.instance = new _EmbeddedWallet();
|
|
169
|
+
}
|
|
170
|
+
return _EmbeddedWallet.instance;
|
|
171
|
+
}
|
|
172
|
+
async init(config) {
|
|
173
|
+
var _a;
|
|
174
|
+
this.config = config;
|
|
175
|
+
if (!config.tomoClientId) {
|
|
176
|
+
throw new Error("tomoClientId is required.");
|
|
177
|
+
}
|
|
178
|
+
if (!config.walletBaseUrl) {
|
|
179
|
+
throw new Error("walletBaseUrl is required.");
|
|
180
|
+
}
|
|
181
|
+
const { origin: walletOrigin, hostname, port } = new URL(config.walletBaseUrl);
|
|
182
|
+
this.walletOrigin = walletOrigin;
|
|
183
|
+
const hostArr = hostname.split(".");
|
|
184
|
+
if (port) {
|
|
185
|
+
hostArr.push(port);
|
|
186
|
+
}
|
|
187
|
+
hostArr.reverse();
|
|
188
|
+
const rdns = hostArr.join(".");
|
|
189
|
+
this.info = {
|
|
190
|
+
uuid: hostArr.join("-"),
|
|
191
|
+
name: config.name,
|
|
192
|
+
icon: config.logo,
|
|
193
|
+
rdns,
|
|
194
|
+
links: {
|
|
195
|
+
homepage: config.walletBaseUrl
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
const productInfo = {
|
|
199
|
+
name: config.name,
|
|
200
|
+
rdns,
|
|
201
|
+
icon: config.logo
|
|
202
|
+
};
|
|
203
|
+
this.btcProvider = new injectProviders.BtcProvider(productInfo, { sendRequest, onResponse });
|
|
204
|
+
this.dogecoinProvider = new injectProviders.DogecoinProvider(productInfo, { sendRequest, onResponse });
|
|
205
|
+
this.evmProvider = new injectProviders.EvmProvider(productInfo, { sendRequest, onResponse });
|
|
206
|
+
this.solanaProvider = new injectProviders.SolanaProvider(productInfo, { sendRequest, onResponse });
|
|
207
|
+
this.tronProvider = new injectProviders.TronProvider(productInfo, { sendRequest, onResponse });
|
|
208
|
+
const { loginByGoogle, loginByX } = oidcAuth.OidcAuth(config);
|
|
209
|
+
this.loginByGoogle = loginByGoogle;
|
|
210
|
+
this.loginByX = loginByX;
|
|
211
|
+
const { isAvailable, message, connectedInfo } = await this.login();
|
|
212
|
+
this.isAvailable = isAvailable;
|
|
213
|
+
const { connected, address = [] } = (connectedInfo == null ? void 0 : connectedInfo.evmProvider) || {};
|
|
214
|
+
if (connected && address.length > 0) {
|
|
215
|
+
(_a = this.evmProvider) == null ? void 0 : _a.setConnectedStatus({ connected, address });
|
|
216
|
+
}
|
|
217
|
+
return { isAvailable, message, connectedInfo };
|
|
218
|
+
}
|
|
219
|
+
setWalletZIndex(zIndex) {
|
|
220
|
+
this.maskZIndex = zIndex;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* loginByCubistV2, create wallet with oidcToken
|
|
224
|
+
*/
|
|
225
|
+
async login(oidcToken) {
|
|
226
|
+
const config = this.config;
|
|
227
|
+
if (!config) {
|
|
228
|
+
throw new Error("config is not initialized");
|
|
229
|
+
}
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
try {
|
|
232
|
+
const { walletBaseUrl, tomoClientId, tomoStage, logo, name } = config || {};
|
|
233
|
+
if (!walletBaseUrl || !tomoClientId) {
|
|
234
|
+
throw new Error("walletBaseUrl + tomoClientId is required");
|
|
235
|
+
}
|
|
236
|
+
const EMBEDDED_WALLET_ID = tomoClientId;
|
|
237
|
+
let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
|
|
238
|
+
if (!walletIframe) {
|
|
239
|
+
walletIframe = document.createElement("iframe");
|
|
240
|
+
walletIframe.style.cssText = ` width: 0; height: 0;`;
|
|
241
|
+
walletIframe.id = EMBEDDED_WALLET_ID;
|
|
242
|
+
document.body.appendChild(walletIframe);
|
|
243
|
+
}
|
|
244
|
+
const dappOrigin = window.location.origin;
|
|
245
|
+
walletIframe.src = `${walletBaseUrl}#dappOrigin=${dappOrigin}&tomoStage=${tomoStage}&tomoClientId=${tomoClientId}&oidcToken=${oidcToken || ""}&logo=${logo || ""}&name=${name || ""}`;
|
|
246
|
+
walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
|
|
247
|
+
this.walletIframe = walletIframe;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.error("login error", error);
|
|
250
|
+
reject(error);
|
|
251
|
+
}
|
|
252
|
+
const receiveResponse = ({ origin, data }) => {
|
|
253
|
+
var _a;
|
|
254
|
+
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
255
|
+
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
256
|
+
resolve(data == null ? void 0 : data.data);
|
|
257
|
+
window.removeEventListener("message", receiveResponse);
|
|
258
|
+
}
|
|
259
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
260
|
+
this.close();
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
window.addEventListener("message", receiveResponse);
|
|
264
|
+
window.addEventListener("message", this.walletCloseHandler);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
popup() {
|
|
268
|
+
if (!this.walletIframe) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const baseCssText = "position: fixed; top:0; left:0; border: none; width: 100%; height: 100%;";
|
|
272
|
+
const maskZIndex = this.maskZIndex;
|
|
273
|
+
this.walletIframe.style.cssText = `${baseCssText} z-index: ${maskZIndex};`;
|
|
274
|
+
}
|
|
275
|
+
close() {
|
|
276
|
+
if (!this.walletIframe) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
this.walletIframe.style.width = "0";
|
|
280
|
+
this.walletIframe.style.height = "0";
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* call relay for cubist logout, retrieve sessionInfo
|
|
284
|
+
*/
|
|
285
|
+
async logout() {
|
|
286
|
+
if (this.walletIframe) {
|
|
287
|
+
this.walletIframe.src = `${this.walletOrigin}#logout=true`;
|
|
288
|
+
}
|
|
289
|
+
window.removeEventListener("message", this.walletCloseHandler);
|
|
290
|
+
this.request("logout");
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* open email login page
|
|
295
|
+
* */
|
|
296
|
+
async loginByEmail({ email }) {
|
|
297
|
+
this.popup();
|
|
298
|
+
return new Promise((resolve, reject) => {
|
|
299
|
+
const emailLoginCloseHandler = (event) => {
|
|
300
|
+
var _a;
|
|
301
|
+
if (((_a = event.data) == null ? void 0 : _a.type) === "wallet-close" && event.origin === this.walletOrigin) {
|
|
302
|
+
window.removeEventListener("message", emailLoginCloseHandler);
|
|
303
|
+
this.close();
|
|
304
|
+
resolve("");
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
window.addEventListener("message", emailLoginCloseHandler);
|
|
308
|
+
this.request("emailLogin", { email }).then(({ oidcToken = "" }) => {
|
|
309
|
+
resolve(oidcToken);
|
|
310
|
+
}).catch((error) => {
|
|
311
|
+
reject(error);
|
|
312
|
+
}).finally(() => {
|
|
313
|
+
window.removeEventListener("message", emailLoginCloseHandler);
|
|
314
|
+
this.close();
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* wallet feature apis
|
|
320
|
+
* wallet action without user approve
|
|
321
|
+
*/
|
|
322
|
+
async request(method, params) {
|
|
323
|
+
var _a;
|
|
324
|
+
const data = {
|
|
325
|
+
type: "wallet-request",
|
|
326
|
+
data: { method, params }
|
|
327
|
+
};
|
|
328
|
+
(_a = this.walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, this.walletOrigin);
|
|
329
|
+
return new Promise((resolve, reject) => {
|
|
330
|
+
const receiveResponse = ({ origin, data: data2 }) => {
|
|
331
|
+
if ((data2 == null ? void 0 : data2.type) === "wallet-response" && origin === this.walletOrigin) {
|
|
332
|
+
resolve(data2 == null ? void 0 : data2.data);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
window.addEventListener("message", receiveResponse);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* wallet features page
|
|
340
|
+
*/
|
|
341
|
+
async open(method, params) {
|
|
342
|
+
var _a;
|
|
343
|
+
const data = {
|
|
344
|
+
type: "wallet-request",
|
|
345
|
+
data: { method, params }
|
|
346
|
+
};
|
|
347
|
+
(_a = this.walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, this.walletOrigin);
|
|
348
|
+
this.popup();
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
exports.EmbeddedWallet = EmbeddedWallet;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export { EmailLoginResult } from '@tomo-inc/oidc-auth';
|
|
2
|
+
import { BtcProvider, DogecoinProvider, EvmProvider, SolanaProvider, TronProvider } from '@tomo-inc/inject-providers';
|
|
3
|
+
|
|
4
|
+
interface InitConfig {
|
|
5
|
+
xClientId: string;
|
|
6
|
+
googleClientId: string;
|
|
7
|
+
name: string;
|
|
8
|
+
logo: string;
|
|
9
|
+
tomoStage: "dev" | "prod";
|
|
10
|
+
tomoClientId: string;
|
|
11
|
+
walletBaseUrl: string;
|
|
12
|
+
relayBaseUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
interface WalletInfo {
|
|
15
|
+
uuid: string;
|
|
16
|
+
name: string;
|
|
17
|
+
icon: string;
|
|
18
|
+
rdns: string;
|
|
19
|
+
iconBackground?: string;
|
|
20
|
+
links?: {
|
|
21
|
+
homepage: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
type WalletRequestMethod = "logout" | "token" | "market" | "assets" | "transactions" | "wallet" | "emailLogin";
|
|
25
|
+
type WalletOpenMethod = "wallet" | "swap" | "onRamp" | "funding" | "mfaVerify" | "mnemonic" | "setting";
|
|
26
|
+
|
|
27
|
+
declare class EmbeddedWallet {
|
|
28
|
+
isAvailable: boolean;
|
|
29
|
+
config: InitConfig | null;
|
|
30
|
+
info: WalletInfo | null;
|
|
31
|
+
btcProvider: BtcProvider | null;
|
|
32
|
+
dogecoinProvider: DogecoinProvider | null;
|
|
33
|
+
evmProvider: EvmProvider | null;
|
|
34
|
+
solanaProvider: SolanaProvider | null;
|
|
35
|
+
tronProvider: TronProvider | null;
|
|
36
|
+
walletIframe: HTMLIFrameElement | null;
|
|
37
|
+
walletOrigin: string;
|
|
38
|
+
maskZIndex: number;
|
|
39
|
+
private static instance;
|
|
40
|
+
loginByGoogle: (() => Promise<string>) | null;
|
|
41
|
+
loginByX: (() => Promise<string>) | null;
|
|
42
|
+
constructor();
|
|
43
|
+
static getInstance(): EmbeddedWallet;
|
|
44
|
+
init(config: InitConfig): Promise<{
|
|
45
|
+
isAvailable: boolean;
|
|
46
|
+
message: string | undefined;
|
|
47
|
+
connectedInfo: any;
|
|
48
|
+
}>;
|
|
49
|
+
setWalletZIndex(zIndex: number): void;
|
|
50
|
+
/**
|
|
51
|
+
* loginByCubistV2, create wallet with oidcToken
|
|
52
|
+
*/
|
|
53
|
+
login(oidcToken?: string): Promise<{
|
|
54
|
+
connectedInfo: any;
|
|
55
|
+
message?: string;
|
|
56
|
+
isAvailable: boolean;
|
|
57
|
+
}>;
|
|
58
|
+
private walletCloseHandler;
|
|
59
|
+
popup(): void;
|
|
60
|
+
close(): void;
|
|
61
|
+
/**
|
|
62
|
+
* call relay for cubist logout, retrieve sessionInfo
|
|
63
|
+
*/
|
|
64
|
+
logout(): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* open email login page
|
|
67
|
+
* */
|
|
68
|
+
loginByEmail({ email }: {
|
|
69
|
+
email?: string;
|
|
70
|
+
}): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* wallet feature apis
|
|
73
|
+
* wallet action without user approve
|
|
74
|
+
*/
|
|
75
|
+
request(method: WalletRequestMethod, params?: any): Promise<any>;
|
|
76
|
+
/**
|
|
77
|
+
* wallet features page
|
|
78
|
+
*/
|
|
79
|
+
open(method: WalletOpenMethod, params?: any): Promise<any>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { EmbeddedWallet, type InitConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export { EmailLoginResult } from '@tomo-inc/oidc-auth';
|
|
2
|
+
import { BtcProvider, DogecoinProvider, EvmProvider, SolanaProvider, TronProvider } from '@tomo-inc/inject-providers';
|
|
3
|
+
|
|
4
|
+
interface InitConfig {
|
|
5
|
+
xClientId: string;
|
|
6
|
+
googleClientId: string;
|
|
7
|
+
name: string;
|
|
8
|
+
logo: string;
|
|
9
|
+
tomoStage: "dev" | "prod";
|
|
10
|
+
tomoClientId: string;
|
|
11
|
+
walletBaseUrl: string;
|
|
12
|
+
relayBaseUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
interface WalletInfo {
|
|
15
|
+
uuid: string;
|
|
16
|
+
name: string;
|
|
17
|
+
icon: string;
|
|
18
|
+
rdns: string;
|
|
19
|
+
iconBackground?: string;
|
|
20
|
+
links?: {
|
|
21
|
+
homepage: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
type WalletRequestMethod = "logout" | "token" | "market" | "assets" | "transactions" | "wallet" | "emailLogin";
|
|
25
|
+
type WalletOpenMethod = "wallet" | "swap" | "onRamp" | "funding" | "mfaVerify" | "mnemonic" | "setting";
|
|
26
|
+
|
|
27
|
+
declare class EmbeddedWallet {
|
|
28
|
+
isAvailable: boolean;
|
|
29
|
+
config: InitConfig | null;
|
|
30
|
+
info: WalletInfo | null;
|
|
31
|
+
btcProvider: BtcProvider | null;
|
|
32
|
+
dogecoinProvider: DogecoinProvider | null;
|
|
33
|
+
evmProvider: EvmProvider | null;
|
|
34
|
+
solanaProvider: SolanaProvider | null;
|
|
35
|
+
tronProvider: TronProvider | null;
|
|
36
|
+
walletIframe: HTMLIFrameElement | null;
|
|
37
|
+
walletOrigin: string;
|
|
38
|
+
maskZIndex: number;
|
|
39
|
+
private static instance;
|
|
40
|
+
loginByGoogle: (() => Promise<string>) | null;
|
|
41
|
+
loginByX: (() => Promise<string>) | null;
|
|
42
|
+
constructor();
|
|
43
|
+
static getInstance(): EmbeddedWallet;
|
|
44
|
+
init(config: InitConfig): Promise<{
|
|
45
|
+
isAvailable: boolean;
|
|
46
|
+
message: string | undefined;
|
|
47
|
+
connectedInfo: any;
|
|
48
|
+
}>;
|
|
49
|
+
setWalletZIndex(zIndex: number): void;
|
|
50
|
+
/**
|
|
51
|
+
* loginByCubistV2, create wallet with oidcToken
|
|
52
|
+
*/
|
|
53
|
+
login(oidcToken?: string): Promise<{
|
|
54
|
+
connectedInfo: any;
|
|
55
|
+
message?: string;
|
|
56
|
+
isAvailable: boolean;
|
|
57
|
+
}>;
|
|
58
|
+
private walletCloseHandler;
|
|
59
|
+
popup(): void;
|
|
60
|
+
close(): void;
|
|
61
|
+
/**
|
|
62
|
+
* call relay for cubist logout, retrieve sessionInfo
|
|
63
|
+
*/
|
|
64
|
+
logout(): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* open email login page
|
|
67
|
+
* */
|
|
68
|
+
loginByEmail({ email }: {
|
|
69
|
+
email?: string;
|
|
70
|
+
}): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* wallet feature apis
|
|
73
|
+
* wallet action without user approve
|
|
74
|
+
*/
|
|
75
|
+
request(method: WalletRequestMethod, params?: any): Promise<any>;
|
|
76
|
+
/**
|
|
77
|
+
* wallet features page
|
|
78
|
+
*/
|
|
79
|
+
open(method: WalletOpenMethod, params?: any): Promise<any>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { EmbeddedWallet, type InitConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { BtcProvider, DogecoinProvider, EvmProvider, SolanaProvider, TronProvider } from '@tomo-inc/inject-providers';
|
|
2
|
+
import { ChainTypes } from '@tomo-inc/wallet-utils';
|
|
3
|
+
import { OidcAuth } from '@tomo-inc/oidc-auth';
|
|
4
|
+
|
|
5
|
+
// src/embedded-wallet.ts
|
|
6
|
+
var evmDappPopups = {
|
|
7
|
+
connect: "/dapp/connect",
|
|
8
|
+
eth_requestAccounts: "/dapp/connect",
|
|
9
|
+
wallet_addEthereumChain: "/dapp-evm/network-add",
|
|
10
|
+
wallet_watchAsset: "/dapp-evm/token-add",
|
|
11
|
+
personal_sign: "/dapp-evm/message-sign",
|
|
12
|
+
eth_encrypt: "/dapp-evm/message-sign",
|
|
13
|
+
eth_decrypt: "/dapp-evm/message-unsign",
|
|
14
|
+
eth_signTypedData_v4: "/dapp-evm/message-sign",
|
|
15
|
+
eth_signTransaction: "/dapp-evm/tx-sign",
|
|
16
|
+
eth_sendTransaction: "/dapp-evm/tx-send"
|
|
17
|
+
};
|
|
18
|
+
var dogeDappPopups = {
|
|
19
|
+
connect: "/dapp/connect",
|
|
20
|
+
requestAccounts: "/dapp/connect",
|
|
21
|
+
getPublicKey: "/dapp-doge/publickey",
|
|
22
|
+
switchNetwork: "/dapp-doge/network-switch",
|
|
23
|
+
signMessage: "/dapp-doge/message-sign",
|
|
24
|
+
requestSignedMessage: "/dapp-doge/message-sign",
|
|
25
|
+
requestDecryptedMessage: "/dapp-doge/message-unsign",
|
|
26
|
+
signPsbt: "/dapp-doge/psbt-sign",
|
|
27
|
+
requestPsbt: "/dapp-doge/psbt-sign",
|
|
28
|
+
signPsbts: "/dapp-doge/psbts-sign",
|
|
29
|
+
requestPsbts: "/dapp-doge/psbts-sign",
|
|
30
|
+
requestTransaction: "/dapp-doge/tx-send",
|
|
31
|
+
requestAvailableDRC20Transaction: "/dapp-doge/drc20-inscribe",
|
|
32
|
+
requestInscriptionTransaction: "/dapp-doge/drc20-tx",
|
|
33
|
+
requestDunesTransaction: "/dapp-doge/dunes-tx"
|
|
34
|
+
};
|
|
35
|
+
var solanaDappPopups = {
|
|
36
|
+
connect: "/dapp/connect",
|
|
37
|
+
requestAccounts: "/dapp/connect",
|
|
38
|
+
getPublicKey: "/dapp-solana/publickey",
|
|
39
|
+
signMessage: "/dapp-solana/message-sign",
|
|
40
|
+
signIn: "/dapp-solana/message-sign",
|
|
41
|
+
signTransaction: "/dapp-solana/tx-send",
|
|
42
|
+
signAllTransactions: "/dapp-solana/tx-send",
|
|
43
|
+
signAndSendTransaction: "/dapp-solana/tx-send",
|
|
44
|
+
signAndSendAllTransactions: "/dapp-solana/tx-send",
|
|
45
|
+
sendSolana: "/dapp-solana/tx-send",
|
|
46
|
+
sendToken: "/dapp-solana/tx-send"
|
|
47
|
+
};
|
|
48
|
+
var tronDappPopups = {
|
|
49
|
+
connect: "/dapp/connect",
|
|
50
|
+
tron_requestAccounts: "/dapp/connect",
|
|
51
|
+
signMessage: "/dapp-tron/message-sign",
|
|
52
|
+
wallet_watchAsset: "/dapp-tron/token-add",
|
|
53
|
+
signTransaction: "/dapp-tron/tx-send",
|
|
54
|
+
sendRawTransaction: "/dapp-tron/tx-send",
|
|
55
|
+
sendTransaction: "/dapp-tron/tx-send",
|
|
56
|
+
sendToken: "/dapp-tron/tx-send"
|
|
57
|
+
};
|
|
58
|
+
var btcDappPopups = {
|
|
59
|
+
connect: "/dapp/connect",
|
|
60
|
+
getPublicKey: "/dapp/connect",
|
|
61
|
+
requestAccounts: "/dapp/connect",
|
|
62
|
+
getBalance: "/dapp/connect",
|
|
63
|
+
switchNetwork: "/dapp-btc/network-switch",
|
|
64
|
+
switchChain: "/dapp-btc/network-switch",
|
|
65
|
+
signMessage: "/dapp-btc/message-sign",
|
|
66
|
+
requestDecryptedMessage: "/dapp-btc/message-unsign",
|
|
67
|
+
pushPsbt: "/dapp-btc/psbt-sign",
|
|
68
|
+
signPsbt: "/dapp-btc/psbt-sign",
|
|
69
|
+
signPsbts: "/dapp-btc/psbts-sign",
|
|
70
|
+
sendBitcoin: "/dapp-btc/tx-send"
|
|
71
|
+
};
|
|
72
|
+
var shopDappPopups = {
|
|
73
|
+
connect: "/dapp-shop/dialog",
|
|
74
|
+
welcomeDialog: "/dapp-shop/dialog",
|
|
75
|
+
addToCartDialog: "/dapp-shop/dialog",
|
|
76
|
+
rewardEstimatedDialog: "/dapp-shop/dialog",
|
|
77
|
+
rewardClaimedDialog: "/dapp-shop/dialog"
|
|
78
|
+
};
|
|
79
|
+
var dappPopups = {
|
|
80
|
+
[ChainTypes.EVM]: evmDappPopups,
|
|
81
|
+
[ChainTypes.DOGE]: dogeDappPopups,
|
|
82
|
+
[ChainTypes.SOL]: solanaDappPopups,
|
|
83
|
+
[ChainTypes.TRON]: tronDappPopups,
|
|
84
|
+
[ChainTypes.BTC]: btcDappPopups,
|
|
85
|
+
shop: shopDappPopups
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/hub.ts
|
|
89
|
+
var notPorivderAPIs = {
|
|
90
|
+
keepAlive: true,
|
|
91
|
+
wallet_getProviderState: true,
|
|
92
|
+
wallet_sendDomainMetadata: true
|
|
93
|
+
};
|
|
94
|
+
var sendRequest = async (chainType, { method, params, dappInfo }) => {
|
|
95
|
+
var _a;
|
|
96
|
+
if (!chainType || !method) {
|
|
97
|
+
throw new Error("chainType or method is not allowed to be empty");
|
|
98
|
+
}
|
|
99
|
+
if (notPorivderAPIs[method]) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const embeddedWallet = EmbeddedWallet.getInstance();
|
|
103
|
+
const walletOrigin = embeddedWallet.walletOrigin;
|
|
104
|
+
const walletIframe = embeddedWallet == null ? void 0 : embeddedWallet.walletIframe;
|
|
105
|
+
if (!walletOrigin || !walletIframe) {
|
|
106
|
+
console.error("walletOrigin is not set", { chainType, method, params, dappInfo });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const popupRoutes = dappPopups[chainType];
|
|
110
|
+
const isNeedApprove = popupRoutes == null ? void 0 : popupRoutes[method];
|
|
111
|
+
if (isNeedApprove) {
|
|
112
|
+
embeddedWallet.popup();
|
|
113
|
+
}
|
|
114
|
+
const data = {
|
|
115
|
+
chainType,
|
|
116
|
+
type: "dapp-request",
|
|
117
|
+
data: { method, params, dappInfo }
|
|
118
|
+
};
|
|
119
|
+
(_a = walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, walletOrigin);
|
|
120
|
+
console.log("sendRequest", data, walletOrigin);
|
|
121
|
+
};
|
|
122
|
+
var onResponse = async (requestParams) => {
|
|
123
|
+
return new Promise((resolve, reject) => {
|
|
124
|
+
const receiveResponse = ({ origin, data }) => {
|
|
125
|
+
if ((data == null ? void 0 : data.type) !== "dapp-response") {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const embeddedWallet = EmbeddedWallet.getInstance();
|
|
129
|
+
if (origin === embeddedWallet.walletOrigin && requestParams.method === (data == null ? void 0 : data.method)) {
|
|
130
|
+
window.removeEventListener("message", receiveResponse);
|
|
131
|
+
console.log("onResponse", data);
|
|
132
|
+
if (data == null ? void 0 : data.success) {
|
|
133
|
+
resolve(data);
|
|
134
|
+
} else {
|
|
135
|
+
reject(data.data);
|
|
136
|
+
}
|
|
137
|
+
embeddedWallet.close();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
window.addEventListener("message", receiveResponse);
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
var EmbeddedWallet = class _EmbeddedWallet {
|
|
144
|
+
constructor() {
|
|
145
|
+
this.isAvailable = false;
|
|
146
|
+
this.config = null;
|
|
147
|
+
this.info = null;
|
|
148
|
+
this.btcProvider = null;
|
|
149
|
+
this.dogecoinProvider = null;
|
|
150
|
+
this.evmProvider = null;
|
|
151
|
+
this.solanaProvider = null;
|
|
152
|
+
this.tronProvider = null;
|
|
153
|
+
this.walletIframe = null;
|
|
154
|
+
this.walletOrigin = "";
|
|
155
|
+
this.maskZIndex = 999;
|
|
156
|
+
this.loginByGoogle = null;
|
|
157
|
+
this.loginByX = null;
|
|
158
|
+
this.walletCloseHandler = ({ origin, data }) => {
|
|
159
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
160
|
+
this.close();
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
static getInstance() {
|
|
165
|
+
if (!_EmbeddedWallet.instance) {
|
|
166
|
+
_EmbeddedWallet.instance = new _EmbeddedWallet();
|
|
167
|
+
}
|
|
168
|
+
return _EmbeddedWallet.instance;
|
|
169
|
+
}
|
|
170
|
+
async init(config) {
|
|
171
|
+
var _a;
|
|
172
|
+
this.config = config;
|
|
173
|
+
if (!config.tomoClientId) {
|
|
174
|
+
throw new Error("tomoClientId is required.");
|
|
175
|
+
}
|
|
176
|
+
if (!config.walletBaseUrl) {
|
|
177
|
+
throw new Error("walletBaseUrl is required.");
|
|
178
|
+
}
|
|
179
|
+
const { origin: walletOrigin, hostname, port } = new URL(config.walletBaseUrl);
|
|
180
|
+
this.walletOrigin = walletOrigin;
|
|
181
|
+
const hostArr = hostname.split(".");
|
|
182
|
+
if (port) {
|
|
183
|
+
hostArr.push(port);
|
|
184
|
+
}
|
|
185
|
+
hostArr.reverse();
|
|
186
|
+
const rdns = hostArr.join(".");
|
|
187
|
+
this.info = {
|
|
188
|
+
uuid: hostArr.join("-"),
|
|
189
|
+
name: config.name,
|
|
190
|
+
icon: config.logo,
|
|
191
|
+
rdns,
|
|
192
|
+
links: {
|
|
193
|
+
homepage: config.walletBaseUrl
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const productInfo = {
|
|
197
|
+
name: config.name,
|
|
198
|
+
rdns,
|
|
199
|
+
icon: config.logo
|
|
200
|
+
};
|
|
201
|
+
this.btcProvider = new BtcProvider(productInfo, { sendRequest, onResponse });
|
|
202
|
+
this.dogecoinProvider = new DogecoinProvider(productInfo, { sendRequest, onResponse });
|
|
203
|
+
this.evmProvider = new EvmProvider(productInfo, { sendRequest, onResponse });
|
|
204
|
+
this.solanaProvider = new SolanaProvider(productInfo, { sendRequest, onResponse });
|
|
205
|
+
this.tronProvider = new TronProvider(productInfo, { sendRequest, onResponse });
|
|
206
|
+
const { loginByGoogle, loginByX } = OidcAuth(config);
|
|
207
|
+
this.loginByGoogle = loginByGoogle;
|
|
208
|
+
this.loginByX = loginByX;
|
|
209
|
+
const { isAvailable, message, connectedInfo } = await this.login();
|
|
210
|
+
this.isAvailable = isAvailable;
|
|
211
|
+
const { connected, address = [] } = (connectedInfo == null ? void 0 : connectedInfo.evmProvider) || {};
|
|
212
|
+
if (connected && address.length > 0) {
|
|
213
|
+
(_a = this.evmProvider) == null ? void 0 : _a.setConnectedStatus({ connected, address });
|
|
214
|
+
}
|
|
215
|
+
return { isAvailable, message, connectedInfo };
|
|
216
|
+
}
|
|
217
|
+
setWalletZIndex(zIndex) {
|
|
218
|
+
this.maskZIndex = zIndex;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* loginByCubistV2, create wallet with oidcToken
|
|
222
|
+
*/
|
|
223
|
+
async login(oidcToken) {
|
|
224
|
+
const config = this.config;
|
|
225
|
+
if (!config) {
|
|
226
|
+
throw new Error("config is not initialized");
|
|
227
|
+
}
|
|
228
|
+
return new Promise((resolve, reject) => {
|
|
229
|
+
try {
|
|
230
|
+
const { walletBaseUrl, tomoClientId, tomoStage, logo, name } = config || {};
|
|
231
|
+
if (!walletBaseUrl || !tomoClientId) {
|
|
232
|
+
throw new Error("walletBaseUrl + tomoClientId is required");
|
|
233
|
+
}
|
|
234
|
+
const EMBEDDED_WALLET_ID = tomoClientId;
|
|
235
|
+
let walletIframe = document.getElementById(EMBEDDED_WALLET_ID);
|
|
236
|
+
if (!walletIframe) {
|
|
237
|
+
walletIframe = document.createElement("iframe");
|
|
238
|
+
walletIframe.style.cssText = ` width: 0; height: 0;`;
|
|
239
|
+
walletIframe.id = EMBEDDED_WALLET_ID;
|
|
240
|
+
document.body.appendChild(walletIframe);
|
|
241
|
+
}
|
|
242
|
+
const dappOrigin = window.location.origin;
|
|
243
|
+
walletIframe.src = `${walletBaseUrl}#dappOrigin=${dappOrigin}&tomoStage=${tomoStage}&tomoClientId=${tomoClientId}&oidcToken=${oidcToken || ""}&logo=${logo || ""}&name=${name || ""}`;
|
|
244
|
+
walletIframe.allow = "publickey-credentials-get; publickey-credentials-create";
|
|
245
|
+
this.walletIframe = walletIframe;
|
|
246
|
+
} catch (error) {
|
|
247
|
+
console.error("login error", error);
|
|
248
|
+
reject(error);
|
|
249
|
+
}
|
|
250
|
+
const receiveResponse = ({ origin, data }) => {
|
|
251
|
+
var _a;
|
|
252
|
+
if ((data == null ? void 0 : data.type) === "wallet-ready" && origin === this.walletOrigin) {
|
|
253
|
+
this.isAvailable = ((_a = data == null ? void 0 : data.data) == null ? void 0 : _a.isAvailable) || false;
|
|
254
|
+
resolve(data == null ? void 0 : data.data);
|
|
255
|
+
window.removeEventListener("message", receiveResponse);
|
|
256
|
+
}
|
|
257
|
+
if ((data == null ? void 0 : data.type) === "wallet-close" && origin === this.walletOrigin) {
|
|
258
|
+
this.close();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
window.addEventListener("message", receiveResponse);
|
|
262
|
+
window.addEventListener("message", this.walletCloseHandler);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
popup() {
|
|
266
|
+
if (!this.walletIframe) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const baseCssText = "position: fixed; top:0; left:0; border: none; width: 100%; height: 100%;";
|
|
270
|
+
const maskZIndex = this.maskZIndex;
|
|
271
|
+
this.walletIframe.style.cssText = `${baseCssText} z-index: ${maskZIndex};`;
|
|
272
|
+
}
|
|
273
|
+
close() {
|
|
274
|
+
if (!this.walletIframe) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
this.walletIframe.style.width = "0";
|
|
278
|
+
this.walletIframe.style.height = "0";
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* call relay for cubist logout, retrieve sessionInfo
|
|
282
|
+
*/
|
|
283
|
+
async logout() {
|
|
284
|
+
if (this.walletIframe) {
|
|
285
|
+
this.walletIframe.src = `${this.walletOrigin}#logout=true`;
|
|
286
|
+
}
|
|
287
|
+
window.removeEventListener("message", this.walletCloseHandler);
|
|
288
|
+
this.request("logout");
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* open email login page
|
|
293
|
+
* */
|
|
294
|
+
async loginByEmail({ email }) {
|
|
295
|
+
this.popup();
|
|
296
|
+
return new Promise((resolve, reject) => {
|
|
297
|
+
const emailLoginCloseHandler = (event) => {
|
|
298
|
+
var _a;
|
|
299
|
+
if (((_a = event.data) == null ? void 0 : _a.type) === "wallet-close" && event.origin === this.walletOrigin) {
|
|
300
|
+
window.removeEventListener("message", emailLoginCloseHandler);
|
|
301
|
+
this.close();
|
|
302
|
+
resolve("");
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
window.addEventListener("message", emailLoginCloseHandler);
|
|
306
|
+
this.request("emailLogin", { email }).then(({ oidcToken = "" }) => {
|
|
307
|
+
resolve(oidcToken);
|
|
308
|
+
}).catch((error) => {
|
|
309
|
+
reject(error);
|
|
310
|
+
}).finally(() => {
|
|
311
|
+
window.removeEventListener("message", emailLoginCloseHandler);
|
|
312
|
+
this.close();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* wallet feature apis
|
|
318
|
+
* wallet action without user approve
|
|
319
|
+
*/
|
|
320
|
+
async request(method, params) {
|
|
321
|
+
var _a;
|
|
322
|
+
const data = {
|
|
323
|
+
type: "wallet-request",
|
|
324
|
+
data: { method, params }
|
|
325
|
+
};
|
|
326
|
+
(_a = this.walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, this.walletOrigin);
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
328
|
+
const receiveResponse = ({ origin, data: data2 }) => {
|
|
329
|
+
if ((data2 == null ? void 0 : data2.type) === "wallet-response" && origin === this.walletOrigin) {
|
|
330
|
+
resolve(data2 == null ? void 0 : data2.data);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
window.addEventListener("message", receiveResponse);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* wallet features page
|
|
338
|
+
*/
|
|
339
|
+
async open(method, params) {
|
|
340
|
+
var _a;
|
|
341
|
+
const data = {
|
|
342
|
+
type: "wallet-request",
|
|
343
|
+
data: { method, params }
|
|
344
|
+
};
|
|
345
|
+
(_a = this.walletIframe.contentWindow) == null ? void 0 : _a.postMessage(data, this.walletOrigin);
|
|
346
|
+
this.popup();
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
export { EmbeddedWallet };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomo-inc/embedded-wallet-providers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"author": "tomo.inc",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@tomo-inc/oidc-auth": "0.0.
|
|
20
|
-
"@tomo-inc/
|
|
21
|
-
"@tomo-inc/
|
|
19
|
+
"@tomo-inc/oidc-auth": "0.0.5",
|
|
20
|
+
"@tomo-inc/wallet-utils": "0.0.5",
|
|
21
|
+
"@tomo-inc/inject-providers": "0.0.3"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/node": "^20.0.0",
|