Integration Guide
Introduction#
aelf-web-login: Modular React wallet collection and components for aelf applications.
website: https://aelf-web-login.vercel.app/
Install#
1yarn add @aelf-web-login/wallet-adapter-night-elf @aelf-web-login/wallet-adapter-portkey-aa @aelf-web-login/wallet-adapter-portkey-discover @aelf-web-login/wallet-adapter-react @aelf-web-login/wallet-adapter-base @aelf-web-login/wallet-adapter-bridge
Then the package.json will be like this
1"dependencies": {2"@aelf-web-login/wallet-adapter-night-elf": "^0.0.2-alpha.7",3"@aelf-web-login/wallet-adapter-portkey-aa": "^0.0.2-alpha.7",4"@aelf-web-login/wallet-adapter-portkey-discover": "^0.0.2-alpha.7",5"@aelf-web-login/wallet-adapter-react": "^0.0.2-alpha.7",6"@aelf-web-login/wallet-adapter-base": "^0.0.2-alpha.7",7"@aelf-web-login/wallet-adapter-bridge": "^0.0.2-alpha.7",8}
Config#
1import { PortkeyDiscoverWallet } from '@aelf-web-login/wallet-adapter-portkey-discover';2import { PortkeyAAWallet } from '@aelf-web-login/wallet-adapter-portkey-aa';3import { NightElfWallet } from '@aelf-web-login/wallet-adapter-night-elf';4import { IConfigProps } from '@aelf-web-login/wallet-adapter-bridge';5import { TChainId, SignInDesignEnum, NetworkEnum } from '@aelf-web-login/wallet-adapter-base';67const APP_NAME = 'explorer.aelf.io';8const WEBSITE_ICON = 'https://explorer.aelf.io/favicon.main.ico';9const CHAIN_ID = 'AELF' as TChainId;10const NETWORK_TYPE = NetworkEnum.TESTNET;11const RPC_SERVER_AELF = 'https://aelf-test-node.aelf.io';12const RPC_SERVER_TDVV = 'https://tdvv-public-node.aelf.io';13const RPC_SERVER_TDVW = 'https://tdvw-test-node.aelf.io';14const GRAPHQL_SERVER = 'https://dapp-aa-portkey-test.portkey.finance/Portkey_DID/PortKeyIndexerCASchema/graphql';15const CONNECT_SERVER = 'https://auth-aa-portkey-test.portkey.finance';1617const didConfig = {18graphQLUrl: GRAPHQL_SERVER,19connectUrl: CONNECT_SERVER,20requestDefaults: {21baseURL: 'https://aa-portkey-test.portkey.finance',22timeout: 30000,23},24socialLogin: {25Portkey: {26websiteName: APP_NAME,27websiteIcon: WEBSITE_ICON,28},29},30};3132const baseConfig = {33networkType: NETWORK_TYPE,34chainId: CHAIN_ID,35keyboard: true,36noCommonBaseModal: false,37design: SignInDesignEnum.CryptoDesign, // "SocialDesign" | "CryptoDesign" | "Web2Design"38titleForSocialDesign: 'Crypto wallet',39iconSrcForSocialDesign: 'url or base64',40};4142const wallets = [43new PortkeyAAWallet({44appName: APP_NAME,45chainId: CHAIN_ID,46autoShowUnlock: true,47}),48new PortkeyDiscoverWallet({49networkType: NETWORK_TYPE,50chainId: CHAIN_ID,51autoRequestAccount: true,52autoLogoutOnDisconnected: true,53autoLogoutOnNetworkMismatch: true,54autoLogoutOnAccountMismatch: true,55autoLogoutOnChainMismatch: true,56}),57new NightElfWallet({58chainId: CHAIN_ID,59appName: APP_NAME,60connectEagerly: true,61defaultRpcUrl: RPC_SERVER_AELF,62nodes: {63AELF: {64chainId: 'AELF',65rpcUrl: RPC_SERVER_AELF,66},67tDVW: {68chainId: 'tDVW',69rpcUrl: RPC_SERVER_TDVW,70},71tDVV: {72chainId: 'tDVV',73rpcUrl: RPC_SERVER_TDVV,74},75},76}),77]7879const config: IConfigProps = {80didConfig,81baseConfig,82wallets83};
Usage#
1import { WebLoginProvider, init, useConnectWallet } from '@aelf-web-login/wallet-adapter-react';23const App = () => {4const bridgeAPI = init(config); // upper config5return (6<WebLoginProvider bridgeAPI={bridgeAPI}>7<Demo />8</WebLoginProvider>9);10};11const Demo = () => {12const {13connectWallet,14disConnectWallet,15walletInfo,16lock,17isLocking,18isConnected,19loginError,20walletType,21getAccountByChainId,22getWalletSyncIsCompleted,23getSignature,24callSendMethod,25callViewMethod26} = useConnectWallet();27}
API#
connectWallet#
1connectWallet: () => Promise<TWalletInfo>
Connect wallet and return walletInfo
1import { Button } from 'aelf-design';23const Demo = () => {4const { connectWallet } = useConnectWallet();5const onConnectBtnClickHandler = async() => {6try {7const rs = await connectWallet();8} catch (e: any) {9console.log(e.message)10}11}12return (13<Button onClick={onConnectBtnClickHandler}>connect</Button>14)15}
disConnectWallet#
1disConnectWallet: () => Promise<void>
Disconnect wallet
1import { Button } from 'aelf-design';23const Demo = () => {4const { disConnectWallet } = useConnectWallet();5const onDisConnectBtnClickHandler = () => {6disConnectWallet()7}8return (9<Button onClick={onDisConnectBtnClickHandler}>disConnect</Button>10)11}
lock#
1lock: () => void
Lock wallet, only portkeyAA wallet take effect
1import { Button } from 'aelf-design';23const Demo = () => {4const { lock } = useConnectWallet();5return (6<Button onClick={lock}>lock</Button>7)8}
getAccountByChainId#
1getAccountByChainId: (chainId: TChainId) => Promise<string>
Get account address of designative chainId
1import { Button } from 'aelf-design';23const Demo = () => {4const { getAccountByChainId } = useConnectWallet();56const getAelfAccountHandler = async() => {7const address = await getAccountByChainId('AELF')8console.log(address)9}10const getTdvwAccountHandler = async() => {11const address = await getAccountByChainId('tDVW')12console.log(address)13}14return (15<Button onClick={getAelfAccountHandler}>account-AELF</Button>16<Button onClick={getTdvwAccountHandler}>account-tDVW</Button>17)18}
getWalletSyncIsCompleted#
1getWalletSyncIsCompleted: (chainId: TChainId) => Promise<string | boolean>
Return account address of designative chainId if sync is competed, otherwise return false
1import { Button } from 'aelf-design';23const Demo = () => {4const { getWalletSyncIsCompleted } = useConnectWallet();56const getAelfSyncIsCompletedHandler = async() => {7const address = await getWalletSyncIsCompleted('AELF')8console.log(address)9}10const getTdvwSyncIsCompletedHandler = async() => {11const address = await getWalletSyncIsCompleted('tDVW')12console.log(address)13}14return (15<Button onClick={getAelfSyncIsCompletedHandler}>sync-AELF</Button>16<Button onClick={getTdvwSyncIsCompletedHandler}>sync-tDVW</Button>17)18}
getSignature#
1const getSignature: (params: TSignatureParams) => Promise<{ `` error: number; `` errorMessage: string; `` signature: string; `` from: string; ``} | null>
Get signature message
1import { Button, Input } from 'aelf-design';23type TSignatureParams = {4appName: string;5address: string;6signInfo: string;7hexToBeSign?: string;8};910const Demo = () => {11const { getSignature } = useConnectWallet();12const [signInfo, setSignInfo] = useState('');13const [signedMessage, setSignedMessage] = useState('');1415const signHandler = async () => {16const sign = await getSignature({17signInfo,18appName: '',19address: '',20});21setSignedMessage(sign.signature);22};2324return (25div>26<div>27<Button onClick={signHandler}>28Sign29</Button>30<Input value={signInfo} onChange={(e) => setSignInfo(e.target.value)} />31<div>{signedMessage}</div>32</div>33</div>34)35}
callSendMethod#
1callSendMethod: <T, R>(props: ICallContractParams<T>) => Promise<R>
Call contract's send method
1import { Button } from 'aelf-design';23interface ICallContractParams<T> {4contractAddress: string;5methodName: string;6args: T;7chainId?: TChainId;8sendOptions?: SendOptions;9}1011const Demo = () => {12const { callSendMethod } = useConnectWallet();13const [result, setResult] = useState({});1415const onApproveHandler = async() => {16const res = await callSendMethod({17chainId: 'tDVW',18contractAddress: 'JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE',19methodName: 'Approve',20args: {21symbol: 'ELF',22spender: 'JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE',23amount: '100000000',24},25});26setResult(res);27}2829return (30<div>31<Button onClick={onApproveHandler}>Approve in tDVW</Button>32<div>33<h4>Result</h4>34<pre className="result">{JSON.stringify(result, null, ' ')}</pre>35</div>36</div>37)38}
callViewMethod#
1callViewMethod: <T, R>(props: ICallContractParams<T>) => Promise<R>
Call contract's view method
1import { Button } from 'aelf-design';23interface ICallContractParams<T> {4contractAddress: string;5methodName: string;6args: T;7chainId?: TChainId;8sendOptions?: SendOptions; // only send method use, ignore in view method9}1011const Demo = () => {12const { callViewMethod, getAccountByChainId } = useConnectWallet();13const [result, setResult] = useState({});1415const onGetBalanceHandler = async() => {16const res = await callViewMethod({17chainId: 'tDVW',18contractAddress: 'ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx',19methodName: 'GetBalance',20args: {21symbol: 'ELF',22owner: await getAccountByChainId('tDVW'),23},24});25setResult(res);26}2728return (29<div>30<Button onClick={onGetBalanceHandler}>GetBalance in tDVW</Button>31<div>32<h4>Result</h4>33<pre className="result">{JSON.stringify(result, null, ' ')}</pre>34</div>35</div>36)37}
walletInfo#
1const walletInfo: TWalletInfo
Wallet information after connecting wallet, can import TWalletInfo from @aelf-web-login/wallet-adapter-base
1type TWalletInfo =2| {3name?: string;4address: string;5extraInfo?: {6[key: string]: any;7};8}9| undefined;1011// walletInfo returned by nightElf12{13name,14address,15extraInfo: {16publicKey,17nightElfInfo: {18name,19appPermission,20defaultAElfBridge: bridge,21aelfBridges: bridges,22nodes,23},24},25}2627// walletInfo returned by portkeyAA28import { DIDWalletInfo } from '@portkey/did-ui-react';29{30name,31address,32extraInfo: {33publicKey,34portkeyInfo: {35...DIDWalletInfo36accounts: {37[chainId]: didWalletInfo.caInfo?.caAddress,38},39nickName,40},41},42}4344// walletInfo returned by portkeyDiscover45import type { Accounts, IPortkeyProvider } from '@portkey/provider-types';46{47address,48extraInfo: {49accounts: Accounts,50nickName,51provider: IPortkeyProvider,52},53}5455const Demo = () => {56const { walletInfo } = useConnectWallet();57console.log(walletInfo)58return null59}
walletType#
1const walletType: WalletTypeEnum
The currently connected wallet type, can import WalletTypeEnum from @aelf-web-login/wallet-adapter-base
1enum WalletTypeEnum {2unknown = 'Unknown',3elf = 'NightElf',4aa = 'PortkeyAA',5discover = 'PortkeyDiscover',6}78const Demo = () => {9const { walletType } = useConnectWallet();10console.log(walletType)11return null12}
isLocking#
1const isLocking: boolean
indicate whether the current state is locked, only portkeyAA wallet take effect, other wallets always return false
1import { Button } from 'aelf-design';23const Demo = () => {4const { isLocking } = useConnectWallet();56return (7<Button>8{isLocking ? 'unlock' : 'connect'}9</Button>10)11}
isConnected#
1const isConnected: boolean
indicate whether the current state is connected
1import { Button } from 'aelf-design';23const Demo = () => {4const { isConnected } = useConnectWallet();56return (7<div>8<Button disabled={isConnected}>connect</Button>9<Button disabled={!isConnected}>disConnect</Button>10</div>11)12}
loginError#
1const loginError: TWalletError | null
indicate are there any errors during the login/logout/unlock process
1type TWalletError = {2name: string;3code: number;4message: string;5nativeError?: any;6}78const Demo = () => {9const { loginError } = useConnectWallet();1011useEffect(() => {12if (!loginError) {13return;14}15console.log(loginError.message);16}, [loginError]);1718return null19}
Development#
1pnpm install
1cd packages/starter2pnpm dev
Publish#
1pnpm release
Edited on: 11 July 2024 04:33:40 GMT+0