@cef-ebsi/wallet-lib
TypeScript icon, indicating that this package has built-in type declarations

5.0.1 • Public • Published

EBSI Logo

Wallet Library

The Wallet Library is a TypeScript reference library implementing some generic EBSI-compliant wallet capabilities.

Table of Contents

  1. Installation
  2. Usage
  3. License

Installation

npm install @cef-ebsi/wallet-lib

Usage

Create a Legal Entity DID

import { EbsiWallet } from "@cef-ebsi/wallet-lib";

// Create a random Legal Entity DID
const did = EbsiWallet.createDid();
console.log(did);
// prints "did:ebsi:zsSgDXeYPhZ3AuKhTFneDf1"

// Create a random Legal Entity DID (same as `EbsiWallet.createDid()`)
const did = EbsiWallet.createDid("LEGAL_ENTITY");

Create a Natural Person DID

import { EbsiWallet } from "@cef-ebsi/wallet-lib";
import { calculateJwkThumbprint, exportJWK, generateKeyPair } from "jose";
import { base64url } from "multiformats/bases/base64";

// Create a Natural Person DID based on the provided JWK
const { publicKey } = await generateKeyPair("ES256K");
const jwk = await exportJWK(publicKey);
const did = EbsiWallet.createDid("NATURAL_PERSON", jwk);

Create a wallet

import { EbsiWallet } from "@cef-ebsi/wallet-lib";

// Instantiating a new instance of EBSI Wallet
const privateKey =
  "0a9a229c18f1777949243bbe875b754b77fb9cb3612c8b5c37876888f54f9731";
const wallet = new EbsiWallet(privateKey);

// Get wallet's public key (different formats)
const publicKey = wallet.getPublicKey();
/*
046a6457531aefa19d1840cca004f9ef1cf50d30c508d960a63fceedf336da829501b3c54c16b2d4f325c602e23f17da353ad0ccba6397f082be1e3eaab07cf3c1
*/

const publicKeyPem = wallet.getPublicKey({ format: "pem" });
/*
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEamRXUxrvoZ0YQMygBPnvHPUNMMUI2WCm
P87t8zbagpUBs8VMFrLU8yXGAuI/F9o1OtDMumOX8IK+Hj6qsHzzwQ==
-----END PUBLIC KEY-----
*/

const publicKeyJwk = wallet.getPublicKey({ format: "jwk" });
/*
{
  kty: "EC",
  crv: "secp256k1",
  x: "amRXUxrvoZ0YQMygBPnvHPUNMMUI2WCmP87t8zbagpU",
  y: "AbPFTBay1PMlxgLiPxfaNTrQzLpjl_CCvh4-qrB888E",
}
*/

// Get wallet's Ethereum address
const publicKey = wallet.getEthereumAddress();
// returns 0xA2aa80C25AebE7d3b9984ef45179b4F39737fFBa

Prepare an Ethereum transaction

import { EbsiWallet } from "@cef-ebsi/wallet-lib";
import { JsonRpcProvider } from "@ethersproject/providers";
import { BigNumber } from "@ethersproject/bignumber";

const accessToken = ""; // This is an OAuth2 access token required by Ledger API, see @cef-ebsi/oauth2-auth library
const provider = new JsonRpcProvider({
  url: "https://api-pilot.ebsi.eu/ledger/v3/blockchains/besu",
  headers: {
    authorization: `Bearer ${accessToken}`,
  },
});

const { chainId } = await ethersProvider.getNetwork();

// This is the raw transaction we want to prepare
const rawTransaction = {
  from: "0x32DEF047DeFd076DB21A2D759aff2A591c972248",
  gasPrice: BigNumber.from("0x4a817c800"),
  to: "0x6fC21092DA55B392b045eD78F4732bff3C580e2c",
  value: BigNumber.from("0x186cc6acd4b0000"),
  nonce: 0,
  data: "0xf2c298be000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000067269636d6f6f0000000000000000000000000000000000000000000000000000",
  chainId,
};

const txRequest = await EbsiWallet.prepareEthereumTransaction(
  rawTransaction,
  provider,
);
// txRequest now contains rawTransaction with an estimated gasLimit

License

Copyright (c) 2019 European Commission Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in compliance with the Licence. You may obtain a copy of the Licence at:

Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for the specific language governing permissions and limitations under the Licence.

Readme

Keywords

none

Package Sidebar

Install

npm i @cef-ebsi/wallet-lib

Weekly Downloads

801

Version

5.0.1

License

EUPL-1.2

Unpacked Size

48.7 kB

Total Files

18

Last publish

Collaborators

  • iamtxena
  • yhuard
  • joticajulian
  • zgorizzo
  • maurolucc