@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.
- package/CHANGELOG.md +7 -0
- package/dist/index.d.mts +94 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +458 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +474 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -0
- package/src/api/accounts.ts +30 -0
- package/src/api/config.ts +12 -0
- package/src/api/ethereum/accounts.ts +86 -0
- package/src/api/ethereum/wallets.ts +103 -0
- package/src/api/index.ts +2 -0
- package/src/api/kheopskit.ts +23 -0
- package/src/api/polkadot/accounts.ts +71 -0
- package/src/api/polkadot/wallets.ts +103 -0
- package/src/api/store.ts +33 -0
- package/src/api/types.ts +92 -0
- package/src/api/wallets.ts +70 -0
- package/src/index.ts +3 -0
- package/src/utils/AccountId.ts +22 -0
- package/src/utils/WalletId.ts +21 -0
- package/src/utils/createStore.ts +45 -0
- package/src/utils/getAccountAddressType.ts +10 -0
- package/src/utils/index.ts +12 -0
- package/src/utils/isEthereumAddress.ts +2 -0
- package/src/utils/isSs58Address.ts +15 -0
- package/src/utils/isTruthy.ts +3 -0
- package/src/utils/isValidAddress.ts +8 -0
- package/src/utils/isWalletPlatform.ts +7 -0
- package/src/utils/sleep.ts +2 -0
- package/src/utils/throwAfter.ts +6 -0
- package/src/utils/types.ts +5 -0
- package/tsconfig.json +10 -0
|
@@ -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,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,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
|
+
};
|