@ab-org/sdk-core 0.1.0 → 0.1.2-beta.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.
Files changed (48) hide show
  1. package/README.md +29 -0
  2. package/dist/chunk-5HURLKIK.js +1005 -0
  3. package/dist/chunk-MVZIAM4H.js +363 -0
  4. package/dist/cubeSignerAuth-DrPc9FeB.d.ts +426 -0
  5. package/dist/index.d.ts +237 -16
  6. package/dist/index.js +1119 -16
  7. package/dist/social-auth.d.ts +11 -0
  8. package/dist/social-auth.js +1 -0
  9. package/dist/social-provider-Bq58TBRt.d.ts +88 -0
  10. package/dist/social-provider.d.ts +2 -0
  11. package/dist/social-provider.js +2 -0
  12. package/dist/social.d.ts +3 -0
  13. package/dist/social.js +2 -0
  14. package/package.json +24 -6
  15. package/dist/core/capabilities.d.ts +0 -32
  16. package/dist/core/capabilities.js +0 -88
  17. package/dist/core/chains.d.ts +0 -23
  18. package/dist/core/chains.js +0 -83
  19. package/dist/core/errors.d.ts +0 -9
  20. package/dist/core/errors.js +0 -51
  21. package/dist/core/sessionStore.d.ts +0 -26
  22. package/dist/core/sessionStore.js +0 -129
  23. package/dist/core/types.d.ts +0 -75
  24. package/dist/core/types.js +0 -1
  25. package/dist/core/walletConnector.d.ts +0 -29
  26. package/dist/core/walletConnector.js +0 -153
  27. package/dist/core/walletExecution.d.ts +0 -22
  28. package/dist/core/walletExecution.js +0 -89
  29. package/dist/hooks/useAccount.d.ts +0 -11
  30. package/dist/hooks/useAccount.js +0 -32
  31. package/dist/hooks/useWalletConnect.d.ts +0 -16
  32. package/dist/hooks/useWalletConnect.js +0 -24
  33. package/dist/providers/base.d.ts +0 -15
  34. package/dist/providers/base.js +0 -30
  35. package/dist/providers/plugin/injectedEvmProvider.d.ts +0 -112
  36. package/dist/providers/plugin/injectedEvmProvider.js +0 -352
  37. package/dist/providers/plugin/injectedWalletRegistry.d.ts +0 -9
  38. package/dist/providers/plugin/injectedWalletRegistry.js +0 -34
  39. package/dist/providers/plugin/metamaskProvider.d.ts +0 -1
  40. package/dist/providers/plugin/metamaskProvider.js +0 -1
  41. package/dist/providers/social/baseSocialProvider.d.ts +0 -21
  42. package/dist/providers/social/baseSocialProvider.js +0 -11
  43. package/dist/providers/social/cubeSignerAuth.d.ts +0 -89
  44. package/dist/providers/social/cubeSignerAuth.js +0 -128
  45. package/dist/providers/social/cubistEvmWalletProvider.d.ts +0 -9
  46. package/dist/providers/social/cubistEvmWalletProvider.js +0 -175
  47. package/dist/providers/social/cubistProvider.d.ts +0 -52
  48. package/dist/providers/social/cubistProvider.js +0 -160
@@ -0,0 +1,11 @@
1
+ import { b as CubeSignerSession, h as SupportedChain, i as WalletProvider } from './cubeSignerAuth-DrPc9FeB.js';
2
+ export { C as CubeSignerAuth, a as CubeSignerConfig, O as OidcLoginHooks, T as TwitterCodeExchangeParams } from './cubeSignerAuth-DrPc9FeB.js';
3
+
4
+ interface CreateCubistEvmWalletProviderArgs {
5
+ session: CubeSignerSession;
6
+ address: string;
7
+ chain: SupportedChain;
8
+ }
9
+ declare const createCubistEvmWalletProvider: ({ session, address, chain, }: CreateCubistEvmWalletProviderArgs) => WalletProvider;
10
+
11
+ export { CubeSignerSession, createCubistEvmWalletProvider };
@@ -0,0 +1 @@
1
+ export { CubeSignerAuth, createCubistEvmWalletProvider } from './chunk-5HURLKIK.js';
@@ -0,0 +1,88 @@
1
+ import { d as WalletAdapter, P as ProviderCategory, c as WalletSession, e as WalletConnectArgs, i as WalletProvider, T as TwitterCodeExchangeParams, b as CubeSignerSession, a as CubeSignerConfig, C as CubeSignerAuth } from './cubeSignerAuth-DrPc9FeB.js';
2
+
3
+ declare abstract class AbstractProvider implements WalletAdapter {
4
+ abstract readonly id: string;
5
+ abstract readonly title: string;
6
+ abstract readonly category: ProviderCategory;
7
+ protected currentSession: WalletSession | null;
8
+ abstract connect(args?: WalletConnectArgs): Promise<WalletSession>;
9
+ disconnect(): Promise<void>;
10
+ protected setSession(session: WalletSession): WalletSession;
11
+ protected clearSession(): void;
12
+ protected get session(): WalletSession | null;
13
+ protected requireSession(message?: string): WalletSession;
14
+ getProvider(): WalletProvider | null;
15
+ reconnect(_persistedSession: WalletSession): Promise<WalletSession | null>;
16
+ }
17
+
18
+ interface SocialLoginOptions {
19
+ hint?: string;
20
+ redirectUri?: string;
21
+ }
22
+ interface SocialConnectArgs extends WalletConnectArgs {
23
+ options?: SocialLoginOptions;
24
+ }
25
+ declare abstract class AbstractSocialProvider extends AbstractProvider {
26
+ readonly category: "social";
27
+ /**
28
+ * Authenticate and cache a social-wallet session.
29
+ *
30
+ * Some providers only need `options`, while others may require an
31
+ * additional credential payload (for example an OIDC token or OAuth result).
32
+ */
33
+ abstract signIn(options?: SocialLoginOptions, payload?: unknown): Promise<void>;
34
+ abstract signOut(): Promise<void>;
35
+ connect(args?: WalletConnectArgs): Promise<WalletSession>;
36
+ }
37
+
38
+ type CubistLoginMethod = {
39
+ type: "google";
40
+ idToken: string;
41
+ } | {
42
+ type: "twitter";
43
+ params: TwitterCodeExchangeParams;
44
+ };
45
+ interface CubistSessionAdapter {
46
+ /**
47
+ * Convert a raw CubeSigner session into a WalletSession
48
+ * (resolve the first key's address, chain, build a provider, etc.).
49
+ */
50
+ resolve(session: CubeSignerSession): Promise<WalletSession>;
51
+ }
52
+ declare class CubistSocialProvider extends AbstractSocialProvider {
53
+ readonly id = "cubist";
54
+ readonly title = "Cubist Social Wallet";
55
+ private auth;
56
+ private adapter;
57
+ private lastCubeSignerSession;
58
+ /** Stash the login method so `connect()` can auto-retry. */
59
+ private lastLogin;
60
+ constructor(config: CubeSignerConfig, adapter?: CubistSessionAdapter);
61
+ private createWalletSession;
62
+ private authenticate;
63
+ private getResolvedLoginMethod;
64
+ /**
65
+ * Preferred explicit sign-in entrypoint.
66
+ */
67
+ signInWith(method: CubistLoginMethod, _options?: SocialLoginOptions): Promise<void>;
68
+ /**
69
+ * Backward-compatible abstract implementation.
70
+ *
71
+ * - If `payload` is provided, it is treated as the login method.
72
+ * - Otherwise reuses the previously cached login method if available.
73
+ */
74
+ signIn(_options?: SocialLoginOptions, payload?: unknown): Promise<void>;
75
+ /**
76
+ * Explicit connect flow that both authenticates and returns the wallet session.
77
+ */
78
+ connectWith(method: CubistLoginMethod, options?: SocialLoginOptions): Promise<WalletSession>;
79
+ connect(args?: WalletConnectArgs): Promise<WalletSession>;
80
+ reconnect(persistedSession: WalletSession): Promise<WalletSession | null>;
81
+ signOut(): Promise<void>;
82
+ disconnect(): Promise<void>;
83
+ /** Direct access to the underlying CubeSignerAuth for advanced use-cases. */
84
+ get cubeSignerAuth(): CubeSignerAuth;
85
+ get cubeSignerSession(): CubeSignerSession | null;
86
+ }
87
+
88
+ export { AbstractSocialProvider as A, type CubistLoginMethod as C, type SocialConnectArgs as S, type CubistSessionAdapter as a, CubistSocialProvider as b, type SocialLoginOptions as c, AbstractProvider as d };
@@ -0,0 +1,2 @@
1
+ export { A as AbstractSocialProvider, C as CubistLoginMethod, a as CubistSessionAdapter, b as CubistSocialProvider, S as SocialConnectArgs, c as SocialLoginOptions } from './social-provider-Bq58TBRt.js';
2
+ import './cubeSignerAuth-DrPc9FeB.js';
@@ -0,0 +1,2 @@
1
+ export { AbstractSocialProvider, CubistSocialProvider } from './chunk-MVZIAM4H.js';
2
+ import './chunk-5HURLKIK.js';
@@ -0,0 +1,3 @@
1
+ export { C as CubeSignerAuth, a as CubeSignerConfig, b as CubeSignerSession, O as OidcLoginHooks, T as TwitterCodeExchangeParams } from './cubeSignerAuth-DrPc9FeB.js';
2
+ export { createCubistEvmWalletProvider } from './social-auth.js';
3
+ export { A as AbstractSocialProvider, C as CubistLoginMethod, a as CubistSessionAdapter, b as CubistSocialProvider, S as SocialConnectArgs, c as SocialLoginOptions } from './social-provider-Bq58TBRt.js';
package/dist/social.js ADDED
@@ -0,0 +1,2 @@
1
+ export { AbstractSocialProvider, CubistSocialProvider } from './chunk-MVZIAM4H.js';
2
+ export { CubeSignerAuth, createCubistEvmWalletProvider } from './chunk-5HURLKIK.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ab-org/sdk-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.2-beta.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**/*",
@@ -8,20 +8,38 @@
8
8
  ],
9
9
  "main": "dist/index.js",
10
10
  "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./social": {
17
+ "types": "./dist/social.d.ts",
18
+ "import": "./dist/social.js"
19
+ },
20
+ "./social/auth": {
21
+ "types": "./dist/social-auth.d.ts",
22
+ "import": "./dist/social-auth.js"
23
+ },
24
+ "./social/provider": {
25
+ "types": "./dist/social-provider.d.ts",
26
+ "import": "./dist/social-provider.js"
27
+ }
28
+ },
11
29
  "publishConfig": {
12
30
  "access": "public",
13
31
  "registry": "https://registry.npmjs.org/"
14
32
  },
15
33
  "dependencies": {
16
- "@cubist-labs/cubesigner-sdk": "^0.4.219",
17
- "eventemitter3": "^5.0.1",
18
- "@ab-org/wallet-utils": "0.0.7"
34
+ "eventemitter3": "^5.0.1"
19
35
  },
20
36
  "devDependencies": {
21
- "typescript": "^5.5.4"
37
+ "typescript": "^5.5.4",
38
+ "tsup": "^8.5.1",
39
+ "@ab-org/wallet-utils": "0.0.7"
22
40
  },
23
41
  "scripts": {
24
- "build": "tsc -p tsconfig.json",
42
+ "build": "pnpm exec tsup",
25
43
  "lint": "eslint 'src/**/*.{ts,tsx}'",
26
44
  "test": "vitest run --passWithNoTests"
27
45
  }
@@ -1,32 +0,0 @@
1
- import type { SupportedChain, SupportedToken, WalletSession } from "./types.js";
2
- export type WalletCapability = "eth_accounts" | "eth_requestAccounts" | "eth_chainId" | "eth_signTransaction" | "eth_sendTransaction" | "personal_sign" | "eth_signTypedData_v4" | "wallet_disconnect" | "wallet_rehydrate" | string;
3
- export interface SessionCapabilityPolicy {
4
- id: string;
5
- appId?: string;
6
- origin?: string;
7
- expiresAt?: number;
8
- methods?: WalletCapability[];
9
- chains?: SupportedChain[];
10
- tokens?: SupportedToken[];
11
- maxAmount?: string;
12
- revocable?: boolean;
13
- metadata?: Record<string, unknown>;
14
- }
15
- export interface SessionCapabilityCheck {
16
- capability: WalletCapability;
17
- chain?: SupportedChain;
18
- token?: SupportedToken;
19
- amount?: string | number | bigint;
20
- appId?: string;
21
- origin?: string;
22
- now?: number;
23
- }
24
- export declare class SessionCapabilityError extends Error {
25
- constructor(message: string);
26
- }
27
- export declare const createSessionCapabilityPolicy: (input?: Partial<SessionCapabilityPolicy>) => SessionCapabilityPolicy;
28
- export declare const isSessionExpired: (session: Pick<WalletSession, "expiresAt"> | null | undefined, now?: number) => boolean;
29
- export declare const isCapabilityPolicyExpired: (policy: SessionCapabilityPolicy | null | undefined, now?: number) => boolean;
30
- export declare const sessionSupportsCapability: (session: Pick<WalletSession, "capabilities"> | null | undefined, capability: WalletCapability) => boolean;
31
- export declare const describeSessionCapabilityPolicy: (policy: SessionCapabilityPolicy | null | undefined) => string[];
32
- export declare const assertSessionCapability: (session: WalletSession, check: SessionCapabilityCheck) => void;
@@ -1,88 +0,0 @@
1
- export class SessionCapabilityError extends Error {
2
- constructor(message) {
3
- super(message);
4
- this.name = "SessionCapabilityError";
5
- }
6
- }
7
- const toBigIntAmount = (value) => {
8
- if (typeof value === "bigint")
9
- return value;
10
- if (typeof value === "number")
11
- return BigInt(value);
12
- return BigInt(value);
13
- };
14
- export const createSessionCapabilityPolicy = (input = {}) => ({
15
- id: input.id ?? `cap:${Date.now()}`,
16
- appId: input.appId,
17
- origin: input.origin,
18
- expiresAt: input.expiresAt,
19
- methods: input.methods,
20
- chains: input.chains,
21
- tokens: input.tokens,
22
- maxAmount: input.maxAmount,
23
- revocable: input.revocable ?? true,
24
- metadata: input.metadata,
25
- });
26
- export const isSessionExpired = (session, now = Date.now()) => Boolean(session?.expiresAt && session.expiresAt <= now);
27
- export const isCapabilityPolicyExpired = (policy, now = Date.now()) => Boolean(policy?.expiresAt && policy.expiresAt <= now);
28
- export const sessionSupportsCapability = (session, capability) => {
29
- const capabilities = session?.capabilities;
30
- if (!capabilities || capabilities.length === 0)
31
- return true;
32
- return capabilities.includes(capability);
33
- };
34
- export const describeSessionCapabilityPolicy = (policy) => {
35
- if (!policy)
36
- return [];
37
- const lines = [];
38
- if (policy.origin)
39
- lines.push(`Origin: ${policy.origin}`);
40
- if (policy.appId)
41
- lines.push(`App: ${policy.appId}`);
42
- if (policy.methods?.length)
43
- lines.push(`Methods: ${policy.methods.join(", ")}`);
44
- if (policy.chains?.length)
45
- lines.push(`Chains: ${policy.chains.join(", ")}`);
46
- if (policy.tokens?.length)
47
- lines.push(`Tokens: ${policy.tokens.join(", ")}`);
48
- if (policy.maxAmount)
49
- lines.push(`Max amount: ${policy.maxAmount}`);
50
- if (policy.expiresAt)
51
- lines.push(`Expires at: ${new Date(policy.expiresAt).toISOString()}`);
52
- return lines;
53
- };
54
- export const assertSessionCapability = (session, check) => {
55
- const now = check.now ?? Date.now();
56
- if (isSessionExpired(session, now)) {
57
- throw new SessionCapabilityError("Wallet session has expired");
58
- }
59
- const policy = session.capabilityPolicy;
60
- if (isCapabilityPolicyExpired(policy, now)) {
61
- throw new SessionCapabilityError("Capability session has expired");
62
- }
63
- if (!sessionSupportsCapability(session, check.capability)) {
64
- throw new SessionCapabilityError(`Wallet session does not support capability "${check.capability}"`);
65
- }
66
- if (!policy)
67
- return;
68
- if (policy.appId && check.appId && policy.appId !== check.appId) {
69
- throw new SessionCapabilityError("Capability session is scoped to a different app");
70
- }
71
- if (policy.origin && check.origin && policy.origin !== check.origin) {
72
- throw new SessionCapabilityError("Capability session is scoped to a different origin");
73
- }
74
- if (policy.methods?.length && !policy.methods.includes(check.capability)) {
75
- throw new SessionCapabilityError(`Capability "${check.capability}" is not allowed for this session`);
76
- }
77
- if (check.chain && policy.chains?.length && !policy.chains.includes(check.chain)) {
78
- throw new SessionCapabilityError(`Chain "${check.chain}" is not allowed for this session`);
79
- }
80
- if (check.token && policy.tokens?.length && !policy.tokens.includes(check.token)) {
81
- throw new SessionCapabilityError(`Token "${check.token}" is not allowed for this session`);
82
- }
83
- if (policy.maxAmount && check.amount !== undefined) {
84
- if (toBigIntAmount(check.amount) > toBigIntAmount(policy.maxAmount)) {
85
- throw new SessionCapabilityError("Requested amount exceeds the capability session limit");
86
- }
87
- }
88
- };
@@ -1,23 +0,0 @@
1
- import type { SupportedChain } from "./types.js";
2
- export type ChainNamespace = "evm" | "solana" | "ab-core";
3
- export type ChainRpcFamily = "evm" | "solana" | "custom";
4
- export interface ChainDescriptor {
5
- chain: SupportedChain;
6
- namespace: ChainNamespace;
7
- reference: string;
8
- label: string;
9
- rpcFamily: ChainRpcFamily;
10
- evmChainId?: number;
11
- supportsSmartSessions: boolean;
12
- supportsAccountAbstraction: boolean;
13
- }
14
- export interface ChainContext {
15
- walletChain: SupportedChain;
16
- walletChainDescriptor: ChainDescriptor;
17
- settlementChain: SupportedChain;
18
- settlementChainDescriptor: ChainDescriptor;
19
- }
20
- export declare const getChainDescriptor: (chain: SupportedChain) => ChainDescriptor;
21
- export declare const getAllChainDescriptors: () => ChainDescriptor[];
22
- export declare const getSupportedChainFromEvmChainId: (value: string | number | bigint) => SupportedChain;
23
- export declare const createChainContext: (walletChain: SupportedChain, settlementChain?: SupportedChain) => ChainContext;
@@ -1,83 +0,0 @@
1
- const chainDescriptors = {
2
- AB_CORE: {
3
- chain: "AB_CORE",
4
- namespace: "ab-core",
5
- reference: "ab:core",
6
- label: "AB Core",
7
- rpcFamily: "custom",
8
- supportsSmartSessions: true,
9
- supportsAccountAbstraction: true,
10
- },
11
- ETH: {
12
- chain: "ETH",
13
- namespace: "evm",
14
- reference: "eip155:1",
15
- label: "Ethereum",
16
- rpcFamily: "evm",
17
- evmChainId: 1,
18
- supportsSmartSessions: true,
19
- supportsAccountAbstraction: true,
20
- },
21
- BSC: {
22
- chain: "BSC",
23
- namespace: "evm",
24
- reference: "eip155:56",
25
- label: "BNB Smart Chain",
26
- rpcFamily: "evm",
27
- evmChainId: 56,
28
- supportsSmartSessions: true,
29
- supportsAccountAbstraction: false,
30
- },
31
- ETH_TENDERLY: {
32
- chain: "ETH_TENDERLY",
33
- namespace: "evm",
34
- reference: "eip155:3030",
35
- label: "Ethereum Tenderly",
36
- rpcFamily: "evm",
37
- evmChainId: 3030,
38
- supportsSmartSessions: true,
39
- supportsAccountAbstraction: true,
40
- },
41
- BSC_TENDERLY: {
42
- chain: "BSC_TENDERLY",
43
- namespace: "evm",
44
- reference: "eip155:3131",
45
- label: "BNB Smart Chain Tenderly",
46
- rpcFamily: "evm",
47
- evmChainId: 3131,
48
- supportsSmartSessions: true,
49
- supportsAccountAbstraction: false,
50
- },
51
- SOL: {
52
- chain: "SOL",
53
- namespace: "solana",
54
- reference: "solana:mainnet-beta",
55
- label: "Solana",
56
- rpcFamily: "solana",
57
- supportsSmartSessions: true,
58
- supportsAccountAbstraction: false,
59
- },
60
- };
61
- export const getChainDescriptor = (chain) => chainDescriptors[chain];
62
- export const getAllChainDescriptors = () => Object.values(chainDescriptors);
63
- const normalizeEvmChainId = (value) => {
64
- if (typeof value === "number")
65
- return value;
66
- if (typeof value === "bigint")
67
- return Number(value);
68
- return Number(BigInt(value));
69
- };
70
- export const getSupportedChainFromEvmChainId = (value) => {
71
- const chainId = normalizeEvmChainId(value);
72
- const descriptor = getAllChainDescriptors().find((item) => item.namespace === "evm" && item.evmChainId === chainId);
73
- if (!descriptor) {
74
- throw new Error(`Unsupported EVM chainId: ${chainId}`);
75
- }
76
- return descriptor.chain;
77
- };
78
- export const createChainContext = (walletChain, settlementChain = walletChain) => ({
79
- walletChain,
80
- walletChainDescriptor: getChainDescriptor(walletChain),
81
- settlementChain,
82
- settlementChainDescriptor: getChainDescriptor(settlementChain),
83
- });
@@ -1,9 +0,0 @@
1
- export declare const EVM_TRANSACTION_SIGNING_UNSUPPORTED = "EVM_TRANSACTION_SIGNING_UNSUPPORTED";
2
- export declare class EvmTransactionSigningUnsupportedError extends Error {
3
- readonly providerName: string;
4
- readonly cause?: unknown | undefined;
5
- readonly code = "EVM_TRANSACTION_SIGNING_UNSUPPORTED";
6
- constructor(providerName: string, cause?: unknown | undefined);
7
- }
8
- export declare const isUnsupportedEvmTransactionSigningError: (error: unknown) => boolean;
9
- export declare const normalizeEvmTransactionSigningError: (providerName: string, error: unknown) => Error;
@@ -1,51 +0,0 @@
1
- export const EVM_TRANSACTION_SIGNING_UNSUPPORTED = "EVM_TRANSACTION_SIGNING_UNSUPPORTED";
2
- export class EvmTransactionSigningUnsupportedError extends Error {
3
- constructor(providerName, cause) {
4
- super(`${providerName} does not support EVM transaction signing`);
5
- this.providerName = providerName;
6
- this.cause = cause;
7
- this.code = EVM_TRANSACTION_SIGNING_UNSUPPORTED;
8
- this.name = "EvmTransactionSigningUnsupportedError";
9
- }
10
- }
11
- const getErrorCode = (error) => {
12
- if (typeof error !== "object" || error === null || !("code" in error))
13
- return undefined;
14
- const { code } = error;
15
- return typeof code === "number" || typeof code === "string" ? code : undefined;
16
- };
17
- const getErrorMessage = (error) => {
18
- if (error instanceof Error)
19
- return error.message;
20
- if (typeof error === "object" && error !== null && "message" in error) {
21
- const { message } = error;
22
- if (typeof message === "string")
23
- return message;
24
- }
25
- return String(error);
26
- };
27
- export const isUnsupportedEvmTransactionSigningError = (error) => {
28
- if (error instanceof EvmTransactionSigningUnsupportedError) {
29
- return true;
30
- }
31
- const code = getErrorCode(error);
32
- if (code === 4200 || code === -32601) {
33
- return true;
34
- }
35
- const message = getErrorMessage(error).toLowerCase();
36
- return (message.includes("eth_signtransaction") &&
37
- (message.includes("unsupported") ||
38
- message.includes("not support") ||
39
- message.includes("not found") ||
40
- message.includes("does not exist") ||
41
- message.includes("unrecognized")));
42
- };
43
- export const normalizeEvmTransactionSigningError = (providerName, error) => {
44
- if (error instanceof Error && !isUnsupportedEvmTransactionSigningError(error)) {
45
- return error;
46
- }
47
- if (isUnsupportedEvmTransactionSigningError(error)) {
48
- return new EvmTransactionSigningUnsupportedError(providerName, error);
49
- }
50
- return error instanceof Error ? error : new Error(String(error));
51
- };
@@ -1,26 +0,0 @@
1
- import EventEmitter from "eventemitter3";
2
- import type { BalanceInfo, WalletSession, WalletState } from "./types.js";
3
- export type WalletEvents = {
4
- "session:changed": [WalletSession | null];
5
- "session:rehydrated": [WalletSession];
6
- "session:expired": [WalletSession];
7
- "session:revoked": [WalletSession];
8
- "balance:changed": [BalanceInfo | null];
9
- "connecting:changed": [boolean];
10
- };
11
- export declare class SessionStore {
12
- private state;
13
- private emitter;
14
- constructor();
15
- getState(): WalletState;
16
- setConnecting(flag: boolean): void;
17
- setSession(session: WalletSession | null): void;
18
- rehydrateSession(session: WalletSession): void;
19
- clearSession(): void;
20
- expireSession(): void;
21
- revokeSession(): void;
22
- setBalance(balance: BalanceInfo | null): void;
23
- on<T extends keyof WalletEvents>(event: T, listener: (...args: WalletEvents[T]) => void): () => EventEmitter<WalletEvents, any>;
24
- private tryRehydrate;
25
- }
26
- export declare const sessionStore: SessionStore;
@@ -1,129 +0,0 @@
1
- import EventEmitter from "eventemitter3";
2
- const STORAGE_KEY = "ab:wallet:session";
3
- function getStorage() {
4
- try {
5
- return typeof localStorage !== "undefined" ? localStorage : null;
6
- }
7
- catch {
8
- return null;
9
- }
10
- }
11
- /** Only persist known JSON-serializable fields; session may contain DOM/React refs or circular refs. */
12
- function persistSession(session) {
13
- const storage = getStorage();
14
- if (!storage)
15
- return;
16
- if (!session) {
17
- storage.removeItem(STORAGE_KEY);
18
- return;
19
- }
20
- const serializable = {
21
- address: session.address,
22
- chain: session.chain,
23
- walletType: session.walletType,
24
- authSource: session.authSource,
25
- sessionId: session.sessionId,
26
- expiresAt: session.expiresAt,
27
- };
28
- if (session.capabilities?.length) {
29
- serializable.capabilities = session.capabilities;
30
- }
31
- try {
32
- storage.setItem(STORAGE_KEY, JSON.stringify(serializable));
33
- }
34
- catch {
35
- storage.removeItem(STORAGE_KEY);
36
- }
37
- }
38
- function readPersistedSession() {
39
- const storage = getStorage();
40
- if (!storage)
41
- return null;
42
- const raw = storage.getItem(STORAGE_KEY);
43
- if (!raw)
44
- return null;
45
- try {
46
- return JSON.parse(raw);
47
- }
48
- catch {
49
- storage.removeItem(STORAGE_KEY);
50
- return null;
51
- }
52
- }
53
- const staleProvider = {
54
- request() {
55
- return Promise.reject(new Error("Session restored from cache. Reconnect your wallet for transactions."));
56
- },
57
- disconnect() {
58
- return Promise.resolve();
59
- },
60
- };
61
- export class SessionStore {
62
- constructor() {
63
- this.state = {
64
- session: null,
65
- balance: null,
66
- isConnecting: false,
67
- };
68
- this.emitter = new EventEmitter();
69
- this.tryRehydrate();
70
- }
71
- getState() {
72
- return { ...this.state };
73
- }
74
- setConnecting(flag) {
75
- this.state.isConnecting = flag;
76
- this.emitter.emit("connecting:changed", flag);
77
- }
78
- setSession(session) {
79
- this.state.session = session;
80
- persistSession(session);
81
- this.emitter.emit("session:changed", session);
82
- }
83
- rehydrateSession(session) {
84
- this.state.session = session;
85
- persistSession(session);
86
- this.emitter.emit("session:changed", session);
87
- this.emitter.emit("session:rehydrated", session);
88
- }
89
- clearSession() {
90
- this.state.session = null;
91
- this.state.balance = null;
92
- persistSession(null);
93
- this.emitter.emit("session:changed", null);
94
- this.emitter.emit("balance:changed", null);
95
- }
96
- expireSession() {
97
- const currentSession = this.state.session;
98
- if (!currentSession)
99
- return;
100
- this.clearSession();
101
- this.emitter.emit("session:expired", currentSession);
102
- }
103
- revokeSession() {
104
- const currentSession = this.state.session;
105
- if (!currentSession)
106
- return;
107
- this.clearSession();
108
- this.emitter.emit("session:revoked", currentSession);
109
- }
110
- setBalance(balance) {
111
- this.state.balance = balance;
112
- this.emitter.emit("balance:changed", balance);
113
- }
114
- on(event, listener) {
115
- this.emitter.on(event, listener);
116
- return () => this.emitter.off(event, listener);
117
- }
118
- tryRehydrate() {
119
- const persisted = readPersistedSession();
120
- if (!persisted)
121
- return;
122
- if (persisted.expiresAt && persisted.expiresAt <= Date.now()) {
123
- persistSession(null);
124
- return;
125
- }
126
- this.state.session = { ...persisted, provider: staleProvider };
127
- }
128
- }
129
- export const sessionStore = new SessionStore();