kit-squared 0.0.1

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.
@@ -0,0 +1,136 @@
1
+ import { disconnectWallet } from "./wallet.js";
2
+ // This is only concerned with the wallet selection component / process.
3
+ import { writable } from "svelte/store";
4
+ import { Event } from "./utils.js";
5
+ import { getWallets } from "@wallet-standard/app";
6
+ import { StandardEvents } from "@wallet-standard/features";
7
+ import { SolanaSignAndSendTransaction } from "@solana/wallet-standard-features";
8
+ import { getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, getOrCreateUiWalletForStandardWallet_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from "@wallet-standard/ui-registry";
9
+ // Technical Things
10
+ const { get: doGetWallets } = getWallets();
11
+ function walletHasSignAndSendFeature(wallet) {
12
+ return SolanaSignAndSendTransaction in wallet.features;
13
+ }
14
+ function subscribeToWalletEvents(wallet) {
15
+ //TODO: this may not be needed with polling
16
+ const dispose = wallet.features[StandardEvents].on("change", ({ accounts }) => {
17
+ if (wallet.name !== _selection.name) {
18
+ //NOT RELEVANT (not connected wallet)
19
+ // console.warn("wallet change ignored for",wallet.name);
20
+ return;
21
+ }
22
+ const newAccounts = accounts?.map(getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.bind(null, wallet));
23
+ if (newAccounts && newAccounts.length > 0) {
24
+ }
25
+ return newAccounts;
26
+ });
27
+ return dispose;
28
+ }
29
+ let walletMap = {
30
+ // name: {
31
+ // wallet,
32
+ // uiWallet
33
+ // }
34
+ };
35
+ // THis is not a store with a _realVersion because the way this new one does things it never updates
36
+ export let availableWallets = [];
37
+ let outputWallets;
38
+ export function recheckWallets() {
39
+ outputWallets = doGetWallets().filter(walletHasSignAndSendFeature);
40
+ for (let wallet of outputWallets) {
41
+ if (walletMap[wallet.name])
42
+ continue;
43
+ subscribeToWalletEvents(wallet);
44
+ const uiWallet = getOrCreateUiWalletForStandardWallet_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(wallet);
45
+ walletMap[wallet.name] = {
46
+ wallet: wallet,
47
+ uiWallet: uiWallet,
48
+ };
49
+ availableWallets.push(wallet.name);
50
+ }
51
+ }
52
+ recheckWallets();
53
+ let selectedWalletIndex = -1;
54
+ let selectedWalletAddress;
55
+ let walletWatchInterval = setInterval(async () => {
56
+ // Interval to watch for changes in selected/connected wallet
57
+ if (!outputWallets || selectedWalletIndex === -1)
58
+ return;
59
+ if (!outputWallets[selectedWalletIndex] || !outputWallets[selectedWalletIndex].accounts || !outputWallets[selectedWalletIndex].accounts[0]) {
60
+ // TODO: catch this for edge cases. this should count as a disconnect
61
+ console.error("This would be a disconnect..");
62
+ disconnectWallet();
63
+ }
64
+ // TODO: it is possible that accounts[0] is not always gonna be the one
65
+ const apparentAddress = outputWallets[selectedWalletIndex].accounts[0].address;
66
+ if (apparentAddress !== selectedWalletAddress) {
67
+ //TODO: this means they have changed their wallet, and it should trigger disconnect cycle
68
+ if (!apparentAddress) {
69
+ // only disconnect
70
+ console.error("This would be a disconnect..");
71
+ disconnectWallet();
72
+ }
73
+ else {
74
+ // disconnect then reconnect
75
+ console.error("This would be a disconnect then reconnect..");
76
+ // log("Apparent address:",apparentAddress);
77
+ // log("selected address:", selectedWalletAddress)
78
+ const selectedWalletName = outputWallets[selectedWalletIndex].name;
79
+ await disconnectWallet();
80
+ await selectWallet(selectedWalletName);
81
+ }
82
+ }
83
+ }, 200);
84
+ export let _selection = {
85
+ selected: false,
86
+ wallet: null,
87
+ name: null,
88
+ };
89
+ export let selection = writable(_selection);
90
+ export function clearSelectedVars() {
91
+ selectedWalletIndex = -1;
92
+ selectedWalletAddress = null;
93
+ _selection.selected = false;
94
+ _selection.wallet = null;
95
+ _selection.name = null;
96
+ selection.set(_selection);
97
+ }
98
+ export const onSelectWallet = Event();
99
+ export async function selectWallet(name) {
100
+ stopExpectWallet();
101
+ if (!name)
102
+ return;
103
+ _selection.wallet = walletMap[name];
104
+ _selection.name = name;
105
+ _selection.selected = true;
106
+ selection.set(_selection);
107
+ await onSelectWallet.trigger();
108
+ selectedWalletIndex = Object.keys(walletMap).indexOf(name);
109
+ selectedWalletAddress = walletMap[name].wallet.accounts[0].address;
110
+ }
111
+ export function getWalletInfo(name) {
112
+ //TODO: return image and whatever else
113
+ return name;
114
+ }
115
+ let expectWalletInterval;
116
+ export function expectWallet(name) {
117
+ stopExpectWallet();
118
+ expectWalletInterval = setInterval(() => {
119
+ if (walletMap &&
120
+ walletMap[name] &&
121
+ walletMap[name].wallet &&
122
+ walletMap[name].wallet.accounts &&
123
+ walletMap[name].wallet.accounts[0]) {
124
+ selectWallet(name);
125
+ }
126
+ }, 200);
127
+ }
128
+ export function stopExpectWallet() {
129
+ clearInterval(expectWalletInterval);
130
+ }
131
+ export function unmountTeardown() {
132
+ //This is really only needed for svelte dev mode. Otherwise should never be lost to an unmounted WalletSelection
133
+ // component.
134
+ stopExpectWallet();
135
+ clearInterval(walletWatchInterval);
136
+ }
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "kit-squared",
3
+ "version": "0.0.1",
4
+ "description": "Solana Kit functionality with SvelteKit",
5
+ "license": "ISC",
6
+ "author": "AnAllergyToAnalogy",
7
+ "scripts": {
8
+ "dev": "vite dev",
9
+ "build": "vite build && npm run prepack",
10
+ "preview": "vite preview",
11
+ "prepare": "svelte-kit sync || echo ''",
12
+ "prepack": "svelte-kit sync && svelte-package",
13
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
14
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
15
+ "package": "svelte-package",
16
+ "publint": "publint"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "!dist/**/*.test.*",
21
+ "!dist/**/*.spec.*"
22
+ ],
23
+ "sideEffects": [
24
+ "**/*.css"
25
+ ],
26
+ "svelte": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "svelte": "./dist/index.js"
33
+ }
34
+ },
35
+ "peerDependencies": {
36
+ "svelte": "^5.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@sveltejs/adapter-auto": "^7.0.1",
40
+ "@sveltejs/kit": "^2.57.0",
41
+ "@sveltejs/package": "^2.5.7",
42
+ "@sveltejs/vite-plugin-svelte": "^7.0.0",
43
+ "publint": "^0.3.18",
44
+ "svelte": "^5.55.2",
45
+ "svelte-check": "^4.4.6",
46
+ "typescript": "^6.0.2",
47
+ "vite": "^8.0.7"
48
+ },
49
+ "keywords": [
50
+ "svelte",
51
+ "solana"
52
+ ],
53
+ "dependencies": {
54
+ "@solana/keys": "^6.8.0",
55
+ "@solana/kit": "^6.8.0",
56
+ "@solana/promises": "^6.8.0",
57
+ "@solana/signers": "^6.8.0",
58
+ "@solana/transaction-confirmation": "^6.8.0",
59
+ "@solana/transactions": "^6.8.0",
60
+ "@solana/wallet-standard-features": "^1.3.0",
61
+ "@wallet-standard/app": "^1.1.0",
62
+ "@wallet-standard/base": "^1.1.0",
63
+ "@wallet-standard/features": "^1.1.0",
64
+ "@wallet-standard/ui": "^1.0.1",
65
+ "@wallet-standard/ui-registry": "^1.0.1",
66
+ "buffer": "^6.0.3"
67
+ }
68
+ }