@vegaprotocol/crypto

0.12.0 • Public • Published

@vegaprotocol/crypto

⚠️ Under active development ⚠️

Crypto operations for Vega supporting Node.js and Browsers

Usage

This module supports both CommonJS and ES Modules:

const { VegaWallet, HARDENED } = require('@vegaprotocol/crypto')

VegaWallet.fromMnemonic('...').then(async wallet => {
  const keys = await wallet.keyPair(HARDENED + 0)

  const msg = Buffer.from('Hello world!')
  const signature = await keys.sign(msg)
})
import { VegaWallet, HARDENED } from '@vegaprotocol/crypto'

const wallet = await VegaWallet.fromMnemonic('...')
const keys = await wallet.keyPair(HARDENED + 0)

const msg = Buffer.from('Hello world!')
const signature = await keys.sign(msg)

API

Note: All APIs are async. In some cases they will run sync, eg in Node.js where loading WASM is sync, or where crypto routines are sync, but everything is kept async as a lowest common denominator between browser APIs and future hardware wallet support.

const wallet = await VegaWallet.fromMnemonic(mnemonic)

Derive a new SLIP-10 VegaWallet from a BIP-0039 mnemonic. Note that the mnemonic is not validated before key derivation.

const seed = await VegaWallet.deriveSeed(mnemonic)

Derive a seed from a BIP-0039 mnemonic. In combination with VegaWallet.fromSeed this is equivalent to VegaWallet.fromMnemonic. Note that the mnemonic is not validated before key derivation.

const wallet = await VegaWallet.fromSeed(seed)

Derive a new SLIP-10 VegaWallet from a seed.

const kp = await wallet.keyPair(index)

Generate a new key pair at index, under the Vega specific subtree.

const { name, version } = kp.algorithm

This contains name and version detailing the version used by the instantiated keyPair. Note if this changes in the future other "builder" methods will be exposed to derive newer versions.

const sig = await kp.sign(msg, [chainId])

Sign msg with key pair kp for optional chainId.

const isValid = await kp.verify(sig, msg, [chainId])

Verify sig is valid for msg under key pair kp for optional chainId.

Spec

See algorithm.md and test-vectors.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @vegaprotocol/crypto

Weekly Downloads

139

Version

0.12.0

License

MIT

Unpacked Size

318 kB

Total Files

53

Last publish

Collaborators

  • dexturr
  • emilbayes
  • curly42
  • eddhannay
  • vega-bot