Skip to content

Commit 7a45fec

Browse files
abstract wallet support
1 parent 46d0b4b commit 7a45fec

File tree

42 files changed

+3427
-613
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3427
-613
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Abstract Global Wallet Adapter
2+
3+
This package is an optional add-on to the thirdweb connect SDK. It allows you to use a abstract global wallet in your app with the same experience as the thirdweb SDK.
4+
5+
## Instructions
6+
7+
### 1. Install the packages
8+
9+
TODO
10+
11+
### 2. Usage
12+
13+
TODO
14+
15+
### Resources
16+
17+
- [Full working demo](https://github.com/thirdweb-dev/expo-starter)
18+
- [React docs](https://portal.thirdweb.com/typescript/v5/react)
19+
- [TypeScript docs](https://portal.thirdweb.com/typescript/v5)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "@thirdweb-dev/abstract-wallet-adapter",
3+
"version": "0.0.1",
4+
"repository": {
5+
"type": "git",
6+
"url": "git+https://github.com/thirdweb-dev/js.git#main"
7+
},
8+
"license": "Apache-2.0",
9+
"bugs": {
10+
"url": "https://github.com/thirdweb-dev/js/issues"
11+
},
12+
"author": "thirdweb eng <[email protected]>",
13+
"type": "module",
14+
"main": "./dist/cjs/exports/thirdweb.js",
15+
"module": "./dist/esm/exports/thirdweb.js",
16+
"types": "./dist/types/exports/thirdweb.d.ts",
17+
"typings": "./dist/types/exports/thirdweb.d.ts",
18+
"exports": {
19+
".": {
20+
"types": "./dist/types/exports/thirdweb.d.ts",
21+
"import": "./dist/esm/exports/thirdweb.js",
22+
"default": "./dist/cjs/exports/thirdweb.js"
23+
},
24+
"./package.json": "./package.json"
25+
},
26+
"files": ["dist/*", "src/*"],
27+
"dependencies": {},
28+
"devDependencies": {
29+
"@abstract-foundation/agw-client": "0.0.1-beta.13",
30+
"@abstract-foundation/agw-react": "0.0.1-beta.12",
31+
"@privy-io/cross-app-connect": "0.0.7",
32+
"thirdweb": "workspace:*",
33+
"rimraf": "6.0.1",
34+
"viem": "2.21.40"
35+
},
36+
"peerDependencies": {
37+
"@abstract-foundation/agw-client": "^0.0.1-beta.13",
38+
"@abstract-foundation/agw-react": "^0.0.1-beta.12",
39+
"@privy-io/cross-app-connect": "^0.0.7",
40+
"thirdweb": ">=5",
41+
"typescript": ">=5.0.4"
42+
},
43+
"peerDependenciesMeta": {
44+
"typescript": {
45+
"optional": true
46+
}
47+
},
48+
"scripts": {
49+
"format": "biome format ./src --write",
50+
"lint": "biome check ./src",
51+
"fix": "biome check ./src --fix",
52+
"build": "pnpm clean && pnpm build:cjs && pnpm build:esm && pnpm build:types",
53+
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/cjs --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
54+
"build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./dist/esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/esm/package.json",
55+
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
56+
"clean": "rimraf dist"
57+
},
58+
"engines": {
59+
"node": ">=18"
60+
}
61+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { abstractWallet } from "../index.js";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { abstractWalletConnector } from "@abstract-foundation/agw-react/connectors";
2+
3+
export function abstractWallet() {
4+
const connector = abstractWalletConnector()({
5+
chains: [mainnet],
6+
emitter: new EventEmitter(),
7+
storage: null,
8+
transports: {},
9+
});
10+
const provider = await connector.getProvider();
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
// This tsconfig file contains the shared config for the build (tsconfig.build.json) and type checking (tsconfig.json) config.
3+
"include": [],
4+
"compilerOptions": {
5+
// Incremental builds
6+
// NOTE: Enabling incremental builds speeds up `tsc`. Keep in mind though that it does not reliably bust the cache when the `tsconfig.json` file changes.
7+
"incremental": false,
8+
9+
// Type checking
10+
"strict": true,
11+
"useDefineForClassFields": true, // Not enabled by default in `strict` mode unless we bump `target` to ES2022.
12+
"noFallthroughCasesInSwitch": true, // Not enabled by default in `strict` mode.
13+
"noImplicitReturns": true, // Not enabled by default in `strict` mode.
14+
"useUnknownInCatchVariables": true, // TODO: This would normally be enabled in `strict` mode but would require some adjustments to the codebase.
15+
"noImplicitOverride": true, // Not enabled by default in `strict` mode.
16+
"noUnusedLocals": true, // Not enabled by default in `strict` mode.
17+
"noUnusedParameters": true, // Not enabled by default in `strict` mode.
18+
"exactOptionalPropertyTypes": false, // Not enabled by default in `strict` mode.
19+
"noUncheckedIndexedAccess": true, // Not enabled by default in `strict` mode.
20+
21+
// JavaScript support
22+
"allowJs": false,
23+
"checkJs": false,
24+
25+
// Interop constraints
26+
"esModuleInterop": false,
27+
"allowSyntheticDefaultImports": true,
28+
"forceConsistentCasingInFileNames": true,
29+
"verbatimModuleSyntax": true,
30+
"importHelpers": true, // This is only used for build validation. Since we do not have `tslib` installed, this will fail if we accidentally make use of anything that'd require injection of helpers.
31+
32+
// Language and environment
33+
"moduleResolution": "NodeNext",
34+
"module": "NodeNext",
35+
"target": "ES2021", // Setting this to `ES2021` enables native support for `Node v16+`: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
36+
"lib": [
37+
"ES2022", // By using ES2022 we get access to the `.cause` property on `Error` instances.
38+
"DOM" // We are adding `DOM` here to get the `fetch`, etc. types. This should be removed once these types are available via DefinitelyTyped.
39+
],
40+
41+
// Skip type checking for node modules
42+
"skipLibCheck": true,
43+
44+
// jsx for "/react" portion
45+
"jsx": "react-jsx"
46+
}
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"include": ["src"],
4+
"exclude": [
5+
"src/**/*.test.ts",
6+
"src/**/*.test.tsx",
7+
"src/**/*.test-d.ts",
8+
"src/**/*.bench.ts",
9+
"src/**/*.macro.ts"
10+
],
11+
"compilerOptions": {
12+
"moduleResolution": "node",
13+
"sourceMap": true,
14+
"rootDir": "./src"
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// This configuration is used for local development and type checking.
3+
"extends": "./tsconfig.base.json",
4+
"include": ["src", "test"],
5+
"exclude": [],
6+
// "references": [{ "path": "./scripts/tsconfig.json" }],
7+
"compilerOptions": {
8+
"baseUrl": ".",
9+
"paths": {
10+
"~test/*": ["./test/src/*"]
11+
}
12+
}
13+
}
65.3 KB
Loading

packages/thirdweb/scripts/wallets/extra-wallets.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,33 @@
9191
"native": null,
9292
"universal": null
9393
}
94+
},
95+
{
96+
"id": "abstract",
97+
"name": "Abstract Global Wallet",
98+
"homepage": "https://abs.xyz/",
99+
"image_id": "abstract.png",
100+
"app": {
101+
"browser": null,
102+
"ios": null,
103+
"android": null,
104+
"mac": null,
105+
"windows": null,
106+
"linux": null,
107+
"chrome": null,
108+
"firefox": null,
109+
"safari": null,
110+
"edge": null,
111+
"opera": null
112+
},
113+
"rdns": "xyz.abs",
114+
"mobile": {
115+
"native": null,
116+
"universal": null
117+
},
118+
"desktop": {
119+
"native": null,
120+
"universal": null
121+
}
94122
}
95123
]
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import type { EIP1193Provider } from "viem";
2+
import { trackConnect } from "../analytics/track/connect.js";
3+
import type { Chain } from "../chains/types.js";
4+
import { getCachedChainIfExists } from "../chains/utils.js";
5+
import type { ThirdwebClient } from "../client/client.js";
6+
import {} from "../utils/address.js";
7+
import {} from "../utils/encoding/hex.js";
8+
import {
9+
autoConnectEip1193Wallet,
10+
connectEip1193Wallet,
11+
} from "../wallets/injected/index.js";
12+
import type { Account, Wallet } from "../wallets/interfaces/wallet.js";
13+
import { createWalletEmitter } from "../wallets/wallet-emitter.js";
14+
import type { WalletId } from "../wallets/wallet-types.js";
15+
16+
export type Eip1193AdapterOptions = {
17+
client: ThirdwebClient;
18+
provider: EIP1193Provider | (() => Promise<EIP1193Provider>);
19+
walletId?: WalletId;
20+
chain?: Chain;
21+
};
22+
23+
/**
24+
* Converts an EIP1193 provider to a Thirdweb wallet.
25+
* @param options - The options for converting an EIP1193 provider to a Thirdweb wallet.
26+
* @returns A Thirdweb wallet.
27+
* @example
28+
* ```ts
29+
* import { fromEip1193Provider } from "thirdweb/wallets";
30+
* const wallet = fromEip1193Provider({ provider, client, walletId });
31+
* ```
32+
* @walletUtils
33+
*/
34+
export function fromEip1193Provider(options: Eip1193AdapterOptions): Wallet {
35+
const id: WalletId = options.walletId ?? "adapter";
36+
const emitter = createWalletEmitter();
37+
let account: Account | undefined = undefined;
38+
let chain: Chain | undefined = undefined;
39+
let provider: EIP1193Provider | undefined = undefined;
40+
const getProvider = async () => {
41+
if (!provider) {
42+
provider =
43+
typeof options.provider === "function"
44+
? await options.provider()
45+
: options.provider;
46+
}
47+
return provider;
48+
};
49+
50+
const unsubscribeChain = emitter.subscribe("chainChanged", (newChain) => {
51+
chain = newChain;
52+
});
53+
54+
function reset() {
55+
account = undefined;
56+
chain = undefined;
57+
}
58+
59+
let handleDisconnect = async () => {};
60+
61+
const unsubscribeDisconnect = emitter.subscribe("disconnect", () => {
62+
reset();
63+
unsubscribeChain();
64+
unsubscribeDisconnect();
65+
});
66+
67+
emitter.subscribe("accountChanged", (_account) => {
68+
account = _account;
69+
});
70+
71+
let handleSwitchChain: (chain: Chain) => Promise<void> = async () => {
72+
throw new Error("Not implemented");
73+
};
74+
75+
return {
76+
id: options.walletId as WalletId,
77+
subscribe: emitter.subscribe,
78+
getConfig: () => undefined,
79+
getChain() {
80+
if (!chain) {
81+
return undefined;
82+
}
83+
84+
chain = getCachedChainIfExists(chain.id) || chain;
85+
return chain;
86+
},
87+
getAccount: () => account,
88+
connect: async () => {
89+
const [connectedAccount, connectedChain, doDisconnect, doSwitchChain] =
90+
await connectEip1193Wallet({
91+
id,
92+
provider: await getProvider(),
93+
client: options.client,
94+
chain: options.chain,
95+
emitter,
96+
});
97+
// set the states
98+
account = connectedAccount;
99+
chain = connectedChain;
100+
handleDisconnect = doDisconnect;
101+
handleSwitchChain = doSwitchChain;
102+
trackConnect({
103+
client: options.client,
104+
walletType: id,
105+
walletAddress: account.address,
106+
});
107+
// return account
108+
return account;
109+
},
110+
autoConnect: async () => {
111+
const [connectedAccount, connectedChain, doDisconnect, doSwitchChain] =
112+
await autoConnectEip1193Wallet({
113+
id,
114+
provider: await getProvider(),
115+
emitter,
116+
chain: options.chain,
117+
client: options.client,
118+
});
119+
// set the states
120+
account = connectedAccount;
121+
chain = connectedChain;
122+
handleDisconnect = doDisconnect;
123+
handleSwitchChain = doSwitchChain;
124+
trackConnect({
125+
client: options.client,
126+
walletType: id,
127+
walletAddress: account.address,
128+
});
129+
// return account
130+
return account;
131+
},
132+
disconnect: async () => {
133+
reset();
134+
await handleDisconnect();
135+
},
136+
switchChain: (c) => handleSwitchChain(c),
137+
};
138+
}

0 commit comments

Comments
 (0)