blockchain-certificates / blockcerts-verifier

A Blockcerts verifier and viewer
MIT License
65 stars 53 forks source link

\<blockcerts-verifier>

Build Status codecov semantic-release

A standalone universal viewer & verifier for blockcerts credentials

Production

The component is developed with Polymer 3. To use the component in your project, install it via:

  npm i @blockcerts/blockcerts-verifier
  <script type="module" src="https://github.com/blockchain-certificates/blockcerts-verifier/raw/master/node_modules/@blockcerts/blockcerts-verifier/dist/main.js"></script>

  <blockcerts-verifier></blockcerts-verifier>

Chrome will support natively the code, but for Firefox, Safari, MS Edge (Opera and Brave), you will need to add the webcomponent loader before:

    <script src="https://github.com/blockchain-certificates/blockcerts-verifier/raw/master/node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>

Have a look at the Demo Pages to see examples of the usage

API Usage

Default behavior

By the default, the component will:

API

The component will understand the following options:

Custom Blockchain explorers - explorerAPIs

Since v4.1.0 of cert-verifier-js accepts custom blockchain explorers, Blockcerts Verifier facilitates communicating such service for the verification process.

As the object would be quite complicated, the option cannot be passed as attribute, but rather via property, as follows:

const explorer = {
  parsingFunction: function (): TransactionData {},
  serviceURL: 'your-explorer-service.url',
  priority: 0 | 1
}

document.addEventListener('DOMContentLoaded', function () {
  const bv = document.querySelector('blockcerts-verifier');
  bv.explorerAPIs = [explorer];
});

See this section: https://github.com/blockchain-certificates/cert-verifier-js#explorerapis to get more information.

Event Tracking API

The component will emit events on different moment of the certificate life cycle. To subscribe and track these events you should add on your consumer page event listeners on the window object.

See the event demo page for a working example.

The information is communicated via the detail key of the event.

Supported Events:

Development

Viewing Your Element

npm run start

Will make the demo page available on http://localhost:8081/demo/.

Modifying the Sanitizer

The sanitizer is used in order to protect against malicious certificates that could hold XSS attacks. It is an overlay of the xss library, since at times, you might want to be able to configure or adapt the whitelist to your own needs.

To modify it, you should edit the sanitizer/index.js file.

Whitelist CSS properties

More specifically if you wish to whitelist some CSS properties, add them to the object whiteListedCssProperties.

Generate the updated sanitizer

  npm run build:sanitizer

This will generate the sanitizer.js file, which is then used by the application and the tests.

If you want to work on the sanitizer in watch mode (and auto-generate your changes), use the following command:

  npm run build:sanitizer -- -w

Running Tests

Application Tests

npm run test:application

NOTE: application must be started to run the tests, or at the very least the mock-server via the npm run start:mock-server (automatically included in the npm run start command).

watch mode

npm run test:application:watch

Component Tests

npm run test:components

"watch" mode

npm run test:components:persist

Will allow refreshing the test page: http://localhost:8000/components/blockcerts-verifier/generated-index.html?cli_browser_id=0

Dealing with CSS

The npm run start command will also start a SASS compiler watcher, which means that any stylesheet within the components folder will be transpiled to a polymer component that can be reused within another component. ie:

import CSS from './_components.button-css';
[...]
_render () {
    return html`${CSS}[...]`
}

Using shared styles

To reduce the amount of code duplication, and following the ITCSS philosophy, you may need to import some of the shared-styles in your component. To do so, in your component's SASS file, add the following instruction:

/* in _components.my-component.sass */

@import '../../../shared-styles/objects.text';

[...component styles]

@import '../../../shared-styles/utils.a11y';

Please note that the SASS watcher does not observe changes in the shared styles folder, and will not automatically recompile any consumer stylesheet. You will have to recompile them yourselves (TODO: improve DevX here).

More info

Please have a look through the ADR documentation to get more context around the architecture and the ways of developing a component.