Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@signumjs/crypto

signum-network65Apache-2.02.0.7TypeScript support: included

Cryptographic functions for building Signum Network apps.

signum, signum-network, blockchain, smartcontracts, crypto, cryptography, signumjs

readme

@signumjs/crypto

Cryptographic functions for building Signum apps.

Featured on Openbase

Installation

SignumJS can be used with NodeJS or Web. Two formats are available

Using with NodeJS and/or modern web frameworks

Install using npm:

npm install @signumjs/crypto

or using yarn:

yarn add @signumjs/crypto

Example

import {sha256AsHex, Crypto, NodeJSCryptoProvider} from '@signumjs/crypto'

Crypto.init(new NodeJSCryptoProvider()); // or WebCryptoProvider
console.log(sha256AsHex('test'))

Using in classic <script>

Each package is available as bundled standalone library using UMD. This way signumJS can be used also within <script>-Tags. This might be useful for Wordpress and/or other PHP applications.

Just import the package using the HTML <script> tag.

<script src='https://cdn.jsdelivr.net/npm/@signumjs/crypto/dist/signumjs.crypto.min.js'></script>

Example

console.log(sig$crypto.sha256AsHex('test'))

The "legacy" web bundle initializes the Crypto module automatically with the WebCryptoAdapter. So, no initialization is necessary.

See more here: @signumjs/crypto Online Documentation

Crossplatform Usage

As there are different crypto implementations for different platforms available the underlying crypto contexts need to be initialized. The crypto package provides used out of the box implementations for modern web browsers and NodeJS (and alike backends, i.e. deno and bun). Depending on the runtime environment the correct CryptoAdapter-implementation needs to be set for cryptographic routines. In a web browser the Crypto Web API is used, i.e. a secure (https) environment is required. In NodeJS the NodeJS Crypto API is used.

Run the following before any usage of crypto functions

Web

import {Crypto, WebCryptoAdapter} from "@signumjs/crypto"
Crypto.init(new WebCryptoAdapter());

NodeJS (Deno, Bun)

import {Crypto, NodeJSCryptoAdapter} from "@signumjs/crypto"
Crypto.init(new NodeJSCryptoAdapter());

For web localhost is considered a secure context

If using signumjs.crypto.min.js the initialization is not required. It is automatically set to WebCryptoAdapter

Implementing CryptoAdapter-Interface

If needed in other environments, e.g. React Native, a custom implementation of the CryptoAdapter interface is required. The interface implements the bare minimum crypto functions needed for Signum:

export interface CryptoAdapter {
    encryptAes256Cbc(plaintext: Uint8Array, key: Uint8Array): Promise<Uint8Array>;

    decryptAes256Cbc(ciphertext: Uint8Array, key: Uint8Array): Promise<Uint8Array>;

    sha256(data: ArrayBuffer): Promise<Uint8Array>;

    getRandomValues(array: Uint8Array): Uint8Array;
}

Like this:

import {type CryptoAdapter} from '@signumjs/crypto'

class CustomCryptoAdapter implements CryptoAdapter {
    decryptAes256Cbc(ciphertext: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
        // Do your platforms implementation here
        return Promise.resolve(undefined);
    }

    encryptAes256Cbc(plaintext: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
        // Do your platforms implementation here
        return Promise.resolve(undefined);
    }

    getRandomValues(array: Uint8Array): Uint8Array {
        // Do your platforms implementation here
        return undefined;
    }

    sha256(data: ArrayBuffer): Uint8Array {
        // Do your platforms implementation here
        return undefined;
    }

}

Then use the custom crypto provider like this:

import {Crypto, sha256AsHex} from '@signumjs/crypto'

Crypto.init(new CustomCryptoAdapter());

(async ()=> {
    // internally uses the custom crypto provider
    console.log("SHA256", await sha256AsHex("blablubb"))
})()

changelog

Change Log

2.0.7

Patch Changes

  • 1a64657: improved crypto randomness for word and string generators

2.0.6

Patch Changes

  • Breaking Change: renamed isAttachmentVersion to getAttachmentVersion and typed the version identifier
  • Added main field in package.json to fix resolution issues

2.0.5

Patch Changes

  • Uses exports in package.json and minor fixes

2.0.4

Patch Changes

  • [Breaking Change] CryptoProvider renamed to CryptoAdapter

    • Crypto Adapters NodeJsCryptoAdapter and WebCryptoAdapter are dedicate path @signumjs/crypto/adapters
    • generateMnemonic has changed API to better support partial settings.

2.0.3

Patch Changes

  • Decoupled Crypto Provider. Breaking Change as this requires the developer to define the platform specific crypto provider before using the sdk

    Breaking Change:

    If you see the following error:

    "No Crypto Provider provided - Use [Crypto.init()] first";

    You need to initialize the crypto module with the platform specific CryptoProvider.

    NodeJS

    import { Crypto, NodeJSCryptoProvider } from "@signumjs/crypto";
    Crypto.init(new NodeJSCryptoProvider());

    Web/Browser

    import { Crypto, WebCryptoProvider } from "@signumjs/crypto";
    Crypto.init(new WebCryptoProvider());

    Further implementations will be provided as external modules/packages, i.e. React Native Expo

2.0.2

Patch Changes

  • Crypto: Custom Crypto Provider on getInstance

2.0.1

Patch Changes

  • Added IIFE bundles

2.0.0

Major Changes

  • 14f4944: This new version uses a completely modernized stack (turborepo, vite, vitest). Furthermore, the crypto package was rewritten to remove deprecated crypto-js library and being extensible. The standards (SRC) packages were reorganized and finally the monitor package was removed Breaking Changes:
    1. @signumjs/monitor was removed, as not really used
    2. @signumjs/crypto was completely rewritten, mainly due to the deprecation of crypto-js and certain security implications
    3. @signumjs/standards were reorganized respecting the Signum Request for Comment (SRC) numbering How to update from 1.0 to 2.0
    4. Monitor was removed Drop signumjs/monitor (no replacement here) - if you used the monitor you can copy the code from the repo and include it manually in your code base
    5. Adjust Crypto
    6. Passphrase generator was dropped, use generateMnemonic instead
    7. generateMasterKeys was renamed to generateSignKeys
    8. hashSHA256 was renamed to sha256AsHex (and more sha256 hashers are available)
    9. encryptAES and decryptAES were dropped - use native crypto methods for AES encryption. Signum uses for their P2P encryption a AES based algorithm using the shared key (encryptMessage and encryptData)

      The new crypto package offers more secure random and sha256 functions and is much more flexible. Check the docs for more info

    10. Using SRC based standards Actually, not much to do here. It's just that the internal structure is organized by the SRC numbering. All notable changes to this project will be documented in this file.

1.0.0 (TBR)

  • Rebranding
  • Patched AES.decrypt method to throw concise error
  • Removed crypto-js. Refactored complete interface

Before Rebranding

0.4.1

  • Patched AES.decrypt method

0.4.0

  • Changed License: From GPL-3.0 to Apache 2.0
  • Updated seedrandom from 3.0.1 to 3.0.5 (removes eval)

0.3.0

  • Moved typings into src folder

0.2.0

  • cleaned up internal converter functions
    • remove unused functions
  • fixed seedrandom import issue

0.1.3

  • now available as standalone bundle (iife)

0.1.1

  • Encryption/Decryption compatible with BRS
  • removed big.js

0.1.0-rc.3

  • Encryption/Decryption of message works (not for old messages yet)
  • reduced package size
    • words dictionary follows BIP39 standard
    • exchanged BN.js by big.js