@dynamic-labs/cosmos 3.0.0-alpha.46 → 3.0.0-alpha.47
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 +25 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.json +6 -2
- package/src/connectors/CosmosWalletConnector.cjs +75 -0
- package/src/connectors/CosmosWalletConnector.d.ts +38 -0
- package/src/connectors/CosmosWalletConnector.js +71 -0
- package/src/connectors/KeplrWalletConnector.cjs +123 -0
- package/src/connectors/KeplrWalletConnector.d.ts +45 -0
- package/src/connectors/KeplrWalletConnector.js +118 -0
- package/src/connectors/index.d.ts +2 -0
- package/src/index.cjs +5 -4
- package/src/index.d.ts +2 -1
- package/src/index.js +5 -4
- package/src/types.d.ts +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
|
|
2
|
+
## [3.0.0-alpha.47](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.46...v3.0.0-alpha.47) (2024-08-21)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### ⚠ BREAKING CHANGES
|
|
6
|
+
|
|
7
|
+
* rename useSelectWalletOption to useWalletOptions (#6616)
|
|
8
|
+
* stop exposing walletConnectorOptions from dynamic context (#6622)
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* add support for base.eth ens resolution ([#6655](https://github.com/dynamic-labs/DynamicAuth/issues/6655)) ([e2fb1c0](https://github.com/dynamic-labs/DynamicAuth/commit/e2fb1c0ca8410e85b7464d032abc6686bd46149f))
|
|
13
|
+
* expose wallet options from useWalletOptions hook ([#6624](https://github.com/dynamic-labs/DynamicAuth/issues/6624)) ([7287359](https://github.com/dynamic-labs/DynamicAuth/commit/7287359d1564564b87e82bf182edef8ef02c16f1))
|
|
14
|
+
* global wallet chain switching ([#6648](https://github.com/dynamic-labs/DynamicAuth/issues/6648)) ([10179fc](https://github.com/dynamic-labs/DynamicAuth/commit/10179fcddc747977c7dd250abd78bad0ec375f8f))
|
|
15
|
+
* implement Apple Sign-In for mobile ([#6632](https://github.com/dynamic-labs/DynamicAuth/issues/6632)) ([e68b088](https://github.com/dynamic-labs/DynamicAuth/commit/e68b088b1c13c1140b43a6d0c215383455a71a4a))
|
|
16
|
+
* surface mfa rate limit error ([#6658](https://github.com/dynamic-labs/DynamicAuth/issues/6658)) ([5fbcbc2](https://github.com/dynamic-labs/DynamicAuth/commit/5fbcbc2382ebcf2721a308d8e87bc8b2b5cb38cf))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* update bottom nav border radius ([#6651](https://github.com/dynamic-labs/DynamicAuth/issues/6651)) ([79212c2](https://github.com/dynamic-labs/DynamicAuth/commit/79212c26735eb06f31655e8b98a3d9c2c8371489))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
* rename useSelectWalletOption to useWalletOptions ([#6616](https://github.com/dynamic-labs/DynamicAuth/issues/6616)) ([3850dad](https://github.com/dynamic-labs/DynamicAuth/commit/3850dad47f847ace84aadc63a84c4e62f84f7df2))
|
|
25
|
+
* stop exposing walletConnectorOptions from dynamic context ([#6622](https://github.com/dynamic-labs/DynamicAuth/issues/6622)) ([b9da7db](https://github.com/dynamic-labs/DynamicAuth/commit/b9da7dbfc4020913ba3d95ec496811e9d8ee17b8))
|
|
26
|
+
|
|
2
27
|
## [3.0.0-alpha.46](https://github.com/dynamic-labs/DynamicAuth/compare/v3.0.0-alpha.45...v3.0.0-alpha.46) (2024-08-19)
|
|
3
28
|
|
|
4
29
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Copyright (c) Microsoft Corporation.
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
10
|
+
purpose with or without fee is hereby granted.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
13
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
14
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
15
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
16
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
17
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
+
***************************************************************************** */
|
|
20
|
+
|
|
21
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
32
|
+
var e = new Error(message);
|
|
33
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.__awaiter = __awaiter;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/******************************************************************************
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
***************************************************************************** */
|
|
16
|
+
|
|
17
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
28
|
+
var e = new Error(message);
|
|
29
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { __awaiter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/cosmos",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.47",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/dynamic-labs/dynamic-auth.git",
|
|
@@ -26,7 +26,11 @@
|
|
|
26
26
|
"./package.json": "./package.json"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@
|
|
29
|
+
"@keplr-wallet/types": "0.12.121",
|
|
30
|
+
"@dynamic-labs/types": "3.0.0-alpha.47",
|
|
31
|
+
"@dynamic-labs/utils": "3.0.0-alpha.47",
|
|
32
|
+
"@dynamic-labs/wallet-book": "3.0.0-alpha.47",
|
|
33
|
+
"@dynamic-labs/wallet-connector-core": "3.0.0-alpha.47"
|
|
30
34
|
},
|
|
31
35
|
"peerDependencies": {}
|
|
32
36
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var utils = require('@dynamic-labs/utils');
|
|
8
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
9
|
+
|
|
10
|
+
class CosmosWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
super(opts);
|
|
13
|
+
this.connectedChain = 'COSMOS';
|
|
14
|
+
this.supportedChains = ['COSMOS'];
|
|
15
|
+
this.chainIdMap = {
|
|
16
|
+
401: 'cosmoshub-4',
|
|
17
|
+
402: 'axelar-dojo-1',
|
|
18
|
+
403: 'osmosis-1',
|
|
19
|
+
};
|
|
20
|
+
this.reverseChainIdMap = {
|
|
21
|
+
'axelar-dojo-1': 402,
|
|
22
|
+
'cosmoshub-4': 401,
|
|
23
|
+
'osmosis-1': 403,
|
|
24
|
+
};
|
|
25
|
+
this.evmNetworks = utils.parseEvmNetworks(opts.cosmosNetworks);
|
|
26
|
+
}
|
|
27
|
+
getSelectedNetwork() {
|
|
28
|
+
const selectedNetwork = this.evmNetworks.find((network) => network.networkId === this.reverseChainIdMap[this.getChainId()]);
|
|
29
|
+
return selectedNetwork;
|
|
30
|
+
}
|
|
31
|
+
getNetwork() {
|
|
32
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
return this.reverseChainIdMap[this.getChainId()];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
getLcdUrl() {
|
|
37
|
+
var _a;
|
|
38
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.lcdUrl;
|
|
39
|
+
}
|
|
40
|
+
getDenom() {
|
|
41
|
+
var _a;
|
|
42
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.nativeCurrency.denom;
|
|
43
|
+
}
|
|
44
|
+
getBalance(address) {
|
|
45
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
try {
|
|
47
|
+
const response = yield fetch(`${this.getLcdUrl()}/cosmos/bank/v1beta1/balances/${address}`).then((res) => res.json());
|
|
48
|
+
const balance = response.balances.reduce((acc, cur) => {
|
|
49
|
+
if (cur.denom === this.getDenom()) {
|
|
50
|
+
return Number(cur.amount) / 1e6;
|
|
51
|
+
}
|
|
52
|
+
return acc;
|
|
53
|
+
}, 0);
|
|
54
|
+
return balance.toString();
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
walletConnectorCore.logger.error(e);
|
|
58
|
+
return '0';
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getConnectedAccounts() {
|
|
63
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const address = yield this.getAddress();
|
|
65
|
+
return [address];
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
endSession() {
|
|
69
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
this.setChainId(null);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.CosmosWalletConnector = CosmosWalletConnector;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { EvmNetwork, GenericNetwork } from '@dynamic-labs/types';
|
|
2
|
+
import { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
3
|
+
import { Chain, WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
|
|
4
|
+
import { CosmoNetwork } from '../types';
|
|
5
|
+
export interface IFetchBalanceResponse {
|
|
6
|
+
balances: IFetchBalanceBalance[];
|
|
7
|
+
pagination: IFetchBalancePagination;
|
|
8
|
+
}
|
|
9
|
+
export interface IFetchBalanceBalance {
|
|
10
|
+
amount: string;
|
|
11
|
+
denom: string;
|
|
12
|
+
}
|
|
13
|
+
export interface IFetchBalancePagination {
|
|
14
|
+
next_key: any;
|
|
15
|
+
total: string;
|
|
16
|
+
}
|
|
17
|
+
export type CosmosWalletConnectorOpts = {
|
|
18
|
+
cosmosNetworks: GenericNetwork[];
|
|
19
|
+
walletBook: WalletBookSchema;
|
|
20
|
+
};
|
|
21
|
+
export declare abstract class CosmosWalletConnector extends WalletConnectorBase {
|
|
22
|
+
evmNetworks: EvmNetwork[];
|
|
23
|
+
connectedChain: Chain;
|
|
24
|
+
supportedChains: Chain[];
|
|
25
|
+
chainIdMap: Record<number, CosmoNetwork>;
|
|
26
|
+
reverseChainIdMap: Record<CosmoNetwork, number>;
|
|
27
|
+
constructor(opts: CosmosWalletConnectorOpts);
|
|
28
|
+
abstract getChainId(): CosmoNetwork;
|
|
29
|
+
abstract setChainId(chainId: CosmoNetwork | null): void;
|
|
30
|
+
getSelectedNetwork(): EvmNetwork | undefined;
|
|
31
|
+
getNetwork(): Promise<number>;
|
|
32
|
+
getLcdUrl(): string | undefined;
|
|
33
|
+
getDenom(): string | undefined;
|
|
34
|
+
getBalance(address: string): Promise<string | undefined>;
|
|
35
|
+
abstract getAddress(): Promise<string>;
|
|
36
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
37
|
+
endSession(): Promise<void>;
|
|
38
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { parseEvmNetworks } from '@dynamic-labs/utils';
|
|
4
|
+
import { WalletConnectorBase, logger } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
|
|
6
|
+
class CosmosWalletConnector extends WalletConnectorBase {
|
|
7
|
+
constructor(opts) {
|
|
8
|
+
super(opts);
|
|
9
|
+
this.connectedChain = 'COSMOS';
|
|
10
|
+
this.supportedChains = ['COSMOS'];
|
|
11
|
+
this.chainIdMap = {
|
|
12
|
+
401: 'cosmoshub-4',
|
|
13
|
+
402: 'axelar-dojo-1',
|
|
14
|
+
403: 'osmosis-1',
|
|
15
|
+
};
|
|
16
|
+
this.reverseChainIdMap = {
|
|
17
|
+
'axelar-dojo-1': 402,
|
|
18
|
+
'cosmoshub-4': 401,
|
|
19
|
+
'osmosis-1': 403,
|
|
20
|
+
};
|
|
21
|
+
this.evmNetworks = parseEvmNetworks(opts.cosmosNetworks);
|
|
22
|
+
}
|
|
23
|
+
getSelectedNetwork() {
|
|
24
|
+
const selectedNetwork = this.evmNetworks.find((network) => network.networkId === this.reverseChainIdMap[this.getChainId()]);
|
|
25
|
+
return selectedNetwork;
|
|
26
|
+
}
|
|
27
|
+
getNetwork() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
return this.reverseChainIdMap[this.getChainId()];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
getLcdUrl() {
|
|
33
|
+
var _a;
|
|
34
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.lcdUrl;
|
|
35
|
+
}
|
|
36
|
+
getDenom() {
|
|
37
|
+
var _a;
|
|
38
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.nativeCurrency.denom;
|
|
39
|
+
}
|
|
40
|
+
getBalance(address) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
try {
|
|
43
|
+
const response = yield fetch(`${this.getLcdUrl()}/cosmos/bank/v1beta1/balances/${address}`).then((res) => res.json());
|
|
44
|
+
const balance = response.balances.reduce((acc, cur) => {
|
|
45
|
+
if (cur.denom === this.getDenom()) {
|
|
46
|
+
return Number(cur.amount) / 1e6;
|
|
47
|
+
}
|
|
48
|
+
return acc;
|
|
49
|
+
}, 0);
|
|
50
|
+
return balance.toString();
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
logger.error(e);
|
|
54
|
+
return '0';
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getConnectedAccounts() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const address = yield this.getAddress();
|
|
61
|
+
return [address];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
endSession() {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
this.setChainId(null);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { CosmosWalletConnector };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../_virtual/_tslib.cjs');
|
|
7
|
+
var CosmosWalletConnector = require('./CosmosWalletConnector.cjs');
|
|
8
|
+
|
|
9
|
+
const DYNAMIC_KEPLR_NETWORK_ID = 'dynamic_keplr_network_id';
|
|
10
|
+
class KeplrWalletConnector extends CosmosWalletConnector.CosmosWalletConnector {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
super(opts);
|
|
13
|
+
this.switchNetworkOnlyFromWallet = true;
|
|
14
|
+
this.name = 'Keplr';
|
|
15
|
+
this.overrideKey = 'keplr';
|
|
16
|
+
this._handleAccountChange = this._handleAccountChange.bind(this);
|
|
17
|
+
}
|
|
18
|
+
getChainId() {
|
|
19
|
+
const defaultChainId = 'cosmoshub-4';
|
|
20
|
+
const storedChainId = localStorage.getItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
21
|
+
return storedChainId !== null && storedChainId !== void 0 ? storedChainId : defaultChainId;
|
|
22
|
+
}
|
|
23
|
+
setChainId(chainId) {
|
|
24
|
+
if (!chainId) {
|
|
25
|
+
localStorage.removeItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
localStorage.setItem(DYNAMIC_KEPLR_NETWORK_ID, chainId);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
getWalletClient() {
|
|
32
|
+
return this.keplr;
|
|
33
|
+
}
|
|
34
|
+
getAccount() {
|
|
35
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const accountsList = yield this.keplr
|
|
37
|
+
.getOfflineSigner(this.getChainId())
|
|
38
|
+
.getAccounts();
|
|
39
|
+
if (!accountsList)
|
|
40
|
+
throw new Error('No accounts found');
|
|
41
|
+
const [account] = accountsList;
|
|
42
|
+
return account;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
get keplr() {
|
|
46
|
+
if (typeof window === 'undefined')
|
|
47
|
+
throw new Error('Keplr is not available');
|
|
48
|
+
// @ts-expect-error keplr is not defined if extension is not installed
|
|
49
|
+
return window.keplr;
|
|
50
|
+
}
|
|
51
|
+
connect() {
|
|
52
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const chainIdsToConnect = this.evmNetworks.map((network) => this.chainIdMap[network.chainId]);
|
|
54
|
+
yield this.keplr.enable(chainIdsToConnect);
|
|
55
|
+
this.setChainId(this.chainIdMap[this.evmNetworks[0].chainId]);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getAddress() {
|
|
59
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
yield this.connect();
|
|
61
|
+
const account = yield this.getAccount();
|
|
62
|
+
return account.address;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getSigner() {
|
|
66
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
return this.keplr.getOfflineSigner(this.getChainId());
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
isInstalledOnBrowser() {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
72
|
+
// @ts-ignore window.keplr is provided by extension
|
|
73
|
+
return typeof window.keplr !== 'undefined';
|
|
74
|
+
}
|
|
75
|
+
_handleAccountChange() {
|
|
76
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
const address = yield this.getAddress();
|
|
78
|
+
this.emit('accountChange', { accounts: [address] });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
setupEventListeners() {
|
|
82
|
+
window.addEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
83
|
+
}
|
|
84
|
+
teardownEventListeners() {
|
|
85
|
+
window.removeEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
86
|
+
this.setChainId(null);
|
|
87
|
+
}
|
|
88
|
+
// SignDoc used for signAmin method;
|
|
89
|
+
getSignDoc(message) {
|
|
90
|
+
return {
|
|
91
|
+
account_number: '0',
|
|
92
|
+
chain_id: this.getChainId(),
|
|
93
|
+
fee: { amount: [], gas: '0' },
|
|
94
|
+
memo: '',
|
|
95
|
+
msgs: [{ type: 'custom/MsgSignText', value: { text: message } }],
|
|
96
|
+
sequence: '0',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
signMessage(messageToSign) {
|
|
100
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const signDoc = this.getSignDoc(messageToSign);
|
|
102
|
+
const address = yield this.getAddress();
|
|
103
|
+
const signatureResponse = yield this.keplr.signAmino(this.getChainId(), address, signDoc);
|
|
104
|
+
if (!signatureResponse)
|
|
105
|
+
throw new Error('Could not sign message');
|
|
106
|
+
return JSON.stringify(signatureResponse);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
supportsNetworkSwitching() {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
switchNetwork(_a) {
|
|
113
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ networkChainId, }) {
|
|
114
|
+
if (!networkChainId)
|
|
115
|
+
return;
|
|
116
|
+
this.setChainId(this.chainIdMap[networkChainId]);
|
|
117
|
+
this.emit('chainChange', { chain: networkChainId.toString() });
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
exports.DYNAMIC_KEPLR_NETWORK_ID = DYNAMIC_KEPLR_NETWORK_ID;
|
|
123
|
+
exports.KeplrWalletConnector = KeplrWalletConnector;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Keplr as KeplrWallet } from '@keplr-wallet/types';
|
|
2
|
+
import { CosmoNetwork } from '../types';
|
|
3
|
+
import { CosmosWalletConnector, CosmosWalletConnectorOpts } from './CosmosWalletConnector';
|
|
4
|
+
export type SwitchNetworkOps = {
|
|
5
|
+
networkChainId?: number;
|
|
6
|
+
networkName?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const DYNAMIC_KEPLR_NETWORK_ID = "dynamic_keplr_network_id";
|
|
9
|
+
export declare class KeplrWalletConnector extends CosmosWalletConnector {
|
|
10
|
+
switchNetworkOnlyFromWallet: boolean;
|
|
11
|
+
name: string;
|
|
12
|
+
overrideKey: string;
|
|
13
|
+
constructor(opts: CosmosWalletConnectorOpts);
|
|
14
|
+
getChainId(): CosmoNetwork;
|
|
15
|
+
setChainId(chainId: CosmoNetwork | null): void;
|
|
16
|
+
getWalletClient(): KeplrWallet;
|
|
17
|
+
protected getAccount(): Promise<import("@keplr-wallet/types").AccountData>;
|
|
18
|
+
get keplr(): KeplrWallet;
|
|
19
|
+
connect(): Promise<void>;
|
|
20
|
+
getAddress(): Promise<string>;
|
|
21
|
+
getSigner(): Promise<import("@keplr-wallet/types").OfflineAminoSigner & import("@keplr-wallet/types").OfflineDirectSigner>;
|
|
22
|
+
isInstalledOnBrowser(): boolean;
|
|
23
|
+
_handleAccountChange(): Promise<void>;
|
|
24
|
+
setupEventListeners(): void;
|
|
25
|
+
teardownEventListeners(): void;
|
|
26
|
+
protected getSignDoc(message: string): {
|
|
27
|
+
account_number: string;
|
|
28
|
+
chain_id: CosmoNetwork;
|
|
29
|
+
fee: {
|
|
30
|
+
amount: never[];
|
|
31
|
+
gas: string;
|
|
32
|
+
};
|
|
33
|
+
memo: string;
|
|
34
|
+
msgs: {
|
|
35
|
+
type: string;
|
|
36
|
+
value: {
|
|
37
|
+
text: string;
|
|
38
|
+
};
|
|
39
|
+
}[];
|
|
40
|
+
sequence: string;
|
|
41
|
+
};
|
|
42
|
+
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
43
|
+
supportsNetworkSwitching(): boolean;
|
|
44
|
+
switchNetwork({ networkChainId, }: SwitchNetworkOps): Promise<void>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { CosmosWalletConnector } from './CosmosWalletConnector.js';
|
|
4
|
+
|
|
5
|
+
const DYNAMIC_KEPLR_NETWORK_ID = 'dynamic_keplr_network_id';
|
|
6
|
+
class KeplrWalletConnector extends CosmosWalletConnector {
|
|
7
|
+
constructor(opts) {
|
|
8
|
+
super(opts);
|
|
9
|
+
this.switchNetworkOnlyFromWallet = true;
|
|
10
|
+
this.name = 'Keplr';
|
|
11
|
+
this.overrideKey = 'keplr';
|
|
12
|
+
this._handleAccountChange = this._handleAccountChange.bind(this);
|
|
13
|
+
}
|
|
14
|
+
getChainId() {
|
|
15
|
+
const defaultChainId = 'cosmoshub-4';
|
|
16
|
+
const storedChainId = localStorage.getItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
17
|
+
return storedChainId !== null && storedChainId !== void 0 ? storedChainId : defaultChainId;
|
|
18
|
+
}
|
|
19
|
+
setChainId(chainId) {
|
|
20
|
+
if (!chainId) {
|
|
21
|
+
localStorage.removeItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
localStorage.setItem(DYNAMIC_KEPLR_NETWORK_ID, chainId);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
getWalletClient() {
|
|
28
|
+
return this.keplr;
|
|
29
|
+
}
|
|
30
|
+
getAccount() {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
const accountsList = yield this.keplr
|
|
33
|
+
.getOfflineSigner(this.getChainId())
|
|
34
|
+
.getAccounts();
|
|
35
|
+
if (!accountsList)
|
|
36
|
+
throw new Error('No accounts found');
|
|
37
|
+
const [account] = accountsList;
|
|
38
|
+
return account;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
get keplr() {
|
|
42
|
+
if (typeof window === 'undefined')
|
|
43
|
+
throw new Error('Keplr is not available');
|
|
44
|
+
// @ts-expect-error keplr is not defined if extension is not installed
|
|
45
|
+
return window.keplr;
|
|
46
|
+
}
|
|
47
|
+
connect() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const chainIdsToConnect = this.evmNetworks.map((network) => this.chainIdMap[network.chainId]);
|
|
50
|
+
yield this.keplr.enable(chainIdsToConnect);
|
|
51
|
+
this.setChainId(this.chainIdMap[this.evmNetworks[0].chainId]);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
getAddress() {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
yield this.connect();
|
|
57
|
+
const account = yield this.getAccount();
|
|
58
|
+
return account.address;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
getSigner() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
return this.keplr.getOfflineSigner(this.getChainId());
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
isInstalledOnBrowser() {
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
68
|
+
// @ts-ignore window.keplr is provided by extension
|
|
69
|
+
return typeof window.keplr !== 'undefined';
|
|
70
|
+
}
|
|
71
|
+
_handleAccountChange() {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const address = yield this.getAddress();
|
|
74
|
+
this.emit('accountChange', { accounts: [address] });
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
setupEventListeners() {
|
|
78
|
+
window.addEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
79
|
+
}
|
|
80
|
+
teardownEventListeners() {
|
|
81
|
+
window.removeEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
82
|
+
this.setChainId(null);
|
|
83
|
+
}
|
|
84
|
+
// SignDoc used for signAmin method;
|
|
85
|
+
getSignDoc(message) {
|
|
86
|
+
return {
|
|
87
|
+
account_number: '0',
|
|
88
|
+
chain_id: this.getChainId(),
|
|
89
|
+
fee: { amount: [], gas: '0' },
|
|
90
|
+
memo: '',
|
|
91
|
+
msgs: [{ type: 'custom/MsgSignText', value: { text: message } }],
|
|
92
|
+
sequence: '0',
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
signMessage(messageToSign) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const signDoc = this.getSignDoc(messageToSign);
|
|
98
|
+
const address = yield this.getAddress();
|
|
99
|
+
const signatureResponse = yield this.keplr.signAmino(this.getChainId(), address, signDoc);
|
|
100
|
+
if (!signatureResponse)
|
|
101
|
+
throw new Error('Could not sign message');
|
|
102
|
+
return JSON.stringify(signatureResponse);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
supportsNetworkSwitching() {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
switchNetwork(_a) {
|
|
109
|
+
return __awaiter(this, arguments, void 0, function* ({ networkChainId, }) {
|
|
110
|
+
if (!networkChainId)
|
|
111
|
+
return;
|
|
112
|
+
this.setChainId(this.chainIdMap[networkChainId]);
|
|
113
|
+
this.emit('chainChange', { chain: networkChainId.toString() });
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { DYNAMIC_KEPLR_NETWORK_ID, KeplrWalletConnector };
|
package/src/index.cjs
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var KeplrWalletConnector = require('./connectors/KeplrWalletConnector.cjs');
|
|
7
|
+
require('../_virtual/_tslib.cjs');
|
|
8
|
+
require('@dynamic-labs/utils');
|
|
9
|
+
require('@dynamic-labs/wallet-connector-core');
|
|
7
10
|
|
|
8
|
-
const CosmosWalletConnectors = (
|
|
9
|
-
...keplr.KeplrWalletConnectors(props),
|
|
10
|
-
];
|
|
11
|
+
const CosmosWalletConnectors = () => [KeplrWalletConnector.KeplrWalletConnector];
|
|
11
12
|
|
|
12
13
|
exports.CosmosWalletConnectors = CosmosWalletConnectors;
|
package/src/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { KeplrWalletConnector } from './connectors';
|
|
2
|
+
export declare const CosmosWalletConnectors: () => (typeof KeplrWalletConnector)[];
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use client'
|
|
2
|
-
import {
|
|
2
|
+
import { KeplrWalletConnector } from './connectors/KeplrWalletConnector.js';
|
|
3
|
+
import '../_virtual/_tslib.js';
|
|
4
|
+
import '@dynamic-labs/utils';
|
|
5
|
+
import '@dynamic-labs/wallet-connector-core';
|
|
3
6
|
|
|
4
|
-
const CosmosWalletConnectors = (
|
|
5
|
-
...KeplrWalletConnectors(props),
|
|
6
|
-
];
|
|
7
|
+
const CosmosWalletConnectors = () => [KeplrWalletConnector];
|
|
7
8
|
|
|
8
9
|
export { CosmosWalletConnectors };
|