mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-07-04 23:41:48 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
7336b6ad80 | |||
461d2ea287 | |||
7c100f845a | |||
c3f50a2dfd | |||
907ed1571d | |||
2aad1dce7b | |||
58f1933980 | |||
ab5504686a | |||
4beb1f8888 |
16
.aegir.js
Normal file
16
.aegir.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
webpack: {
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'node-forge': path.resolve(
|
||||||
|
path.dirname(require.resolve('libp2p-crypto')),
|
||||||
|
'../vendor/forge.bundle.js'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ language: node_js
|
|||||||
node_js:
|
node_js:
|
||||||
- 4
|
- 4
|
||||||
- 5
|
- 5
|
||||||
|
- stable
|
||||||
|
|
||||||
# Make sure we have new NPM.
|
# Make sure we have new NPM.
|
||||||
before_install:
|
before_install:
|
||||||
|
74
README.md
74
README.md
@ -29,8 +29,8 @@ var bs58 = require('bs58')
|
|||||||
var id = PeerId.create({ bits: 32 })
|
var id = PeerId.create({ bits: 32 })
|
||||||
|
|
||||||
console.log('id ', id.toB58String())
|
console.log('id ', id.toB58String())
|
||||||
console.log('priv key ', bs58.encode(id.privKey))
|
console.log('priv key ', bs58.encode(id.privKey.bytes))
|
||||||
console.log('pub key ', bs58.encode(id.pubKey))
|
console.log('pub key ', bs58.encode(id.pubKey.bytes))
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -51,7 +51,7 @@ pub key 2BeBZVKJ9RQs4i4LbGv4ReEeuBA5dck2Gje3wt67e44XuyyPq5jE
|
|||||||
|
|
||||||
## Node.js
|
## Node.js
|
||||||
|
|
||||||
```JavaScript
|
```js
|
||||||
var PeerId = require('peer-id')
|
var PeerId = require('peer-id')
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ transpiled version with the right shims added. This means that you can require
|
|||||||
it and use with your favourite bundler without having to adjust asset management
|
it and use with your favourite bundler without having to adjust asset management
|
||||||
process.
|
process.
|
||||||
|
|
||||||
```JavaScript
|
```js
|
||||||
var PeerId = require('peer-id')
|
var PeerId = require('peer-id')
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -85,50 +85,51 @@ const PeerId = require('peer-id')
|
|||||||
|
|
||||||
## Create
|
## Create
|
||||||
|
|
||||||
### PeerId.create()
|
### `new PeerId(id[, privKey, pubKey])`
|
||||||
|
|
||||||
Generates a new Peer ID, complete with public/private keypair. A Peer ID has the
|
- `id: Buffer` - The multihash of the publick key as `Buffer`
|
||||||
following properties:
|
- `privKey: RsaPrivateKey` - The private key
|
||||||
|
- `pubKey: RsaPublicKey` - The public key
|
||||||
|
|
||||||
- `pubKey` - Buffer containing the public key bytes
|
The key format is detailed in [libp2p-crypto](https://github.com/ipfs/js-libp2p-crypto).
|
||||||
- `privKey` - Buffer containing the private key bytes
|
|
||||||
- `id` - Buffer containing the multihash bytes
|
### `create([opts])`
|
||||||
|
|
||||||
|
Generates a new Peer ID, complete with public/private keypair.
|
||||||
|
|
||||||
|
- `opts: Object`: Default: `{bits: 2048}`
|
||||||
|
|
||||||
## Import
|
## Import
|
||||||
|
|
||||||
### PeerId.createFromHexString(str)
|
### `createFromHexString(str)`
|
||||||
|
|
||||||
Creates a Peer ID from hex string representing the key's multihash.
|
Creates a Peer ID from hex string representing the key's multihash.
|
||||||
|
|
||||||
### PeerId.createFromBytes(buf)
|
### `createFromBytes(buf)`
|
||||||
|
|
||||||
Creates a Peer ID from a buffer representing the key's multihash.
|
Creates a Peer ID from a buffer representing the key's multihash.
|
||||||
|
|
||||||
### PeerId.createFromB58String(str)
|
### `createFromB58String(str)`
|
||||||
Creates a Peer ID from a Base58 string representing the key's multihash.
|
Creates a Peer ID from a Base58 string representing the key's multihash.
|
||||||
|
|
||||||
### PeerId.createFromPubKey(pubKey)
|
### `createFromPubKey(pubKey)`
|
||||||
|
|
||||||
Creates a Peer ID from a buffer containing a public key.
|
Creates a Peer ID from a buffer containing a public key.
|
||||||
|
|
||||||
### PeerId.createFromPrivKey(privKey)
|
### `createFromPrivKey(privKey)`
|
||||||
|
|
||||||
Creates a Peer ID from a buffer containing a private key.
|
Creates a Peer ID from a buffer containing a private key.
|
||||||
|
|
||||||
|
### `createFromJSON(obj)`
|
||||||
|
|
||||||
|
- `obj.id: String` - The multihash encoded in `base58`
|
||||||
|
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
|
||||||
|
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
|
||||||
|
|
||||||
|
|
||||||
## Export
|
## Export
|
||||||
|
|
||||||
### id.toPrint()
|
### `toHexString()`
|
||||||
|
|
||||||
Returns an object with the ID's properties in hex format:
|
|
||||||
```js
|
|
||||||
{
|
|
||||||
id: 'QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi',
|
|
||||||
privKey: '080012a609308204a20201000282010100a608889914da08959d3a3db0734cee812c96...',
|
|
||||||
pubKey: '080012a60230820122300d06092a864886f70d01010105000382010f003082010a0282010...'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### id.toHexString()
|
|
||||||
|
|
||||||
Returns the Peer ID's `id` as a hex string.
|
Returns the Peer ID's `id` as a hex string.
|
||||||
|
|
||||||
@ -136,16 +137,15 @@ Returns the Peer ID's `id` as a hex string.
|
|||||||
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
|
1220d6243998f2fc56343ad7ed0342ab7886a4eb18d736f1b67d44b37fcc81e0f39f
|
||||||
```
|
```
|
||||||
|
|
||||||
### id.toBytes()
|
### `toBytes()`
|
||||||
|
|
||||||
Returns the Peer ID's `id` as a buffer.
|
Returns the Peer ID's `id` as a buffer.
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>
|
<Buffer 12 20 d6 24 39 98 f2 fc 56 34 3a d7 ed 03 42 ab 78 86 a4 eb 18 d7 36 f1 b6 7d 44 b3 7f cc 81 e0 f3 9f>
|
||||||
```
|
```
|
||||||
|
|
||||||
### id.toB58String()
|
### `toB58String()`
|
||||||
|
|
||||||
Returns the Peer ID's `id` as a base58 string.
|
Returns the Peer ID's `id` as a base58 string.
|
||||||
|
|
||||||
@ -153,6 +153,20 @@ Returns the Peer ID's `id` as a base58 string.
|
|||||||
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `toJSON()`
|
||||||
|
|
||||||
|
Returns an `obj` of the form
|
||||||
|
|
||||||
|
- `obj.id: String` - The multihash encoded in `base58`
|
||||||
|
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
|
||||||
|
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'
|
||||||
|
|
||||||
|
|
||||||
|
### `toPrint()`
|
||||||
|
|
||||||
|
Alias for `.toJSON()`.
|
||||||
|
|
||||||
|
|
||||||
# License
|
# License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
24
package.json
24
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.6.7",
|
"version": "0.7.0",
|
||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"bin": "src/bin.js",
|
"bin": "src/bin.js",
|
||||||
@ -12,6 +12,8 @@
|
|||||||
"test:node": "aegir-test node",
|
"test:node": "aegir-test node",
|
||||||
"test:browser": "aegir-test browser",
|
"test:browser": "aegir-test browser",
|
||||||
"release": "aegir-release",
|
"release": "aegir-release",
|
||||||
|
"release-minor": "aegir-release --type minor",
|
||||||
|
"release-major": "aegir-release --type major",
|
||||||
"coverage": "aegir-coverage",
|
"coverage": "aegir-coverage",
|
||||||
"coverage-publish": "aegir-coverage publish"
|
"coverage-publish": "aegir-coverage publish"
|
||||||
},
|
},
|
||||||
@ -32,30 +34,18 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/diasdavid/js-peer-id",
|
"homepage": "https://github.com/diasdavid/js-peer-id",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^3.0.4",
|
"aegir": "^3.1.1",
|
||||||
"buffer-loader": "0.0.1",
|
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"pre-commit": "^1.1.2"
|
"pre-commit": "^1.1.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^3.0.0",
|
"libp2p-crypto": "^0.5.0",
|
||||||
"multihashing": "^0.2.0",
|
"multihashes": "^0.2.2"
|
||||||
"node-forge": "^0.6.38",
|
|
||||||
"protocol-buffers": "^3.1.4"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/diasdavid/js-peer-id.git"
|
"url": "https://github.com/diasdavid/js-peer-id.git"
|
||||||
},
|
},
|
||||||
"aegir": {
|
|
||||||
"webpack": {
|
|
||||||
"resolve": {
|
|
||||||
"alias": {
|
|
||||||
"node-forge": "../vendor/forge.bundle.js"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"David Dias <mail@daviddias.me>",
|
"David Dias <mail@daviddias.me>",
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
enum KeyType {
|
|
||||||
RSA = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
message PublicKey {
|
|
||||||
required KeyType Type = 1;
|
|
||||||
required bytes Data = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message PrivateKey {
|
|
||||||
required KeyType Type = 1;
|
|
||||||
required bytes Data = 2;
|
|
||||||
}
|
|
230
src/index.js
230
src/index.js
@ -4,137 +4,101 @@
|
|||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const fs = require('fs')
|
const mh = require('multihashes')
|
||||||
const multihashing = require('multihashing')
|
const crypto = require('libp2p-crypto')
|
||||||
const base58 = require('bs58')
|
const assert = require('assert')
|
||||||
const forge = require('node-forge')
|
|
||||||
const protobuf = require('protocol-buffers')
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
|
class PeerId {
|
||||||
|
constructor (id, privKey, pubKey) {
|
||||||
|
assert(Buffer.isBuffer(id), 'invalid id provided')
|
||||||
|
|
||||||
exports = module.exports = PeerId
|
if (pubKey) {
|
||||||
|
assert(id.equals(pubKey.hash()), 'inconsistent arguments')
|
||||||
|
}
|
||||||
|
|
||||||
exports.Buffer = Buffer
|
if (privKey) {
|
||||||
|
assert(id.equals(privKey.public.hash()), 'inconsistent arguments')
|
||||||
|
}
|
||||||
|
|
||||||
function PeerId (id, privKey, pubKey) {
|
if (privKey && pubKey) {
|
||||||
const self = this
|
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
||||||
|
}
|
||||||
|
|
||||||
if (!(self instanceof PeerId)) {
|
this.id = id
|
||||||
throw new Error('Id must be called with new')
|
this.privKey = privKey
|
||||||
|
this._pubKey = pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
self.privKey = privKey
|
get pubKey () {
|
||||||
self.pubKey = pubKey
|
if (this._pubKey) {
|
||||||
self.id = id // multihash - sha256 - buffer
|
return this._pubKey
|
||||||
|
}
|
||||||
|
|
||||||
// pretty print
|
if (this.privKey) {
|
||||||
self.toPrint = function () {
|
return this.privKey.public
|
||||||
return {
|
|
||||||
id: self.toB58String(),
|
|
||||||
privKey: privKey.toString('hex'),
|
|
||||||
pubKey: pubKey.toString('hex')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.toJSON = function () {
|
// Return the protobuf version of the public key,
|
||||||
|
// matching go ipfs formatting
|
||||||
|
marshalPubKey () {
|
||||||
|
if (this.pubKey) {
|
||||||
|
return crypto.marshalPublicKey(this.pubKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the protobuf version of the private key,
|
||||||
|
// matching go ipfs formatting
|
||||||
|
marshalPrivKey () {
|
||||||
|
if (this.privKey) {
|
||||||
|
return crypto.marshalPrivateKey(this.privKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pretty print
|
||||||
|
toPrint () {
|
||||||
|
return this.toJSON()
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the jsonified version of the key, matching the formatting
|
||||||
|
// of go-ipfs for its config file
|
||||||
|
toJSON () {
|
||||||
return {
|
return {
|
||||||
id: self.id.toString('hex'),
|
id: mh.toB58String(this.id),
|
||||||
privKey: self.privKey.toString('hex'),
|
privKey: toB64Opt(this.marshalPrivKey()),
|
||||||
pubKey: self.pubKey.toString('hex')
|
pubKey: toB64Opt(this.marshalPubKey())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode/decode functions
|
// encode/decode functions
|
||||||
self.toHexString = function () {
|
toHexString () {
|
||||||
return self.id.toString('hex')
|
return mh.toHexString(this.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.toBytes = function () {
|
toBytes () {
|
||||||
return self.id
|
return this.id
|
||||||
}
|
}
|
||||||
|
|
||||||
self.toB58String = function () {
|
toB58String () {
|
||||||
return base58.encode(self.id)
|
return mh.toB58String(this.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// unwrap the private key protobuf
|
exports = module.exports = PeerId
|
||||||
function keyUnmarshal (key) {
|
exports.Buffer = Buffer
|
||||||
return pbCrypto.PrivateKey.decode(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a public key protobuf to be base64 string stored in config
|
|
||||||
function keyMarshal (data, type) {
|
|
||||||
const RSA = 0
|
|
||||||
|
|
||||||
let epb
|
|
||||||
if (type === 'Public') {
|
|
||||||
epb = pbCrypto.PublicKey.encode({
|
|
||||||
Type: RSA,
|
|
||||||
Data: data
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'Private') {
|
|
||||||
epb = pbCrypto.PrivateKey.encode({
|
|
||||||
Type: RSA,
|
|
||||||
Data: data
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return epb
|
|
||||||
}
|
|
||||||
|
|
||||||
// this returns a base64 encoded protobuf of the public key
|
|
||||||
function formatKey (key, type) {
|
|
||||||
// create der buffer of public key asn.1 object
|
|
||||||
const der = forge.asn1.toDer(key)
|
|
||||||
|
|
||||||
// create forge buffer of der public key buffer
|
|
||||||
const fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
|
||||||
|
|
||||||
// convert forge buffer to node buffer public key
|
|
||||||
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
|
||||||
|
|
||||||
// protobuf the new DER bytes to the PublicKey Data: field
|
|
||||||
const marsheledKey = keyMarshal(nDerBuf, type)
|
|
||||||
|
|
||||||
// encode the protobuf public key to base64 string
|
|
||||||
const b64 = marsheledKey.toString('base64')
|
|
||||||
return b64
|
|
||||||
}
|
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
exports.create = function (opts) {
|
exports.create = function (opts) {
|
||||||
opts = opts || {}
|
opts = opts || {}
|
||||||
opts.bits = opts.bits || 2048
|
opts.bits = opts.bits || 2048
|
||||||
|
|
||||||
// generate keys
|
const privKey = crypto.generateKeyPair('RSA', opts.bits)
|
||||||
const pair = forge.rsa.generateKeyPair({
|
|
||||||
bits: opts.bits,
|
|
||||||
e: 0x10001
|
|
||||||
})
|
|
||||||
|
|
||||||
// return the RSA public/private key to asn1 object
|
return new PeerId(privKey.public.hash(), privKey)
|
||||||
const asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
|
||||||
const asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
|
||||||
|
|
||||||
// format the keys to protobuf base64 encoded string
|
|
||||||
const protoPublic64 = formatKey(asnPub, 'Public')
|
|
||||||
const protoPrivate64 = formatKey(asnPriv, 'Private')
|
|
||||||
|
|
||||||
// store the keys as a buffer
|
|
||||||
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
|
|
||||||
const bufProtoPriv64 = new Buffer(protoPrivate64, 'base64')
|
|
||||||
|
|
||||||
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
|
||||||
|
|
||||||
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromHexString = function (str) {
|
exports.createFromHexString = function (str) {
|
||||||
return new PeerId(new Buffer(str, 'hex'))
|
return new PeerId(mh.fromHexString(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromBytes = function (buf) {
|
exports.createFromBytes = function (buf) {
|
||||||
@ -142,51 +106,47 @@ exports.createFromBytes = function (buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromB58String = function (str) {
|
exports.createFromB58String = function (str) {
|
||||||
return new PeerId(new Buffer(base58.decode(str)))
|
return new PeerId(mh.fromB58String(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public Key input will be a buffer
|
// Public Key input will be a buffer
|
||||||
exports.createFromPubKey = function (pubKey) {
|
exports.createFromPubKey = function (key) {
|
||||||
const buf = new Buffer(pubKey, 'base64')
|
let buf = key
|
||||||
const mhId = multihashing(buf, 'sha2-256')
|
if (typeof buf === 'string') {
|
||||||
return new PeerId(mhId, null, pubKey)
|
buf = new Buffer(key, 'base64')
|
||||||
|
}
|
||||||
|
const pubKey = crypto.unmarshalPublicKey(buf)
|
||||||
|
return new PeerId(pubKey.hash(), null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private key input will be a string
|
// Private key input will be a string
|
||||||
exports.createFromPrivKey = function (privKey) {
|
exports.createFromPrivKey = function (key) {
|
||||||
// create a buffer from the base64 encoded string
|
let buf = key
|
||||||
const buf = new Buffer(privKey, 'base64')
|
if (typeof buf === 'string') {
|
||||||
|
buf = new Buffer(key, 'base64')
|
||||||
|
}
|
||||||
|
|
||||||
// get the private key data from the protobuf
|
const privKey = crypto.unmarshalPrivateKey(buf)
|
||||||
const mpk = keyUnmarshal(buf)
|
return new PeerId(privKey.public.hash(), privKey)
|
||||||
|
|
||||||
// create a forge buffer
|
|
||||||
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
|
||||||
|
|
||||||
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
|
||||||
const asnPriv = forge.asn1.fromDer(fbuf)
|
|
||||||
|
|
||||||
// get the RSA privatekey data from the asn1 object
|
|
||||||
const privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
|
||||||
|
|
||||||
// set the RSA public key to the modulus and exponent of the private key
|
|
||||||
const publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
|
||||||
|
|
||||||
// return the RSA public key to asn1 object
|
|
||||||
const asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
|
||||||
|
|
||||||
// format the public key
|
|
||||||
const protoPublic64 = formatKey(asnPub, 'Public')
|
|
||||||
|
|
||||||
// buffer the public key for consistency before storing
|
|
||||||
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
|
|
||||||
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
|
||||||
return new PeerId(mhId, privKey, bufProtoPub64)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromJSON = function (obj) {
|
exports.createFromJSON = function (obj) {
|
||||||
return new PeerId(
|
let priv
|
||||||
new Buffer(obj.id, 'hex'),
|
let pub
|
||||||
new Buffer(obj.privKey, 'hex'),
|
|
||||||
new Buffer(obj.pubKey, 'hex'))
|
if (obj.privKey) {
|
||||||
|
priv = crypto.unmarshalPrivateKey(new Buffer(obj.privKey, 'base64'))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj.pubKey) {
|
||||||
|
pub = crypto.unmarshalPublicKey(new Buffer(obj.pubKey, 'base64'))
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PeerId(mh.fromB58String(obj.id), priv, pub)
|
||||||
|
}
|
||||||
|
|
||||||
|
function toB64Opt (val) {
|
||||||
|
if (val) {
|
||||||
|
return val.toString('base64')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
test/fixtures/go-private-key.js
vendored
Normal file
6
test/fixtures/go-private-key.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
id: 'QmRLoXS3E73psYaUsma1VSbboTa2J8Z9kso1tpiGLk9WQ4',
|
||||||
|
privKey: 'CAASpwkwggSjAgEAAoIBAQDWBEbO8kc6a5kEks09CKPQargY3p0DCmCczoCT52/RYFqxvH9dI+s+u4ZAvF9aLWOBvFomL7jHZODPxKDrbiNCmyEbViNgZYK+PNbwh0V3ZGbB27X3q8yZtLvYA8dhcNkz/2SHBarSoC4QLA5MXUuSWtVaYMY3MzMnzBF57Jc9Ase7NvHOIUI90M7aN5izP7hxPXpZ+shiN+TyjM8mFxYONG7ZSsY3IxUhtrU5MRzFX+tp1o/gb/aa51mHf7AL3N02j5ABiYbCK97Rbwr03hsBcwgMxoDPJmP3WZ+D5yyPcOIIF1Vd7+4/f7FQJnIw3xr9/jvaFbPyDCVbBOhr9oyxAgMBAAECggEALlrgx2Q8v0+c5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vBOpTicA/wXPX9Q5O/yjegqhqLT/LXAm9ziFzy5b9/9SzXPukKebXXbvc0FOmcsrcxtijlPyUzf9fKM1ShiwqqsgM9eNyZ9GWUJw2GFATCWW7pl7rtnWk='
|
||||||
|
}
|
7
test/fixtures/sample-id.js
vendored
Normal file
7
test/fixtures/sample-id.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
id: '122019318b6e5e0cf93a2314bf01269a2cc23cd3dcd452d742cdb9379d8646f6e4a9',
|
||||||
|
privKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ1bovzGoCwx3iGIatQKFOxyVCyO1PsJYT7JO+kZbhBWRRE+L7l+ppPER9bdLFxs1t5CrKc078h+wuUr05S1P1JjXk68pk3+kQKBgQDeK8AR11373Mzib6uzpjGzgNRMzdYNuExWjxyxAzz53NAR7zrP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l7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE='
|
||||||
|
}
|
@ -2,103 +2,169 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
const crypto = require('libp2p-crypto')
|
||||||
|
const mh = require('multihashes')
|
||||||
|
|
||||||
const PeerId = require('../src')
|
const PeerId = require('../src')
|
||||||
|
|
||||||
const testId = {
|
const testId = require('./fixtures/sample-id')
|
||||||
id: '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d',
|
const testIdHex = testId.id
|
||||||
privKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8Epdx+7F5p80XAFLDUnELzOzKftvWGZmWfSeDnslwVONkL/1VAzwKy7Ce6hk4SxRE7l2NE2OklSHOzCGU1f78ZzVYKSnS5Ag9YrGjOAmTOXDbKNKN/qIorAQ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3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAE='
|
const testIdB58String = mh.toB58String(testIdBytes)
|
||||||
}
|
|
||||||
|
|
||||||
const testIdHex = '1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d'
|
const goId = require('./fixtures/go-private-key')
|
||||||
|
|
||||||
const testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440a69366f1bc31deb5c72d', 'hex')
|
describe('PeerId', () => {
|
||||||
|
it('create an id without \'new\'', () => {
|
||||||
const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
|
||||||
|
|
||||||
describe('id', function (done) {
|
|
||||||
this.timeout(30000)
|
|
||||||
|
|
||||||
it('create an id without \'new\'', (done) => {
|
|
||||||
expect(PeerId).to.throw(Error)
|
expect(PeerId).to.throw(Error)
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('create a new id', (done) => {
|
it('create a new id', () => {
|
||||||
const id = PeerId.create()
|
const id = PeerId.create()
|
||||||
expect(id.toB58String().length).to.equal(46)
|
expect(id.toB58String().length).to.equal(46)
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recreate an Id from Hex string', (done) => {
|
it('recreate an Id from Hex string', () => {
|
||||||
const id = PeerId.createFromHexString(testIdHex)
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(testIdBytes).to.deep.equal(id.id)
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate an Id from a Buffer', (done) => {
|
it('Recreate an Id from a Buffer', () => {
|
||||||
const id = PeerId.createFromBytes(testIdBytes)
|
const id = PeerId.createFromBytes(testIdBytes)
|
||||||
expect(testId.id).to.equal(id.toHexString())
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate a B58 String', (done) => {
|
it('Recreate a B58 String', () => {
|
||||||
const id = PeerId.createFromB58String(testIdB58String)
|
const id = PeerId.createFromB58String(testIdB58String)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Public Key', (done) => {
|
it('Recreate from a Public Key', () => {
|
||||||
const id = PeerId.createFromPubKey(testId.pubKey)
|
const id = PeerId.createFromPubKey(testId.pubKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Recreate from a Private Key', (done) => {
|
it('Recreate from a Private Key', () => {
|
||||||
const id = PeerId.createFromPrivKey(testId.privKey)
|
const id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
|
||||||
|
const id2 = PeerId.createFromPrivKey(new Buffer(testId.privKey, 'base64'))
|
||||||
|
expect(testIdB58String).to.equal(id2.toB58String())
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Compare generated ID with one created from PubKey', (done) => {
|
it('Compare generated ID with one created from PubKey', () => {
|
||||||
const id1 = PeerId.create()
|
const id1 = PeerId.create()
|
||||||
const id2 = PeerId.createFromPubKey(id1.pubKey)
|
const id2 = PeerId.createFromPubKey(id1.marshalPubKey())
|
||||||
expect(id2.id).to.deep.equal(id1.id)
|
expect(id1.id).to.be.eql(id2.id)
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Non-default # of bits', (done) => {
|
it('Non-default # of bits', () => {
|
||||||
const shortId = PeerId.create({ bits: 128 })
|
const shortId = PeerId.create({ bits: 128 })
|
||||||
const longId = PeerId.create({ bits: 256 })
|
const longId = PeerId.create({ bits: 256 })
|
||||||
expect(shortId.privKey.length).is.below(longId.privKey.length)
|
expect(shortId.privKey.bytes.length).is.below(longId.privKey.bytes.length)
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Pretty printing', (done) => {
|
it('Pretty printing', () => {
|
||||||
const id = PeerId.createFromPrivKey(testId.privKey)
|
const id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
var out = id.toPrint()
|
const out = id.toPrint()
|
||||||
var expected = {
|
|
||||||
id: 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A',
|
expect(out.id).to.equal(testIdB58String)
|
||||||
privKey: 'CAASpgkwggSiAgEAAoIBAQC2SKo/HMFZeBml1AF3XijzrxrfQXdJzjePBZAbdxqKR1Mc6juRHXij6HXYPjlAk01BhF1S3Ll4Lwi0cAHhggf457sMg55UWyeGKeUv0ucgvCpBwlR5cQ020i0MgzjPWOLWq1rtvSbNcAi2ZEVn6+Q2EcHo3wUvWRtLeKz+DZSZfw2PEDC+DGPJPl7f8g7zl56YymmmzH9liZLNrzg/qidokUv5u1pdGrcpLuPNeTODk0cqKB+OUbuKj9GShYECCEjaybJDl9276oalL9ghBtSeEv20kugatTvYy590wFlJkkvyl+nPxIH0EEYMKK9XRWlu9XYnoSfboiwcv8M3SlsjAgMBAAECggEAZtju/bcKvKFPz0mkHiaJcpycy9STKphorpCT83srBVQi59CdFU6Mj+aL/xt0kCPMVigJw8P3/YCEJ9J+rS8BsoWE+xWUEsJvtXoT7vzPHaAtM3ci1HZd302Mz1+GgS8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6KHrK1o2uw==',
|
expect(out.privKey).to.equal(testId.privKey)
|
||||||
pubKey: '080012a60230820122300d06092a864886f70d01010105000382010f003082010a0282010100b648aa3f1cc1597819a5d401775e28f3af1adf417749ce378f05901b771a8a47531cea3b911d78a3e875d83e3940934d41845d52dcb9782f08b47001e18207f8e7bb0c839e545b278629e52fd2e720bc2a41c25479710d36d22d0c8338cf58e2d6ab5aedbd26cd7008b6644567ebe43611c1e8df052f591b4b78acfe0d94997f0d8f1030be0c63c93e5edff20ef3979e98ca69a6cc7f658992cdaf383faa2768914bf9bb5a5d1ab7292ee3cd79338393472a281f8e51bb8a8fd1928581020848dac9b24397ddbbea86a52fd82106d49e12fdb492e81ab53bd8cb9f74c05949924bf297e9cfc481f410460c28af5745696ef57627a127dba22c1cbfc3374a5b230203010001'
|
expect(out.pubKey).to.equal(testId.pubKey)
|
||||||
}
|
|
||||||
expect(out.id).to.equal(expected.id)
|
|
||||||
expect(out.privKey).to.equal(expected.privKey)
|
|
||||||
expect(out.pubKey).to.equal(expected.pubKey)
|
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('toBytes', (done) => {
|
it('toBytes', () => {
|
||||||
const id = PeerId.createFromHexString(testIdHex)
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
|
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
|
||||||
done()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('toJSON', (done) => {
|
describe('toJSON', () => {
|
||||||
const id = PeerId.create()
|
it('full node', () => {
|
||||||
expect(id.toB58String()).to.equal(PeerId.createFromJSON(id.toJSON()).toB58String())
|
const id = PeerId.create({bits: 64})
|
||||||
expect(id.privKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).privKey)
|
expect(
|
||||||
expect(id.pubKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).pubKey)
|
id.toB58String()
|
||||||
done()
|
).to.equal(
|
||||||
|
PeerId.createFromJSON(id.toJSON()).toB58String()
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
id.privKey.bytes
|
||||||
|
).to.deep.equal(
|
||||||
|
PeerId.createFromJSON(id.toJSON()).privKey.bytes
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
id.pubKey.bytes
|
||||||
|
).to.deep.equal(
|
||||||
|
PeerId.createFromJSON(id.toJSON()).pubKey.bytes
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('only id', () => {
|
||||||
|
const key = crypto.generateKeyPair('RSA', 64)
|
||||||
|
const id = PeerId.createFromBytes(key.public.hash())
|
||||||
|
expect(
|
||||||
|
id.toB58String()
|
||||||
|
).to.equal(
|
||||||
|
PeerId.createFromJSON(id.toJSON()).toB58String()
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(id.privKey).to.not.exist
|
||||||
|
expect(id.pubKey).to.not.exist
|
||||||
|
})
|
||||||
|
|
||||||
|
it('go interop', () => {
|
||||||
|
const id = PeerId.createFromJSON(goId)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
mh.toB58String(id.privKey.public.hash())
|
||||||
|
).to.be.eql(
|
||||||
|
goId.id
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('throws on inconsistent data', () => {
|
||||||
|
const k1 = crypto.generateKeyPair('RSA', 64)
|
||||||
|
const k2 = crypto.generateKeyPair('RSA', 64)
|
||||||
|
const k3 = crypto.generateKeyPair('RSA', 64)
|
||||||
|
|
||||||
|
it('missmatch id - private key', () => {
|
||||||
|
expect(
|
||||||
|
() => new PeerId(k1.public.hash(), k2)
|
||||||
|
).to.throw(
|
||||||
|
/inconsistent arguments/
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('missmatch id - public key', () => {
|
||||||
|
expect(
|
||||||
|
() => new PeerId(k1.public.hash(), null, k2.public)
|
||||||
|
).to.throw(
|
||||||
|
/inconsistent arguments/
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('missmatch private - public key', () => {
|
||||||
|
expect(
|
||||||
|
() => new PeerId(k1.public.hash(), k1, k2.public)
|
||||||
|
).to.throw(
|
||||||
|
/inconsistent arguments/
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('missmatch id - private - public key', () => {
|
||||||
|
expect(
|
||||||
|
() => new PeerId(k1.public.hash(), k1, k3.public)
|
||||||
|
).to.throw(
|
||||||
|
/inconsistent arguments/
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('invalid id', () => {
|
||||||
|
expect(
|
||||||
|
() => new PeerId(k1.public.hash().toString())
|
||||||
|
).to.throw(
|
||||||
|
/invalid id/
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
29552
vendor/forge.bundle.js
vendored
29552
vendor/forge.bundle.js
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user