An Arweave.js dapp wallet aggregator for react. (similar to useWallet but for Arweave.)
- Implement persistent sessions with
"sesssionUtils/sessionStorage.ts"
Add it to your project:
yarn add arjs-react
example project hosted on the permaweb:
https://arweave.net:443/PklkS62k62MKioyq0Sn5WbbD1jToMyefhgyaj3XqpS4
Use it in your React app:
//App.js
import React, { useState, useEffect, useMemo } from 'react'
import { ArjsProvider, useArjs } from 'arjs-react'
function _App() {
const wallet = useArjs();
const permission = { permissions: ["SIGN_TRANSACTION"] }
const [key, setKey] = useState('')
const activate = (connector, key) => wallet.connect(connector, key)
const getKey = (e) =>{ setKey(e.target.value)}
const [balance, setBalance] = useState("Requesting...");
const [address, setAddress] = useState("Requesting...");
wallet.ready(() => {
if(wallet.status == "connected")(async () => {
console.log(wallet)
setBalance(wallet.getArweave().ar.winstonToAr( await wallet.getBalance("self")))
setAddress(await wallet.getAddress())
})()
})
return(
<>
<h1>Wallet</h1>
{wallet.status == "connected" ? (
<div>
<div>Account: {address}</div>
<div>Balance: {balance}</div>
<button onClick={() => wallet.disconnect()}>disconnect</button>
</div>
) : (
<div>
Connect:
<button onClick={() => activate('arweave', key)}>Arweave (with Key)</button>
<input type="text" value={key} placeholder={'key here'} onChange={getKey}/>
<button onClick={() => activate('arconnect', permission)}>ArConnect</button>
</div>
)}
</>
)}
//wrap the root component with <ArjsProvider />
function App(){
return (
<ArjsProvider
//Add wallets here
connectors={{
arconnect: true,
arweave: true
}}
//enable/disable smartweave contract interaction here
enableSWC={false}>
<_App />
</ArjsProvider>
)}
export default App
This is the provider component. It should be placed above any component using useArjs()
. Apart from children
, it can accept four other props:
Enables smartweave transactions in wallet.arweave.smartweave
.
Defaults to false
.
Configuration for the different connectors. accepts a key:
dapp wallet name with a truthy value
, may accept wallet configurations when new wallets are added.
arweave
:{}
arconnect
:{}
Gateway accepts an abject with the arweave gateway parameters identical to the input for Arweave.init({})
, if not implemented the code will default to arweave.net
. This can be useful for use with a testnet (testnet interoperability untested)
The default poll rate used in all wallet.poll()
when a rate is not explicitly set in the wallet.poll()
function. is set 2000
(ms) by default.
This is the hook to be used throughout the app. It returns an object representing the connected account (“wallet”), containing:
connect(connectorId, arg)
: Call this function with a connector ID to “connect” to a provider (see above for the connectors provided by default) and anarg
which can either be thearconnect
permissions to request or the walletkey
to initialize "Arweave.js".ready(callback)
: Runs a function once a wallet is selected andstate
="connected"
. callback nested in an if statementstatus = "connected"
wrapped in auseEffect
with[arweave, status]
as dependents.poll(callback, rate)
: Runs a loop function with delayrate
once a wallet is selected andstate
="connected"
. if statement, ifstatus = "connected"
wrapped in a withuseEffect
with[arweave, status]
as dependents. ifrate
is not setpoll()
will use thePollRate
set in<ArjsProvider />
or it's default value2000
(ms). can be interchanged withready()
if the callback is required to be run in interval (e.g., a wallet polling the most updated balance).connector
: The "key" of the wallet you're connected to (e.g., "arweave", "arconnect").connectors
: The full list of connectors.disconnect()
: Call this function to “disconnect” from the current provider. This will this will not disconnectarconnect
to disconnect fromarconnect
usearweave.disconnect()
in thewallet
object.status
: Contains the current status of the wallet connection. The possible values are:- "disconnected": no wallet connected (default state).
- "connecting": trying to connect to the wallet.
- "connected": connected to the wallet (i.e. the account is available).
- "failed": a connection error happened.
- All the children of
arweave
shown below exceptdisconnect
are available directly in thewallet
object arweave
:isloading
: Integer that increases whensmartweave.write
smartweave.read
smartweave.iread
smartweave.sign
smartweave.post
are ran and decreases by on as each function completes execution. (may remove this for the non sw functions in a later update.)loadStatus("add" | "sub")
:loadStatus("add")
incrementsisloading
by one,loadStatus("sub")
decrementsisloading
by one.transaction(data):
returnsarweave.createTransaction(data)
post(transaction):
returnsarweave.transactions.post(transaction)
addTag(transaction, name, value):
returnstransaction.addTag(name, value)
sign(transaction):
returnsarweave.transactions.getUploader(transaction)
smartweave:
returns:write(input, id)
executesinteractWrite(arweave, wallet, id, input)
read(id)
executesreadContract(arweave, id)
(Can be executed without initializing a wallet.)iread(id)
executesinteractRead(arweave, wallet, id, input)
- click here for Smartweave SDK readme.
getArweave
: returns "theArweave.js object
provided by the connected wallet."disconnect
: returnswindow.arweaveWallet.disconnect()
only available when connected with ArConnect.getBalance
: returns"current wallet balance in winston as string"
getAddress
: returns"current wallet address as string"
Added smartweave interactRead
support for ArConnect.
To run the examples, switch to the respective directories. Then, simply run yarn dev
.
arjs-react is a greatly inspired by useWallet() and it's file structure.