@velas/account-client
v0.3.9
Published
browser sdk
Downloads
20
Keywords
Readme
@velas/account-client
Client Side JavaScript toolkit for Velas Account API.
Install
npm install @velas/account-client --save
Prerequisites
To run this package you need to have the following:
- Velas Tool Suite (latest stable) – How to install
- Relying party package – How to install
Once you have all the dependencies set up, you have to check check what cluster the Velas command-line tool (CLI) is currently targeting by running the following command:
velas config get
For example to target the Testnet cluster, run:
velas config set --url https://api.testnet.velas.com
velas-keygen new
You should also check the balance of your default CLI key to be able to make transactions:
velas balance
To get tokens on testnet, you just need to run:
velas airdrop 10 --faucet-host airdrop.testnet.velas.com
Your app registration
To start integration you have to register your app in blockchain:
vpl-relying-party create-account [YOUR_APP_NAME] [ICON_CID] [URL] [REDIRECT_URIs]
If you dont have uploaded your app icon on ipfs you can use default ICON_CID (QmXpQXWoBpf8vNxdKpxtiZALXn4Pk6K7nNe5RqiKD2VGJX) or use online uploader
ARGS:
<YOUR_APP_NAME> Your app name (example: "Demo Shop");
<ICON_CID> IPFS CID of image of your app logo (example: QmXpQXWoBpf8vNxdKpxtiZALXn4Pk6K7nNe5RqiKD2VGJX);
<URL> Domain name of your app (example: https://your.domain.com);
<REDIRECT_URIs> Allowed redirect URIs for VAccount (example: http://localhost:3000 https://your.domain.com/login);
Sponsor backend (SPONSOR_API_HOST)
Also before integration you need backend that will pay for user auth transactions. Use example solution to start with (change it according to your needs).
Initialize
import { VAClient } from '@velas/account-client';
const vaclient = new VAClient({
mode: 'redirect',
clientID: '{YOUR_CLIENT_ID}' // Relying Party Address from prerequisites step
redirectUri: '{YOUR_REDIRECT_URI}' // https://your.domain.com/logintransactionsSponsorApiHost
StorageHandler, // example/storageHandler.js
KeyStorageHandler, // example/keyStorageHandler.js
accountProviderHost: '{YOUR_AGENT_DOMAIN}' // domain of authentication provider (account.testnet.velas.com)
networkApiHost: '{NODE_ADDRESS}' // https://api.testnet.velas.com
transactionsSponsorApiHost: '{SPONSOR_API_HOST}' // https://your.domain.com
transactionsSponsorPubKey: '{SPONSOR_PUB_KEY}' // fRppcHVjpknkW5N5R9sfRppQxYJrJYVV7QJGKchkQj5
API
authorize(options, callback)
Redirects to the /authorize
endpoint to start an authentication transaction.Velas Account will call back to your application with the results at the specified redirectUri
.
Note: The default scope for this method is openid
.
vaclient.authorize({
csrfToken: async function () {
const response = await fetch(`${SPONSOR_API_HOST}/csrf`);
const result = await response.json();
return result.token
},
scope: 'VAcccHVjpknkW5N5R9sfRppQxYJrJYVV7QJGKchkQj5:11', // transfer scope
}, processAuthResult);
handleRedirectCallback(options, callback)
Parses a URL hash fragment to extract the result of an Velas Account authentication response.
const processAuthResult = (err, authResult) => {
if (authResult && authResult.access_token_payload) {
setCurrentSession(authResult); // your success login hendler
} else if (err) {
setError(err.description); // your error hendler
};
};
vaclient.handleRedirectCallback(processAuthResult);
userinfo(access_token, callbakc)
vaclient.userinfo(session.access_token, (err, result) => { // session is authResult object from authorize step;
if (err) {
console.log(err);
} else {
console.log("userinfo", result.userinfo);
};
});
Usage
Sign a transfer transaction
import * as Web3 from '@velas/web3';
import { VelasAccountProgram } from '@velas/account-client';
const { Connection, PublicKey } = Web3;
const connection = new Connection(process.env.NODE_HOST, 'singleGossip');
const fromPubkey = new PublicKey(session.access_token_payload.sub); // session is authResult object from authorize step;
const session_key = new PublicKey(session.access_token_payload.ses);
const storage = await VelasAccountProgram.findStorageAddress({
connection,
accountPublicKey: fromPubkey,
});
const transaction = VelasAccountProgram.transfer({
storage,
fromPubkey,
to,
lamports: 10000, // amount
session_key,
});
const { blockhash } = await connection.getRecentBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = session_key;
vaclient.sendTransaction( session.access_token, { transaction: transaction.serializeMessage() }, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result.signature)
};
});
Sign evm transfer transaction
import Web3 from 'web3';
const web3 = new Web3(vaclient.provider); // vaclient intialized on "Initialize" step;
const nonce = await web3.eth.getTransactionCount(userinfo.evm_address) // userinfo object from "userinfo" step;
web3.eth.sendTransaction({
nonce,
from,
to: '0xB90168C8CBcd351D069ffFdA7B71cd846924d551',
value: this.web3.utils.toWei('0.01', 'ether'),
}).then((e) => {
console.log(e.transactionHash)
}).catch((e) => {
console.log(e)
});