0.3.9 • Published 1 year ago

@velas/account-client v0.3.9

Weekly downloads
-
License
Velas
Repository
-
Last release
1 year ago

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

1 year ago

0.3.7

1 year ago

0.3.0

1 year ago

0.3.6

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.2.40

2 years ago

0.2.39

2 years ago

0.2.38

2 years ago

0.2.37

2 years ago

0.2.36

2 years ago

0.2.35

2 years ago

0.2.34

2 years ago

0.2.33

2 years ago

0.2.32

2 years ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.30

2 years ago

0.2.31

2 years ago

0.2.29

2 years ago

0.2.28

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.1

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.1.32

3 years ago

0.1.31

3 years ago

0.1.30

3 years ago

0.1.29

3 years ago

0.1.27

3 years ago

0.1.28

3 years ago

0.1.26

3 years ago

0.1.25

3 years ago

0.1.24

3 years ago

0.1.22

3 years ago

0.1.21

3 years ago

0.1.2-0.1

3 years ago

0.1.18

3 years ago

0.1.16

3 years ago

0.1.17

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.13

3 years ago

0.1.14

3 years ago

0.1.15

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago