GithubHelp home page GithubHelp logo

libp2p / js-libp2p-crypto-secp256k1 Goto Github PK

View Code? Open in Web Editor NEW
10.0 18.0 14.0 1.22 MB

⛔️ DEPRECATED: js-libp2p-crypto-secp256k1 is now included in https://github.com/libp2p/js-libp2p-crypto

Home Page: https://libp2p.io

License: MIT License

JavaScript 100.00%

js-libp2p-crypto-secp256k1's Introduction

⛔️ DEPRECATED: js-libp2p-crypto-secp256k1 is now included in libp2p-crypto

js-libp2p-crypto-secp256k1

Discourse posts Dependency Status js-standard-style

Support for secp256k1 keys in js-libp2p-crypto

This repo contains a js-libp2p-crypto-compatible implementation of cryptographic signature generation and verification using the secp256k1 elliptic curve popularized by Bitcoin and other crypto currencies.

Lead Captain

Friedel Ziegelmayer

Table of Contents

Install

npm install --save libp2p-crypto-secp256k1

Usage

This module is designed to work with js-libp2p-crypto.
Installing libp2p-crypto-secp256k1 will automatically add support for the 'secp256k1' key type, which can be used as an argument to the libp2p-crypto API functions generateKeyPair, unmarshalPublicKey, and marshalPrivateKey. The keys returned from those functions will be instances of the Secp256k1PublicKey or Secp256k1PrivateKey classes provided by this module.

Example

const crypto = require('libp2p-crypto')
const msg = Buffer.from('Hello World')

const key = await crypto.generateKeyPair('secp256k1', 256)
// assuming no error, key will be an instance of Secp256k1PrivateKey
// the public key is available as key.public
const sig = await key.sign(msg)

const valid = await key.public.verify(msg, sig)
assert(valid, 'Something went horribly wrong')

API

The functions below are the public API of this module. For usage within libp2p-crypto, see the libp2p-crypto API documentation.

generateKeyPair([bits])

  • bits: Number - Optional, included for compatibility with js-libp2p-crypto. Ignored if present; private keys will always be 256 bits.

Returns Promise<Secp256k1PrivateKey>

unmarshalSecp256k1PublicKey(bytes)

  • bytes: Buffer

Converts a serialized secp256k1 public key into an instance of Secp256k1PublicKey and returns it

unmarshalSecp256k1PrivateKey(bytes)

  • bytes: Buffer

Returns Promise<Secp256k1PrivateKey>

Converts a serialized secp256k1 private key into an instance of Secp256k1PrivateKey.

Secp256k1PublicKey

.verify(data, sig)

  • data: Buffer
  • sig: Buffer

Returns Promise<Boolean>

Calculates the SHA-256 hash of data, and verifies the DER-encoded signature in sig.

Secp256k1PrivateKey

.public

Accessor for the Secp256k1PublicKey associated with this private key.

.sign(data)

  • data: Buffer

Returns Promise<Buffer>

Calculates the SHA-256 hash of data and signs it, resolves with the DER-encoded signature.

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the IPFS Code of Conduct.

License

MIT

js-libp2p-crypto-secp256k1's People

Contributors

achingbrain avatar aknuds1 avatar alanshaw avatar albertoelias avatar daviddias avatar dependabot-preview[bot] avatar dignifiedquire avatar hugomrdias avatar jacobheun avatar vasco-santos avatar victorb avatar yusefnapora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-libp2p-crypto-secp256k1's Issues

Secp256k1 public key inline in peer id inconsistent with Go implementation

There appears to be an inconsistency between Go and JS implementation of PeerId.

It seems the Go version will create PeerId by embedding the public key inline into the id when key length < 43 bytes.

The JS version doesn't seem to check key length when creating the id and always uses sha2-256 algorithm.

My key is 32 bytes so the Go implementation embeds it into the id. The same key, however, results in a different id on the browser. I have verified key bytes are identical on both sides.

Error: dialed to the wrong peer, Ids do not match
    at PeerId.createFromPubKey (crypto.js:80)
    at pubKey.hash (index.js:188)
    at Multihashing.Multihashing.digest (index.js:33)
    at index.js:15
    at run (setImmediate.js:40)
    at runIfPresent (setImmediate.js:69)
    at onGlobalMessage (setImmediate.js:109)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:496)

peer.go master

// IDFromPublicKey returns the Peer ID corresponding to pk
func IDFromPublicKey(pk ic.PubKey) (ID, error) {
	b, err := pk.Bytes()
	if err != nil {
		return "", err
	}
	var alg uint64 = mh.SHA2_256
	if len(b) <= MaxInlineKeyLength {  // <<<<< Go version uses sha2-256 for key sizes > 42 bytes
		alg = mh.ID
	}
	hash, _ := mh.Sum(b, alg, -1)
	return ID(hash), nil
}

index.js v0.2.2

...

    hash (callback) {
      ensure(callback)
      multihashing(this.bytes, 'sha2-256', callback)  // <<<<< JS version uses sha2-256 regardless of key length
    }
...

Can't unmarshal private key to create PeerId with libp2p-crypto 0.13.0, peer-id 0.11.0

If you import a private key from buffer, then try to create a peer id from that, the function blows up because an extra "null" is provided to the callback. The solution is just to avoid the extra "null" in the callback, but I couldn't figure out why that's there to begin with - possibly a previous version compatibility?

Used PeerId.createFromPrivKey
stack trace:
/Users/blakebyrnes/Projects/ulixee/toyhorse/node_modules/peer-id/src/index.js:225
cb(err, digest, privKey)
^

TypeError: cb is not a function
at privKey.public.hash (/Users/blakebyrnes/Projects/ulixee/toyhorse/node_modules/peer-id/src/index.js:225:9)
at Multihashing.Multihashing.digest (/Users/blakebyrnes/Projects/ulixee/toyhorse/node_modules/multihashing-async/src/index.js:33:5)
at setImmediate (/Users/blakebyrnes/Projects/ulixee/toyhorse/node_modules/multihashing-async/src/utils.js:8:7)
at Immediate. (/Users/blakebyrnes/Projects/ulixee/toyhorse/node_modules/async/internal/setImmediate.js:27:16)
at runCallback (timers.js:763:18)
at tryOnImmediate (timers.js:734:5)
at processImmediate (timers.js:716:5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.