Skip to main content

Auth SDK Usage Guide

Installation​

Using NPM/Yarn​

npm install --save @arcana/auth
yarn add @arcana/auth

Using CDN​

<script src="https://cdn.jsdelivr.net/npm/@arcana/auth"></script>
<script src="https://unpkg.com/@arcana/auth"></script>

Usage​

Import​

const { AuthProvider } = window.arcana.auth
// or
import { AuthProvider } from '@arcana/auth'

Initialize​

import { AppMode } from '@arcana/auth'
const auth = new AuthProvider(`${appId}`)

const position = 'left' // values - 'left' or 'right'

await auth.init({ appMode: AppMode.Widget, position })

provider = auth.provider
// or
provider = window.arcana.provider
// or
provider = window.ethereum

Auth API’s​

Login/logout​

Social login

await auth.loginWithSocial(`${verifier}`)

Email link login

await auth.loginWithLink(`${email}`)

Check is logged in

const loggedIn = await auth.isLoggedIn()

User Info

const info = await auth.getUser()
/*
interface UserInfo {
id: string
email?: string
name?: string
picture?: string
address: string
publicKey: string
}
*/

Logout

await auth.logout()

Get public key associated with an email​

await auth.getPublicKey(`${email}`)

Utils​

ECIES encryption​

import { encryptWithPublicKey } from '@arcana/auth'

encryptWithPublicKey({
publicKey: '',
message: 'test-message',
}).then((ciphertext) => {
// Do something with ciphertext
})

Compute Address​

import { computeAddress } from '@arcana/auth'

const address = computeAddress(publicKey: string);

Events​

Subscribing

provider.on('chainChanged', handler: (chainId: number) => void);
provider.on('accountsChanged', handler: (accounts: string[]) => void);
provider.on('connect', handler: ({ chainId: number }) => void);
provider.isConnected(): Promise<boolean>;

Unsubscribing

provider.removeListener(`${eventName}`, handler)

Using with web3/ethers​

Ethers JS​

Installation

npm install --save ethers

Usage

import { ethers } from 'ethers'

const provider = new ethers.providers.Web3Provider(auth.provider)

const signer = provider.getSigner()

const signedMessage = await signer.signMessage('sample_message')

Web3 JS​

Installation

npm install --save web3

Usage

import Web3 from 'web3'

const provider = new Web3(auth.provider)

const signer = provider.getSigner()

const signedMessage = await signer.signMessage('sample_message')

RPC API’s​

eth_accounts​

provider.request({ method: 'eth_accounts' }).then((accounts) => {
// Set default account to accounts[0]
from = accounts[0]
})

eth_sign​

provider
.request({
method: 'eth_sign',
params: [from, 'some_random_data'],
})
.then((signature) => {
// Use signature
})

personal_sign​

provider
.request({
method: 'personal_sign',
params: ['some personal signing data', from],
})
.then((personalSignature) => {
// Use personal signature
})

eth_getEncryptionPublicKey​

provider
.request({
method: 'eth_getEncryptionPublicKey',
params: [from],
})
.then((publicKey) => {
// Use public key
})

eth_decrypt​

provider
.request({
method: 'eth_decrypt',
params: [ciphertext, from],
})
.then((plaintext) => {
// Use plaintext
})

eth_signTypedData_v4​

provider
.request({
method: 'eth_signTypedData_v4',
params: [from, msgParams],
})
.then((signature) => {
// Use signature
})