2018-05-01 14:41:12 +01:00
|
|
|
# js-libp2p-crypto-secp256k1
|
2017-02-09 06:35:39 -05:00
|
|
|
|
2019-04-18 18:30:06 +01:00
|
|
|
[](http://protocol.ai)
|
|
|
|
[](http://libp2p.io/)
|
|
|
|
[](http://webchat.freenode.net/?channels=%23libp2p)
|
|
|
|
[](https://discuss.libp2p.io)
|
|
|
|
[](https://codecov.io/gh/libp2p/js-libp2p-crypto-secp256k1)
|
|
|
|
[](https://travis-ci.com/libp2p/js-libp2p-crypto-secp256k1)
|
|
|
|
[](https://david-dm.org/libp2p/js-libp2p-crypto-secp256k1)
|
2017-02-09 06:35:39 -05:00
|
|
|
[](https://github.com/feross/standard)
|
|
|
|

|
|
|
|

|
|
|
|
|
|
|
|
> Support for secp256k1 keys in js-libp2p-crypto
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
This repo contains a [js-libp2p-crypto](https://github.com/libp2p/js-libp2p-crypto)-compatible
|
|
|
|
implementation of cryptographic signature generation and verification using the
|
|
|
|
[secp256k1 elliptic curve](https://en.bitcoin.it/wiki/Secp256k1) popularized by Bitcoin and other
|
2017-02-09 06:35:39 -05:00
|
|
|
crypto currencies.
|
|
|
|
|
2018-05-01 14:41:12 +01:00
|
|
|
## Lead Captain
|
|
|
|
|
|
|
|
[Friedel Ziegelmayer](https://github.com/dignifiedquire/)
|
|
|
|
|
2017-02-09 06:35:39 -05:00
|
|
|
## Table of Contents
|
|
|
|
|
|
|
|
- [Install](#install)
|
|
|
|
- [Usage](#usage)
|
|
|
|
- [Example](#example)
|
|
|
|
- [API](#api)
|
2019-07-10 11:35:58 +01:00
|
|
|
- [`generateKeyPair([bits])`](#generatekeypairbits)
|
2017-02-09 06:35:39 -05:00
|
|
|
- [`unmarshalSecp256k1PublicKey(bytes)`](#unmarshalsecp256k1publickeybytes)
|
2019-07-10 11:35:58 +01:00
|
|
|
- [`unmarshalSecp256k1PrivateKey(bytes)`](#unmarshalsecp256k1privatekeybytes)
|
2017-02-09 06:35:39 -05:00
|
|
|
- [`Secp256k1PublicKey`](#secp256k1publickey)
|
2019-07-10 11:35:58 +01:00
|
|
|
- [`.verify(data, sig)`](#verifydata-sig)
|
2017-02-09 06:35:39 -05:00
|
|
|
- [`Secp256k1PrivateKey`](#secp256k1privatekey)
|
|
|
|
- [`.public`](#public)
|
2019-07-10 11:35:58 +01:00
|
|
|
- [`.sign(data)`](#signdata)
|
2017-02-09 06:35:39 -05:00
|
|
|
- [Contribute](#contribute)
|
|
|
|
- [License](#license)
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
|
|
|
```sh
|
|
|
|
npm install --save libp2p-crypto-secp256k1
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
This module is designed to work with [js-libp2p-crypto](https://github.com/libp2p/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](https://github.com/libp2p/js-libp2p-crypto#api)
|
|
|
|
`generateKeyPair`, `unmarshalPublicKey`, and `marshalPrivateKey`. The keys returned from those functions will be
|
|
|
|
instances of the `Secp256k1PublicKey` or `Secp256k1PrivateKey` classes provided by this module.
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
```js
|
|
|
|
const crypto = require('libp2p-crypto')
|
|
|
|
const msg = Buffer.from('Hello World')
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
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')
|
2017-02-09 06:35:39 -05:00
|
|
|
```
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
|
The functions below are the public API of this module.
|
2019-07-10 11:35:58 +01:00
|
|
|
For usage within `libp2p-crypto`, see the [`libp2p-crypto` API documentation](https://github.com/libp2p/js-libp2p-crypto#api).
|
2017-02-09 06:35:39 -05:00
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
### `generateKeyPair([bits])`
|
2017-02-09 06:35:39 -05:00
|
|
|
- `bits: Number` - Optional, included for compatibility with js-libp2p-crypto. Ignored if present; private keys will always be 256 bits.
|
2019-07-10 11:35:58 +01:00
|
|
|
|
|
|
|
Returns `Promise<Secp256k1PrivateKey>`
|
2017-02-09 06:35:39 -05:00
|
|
|
|
|
|
|
### `unmarshalSecp256k1PublicKey(bytes)`
|
|
|
|
- `bytes: Buffer`
|
|
|
|
|
|
|
|
Converts a serialized secp256k1 public key into an instance of `Secp256k1PublicKey` and returns it
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
### `unmarshalSecp256k1PrivateKey(bytes)`
|
2017-02-09 06:35:39 -05:00
|
|
|
- `bytes: Buffer`
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
Returns `Promise<Secp256k1PrivateKey>`
|
|
|
|
|
|
|
|
Converts a serialized secp256k1 private key into an instance of `Secp256k1PrivateKey`.
|
2017-02-09 06:35:39 -05:00
|
|
|
|
|
|
|
### `Secp256k1PublicKey`
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
#### `.verify(data, sig)`
|
2017-02-09 06:35:39 -05:00
|
|
|
- `data: Buffer`
|
|
|
|
- `sig: Buffer`
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
Returns `Promise<Boolean>`
|
|
|
|
|
|
|
|
Calculates the SHA-256 hash of `data`, and verifies the DER-encoded signature in `sig`.
|
2017-02-09 06:35:39 -05:00
|
|
|
|
|
|
|
### `Secp256k1PrivateKey`
|
|
|
|
|
|
|
|
#### `.public`
|
|
|
|
|
|
|
|
Accessor for the `Secp256k1PublicKey` associated with this private key.
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
#### `.sign(data)`
|
2017-02-09 06:35:39 -05:00
|
|
|
- `data: Buffer`
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
Returns `Promise<Buffer>`
|
|
|
|
|
|
|
|
Calculates the SHA-256 hash of `data` and signs it, resolves with the DER-encoded signature.
|
2017-02-09 06:35:39 -05:00
|
|
|
|
|
|
|
## Contribute
|
|
|
|
|
2019-07-10 11:35:58 +01:00
|
|
|
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-crypto-secp256k1/issues)!
|
2017-02-09 06:35:39 -05:00
|
|
|
|
|
|
|
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
|
|
|
|
|
|
|
[](https://github.com/ipfs/community/blob/master/contributing.md)
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
[MIT](LICENSE)
|