@provablehq/aleo-wallet-adaptor-react 0.1.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/LICENSE +674 -0
- package/README.md +36 -0
- package/dist/index.d.mts +121 -0
- package/dist/index.d.ts +121 -0
- package/dist/index.js +406 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +385 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @provablehq/aleo-wallet-adaptor-react
|
|
2
|
+
|
|
3
|
+
React context, hooks, and utilities for consuming Aleo wallet adapters in browser applications.
|
|
4
|
+
|
|
5
|
+
## When to use it
|
|
6
|
+
|
|
7
|
+
- Wrap your React tree with a wallet provider that manages connection state, auto-connect, and error handling.
|
|
8
|
+
- Access wallet methods (`connect`, `executeTransaction`, `decrypt`, etc.) through a simple `useWallet` hook.
|
|
9
|
+
- Pair with the UI kit (`@provablehq/aleo-wallet-adaptor-react-ui`) or build your own custom interface.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @provablehq/aleo-wallet-adaptor-react
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { AleoWalletProvider } from '@provablehq/aleo-wallet-adaptor-react';
|
|
21
|
+
import { GalileoWalletAdapter } from '@provablehq/aleo-wallet-adaptor-prove-alpha';
|
|
22
|
+
|
|
23
|
+
const wallets = [new GalileoWalletAdapter()];
|
|
24
|
+
|
|
25
|
+
export function App({ children }: { children: React.ReactNode }) {
|
|
26
|
+
return <AleoWalletProvider wallets={wallets}>{children}</AleoWalletProvider>;
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Related packages
|
|
31
|
+
|
|
32
|
+
- `@provablehq/aleo-wallet-adaptor-core` – underlying adapter interfaces consumed by the provider.
|
|
33
|
+
- `@provablehq/aleo-wallet-adaptor-react-ui` – drop-in modals and buttons that work with this context.
|
|
34
|
+
- Wallet adapters such as `@provablehq/aleo-wallet-adaptor-prove-alpha`, `-puzzle`, `-leo`, etc.
|
|
35
|
+
|
|
36
|
+
Live demo: https://aleo-dev-toolkit-react-app.vercel.app/
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { FC, ReactNode } from 'react';
|
|
3
|
+
import { WalletAdapter, WalletReadyState, WalletName, AleoDeployment } from '@provablehq/aleo-wallet-standard';
|
|
4
|
+
import { Network, TransactionOptions, TransactionStatusResponse } from '@provablehq/aleo-types';
|
|
5
|
+
import { WalletError, DecryptPermission } from '@provablehq/aleo-wallet-adaptor-core';
|
|
6
|
+
|
|
7
|
+
interface Wallet {
|
|
8
|
+
adapter: WalletAdapter;
|
|
9
|
+
readyState: WalletReadyState;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Wallet context state
|
|
13
|
+
*/
|
|
14
|
+
interface WalletContextState {
|
|
15
|
+
/**
|
|
16
|
+
* All available wallet adapters
|
|
17
|
+
*/
|
|
18
|
+
wallets: Wallet[];
|
|
19
|
+
/**
|
|
20
|
+
* The connected wallet adapter
|
|
21
|
+
*/
|
|
22
|
+
wallet: Wallet | null;
|
|
23
|
+
/**
|
|
24
|
+
* The connected account
|
|
25
|
+
*/
|
|
26
|
+
address: string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the wallet is connected
|
|
29
|
+
*/
|
|
30
|
+
connected: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the wallet is connecting
|
|
33
|
+
*/
|
|
34
|
+
connecting: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the wallet is disconnecting
|
|
37
|
+
*/
|
|
38
|
+
disconnecting: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the wallet is auto-connecting
|
|
41
|
+
*/
|
|
42
|
+
autoConnect: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The current network
|
|
45
|
+
*/
|
|
46
|
+
network: Network | null;
|
|
47
|
+
/**
|
|
48
|
+
* Select a wallet by name
|
|
49
|
+
* @param name The name of the wallet to select
|
|
50
|
+
*/
|
|
51
|
+
selectWallet: (name: WalletName) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Connect to the selected wallet
|
|
54
|
+
*/
|
|
55
|
+
connect: (network: Network) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Disconnect from the connected wallet
|
|
58
|
+
*/
|
|
59
|
+
disconnect: () => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute a transaction
|
|
62
|
+
*/
|
|
63
|
+
executeTransaction: (options: TransactionOptions) => Promise<{
|
|
64
|
+
transactionId: string;
|
|
65
|
+
} | undefined>;
|
|
66
|
+
/**
|
|
67
|
+
* Get transaction status
|
|
68
|
+
*/
|
|
69
|
+
transactionStatus: (transactionId: string) => Promise<TransactionStatusResponse>;
|
|
70
|
+
/**
|
|
71
|
+
* Sign a message
|
|
72
|
+
*/
|
|
73
|
+
signMessage: (message: Uint8Array | string) => Promise<Uint8Array | undefined>;
|
|
74
|
+
/**
|
|
75
|
+
* Switch the network
|
|
76
|
+
*/
|
|
77
|
+
switchNetwork: (network: Network) => Promise<boolean>;
|
|
78
|
+
/**
|
|
79
|
+
* Decrypt a ciphertext
|
|
80
|
+
*/
|
|
81
|
+
decrypt: (cipherText: string) => Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* Request records
|
|
84
|
+
*/
|
|
85
|
+
requestRecords: (program: string, includePlaintext?: boolean) => Promise<unknown[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Execute a deployment
|
|
88
|
+
*/
|
|
89
|
+
executeDeployment: (deployment: AleoDeployment) => Promise<{
|
|
90
|
+
transactionId: string;
|
|
91
|
+
}>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Wallet context
|
|
95
|
+
*/
|
|
96
|
+
declare const WalletContext: react.Context<WalletContextState | undefined>;
|
|
97
|
+
/**
|
|
98
|
+
* Custom hook to use the wallet context
|
|
99
|
+
* @returns The wallet context state
|
|
100
|
+
*/
|
|
101
|
+
declare function useWalletContext(): WalletContextState;
|
|
102
|
+
|
|
103
|
+
interface WalletProviderProps {
|
|
104
|
+
children: ReactNode;
|
|
105
|
+
wallets: WalletAdapter[];
|
|
106
|
+
network?: Network;
|
|
107
|
+
autoConnect?: boolean;
|
|
108
|
+
onError?: (error: WalletError) => void;
|
|
109
|
+
localStorageKey?: string;
|
|
110
|
+
decryptPermission?: DecryptPermission;
|
|
111
|
+
programs?: string[];
|
|
112
|
+
}
|
|
113
|
+
declare const AleoWalletProvider: FC<WalletProviderProps>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Custom hook to use the wallet
|
|
117
|
+
* @returns The wallet context state
|
|
118
|
+
*/
|
|
119
|
+
declare function useWallet(): WalletContextState;
|
|
120
|
+
|
|
121
|
+
export { AleoWalletProvider, type Wallet, WalletContext, type WalletContextState, useWallet, useWalletContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { FC, ReactNode } from 'react';
|
|
3
|
+
import { WalletAdapter, WalletReadyState, WalletName, AleoDeployment } from '@provablehq/aleo-wallet-standard';
|
|
4
|
+
import { Network, TransactionOptions, TransactionStatusResponse } from '@provablehq/aleo-types';
|
|
5
|
+
import { WalletError, DecryptPermission } from '@provablehq/aleo-wallet-adaptor-core';
|
|
6
|
+
|
|
7
|
+
interface Wallet {
|
|
8
|
+
adapter: WalletAdapter;
|
|
9
|
+
readyState: WalletReadyState;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Wallet context state
|
|
13
|
+
*/
|
|
14
|
+
interface WalletContextState {
|
|
15
|
+
/**
|
|
16
|
+
* All available wallet adapters
|
|
17
|
+
*/
|
|
18
|
+
wallets: Wallet[];
|
|
19
|
+
/**
|
|
20
|
+
* The connected wallet adapter
|
|
21
|
+
*/
|
|
22
|
+
wallet: Wallet | null;
|
|
23
|
+
/**
|
|
24
|
+
* The connected account
|
|
25
|
+
*/
|
|
26
|
+
address: string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Whether the wallet is connected
|
|
29
|
+
*/
|
|
30
|
+
connected: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whether the wallet is connecting
|
|
33
|
+
*/
|
|
34
|
+
connecting: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the wallet is disconnecting
|
|
37
|
+
*/
|
|
38
|
+
disconnecting: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the wallet is auto-connecting
|
|
41
|
+
*/
|
|
42
|
+
autoConnect: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The current network
|
|
45
|
+
*/
|
|
46
|
+
network: Network | null;
|
|
47
|
+
/**
|
|
48
|
+
* Select a wallet by name
|
|
49
|
+
* @param name The name of the wallet to select
|
|
50
|
+
*/
|
|
51
|
+
selectWallet: (name: WalletName) => void;
|
|
52
|
+
/**
|
|
53
|
+
* Connect to the selected wallet
|
|
54
|
+
*/
|
|
55
|
+
connect: (network: Network) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Disconnect from the connected wallet
|
|
58
|
+
*/
|
|
59
|
+
disconnect: () => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute a transaction
|
|
62
|
+
*/
|
|
63
|
+
executeTransaction: (options: TransactionOptions) => Promise<{
|
|
64
|
+
transactionId: string;
|
|
65
|
+
} | undefined>;
|
|
66
|
+
/**
|
|
67
|
+
* Get transaction status
|
|
68
|
+
*/
|
|
69
|
+
transactionStatus: (transactionId: string) => Promise<TransactionStatusResponse>;
|
|
70
|
+
/**
|
|
71
|
+
* Sign a message
|
|
72
|
+
*/
|
|
73
|
+
signMessage: (message: Uint8Array | string) => Promise<Uint8Array | undefined>;
|
|
74
|
+
/**
|
|
75
|
+
* Switch the network
|
|
76
|
+
*/
|
|
77
|
+
switchNetwork: (network: Network) => Promise<boolean>;
|
|
78
|
+
/**
|
|
79
|
+
* Decrypt a ciphertext
|
|
80
|
+
*/
|
|
81
|
+
decrypt: (cipherText: string) => Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* Request records
|
|
84
|
+
*/
|
|
85
|
+
requestRecords: (program: string, includePlaintext?: boolean) => Promise<unknown[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Execute a deployment
|
|
88
|
+
*/
|
|
89
|
+
executeDeployment: (deployment: AleoDeployment) => Promise<{
|
|
90
|
+
transactionId: string;
|
|
91
|
+
}>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Wallet context
|
|
95
|
+
*/
|
|
96
|
+
declare const WalletContext: react.Context<WalletContextState | undefined>;
|
|
97
|
+
/**
|
|
98
|
+
* Custom hook to use the wallet context
|
|
99
|
+
* @returns The wallet context state
|
|
100
|
+
*/
|
|
101
|
+
declare function useWalletContext(): WalletContextState;
|
|
102
|
+
|
|
103
|
+
interface WalletProviderProps {
|
|
104
|
+
children: ReactNode;
|
|
105
|
+
wallets: WalletAdapter[];
|
|
106
|
+
network?: Network;
|
|
107
|
+
autoConnect?: boolean;
|
|
108
|
+
onError?: (error: WalletError) => void;
|
|
109
|
+
localStorageKey?: string;
|
|
110
|
+
decryptPermission?: DecryptPermission;
|
|
111
|
+
programs?: string[];
|
|
112
|
+
}
|
|
113
|
+
declare const AleoWalletProvider: FC<WalletProviderProps>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Custom hook to use the wallet
|
|
117
|
+
* @returns The wallet context state
|
|
118
|
+
*/
|
|
119
|
+
declare function useWallet(): WalletContextState;
|
|
120
|
+
|
|
121
|
+
export { AleoWalletProvider, type Wallet, WalletContext, type WalletContextState, useWallet, useWalletContext };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AleoWalletProvider: () => AleoWalletProvider,
|
|
24
|
+
WalletContext: () => WalletContext,
|
|
25
|
+
useWallet: () => useWallet,
|
|
26
|
+
useWalletContext: () => useWalletContext
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/context.ts
|
|
31
|
+
var import_react = require("react");
|
|
32
|
+
var WalletContext = (0, import_react.createContext)(void 0);
|
|
33
|
+
function useWalletContext() {
|
|
34
|
+
const ctx = (0, import_react.useContext)(WalletContext);
|
|
35
|
+
if (!ctx) {
|
|
36
|
+
throw new Error("`useWalletContext` must be used inside `AleoWalletProvider`");
|
|
37
|
+
}
|
|
38
|
+
return ctx;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/WalletProvider.tsx
|
|
42
|
+
var import_react3 = require("react");
|
|
43
|
+
var import_aleo_wallet_standard = require("@provablehq/aleo-wallet-standard");
|
|
44
|
+
var import_aleo_types = require("@provablehq/aleo-types");
|
|
45
|
+
|
|
46
|
+
// src/useLocalStorage.ts
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
function useLocalStorage(key, defaultState) {
|
|
49
|
+
const state = (0, import_react2.useState)(() => {
|
|
50
|
+
try {
|
|
51
|
+
const value2 = localStorage.getItem(key);
|
|
52
|
+
if (value2) return JSON.parse(value2);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (typeof window !== "undefined") {
|
|
55
|
+
console.error(error);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return defaultState;
|
|
59
|
+
});
|
|
60
|
+
const value = state[0];
|
|
61
|
+
const isFirstRender = (0, import_react2.useRef)(true);
|
|
62
|
+
(0, import_react2.useEffect)(() => {
|
|
63
|
+
if (isFirstRender.current) {
|
|
64
|
+
isFirstRender.current = false;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
if (value === null) {
|
|
69
|
+
localStorage.removeItem(key);
|
|
70
|
+
} else {
|
|
71
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
72
|
+
}
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (typeof window !== "undefined") {
|
|
75
|
+
console.error(error);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}, [value, key]);
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/WalletProvider.tsx
|
|
83
|
+
var import_aleo_wallet_adaptor_core = require("@provablehq/aleo-wallet-adaptor-core");
|
|
84
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
85
|
+
var initialState = {
|
|
86
|
+
wallet: null,
|
|
87
|
+
adapter: null,
|
|
88
|
+
publicKey: null,
|
|
89
|
+
connected: false,
|
|
90
|
+
network: null
|
|
91
|
+
};
|
|
92
|
+
var AleoWalletProvider = ({
|
|
93
|
+
children,
|
|
94
|
+
wallets: adapters,
|
|
95
|
+
autoConnect = false,
|
|
96
|
+
network: initialNetwork = import_aleo_types.Network.TESTNET3,
|
|
97
|
+
onError,
|
|
98
|
+
localStorageKey = "walletName",
|
|
99
|
+
decryptPermission = import_aleo_wallet_adaptor_core.DecryptPermission.NoDecrypt,
|
|
100
|
+
programs
|
|
101
|
+
}) => {
|
|
102
|
+
const [name, setName] = useLocalStorage(localStorageKey, null);
|
|
103
|
+
const [{ wallet, adapter, publicKey, connected, network }, setState] = (0, import_react3.useState)(initialState);
|
|
104
|
+
const readyState = adapter?.readyState || import_aleo_wallet_standard.WalletReadyState.UNSUPPORTED;
|
|
105
|
+
const [connecting, setConnecting] = (0, import_react3.useState)(false);
|
|
106
|
+
const [disconnecting, setDisconnecting] = (0, import_react3.useState)(false);
|
|
107
|
+
const isConnecting = (0, import_react3.useRef)(false);
|
|
108
|
+
const isDisconnecting = (0, import_react3.useRef)(false);
|
|
109
|
+
const isUnloading = (0, import_react3.useRef)(false);
|
|
110
|
+
const [wallets, setWallets] = (0, import_react3.useState)(
|
|
111
|
+
() => adapters.map((adapter2) => ({
|
|
112
|
+
adapter: adapter2,
|
|
113
|
+
readyState: adapter2.readyState
|
|
114
|
+
}))
|
|
115
|
+
);
|
|
116
|
+
(0, import_react3.useEffect)(() => {
|
|
117
|
+
setWallets(
|
|
118
|
+
(wallets2) => adapters.map((adapter2, index) => {
|
|
119
|
+
const wallet2 = wallets2[index];
|
|
120
|
+
return wallet2 && wallet2.adapter === adapter2 && wallet2.readyState === adapter2.readyState ? wallet2 : {
|
|
121
|
+
adapter: adapter2,
|
|
122
|
+
readyState: adapter2.readyState
|
|
123
|
+
};
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
function handleReadyStateChange(readyState2) {
|
|
127
|
+
setWallets((prevWallets) => {
|
|
128
|
+
const index = prevWallets.findIndex(({ adapter: adapter3 }) => adapter3 === this);
|
|
129
|
+
if (index === -1) return prevWallets;
|
|
130
|
+
const { adapter: adapter2 } = prevWallets[index];
|
|
131
|
+
return [
|
|
132
|
+
...prevWallets.slice(0, index),
|
|
133
|
+
{ adapter: adapter2, readyState: readyState2 },
|
|
134
|
+
...prevWallets.slice(index + 1)
|
|
135
|
+
];
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function handleNetworkChange(network2) {
|
|
139
|
+
setState((state) => ({
|
|
140
|
+
...state,
|
|
141
|
+
network: network2
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
adapters.forEach((adapter2) => adapter2.on("readyStateChange", handleReadyStateChange, adapter2));
|
|
145
|
+
adapters.forEach((adapter2) => adapter2.on("networkChange", handleNetworkChange, adapter2));
|
|
146
|
+
return () => {
|
|
147
|
+
adapters.forEach((adapter2) => adapter2.off("readyStateChange", handleReadyStateChange, adapter2));
|
|
148
|
+
adapters.forEach((adapter2) => adapter2.off("networkChange", handleNetworkChange, adapter2));
|
|
149
|
+
};
|
|
150
|
+
}, [adapters]);
|
|
151
|
+
(0, import_react3.useEffect)(() => {
|
|
152
|
+
const wallet2 = name && wallets.find(({ adapter: adapter2 }) => adapter2.name === name);
|
|
153
|
+
if (wallet2) {
|
|
154
|
+
setState({
|
|
155
|
+
wallet: wallet2,
|
|
156
|
+
adapter: wallet2.adapter,
|
|
157
|
+
connected: wallet2.adapter.connected,
|
|
158
|
+
publicKey: wallet2.adapter.account?.address ?? null,
|
|
159
|
+
network: wallet2.adapter.network ?? null
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
setState(initialState);
|
|
163
|
+
}
|
|
164
|
+
}, [name, wallets]);
|
|
165
|
+
(0, import_react3.useEffect)(() => {
|
|
166
|
+
function listener() {
|
|
167
|
+
isUnloading.current = true;
|
|
168
|
+
}
|
|
169
|
+
window.addEventListener("beforeunload", listener);
|
|
170
|
+
return () => window.removeEventListener("beforeunload", listener);
|
|
171
|
+
}, [isUnloading]);
|
|
172
|
+
const handleConnect = (0, import_react3.useCallback)(() => {
|
|
173
|
+
if (!adapter) return;
|
|
174
|
+
setState((state) => ({
|
|
175
|
+
...state,
|
|
176
|
+
connected: adapter.connected,
|
|
177
|
+
publicKey: adapter.account?.address ?? null,
|
|
178
|
+
network: adapter.network ?? null
|
|
179
|
+
}));
|
|
180
|
+
}, [adapter]);
|
|
181
|
+
const handleDisconnect = (0, import_react3.useCallback)(() => {
|
|
182
|
+
if (!isUnloading.current) setName(null);
|
|
183
|
+
}, [isUnloading, setName]);
|
|
184
|
+
const handleError = (0, import_react3.useCallback)(
|
|
185
|
+
(error) => {
|
|
186
|
+
if (!isUnloading.current) (onError || console.error)(error);
|
|
187
|
+
return error;
|
|
188
|
+
},
|
|
189
|
+
[isUnloading, onError]
|
|
190
|
+
);
|
|
191
|
+
(0, import_react3.useEffect)(() => {
|
|
192
|
+
if (adapter) {
|
|
193
|
+
adapter.on("connect", handleConnect);
|
|
194
|
+
adapter.on("disconnect", handleDisconnect);
|
|
195
|
+
adapter.on("error", handleError);
|
|
196
|
+
return () => {
|
|
197
|
+
adapter.off("connect", handleConnect);
|
|
198
|
+
adapter.off("disconnect", handleDisconnect);
|
|
199
|
+
adapter.off("error", handleError);
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}, [adapter, handleConnect, handleDisconnect, handleError]);
|
|
203
|
+
(0, import_react3.useEffect)(() => {
|
|
204
|
+
return () => {
|
|
205
|
+
if (adapter && adapter.connected) {
|
|
206
|
+
adapter.disconnect();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}, [adapter]);
|
|
210
|
+
(0, import_react3.useEffect)(() => {
|
|
211
|
+
if (isConnecting.current || connected || !autoConnect || !adapter || !(readyState === import_aleo_wallet_standard.WalletReadyState.INSTALLED || readyState === import_aleo_wallet_standard.WalletReadyState.LOADABLE))
|
|
212
|
+
return;
|
|
213
|
+
(async function() {
|
|
214
|
+
isConnecting.current = true;
|
|
215
|
+
setConnecting(true);
|
|
216
|
+
try {
|
|
217
|
+
await adapter.connect(initialNetwork, decryptPermission, programs);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
setName(null);
|
|
220
|
+
} finally {
|
|
221
|
+
setConnecting(false);
|
|
222
|
+
isConnecting.current = false;
|
|
223
|
+
}
|
|
224
|
+
})();
|
|
225
|
+
}, [isConnecting, connected, autoConnect, adapter, readyState, setName]);
|
|
226
|
+
(0, import_react3.useEffect)(() => {
|
|
227
|
+
if (adapter && connected && adapter.network !== initialNetwork) {
|
|
228
|
+
try {
|
|
229
|
+
switchNetwork(initialNetwork);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error("Failed to switch network, disconnecting");
|
|
232
|
+
disconnect();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}, [initialNetwork]);
|
|
236
|
+
(0, import_react3.useEffect)(() => {
|
|
237
|
+
if (adapter && connected) {
|
|
238
|
+
disconnect();
|
|
239
|
+
}
|
|
240
|
+
}, [decryptPermission, programs]);
|
|
241
|
+
const connect = (0, import_react3.useCallback)(async () => {
|
|
242
|
+
if (isConnecting.current || isDisconnecting.current || connected) return;
|
|
243
|
+
if (!adapter) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotSelectedError());
|
|
244
|
+
if (!(readyState === import_aleo_wallet_standard.WalletReadyState.INSTALLED || readyState === import_aleo_wallet_standard.WalletReadyState.LOADABLE)) {
|
|
245
|
+
setName(null);
|
|
246
|
+
if (typeof window !== "undefined") {
|
|
247
|
+
window.open(adapter.url, "_blank");
|
|
248
|
+
}
|
|
249
|
+
throw handleError(new import_aleo_wallet_adaptor_core.WalletNotReadyError());
|
|
250
|
+
}
|
|
251
|
+
isConnecting.current = true;
|
|
252
|
+
setConnecting(true);
|
|
253
|
+
try {
|
|
254
|
+
await adapter.connect(initialNetwork, decryptPermission, programs);
|
|
255
|
+
} catch (error) {
|
|
256
|
+
setName(null);
|
|
257
|
+
throw error;
|
|
258
|
+
} finally {
|
|
259
|
+
setConnecting(false);
|
|
260
|
+
isConnecting.current = false;
|
|
261
|
+
}
|
|
262
|
+
}, [isConnecting, isDisconnecting, connected, adapter, readyState, handleError, setName]);
|
|
263
|
+
const disconnect = (0, import_react3.useCallback)(async () => {
|
|
264
|
+
if (isDisconnecting.current) return;
|
|
265
|
+
if (!adapter) return setName(null);
|
|
266
|
+
isDisconnecting.current = true;
|
|
267
|
+
setDisconnecting(true);
|
|
268
|
+
try {
|
|
269
|
+
await adapter.disconnect();
|
|
270
|
+
} catch (error) {
|
|
271
|
+
setName(null);
|
|
272
|
+
throw error;
|
|
273
|
+
} finally {
|
|
274
|
+
setDisconnecting(false);
|
|
275
|
+
isDisconnecting.current = false;
|
|
276
|
+
}
|
|
277
|
+
}, [isDisconnecting, adapter, setName]);
|
|
278
|
+
const executeTransaction = (0, import_react3.useCallback)(
|
|
279
|
+
async (transaction) => {
|
|
280
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
281
|
+
if (!adapter || !("executeTransaction" in adapter))
|
|
282
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("executeTransaction"));
|
|
283
|
+
await checkNetwork();
|
|
284
|
+
return await adapter.executeTransaction(transaction);
|
|
285
|
+
},
|
|
286
|
+
[adapter, handleError, connected]
|
|
287
|
+
);
|
|
288
|
+
const transactionStatus = (0, import_react3.useCallback)(
|
|
289
|
+
async (transactionId) => {
|
|
290
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
291
|
+
if (!adapter || !("transactionStatus" in adapter))
|
|
292
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("transactionStatus"));
|
|
293
|
+
return await adapter.transactionStatus(transactionId);
|
|
294
|
+
},
|
|
295
|
+
[adapter, handleError, connected]
|
|
296
|
+
);
|
|
297
|
+
const signMessage = (0, import_react3.useMemo)(
|
|
298
|
+
() => async (message) => {
|
|
299
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
300
|
+
if (!adapter || !("signMessage" in adapter))
|
|
301
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("signMessage"));
|
|
302
|
+
return await adapter.signMessage(
|
|
303
|
+
typeof message === "string" ? new TextEncoder().encode(message) : message
|
|
304
|
+
);
|
|
305
|
+
},
|
|
306
|
+
[adapter, handleError, connected]
|
|
307
|
+
);
|
|
308
|
+
const switchNetwork = (0, import_react3.useCallback)(
|
|
309
|
+
async (network2) => {
|
|
310
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
311
|
+
if (!adapter || !("switchNetwork" in adapter))
|
|
312
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("switchNetwork"));
|
|
313
|
+
let switched = false;
|
|
314
|
+
try {
|
|
315
|
+
isConnecting.current = true;
|
|
316
|
+
setConnecting(true);
|
|
317
|
+
await adapter.switchNetwork(network2);
|
|
318
|
+
switched = true;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (error instanceof import_aleo_wallet_adaptor_core.MethodNotImplementedError) {
|
|
321
|
+
await disconnect();
|
|
322
|
+
}
|
|
323
|
+
console.error("Failed to switch network");
|
|
324
|
+
} finally {
|
|
325
|
+
isConnecting.current = false;
|
|
326
|
+
setConnecting(false);
|
|
327
|
+
}
|
|
328
|
+
return switched;
|
|
329
|
+
},
|
|
330
|
+
[adapter, handleError, connected]
|
|
331
|
+
);
|
|
332
|
+
const decrypt = (0, import_react3.useCallback)(
|
|
333
|
+
async (cipherText) => {
|
|
334
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
335
|
+
if (!adapter || !("decrypt" in adapter))
|
|
336
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("decrypt"));
|
|
337
|
+
return await adapter.decrypt(cipherText);
|
|
338
|
+
},
|
|
339
|
+
[adapter, handleError, connected]
|
|
340
|
+
);
|
|
341
|
+
const requestRecords = (0, import_react3.useCallback)(
|
|
342
|
+
async (program, includePlaintext) => {
|
|
343
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
344
|
+
if (!adapter || !("requestRecords" in adapter))
|
|
345
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("requestRecords"));
|
|
346
|
+
return await adapter.requestRecords(program, includePlaintext);
|
|
347
|
+
},
|
|
348
|
+
[adapter, handleError, connected]
|
|
349
|
+
);
|
|
350
|
+
const executeDeployment = (0, import_react3.useCallback)(
|
|
351
|
+
async (deployment) => {
|
|
352
|
+
if (!connected) throw handleError(new import_aleo_wallet_adaptor_core.WalletNotConnectedError());
|
|
353
|
+
if (!adapter || !("executeDeployment" in adapter))
|
|
354
|
+
throw handleError(new import_aleo_wallet_adaptor_core.MethodNotImplementedError("executeDeployment"));
|
|
355
|
+
return await adapter.executeDeployment(deployment);
|
|
356
|
+
},
|
|
357
|
+
[adapter, handleError, connected]
|
|
358
|
+
);
|
|
359
|
+
const checkNetwork = (0, import_react3.useCallback)(async () => {
|
|
360
|
+
if (adapter && adapter.network !== initialNetwork) {
|
|
361
|
+
const switchResult = await switchNetwork(initialNetwork);
|
|
362
|
+
if (!switchResult) {
|
|
363
|
+
throw handleError(new import_aleo_wallet_adaptor_core.WalletSwitchNetworkError("Failed to switch network"));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}, [adapter, initialNetwork, switchNetwork]);
|
|
367
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
368
|
+
WalletContext.Provider,
|
|
369
|
+
{
|
|
370
|
+
value: {
|
|
371
|
+
autoConnect,
|
|
372
|
+
wallets,
|
|
373
|
+
wallet,
|
|
374
|
+
address: publicKey ?? null,
|
|
375
|
+
connected,
|
|
376
|
+
connecting,
|
|
377
|
+
disconnecting,
|
|
378
|
+
network,
|
|
379
|
+
selectWallet: setName,
|
|
380
|
+
connect,
|
|
381
|
+
disconnect,
|
|
382
|
+
executeTransaction,
|
|
383
|
+
transactionStatus,
|
|
384
|
+
signMessage,
|
|
385
|
+
switchNetwork,
|
|
386
|
+
decrypt,
|
|
387
|
+
requestRecords,
|
|
388
|
+
executeDeployment
|
|
389
|
+
},
|
|
390
|
+
children
|
|
391
|
+
}
|
|
392
|
+
);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// src/useWallet.ts
|
|
396
|
+
function useWallet() {
|
|
397
|
+
return useWalletContext();
|
|
398
|
+
}
|
|
399
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
400
|
+
0 && (module.exports = {
|
|
401
|
+
AleoWalletProvider,
|
|
402
|
+
WalletContext,
|
|
403
|
+
useWallet,
|
|
404
|
+
useWalletContext
|
|
405
|
+
});
|
|
406
|
+
//# sourceMappingURL=index.js.map
|