@i3m/server-wallet
TypeScript icon, indicating that this package has built-in type declarations

2.6.1 • Public • Published

License: EUPL_1.2 Contributor Covenant JavaScript Style Guide

@i3m/server-wallet

Get a ready-to-go Wallet requiring no user interaction and using a simple file to store wallet data. It should be used only when no human interaction is possible.

Usage

@i3m/server-wallet can be imported to your project with npm:

npm install @i3m/server-wallet

Then either require (Node.js CJS):

const serverWallet = require('@i3m/server-wallet')

or import (JavaScript ES module):

import * as serverWallet from '@i3m/server-wallet'

The appropriate version (either cjs or esm) should be automatically chosen when importing. However, if your bundler does not import the appropriate module version, you can force it to use a specific one by just importing one of the followings:

  • @i3m/server-wallet/dist/cjs/index.node: for Node.js CJS module
  • @i3m/server-wallet/dist/esm/index.node: for Node.js ESM module

If you are coding TypeScript, types will not be automatically detected when using the specific versions. You can easily get the types in by creating adding to a types declaration file (.d.ts) the following line:

declare module '@i3m/server-wallet/dist/esm/index.browser' // use the specific file you were importing

The server wallet uses a file as storage. Optional filepath is the path to the Wallet's storage file. If you are using a container it should be a path to a file that persists (like one in a volume)

The wallet's storage-file can be encrypted for added security by passing an optional password.

Example of instantiation of a server wallet in typescript:

const wallet = await serverWalletBuilder({ password: '1735b07cb074bc1057vc130377$(==)(5v0bx23YGSA', filepath: '/path/where/the/wallet/will/store/things' })

For connecting to the blockchain, the wallet server requires an RPC endpoint. It comes with one configured by default, but just in case it is malfunctioning or you just need to use another one, you can create your wallet as:

const wallet = await serverWalletBuilder({
  password: '1735b07cb074bc1057vc130377$(==)(5v0bx23YGSA',
  filepath: '/path/where/the/wallet/will/store/things',
  provider: 'did:ethr:i3m', // should match the key in `providerData`
  providerData: {
    'did:ethr:i3m': {
      network: 'i3m',
      rpcUrl: 'http://95.211.3.249:8545' // The URL of your RPC endpoint
    }
  }
})

Create an identity (DID)

const resp = await wallet.identityCreate({
  alias: 'identity1'
})
console.log(`DID for identity1 created: `, resp.did)

List identities

const identities = await wallet.identityList({})
console.log('Identities: ', identities)

Generate a signet JWT

You can generate a signature as a JWT for a generic JSON object as, for instance:

const objectToSign = {
  field1: 'yellow',
  field2: 'brown'
}
jwt = (await wallet.identitySign({ did: 'one of the dids in the wallet' }, { type: 'JWT', data: { payload: objectToSign } })).signature

Verify a signed JWT

You can also use your wallet to verify a JWT signed by other wallets as:

const verification = await wallet.didJwtVerify({ jwt })
if (verification.verification === 'success') {
  // properly verified
} else {
  // failed with error msg in verification.error
}

The verification can also check for specific payload claims. An expected value of '' can be used to just check that the claim is in the payload.

const verification = await wallet.didJwtVerify({
  jwt,
  expectedPayloadClaims: {
    field1: 'yellow'  // check that "field1"="yellow" is in the JWT payload
    field2: '' // check that "field2" is defined in the JWT payload
  }
})
if (verification.verification === 'success') {
  // properly verified
} else {
  // failed with error msg in verification.error
}

API reference documentation

Check the API

Package Sidebar

Install

npm i @i3m/server-wallet

Weekly Downloads

2

Version

2.6.1

License

EUPL-1.2

Unpacked Size

45.8 kB

Total Files

10

Last publish

Collaborators

  • rish2497
  • ferogar
  • juanelasisme