npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@velas/account-client

v0.3.9

Published

browser sdk

Downloads

20

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:

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)
});