@kheopskit/core 1.0.0 → 4.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/MIGRATING_TO_V4.md +259 -0
- package/README.md +67 -0
- package/dist/chunk-4RBYRNY3.mjs +164 -0
- package/dist/chunk-4RBYRNY3.mjs.map +1 -0
- package/dist/chunk-BWUUHUDK.mjs +24 -0
- package/dist/chunk-BWUUHUDK.mjs.map +1 -0
- package/dist/chunk-D3EQMFZ2.js +24 -0
- package/dist/chunk-D3EQMFZ2.js.map +1 -0
- package/dist/chunk-FIAL4HTE.js +1 -0
- package/dist/chunk-FIAL4HTE.js.map +1 -0
- package/dist/chunk-KWFQDD7E.mjs +578 -0
- package/dist/chunk-KWFQDD7E.mjs.map +1 -0
- package/dist/chunk-NU46D4MZ.js +578 -0
- package/dist/chunk-NU46D4MZ.js.map +1 -0
- package/dist/chunk-PNPPI5CH.mjs +201 -0
- package/dist/chunk-PNPPI5CH.mjs.map +1 -0
- package/dist/chunk-SIUWQBT4.js +201 -0
- package/dist/chunk-SIUWQBT4.js.map +1 -0
- package/dist/chunk-TMAPQWW2.js +164 -0
- package/dist/chunk-TMAPQWW2.js.map +1 -0
- package/dist/chunk-YFD3IKK5.mjs +1 -0
- package/dist/chunk-YFD3IKK5.mjs.map +1 -0
- package/dist/ethereum.d.mts +60 -0
- package/dist/ethereum.d.ts +60 -0
- package/dist/ethereum.js +332 -0
- package/dist/ethereum.js.map +1 -0
- package/dist/ethereum.mjs +332 -0
- package/dist/ethereum.mjs.map +1 -0
- package/dist/getCachedObservable-C4E8dfMp.d.mts +20 -0
- package/dist/getCachedObservable-C4E8dfMp.d.ts +20 -0
- package/dist/index.d.mts +44 -270
- package/dist/index.d.ts +44 -270
- package/dist/index.js +160 -1394
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +119 -1387
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +86 -0
- package/dist/internal.d.ts +86 -0
- package/dist/internal.js +32 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.mjs +32 -0
- package/dist/internal.mjs.map +1 -0
- package/dist/polkadot.d.mts +69 -0
- package/dist/polkadot.d.ts +69 -0
- package/dist/polkadot.js +314 -0
- package/dist/polkadot.js.map +1 -0
- package/dist/polkadot.mjs +314 -0
- package/dist/polkadot.mjs.map +1 -0
- package/dist/solana.d.mts +97 -0
- package/dist/solana.d.ts +97 -0
- package/dist/solana.js +466 -0
- package/dist/solana.js.map +1 -0
- package/dist/solana.mjs +466 -0
- package/dist/solana.mjs.map +1 -0
- package/dist/types-BNzRUNw-.d.mts +319 -0
- package/dist/types-BNzRUNw-.d.ts +319 -0
- package/package.json +76 -16
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
acceptsCachedAccount,
|
|
3
|
+
getCachedIcon,
|
|
4
|
+
hydrateAccount,
|
|
5
|
+
hydrateWallet,
|
|
6
|
+
serializeAccount,
|
|
7
|
+
serializeWallet,
|
|
8
|
+
setCachedIcons,
|
|
9
|
+
sortAccounts,
|
|
10
|
+
sortWallets
|
|
11
|
+
} from "./chunk-4RBYRNY3.mjs";
|
|
12
|
+
import "./chunk-BWUUHUDK.mjs";
|
|
13
|
+
import {
|
|
14
|
+
clearCachedObservable,
|
|
15
|
+
clearCachedObservablesByPrefix,
|
|
16
|
+
getSafeLocalStorage
|
|
17
|
+
} from "./chunk-PNPPI5CH.mjs";
|
|
18
|
+
export {
|
|
19
|
+
acceptsCachedAccount,
|
|
20
|
+
clearCachedObservable,
|
|
21
|
+
clearCachedObservablesByPrefix,
|
|
22
|
+
getCachedIcon,
|
|
23
|
+
getSafeLocalStorage,
|
|
24
|
+
hydrateAccount,
|
|
25
|
+
hydrateWallet,
|
|
26
|
+
serializeAccount,
|
|
27
|
+
serializeWallet,
|
|
28
|
+
setCachedIcons,
|
|
29
|
+
sortAccounts,
|
|
30
|
+
sortWallets
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=internal.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { i as PolkadotAccountType, W as WalletAccountId, a as WalletId, j as PolkadotAppKitWallet, K as KheopskitPlatform } from './types-BNzRUNw-.mjs';
|
|
2
|
+
import { InjectedPolkadotAccount, InjectedExtension } from 'polkadot-api/pjs-signer';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if `address` is a valid SS58-encoded 32-byte account id (the
|
|
7
|
+
* only form Substrate wallets inject), verifying the blake2b-512 checksum.
|
|
8
|
+
*
|
|
9
|
+
* Dependency-free (base58 via @scure/base, blake2b via @noble/hashes) so core
|
|
10
|
+
* stays decoupled from polkadot-api.
|
|
11
|
+
*/
|
|
12
|
+
declare const isSs58Address: (address: string) => boolean;
|
|
13
|
+
|
|
14
|
+
type PolkadotInjectedWallet = {
|
|
15
|
+
id: WalletId;
|
|
16
|
+
platform: "polkadot";
|
|
17
|
+
type: "injected";
|
|
18
|
+
/**
|
|
19
|
+
* Stable identifier of the underlying wallet source. For Polkadot this is the
|
|
20
|
+
* injected extension identifier. Named consistently across platforms
|
|
21
|
+
* (Ethereum: EIP-6963 rdns; Solana: Wallet Standard name).
|
|
22
|
+
*/
|
|
23
|
+
sourceId: string;
|
|
24
|
+
extension: InjectedExtension | undefined;
|
|
25
|
+
name: string;
|
|
26
|
+
icon: string;
|
|
27
|
+
isConnected: boolean;
|
|
28
|
+
connect: () => Promise<void>;
|
|
29
|
+
disconnect: () => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
type PolkadotWallet = PolkadotInjectedWallet | PolkadotAppKitWallet;
|
|
32
|
+
/**
|
|
33
|
+
* A Polkadot account. Inherits the fields of polkadot-api's
|
|
34
|
+
* `InjectedPolkadotAccount` — notably the signing surface **`polkadotSigner`**
|
|
35
|
+
* (a `PolkadotSigner`) — and narrows `type` to {@link PolkadotAccountType}.
|
|
36
|
+
*
|
|
37
|
+
* Per-platform signing surfaces differ: Ethereum exposes `client` (viem),
|
|
38
|
+
* Solana `signer`/`getSigner(chain)`. `polkadotSigner` is absent while
|
|
39
|
+
* `state.isHydrating` is `true`.
|
|
40
|
+
*/
|
|
41
|
+
type PolkadotAccount = Omit<InjectedPolkadotAccount, "type"> & {
|
|
42
|
+
type: PolkadotAccountType;
|
|
43
|
+
id: WalletAccountId;
|
|
44
|
+
platform: "polkadot";
|
|
45
|
+
walletName: string;
|
|
46
|
+
walletId: WalletId;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type PolkadotPluginOptions = {
|
|
50
|
+
/**
|
|
51
|
+
* Allowed Polkadot account key types. Accounts with other key types are
|
|
52
|
+
* filtered out from kheopskit state.
|
|
53
|
+
*
|
|
54
|
+
* @default ["sr25519", "ed25519", "ecdsa"]
|
|
55
|
+
*/
|
|
56
|
+
accountTypes?: PolkadotAccountType[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Polkadot platform plugin. Pass to `getKheopskit$({ platforms: [polkadot()] })`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { polkadot } from "@kheopskit/core/polkadot";
|
|
64
|
+
* polkadot({ accountTypes: ["sr25519", "ed25519", "ecdsa"] });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare const polkadot: (options?: PolkadotPluginOptions) => KheopskitPlatform<"polkadot", PolkadotWallet, PolkadotAccount>;
|
|
68
|
+
|
|
69
|
+
export { type PolkadotAccount, PolkadotAccountType, PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotPluginOptions, type PolkadotWallet, isSs58Address, polkadot };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { i as PolkadotAccountType, W as WalletAccountId, a as WalletId, j as PolkadotAppKitWallet, K as KheopskitPlatform } from './types-BNzRUNw-.js';
|
|
2
|
+
import { InjectedPolkadotAccount, InjectedExtension } from 'polkadot-api/pjs-signer';
|
|
3
|
+
import 'rxjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if `address` is a valid SS58-encoded 32-byte account id (the
|
|
7
|
+
* only form Substrate wallets inject), verifying the blake2b-512 checksum.
|
|
8
|
+
*
|
|
9
|
+
* Dependency-free (base58 via @scure/base, blake2b via @noble/hashes) so core
|
|
10
|
+
* stays decoupled from polkadot-api.
|
|
11
|
+
*/
|
|
12
|
+
declare const isSs58Address: (address: string) => boolean;
|
|
13
|
+
|
|
14
|
+
type PolkadotInjectedWallet = {
|
|
15
|
+
id: WalletId;
|
|
16
|
+
platform: "polkadot";
|
|
17
|
+
type: "injected";
|
|
18
|
+
/**
|
|
19
|
+
* Stable identifier of the underlying wallet source. For Polkadot this is the
|
|
20
|
+
* injected extension identifier. Named consistently across platforms
|
|
21
|
+
* (Ethereum: EIP-6963 rdns; Solana: Wallet Standard name).
|
|
22
|
+
*/
|
|
23
|
+
sourceId: string;
|
|
24
|
+
extension: InjectedExtension | undefined;
|
|
25
|
+
name: string;
|
|
26
|
+
icon: string;
|
|
27
|
+
isConnected: boolean;
|
|
28
|
+
connect: () => Promise<void>;
|
|
29
|
+
disconnect: () => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
type PolkadotWallet = PolkadotInjectedWallet | PolkadotAppKitWallet;
|
|
32
|
+
/**
|
|
33
|
+
* A Polkadot account. Inherits the fields of polkadot-api's
|
|
34
|
+
* `InjectedPolkadotAccount` — notably the signing surface **`polkadotSigner`**
|
|
35
|
+
* (a `PolkadotSigner`) — and narrows `type` to {@link PolkadotAccountType}.
|
|
36
|
+
*
|
|
37
|
+
* Per-platform signing surfaces differ: Ethereum exposes `client` (viem),
|
|
38
|
+
* Solana `signer`/`getSigner(chain)`. `polkadotSigner` is absent while
|
|
39
|
+
* `state.isHydrating` is `true`.
|
|
40
|
+
*/
|
|
41
|
+
type PolkadotAccount = Omit<InjectedPolkadotAccount, "type"> & {
|
|
42
|
+
type: PolkadotAccountType;
|
|
43
|
+
id: WalletAccountId;
|
|
44
|
+
platform: "polkadot";
|
|
45
|
+
walletName: string;
|
|
46
|
+
walletId: WalletId;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type PolkadotPluginOptions = {
|
|
50
|
+
/**
|
|
51
|
+
* Allowed Polkadot account key types. Accounts with other key types are
|
|
52
|
+
* filtered out from kheopskit state.
|
|
53
|
+
*
|
|
54
|
+
* @default ["sr25519", "ed25519", "ecdsa"]
|
|
55
|
+
*/
|
|
56
|
+
accountTypes?: PolkadotAccountType[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Polkadot platform plugin. Pass to `getKheopskit$({ platforms: [polkadot()] })`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { polkadot } from "@kheopskit/core/polkadot";
|
|
64
|
+
* polkadot({ accountTypes: ["sr25519", "ed25519", "ecdsa"] });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare const polkadot: (options?: PolkadotPluginOptions) => KheopskitPlatform<"polkadot", PolkadotWallet, PolkadotAccount>;
|
|
68
|
+
|
|
69
|
+
export { type PolkadotAccount, PolkadotAccountType, PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotPluginOptions, type PolkadotWallet, isSs58Address, polkadot };
|
package/dist/polkadot.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkD3EQMFZ2js = require('./chunk-D3EQMFZ2.js');
|
|
4
|
+
require('./chunk-FIAL4HTE.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var _chunkNU46D4MZjs = require('./chunk-NU46D4MZ.js');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
var _chunkSIUWQBT4js = require('./chunk-SIUWQBT4.js');
|
|
17
|
+
|
|
18
|
+
// src/api/polkadot/accounts.ts
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
var _pjssigner = require('polkadot-api/pjs-signer');
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
var _rxjs = require('rxjs');
|
|
31
|
+
var getInjectedWalletAccounts$ = (wallet) => {
|
|
32
|
+
if (!wallet.isConnected) return _rxjs.of.call(void 0, []);
|
|
33
|
+
return new (0, _rxjs.Observable)((subscriber) => {
|
|
34
|
+
const getAccount = (account) => ({
|
|
35
|
+
id: _chunkNU46D4MZjs.getWalletAccountId.call(void 0, wallet.id, account.address),
|
|
36
|
+
...account,
|
|
37
|
+
type: _nullishCoalesce(account.type, () => ( "sr25519")),
|
|
38
|
+
platform: "polkadot",
|
|
39
|
+
walletName: wallet.name,
|
|
40
|
+
walletId: wallet.id
|
|
41
|
+
});
|
|
42
|
+
const extension = wallet.extension;
|
|
43
|
+
const unsubscribe = extension.subscribe((accounts) => {
|
|
44
|
+
subscriber.next(accounts.map(getAccount));
|
|
45
|
+
});
|
|
46
|
+
subscriber.next(extension.getAccounts().map(getAccount));
|
|
47
|
+
return () => {
|
|
48
|
+
return unsubscribe();
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
var getAppKitPolkadotSigner = (appKit, address) => {
|
|
53
|
+
const provider = appKit.getProvider("polkadot");
|
|
54
|
+
if (!provider) throw new (0, _chunkNU46D4MZjs.KheopskitError)("NO_PROVIDER", "No provider found");
|
|
55
|
+
if (!provider.session)
|
|
56
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)("NO_SESSION", "No session found");
|
|
57
|
+
return _pjssigner.getPolkadotSignerFromPjs.call(void 0,
|
|
58
|
+
address,
|
|
59
|
+
(transactionPayload) => {
|
|
60
|
+
if (!provider.session)
|
|
61
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)("NO_SESSION", "No session found");
|
|
62
|
+
return provider.client.request({
|
|
63
|
+
topic: provider.session.topic,
|
|
64
|
+
chainId: `polkadot:${transactionPayload.genesisHash.substring(2, 34)}`,
|
|
65
|
+
request: {
|
|
66
|
+
method: "polkadot_signTransaction",
|
|
67
|
+
params: {
|
|
68
|
+
address,
|
|
69
|
+
transactionPayload
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
async ({ address: address2, data }) => {
|
|
75
|
+
if (!provider.session)
|
|
76
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)("NO_SESSION", "No session found");
|
|
77
|
+
const networks = appKit.getCaipNetworks("polkadot");
|
|
78
|
+
const chainId = _optionalChain([networks, 'access', _ => _[0], 'optionalAccess', _2 => _2.caipNetworkId]);
|
|
79
|
+
if (!chainId)
|
|
80
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)(
|
|
81
|
+
"NO_SESSION",
|
|
82
|
+
"No CAIP network available for polkadot"
|
|
83
|
+
);
|
|
84
|
+
return provider.client.request({
|
|
85
|
+
topic: provider.session.topic,
|
|
86
|
+
chainId,
|
|
87
|
+
request: {
|
|
88
|
+
method: "polkadot_signMessage",
|
|
89
|
+
params: {
|
|
90
|
+
address: address2,
|
|
91
|
+
message: data
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
var getAppKitAccounts$ = (wallet) => {
|
|
99
|
+
const provider = wallet.appKit.getProvider("polkadot");
|
|
100
|
+
if (!wallet.isConnected || !_optionalChain([provider, 'optionalAccess', _3 => _3.session])) return _rxjs.of.call(void 0, []);
|
|
101
|
+
return _chunkSIUWQBT4js.getCachedObservable$.call(void 0,
|
|
102
|
+
`accounts:${wallet.id}:`,
|
|
103
|
+
() => new (0, _rxjs.Observable)((subscriber) => {
|
|
104
|
+
const buildAccounts = () => {
|
|
105
|
+
const session = provider.session;
|
|
106
|
+
if (!session) return [];
|
|
107
|
+
const addresses = [
|
|
108
|
+
...new Set(
|
|
109
|
+
Object.values(session.namespaces).flatMap((namespace) => _nullishCoalesce(namespace.accounts, () => ( []))).filter((account) => account.startsWith("polkadot:")).map((account) => account.split(":")[2]).filter((address) => !!address)
|
|
110
|
+
)
|
|
111
|
+
];
|
|
112
|
+
return addresses.map(
|
|
113
|
+
(address) => ({
|
|
114
|
+
id: _chunkNU46D4MZjs.getWalletAccountId.call(void 0, wallet.id, address),
|
|
115
|
+
platform: "polkadot",
|
|
116
|
+
walletName: wallet.name,
|
|
117
|
+
walletId: wallet.id,
|
|
118
|
+
address,
|
|
119
|
+
polkadotSigner: getAppKitPolkadotSigner(wallet.appKit, address),
|
|
120
|
+
genesisHash: null,
|
|
121
|
+
name: `${wallet.name} Polkadot`,
|
|
122
|
+
// WalletConnect (Reown AppKit) doesn't expose account key type;
|
|
123
|
+
// default to sr25519, which is the most common Polkadot key type.
|
|
124
|
+
type: "sr25519"
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
};
|
|
128
|
+
subscriber.next(buildAccounts());
|
|
129
|
+
const reemit = () => subscriber.next(buildAccounts());
|
|
130
|
+
provider.on("session_update", reemit);
|
|
131
|
+
provider.on("accountsChanged", reemit);
|
|
132
|
+
return () => {
|
|
133
|
+
provider.off("session_update", reemit);
|
|
134
|
+
provider.off("accountsChanged", reemit);
|
|
135
|
+
};
|
|
136
|
+
}).pipe(_rxjs.shareReplay.call(void 0, { refCount: true, bufferSize: 1 }))
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
var getPolkadotAccounts$ = (polkadotWallets$, polkadotAccountTypes) => new (0, _rxjs.Observable)((subscriber) => {
|
|
140
|
+
if (polkadotAccountTypes.length === 0) {
|
|
141
|
+
console.warn(
|
|
142
|
+
"[kheopskit] config.polkadotAccountTypes is empty; all Polkadot accounts will be filtered out."
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
const sub = polkadotWallets$.pipe(
|
|
146
|
+
_rxjs.map.call(void 0, (wallets) => wallets.filter((w) => w.isConnected)),
|
|
147
|
+
_rxjs.switchMap.call(void 0,
|
|
148
|
+
(wallets) => wallets.length ? _rxjs.combineLatest.call(void 0, [
|
|
149
|
+
...wallets.filter((w) => w.type === "injected").map(getInjectedWalletAccounts$),
|
|
150
|
+
...wallets.filter((w) => w.type === "appKit").map(getAppKitAccounts$)
|
|
151
|
+
]) : _rxjs.of.call(void 0, [])
|
|
152
|
+
),
|
|
153
|
+
_rxjs.map.call(void 0,
|
|
154
|
+
(accounts) => accounts.flat().filter((account) => polkadotAccountTypes.includes(account.type))
|
|
155
|
+
),
|
|
156
|
+
_rxjs.distinctUntilChanged.call(void 0, isSameAccountsList)
|
|
157
|
+
).subscribe(subscriber);
|
|
158
|
+
return () => {
|
|
159
|
+
sub.unsubscribe();
|
|
160
|
+
};
|
|
161
|
+
}).pipe(_rxjs.shareReplay.call(void 0, { refCount: true, bufferSize: 1 }));
|
|
162
|
+
var isSameAccountsList = (a, b) => {
|
|
163
|
+
if (a.length !== b.length) return false;
|
|
164
|
+
return a.every((account, i) => account.id === _optionalChain([b, 'access', _4 => _4[i], 'optionalAccess', _5 => _5.id]));
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// src/api/polkadot/wallets.ts
|
|
168
|
+
var _lodashes = require('lodash-es');
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
var getInjectedWalletsIds = () => typeof window === "undefined" ? [] : _pjssigner.getInjectedExtensions.call(void 0, ).map((name) => _chunkSIUWQBT4js.getWalletId.call(void 0, "polkadot", name));
|
|
182
|
+
var createWalletIdsPoller$ = () => {
|
|
183
|
+
return new (0, _rxjs.Observable)((subscriber) => {
|
|
184
|
+
subscriber.next(getInjectedWalletsIds());
|
|
185
|
+
const intervals = [100, 200, 300, 500];
|
|
186
|
+
let index = 0;
|
|
187
|
+
let timer;
|
|
188
|
+
const poll = () => {
|
|
189
|
+
subscriber.next(getInjectedWalletsIds());
|
|
190
|
+
if (index < intervals.length) {
|
|
191
|
+
const delay = intervals[index++];
|
|
192
|
+
timer = setTimeout(poll, delay);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
if (intervals.length > 0) {
|
|
196
|
+
timer = setTimeout(poll, _nullishCoalesce(intervals[index++], () => ( 100)));
|
|
197
|
+
}
|
|
198
|
+
return () => {
|
|
199
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
200
|
+
};
|
|
201
|
+
}).pipe(
|
|
202
|
+
_rxjs.distinctUntilChanged.call(void 0, _lodashes.isEqual),
|
|
203
|
+
_rxjs.shareReplay.call(void 0, { refCount: true, bufferSize: 1 })
|
|
204
|
+
);
|
|
205
|
+
};
|
|
206
|
+
var createPolkadotInjectedWallets$ = (store2) => new (0, _rxjs.Observable)((subscriber) => {
|
|
207
|
+
const enabledExtensions$ = new (0, _rxjs.BehaviorSubject)(/* @__PURE__ */ new Map());
|
|
208
|
+
const connect = async (walletId) => {
|
|
209
|
+
if (enabledExtensions$.value.has(walletId))
|
|
210
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)(
|
|
211
|
+
"WALLET_ALREADY_CONNECTED",
|
|
212
|
+
`wallet ${walletId} is already connected`,
|
|
213
|
+
{ walletId }
|
|
214
|
+
);
|
|
215
|
+
const { identifier } = _chunkSIUWQBT4js.parseWalletId.call(void 0, walletId);
|
|
216
|
+
const extension = await _pjssigner.connectInjectedExtension.call(void 0, identifier);
|
|
217
|
+
const newMap = new Map(enabledExtensions$.value);
|
|
218
|
+
newMap.set(walletId, extension);
|
|
219
|
+
enabledExtensions$.next(newMap);
|
|
220
|
+
store2.addEnabledWalletId(walletId);
|
|
221
|
+
};
|
|
222
|
+
const disconnect = async (walletId) => {
|
|
223
|
+
if (!enabledExtensions$.value.has(walletId))
|
|
224
|
+
throw new (0, _chunkNU46D4MZjs.KheopskitError)(
|
|
225
|
+
"WALLET_NOT_CONNECTED",
|
|
226
|
+
`wallet ${walletId} is not connected`,
|
|
227
|
+
{ walletId }
|
|
228
|
+
);
|
|
229
|
+
const newMap = new Map(enabledExtensions$.value);
|
|
230
|
+
newMap.delete(walletId);
|
|
231
|
+
enabledExtensions$.next(newMap);
|
|
232
|
+
store2.removeEnabledWalletId(walletId);
|
|
233
|
+
};
|
|
234
|
+
const walletIds$ = createWalletIdsPoller$();
|
|
235
|
+
const subscription = _rxjs.combineLatest.call(void 0, [walletIds$, enabledExtensions$]).pipe(
|
|
236
|
+
_rxjs.map.call(void 0, ([walletIds, enabledExtensions]) => {
|
|
237
|
+
return walletIds.map((id) => {
|
|
238
|
+
const { identifier } = _chunkSIUWQBT4js.parseWalletId.call(void 0, id);
|
|
239
|
+
const extension = enabledExtensions.get(id);
|
|
240
|
+
const extInfo = _chunkD3EQMFZ2js.POLKADOT_EXTENSIONS[identifier];
|
|
241
|
+
return {
|
|
242
|
+
id,
|
|
243
|
+
type: "injected",
|
|
244
|
+
platform: "polkadot",
|
|
245
|
+
name: _nullishCoalesce(_optionalChain([extInfo, 'optionalAccess', _6 => _6.name]), () => ( identifier)),
|
|
246
|
+
icon: _nullishCoalesce(_optionalChain([extInfo, 'optionalAccess', _7 => _7.icon]), () => ( "")),
|
|
247
|
+
sourceId: identifier,
|
|
248
|
+
extension,
|
|
249
|
+
isConnected: !!extension,
|
|
250
|
+
connect: () => connect(id),
|
|
251
|
+
disconnect: () => disconnect(id)
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
}),
|
|
255
|
+
_rxjs.distinctUntilChanged.call(void 0, walletsEqual)
|
|
256
|
+
).subscribe(subscriber);
|
|
257
|
+
return () => {
|
|
258
|
+
subscription.unsubscribe();
|
|
259
|
+
};
|
|
260
|
+
}).pipe(_rxjs.shareReplay.call(void 0, { refCount: true, bufferSize: 1 }));
|
|
261
|
+
var getPolkadotWallets$ = (config, store2 = _chunkNU46D4MZjs.store) => {
|
|
262
|
+
return new (0, _rxjs.Observable)((subscriber) => {
|
|
263
|
+
const subscription = _rxjs.combineLatest.call(void 0, [
|
|
264
|
+
createPolkadotInjectedWallets$(store2),
|
|
265
|
+
_chunkNU46D4MZjs.getAppKitWallets$.call(void 0, config).pipe(_rxjs.map.call(void 0, (w) => w.polkadot))
|
|
266
|
+
]).pipe(
|
|
267
|
+
_rxjs.map.call(void 0,
|
|
268
|
+
([injectedWallets, appKitWallet]) => appKitWallet ? [...injectedWallets, appKitWallet] : injectedWallets
|
|
269
|
+
)
|
|
270
|
+
).subscribe(subscriber);
|
|
271
|
+
return () => {
|
|
272
|
+
subscription.unsubscribe();
|
|
273
|
+
};
|
|
274
|
+
}).pipe(_rxjs.shareReplay.call(void 0, { refCount: true, bufferSize: 1 }));
|
|
275
|
+
};
|
|
276
|
+
var walletsEqual = (a, b) => {
|
|
277
|
+
if (a.length !== b.length) return false;
|
|
278
|
+
return a.every(
|
|
279
|
+
(w, i) => w.id === _optionalChain([b, 'access', _8 => _8[i], 'optionalAccess', _9 => _9.id]) && w.isConnected === _optionalChain([b, 'access', _10 => _10[i], 'optionalAccess', _11 => _11.isConnected]) && w.name === _optionalChain([b, 'access', _12 => _12[i], 'optionalAccess', _13 => _13.name])
|
|
280
|
+
);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// src/api/polkadot/plugin.ts
|
|
284
|
+
var DEFAULT_ACCOUNT_TYPES = [
|
|
285
|
+
"sr25519",
|
|
286
|
+
"ed25519",
|
|
287
|
+
"ecdsa"
|
|
288
|
+
];
|
|
289
|
+
var VALID_ACCOUNT_TYPES = /* @__PURE__ */ new Set([
|
|
290
|
+
"sr25519",
|
|
291
|
+
"ed25519",
|
|
292
|
+
"ecdsa",
|
|
293
|
+
"ethereum"
|
|
294
|
+
]);
|
|
295
|
+
var polkadot = (options = {}) => {
|
|
296
|
+
const accountTypes = _nullishCoalesce(options.accountTypes, () => ( DEFAULT_ACCOUNT_TYPES));
|
|
297
|
+
const invalid = accountTypes.filter((t) => !VALID_ACCOUNT_TYPES.has(t));
|
|
298
|
+
if (invalid.length > 0) {
|
|
299
|
+
console.warn(
|
|
300
|
+
`[kheopskit] Unknown polkadot accountTypes: ${JSON.stringify(invalid)}. Valid values: "sr25519", "ed25519", "ecdsa", "ethereum".`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
platform: "polkadot",
|
|
305
|
+
getWallets$: (ctx) => getPolkadotWallets$(ctx.config, ctx.store),
|
|
306
|
+
getAccounts$: (wallets$) => getPolkadotAccounts$(wallets$, accountTypes),
|
|
307
|
+
acceptsCachedAccount: (cached) => accountTypes.includes(_nullishCoalesce(cached.polkadotAccountType, () => ( "sr25519")))
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
exports.isSs58Address = _chunkNU46D4MZjs.isSs58Address; exports.polkadot = polkadot;
|
|
314
|
+
//# sourceMappingURL=polkadot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/kheopskit/kheopskit/packages/core/dist/polkadot.js","../src/api/polkadot/accounts.ts","../src/api/polkadot/wallets.ts","../src/api/polkadot/plugin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AAA;AAEG,IAAA;AAClC,EAAA;AAEiD,EAAA;AACN,IAAA;AAC/B,MAAA;AACT,QAAA;AACkB,QAAA;AACP,QAAA;AACZ,MAAA;AAE8C,IAAA;AACzB,IAAA;AACQ,IAAA;AAEM,IAAA;AACrC,EAAA;AAE0C,EAAA;AAGxC,EAAA;AACwC,IAAA;AACc,MAAA;AACb,QAAA;AACG,QAAA;AACI,QAAA;AAEvC,QAAA;AACN,UAAA;AACM,UAAA;AACI,UAAA;AACa,UAAA;AACA,UAAA;AACb,UAAA;AACV,UAAA;AACe,UAAA;AACU,UAAA;AACM,UAAA;AAChC,QAAA;AACA,MAAA;AACD,IAAA;AACgC,IAAA;AAEb,EAAA;AAET,EAAA;AACa,IAAA;AAC1B,EAAA;AACqD;AAKlD;AACoD,EAAA;AACpB,IAAA;AACE,MAAA;AACiB,MAAA;AAEpD,IAAA;AACAF,MAAAA;AACoB,QAAA;AACpB,MAAA;AAEoB,IAAA;AAET,IAAA;AACa,MAAA;AAC1B,IAAA;AACqD,EAAA;AACvD;AAQc;AACqB,EAAA;AACzB,EAAA;AAIL,IAAA;AACJ,EAAA;AACD;AF6G6E;AACA;AGjRxB;AACpD,EAAA;AACA,EAAA;AACA,EAAA;AACD;AAE4C;AAC3C,EAAA;AACA,EAAA;AACA,EAAA;AACA,EAAA;AACA;AAuBoE;AACvB,EAAA;AAEyB,EAAA;AAC9C,EAAA;AACf,IAAA;AAC8D,MAAA;AACtE,IAAA;AACD,EAAA;AAEO,EAAA;AACI,IAAA;AAEgC,IAAA;AAC6B,IAAA;AAEzC,IAAA;AAC/B,EAAA;AACD;AHwP6E;AACA;AACA;AACA","file":"/home/runner/work/kheopskit/kheopskit/packages/core/dist/polkadot.js","sourcesContent":[null,"import {\n\tgetPolkadotSignerFromPjs,\n\ttype InjectedExtension,\n\ttype InjectedPolkadotAccount,\n} from \"polkadot-api/pjs-signer\";\nimport {\n\tcombineLatest,\n\tdistinctUntilChanged,\n\tmap,\n\tObservable,\n\tof,\n\tshareReplay,\n\tswitchMap,\n} from \"rxjs\";\nimport { getWalletAccountId } from \"../../utils\";\nimport { getCachedObservable$ } from \"../../utils/getCachedObservable\";\nimport { KheopskitError } from \"../errors\";\nimport type {\n\tAppKitInstance,\n\tPolkadotAccountType,\n\tPolkadotAppKitWallet,\n} from \"../types\";\nimport type {\n\tPolkadotAccount,\n\tPolkadotInjectedWallet,\n\tPolkadotWallet,\n} from \"./types\";\n\nconst getInjectedWalletAccounts$ = (\n\twallet: PolkadotInjectedWallet,\n): Observable<PolkadotAccount[]> => {\n\tif (!wallet.isConnected) return of([]);\n\n\treturn new Observable<PolkadotAccount[]>((subscriber) => {\n\t\tconst getAccount = (account: InjectedPolkadotAccount): PolkadotAccount => ({\n\t\t\tid: getWalletAccountId(wallet.id, account.address),\n\t\t\t...account,\n\t\t\ttype: account.type ?? \"sr25519\",\n\t\t\tplatform: \"polkadot\",\n\t\t\twalletName: wallet.name,\n\t\t\twalletId: wallet.id,\n\t\t});\n\n\t\tconst extension = wallet.extension as InjectedExtension;\n\n\t\t// subscribe to changes\n\t\tconst unsubscribe = extension.subscribe((accounts) => {\n\t\t\tsubscriber.next(accounts.map(getAccount));\n\t\t});\n\n\t\t// initial value\n\t\tsubscriber.next(extension.getAccounts().map(getAccount));\n\n\t\treturn () => {\n\t\t\treturn unsubscribe();\n\t\t};\n\t});\n};\n\nconst getAppKitPolkadotSigner = (appKit: AppKitInstance, address: string) => {\n\tconst provider = appKit.getProvider(\"polkadot\");\n\tif (!provider) throw new KheopskitError(\"NO_PROVIDER\", \"No provider found\");\n\tif (!provider.session)\n\t\tthrow new KheopskitError(\"NO_SESSION\", \"No session found\");\n\n\treturn getPolkadotSignerFromPjs(\n\t\taddress,\n\t\t(transactionPayload) => {\n\t\t\tif (!provider.session)\n\t\t\t\tthrow new KheopskitError(\"NO_SESSION\", \"No session found\");\n\n\t\t\treturn provider.client.request({\n\t\t\t\ttopic: provider.session.topic,\n\t\t\t\tchainId: `polkadot:${transactionPayload.genesisHash.substring(2, 34)}`,\n\t\t\t\trequest: {\n\t\t\t\t\tmethod: \"polkadot_signTransaction\",\n\t\t\t\t\tparams: {\n\t\t\t\t\t\taddress,\n\t\t\t\t\t\ttransactionPayload,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\tasync ({ address, data }) => {\n\t\t\tif (!provider.session)\n\t\t\t\tthrow new KheopskitError(\"NO_SESSION\", \"No session found\");\n\t\t\tconst networks = appKit.getCaipNetworks(\"polkadot\");\n\t\t\tconst chainId = networks[0]?.caipNetworkId;\n\t\t\tif (!chainId)\n\t\t\t\tthrow new KheopskitError(\n\t\t\t\t\t\"NO_SESSION\",\n\t\t\t\t\t\"No CAIP network available for polkadot\",\n\t\t\t\t);\n\n\t\t\treturn provider.client.request({\n\t\t\t\ttopic: provider.session.topic,\n\t\t\t\tchainId,\n\t\t\t\trequest: {\n\t\t\t\t\tmethod: \"polkadot_signMessage\",\n\t\t\t\t\tparams: {\n\t\t\t\t\t\taddress,\n\t\t\t\t\t\tmessage: data,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t);\n};\n\nconst getAppKitAccounts$ = (\n\twallet: PolkadotAppKitWallet,\n): Observable<PolkadotAccount[]> => {\n\tconst provider = wallet.appKit.getProvider(\"polkadot\");\n\n\tif (!wallet.isConnected || !provider?.session) return of([]);\n\n\treturn getCachedObservable$(`accounts:${wallet.id}:`, () =>\n\t\tnew Observable<PolkadotAccount[]>((subscriber) => {\n\t\t\t// AppKit's getAccount(\"polkadot\").allAccounts is always empty because\n\t\t\t// AppKit has no native polkadot adapter; the WalletConnect session is the\n\t\t\t// source of truth. Accounts are CAIP-10 strings\n\t\t\t// (\"polkadot:<chainRef>:<address>\"), one entry per chain, so dedupe to\n\t\t\t// unique addresses.\n\t\t\tconst buildAccounts = (): PolkadotAccount[] => {\n\t\t\t\tconst session = provider.session;\n\t\t\t\tif (!session) return [];\n\n\t\t\t\tconst addresses = [\n\t\t\t\t\t...new Set(\n\t\t\t\t\t\tObject.values(session.namespaces)\n\t\t\t\t\t\t\t.flatMap((namespace) => namespace.accounts ?? [])\n\t\t\t\t\t\t\t.filter((account) => account.startsWith(\"polkadot:\"))\n\t\t\t\t\t\t\t.map((account) => account.split(\":\")[2])\n\t\t\t\t\t\t\t.filter((address): address is string => !!address),\n\t\t\t\t\t),\n\t\t\t\t];\n\n\t\t\t\treturn addresses.map(\n\t\t\t\t\t(address): PolkadotAccount => ({\n\t\t\t\t\t\tid: getWalletAccountId(wallet.id, address),\n\t\t\t\t\t\tplatform: \"polkadot\",\n\t\t\t\t\t\twalletName: wallet.name,\n\t\t\t\t\t\twalletId: wallet.id,\n\t\t\t\t\t\taddress,\n\t\t\t\t\t\tpolkadotSigner: getAppKitPolkadotSigner(wallet.appKit, address),\n\t\t\t\t\t\tgenesisHash: null,\n\t\t\t\t\t\tname: `${wallet.name} Polkadot`,\n\t\t\t\t\t\t// WalletConnect (Reown AppKit) doesn't expose account key type;\n\t\t\t\t\t\t// default to sr25519, which is the most common Polkadot key type.\n\t\t\t\t\t\ttype: \"sr25519\",\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t};\n\n\t\t\tsubscriber.next(buildAccounts());\n\n\t\t\t// Re-derive when the WalletConnect session's accounts change, mirroring\n\t\t\t// the injected extension's subscribe and the Solana AppKit path.\n\t\t\tconst reemit = () => subscriber.next(buildAccounts());\n\t\t\tprovider.on(\"session_update\", reemit);\n\t\t\tprovider.on(\"accountsChanged\", reemit);\n\n\t\t\treturn () => {\n\t\t\t\tprovider.off(\"session_update\", reemit);\n\t\t\t\tprovider.off(\"accountsChanged\", reemit);\n\t\t\t};\n\t\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),\n\t);\n};\n\nexport const getPolkadotAccounts$ = (\n\tpolkadotWallets$: Observable<PolkadotWallet[]>,\n\tpolkadotAccountTypes: PolkadotAccountType[],\n) =>\n\tnew Observable<PolkadotAccount[]>((subscriber) => {\n\t\tif (polkadotAccountTypes.length === 0) {\n\t\t\tconsole.warn(\n\t\t\t\t\"[kheopskit] config.polkadotAccountTypes is empty; all Polkadot accounts will be filtered out.\",\n\t\t\t);\n\t\t}\n\n\t\tconst sub = polkadotWallets$\n\t\t\t.pipe(\n\t\t\t\tmap((wallets) => wallets.filter((w) => w.isConnected)),\n\t\t\t\tswitchMap((wallets) =>\n\t\t\t\t\twallets.length\n\t\t\t\t\t\t? combineLatest([\n\t\t\t\t\t\t\t\t...wallets\n\t\t\t\t\t\t\t\t\t.filter((w) => w.type === \"injected\")\n\t\t\t\t\t\t\t\t\t.map(getInjectedWalletAccounts$),\n\t\t\t\t\t\t\t\t...wallets\n\t\t\t\t\t\t\t\t\t.filter((w) => w.type === \"appKit\")\n\t\t\t\t\t\t\t\t\t.map(getAppKitAccounts$),\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t: of([]),\n\t\t\t\t),\n\t\t\t\tmap((accounts) =>\n\t\t\t\t\taccounts\n\t\t\t\t\t\t.flat()\n\t\t\t\t\t\t.filter((account) => polkadotAccountTypes.includes(account.type)),\n\t\t\t\t),\n\t\t\t\tdistinctUntilChanged(isSameAccountsList),\n\t\t\t)\n\t\t\t.subscribe(subscriber);\n\n\t\treturn () => {\n\t\t\tsub.unsubscribe();\n\t\t};\n\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));\n\nconst isSameAccountsList = (a: PolkadotAccount[], b: PolkadotAccount[]) => {\n\tif (a.length !== b.length) return false;\n\treturn a.every((account, i) => account.id === b[i]?.id);\n};\n","import { isEqual } from \"lodash-es\";\nimport {\n\tconnectInjectedExtension,\n\tgetInjectedExtensions,\n\ttype InjectedExtension,\n} from \"polkadot-api/pjs-signer\";\nimport {\n\tBehaviorSubject,\n\tcombineLatest,\n\tdistinctUntilChanged,\n\tmap,\n\tObservable,\n\tshareReplay,\n} from \"rxjs\";\nimport { POLKADOT_EXTENSIONS } from \"../../utils/polkadotExtensions\";\nimport {\n\tgetWalletId,\n\tparseWalletId,\n\ttype WalletId,\n} from \"../../utils/WalletId\";\nimport { getAppKitWallets$ } from \"../appKit\";\nimport { KheopskitError } from \"../errors\";\nimport { store as defaultStore, type KheopskitStore } from \"../store\";\nimport type { KheopskitConfig } from \"../types\";\nimport type { PolkadotInjectedWallet, PolkadotWallet } from \"./types\";\n\nconst getInjectedWalletsIds = () =>\n\ttypeof window === \"undefined\"\n\t\t? []\n\t\t: getInjectedExtensions().map((name) => getWalletId(\"polkadot\", name));\n\n// Create a polling observable that starts immediately and polls at intervals\nconst createWalletIdsPoller$ = () => {\n\treturn new Observable<WalletId[]>((subscriber) => {\n\t\t// Emit immediately on subscribe\n\t\tsubscriber.next(getInjectedWalletsIds());\n\n\t\t// Poll at shorter intervals initially, then slow down\n\t\tconst intervals = [100, 200, 300, 500];\n\t\tlet index = 0;\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\n\t\tconst poll = () => {\n\t\t\tsubscriber.next(getInjectedWalletsIds());\n\t\t\tif (index < intervals.length) {\n\t\t\t\tconst delay = intervals[index++];\n\t\t\t\ttimer = setTimeout(poll, delay);\n\t\t\t}\n\t\t};\n\n\t\t// Start polling after first immediate emission\n\t\tif (intervals.length > 0) {\n\t\t\ttimer = setTimeout(poll, intervals[index++] ?? 100);\n\t\t}\n\n\t\treturn () => {\n\t\t\t// Cancel any pending poll so it can't fire after unsubscribe.\n\t\t\tif (timer !== undefined) clearTimeout(timer);\n\t\t};\n\t}).pipe(\n\t\tdistinctUntilChanged<WalletId[]>(isEqual),\n\t\tshareReplay({ refCount: true, bufferSize: 1 }),\n\t);\n};\n\nconst createPolkadotInjectedWallets$ = (store: KheopskitStore) =>\n\tnew Observable<PolkadotInjectedWallet[]>((subscriber) => {\n\t\tconst enabledExtensions$ = new BehaviorSubject<\n\t\t\tMap<WalletId, InjectedExtension>\n\t\t>(new Map());\n\n\t\tconst connect = async (walletId: WalletId) => {\n\t\t\tif (enabledExtensions$.value.has(walletId))\n\t\t\t\tthrow new KheopskitError(\n\t\t\t\t\t\"WALLET_ALREADY_CONNECTED\",\n\t\t\t\t\t`wallet ${walletId} is already connected`,\n\t\t\t\t\t{ walletId },\n\t\t\t\t);\n\t\t\tconst { identifier } = parseWalletId(walletId);\n\t\t\tconst extension = await connectInjectedExtension(identifier);\n\n\t\t\tconst newMap = new Map(enabledExtensions$.value);\n\t\t\tnewMap.set(walletId, extension);\n\t\t\tenabledExtensions$.next(newMap);\n\n\t\t\tstore.addEnabledWalletId(walletId);\n\t\t};\n\n\t\tconst disconnect = async (walletId: WalletId) => {\n\t\t\tif (!enabledExtensions$.value.has(walletId))\n\t\t\t\tthrow new KheopskitError(\n\t\t\t\t\t\"WALLET_NOT_CONNECTED\",\n\t\t\t\t\t`wallet ${walletId} is not connected`,\n\t\t\t\t\t{ walletId },\n\t\t\t\t);\n\n\t\t\tconst newMap = new Map(enabledExtensions$.value);\n\t\t\tnewMap.delete(walletId);\n\t\t\tenabledExtensions$.next(newMap);\n\n\t\t\tstore.removeEnabledWalletId(walletId);\n\t\t};\n\n\t\tconst walletIds$ = createWalletIdsPoller$();\n\n\t\tconst subscription = combineLatest([walletIds$, enabledExtensions$])\n\t\t\t.pipe(\n\t\t\t\tmap(([walletIds, enabledExtensions]) => {\n\t\t\t\t\treturn walletIds.map((id): PolkadotInjectedWallet => {\n\t\t\t\t\t\tconst { identifier } = parseWalletId(id);\n\t\t\t\t\t\tconst extension = enabledExtensions.get(id);\n\t\t\t\t\t\tconst extInfo = POLKADOT_EXTENSIONS[identifier];\n\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\ttype: \"injected\",\n\t\t\t\t\t\t\tplatform: \"polkadot\",\n\t\t\t\t\t\t\tname: extInfo?.name ?? identifier,\n\t\t\t\t\t\t\ticon: extInfo?.icon ?? \"\",\n\t\t\t\t\t\t\tsourceId: identifier,\n\t\t\t\t\t\t\textension,\n\t\t\t\t\t\t\tisConnected: !!extension,\n\t\t\t\t\t\t\tconnect: () => connect(id),\n\t\t\t\t\t\t\tdisconnect: () => disconnect(id),\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\t\t\t\t}),\n\t\t\t\tdistinctUntilChanged(walletsEqual),\n\t\t\t)\n\t\t\t.subscribe(subscriber);\n\n\t\treturn () => {\n\t\t\tsubscription.unsubscribe();\n\t\t};\n\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));\n\nexport const getPolkadotWallets$ = (\n\tconfig: KheopskitConfig,\n\tstore: KheopskitStore = defaultStore,\n) => {\n\treturn new Observable<PolkadotWallet[]>((subscriber) => {\n\t\tconst subscription = combineLatest([\n\t\t\tcreatePolkadotInjectedWallets$(store),\n\t\t\tgetAppKitWallets$(config).pipe(map((w) => w.polkadot)),\n\t\t])\n\t\t\t.pipe(\n\t\t\t\tmap(([injectedWallets, appKitWallet]) =>\n\t\t\t\t\tappKitWallet ? [...injectedWallets, appKitWallet] : injectedWallets,\n\t\t\t\t),\n\t\t\t)\n\t\t\t.subscribe(subscriber);\n\n\t\treturn () => {\n\t\t\tsubscription.unsubscribe();\n\t\t};\n\t}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));\n};\n\n/**\n * Compare two wallet arrays by their relevant properties (not functions).\n */\nconst walletsEqual = (\n\ta: PolkadotInjectedWallet[],\n\tb: PolkadotInjectedWallet[],\n): boolean => {\n\tif (a.length !== b.length) return false;\n\treturn a.every(\n\t\t(w, i) =>\n\t\t\tw.id === b[i]?.id &&\n\t\t\tw.isConnected === b[i]?.isConnected &&\n\t\t\tw.name === b[i]?.name,\n\t);\n};\n","import type {\n\tKheopskitPlatform,\n\tPlatformContext,\n\tPolkadotAccountType,\n} from \"../types\";\nimport { getPolkadotAccounts$ } from \"./accounts\";\nimport type { PolkadotAccount, PolkadotWallet } from \"./types\";\nimport { getPolkadotWallets$ } from \"./wallets\";\n\nconst DEFAULT_ACCOUNT_TYPES: PolkadotAccountType[] = [\n\t\"sr25519\",\n\t\"ed25519\",\n\t\"ecdsa\",\n];\n\nconst VALID_ACCOUNT_TYPES = new Set<string>([\n\t\"sr25519\",\n\t\"ed25519\",\n\t\"ecdsa\",\n\t\"ethereum\",\n]);\n\nexport type PolkadotPluginOptions = {\n\t/**\n\t * Allowed Polkadot account key types. Accounts with other key types are\n\t * filtered out from kheopskit state.\n\t *\n\t * @default [\"sr25519\", \"ed25519\", \"ecdsa\"]\n\t */\n\taccountTypes?: PolkadotAccountType[];\n};\n\n/**\n * Polkadot platform plugin. Pass to `getKheopskit$({ platforms: [polkadot()] })`.\n *\n * @example\n * ```ts\n * import { polkadot } from \"@kheopskit/core/polkadot\";\n * polkadot({ accountTypes: [\"sr25519\", \"ed25519\", \"ecdsa\"] });\n * ```\n */\nexport const polkadot = (\n\toptions: PolkadotPluginOptions = {},\n): KheopskitPlatform<\"polkadot\", PolkadotWallet, PolkadotAccount> => {\n\tconst accountTypes = options.accountTypes ?? DEFAULT_ACCOUNT_TYPES;\n\n\tconst invalid = accountTypes.filter((t) => !VALID_ACCOUNT_TYPES.has(t));\n\tif (invalid.length > 0) {\n\t\tconsole.warn(\n\t\t\t`[kheopskit] Unknown polkadot accountTypes: ${JSON.stringify(invalid)}. Valid values: \"sr25519\", \"ed25519\", \"ecdsa\", \"ethereum\".`,\n\t\t);\n\t}\n\n\treturn {\n\t\tplatform: \"polkadot\",\n\t\tgetWallets$: (ctx: PlatformContext) =>\n\t\t\tgetPolkadotWallets$(ctx.config, ctx.store),\n\t\tgetAccounts$: (wallets$) => getPolkadotAccounts$(wallets$, accountTypes),\n\t\tacceptsCachedAccount: (cached) =>\n\t\t\taccountTypes.includes(cached.polkadotAccountType ?? \"sr25519\"),\n\t};\n};\n"]}
|