npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cmdcode/musig2

v2.4.3

Published

Create digital signatures based on the Musig2 protocol.

Downloads

57

Readme

musig2

A simple and easy-to-use musig2 library, written in typescript.

  • Simplified version of the latest musig2 protocol BIP0327.
  • Uses four simple methods for a signing session: get_ctx, musign, combine_psigs and verify.
  • Supports key tweaking for taproot script paths.
  • Includes keys util for generating proper keys and nonce values.

NOTE: This library is still under development. Expect dragons!

More documentation coming soon!

Import

This package is available on NPM for easy import into your nodejs or browser-based project:

# Node via NPM:
npm install @cmdcode/musig2
# Node via Yarn:
yarn add @cmdcode/musig2

Example import as an ES module:

import * as musig from '@cmdcode/musig2'

Example import into a browser-based project:

<script src="https://unpkg.com/@cmdcode/musig2"></script>
<script> const musig = window.musig2 </script>

Basic Usage

Here is a basic example of using Musig2 for signing. The steps are as follows:

  • Each signer must collect the public keys and nonces from other signers.
  • Each signer then creates a partial signature and shares it around.
  • Once all partial signatures are collected, any signer can combine them into the full signature.

Check out test/src/demo.test.ts for a full reference implementation.

// Import the package.
import * as musig from '@cmdcode/musig2'

// Encode an example string as bytes.
  const encoder = new TextEncoder()
  const message = encoder.encode('Hello world!')

  // Let's create an example list of signers.
  const signers = [ 'alice', 'bob', 'carol' ]
  // We'll store each member's wallet in an array.
  const wallets : any[] = []
  // Let's also add some additional key tweaks.
  const tweak1  = musig.util.random(32)
  const tweak2  = musig.util.random(32)
  const options = { key_tweaks : [ tweak1, tweak2 ] }

  // Setup a dummy wallet for each signer.
  for (const name of signers) {
    // Generate some random secrets using WebCrypto.
    const secret = musig.util.random(32)
    const nonce  = musig.util.random(64)
    // Create a pair of signing keys.
    const [ sec_key, pub_key     ] = musig.keys.get_keypair(secret)
    // Create a pair of nonces (numbers only used once).
    const [ sec_nonce, pub_nonce ] = musig.keys.get_nonce_pair(nonce)
    // Add the member's wallet to the array.
    wallets.push({
      name, sec_key, pub_key, sec_nonce, pub_nonce
    })
  }

  // Collect public keys and nonces from all signers.
  const group_keys   = wallets.map(e => e.pub_key)
  const group_nonces = wallets.map(e => e.pub_nonce)

  // Combine all your collected keys into a signing session.
  const ctx = musig.ctx.get_ctx(group_keys, group_nonces, message, options)

  // Each member creates their own partial signature,
  // using their own computed signing session.
  const group_sigs = wallets.map(wallet => {
    return musig.sign.with_ctx(
      ctx,
      wallet.sec_key,
      wallet.sec_nonce
    )
  })

  // Combine all the partial signatures into our final signature.
  const signature = musig.combine.psigs(ctx, group_sigs)

  // Check if the signature is valid.
  const isValid1 = musig.verify.with_ctx(ctx, signature)

You can also verify the signature using an independent cryptography library, such as the excellent @noble/curves library by Paul Miller.

// BONUS: Check if the signature is valid using an independent library.
import { schnorr } from '@noble/curves/secp256k1'

const isValid2 = schnorr.verify(signature, message, ctx.group_pubkey)

Development / Testing

This library uses yarn for package management.

## Clean up any old builds.
yarn clean
## Run all tests in the suite.
yarn test
## Build a new release.
yarn release

Bugs / Issues

If you run into any bugs or have any questions, please submit an issue ticket.

Contribution

Feel free to fork and make contributions. Suggestions are welcome!

License

Use this library however you want!

Contact

You can find me on nostr at: npub1gg5uy8cpqx4u8wj9yvlpwm5ht757vudmrzn8y27lwunt5f2ytlusklulq3