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.
- package/README.md +218 -0
- package/dist/WalletSelection.svelte +120 -0
- package/dist/WalletSelection.svelte.d.ts +18 -0
- package/dist/components/walletSelectionComponent.d.ts +4 -0
- package/dist/components/walletSelectionComponent.js +33 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -0
- package/dist/library/connectSchedule.d.ts +16 -0
- package/dist/library/connectSchedule.js +51 -0
- package/dist/library/connection.d.ts +31 -0
- package/dist/library/connection.js +69 -0
- package/dist/library/program.d.ts +54 -0
- package/dist/library/program.js +431 -0
- package/dist/library/utils.d.ts +50 -0
- package/dist/library/utils.js +297 -0
- package/dist/library/wallet.d.ts +37 -0
- package/dist/library/wallet.js +233 -0
- package/dist/library/walletSelection.d.ts +31 -0
- package/dist/library/walletSelection.js +136 -0
- package/package.json +68 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { browser } from "$app/environment";
|
|
2
|
+
import { getProgramDerivedAddress } from "@solana/kit";
|
|
3
|
+
import * as kit from "@solana/kit";
|
|
4
|
+
export const ZERO_ADDRESS = "11111111111111111111111111111111";
|
|
5
|
+
export function integerTypeToSize(type) {
|
|
6
|
+
return type.substring(1);
|
|
7
|
+
}
|
|
8
|
+
export function isIntergerType(type) {
|
|
9
|
+
const types = ["isize", "usize"];
|
|
10
|
+
for (let i = 0; i < 8; i++) {
|
|
11
|
+
const s = 8 * (2 ** i);
|
|
12
|
+
types.push("u" + s);
|
|
13
|
+
types.push("i" + s);
|
|
14
|
+
}
|
|
15
|
+
return types.includes(type);
|
|
16
|
+
}
|
|
17
|
+
// Encoders
|
|
18
|
+
// export const typeEncoder = (()=>{
|
|
19
|
+
// //TODO: types probably not complete
|
|
20
|
+
//
|
|
21
|
+
// // const types = ["isize","usize"];
|
|
22
|
+
// const types = ["Address"];
|
|
23
|
+
// for(let i = 0; i < 8; i++){
|
|
24
|
+
// const s = 8 * (2 ** i);
|
|
25
|
+
// types.push("U"+s);
|
|
26
|
+
// types.push("I"+s);
|
|
27
|
+
// }
|
|
28
|
+
// const e = {};
|
|
29
|
+
// for(let T of types){
|
|
30
|
+
// const t = pascalToCamel(T);
|
|
31
|
+
// e[t] = kit[`get${T}Encoder`]();
|
|
32
|
+
// }
|
|
33
|
+
// return e;
|
|
34
|
+
// })();
|
|
35
|
+
export const typeEncoder = (() => {
|
|
36
|
+
const types = ["Address"];
|
|
37
|
+
for (let i = 0; i < 5; i++) {
|
|
38
|
+
const s = 8 * (2 ** i);
|
|
39
|
+
types.push("U" + s);
|
|
40
|
+
types.push("I" + s);
|
|
41
|
+
}
|
|
42
|
+
// log(types);
|
|
43
|
+
const e = {};
|
|
44
|
+
for (let T of types) {
|
|
45
|
+
const t = pascalToCamel(T);
|
|
46
|
+
const functionName = `get${T}Encoder`;
|
|
47
|
+
//@ts-ignore
|
|
48
|
+
e[t] = kit[functionName]();
|
|
49
|
+
}
|
|
50
|
+
return e;
|
|
51
|
+
})();
|
|
52
|
+
export function address(str) {
|
|
53
|
+
return typeEncoder.address.encode(str);
|
|
54
|
+
}
|
|
55
|
+
export function camelToSnake(name) {
|
|
56
|
+
let snaked = "";
|
|
57
|
+
for (let i = 0; i < name.length; i++) {
|
|
58
|
+
const s = name[i];
|
|
59
|
+
const l = name[i].toLowerCase();
|
|
60
|
+
if (s === l) {
|
|
61
|
+
snaked += s;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
snaked += "_" + l;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return snaked;
|
|
68
|
+
}
|
|
69
|
+
export function snakeToCamel(name) {
|
|
70
|
+
const params = name.split("_");
|
|
71
|
+
for (let i = 1; i < params.length; i++) {
|
|
72
|
+
const p = params[i];
|
|
73
|
+
params[i] = p.substring(0, 1).toUpperCase() + p.substring(1);
|
|
74
|
+
}
|
|
75
|
+
return params.join("");
|
|
76
|
+
}
|
|
77
|
+
export function pascalToCamel(name) {
|
|
78
|
+
return name.substring(0, 1).toLowerCase() + name.substring(1);
|
|
79
|
+
}
|
|
80
|
+
export function camelToPascal(name) {
|
|
81
|
+
return name.substring(0, 1).toUpperCase() + name.substring(1);
|
|
82
|
+
}
|
|
83
|
+
export function snakeToPascal(name) {
|
|
84
|
+
return camelToPascal(snakeToCamel(name));
|
|
85
|
+
}
|
|
86
|
+
export function pascalToSnake(name) {
|
|
87
|
+
return camelToSnake(pascalToCamel(name));
|
|
88
|
+
}
|
|
89
|
+
;
|
|
90
|
+
export function EventSingle() {
|
|
91
|
+
// On event with single callback
|
|
92
|
+
let callback = async () => { };
|
|
93
|
+
function onEvent(_callback = (...args) => { }) {
|
|
94
|
+
//@ts-ignore
|
|
95
|
+
callback = _callback;
|
|
96
|
+
return () => {
|
|
97
|
+
//remove callback
|
|
98
|
+
callback = async () => { };
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async function triggerEvent(...args) {
|
|
102
|
+
//@ts-ignore
|
|
103
|
+
await callback(...args);
|
|
104
|
+
}
|
|
105
|
+
onEvent.trigger = triggerEvent;
|
|
106
|
+
return onEvent;
|
|
107
|
+
}
|
|
108
|
+
export function Event() {
|
|
109
|
+
// returns onEvent function,
|
|
110
|
+
// this function returns another func which you use to unsubscribe from event
|
|
111
|
+
// onEvent.triggger(..args) triggers the event
|
|
112
|
+
let cid = 0;
|
|
113
|
+
let callbacks = {};
|
|
114
|
+
function onEvent(callback) {
|
|
115
|
+
const _cid = cid++;
|
|
116
|
+
callbacks[_cid] = callback;
|
|
117
|
+
return () => {
|
|
118
|
+
//remove callback
|
|
119
|
+
delete callbacks[_cid];
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async function triggerEvent(...args) {
|
|
123
|
+
for (let id in callbacks) {
|
|
124
|
+
await callbacks[id](...args);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function killAll() {
|
|
128
|
+
for (let _cid in callbacks) {
|
|
129
|
+
delete callbacks[_cid];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
onEvent.trigger = triggerEvent;
|
|
133
|
+
onEvent.killAll = killAll;
|
|
134
|
+
return onEvent;
|
|
135
|
+
}
|
|
136
|
+
export async function getPDA(seeds, programId) {
|
|
137
|
+
let seeds_parsed = [];
|
|
138
|
+
for (let s = 0; s < seeds.length; s++) {
|
|
139
|
+
const seed = seeds[s];
|
|
140
|
+
//@ts-ignore
|
|
141
|
+
if (seed.buffer) {
|
|
142
|
+
//Assume it's already been encoded (including Address), just add to parsed
|
|
143
|
+
seeds_parsed.push(seed);
|
|
144
|
+
}
|
|
145
|
+
else if (typeof seed === "string") {
|
|
146
|
+
// Strings just go as is
|
|
147
|
+
seeds_parsed.push(seed);
|
|
148
|
+
}
|
|
149
|
+
else if (Array.isArray(seed)) {
|
|
150
|
+
//@ts-ignore
|
|
151
|
+
if (seed.length !== 2) {
|
|
152
|
+
//@ts-ignore
|
|
153
|
+
throw new Error("Unexpected seed length as Array: " + seed.length);
|
|
154
|
+
}
|
|
155
|
+
const value = seed[0];
|
|
156
|
+
const t = seed[1];
|
|
157
|
+
if (!isIntergerType(t)) {
|
|
158
|
+
throw new Error("Not an integer type: " + t);
|
|
159
|
+
}
|
|
160
|
+
seeds_parsed.push(typeEncoder[t].encode(value));
|
|
161
|
+
// if(!isIntegerType)
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
throw new Error("Unable to interpret seed: " + seed);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const [pda, bump] = await getProgramDerivedAddress({
|
|
168
|
+
programAddress: programId,
|
|
169
|
+
//@ts-ignore
|
|
170
|
+
seeds: seeds_parsed
|
|
171
|
+
});
|
|
172
|
+
return pda;
|
|
173
|
+
}
|
|
174
|
+
export function lamportsToSol(lamports) {
|
|
175
|
+
function _trimTail(str) {
|
|
176
|
+
let clean = "";
|
|
177
|
+
let i = str.indexOf(".");
|
|
178
|
+
if (i === -1) {
|
|
179
|
+
return str;
|
|
180
|
+
}
|
|
181
|
+
while (true) {
|
|
182
|
+
const last = str.length - 1;
|
|
183
|
+
if (str[last] === "0") {
|
|
184
|
+
if (str[last - 1] !== ".") {
|
|
185
|
+
str = str.substring(0, str.length - 1);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
return str;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
return str;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
let str = lamports.toString();
|
|
197
|
+
if (str.length <= 9) {
|
|
198
|
+
let pad = 9 - str.length;
|
|
199
|
+
return _trimTail("0." + zeroes(pad) + str);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
let head = str.substring(0, str.length - 9);
|
|
203
|
+
if (head.length === 0) {
|
|
204
|
+
head = "0";
|
|
205
|
+
}
|
|
206
|
+
const tail = str.substring(head.length);
|
|
207
|
+
return _trimTail(head + "." + tail);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export function roundSol(sol) {
|
|
211
|
+
let lamports = solToLamports(sol);
|
|
212
|
+
let length = lamports.toString().length;
|
|
213
|
+
let shave = (length <= 9) ? (9 - length + 3) : 3;
|
|
214
|
+
return Math.floor(sol * (10 ** shave)) / (10 ** shave);
|
|
215
|
+
}
|
|
216
|
+
export function zeroes(length) {
|
|
217
|
+
return "".padEnd(length, "0");
|
|
218
|
+
}
|
|
219
|
+
export function solToLamports(sol) {
|
|
220
|
+
let str = sol.toString();
|
|
221
|
+
const decimals = 9;
|
|
222
|
+
let elements = str.split(".");
|
|
223
|
+
if (elements.length === 1) {
|
|
224
|
+
return BigInt(elements[0] + zeroes(9));
|
|
225
|
+
}
|
|
226
|
+
else if (elements[1].length <= 9) {
|
|
227
|
+
return BigInt(elements[0] + (elements[1].padEnd(9, "0")));
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return BigInt(elements[0] + (elements[1].substring(0, 9)));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
export function safeMultiply(num0, num1) {
|
|
234
|
+
if (num0 === 0 && num1 === 0)
|
|
235
|
+
return 0;
|
|
236
|
+
const N = 10 ** 9;
|
|
237
|
+
const lam0 = BigInt(Math.floor(num0 * N));
|
|
238
|
+
const lam1 = BigInt(Math.floor(num1 * N));
|
|
239
|
+
let lam = lam0 * lam1 / (10n ** 9n);
|
|
240
|
+
return Number(lamportsToSol(lam));
|
|
241
|
+
}
|
|
242
|
+
export function sleep(ms) {
|
|
243
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
244
|
+
}
|
|
245
|
+
export function fnv32a() {
|
|
246
|
+
let str = JSON.stringify(arguments);
|
|
247
|
+
let hval = 0x811c9dc5;
|
|
248
|
+
for (let i = 0; i < str.length; ++i) {
|
|
249
|
+
hval ^= str.charCodeAt(i);
|
|
250
|
+
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
|
|
251
|
+
}
|
|
252
|
+
return hval >>> 0;
|
|
253
|
+
}
|
|
254
|
+
export function now(ms = false) {
|
|
255
|
+
let t = BigInt(Date.now());
|
|
256
|
+
if (ms) {
|
|
257
|
+
return t;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
return t / 1000n;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
export function parseSeconds(time) {
|
|
264
|
+
function p(str) {
|
|
265
|
+
return String(str).padStart(2, "0");
|
|
266
|
+
}
|
|
267
|
+
let seconds = time % 60n;
|
|
268
|
+
time /= 60n;
|
|
269
|
+
let minutes = time % 60n;
|
|
270
|
+
time /= 60n;
|
|
271
|
+
let hours = time % 24n;
|
|
272
|
+
let days = time / 24n;
|
|
273
|
+
let parsed = p(hours) + ":" + p(minutes) + ":" + p(seconds);
|
|
274
|
+
if (days) {
|
|
275
|
+
parsed = p(days) + parsed;
|
|
276
|
+
}
|
|
277
|
+
return parsed;
|
|
278
|
+
}
|
|
279
|
+
export function parseMilliseconds(time) {
|
|
280
|
+
let milliseconds = time % 1000n;
|
|
281
|
+
time /= 1000n;
|
|
282
|
+
let ms = String(milliseconds).padStart(3, "0");
|
|
283
|
+
let parsed = parseSeconds(time);
|
|
284
|
+
return parsed + "." + ms;
|
|
285
|
+
}
|
|
286
|
+
export function isSameKey(key0, key1) {
|
|
287
|
+
return key0.toString() === key1.toString();
|
|
288
|
+
}
|
|
289
|
+
export function readStorage(key, defaultVal = null) {
|
|
290
|
+
return browser ? window.localStorage.getItem(key) ?? defaultVal : defaultVal;
|
|
291
|
+
}
|
|
292
|
+
export function writeStorage(key, value) {
|
|
293
|
+
localStorage.setItem(key, value);
|
|
294
|
+
}
|
|
295
|
+
export function clearStorage(key) {
|
|
296
|
+
localStorage.removeItem(key);
|
|
297
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type UiWallet, type UiWalletAccount } from "@wallet-standard/ui";
|
|
2
|
+
export declare const WALLET_STATE: {
|
|
3
|
+
INITIAL: string;
|
|
4
|
+
CONNECTING: string;
|
|
5
|
+
CONNECTED: string;
|
|
6
|
+
ERROR: string;
|
|
7
|
+
};
|
|
8
|
+
export declare let walletState: import("svelte/store").Writable<string>;
|
|
9
|
+
export declare const walletInitial: import("svelte/store").Readable<boolean>;
|
|
10
|
+
export declare const walletConnecting: import("svelte/store").Readable<boolean>;
|
|
11
|
+
export declare const walletConnected: import("svelte/store").Readable<boolean>;
|
|
12
|
+
export declare const walletError: import("svelte/store").Readable<boolean>;
|
|
13
|
+
export declare let signer: null | TransactionSendingSigner;
|
|
14
|
+
export declare let myKey: string | null;
|
|
15
|
+
export declare let me: import("svelte/store").Writable<string | null>;
|
|
16
|
+
export declare let myKeyString: string | null;
|
|
17
|
+
export type NetworkType = "mainnet" | "devnet" | "testnet";
|
|
18
|
+
export declare function isMe(address: any): boolean;
|
|
19
|
+
export declare function getStoredWalletSelection(): string | null;
|
|
20
|
+
export declare let connectedWalletAccount: UiWalletAccount | null;
|
|
21
|
+
export declare let hasConnectedWalletAccount: import("svelte/store").Writable<boolean>;
|
|
22
|
+
export declare let connectedWallet: UiWallet | null;
|
|
23
|
+
export declare let hasConnectedWallet: import("svelte/store").Writable<boolean>;
|
|
24
|
+
export declare function disconnectWallet(): Promise<void>;
|
|
25
|
+
import type { TransactionSendingSigner } from "@solana/signers";
|
|
26
|
+
export declare function initialiseWallet(): void;
|
|
27
|
+
export declare function unmountWallet(): void;
|
|
28
|
+
export declare const onWalletConnect: {
|
|
29
|
+
(callback: import("./utils.js").Callback): () => void;
|
|
30
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
31
|
+
killAll: () => void;
|
|
32
|
+
};
|
|
33
|
+
export declare const onWalletDisconnect: {
|
|
34
|
+
(callback: import("./utils.js").Callback): () => void;
|
|
35
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
36
|
+
killAll: () => void;
|
|
37
|
+
};
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
let log = console.log;
|
|
2
|
+
import { writable, derived } from "svelte/store";
|
|
3
|
+
import { Event } from "./utils.js";
|
|
4
|
+
import { getNetworkType } from "./connection.js";
|
|
5
|
+
import { _selection, onSelectWallet, expectWallet, clearSelectedVars, unmountTeardown } from "./walletSelection.js";
|
|
6
|
+
import { clearStorage, readStorage, writeStorage } from "./utils.js";
|
|
7
|
+
import { getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from "@wallet-standard/ui-registry";
|
|
8
|
+
import { getWalletAccountFeature, getWalletFeature } from "@wallet-standard/ui";
|
|
9
|
+
import { StandardConnect, StandardDisconnect } from "@wallet-standard/features";
|
|
10
|
+
import { SolanaSignAndSendTransaction } from "@solana/wallet-standard-features";
|
|
11
|
+
import { address } from "@solana/kit";
|
|
12
|
+
import { getTransactionEncoder } from "@solana/transactions";
|
|
13
|
+
import { getAbortablePromise } from "@solana/promises";
|
|
14
|
+
export const WALLET_STATE = {
|
|
15
|
+
INITIAL: "INITIAL",
|
|
16
|
+
CONNECTING: "CONNECTING",
|
|
17
|
+
CONNECTED: "CONNECTED",
|
|
18
|
+
ERROR: "ERROR",
|
|
19
|
+
};
|
|
20
|
+
const INITIAL = WALLET_STATE.INITIAL;
|
|
21
|
+
const CONNECTING = WALLET_STATE.CONNECTING;
|
|
22
|
+
const CONNECTED = WALLET_STATE.CONNECTED;
|
|
23
|
+
const ERROR = WALLET_STATE.ERROR;
|
|
24
|
+
export let walletState = writable(WALLET_STATE.INITIAL);
|
|
25
|
+
function _setWalletState(state) {
|
|
26
|
+
walletState.set(state);
|
|
27
|
+
}
|
|
28
|
+
// Helper vars
|
|
29
|
+
export const walletInitial = derived(walletState, ($state) => {
|
|
30
|
+
return $state === WALLET_STATE.INITIAL;
|
|
31
|
+
});
|
|
32
|
+
export const walletConnecting = derived(walletState, ($state) => {
|
|
33
|
+
return $state === WALLET_STATE.CONNECTING;
|
|
34
|
+
});
|
|
35
|
+
export const walletConnected = derived(walletState, ($state) => {
|
|
36
|
+
return $state === WALLET_STATE.CONNECTED;
|
|
37
|
+
});
|
|
38
|
+
export const walletError = derived(walletState, ($state) => {
|
|
39
|
+
return $state === WALLET_STATE.ERROR;
|
|
40
|
+
});
|
|
41
|
+
export let signer;
|
|
42
|
+
export let myKey = null;
|
|
43
|
+
export let me = writable(myKey);
|
|
44
|
+
export let myKeyString = null;
|
|
45
|
+
export function isMe(address) {
|
|
46
|
+
if (!address || !myKey)
|
|
47
|
+
return false;
|
|
48
|
+
return myKey.toString() === address.toString();
|
|
49
|
+
}
|
|
50
|
+
// Wallet Storage
|
|
51
|
+
function storeWalletSelection(name) {
|
|
52
|
+
writeStorage("walletSelection", name);
|
|
53
|
+
}
|
|
54
|
+
export function getStoredWalletSelection() {
|
|
55
|
+
return readStorage("walletSelection", null);
|
|
56
|
+
}
|
|
57
|
+
function clearStoredWalletSelection() {
|
|
58
|
+
clearStorage("walletSelection");
|
|
59
|
+
}
|
|
60
|
+
//@ts-ignore
|
|
61
|
+
export let connectedWalletAccount = null;
|
|
62
|
+
//@ts-ignore
|
|
63
|
+
export let hasConnectedWalletAccount = writable(false);
|
|
64
|
+
//@ts-ignore
|
|
65
|
+
export let connectedWallet = null;
|
|
66
|
+
//@ts-ignore
|
|
67
|
+
export let hasConnectedWallet = writable(false);
|
|
68
|
+
function uiWalletAccountsAreSame(a, b) {
|
|
69
|
+
if (a.address !== b.address) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const underlyingWalletA = getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(a);
|
|
73
|
+
const underlyingWalletB = getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(b);
|
|
74
|
+
return underlyingWalletA === underlyingWalletB;
|
|
75
|
+
}
|
|
76
|
+
async function connectWallet(uiWallet) {
|
|
77
|
+
// log("===---===---===")
|
|
78
|
+
// log("connect ui wallet", uiWallet);
|
|
79
|
+
_setWalletState(CONNECTING);
|
|
80
|
+
// Get some sorta thing
|
|
81
|
+
const wallet = getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWallet);
|
|
82
|
+
// log(2)
|
|
83
|
+
// log(wallet);
|
|
84
|
+
// Accounts that are available?
|
|
85
|
+
const existingAccounts = [...uiWallet.accounts];
|
|
86
|
+
// log(3)
|
|
87
|
+
// log(existingAccounts);
|
|
88
|
+
// Do some stuff to get the ability to connect
|
|
89
|
+
const connectFeature = getWalletFeature(uiWallet, StandardConnect);
|
|
90
|
+
// log(4)
|
|
91
|
+
// log(connectFeature)
|
|
92
|
+
// Wallet accounts for all the wallets in this connected one maybe
|
|
93
|
+
const accountsPromise = connectFeature
|
|
94
|
+
.connect()
|
|
95
|
+
.then(({ accounts }) => {
|
|
96
|
+
// console.log("connected", accounts);
|
|
97
|
+
// return accounts;
|
|
98
|
+
return accounts.map(getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.bind(null, wallet));
|
|
99
|
+
});
|
|
100
|
+
// log(5)
|
|
101
|
+
// log(accountsPromise);
|
|
102
|
+
// I don't know whhy this is called nextAccounts. Some other layer of wallet type.
|
|
103
|
+
const nextAccounts = await accountsPromise;
|
|
104
|
+
// console.log("next accounts", nextAccounts);
|
|
105
|
+
// log(6)
|
|
106
|
+
// log(nextAccounts)
|
|
107
|
+
// Just set connectedWallet
|
|
108
|
+
connectedWallet = uiWallet;
|
|
109
|
+
hasConnectedWallet.set(true);
|
|
110
|
+
//TODO: not sure what this is doing
|
|
111
|
+
// Try to choose the first never-before-seen account. Gonna assume this is the right move for now
|
|
112
|
+
for (const nextAccount of nextAccounts) {
|
|
113
|
+
if (!existingAccounts.some((existingAccount) => uiWalletAccountsAreSame(nextAccount, existingAccount))) {
|
|
114
|
+
// onAccountSelect(nextAccount);
|
|
115
|
+
// console.error("Found not seen account, set as connectedWalletAccount");
|
|
116
|
+
connectedWalletAccount = nextAccount;
|
|
117
|
+
hasConnectedWalletAccount.set(true);
|
|
118
|
+
_setWalletState(CONNECTED);
|
|
119
|
+
// localStorage.setItem(
|
|
120
|
+
// STORAGE_KEY,
|
|
121
|
+
// `${wallet.name}:${connectedWalletAccount.address}`,
|
|
122
|
+
// );
|
|
123
|
+
// console.log("compare accounts", connectedWalletAccount);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (nextAccounts.length > 0) {
|
|
128
|
+
// onAccountSelect(nextAccounts[0]);
|
|
129
|
+
// console.error("Fallback to just using the first one available")
|
|
130
|
+
connectedWalletAccount = nextAccounts[0];
|
|
131
|
+
hasConnectedWalletAccount.set(true);
|
|
132
|
+
_setWalletState(CONNECTED);
|
|
133
|
+
// localStorage.setItem(
|
|
134
|
+
// STORAGE_KEY,
|
|
135
|
+
// `${wallet.name}:${connectedWalletAccount.address}`,
|
|
136
|
+
// );
|
|
137
|
+
// console.log("default accounts", connectedWalletAccount);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.error("No available wallets found for unspecified reason");
|
|
141
|
+
_setWalletState(INITIAL);
|
|
142
|
+
}
|
|
143
|
+
export async function disconnectWallet() {
|
|
144
|
+
log("disconnect wallet..");
|
|
145
|
+
clearStoredWalletSelection();
|
|
146
|
+
clearSelectedVars();
|
|
147
|
+
if (!connectedWallet) {
|
|
148
|
+
// connected wallet var empty. just clear vars.
|
|
149
|
+
// one or both expected account objects are missing
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const disconnectFeature = getWalletFeature(connectedWallet, StandardDisconnect);
|
|
153
|
+
await disconnectFeature.disconnect();
|
|
154
|
+
}
|
|
155
|
+
connectedWallet = null;
|
|
156
|
+
hasConnectedWallet.set(false);
|
|
157
|
+
connectedWalletAccount = null;
|
|
158
|
+
hasConnectedWalletAccount.set(false);
|
|
159
|
+
signer = null;
|
|
160
|
+
myKey = null;
|
|
161
|
+
me.set(null);
|
|
162
|
+
_setWalletState(INITIAL);
|
|
163
|
+
log("disconnected. triggering onWalletDisconnect");
|
|
164
|
+
onWalletDisconnect.trigger();
|
|
165
|
+
}
|
|
166
|
+
function _buildTransactionSendingSigner(_networkType) {
|
|
167
|
+
if (!connectedWalletAccount)
|
|
168
|
+
throw new Error("connectedWalletAccount is null");
|
|
169
|
+
const signAndSendTransactionFeature = getWalletAccountFeature(connectedWalletAccount, SolanaSignAndSendTransaction);
|
|
170
|
+
const transactionSendingSigner = {
|
|
171
|
+
address: address(connectedWalletAccount.address),
|
|
172
|
+
async signAndSendTransactions(transactions, config = {}) {
|
|
173
|
+
const { abortSignal, ...options } = config;
|
|
174
|
+
abortSignal?.throwIfAborted();
|
|
175
|
+
const transactionEncoder = getTransactionEncoder();
|
|
176
|
+
if (transactions.length > 1) {
|
|
177
|
+
//TODO: pretty sure this is just for this specific case
|
|
178
|
+
throw new Error("should only have one transaction");
|
|
179
|
+
}
|
|
180
|
+
if (transactions.length === 0) {
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
if (!connectedWalletAccount) {
|
|
184
|
+
throw new Error("no connected wallet account");
|
|
185
|
+
}
|
|
186
|
+
const [transaction] = transactions;
|
|
187
|
+
const wireTransactionBytes = transactionEncoder.encode(transaction);
|
|
188
|
+
const walletAccount = getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(connectedWalletAccount);
|
|
189
|
+
let chain = `solana:${getNetworkType()}`;
|
|
190
|
+
const inputWithOptions = {
|
|
191
|
+
...options,
|
|
192
|
+
transaction: wireTransactionBytes,
|
|
193
|
+
chain,
|
|
194
|
+
account: walletAccount,
|
|
195
|
+
};
|
|
196
|
+
const [{ signature }] = await getAbortablePromise(signAndSendTransactionFeature.signAndSendTransaction(inputWithOptions), abortSignal);
|
|
197
|
+
return Object.freeze([signature]);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
return transactionSendingSigner;
|
|
201
|
+
}
|
|
202
|
+
// Done by main init
|
|
203
|
+
export function initialiseWallet() {
|
|
204
|
+
let networkType = getNetworkType();
|
|
205
|
+
if (!networkType)
|
|
206
|
+
throw new Error("Network not configured.");
|
|
207
|
+
onSelectWallet(async () => {
|
|
208
|
+
if (!_selection || !_selection.wallet)
|
|
209
|
+
throw new Error("Wallet selection is null");
|
|
210
|
+
// They just specified which wallet they are gonna use it didn't connect. Connect is done else where
|
|
211
|
+
// Connect wallet
|
|
212
|
+
await connectWallet(_selection.wallet.uiWallet);
|
|
213
|
+
signer = _buildTransactionSendingSigner(networkType);
|
|
214
|
+
myKey = signer.address;
|
|
215
|
+
myKeyString = signer.address;
|
|
216
|
+
me.set(myKey.toString());
|
|
217
|
+
// Update local storage
|
|
218
|
+
storeWalletSelection(_selection.name);
|
|
219
|
+
onWalletConnect.trigger();
|
|
220
|
+
});
|
|
221
|
+
initialWalletCheck();
|
|
222
|
+
}
|
|
223
|
+
export function unmountWallet() {
|
|
224
|
+
unmountTeardown();
|
|
225
|
+
}
|
|
226
|
+
function initialWalletCheck() {
|
|
227
|
+
//Called by walletSelection.ts after it first sees what wallets it has
|
|
228
|
+
if (getStoredWalletSelection()) {
|
|
229
|
+
expectWallet(getStoredWalletSelection());
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
export const onWalletConnect = Event();
|
|
233
|
+
export const onWalletDisconnect = Event();
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { WalletWithFeatures } from "@wallet-standard/base";
|
|
2
|
+
import { type StandardEventsFeature } from "@wallet-standard/features";
|
|
3
|
+
import type { UiWallet } from "@wallet-standard/ui";
|
|
4
|
+
type WalletMapElement = {
|
|
5
|
+
wallet: WalletWithFeatures<StandardEventsFeature>;
|
|
6
|
+
uiWallet: UiWallet;
|
|
7
|
+
};
|
|
8
|
+
export declare let availableWallets: string[];
|
|
9
|
+
export declare function recheckWallets(): void;
|
|
10
|
+
export declare let _selection: {
|
|
11
|
+
selected: boolean;
|
|
12
|
+
wallet: null | WalletMapElement;
|
|
13
|
+
name: null | string;
|
|
14
|
+
};
|
|
15
|
+
export declare let selection: import("svelte/store").Writable<{
|
|
16
|
+
selected: boolean;
|
|
17
|
+
wallet: null | WalletMapElement;
|
|
18
|
+
name: null | string;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function clearSelectedVars(): void;
|
|
21
|
+
export declare const onSelectWallet: {
|
|
22
|
+
(callback: import("./utils.js").Callback): () => void;
|
|
23
|
+
trigger: (...args: any[]) => Promise<void>;
|
|
24
|
+
killAll: () => void;
|
|
25
|
+
};
|
|
26
|
+
export declare function selectWallet(name: string): Promise<void>;
|
|
27
|
+
export declare function getWalletInfo(name: string): string;
|
|
28
|
+
export declare function expectWallet(name: string): void;
|
|
29
|
+
export declare function stopExpectWallet(): void;
|
|
30
|
+
export declare function unmountTeardown(): void;
|
|
31
|
+
export {};
|