@dynamic-labs/blocto-evm 0.19.0-alpha.21
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/CHANGELOG.md +1865 -0
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/_virtual/_tslib.cjs +30 -0
- package/_virtual/_tslib.js +26 -0
- package/package.json +36 -0
- package/src/BloctoEmailConnector.cjs +86 -0
- package/src/BloctoEmailConnector.d.ts +13 -0
- package/src/BloctoEmailConnector.js +82 -0
- package/src/BloctoEvm.cjs +142 -0
- package/src/BloctoEvm.d.ts +24 -0
- package/src/BloctoEvm.js +134 -0
- package/src/index.cjs +17 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +13 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Dynamic Labs, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/******************************************************************************
|
|
6
|
+
Copyright (c) Microsoft Corporation.
|
|
7
|
+
|
|
8
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
9
|
+
purpose with or without fee is hereby granted.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
13
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
***************************************************************************** */
|
|
19
|
+
|
|
20
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.__awaiter = __awaiter;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
|
|
16
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { __awaiter };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamic-labs/blocto-evm",
|
|
3
|
+
"version": "0.19.0-alpha.21",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/dynamic-labs/DynamicAuth.git",
|
|
7
|
+
"directory": "packages/blocto-evm"
|
|
8
|
+
},
|
|
9
|
+
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/dynamic-labs/DynamicAuth/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/dynamic-labs/DynamicAuth#readme",
|
|
14
|
+
"author": "Dynamic Labs, Inc.",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "./src/index.cjs",
|
|
17
|
+
"module": "./src/index.js",
|
|
18
|
+
"types": "./src/index.d.ts",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./src/index.d.ts",
|
|
23
|
+
"import": "./src/index.js",
|
|
24
|
+
"require": "./src/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@blocto/sdk": "0.5.4",
|
|
30
|
+
"@dynamic-labs/ethereum": "0.19.0-alpha.21",
|
|
31
|
+
"@dynamic-labs/wallet-connector-core": "0.19.0-alpha.21"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"viem": "^1.1.0"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
6
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
7
|
+
var BloctoEvm = require('./BloctoEvm.cjs');
|
|
8
|
+
|
|
9
|
+
class BloctoEmailConnector extends BloctoEvm.BloctoEvm {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
this.canConnectViaEmail = true;
|
|
13
|
+
}
|
|
14
|
+
get email() {
|
|
15
|
+
return this._email;
|
|
16
|
+
}
|
|
17
|
+
setEmail(email) {
|
|
18
|
+
this._email = email;
|
|
19
|
+
if (!email) {
|
|
20
|
+
localStorage.removeItem(BloctoEmailConnector.storageEmailKey);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
localStorage.setItem(BloctoEmailConnector.storageEmailKey, email);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
fetchPublicAddress() {
|
|
27
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
try {
|
|
29
|
+
const client = this.getClient();
|
|
30
|
+
if (!(client === null || client === void 0 ? void 0 : client.ethereum)) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const addresses = yield client.ethereum.request({
|
|
34
|
+
method: 'eth_requestAccounts',
|
|
35
|
+
// When the email is provided, the login modal will prefill the email field.
|
|
36
|
+
params: this.email ? [this.email] : undefined,
|
|
37
|
+
});
|
|
38
|
+
if ((addresses === null || addresses === void 0 ? void 0 : addresses.length) > 0) {
|
|
39
|
+
return addresses[0];
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
walletConnectorCore.logger.error(e);
|
|
45
|
+
return Promise.reject(e);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
getConnectedAccounts() {
|
|
50
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const client = this.getClient();
|
|
52
|
+
if (!client) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
if (client.ethereum) {
|
|
56
|
+
const accounts = yield client.ethereum.request({
|
|
57
|
+
method: 'eth_requestAccounts',
|
|
58
|
+
// When the email is provided, the login modal will prefill the email field.
|
|
59
|
+
params: this.email ? [this.email] : undefined,
|
|
60
|
+
});
|
|
61
|
+
return accounts || [];
|
|
62
|
+
}
|
|
63
|
+
return [];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
clearEmail() {
|
|
67
|
+
this.setEmail(undefined);
|
|
68
|
+
}
|
|
69
|
+
endSession() {
|
|
70
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const client = this.getClient();
|
|
72
|
+
localStorage.removeItem('sdk.session');
|
|
73
|
+
if (this.email && // cancel the session only if the email is set
|
|
74
|
+
client &&
|
|
75
|
+
client.ethereum &&
|
|
76
|
+
this.checkForSessionAccounts()) {
|
|
77
|
+
yield client.ethereum.request({ method: 'wallet_disconnect' });
|
|
78
|
+
this.removeClient();
|
|
79
|
+
}
|
|
80
|
+
this.clearEmail();
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
BloctoEmailConnector.storageEmailKey = 'blocto-email';
|
|
85
|
+
|
|
86
|
+
exports.BloctoEmailConnector = BloctoEmailConnector;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IEmailWalletConnector } from '@dynamic-labs/wallet-connector-core';
|
|
2
|
+
import { BloctoEvm } from './BloctoEvm';
|
|
3
|
+
export declare class BloctoEmailConnector extends BloctoEvm implements IEmailWalletConnector {
|
|
4
|
+
canConnectViaEmail: boolean;
|
|
5
|
+
protected _email: string | undefined | null;
|
|
6
|
+
get email(): string | null | undefined;
|
|
7
|
+
setEmail(email: BloctoEmailConnector['email']): void;
|
|
8
|
+
fetchPublicAddress(): Promise<`0x${string}` | undefined>;
|
|
9
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
10
|
+
clearEmail(): void;
|
|
11
|
+
endSession(): Promise<void>;
|
|
12
|
+
static storageEmailKey: string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
import { logger } from '@dynamic-labs/wallet-connector-core';
|
|
3
|
+
import { BloctoEvm } from './BloctoEvm.js';
|
|
4
|
+
|
|
5
|
+
class BloctoEmailConnector extends BloctoEvm {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.canConnectViaEmail = true;
|
|
9
|
+
}
|
|
10
|
+
get email() {
|
|
11
|
+
return this._email;
|
|
12
|
+
}
|
|
13
|
+
setEmail(email) {
|
|
14
|
+
this._email = email;
|
|
15
|
+
if (!email) {
|
|
16
|
+
localStorage.removeItem(BloctoEmailConnector.storageEmailKey);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
localStorage.setItem(BloctoEmailConnector.storageEmailKey, email);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
fetchPublicAddress() {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
try {
|
|
25
|
+
const client = this.getClient();
|
|
26
|
+
if (!(client === null || client === void 0 ? void 0 : client.ethereum)) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
const addresses = yield client.ethereum.request({
|
|
30
|
+
method: 'eth_requestAccounts',
|
|
31
|
+
// When the email is provided, the login modal will prefill the email field.
|
|
32
|
+
params: this.email ? [this.email] : undefined,
|
|
33
|
+
});
|
|
34
|
+
if ((addresses === null || addresses === void 0 ? void 0 : addresses.length) > 0) {
|
|
35
|
+
return addresses[0];
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
logger.error(e);
|
|
41
|
+
return Promise.reject(e);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getConnectedAccounts() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const client = this.getClient();
|
|
48
|
+
if (!client) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
if (client.ethereum) {
|
|
52
|
+
const accounts = yield client.ethereum.request({
|
|
53
|
+
method: 'eth_requestAccounts',
|
|
54
|
+
// When the email is provided, the login modal will prefill the email field.
|
|
55
|
+
params: this.email ? [this.email] : undefined,
|
|
56
|
+
});
|
|
57
|
+
return accounts || [];
|
|
58
|
+
}
|
|
59
|
+
return [];
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
clearEmail() {
|
|
63
|
+
this.setEmail(undefined);
|
|
64
|
+
}
|
|
65
|
+
endSession() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const client = this.getClient();
|
|
68
|
+
localStorage.removeItem('sdk.session');
|
|
69
|
+
if (this.email && // cancel the session only if the email is set
|
|
70
|
+
client &&
|
|
71
|
+
client.ethereum &&
|
|
72
|
+
this.checkForSessionAccounts()) {
|
|
73
|
+
yield client.ethereum.request({ method: 'wallet_disconnect' });
|
|
74
|
+
this.removeClient();
|
|
75
|
+
}
|
|
76
|
+
this.clearEmail();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
BloctoEmailConnector.storageEmailKey = 'blocto-email';
|
|
81
|
+
|
|
82
|
+
export { BloctoEmailConnector };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _tslib = require('../_virtual/_tslib.cjs');
|
|
6
|
+
var BloctoSDK = require('@blocto/sdk');
|
|
7
|
+
var viem = require('viem');
|
|
8
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
var ethereum = require('@dynamic-labs/ethereum');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var BloctoSDK__default = /*#__PURE__*/_interopDefaultLegacy(BloctoSDK);
|
|
14
|
+
|
|
15
|
+
class BloctoEvm extends ethereum.EthWalletConnector {
|
|
16
|
+
constructor(props) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
super(props);
|
|
19
|
+
this.LOCAL_STORAGE_KEY = 'dynamic_blocto_evm_chain_id';
|
|
20
|
+
this.SESSION_STORAGE_BLOCTO_KEY = 'BLOCTO_SDK';
|
|
21
|
+
this.name = 'bloctoevm';
|
|
22
|
+
this.canConnectViaCustodialService = true;
|
|
23
|
+
this.connectedChain = 'EVM';
|
|
24
|
+
this.supportedChains = ['EVM', 'ETH'];
|
|
25
|
+
this.supportsNetworkSwitching = () => false;
|
|
26
|
+
// Taken from : https://docs.blocto.app/blocto-sdk/javascript-sdk/evm-sdk/provider
|
|
27
|
+
const BLOCTO_SUPPORTED_CHAIN_IDS = [1, 4, 56, 97, 137, 80001, 43114, 43113];
|
|
28
|
+
this.evmNetworks = this.evmNetworks.filter((n) => BLOCTO_SUPPORTED_CHAIN_IDS.includes(n.chainId));
|
|
29
|
+
this.chainId =
|
|
30
|
+
Number(localStorage.getItem(this.LOCAL_STORAGE_KEY)) ||
|
|
31
|
+
((_a = this.evmNetworks.find((n) => n.chainId === 137)) === null || _a === void 0 ? void 0 : _a.chainId) ||
|
|
32
|
+
((_b = this.evmNetworks[0]) === null || _b === void 0 ? void 0 : _b.chainId);
|
|
33
|
+
if (this.evmNetworks.length > 0) {
|
|
34
|
+
this.initClient();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Check if there are already any accounts in the session
|
|
38
|
+
// Blocto in version > 0.5.0 started to store the accounts in the session storage
|
|
39
|
+
checkForSessionAccounts() {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
try {
|
|
42
|
+
const sessionValue = sessionStorage.getItem(this.SESSION_STORAGE_BLOCTO_KEY);
|
|
43
|
+
const parsedSessionValue = JSON.parse(sessionValue);
|
|
44
|
+
return (((_a = parsedSessionValue === null || parsedSessionValue === void 0 ? void 0 : parsedSessionValue.data) === null || _a === void 0 ? void 0 : _a.accounts) &&
|
|
45
|
+
Object.keys((_b = parsedSessionValue === null || parsedSessionValue === void 0 ? void 0 : parsedSessionValue.data) === null || _b === void 0 ? void 0 : _b.accounts).length > 0);
|
|
46
|
+
}
|
|
47
|
+
catch (_c) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
initClient() {
|
|
52
|
+
if (!this.client) {
|
|
53
|
+
const network = this.evmNetworks.find((n) => n.chainId === this.chainId);
|
|
54
|
+
if (network) {
|
|
55
|
+
this.client = new BloctoSDK__default["default"]({
|
|
56
|
+
ethereum: {
|
|
57
|
+
chainId: `0x${this.chainId.toString(16)}`,
|
|
58
|
+
rpc: network.rpcUrls[0],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
walletConnectorCore.logger.error("Couldn't find a configured network for Blocto EVM");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
getClient() {
|
|
68
|
+
return this.client;
|
|
69
|
+
}
|
|
70
|
+
removeClient() {
|
|
71
|
+
this.client = undefined;
|
|
72
|
+
}
|
|
73
|
+
getWalletClient() {
|
|
74
|
+
const client = this.getClient();
|
|
75
|
+
if ((client === null || client === void 0 ? void 0 : client.ethereum) && this.checkForSessionAccounts()) {
|
|
76
|
+
return viem.createWalletClient({ transport: viem.custom(client.ethereum) });
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// We are calling the client directly and not via Web3Provider because if we will wrap it in Ethers,
|
|
83
|
+
// Ethers will be making retries on failure. This becomes an issue on the connect step.
|
|
84
|
+
// When Blocto's modal closes prior to receiving a response with the address on the connect step,
|
|
85
|
+
// then ethers would retry and the modal will open again.
|
|
86
|
+
fetchPublicAddress() {
|
|
87
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
try {
|
|
89
|
+
const client = this.getClient();
|
|
90
|
+
if (!(client === null || client === void 0 ? void 0 : client.ethereum)) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const addresses = yield client.ethereum.request({
|
|
94
|
+
method: 'eth_requestAccounts',
|
|
95
|
+
});
|
|
96
|
+
if ((addresses === null || addresses === void 0 ? void 0 : addresses.length) > 0) {
|
|
97
|
+
return addresses[0];
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
walletConnectorCore.logger.error(e);
|
|
103
|
+
return Promise.reject(e);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
signMessage(messageToSign) {
|
|
108
|
+
var _a;
|
|
109
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
try {
|
|
111
|
+
const account = yield this.fetchPublicAddress();
|
|
112
|
+
if (!account) {
|
|
113
|
+
return Promise.resolve(undefined);
|
|
114
|
+
}
|
|
115
|
+
return yield ((_a = this.getWalletClient()) === null || _a === void 0 ? void 0 : _a.signMessage({
|
|
116
|
+
account,
|
|
117
|
+
message: messageToSign,
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
if (e.message === 'User declined the signing request') {
|
|
122
|
+
return Promise.reject({ code: '4001' });
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return Promise.reject(e);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
endSession() {
|
|
131
|
+
var _a;
|
|
132
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
localStorage.removeItem('sdk.session');
|
|
134
|
+
if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.ethereum) && this.checkForSessionAccounts()) {
|
|
135
|
+
yield this.client.ethereum.request({ method: 'wallet_disconnect' });
|
|
136
|
+
this.removeClient();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
exports.BloctoEvm = BloctoEvm;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import BloctoSDK from '@blocto/sdk';
|
|
2
|
+
import { Hex, WalletClient } from 'viem';
|
|
3
|
+
import { Chain } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
import { EthWalletConnector, EthWalletConnectorOpts } from '@dynamic-labs/ethereum';
|
|
5
|
+
export declare class BloctoEvm extends EthWalletConnector {
|
|
6
|
+
LOCAL_STORAGE_KEY: string;
|
|
7
|
+
SESSION_STORAGE_BLOCTO_KEY: string;
|
|
8
|
+
protected client?: BloctoSDK;
|
|
9
|
+
name: string;
|
|
10
|
+
canConnectViaCustodialService: boolean;
|
|
11
|
+
connectedChain: Chain;
|
|
12
|
+
supportedChains: Chain[];
|
|
13
|
+
chainId: number;
|
|
14
|
+
supportsNetworkSwitching: () => boolean;
|
|
15
|
+
constructor(props: EthWalletConnectorOpts);
|
|
16
|
+
protected checkForSessionAccounts(): boolean;
|
|
17
|
+
protected initClient(): void;
|
|
18
|
+
protected getClient(): BloctoSDK | undefined;
|
|
19
|
+
protected removeClient(): void;
|
|
20
|
+
getWalletClient(): WalletClient | undefined;
|
|
21
|
+
fetchPublicAddress(): Promise<Hex | undefined>;
|
|
22
|
+
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
23
|
+
endSession(): Promise<void>;
|
|
24
|
+
}
|
package/src/BloctoEvm.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
+
import BloctoSDK from '@blocto/sdk';
|
|
3
|
+
import { createWalletClient, custom } from 'viem';
|
|
4
|
+
import { logger } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
import { EthWalletConnector } from '@dynamic-labs/ethereum';
|
|
6
|
+
|
|
7
|
+
class BloctoEvm extends EthWalletConnector {
|
|
8
|
+
constructor(props) {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
super(props);
|
|
11
|
+
this.LOCAL_STORAGE_KEY = 'dynamic_blocto_evm_chain_id';
|
|
12
|
+
this.SESSION_STORAGE_BLOCTO_KEY = 'BLOCTO_SDK';
|
|
13
|
+
this.name = 'bloctoevm';
|
|
14
|
+
this.canConnectViaCustodialService = true;
|
|
15
|
+
this.connectedChain = 'EVM';
|
|
16
|
+
this.supportedChains = ['EVM', 'ETH'];
|
|
17
|
+
this.supportsNetworkSwitching = () => false;
|
|
18
|
+
// Taken from : https://docs.blocto.app/blocto-sdk/javascript-sdk/evm-sdk/provider
|
|
19
|
+
const BLOCTO_SUPPORTED_CHAIN_IDS = [1, 4, 56, 97, 137, 80001, 43114, 43113];
|
|
20
|
+
this.evmNetworks = this.evmNetworks.filter((n) => BLOCTO_SUPPORTED_CHAIN_IDS.includes(n.chainId));
|
|
21
|
+
this.chainId =
|
|
22
|
+
Number(localStorage.getItem(this.LOCAL_STORAGE_KEY)) ||
|
|
23
|
+
((_a = this.evmNetworks.find((n) => n.chainId === 137)) === null || _a === void 0 ? void 0 : _a.chainId) ||
|
|
24
|
+
((_b = this.evmNetworks[0]) === null || _b === void 0 ? void 0 : _b.chainId);
|
|
25
|
+
if (this.evmNetworks.length > 0) {
|
|
26
|
+
this.initClient();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Check if there are already any accounts in the session
|
|
30
|
+
// Blocto in version > 0.5.0 started to store the accounts in the session storage
|
|
31
|
+
checkForSessionAccounts() {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
try {
|
|
34
|
+
const sessionValue = sessionStorage.getItem(this.SESSION_STORAGE_BLOCTO_KEY);
|
|
35
|
+
const parsedSessionValue = JSON.parse(sessionValue);
|
|
36
|
+
return (((_a = parsedSessionValue === null || parsedSessionValue === void 0 ? void 0 : parsedSessionValue.data) === null || _a === void 0 ? void 0 : _a.accounts) &&
|
|
37
|
+
Object.keys((_b = parsedSessionValue === null || parsedSessionValue === void 0 ? void 0 : parsedSessionValue.data) === null || _b === void 0 ? void 0 : _b.accounts).length > 0);
|
|
38
|
+
}
|
|
39
|
+
catch (_c) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
initClient() {
|
|
44
|
+
if (!this.client) {
|
|
45
|
+
const network = this.evmNetworks.find((n) => n.chainId === this.chainId);
|
|
46
|
+
if (network) {
|
|
47
|
+
this.client = new BloctoSDK({
|
|
48
|
+
ethereum: {
|
|
49
|
+
chainId: `0x${this.chainId.toString(16)}`,
|
|
50
|
+
rpc: network.rpcUrls[0],
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
logger.error("Couldn't find a configured network for Blocto EVM");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
getClient() {
|
|
60
|
+
return this.client;
|
|
61
|
+
}
|
|
62
|
+
removeClient() {
|
|
63
|
+
this.client = undefined;
|
|
64
|
+
}
|
|
65
|
+
getWalletClient() {
|
|
66
|
+
const client = this.getClient();
|
|
67
|
+
if ((client === null || client === void 0 ? void 0 : client.ethereum) && this.checkForSessionAccounts()) {
|
|
68
|
+
return createWalletClient({ transport: custom(client.ethereum) });
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// We are calling the client directly and not via Web3Provider because if we will wrap it in Ethers,
|
|
75
|
+
// Ethers will be making retries on failure. This becomes an issue on the connect step.
|
|
76
|
+
// When Blocto's modal closes prior to receiving a response with the address on the connect step,
|
|
77
|
+
// then ethers would retry and the modal will open again.
|
|
78
|
+
fetchPublicAddress() {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
try {
|
|
81
|
+
const client = this.getClient();
|
|
82
|
+
if (!(client === null || client === void 0 ? void 0 : client.ethereum)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const addresses = yield client.ethereum.request({
|
|
86
|
+
method: 'eth_requestAccounts',
|
|
87
|
+
});
|
|
88
|
+
if ((addresses === null || addresses === void 0 ? void 0 : addresses.length) > 0) {
|
|
89
|
+
return addresses[0];
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
logger.error(e);
|
|
95
|
+
return Promise.reject(e);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
signMessage(messageToSign) {
|
|
100
|
+
var _a;
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
try {
|
|
103
|
+
const account = yield this.fetchPublicAddress();
|
|
104
|
+
if (!account) {
|
|
105
|
+
return Promise.resolve(undefined);
|
|
106
|
+
}
|
|
107
|
+
return yield ((_a = this.getWalletClient()) === null || _a === void 0 ? void 0 : _a.signMessage({
|
|
108
|
+
account,
|
|
109
|
+
message: messageToSign,
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
if (e.message === 'User declined the signing request') {
|
|
114
|
+
return Promise.reject({ code: '4001' });
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return Promise.reject(e);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
endSession() {
|
|
123
|
+
var _a;
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
localStorage.removeItem('sdk.session');
|
|
126
|
+
if (((_a = this.client) === null || _a === void 0 ? void 0 : _a.ethereum) && this.checkForSessionAccounts()) {
|
|
127
|
+
yield this.client.ethereum.request({ method: 'wallet_disconnect' });
|
|
128
|
+
this.removeClient();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { BloctoEvm };
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var BloctoEvm = require('./BloctoEvm.cjs');
|
|
6
|
+
var BloctoEmailConnector = require('./BloctoEmailConnector.cjs');
|
|
7
|
+
|
|
8
|
+
const BloctoEvmWalletConnectors = (
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
|
+
_props) => [
|
|
11
|
+
BloctoEvm.BloctoEvm,
|
|
12
|
+
...(_props.apiProviders.blocto ? [BloctoEmailConnector.BloctoEmailConnector] : []),
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
exports.BloctoEvm = BloctoEvm.BloctoEvm;
|
|
16
|
+
exports.BloctoEmailConnector = BloctoEmailConnector.BloctoEmailConnector;
|
|
17
|
+
exports.BloctoEvmWalletConnectors = BloctoEvmWalletConnectors;
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BloctoEvm } from './BloctoEvm.js';
|
|
2
|
+
export { BloctoEvm } from './BloctoEvm.js';
|
|
3
|
+
import { BloctoEmailConnector } from './BloctoEmailConnector.js';
|
|
4
|
+
export { BloctoEmailConnector } from './BloctoEmailConnector.js';
|
|
5
|
+
|
|
6
|
+
const BloctoEvmWalletConnectors = (
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
+
_props) => [
|
|
9
|
+
BloctoEvm,
|
|
10
|
+
...(_props.apiProviders.blocto ? [BloctoEmailConnector] : []),
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
export { BloctoEvmWalletConnectors };
|