@ab-org/sdk-core 0.0.1 → 0.1.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.
@@ -64,10 +64,10 @@ export interface InjectedWalletConfig {
64
64
  resolveProvider: () => InjectedEthereumProvider | undefined;
65
65
  }
66
66
  /**
67
- * Force wallet to BSC_TENDERLY (chainId 3131). If the chain is missing (4902), add it then switch.
68
- * Call after external wallet connect so subsequent business runs on Tenderly BSC.
67
+ * Force wallet to the default funding chain (e.g. 3131). If the chain is missing (4902), add it then switch.
68
+ * Call after external wallet connect so subsequent business runs on the configured funding network.
69
69
  */
70
- export declare function switchToFallbackBscTestnet(provider: InjectedEthereumProvider): Promise<void>;
70
+ export declare function switchToFallbackFundingChain(provider: InjectedEthereumProvider): Promise<void>;
71
71
  export declare class InjectedEvmProvider extends AbstractProvider {
72
72
  private config;
73
73
  private injected?;
@@ -1,3 +1,4 @@
1
+ import { getChainInfo } from "@ab-org/wallet-utils";
1
2
  import { AbstractProvider } from "../base.js";
2
3
  import { createChainContext, getSupportedChainFromEvmChainId } from "../../core/chains.js";
3
4
  import { normalizeEvmTransactionSigningError } from "../../core/errors.js";
@@ -111,27 +112,32 @@ function rdnsIncludes(info, value) {
111
112
  function nameIncludes(info, value) {
112
113
  return info.name.toLowerCase().includes(value.toLowerCase());
113
114
  }
114
- /** BSC Tenderly testnet (testBsc / chainId 3131) fallback when wallet is on an unsupported EVM chain */
115
- const FALLBACK_BSC_TEST_CHAIN_ID_HEX = "0xc3b";
116
- const FALLBACK_BSC_TEST_CHAIN_PARAMS = {
117
- chainId: FALLBACK_BSC_TEST_CHAIN_ID_HEX,
118
- chainName: "BSC Tenderly",
119
- nativeCurrency: { name: "BNB", symbol: "BNB", decimals: 18 },
120
- rpcUrls: [
121
- "https://virtual.binance.eu.rpc.tenderly.co/e643ea28-32eb-4fb9-8116-90be24f7defa",
122
- ],
123
- blockExplorerUrls: [
124
- "https://dashboard.tenderly.co/explorer/vnet/6593bc72-f548-497d-bff9-5be061436a48",
125
- ],
126
- };
115
+ /** Default funding chain (see `@ab-org/wallet-utils` `getChainInfo()`, currently Tenderly BSC 3131). */
116
+ const FALLBACK_FUNDING_CHAIN = getChainInfo();
117
+ function evmChainInfoToWalletAddEthereumChainParams(info) {
118
+ const chainIdHex = `0x${BigInt(info.chainId).toString(16)}`;
119
+ return {
120
+ chainId: chainIdHex,
121
+ chainName: info.chainName,
122
+ nativeCurrency: {
123
+ name: info.nativeCurrencyName,
124
+ symbol: info.nativeCurrencySymbol,
125
+ decimals: info.nativeCurrencyDecimals,
126
+ },
127
+ rpcUrls: [...info.rpcUrls],
128
+ blockExplorerUrls: [info.blockExplorerUrl],
129
+ };
130
+ }
131
+ const FALLBACK_FUNDING_CHAIN_ID_HEX = `0x${BigInt(FALLBACK_FUNDING_CHAIN.chainId).toString(16)}`;
132
+ const FALLBACK_FUNDING_CHAIN_PARAMS = evmChainInfoToWalletAddEthereumChainParams(FALLBACK_FUNDING_CHAIN);
127
133
  /**
128
- * Force wallet to BSC_TENDERLY (chainId 3131). If the chain is missing (4902), add it then switch.
129
- * Call after external wallet connect so subsequent business runs on Tenderly BSC.
134
+ * Force wallet to the default funding chain (e.g. 3131). If the chain is missing (4902), add it then switch.
135
+ * Call after external wallet connect so subsequent business runs on the configured funding network.
130
136
  */
131
- export async function switchToFallbackBscTestnet(provider) {
137
+ export async function switchToFallbackFundingChain(provider) {
132
138
  const switchChain = () => provider.request({
133
139
  method: "wallet_switchEthereumChain",
134
- params: [{ chainId: FALLBACK_BSC_TEST_CHAIN_ID_HEX }],
140
+ params: [{ chainId: FALLBACK_FUNDING_CHAIN_ID_HEX }],
135
141
  });
136
142
  try {
137
143
  await switchChain();
@@ -141,7 +147,7 @@ export async function switchToFallbackBscTestnet(provider) {
141
147
  if (code === 4902) {
142
148
  await provider.request({
143
149
  method: "wallet_addEthereumChain",
144
- params: [FALLBACK_BSC_TEST_CHAIN_PARAMS],
150
+ params: [FALLBACK_FUNDING_CHAIN_PARAMS],
145
151
  });
146
152
  await switchChain();
147
153
  return;
@@ -192,7 +198,7 @@ export class InjectedEvmProvider extends AbstractProvider {
192
198
  if (!address) {
193
199
  throw new Error(`${this.title} did not return an account`);
194
200
  }
195
- await switchToFallbackBscTestnet(provider);
201
+ await switchToFallbackFundingChain(provider);
196
202
  const chainId = await walletProvider.request({ method: "eth_chainId" });
197
203
  const chain = getSupportedChainFromEvmChainId(chainId);
198
204
  const session = {
@@ -211,13 +217,33 @@ export class InjectedEvmProvider extends AbstractProvider {
211
217
  const provider = this.injected ?? this.config.resolveProvider();
212
218
  if (!provider)
213
219
  return null;
220
+ const walletProvider = createInjectedWalletProvider(this.title, provider);
214
221
  const accounts = await provider.request({
215
222
  method: "eth_accounts",
216
223
  params: [],
217
224
  });
218
- if (!accounts?.length)
225
+ const [address] = accounts ?? [];
226
+ if (!address)
219
227
  return null;
220
- return this.connect();
228
+ let chain;
229
+ try {
230
+ const chainId = await walletProvider.request({ method: "eth_chainId" });
231
+ chain = getSupportedChainFromEvmChainId(chainId);
232
+ }
233
+ catch {
234
+ return null;
235
+ }
236
+ const session = {
237
+ address,
238
+ chain,
239
+ provider: walletProvider,
240
+ walletType: "injected",
241
+ authSource: "wallet",
242
+ sessionId: `${this.id}:${address.toLowerCase()}`,
243
+ capabilities: injectedWalletCapabilities,
244
+ chainContext: createChainContext(chain),
245
+ };
246
+ return this.setSession(session);
221
247
  }
222
248
  }
223
249
  export class MetaMaskProvider extends InjectedEvmProvider {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ab-org/sdk-core",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**/*",
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@cubist-labs/cubesigner-sdk": "^0.4.219",
17
- "eventemitter3": "^5.0.1"
17
+ "eventemitter3": "^5.0.1",
18
+ "@ab-org/wallet-utils": "0.0.7"
18
19
  },
19
20
  "devDependencies": {
20
21
  "typescript": "^5.5.4"