Compare commits

...

22 Commits

Author SHA1 Message Date
efc96106a4 chore: release version v0.13.10 2020-03-18 17:38:41 +01:00
16e1c74e9a chore: update contributors 2020-03-18 17:38:40 +01:00
c305c36b26 fix: add buffer (#120)
related to this https://github.com/ipfs/js-ipfs/issues/2924
2020-03-18 17:33:29 +01:00
7e985b127f chore(deps-dev): bump @types/mocha from 5.2.7 to 7.0.2 (#119)
Bumps [@types/mocha](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/mocha) from 5.2.7 to 7.0.2.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/mocha)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-04 14:23:01 +01:00
40d68cb512 chore(deps-dev): bump aegir from 20.6.1 to 21.2.0
Bumps [aegir](https://github.com/ipfs/aegir) from 20.6.1 to 21.2.0.
- [Release notes](https://github.com/ipfs/aegir/releases)
- [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ipfs/aegir/compare/v20.6.1...v21.2.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-20 08:14:43 -05:00
1d86506c64 chore: release version v0.13.9 2020-02-19 12:06:43 -05:00
9dc7ce5f5e chore: update contributors 2020-02-19 12:06:42 -05:00
f775e3509b chore: update index.d.ts (#115)
Tell Typescript that `isPeerId(id)` returns true if, and only if, `id` is of type `PeerId`.

See https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
2020-02-19 11:51:59 -05:00
00819ba4eb chore: update index.d.ts (#116)
This tells Typescript that `id` is immutable.
2020-02-19 09:17:30 -05:00
6870c8c4d7 chore: release version v0.13.8 2020-02-18 08:44:53 -05:00
71d3a100c9 chore: update contributors 2020-02-18 08:44:52 -05:00
f44645ef7e fix: remove use of assert module (#117)
The polyfill is big, we can simulate it by throwing an Error and it doesn't work under React Native.
2020-02-18 08:42:39 -05:00
a68855a5d2 chore: release version v0.13.7 2020-01-27 12:10:00 +01:00
d8c307a44f chore: update contributors 2020-01-27 12:09:59 +01:00
a5070aea6e feat: adds typescript types + type tests (#110)
* feat: adds typescript types + type tests

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>

* feat: updates to latest cids release

Signed-off-by: Carson Farmer <carson.farmer@gmail.com>
2020-01-27 12:07:52 +01:00
fc9698476f chore: release version v0.13.6 2019-12-18 10:43:10 +00:00
ffe04c12a0 chore: update contributors 2019-12-18 10:43:10 +00:00
65e0b746a5 fix: catch errors thrown by multihash decode (#109) 2019-12-18 10:17:59 +00:00
74cdb24544 chore: typo publick -> public (#108) 2019-11-28 11:39:55 +01:00
b5335cd4ee chore: release version v0.13.5 2019-11-12 15:06:13 +01:00
519052693d chore: update contributors 2019-11-12 15:06:13 +01:00
bbf0416f08 feat: deprecate isEqual in favor of equals (#107) 2019-11-12 15:00:11 +01:00
7 changed files with 375 additions and 30 deletions

View File

@ -1,3 +1,63 @@
<a name="0.13.10"></a>
## [0.13.10](https://github.com/libp2p/js-peer-id/compare/v0.13.9...v0.13.10) (2020-03-18)
### Bug Fixes
* add buffer ([#120](https://github.com/libp2p/js-peer-id/issues/120)) ([c305c36](https://github.com/libp2p/js-peer-id/commit/c305c36))
<a name="0.13.9"></a>
## [0.13.9](https://github.com/libp2p/js-peer-id/compare/v0.13.8...v0.13.9) (2020-02-19)
<a name="0.13.8"></a>
## [0.13.8](https://github.com/libp2p/js-peer-id/compare/v0.13.6...v0.13.8) (2020-02-18)
### Bug Fixes
* remove use of assert module ([#117](https://github.com/libp2p/js-peer-id/issues/117)) ([f44645e](https://github.com/libp2p/js-peer-id/commit/f44645e))
### Features
* adds typescript types + type tests ([#110](https://github.com/libp2p/js-peer-id/issues/110)) ([a5070ae](https://github.com/libp2p/js-peer-id/commit/a5070ae))
<a name="0.13.7"></a>
## [0.13.7](https://github.com/libp2p/js-peer-id/compare/v0.13.6...v0.13.7) (2020-01-27)
### Features
* adds typescript types + type tests ([#110](https://github.com/libp2p/js-peer-id/issues/110)) ([a5070ae](https://github.com/libp2p/js-peer-id/commit/a5070ae))
<a name="0.13.6"></a>
## [0.13.6](https://github.com/libp2p/js-peer-id/compare/v0.13.5...v0.13.6) (2019-12-18)
### Bug Fixes
* catch errors thrown by multihash decode ([#109](https://github.com/libp2p/js-peer-id/issues/109)) ([65e0b74](https://github.com/libp2p/js-peer-id/commit/65e0b74))
<a name="0.13.5"></a>
## [0.13.5](https://github.com/libp2p/js-peer-id/compare/v0.13.4...v0.13.5) (2019-11-12)
### Features
* deprecate isEqual in favor of equals ([#107](https://github.com/libp2p/js-peer-id/issues/107)) ([bbf0416](https://github.com/libp2p/js-peer-id/commit/bbf0416))
<a name="0.13.4"></a> <a name="0.13.4"></a>
## [0.13.4](https://github.com/libp2p/js-peer-id/compare/v0.13.3...v0.13.4) (2019-11-04) ## [0.13.4](https://github.com/libp2p/js-peer-id/compare/v0.13.3...v0.13.4) (2019-11-04)

View File

@ -17,6 +17,9 @@
## Table of Contents ## Table of Contents
- [peer-id](#peer-id)
- [Lead Maintainer](#lead-maintainer)
- [Table of Contents](#table-of-contents)
- [Description](#description) - [Description](#description)
- [Example](#example) - [Example](#example)
- [Installation](#installation) - [Installation](#installation)
@ -37,13 +40,18 @@
- [`createFromPubKey(pubKey)`](#createfrompubkeypubkey) - [`createFromPubKey(pubKey)`](#createfrompubkeypubkey)
- [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey) - [`createFromPrivKey(privKey)`](#createfromprivkeyprivkey)
- [`createFromJSON(obj)`](#createfromjsonobj) - [`createFromJSON(obj)`](#createfromjsonobj)
- [`createFromProtobuf(buf)`](#createfromprotobufbuf)
- [Export](#export) - [Export](#export)
- [`toHexString()`](#tohexstring)
- [`toBytes()`](#tobytes) - [`toBytes()`](#tobytes)
- [`toString()`](#tostring) - [`toString()`](#tostring)
- [`toB58String()`](#tob58string) - [`toB58String()`](#tob58string)
- [`toHexString()`](#tohexstring)
- [`toJSON()`](#tojson) - [`toJSON()`](#tojson)
- [`marshal(excludePrivateKey)`](#marshalexcludeprivatekey)
- [`marshalPubKey()`](#marshalpubkey)
- [`toPrint()`](#toprint) - [`toPrint()`](#toprint)
- [`equals(id)`](#equalsid)
- [`isEqual(id)`](#isequalid)
- [License](#license) - [License](#license)
# Description # Description
@ -119,7 +127,7 @@ const PeerId = require('peer-id')
### `new PeerId(id[, privKey, pubKey])` ### `new PeerId(id[, privKey, pubKey])`
- `id: Buffer` - The multihash of the publick key as `Buffer` - `id: Buffer` - The multihash of the public key as `Buffer`
- `privKey: RsaPrivateKey` - The private key - `privKey: RsaPrivateKey` - The private key
- `pubKey: RsaPublicKey` - The public key - `pubKey: RsaPublicKey` - The public key
@ -256,7 +264,14 @@ Returns the Peer ID as a printable string without the `Qm` prefix.
Example: `<peer.ID xxxxxx>` Example: `<peer.ID xxxxxx>`
### `equals(id)`
Returns `true` if the given PeerId is equal to the current instance.
- `id` can be a PeerId or a Buffer containing the id
### `isEqual(id)` ### `isEqual(id)`
**Deprecation Notice**: Use [`equals`](#equalsid), `isEqual` will be removed in 0.14.0.
- `id` can be a PeerId or a Buffer containing the id - `id` can be a PeerId or a Buffer containing the id

View File

@ -1,9 +1,10 @@
{ {
"name": "peer-id", "name": "peer-id",
"version": "0.13.4", "version": "0.13.10",
"description": "IPFS Peer Id implementation in Node.js", "description": "IPFS Peer Id implementation in Node.js",
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>", "leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
"main": "src/index.js", "main": "src/index.js",
"types": "src/index.d.ts",
"bin": "src/bin.js", "bin": "src/bin.js",
"scripts": { "scripts": {
"lint": "aegir lint", "lint": "aegir lint",
@ -11,6 +12,7 @@
"test": "aegir test", "test": "aegir test",
"test:node": "aegir test -t node", "test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker", "test:browser": "aegir test -t browser -t webworker",
"test:types": "npx tsc",
"release": "aegir release", "release": "aegir release",
"release-minor": "aegir release --type minor", "release-minor": "aegir release --type minor",
"release-major": "aegir release --type major", "release-major": "aegir release --type major",
@ -34,46 +36,50 @@
}, },
"homepage": "https://github.com/libp2p/js-peer-id", "homepage": "https://github.com/libp2p/js-peer-id",
"devDependencies": { "devDependencies": {
"aegir": "^20.0.0", "@types/chai": "^4.2.7",
"@types/dirty-chai": "^2.0.2",
"@types/mocha": "^7.0.2",
"aegir": "^21.2.0",
"bundlesize": "~0.18.0", "bundlesize": "~0.18.0",
"chai": "^4.2.0", "chai": "^4.2.0",
"dirty-chai": "^2.0.1" "dirty-chai": "^2.0.1"
}, },
"dependencies": { "dependencies": {
"cids": "~0.7.1", "buffer": "^5.5.0",
"cids": "^0.7.3",
"class-is": "^1.1.0", "class-is": "^1.1.0",
"libp2p-crypto": "~0.17.0", "libp2p-crypto": "~0.17.3",
"multihashes": "~0.4.15", "multihashes": "~0.4.15",
"protons": "^1.0.1" "protons": "^1.0.2"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/libp2p/js-peer-id.git" "url": "https://github.com/libp2p/js-peer-id.git"
}, },
"contributors": [ "contributors": [
"Arve Knudsen <arve.knudsen@gmail.com>",
"David Dias <daviddias.p@gmail.com>", "David Dias <daviddias.p@gmail.com>",
"David Dias <mail@daviddias.me>", "Vasco Santos <vasco.santos@moxy.studio>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>", "nginnever <ginneversource@gmail.com>",
"Henrique Dias <hacdias@gmail.com>", "dignifiedquire <dignifiedquire@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>", "Jacob Heun <jacobheun@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>", "Maciej Krüger <mkg20001@gmail.com>",
"Hugo Dias <hugomrdias@gmail.com>",
"Nate Foss <npfoss@gmail.com>",
"Robert Kiel <robert.kiel@validitylabs.org>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
"Arve Knudsen <arve.knudsen@gmail.com>",
"Carson Farmer <carson.farmer@gmail.com>",
"Christian Paul <info@jaller.de>",
"Henrique Dias <hacdias@gmail.com>",
"Marcin Rataj <lidel@lidel.org>", "Marcin Rataj <lidel@lidel.org>",
"Michael Garvin <gar+gh@danger.computer>", "Michael Garvin <gar+gh@danger.computer>",
"Nate Foss <npfoss@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"Prashanth Chandra <coolshanth94@gmail.com>", "Prashanth Chandra <coolshanth94@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>", "Richard Littauer <richard.littauer@gmail.com>",
"Richard Schneider <makaretu@gmail.com>", "Richard Schneider <makaretu@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"Topper Bowers <topper@quorumcontrol.com>", "Topper Bowers <topper@quorumcontrol.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Vasco Santos <vasco.santos@ua.pt>",
"Yahya <ya7yaz@gmail.com>", "Yahya <ya7yaz@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>", "Alex Potsides <alex@achingbrain.net>"
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
] ]
} }

188
src/index.d.ts vendored Normal file
View File

@ -0,0 +1,188 @@
import crypto, { PrivateKey, PublicKey, KeyType } from "libp2p-crypto";
import CID from 'cids'
declare namespace PeerId {
/**
* Options for PeerId creation.
*/
type CreateOptions = {
/**
* The number of bits to use.
*/
bits?: number;
/**
* The type of key to use.
*/
keyType?: KeyType;
};
/**
* PeerId JSON format.
*/
type JSONPeerId = {
/**
* String representation of PeerId.
*/
id: string;
/**
* Public key.
*/
pubKey?: string;
/**
* Private key.
*/
privKey: string;
};
/**
* Checks if a value is an instance of PeerId.
* @param id The value to check.
*/
function isPeerId(id: any): id is PeerId
/**
* Create a new PeerId.
* @param opts Options.
*/
function create(opts?: PeerId.CreateOptions): Promise<PeerId>;
/**
* Create PeerId from hex string.
* @param str The input hex string.
*/
function createFromHexString(str: string): PeerId;
/**
* Create PeerId from raw bytes.
* @param buf The raw bytes.
*/
function createFromBytes(buf: Buffer): PeerId;
/**
* Create PeerId from base58-encoded string.
* @param str The base58-encoded string.
*/
function createFromB58String(str: string): PeerId;
/**
* Create PeerId from CID.
* @param cid The CID.
*/
function createFromCID(cid: CID | Buffer | string | object): PeerId;
/**
* Create PeerId from public key.
* @param key Public key, as Buffer or base64-encoded string.
*/
function createFromPubKey(key: Buffer | string): Promise<PeerId>;
/**
* Create PeerId from private key.
* @param key Private key, as Buffer or base64-encoded string.
*/
function createFromPrivKey(key: Buffer | string): Promise<PeerId>;
/**
* Create PeerId from PeerId JSON formatted object.
* @see {@link PeerId#toJSON}
* @param json PeerId in JSON format.
*/
function createFromJSON(json: JSONPeerId): Promise<PeerId>;
/**
* Create PeerId from Protobuf bytes.
* @param buf Protobuf bytes, as Buffer or hex-encoded string.
*/
function createFromProtobuf(buf: Buffer | string): Promise<PeerId>;
}
/**
* PeerId is an object representation of a peer identifier.
*/
declare class PeerId {
constructor(id: Buffer | string, privKey?: PrivateKey, pubKey?: PublicKey);
/**
* Raw id.
*/
readonly id: Buffer;
/**
* Private key.
*/
privKey: PrivateKey;
/**
* Public key.
*/
pubKey: PublicKey;
/**
* Return the protobuf version of the public key, matching go ipfs formatting.
*/
marshalPubKey(): Buffer;
/**
* Return the protobuf version of the private key, matching go ipfs formatting.
*/
marshalPrivKey(): Buffer;
/**
* Return the protobuf version of the peer-id.
* @param excludePriv Whether to exclude the private key information from the output.
*/
marshal(excludePriv?: boolean): Buffer;
/**
* String representation.
*/
toPrint(): string;
/**
* Return the jsonified version of the key.
* Matches the formatting of go-ipfs for its config file.
* @see {@link PeerId.createFromJSON}
*/
toJSON(): PeerId.JSONPeerId;
/**
* Encode to hex.
*/
toHexString(): string;
/**
* Return raw id bytes.
*/
toBytes(): Buffer;
/**
* Encode to base58 string.
*/
toB58String(): string;
/**
* Return self-describing string representation.
* Uses default format from RFC 0001: https://github.com/libp2p/specs/pull/209
*/
toString(): string;
/**
* Checks the equality of `this` peer against a given PeerId.
* @param id The other PeerId.
*/
equals(id: PeerId | Buffer): boolean;
/**
* Checks the equality of `this` peer against a given PeerId.
* @deprecated Use {.equals}
* @param id The other PeerId.
*/
isEqual(id: PeerId | Buffer): boolean;
/**
* Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/
isValid(): boolean;
}
export = PeerId;

View File

@ -4,19 +4,21 @@
'use strict' 'use strict'
const { Buffer } = require('buffer')
const mh = require('multihashes') const mh = require('multihashes')
const CID = require('cids') const CID = require('cids')
const cryptoKeys = require('libp2p-crypto/src/keys') const cryptoKeys = require('libp2p-crypto/src/keys')
const assert = require('assert')
const withIs = require('class-is') const withIs = require('class-is')
const { PeerIdProto } = require('./proto') const { PeerIdProto } = require('./proto')
class PeerId { class PeerId {
constructor (id, privKey, pubKey) { constructor (id, privKey, pubKey) {
assert(Buffer.isBuffer(id), 'invalid id provided') if (!Buffer.isBuffer(id)) {
throw new Error('invalid id provided')
}
if (privKey && pubKey) { if (privKey && pubKey && !privKey.public.bytes.equals(pubKey.bytes)) {
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments') throw new Error('inconsistent arguments')
} }
this._id = id this._id = id
@ -50,12 +52,17 @@ class PeerId {
return this._privKey.public return this._privKey.public
} }
try {
const decoded = mh.decode(this.id) const decoded = mh.decode(this.id)
if (decoded.name === 'identity') { if (decoded.name === 'identity') {
this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest) this._pubKey = cryptoKeys.unmarshalPublicKey(decoded.digest)
return this._pubKey
} }
} catch (_) {
// Ignore, there is no valid public key
}
return this._pubKey
} }
set pubKey (pubKey) { set pubKey (pubKey) {
@ -133,7 +140,12 @@ class PeerId {
return this._idCIDString return this._idCIDString
} }
isEqual (id) { /**
* Checks the equality of `this` peer against a given PeerId.
* @param {Buffer|PeerId} id
* @returns {boolean}
*/
equals (id) {
if (Buffer.isBuffer(id)) { if (Buffer.isBuffer(id)) {
return this.id.equals(id) return this.id.equals(id)
} else if (id.id) { } else if (id.id) {
@ -143,6 +155,16 @@ class PeerId {
} }
} }
/**
* Checks the equality of `this` peer against a given PeerId.
* @deprecated Use `.equals`
* @param {Buffer|PeerId} id
* @returns {boolean}
*/
isEqual (id) {
return this.equals(id)
}
/* /*
* Check if this PeerId instance is valid (privKey -> pubKey -> Id) * Check if this PeerId instance is valid (privKey -> pubKey -> Id)
*/ */

View File

@ -2,6 +2,7 @@
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'
const { Buffer } = require('buffer')
const chai = require('chai') const chai = require('chai')
const dirtyChai = require('dirty-chai') const dirtyChai = require('dirty-chai')
chai.use(dirtyChai) chai.use(dirtyChai)
@ -231,6 +232,18 @@ describe('PeerId', () => {
expect(ids[0].isEqual(ids[1].id)).to.equal(false) expect(ids[0].isEqual(ids[1].id)).to.equal(false)
}) })
it('equals', async () => {
const ids = await Promise.all([
PeerId.create(testOpts),
PeerId.create(testOpts)
])
expect(ids[0].equals(ids[0])).to.equal(true)
expect(ids[0].equals(ids[1])).to.equal(false)
expect(ids[0].equals(ids[0].id)).to.equal(true)
expect(ids[0].equals(ids[1].id)).to.equal(false)
})
describe('fromJSON', () => { describe('fromJSON', () => {
it('full node', async () => { it('full node', async () => {
const id = await PeerId.create(testOpts) const id = await PeerId.create(testOpts)
@ -259,24 +272,28 @@ describe('PeerId', () => {
it('set privKey (valid)', async () => { it('set privKey (valid)', async () => {
const peerId = await PeerId.create(testOpts) const peerId = await PeerId.create(testOpts)
// @ts-ignore
peerId.privKey = peerId._privKey peerId.privKey = peerId._privKey
expect(peerId.isValid()).to.equal(true) expect(peerId.isValid()).to.equal(true)
}) })
it('set pubKey (valid)', async () => { it('set pubKey (valid)', async () => {
const peerId = await PeerId.create(testOpts) const peerId = await PeerId.create(testOpts)
// @ts-ignore
peerId.pubKey = peerId._pubKey peerId.pubKey = peerId._pubKey
expect(peerId.isValid()).to.equal(true) expect(peerId.isValid()).to.equal(true)
}) })
it('set privKey (invalid)', async () => { it('set privKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts) const peerId = await PeerId.create(testOpts)
// @ts-ignore
peerId.privKey = Buffer.from('bufff') peerId.privKey = Buffer.from('bufff')
expect(peerId.isValid()).to.equal(false) expect(peerId.isValid()).to.equal(false)
}) })
it('set pubKey (invalid)', async () => { it('set pubKey (invalid)', async () => {
const peerId = await PeerId.create(testOpts) const peerId = await PeerId.create(testOpts)
// @ts-ignore
peerId.pubKey = Buffer.from('bufff') peerId.pubKey = Buffer.from('bufff')
expect(peerId.isValid()).to.equal(false) expect(peerId.isValid()).to.equal(false)
}) })

37
tsconfig.json Normal file
View File

@ -0,0 +1,37 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"target": "ES5",
"noImplicitAny": false,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowJs": true,
"checkJs": true,
"baseUrl": ".",
"paths": {
"peer-id": [
"./src",
"../src",
]
},
"types": [
"node",
"mocha",
"chai"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"./src/index.d.ts",
],
"include": [
"./test/**/*.spec.js"
]
}