Build in progress, to use it you will need the archethic libjs typescript version package installed in your project.
This package was build to simplify the connection between your react DApp and the archethic wallet.
It is build as a react context provider, so you can easily use it in your react app and access the wallet from anywhere in your app.
To build the package, run the following command:
npm install
tsc
In your main app file, wrap your app with the WalletProvider component and pass the origin object as a prop. The origin object is required and must contain the name of your app.
// App.js
import WalletProvider from "archethic-provider";
// required origin object
const origin = {
name: "Your app name",
url: "https://yourapp.com"
}
const App = () => {
return (
<WalletProvider origin={origin} initilizeOnPageLoad={true} >
<YourApp />
</WalletProvider>
);
};
In a component, you can access the wallet object by using the useWallet hook.
// YourComponent.js
import { useWallet } from "archethic-provider";
const YourComponent = () => {
const { isConnected, walletEndpoint, currentAccount, connect, archethic } = useWallet();
return (
<div>
<p>Is connected: {isConnected}</p>
<p>Wallet endpoint: {walletEndpoint.origin}</p>
<p>Current account: {currentAccount.name}</p>
<p>Current account: {currentAccount.genesisAddress}</p>
<button onClick={!isConnected && connect}>Connect</button>
</div>
)
}
You can also use all the archethic libjs SDK by using the archethic object.