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

@sovrano-io/auth-sdk

v1.0.11

Published

Sovrano wallet auth sdk for koinos dapps

Downloads

754

Readme

Sovrano auth-sdk Documentation

The auth-sdk is designed to facilitate dApp integration with Sovrano, supporting authentication and authorization through redirects.

Installation

To install the package, use:

npm install @sovrano-io/auth-sdk

Configuration

To configure the SDK, set the environment variable AUTH_SDK_REDIRECT_URL:

  • Production: https://auth.sovrano.app
  • Testnet: https://auth-testnet.sovrano.app

This variable determines the Sovrano authentication server based on your deployment environment.

Main Functionalities

1. Signup

Purpose: Starts the account creation process on Sovrano.

Request Example

import { Signup } from "@sovrano-io/auth-sdk";

const signupUrl = Signup.getRequestUrl({
    redirectUrl: "https://example.com/callback",
});

// Redirect the user to `signupUrl` to initiate registration

Handling Signup Response

Once the user completes the signup, they are redirected to the specified redirectUrl with the response.

import { Signup } from "@sovrano-io/auth-sdk";

const responseUrl = "https://example.com/callback?data=encodedResponseData";
const signupResponse = Signup.getResponse(responseUrl);

if (signupResponse.type === "success") {
    console.log("Signup successful!");
    console.log("Address:", signupResponse.address);
    console.log("Nickname:", signupResponse.nickname);
} else {
    console.error("Error:", signupResponse.errorMessage);
    console.error("Error Code:", signupResponse.errorCode);
}

2. Connector

Purpose: Initiates the login process to connect the user's Sovrano account to the dApp.

Request Example

import { Connector } from "@sovrano-io/auth-sdk";

const connectUrl = Connector.getRequestUrl({
    appName: "MyDApp",
    redirectUrl: "https://example.com/callback",
});

// Redirect the user to `connectUrl` for login

Handling Connector Response

Upon successful login, Sovrano redirects the user to redirectUrl with their account data.

import { Connector } from "@sovrano-io/auth-sdk";

const responseUrl = "https://example.com/callback?data=encodedResponseData";
const connectResponse = Connector.getResponse(responseUrl);

if (connectResponse.type === "success") {
    console.log("Login successful!");
    console.log("Address:", connectResponse.address);
    console.log("Nickname:", connectResponse.nickname);
} else {
    console.error("Error:", connectResponse.errorMessage);
    console.error("Error Code:", connectResponse.errorCode);
}

3. Authorizer

Purpose: Sends an unsigned transaction to the user for authorization.


Understanding Operation Authorization

To authorize an operation in Sovrano, a transaction is created with two key operations:

  1. Pre-authorization (Allowance): This operation records the pre-authorization details, presenting the operation for the user to review and sign.

  2. Execution: This operation executes the authorized action if it has been pre-approved.

Both validator modules (for pre-authorization) and executor modules (for carrying out the operation) are installed within specific scopes in the user’s account. These scopes define when and under what conditions each module is activated. Validators are responsible for defining and enforcing rules on whether a particular transaction should proceed, while executor modules carry out the approved actions.

For more details on modules, visit the official documentation:
Veive Core Modules - Validation Any

After constructing the transaction, it is signed by the user and redirected back to the dApp, which then decodes and broadcasts it on the blockchain.


Example: Creating a Transaction with koilib

To authorize a transaction, first create an unsigned transaction with koilib.

  1. Install koilib if not already installed:

    npm install koilib
  2. Create an unsigned transaction:

import { Provider, Transaction, OperationJson, Contract } from "koilib";
import { Authorizer, Account } from "@sovrano-io/auth-sdk";

const provider = new Provider("https://api.koinos.io");

const account = new Account({
    address: 'my-user-address',
    provider,
})

const transaction = new Transaction({ provider });

// Step 1: Prepare the allowance operation
const operation = await koin.transfer({
    from: account.address,
    to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
    value: "1012345678", // 10.12345678 KOIN
});

const allow_operation = await account.allow({ operation });
transaction.pushOperation(allow_operation);

// Step 2: Prepare the execution operation
const exec_operation = await account.execute({ operation });
transaction.pushOperation(exec_operation);

await transaction.prepare();

// Request Authorization with the unsigned transaction:
const authorizeUrl = Authorizer.getRequestUrl({
    transactionJson: transaction.transaction,
    redirectUrl: "https://example.com/callback",
    address: "user-address",
});

// Redirect the user to `authorizeUrl` to authorize the transaction
window.location.href = authorizeUrl;

Handling Authorizer Response

After signing, Sovrano redirects the user with the signed transaction in the response.

import { Authorizer } from "@sovrano-io/auth-sdk";

const responseUrl = "https://example.com/callback?data=encodedResponseData";
const authorizeResponse = Authorizer.getResponse(responseUrl);

if (authorizeResponse.type === "success") {
    console.log("Transaction authorized:", authorizeResponse.transactionJson);

    // Send the signed transaction to the blockchain using `koilib`
    const signedTransaction = new Transaction({
        provider,
        transactionJson: authorizeResponse.transactionJson,
    });

    signedTransaction.send()
        .then(receipt => console.log("Transaction successfully sent:", receipt))
        .catch(error => console.error("Error sending transaction:", error));
} else {
    console.error("Authorization Error:", authorizeResponse.errorMessage);
    console.error("Error Code:", authorizeResponse.errorCode);
}