@credify/nodejs
    TypeScript icon, indicating that this package has built-in type declarations

    1.15.1 • Public • Published

    npm

    Credify NodeJS

    How to install

    # yarn
    $ yarn add @credify/nodejs
    
    # npm
    $ npm install @credify/nodejs

    How to use

    Initialization

    const { Credify } = require("@credify/nodejs");
    const signingPrivateKey = "";
    const apiKey = "";
    const config = {
      mode: "sandbox",
    };
    const credify = await Credify.create(signingPrivateKey, apiKey, config);

    Auth - token introspection

    This function validates an access token passed from Credify. You need to pass the token and a specific scope to test its validity.

    const token =
      "eyJhbGciOiJSUzI1NiIsImtpZCI6InB1YmxpYzo4ZDA1NjJmNS0zMjI0LTQ1OGYtODBkZS01YjczNTdkMWUwMjIiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOltdLCJjbGllbnRfaWQiOiI4ZjUzMGRmNi05OGFhLTQ4NDktYmNhZi1kNjI3MDU0MzUyNjYiLCJleHAiOjE2MTcxOTA4MDUsImV4dCI6eyJlY29zeXN0ZW1faWQiOiI2MmZjNTcyNC0zNzUxLTQ3OTctYmU3OS02MDU2MzVmYTZjOTMifSwiaWF0IjoxNjE2NTg2MDA1LCJpc3MiOiJodHRwczovL2Rldi1vaWRjLWNvcmUuY3JlZGlmeS5vbmUvIiwianRpIjoiNzM5ZjY0MTQtY2I1MS00YmM5LTg5NzUtYjQ3Nzc0MWNiODBjIiwibmJmIjoxNjE2NTg2MDA1LCJzY3AiOlsib3JnYW5pemF0aW9uIiwib2lkY19jbGllbnQiLCJjbGFpbV9wcm92aWRlciJdLCJzdWIiOiI4ZjUzMGRmNi05OGFhLTQ4NDktYmNhZi1kNjI3MDU0MzUyNjYifQ.PODoedbtpiZ2Uwe5XNRbQTdMY6C4cSQVXqxlt0yaLJqNkjo6dYLPp6GvcbJmCfMCJAkTXK13_Il7Ojl4jkY9K2fJwsqdTpsKbw5A4HPl4snhBMBkUGBMud2DmAYRo7N_DLUX0YEAaQ91OKxlM7ddAcANbkx8M9MTgXNxN3UDBZKL8KaGiN7Szm1gKXqaaRxM0_pl96xxktpsbLO5uYKIlpzPp_4h5N-TAkChK9BLnXUjQRccphmtMFeUTf9br7kVyFKgewDWsyF_UAv7gegf7qY4TPYJb3WNmllnrVGglhSVug2xCczn90a_EyLscG2b7KwHobRrgxowtb-jcHfHuu7OHQG6LU75YyoXiiNzg6qVh4VDQJpiC3Z7rkQQaL9_JHXtq5sHPh7Y7TmozeIQhvaG93vXJXePEfGyqS2wXs_XYlSAtjZJ3ALo7qdbfbWO94EUyPg_hGIU3t3LYn1r8VUbVTNpjkfYbQnfnoZMBhuZ2lPcyNuXtdO73470RX2vKbU_eKo_jEPIVScOtnYlGOLnLyymTLVKgd_PrKckOycjqB-2yQ0o4fX3puSNvpHw4mrcPDOb0G7g35JL2tDyRCU7Arh22VEE6XclkPkQbbdvRImxXplH9grBhxN4t7DVM-LtYDNMW_RJWlOKcdDQJQgNbHPJCsZBby2FUp84Pvw";
    const scope = "organization";
    credify.auth
      .introspectToken(token, scope)
      .then((isValid) => console.log(isValid));

    Create a new account

    This function generates a new user with a provided profile and password on the Credify system. The output is a new user's ID.

    const profile = {
      name: {
        first_name: "John",
        last_name: "Kane",
      },
      emails: [
        {
          email: "john.kane@credify.one",
        },
      ],
      phones: [
        {
          phone_number: "38123456789",
          country_code: "+84",
        },
      ],
    };
    const password = "Aa12345678!";
    credify.entity.create(profile, password).then((id) => {
      console.log(id);
    });

    Push a custom claim

    This records claim tokens into a blockchain. The claim tokens will be generated out of provided claim values.

    const organizationId = "";
    const entityId = "";
    const claims = {
      "your-scope-name-1": {
        "your-claim-name-1": "this-entity-claim-value",
        "your-claim-name-2": "this-entity-claim-value",
      },
      "your-scope-name-2": {
        "your-claim-name-3": "this-entity-claim-value",
        "your-claim-name-4": "this-entity-claim-value",
      },
    };
    credify.claims.push(organizationId, entityId, claims).then((res) => {
      console.log(res);
    });

    Encrypt claims

    This encrypts claim values with a passed encryption public key.

    const claims = {
      "your-scope-name-1": {
        "your-claim-name-1": "this-entity-claim-value",
        "your-claim-name-2": "this-entity-claim-value",
      },
      "your-scope-name-2": {
        "your-claim-name-3": "this-entity-claim-value",
        "your-claim-name-4": "this-entity-claim-value",
      },
    };
    const encryptionPublicKey = "";
    credify.claims.encrypt(claims, encryptionPublicKey).then((res) => {
      console.log(res);
    });

    Get offers for Market

    This retrieves filtered offers list.

    credify.offer.getList("123").then((res) => {
      console.log(res);
    });

    Update offer transaction's status for Service Provider

    This updates status of a specified offer transaction.

    const id = "xxxx";
    const status = "COMPLETED";
    credify.offer.updateStatus(id, status).then((res) => {
      console.log(res);
    });

    If the transactionAmount is dynamic, you may pass transactionAmount to this function.

    const id = "xxxx";
    const status = "COMPLETED";
    const transactionAmount = {
      currency: "VND", // "VND" || "USD" || "JPY"
      value: 250000,
    };
    credify.offer.updateStatus(id, status, transactionAmount).then((res) => {
      console.log(res);
    });

    If have referenceId, you may pass referenceId to this function.

    const id = "xxxx";
    const status = "COMPLETED";
    const referenceId = "referenceId";
    credify.offer.updateStatus(id, status, null, referenceId).then((res) => {
      console.log(res);
    });

    If the vat is dynamic, you may pass vat to this function.

    const id = "xxxx";
    const status = "COMPLETED";
    const vat = {
      currency: "VND", // "VND" || "USD" || "JPY"
      value: 250000,
    };
    credify.offer.updateStatus(id, status, null, null, vat).then((res) => {
      console.log(res);
    });

    Get campaigns for Service Provider

    This returns a list of campaign list.

    const offerStatus = "APPROVED"; // APPROVED || REJECTED || IN_REVIEW
    const campaignStatus: "APPROVED"; // APPROVED || REJECTED || IN_REVIEW || ARCHIVED || DRAFT
    credify.offer.getCampaigns(offerStatus, campaignStatus).then((res) => {
      console.log(res);
    });

    Get offer detail for Service Provider

    This returns detail information of a specified offer.

    const offerCode = "";
    credify.offer.getOfferDetail(offerCode).then((res) => {
      console.log(res);
    });

    Kicks off OIDC

    This generates an ephemeral encryption key to secure an OIDC flow.

    const organizationId = "";
    const redirectUrl = "";
    const scopes = ["openid", "phone", "email"];
    const options = {
      userId: "",
      offerCode: "",
    };
    credify.oidc
      .initiateOIDC(organizationId, redirectUrl, scopes, options)
      .then((res) => {
        console.log(`OIDC URL >>> ${res.oidcUrl}`);
        console.log(`Private key >>> ${res.privateKey}`);
      });

    Generate an access token

    This manages OpenID Connect token API, generating a new access token from an authorization code. This is a helper function when you use an authorization flow.

    const id = "YOUR ID";
    const code = "AUTHORIZATION CODE";
    const redirectUrl = "YOUR REDIRECTION URL";
    credify.oidc.generateAccessToken(id, code, redirectUrl).then((response) => {
      console.log(response);
    });

    GET userinfo

    This manages OpenID Connect userinfo API. This needs an ephemeral private key that is generated in the above step, and returns decrypted user information. Note that private key needs to be base64 URL encoded string value.

    const accessToken = "";
    const privateKeyInBase64Url = "";
    credify.oidc.userinfo(accessToken, privateKeyInBase64Url).then((userinfo) => {
      console.log(userinfo);
    });

    Evaluate offer

    This evaluates an offer based on conditions and required scopes.

    // prepare claims for the user, could be read from database for example
    const claims: Claims = {
      scope1: {
        claim1: 2.0,
      },
    };
    
    // offer data if from payload of offer filtering API or offer evaluation API
    const condition: OfferCondition = {
      kind: OfferConditionKind.InRangeCondition,
      claim: {
        name: "claim1",
        valueType: "FLOAT",
        scope: {
          name: "scope1",
        },
      },
      value: 1.0,
      upper: 3.0,
    };
    
    const requiredScopes = [["scope1"], ["scope2"]];
    // evaluate the offer based on conditions and required scopes
    const result = credify.offer.evaluateOffer([condition], requiredScopes, claims);

    Keywords

    Install

    npm i @credify/nodejs

    DownloadsWeekly Downloads

    85

    Version

    1.15.1

    License

    MIT

    Unpacked Size

    195 kB

    Total Files

    31

    Last publish

    Collaborators

    • ngo275
    • credify-pte-ltd