@dynamic-labs/cosmos 3.0.0-alpha.8 → 3.0.0
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 +622 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.json +6 -2
- package/src/connectors/CosmosWalletConnector.cjs +73 -0
- package/src/connectors/CosmosWalletConnector.d.ts +47 -0
- package/src/connectors/CosmosWalletConnector.js +69 -0
- package/src/connectors/KeplrWalletConnector.cjs +138 -0
- package/src/connectors/KeplrWalletConnector.d.ts +45 -0
- package/src/connectors/KeplrWalletConnector.js +133 -0
- package/src/connectors/index.d.ts +2 -0
- package/src/index.cjs +9 -4
- package/src/index.d.ts +5 -1
- package/src/index.js +7 -4
- package/src/types.d.ts +7 -0
- package/src/wallet/CosmosWallet.cjs +73 -0
- package/src/wallet/CosmosWallet.d.ts +7 -0
- package/src/wallet/CosmosWallet.js +69 -0
- package/src/wallet/index.d.ts +2 -0
- package/src/wallet/isCosmosWallet/index.d.ts +1 -0
- package/src/wallet/isCosmosWallet/isCosmosWallet.cjs +8 -0
- package/src/wallet/isCosmosWallet/isCosmosWallet.d.ts +3 -0
- package/src/wallet/isCosmosWallet/isCosmosWallet.js +4 -0
|
@@ -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
|
|
3
|
+
"version": "3.0.0",
|
|
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",
|
|
31
|
+
"@dynamic-labs/utils": "3.0.0",
|
|
32
|
+
"@dynamic-labs/wallet-book": "3.0.0",
|
|
33
|
+
"@dynamic-labs/wallet-connector-core": "3.0.0"
|
|
30
34
|
},
|
|
31
35
|
"peerDependencies": {}
|
|
32
36
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
var CosmosWallet = require('../wallet/CosmosWallet.cjs');
|
|
10
|
+
|
|
11
|
+
class CosmosWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
super(opts);
|
|
14
|
+
this.ChainWallet = CosmosWallet.CosmosWallet;
|
|
15
|
+
this.connectedChain = 'COSMOS';
|
|
16
|
+
this.supportedChains = ['COSMOS'];
|
|
17
|
+
this.evmNetworks = utils.parseEvmNetworks(opts.cosmosNetworks);
|
|
18
|
+
this.chainIdMapping = this.evmNetworks.map((network) => ({
|
|
19
|
+
cosmosNetworkId: network.name,
|
|
20
|
+
dynamicChainId: utils.parseChainId(network.networkId),
|
|
21
|
+
evmNetwork: network,
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
getSelectedNetwork() {
|
|
25
|
+
const selectedNetwork = this.chainIdMapping.find((mapping) => mapping.cosmosNetworkId === this.getChainId());
|
|
26
|
+
return selectedNetwork === null || selectedNetwork === void 0 ? void 0 : selectedNetwork.evmNetwork;
|
|
27
|
+
}
|
|
28
|
+
getNetwork() {
|
|
29
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.chainId;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
getLcdUrl() {
|
|
35
|
+
var _a;
|
|
36
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.lcdUrl;
|
|
37
|
+
}
|
|
38
|
+
getDenom() {
|
|
39
|
+
var _a;
|
|
40
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.nativeCurrency.denom;
|
|
41
|
+
}
|
|
42
|
+
getBalance(address) {
|
|
43
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
try {
|
|
45
|
+
const response = yield fetch(`${this.getLcdUrl()}/cosmos/bank/v1beta1/balances/${address}`).then((res) => res.json());
|
|
46
|
+
const balance = response.balances.reduce((acc, cur) => {
|
|
47
|
+
if (cur.denom === this.getDenom()) {
|
|
48
|
+
return Number(cur.amount) / 1e6;
|
|
49
|
+
}
|
|
50
|
+
return acc;
|
|
51
|
+
}, 0);
|
|
52
|
+
return balance.toString();
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
walletConnectorCore.logger.error(e);
|
|
56
|
+
return '0';
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
getConnectedAccounts() {
|
|
61
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
const address = yield this.getAddress();
|
|
63
|
+
return [address];
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
endSession() {
|
|
67
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
this.setChainId(null);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
exports.CosmosWalletConnector = CosmosWalletConnector;
|
|
@@ -0,0 +1,47 @@
|
|
|
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 { ICosmosOfflineSigner, ICosmosProvider } from '../types';
|
|
5
|
+
import { CosmosWallet } from '../wallet';
|
|
6
|
+
export interface IFetchBalanceResponse {
|
|
7
|
+
balances: IFetchBalanceBalance[];
|
|
8
|
+
pagination: IFetchBalancePagination;
|
|
9
|
+
}
|
|
10
|
+
export interface IFetchBalanceBalance {
|
|
11
|
+
amount: string;
|
|
12
|
+
denom: string;
|
|
13
|
+
}
|
|
14
|
+
export interface IFetchBalancePagination {
|
|
15
|
+
next_key: any;
|
|
16
|
+
total: string;
|
|
17
|
+
}
|
|
18
|
+
export type CosmosWalletConnectorOpts = {
|
|
19
|
+
cosmosNetworks: GenericNetwork[];
|
|
20
|
+
walletBook: WalletBookSchema;
|
|
21
|
+
};
|
|
22
|
+
type CosmosChainIDMapObject = {
|
|
23
|
+
dynamicChainId: number;
|
|
24
|
+
cosmosNetworkId: string;
|
|
25
|
+
evmNetwork: EvmNetwork;
|
|
26
|
+
};
|
|
27
|
+
export declare abstract class CosmosWalletConnector extends WalletConnectorBase {
|
|
28
|
+
evmNetworks: EvmNetwork[];
|
|
29
|
+
ChainWallet: typeof CosmosWallet;
|
|
30
|
+
connectedChain: Chain;
|
|
31
|
+
supportedChains: Chain[];
|
|
32
|
+
chainIdMapping: CosmosChainIDMapObject[];
|
|
33
|
+
constructor(opts: CosmosWalletConnectorOpts);
|
|
34
|
+
abstract getChainId(): string;
|
|
35
|
+
abstract setChainId(chainId: string | null): void;
|
|
36
|
+
abstract getProvider(): Promise<ICosmosProvider | undefined>;
|
|
37
|
+
abstract getOfflineSigner(): Promise<ICosmosOfflineSigner>;
|
|
38
|
+
getSelectedNetwork(): EvmNetwork | undefined;
|
|
39
|
+
getNetwork(): Promise<number | undefined>;
|
|
40
|
+
getLcdUrl(): string | undefined;
|
|
41
|
+
getDenom(): string | undefined;
|
|
42
|
+
getBalance(address: string): Promise<string | undefined>;
|
|
43
|
+
abstract getAddress(): Promise<string>;
|
|
44
|
+
getConnectedAccounts(): Promise<string[]>;
|
|
45
|
+
endSession(): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../_virtual/_tslib.js';
|
|
3
|
+
import { parseEvmNetworks, parseChainId } from '@dynamic-labs/utils';
|
|
4
|
+
import { WalletConnectorBase, logger } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
import { CosmosWallet } from '../wallet/CosmosWallet.js';
|
|
6
|
+
|
|
7
|
+
class CosmosWalletConnector extends WalletConnectorBase {
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
super(opts);
|
|
10
|
+
this.ChainWallet = CosmosWallet;
|
|
11
|
+
this.connectedChain = 'COSMOS';
|
|
12
|
+
this.supportedChains = ['COSMOS'];
|
|
13
|
+
this.evmNetworks = parseEvmNetworks(opts.cosmosNetworks);
|
|
14
|
+
this.chainIdMapping = this.evmNetworks.map((network) => ({
|
|
15
|
+
cosmosNetworkId: network.name,
|
|
16
|
+
dynamicChainId: parseChainId(network.networkId),
|
|
17
|
+
evmNetwork: network,
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
getSelectedNetwork() {
|
|
21
|
+
const selectedNetwork = this.chainIdMapping.find((mapping) => mapping.cosmosNetworkId === this.getChainId());
|
|
22
|
+
return selectedNetwork === null || selectedNetwork === void 0 ? void 0 : selectedNetwork.evmNetwork;
|
|
23
|
+
}
|
|
24
|
+
getNetwork() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.chainId;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
getLcdUrl() {
|
|
31
|
+
var _a;
|
|
32
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.lcdUrl;
|
|
33
|
+
}
|
|
34
|
+
getDenom() {
|
|
35
|
+
var _a;
|
|
36
|
+
return (_a = this.getSelectedNetwork()) === null || _a === void 0 ? void 0 : _a.nativeCurrency.denom;
|
|
37
|
+
}
|
|
38
|
+
getBalance(address) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
try {
|
|
41
|
+
const response = yield fetch(`${this.getLcdUrl()}/cosmos/bank/v1beta1/balances/${address}`).then((res) => res.json());
|
|
42
|
+
const balance = response.balances.reduce((acc, cur) => {
|
|
43
|
+
if (cur.denom === this.getDenom()) {
|
|
44
|
+
return Number(cur.amount) / 1e6;
|
|
45
|
+
}
|
|
46
|
+
return acc;
|
|
47
|
+
}, 0);
|
|
48
|
+
return balance.toString();
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
logger.error(e);
|
|
52
|
+
return '0';
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
getConnectedAccounts() {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
const address = yield this.getAddress();
|
|
59
|
+
return [address];
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
endSession() {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
this.setChainId(null);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { CosmosWalletConnector };
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
getDefaultChainId() {
|
|
19
|
+
if (this.chainIdMapping.length === 0) {
|
|
20
|
+
return 'cosmoshub-4';
|
|
21
|
+
}
|
|
22
|
+
if (this.chainIdMapping.find((chain) => chain.cosmosNetworkId === 'cosmoshub-4')) {
|
|
23
|
+
return 'cosmoshub-4';
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return this.chainIdMapping[0].cosmosNetworkId;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
getChainId() {
|
|
30
|
+
const defaultChainId = this.getDefaultChainId();
|
|
31
|
+
const storedChainId = localStorage.getItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
32
|
+
return storedChainId !== null && storedChainId !== void 0 ? storedChainId : defaultChainId;
|
|
33
|
+
}
|
|
34
|
+
setChainId(chainId) {
|
|
35
|
+
if (!chainId) {
|
|
36
|
+
localStorage.removeItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
localStorage.setItem(DYNAMIC_KEPLR_NETWORK_ID, chainId);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getAccount() {
|
|
43
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const accountsList = yield this.keplr
|
|
45
|
+
.getOfflineSigner(this.getChainId())
|
|
46
|
+
.getAccounts();
|
|
47
|
+
if (!accountsList)
|
|
48
|
+
throw new Error('No accounts found');
|
|
49
|
+
const [account] = accountsList;
|
|
50
|
+
return account;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
get keplr() {
|
|
54
|
+
if (typeof window === 'undefined')
|
|
55
|
+
throw new Error('Keplr is not available');
|
|
56
|
+
// @ts-expect-error keplr is not defined if extension is not installed
|
|
57
|
+
return window.keplr;
|
|
58
|
+
}
|
|
59
|
+
connect() {
|
|
60
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
yield this.keplr.enable(this.chainIdMapping.map((mapping) => mapping.cosmosNetworkId));
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
getAddress() {
|
|
65
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
yield this.connect();
|
|
67
|
+
const account = yield this.getAccount();
|
|
68
|
+
return account.address;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
getProvider() {
|
|
72
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
return this.keplr;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
getOfflineSigner() {
|
|
77
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
return this.keplr.getOfflineSigner(this.getChainId());
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
isInstalledOnBrowser() {
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
83
|
+
// @ts-ignore window.keplr is provided by extension
|
|
84
|
+
return typeof window.keplr !== 'undefined';
|
|
85
|
+
}
|
|
86
|
+
_handleAccountChange() {
|
|
87
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const address = yield this.getAddress();
|
|
89
|
+
this.emit('accountChange', { accounts: [address] });
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
setupEventListeners() {
|
|
93
|
+
window.addEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
94
|
+
}
|
|
95
|
+
teardownEventListeners() {
|
|
96
|
+
window.removeEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
97
|
+
this.setChainId(null);
|
|
98
|
+
}
|
|
99
|
+
// SignDoc used for signAmin method;
|
|
100
|
+
getSignDoc(message) {
|
|
101
|
+
return {
|
|
102
|
+
account_number: '0',
|
|
103
|
+
chain_id: this.getChainId(),
|
|
104
|
+
fee: { amount: [], gas: '0' },
|
|
105
|
+
memo: '',
|
|
106
|
+
msgs: [{ type: 'custom/MsgSignText', value: { text: message } }],
|
|
107
|
+
sequence: '0',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
signMessage(messageToSign) {
|
|
111
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
const signDoc = this.getSignDoc(messageToSign);
|
|
113
|
+
const address = yield this.getAddress();
|
|
114
|
+
const signatureResponse = yield this.keplr.signAmino(this.getChainId(), address, signDoc);
|
|
115
|
+
if (!signatureResponse)
|
|
116
|
+
throw new Error('Could not sign message');
|
|
117
|
+
return JSON.stringify(signatureResponse);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
supportsNetworkSwitching() {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
switchNetwork(_a) {
|
|
124
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ networkChainId, }) {
|
|
125
|
+
if (!networkChainId)
|
|
126
|
+
return;
|
|
127
|
+
this.chainIdMapping.forEach((mapping) => {
|
|
128
|
+
if (mapping.dynamicChainId === networkChainId) {
|
|
129
|
+
this.setChainId(mapping.cosmosNetworkId);
|
|
130
|
+
this.emit('chainChange', { chain: networkChainId.toString() });
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
exports.DYNAMIC_KEPLR_NETWORK_ID = DYNAMIC_KEPLR_NETWORK_ID;
|
|
138
|
+
exports.KeplrWalletConnector = KeplrWalletConnector;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Keplr as KeplrWallet } from '@keplr-wallet/types';
|
|
2
|
+
import { CosmosWalletConnector, CosmosWalletConnectorOpts } from './CosmosWalletConnector';
|
|
3
|
+
export type SwitchNetworkOps = {
|
|
4
|
+
networkChainId?: number;
|
|
5
|
+
networkName?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const DYNAMIC_KEPLR_NETWORK_ID = "dynamic_keplr_network_id";
|
|
8
|
+
export declare class KeplrWalletConnector extends CosmosWalletConnector {
|
|
9
|
+
switchNetworkOnlyFromWallet: boolean;
|
|
10
|
+
name: string;
|
|
11
|
+
overrideKey: string;
|
|
12
|
+
constructor(opts: CosmosWalletConnectorOpts);
|
|
13
|
+
getDefaultChainId(): string;
|
|
14
|
+
getChainId(): string;
|
|
15
|
+
setChainId(chainId: string | null): void;
|
|
16
|
+
protected getAccount(): Promise<import("@keplr-wallet/types").AccountData>;
|
|
17
|
+
get keplr(): KeplrWallet;
|
|
18
|
+
connect(): Promise<void>;
|
|
19
|
+
getAddress(): Promise<string>;
|
|
20
|
+
getProvider(): Promise<KeplrWallet>;
|
|
21
|
+
getOfflineSigner(): 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: string;
|
|
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,133 @@
|
|
|
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
|
+
getDefaultChainId() {
|
|
15
|
+
if (this.chainIdMapping.length === 0) {
|
|
16
|
+
return 'cosmoshub-4';
|
|
17
|
+
}
|
|
18
|
+
if (this.chainIdMapping.find((chain) => chain.cosmosNetworkId === 'cosmoshub-4')) {
|
|
19
|
+
return 'cosmoshub-4';
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return this.chainIdMapping[0].cosmosNetworkId;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
getChainId() {
|
|
26
|
+
const defaultChainId = this.getDefaultChainId();
|
|
27
|
+
const storedChainId = localStorage.getItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
28
|
+
return storedChainId !== null && storedChainId !== void 0 ? storedChainId : defaultChainId;
|
|
29
|
+
}
|
|
30
|
+
setChainId(chainId) {
|
|
31
|
+
if (!chainId) {
|
|
32
|
+
localStorage.removeItem(DYNAMIC_KEPLR_NETWORK_ID);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
localStorage.setItem(DYNAMIC_KEPLR_NETWORK_ID, chainId);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
getAccount() {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const accountsList = yield this.keplr
|
|
41
|
+
.getOfflineSigner(this.getChainId())
|
|
42
|
+
.getAccounts();
|
|
43
|
+
if (!accountsList)
|
|
44
|
+
throw new Error('No accounts found');
|
|
45
|
+
const [account] = accountsList;
|
|
46
|
+
return account;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
get keplr() {
|
|
50
|
+
if (typeof window === 'undefined')
|
|
51
|
+
throw new Error('Keplr is not available');
|
|
52
|
+
// @ts-expect-error keplr is not defined if extension is not installed
|
|
53
|
+
return window.keplr;
|
|
54
|
+
}
|
|
55
|
+
connect() {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
yield this.keplr.enable(this.chainIdMapping.map((mapping) => mapping.cosmosNetworkId));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
getAddress() {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
yield this.connect();
|
|
63
|
+
const account = yield this.getAccount();
|
|
64
|
+
return account.address;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
getProvider() {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
return this.keplr;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
getOfflineSigner() {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
return this.keplr.getOfflineSigner(this.getChainId());
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
isInstalledOnBrowser() {
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
79
|
+
// @ts-ignore window.keplr is provided by extension
|
|
80
|
+
return typeof window.keplr !== 'undefined';
|
|
81
|
+
}
|
|
82
|
+
_handleAccountChange() {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const address = yield this.getAddress();
|
|
85
|
+
this.emit('accountChange', { accounts: [address] });
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
setupEventListeners() {
|
|
89
|
+
window.addEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
90
|
+
}
|
|
91
|
+
teardownEventListeners() {
|
|
92
|
+
window.removeEventListener('keplr_keystorechange', this._handleAccountChange);
|
|
93
|
+
this.setChainId(null);
|
|
94
|
+
}
|
|
95
|
+
// SignDoc used for signAmin method;
|
|
96
|
+
getSignDoc(message) {
|
|
97
|
+
return {
|
|
98
|
+
account_number: '0',
|
|
99
|
+
chain_id: this.getChainId(),
|
|
100
|
+
fee: { amount: [], gas: '0' },
|
|
101
|
+
memo: '',
|
|
102
|
+
msgs: [{ type: 'custom/MsgSignText', value: { text: message } }],
|
|
103
|
+
sequence: '0',
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
signMessage(messageToSign) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const signDoc = this.getSignDoc(messageToSign);
|
|
109
|
+
const address = yield this.getAddress();
|
|
110
|
+
const signatureResponse = yield this.keplr.signAmino(this.getChainId(), address, signDoc);
|
|
111
|
+
if (!signatureResponse)
|
|
112
|
+
throw new Error('Could not sign message');
|
|
113
|
+
return JSON.stringify(signatureResponse);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
supportsNetworkSwitching() {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
switchNetwork(_a) {
|
|
120
|
+
return __awaiter(this, arguments, void 0, function* ({ networkChainId, }) {
|
|
121
|
+
if (!networkChainId)
|
|
122
|
+
return;
|
|
123
|
+
this.chainIdMapping.forEach((mapping) => {
|
|
124
|
+
if (mapping.dynamicChainId === networkChainId) {
|
|
125
|
+
this.setChainId(mapping.cosmosNetworkId);
|
|
126
|
+
this.emit('chainChange', { chain: networkChainId.toString() });
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { DYNAMIC_KEPLR_NETWORK_ID, KeplrWalletConnector };
|
package/src/index.cjs
CHANGED
|
@@ -3,10 +3,15 @@
|
|
|
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');
|
|
10
|
+
var CosmosWallet = require('./wallet/CosmosWallet.cjs');
|
|
11
|
+
var isCosmosWallet = require('./wallet/isCosmosWallet/isCosmosWallet.cjs');
|
|
7
12
|
|
|
8
|
-
const CosmosWalletConnectors = (
|
|
9
|
-
...keplr.KeplrWalletConnectors(props),
|
|
10
|
-
];
|
|
13
|
+
const CosmosWalletConnectors = () => [KeplrWalletConnector.KeplrWalletConnector];
|
|
11
14
|
|
|
15
|
+
exports.CosmosWallet = CosmosWallet.CosmosWallet;
|
|
16
|
+
exports.isCosmosWallet = isCosmosWallet.isCosmosWallet;
|
|
12
17
|
exports.CosmosWalletConnectors = CosmosWalletConnectors;
|
package/src/index.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { KeplrWalletConnector } from './connectors';
|
|
2
|
+
export * from './wallet';
|
|
3
|
+
export { type ICosmosOfflineSigner, type ICosmosProvider } from './types';
|
|
4
|
+
export { type CosmosWalletConnector } from './connectors/CosmosWalletConnector';
|
|
5
|
+
export declare const CosmosWalletConnectors: () => (typeof KeplrWalletConnector)[];
|
package/src/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
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';
|
|
6
|
+
export { CosmosWallet } from './wallet/CosmosWallet.js';
|
|
7
|
+
export { isCosmosWallet } from './wallet/isCosmosWallet/isCosmosWallet.js';
|
|
3
8
|
|
|
4
|
-
const CosmosWalletConnectors = (
|
|
5
|
-
...KeplrWalletConnectors(props),
|
|
6
|
-
];
|
|
9
|
+
const CosmosWalletConnectors = () => [KeplrWalletConnector];
|
|
7
10
|
|
|
8
11
|
export { CosmosWalletConnectors };
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Window as KeplrWindow, type Keplr as KeplrWallet, type OfflineAminoSigner, type OfflineDirectSigner } from '@keplr-wallet/types';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window extends KeplrWindow {
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
export type ICosmosOfflineSigner = OfflineAminoSigner & OfflineDirectSigner;
|
|
7
|
+
export type ICosmosProvider = KeplrWallet;
|