@terrawallet/react 0.0.0-stage → 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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * wagmi connector: `connectors: [terraWallet()]`.
3
+ *
4
+ * import { createConfig, http } from "wagmi";
5
+ * import { base, mainnet } from "wagmi/chains";
6
+ * import { terraWallet } from "@terrawallet/react/wagmi";
7
+ *
8
+ * export const config = createConfig({
9
+ * chains: [base, mainnet],
10
+ * connectors: [terraWallet({ walletConnectProjectId: "..." })],
11
+ * transports: { [base.id]: http(), [mainnet.id]: http() },
12
+ * });
13
+ *
14
+ * Works with wagmi 2.17+ and 3.x. It has no runtime dependency on wagmi (the connector contract is a plain
15
+ * function), so it adds nothing to apps that do not use it.
16
+ */
17
+ import type { CreateConnectorFn } from "@wagmi/core";
18
+ import { type EIP1193Provider, type TerraConnect, type TerraConnectOptions } from "@terrawallet/connect";
19
+ import type { ThemeOptions } from "@terrawallet/elements";
20
+ export interface TerraWalletParameters extends TerraConnectOptions {
21
+ /** Share a client with <TerraProvider client={...}>. */
22
+ client?: TerraConnect;
23
+ /** Show the Terra Wallet modal on connect (extension, QR code, app deep link). Default true. */
24
+ modal?: boolean;
25
+ theme?: ThemeOptions;
26
+ }
27
+ export declare function terraWallet(parameters?: TerraWalletParameters): CreateConnectorFn<EIP1193Provider, {
28
+ getTerraClient(): TerraConnect;
29
+ }>;
package/dist/wagmi.js ADDED
@@ -0,0 +1,113 @@
1
+ // src/wagmi.ts
2
+ import {
3
+ TERRA_RDNS,
4
+ assertPublishableKey,
5
+ createTerraConnect,
6
+ getChain,
7
+ toProviderError
8
+ } from "@terrawallet/connect";
9
+ function toTerraChain(c) {
10
+ return getChain(c.id) ?? {
11
+ id: c.id,
12
+ name: c.name,
13
+ symbol: c.nativeCurrency.symbol,
14
+ decimals: c.nativeCurrency.decimals,
15
+ rpcUrls: c.rpcUrls.default.http,
16
+ ...c.blockExplorers ? { explorerUrl: c.blockExplorers.default.url } : {},
17
+ logo: "",
18
+ ...c.testnet ? { testnet: true } : {}
19
+ };
20
+ }
21
+ function terraWallet(parameters = {}) {
22
+ const { client: given, modal = true, theme, ...options } = parameters;
23
+ if (options.publishableKey !== void 0) assertPublishableKey(options.publishableKey);
24
+ return (config) => {
25
+ let client;
26
+ let off = [];
27
+ const terra = () => client ?? (client = given ?? createTerraConnect({ deepLink: "manual", autoReconnect: false, ...options, chains: options.chains ?? config.chains.map((c) => toTerraChain(c)) }));
28
+ function onAccountsChanged(accounts) {
29
+ if (!accounts.length) onDisconnect();
30
+ else config.emitter.emit("change", { accounts });
31
+ }
32
+ function onChainChanged(chainId) {
33
+ config.emitter.emit("change", { chainId: Number(chainId) });
34
+ }
35
+ function onDisconnect() {
36
+ stopListening();
37
+ config.emitter.emit("disconnect");
38
+ }
39
+ function listen(c) {
40
+ if (off.length) return;
41
+ off = [c.on("accountsChanged", onAccountsChanged), c.on("chainChanged", onChainChanged), c.on("disconnect", onDisconnect)];
42
+ }
43
+ function stopListening() {
44
+ for (const f of off) f();
45
+ off = [];
46
+ }
47
+ return {
48
+ id: "terraWallet",
49
+ name: "Terra Wallet",
50
+ type: "terraWallet",
51
+ rdns: TERRA_RDNS,
52
+ icon: "https://terrawallet.us/icon.png",
53
+ async connect({ chainId, isReconnecting, withCapabilities } = {}) {
54
+ const c = terra();
55
+ try {
56
+ if (isReconnecting) {
57
+ if (!await c.reconnect()) throw new Error("Terra Wallet is not connected.");
58
+ } else if (modal && typeof window !== "undefined") {
59
+ const { connectWithModal } = await import("@terrawallet/elements");
60
+ await connectWithModal(c, { theme });
61
+ } else {
62
+ await c.connect();
63
+ }
64
+ listen(c);
65
+ let current = c.getState().chainId ?? config.chains[0].id;
66
+ if (chainId && chainId !== current) current = await c.switchChain(chainId).catch(() => current);
67
+ const accounts = c.getState().accounts;
68
+ return {
69
+ accounts: withCapabilities ? accounts.map((address) => ({ address, capabilities: {} })) : accounts,
70
+ chainId: current
71
+ };
72
+ } catch (e) {
73
+ throw toProviderError(e);
74
+ }
75
+ },
76
+ async disconnect() {
77
+ stopListening();
78
+ await terra().disconnect();
79
+ },
80
+ async getAccounts() {
81
+ return terra().getState().accounts;
82
+ },
83
+ async getChainId() {
84
+ return terra().getState().chainId ?? config.chains[0].id;
85
+ },
86
+ async getProvider() {
87
+ return terra().getProvider();
88
+ },
89
+ async isAuthorized() {
90
+ const c = terra();
91
+ return c.getState().status === "connected" || await c.reconnect();
92
+ },
93
+ async switchChain({ chainId }) {
94
+ const chain = config.chains.find((x) => x.id === chainId);
95
+ if (!chain) throw toProviderError(Object.assign(new Error("This chain is not in your wagmi config."), { code: 4902 }));
96
+ try {
97
+ await terra().switchChain(chainId);
98
+ } catch (e) {
99
+ throw toProviderError(e);
100
+ }
101
+ onChainChanged(chainId);
102
+ return chain;
103
+ },
104
+ onAccountsChanged,
105
+ onChainChanged,
106
+ onDisconnect,
107
+ getTerraClient: () => terra()
108
+ };
109
+ };
110
+ }
111
+ export {
112
+ terraWallet
113
+ };
package/package.json CHANGED
@@ -1,6 +1,87 @@
1
1
  {
2
2
  "name": "@terrawallet/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "React bindings for Terra Wallet: <TerraProvider>, <ConnectButton/>, <WalletModal/>, hooks, and a wagmi connector.",
5
+ "keywords": [
6
+ "terra-wallet",
7
+ "terra-pay",
8
+ "wallet",
9
+ "web3",
10
+ "crypto-payments"
11
+ ],
12
+ "homepage": "https://developers.terrawallet.us/docs",
13
+ "bugs": {
14
+ "url": "https://github.com/theterrawallet/terra-sdk/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/theterrawallet/terra-sdk.git",
19
+ "directory": "packages/react"
20
+ },
21
+ "license": "Apache-2.0",
22
+ "author": "Terra Wallet (https://terrawallet.us)",
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ },
32
+ "./wagmi": {
33
+ "types": "./dist/wagmi.d.ts",
34
+ "import": "./dist/wagmi.js"
35
+ },
36
+ "./package.json": "./package.json"
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "README.md",
41
+ "LICENSE",
42
+ "NOTICE"
43
+ ],
44
+ "dependencies": {
45
+ "@terrawallet/connect": "0.1.0",
46
+ "@terrawallet/elements": "0.1.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@wagmi/core": ">=2.17.0 <4",
50
+ "react": ">=18.2.0",
51
+ "viem": "2.x"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "@wagmi/core": {
55
+ "optional": true
56
+ },
57
+ "viem": {
58
+ "optional": true
59
+ }
60
+ },
61
+ "devDependencies": {
62
+ "@types/node": "22.19.19",
63
+ "@types/react": "19.2.16",
64
+ "@types/react-dom": "19.2.3",
65
+ "@wagmi/core": "3.6.9",
66
+ "esbuild": "0.27.7",
67
+ "happy-dom": "21.0.4",
68
+ "react": "19.0.0",
69
+ "react-dom": "19.0.0",
70
+ "tsx": "4.22.4",
71
+ "typescript": "5.9.3",
72
+ "viem": "2.57.16"
73
+ },
74
+ "engines": {
75
+ "node": ">=18"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public",
79
+ "provenance": true
80
+ },
81
+ "scripts": {
82
+ "build": "node build.mjs",
83
+ "typecheck": "tsc --noEmit",
84
+ "test": "node --import tsx --test test/*.test.ts",
85
+ "sbom": "node ../../scripts/sbom.mjs ."
86
+ }
6
87
  }