@kheopskit/core 0.0.1-alpha.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.
@@ -0,0 +1,22 @@
1
+ import type { SS58String } from "polkadot-api";
2
+
3
+ import { isValidAddress } from "./isValidAddress";
4
+
5
+ export type AccountId = string;
6
+
7
+ export const getAccountId = (
8
+ walletId: string,
9
+ address: SS58String
10
+ ): AccountId => {
11
+ if (!walletId) throw new Error("Missing walletId");
12
+ if (!isValidAddress(address)) throw new Error("Invalid address");
13
+ return `${walletId}::${address}`;
14
+ };
15
+
16
+ export const parseAccountId = (accountId: string) => {
17
+ if (!accountId) throw new Error("Invalid walletAccountId");
18
+ const [walletId, address] = accountId.split("::");
19
+ if (!walletId) throw new Error("Missing walletId");
20
+ if (!address || !isValidAddress(address)) throw new Error("Invalid address");
21
+ return { walletId, address };
22
+ };
@@ -0,0 +1,21 @@
1
+ import type { WalletPlatform } from "@/api/types";
2
+ import { isWalletPlatform } from "./isWalletPlatform";
3
+
4
+ export type WalletId = string;
5
+
6
+ export const getWalletId = (
7
+ platform: WalletPlatform,
8
+ identifier: string
9
+ ): WalletId => {
10
+ if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
11
+ if (!identifier) throw new Error("Invalid name");
12
+ return `${platform}:${identifier}`;
13
+ };
14
+
15
+ export const parseWalletId = (walletId: string) => {
16
+ if (!walletId) throw new Error("Invalid walletId");
17
+ const [platform, identifier] = walletId.split(":");
18
+ if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
19
+ if (!identifier) throw new Error("Invalid address");
20
+ return { platform, identifier };
21
+ };
@@ -0,0 +1,45 @@
1
+ import { BehaviorSubject, filter, fromEvent, map } from "rxjs";
2
+
3
+ export const createStore = <T>(key: string, defaultValue: T) => {
4
+ const subject = new BehaviorSubject<T>(getStoredData(key, defaultValue));
5
+
6
+ // Cross-tab sync via 'storage' event (won't fire if key is updated from same tab)
7
+ fromEvent<StorageEvent>(window, "storage")
8
+ .pipe(
9
+ filter((event) => event.key === key),
10
+ map((event) => parseData(event.newValue, defaultValue))
11
+ )
12
+ .subscribe((newValue) => subject.next(newValue));
13
+
14
+ const update = (val: T) => {
15
+ setStoredData(key, val);
16
+ subject.next(val);
17
+ };
18
+
19
+ return {
20
+ observable: subject.asObservable(),
21
+ set: (val: T) => update(val),
22
+ mutate: (transform: (prev: T) => T) =>
23
+ update(transform(subject.getValue())),
24
+ get: () => structuredClone(subject.getValue()),
25
+ };
26
+ };
27
+
28
+ const parseData = <T>(str: string | null, defaultValue: T): T => {
29
+ try {
30
+ if (str) return JSON.parse(str);
31
+ } catch {
32
+ // invalid data
33
+ }
34
+ return defaultValue;
35
+ };
36
+
37
+ const getStoredData = <T>(key: string, defaultValue: T): T => {
38
+ const str = localStorage.getItem(key);
39
+ return parseData(str, defaultValue);
40
+ };
41
+
42
+ const setStoredData = <T>(key: string, val: T) => {
43
+ const str = JSON.stringify(val);
44
+ localStorage.setItem(key, str);
45
+ };
@@ -0,0 +1,10 @@
1
+ import { isEthereumAddress } from "./isEthereumAddress";
2
+ import { isSs58Address } from "./isSs58Address";
3
+ import type { AccountAddressType } from "./types";
4
+
5
+ export const getAccountAddressType = (address: string): AccountAddressType => {
6
+ if (address.startsWith("0x")) {
7
+ if (isEthereumAddress(address)) return "ethereum";
8
+ } else if (isSs58Address(address)) return "ss58";
9
+ throw new Error("Invalid address");
10
+ };
@@ -0,0 +1,12 @@
1
+ // export all modules from this folder, except exports.ts
2
+ export * from "./createStore";
3
+ export * from "./getAccountAddressType";
4
+ export * from "./AccountId";
5
+ export * from "./isEthereumAddress";
6
+ export * from "./isSs58Address";
7
+ export * from "./isTruthy";
8
+ export * from "./isValidAddress";
9
+ export * from "./sleep";
10
+ export * from "./throwAfter";
11
+ export * from "./types";
12
+ export * from "./isWalletPlatform";
@@ -0,0 +1,2 @@
1
+ export const isEthereumAddress = (address: string): boolean =>
2
+ /^0x[a-fA-F0-9]{40}$/.test(address);
@@ -0,0 +1,15 @@
1
+ import { AccountId, type SS58String } from "polkadot-api";
2
+
3
+ const accountIdEncoder = AccountId().enc;
4
+
5
+ export const isSs58Address = (
6
+ address: SS58String | string
7
+ ): address is SS58String => {
8
+ try {
9
+ if (!address) return false;
10
+ accountIdEncoder(address);
11
+ return true;
12
+ } catch (_err) {
13
+ return false;
14
+ }
15
+ };
@@ -0,0 +1,3 @@
1
+ export const isTruthy = <T>(
2
+ value: T | null | undefined | false | 0 | ""
3
+ ): value is T => !!value;
@@ -0,0 +1,8 @@
1
+ import { isEthereumAddress } from "./isEthereumAddress";
2
+ import { isSs58Address } from "./isSs58Address";
3
+
4
+ export const isValidAddress = (address: string): boolean => {
5
+ return address.startsWith("0x")
6
+ ? isEthereumAddress(address)
7
+ : isSs58Address(address);
8
+ };
@@ -0,0 +1,7 @@
1
+ import type { WalletPlatform } from "@/api/types";
2
+
3
+ export const isWalletPlatform = (
4
+ platform: unknown
5
+ ): platform is WalletPlatform =>
6
+ typeof platform === "string" &&
7
+ ["polkadot", "ethereum"].includes(platform as WalletPlatform);
@@ -0,0 +1,2 @@
1
+ export const sleep = (ms: number) =>
2
+ new Promise((resolve) => setTimeout(resolve, ms));
@@ -0,0 +1,6 @@
1
+ export const throwAfter = (ms: number, message?: string) =>
2
+ new Promise<never>((_, reject) => {
3
+ setTimeout(() => {
4
+ reject(new Error(message ?? "Timeout"));
5
+ }, ms);
6
+ });
@@ -0,0 +1,5 @@
1
+ // TODO: mnve file to its own @kheopswap/types package ?
2
+
3
+ export type AccountAddressType = "ss58" | "ethereum";
4
+
5
+ export type UnsubscribeFn = () => void;
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.base",
3
+ "include": ["src", "tests"],
4
+ "compilerOptions": {
5
+ "baseUrl": "src",
6
+ "paths": {
7
+ "@/*": ["*"]
8
+ }
9
+ }
10
+ }