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

Package detail

cardano-addresses

input-output-hk389Apache-2.03.12.0TypeScript support: included

readme

cardano-addresses TypeScript binding

This is a Typescript (or Javascript) version of the cardano-addresses API.

For the time being, this module is experimental, and exposes only a subset of the functionality of the Haskell API. We would like to increase the TypeScript binding's coverage of the Haskell API in future, as needs arise.

How it works

This package comprises of four parts:

Development Info

NodeJS module: Building and testing

Start a nix-shell in this directory (not the top-level) and run:

$ cd jsapi
$ nix-shell
[nix-shell:~/iohk/cardano-addresses/jsapi]$ npm install && npm run build
...
[nix-shell:~/iohk/cardano-addresses/jsapi]$ npm run test

Behind the scenes, this will use Nix to make the ghcjs build of the cardano-addresses library. The path to this Javascript file is stored in the $CARDANO_ADDRESSES_JS environment variable.

Haskell module: Building and testing

To try it out run nix-shell from the repo top-level directory:

$ nix-shell
[nix-shell:~/iohk/cardano-addresses]$ js-unknown-ghcjs-cabal build cardano-addresses-jsapi:jsapi-test
[nix-shell:~/iohk/cardano-addresses]$ node dist-newstyle/build/js-ghcjs/ghcjs-8.10.4/cardano-addresses-jsapi-3.5.0/t/jsapi-test/build/jsapi-test/jsapi-test.jsexe/all.js

That test initializes the api from a JS function that is called from Main.hs. To build .js file that might be easier to use from a JS app run:

$ nix-build jsapi/default.nix -A cardano-addresses-js
/nix/store/dw0xwvjvwac68i2a4dkkpx4mw8yji9z8-cardano-addresses-js-3.5.0
$ tree ./result
./result
├── cardano-addresses-jsapi.cjs.js
└── cardano-addresses-jsapi.js

This replaces the regular runmain.js with jsapi/glue/runmain.js. That exposes a single function you can call and pass in a continuation.

To initialize, call the runCardanoAddressesApi with a continuation that like the one in jsapi/glue/test.js. You will be passed api and cleanup.

TODO

  • <input disabled="" type="checkbox"> More API endpoints depending on user needs.
  • <input disabled="" type="checkbox"> Used "typed" objects as parameters for the inspectAddress API, instead of strings which must be parsed.
  • <input disabled="" type="checkbox"> Add a build step to optimise output file sizes (i.e. minification, tree shaking, etc).
  • <input disabled="" type="checkbox"> Solve issue on nodejs where registered event handlers remain after API cleanup, preventing the nodejs runtime from exiting.
  • <input disabled="" type="checkbox"> Add helper functions to the JSaddle API so that it can output code for ES6 Promises.
  • <input disabled="" type="checkbox"> Bring back headless testing of JSaddle code.
  • <input disabled="" type="checkbox"> Replace TSDX with a better build system.
  • <input disabled="" type="checkbox"> Automatically update docs in CI.
  • <input disabled="" type="checkbox"> Add release workflow to CI which builds the package and uploads to NPM.

More details

NodeJS package

For better or worse, this is based on the TSDX template. The TSDX standard documentation follows...

Commands

To run TSDX, use:

npm start # or yarn start

This builds to /dist and runs the project in watch mode so any edits you save inside src causes a rebuild to /dist.

To do a one-off build, use npm run build or yarn build.

To run tests, use npm test or yarn test.

Configuration

Code quality is set up for you with prettier, and lint-staged. Adjust the respective fields in package.json accordingly.

Jest

Jest tests are set up to run with npm test or yarn test.

Bundle Analysis

size-limit is set up to calculate the real cost of your library with npm run size and visualize the bundle with npm run analyze.

Rollup

TSDX uses Rollup as a bundler and generates multiple rollup configs for various module formats and build settings. See Optimizations for details.

TypeScript

tsconfig.json is set up to interpret dom and esnext types, as well as react for jsx. Adjust according to your needs.

Continuous Integration

GitHub Actions

Two actions are added by default:

  • tsdx-build which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
  • tsdx-size which comments cost comparison of your library on every pull request using size-limit

Optimizations

Please see the main tsdx optimizations docs. In particular, know that you can take advantage of development-only optimizations:

// ./types/index.d.ts
declare var __DEV__: boolean;

// inside your code...
if (__DEV__) {
  console.log('foo');
}

You can also choose to install and use invariant and warning functions.

Module Formats

CJS, ESModules, and UMD module formats are supported.

The appropriate paths are configured in package.json and dist/index.js accordingly. Please report if any issues are found.

Publishing to NPM

TSDX recommend using np.

changelog

[4.0.0] - 2025-02-04

Added

  • Unify core and command-line packages into one
  • Drop JavaScript support and get rid of JavaScript codebase
  • CIP-0129 support added, affecting cardano-address key hash and cardano-address script hash
  • CIP-0129 support with backward compatibility added, affecting cardano-address key hash and cardano-address script hash
  • scriptHashToText, scriptHashFromText and prettyErrScriptHashFromText added to Cardano.Address.Script
  • Simplify filenames by using a hashed prefix instead of the full mnemonic, enhancing portability).
  • hashKey for Shelley style
  • Key derivation support for DRep, CCCold and CCHot in accordance with CIP-0105. Also supported in CLI.
  • Fix field for staking scripts in AddressInfo.
  • Rename infoScriptHash field in AddressInfo to infoSpendingScriptHash.
  • Get signing key and chain code from extended signing key via cardano-address key private
  • Support for new environments preview and preprod.
  • Supported derivation of root private key using second factor mnemonic or its base16/base64/plain text equivalent

[3.12.0] - 2022-08-17

Added

  • Supported credential construction from key hashes in both payment and delegation credential. The construction scheme is also supported in CLI.

[3.11.0] - 2022-06-21

Added

  • Compatibility with Aeson 2 library.

[3.10.0] - 2022-05-26

Added

  • cardano-address key walletid support for Shared wallets.

[3.9.0] - 2022-03-08

Added

  • Support for policy key derivation and hashing according to CIP-1855.

[3.8.0] - 2022-02-11

Added

Changed

  • Several nodejs package dependencies updated according to npm audit and dependabot alerts.

[3.7.0] - 2021-12-15

Added

  • Address type in address inspect command.

[3.6.0] - 2021-09-08

Changed

  • Bech32 prefixes for shared account addresses were fixed to match the latest draft of CIP-1854.

[3.5.0] - 2021-06-28

Added

  • Initial release of a NPM Package which uses the GHCJS build.

Changed

  • The supported compiler version is now GHC 8.10.4.

  • Updated validateScriptTemplate and validateScriptOfTemplate functions for multi-signature wallets.

[3.4.0] - 2021-04-30

Added

  • Added Cardano.Address.Style.Shelley.eitherInspectAddress function with stronger result and error types.

  • Added Cardano.Address.Style.Shared module which defines a shared wallet style enabling multisig.

Changed

  • The constructors of Cardano.Address.Style.Shelley.ErrInspectAddress have changed. Any code which pattern matches on this type will need minor changes.

  • A number of Bech32 prefixes were changed to account for CIP changes. The whole family of *_shared_* prefixes were introduced to accommodate newly added shared wallet style. In specific, there in no longer script_vkh but addr_shared_vkh and stake_shared_vkh to denote spending and stake verification key hashes, respectively.

  • KeyHash now needs KeyRole values to be specified, except binary payload. It was needed change to enable differentiating between spending and stake key hashes.

Removed

  • Multisig related functions were deleted from Cardano.Address.Style.Shelley as they found a new place in Cardano.Address.Style.Shared.

[3.3.0] - 2021-04-09

Added

  • The library now builds with ghcjs and passes tests running on nodejs.

  • The cardano-address inspect command now includes bech32 encodings. In the output, the JSON attribute will have a _bech32 suffix. For example, stake_key_hash_bech32.

Changed

  • The Index type no longer has an Enum instance. Use indexFromWord32 and indexToWord32 instead.

  • (Breaking change) The Cardano.Address.bech32 function will now use the addr_test prefix instead of addr, if the given address is not a mainnet address.

Removed

N/A

[3.2.0] - 2020-01-22

Added

  • Added definition for 'Cosigner'.

  • Added definition for 'ScriptTemplate', ToJSON and FromJSON instances added.

  • Added validation of 'ScriptTemplate'.

  • Added command-line for 'cardano-address script validate'

  • Introduced 'foldScript' function.

Changed

  • Fixed cardano-address --version reporting.

  • Added Ord instance for 'ScriptHash'.

  • Extended script to account for ActiveFromSlot and ActiveUntilSlot, ie., introducing timelocks. ToJSON and FromJSON instances were updated. Also command-line supports timelocks now via 'active_from' and 'active_until`.

  • Introduced Required and Recommended validation and adjusted the current one.

  • Fixed cardano-address address bootstrap example in command-line help.

  • Add missing crc32 integrity check when decoding Byron/Icarus addresses.

  • Parametrized 'Script', and use it with 'ScriptHash' and 'Cosigner'.

  • Better error reporting upon validation.

  • Correcting cardano-address hash example in command-line help.

  • Adding more examples to README.md

Removed

N/A

[3.1.0] - 2020-11-13

Added

N/A

Changed

  • 'keyHashFromText' now works seamlessly with key, extended keys or key hashes. In case a key or extended key is given, the relevant part will be hashed on the fly. Said differently, it means that the command-line and the JSON instance for 'Script' works transparently with keys or key hashes.

  • Fixed a bug with the key hash command which failed when provided with extended keys.

  • The 'FromJSON' instance for 'Script' now runs the validation within the JSON parser, such that when the parser succeeds the resulting 'Script' is indeed valid.

  • The 'FromJSON' instance for 'Script' is now much better at showing errors.

Removed

N/A

[3.0.0] - 2020-11-12

Added

  • Support for (multisig) scripts and script addresses in modules:

    • Cardano.Address.Script
    • Cardano.Address.Script.Parser
  • Support for constructing scripts and script addresses via the command-line.

  • Support for constructing rewards addresses via the library and command-line.

  • New command for computing key and script hashes that are required in the construction of larger objects (e.g. addresses).

  • Support for cabal build.

Changed

  • The command-line API no longer support multi-encoding (base16, bech32 and base58) but instead, enforces bech32 for keys and addresses, with specific human readable prefixes. It is still possible to easily go from base16-encoded data to bech32 by piping data through the bech32 command-line.

  • It is no longer possible to derive child keys to and from any path. Are only allowed:

    • root -> account
    • root -> address
    • account -> address This is reflected in the bech32 prefixes of the inputs and outputs.
  • Allow constructing delegation addresses from a script. This works transparently from previous version of the command-line, but the command now also accepts script hashes as possible valid inputs.

Removed

  • No more --legacy option on the key child command. Which derivation scheme to use is now inferred from the bech32 prefixe used and the derivation path.

  • Cardano.Address.Errors module. Errors data-types have been moved to their respective module Cardano.Address.Styles.{Byron,Icarus,Shelley}

  • Anything related to Jormungandr in both the library and the command-line.

[2.1.0] - 2020-09-29

Added

  • Added constructors to derive keys on the multisig role.
  • Made the parser for --network-tag more user friendly by now accepting pre-defined keywords such as "mainnet" or "testnet".

Changed

  • Renamed AccountingStyle into Role to better capture the semantic of the 4th level in derivation paths.
  • Made script hashes 28-byte long again, after this was fixed upstream in the Cardano ledger.

Removed

N/A

[2.0.0] - 2020-09-10

Added

  • Command-line interface cardano-address for managing recovery-phrases, keys and addresses.
  • Support for Shelley-specific address types.
  • Support for Jormungandr-specific address types.

Changed

  • Repository structure re-organized in two packages: core & command-line.

Removed

N/A

[1.0.0] - 2020-04-21

Added

  • 'Cardano.Mnemonic' module for mnemonic generation and manipulation.
  • 'Cardano.Address' module for address creation, encoding and decoding.
  • 'Cardano.Address.Derivation' module for primitives and abstractions regarding hierarchical derivation of credentials.
  • 'Cardano.Address.Style.Byron' module implementing derivation primitives for 'Byron' addresses.
  • 'Cardano.Address.Style.Icarus' module implementing derivation primitives for 'Icarus' addresses.

Changed

N/A

Removed

N/A