commit a499c5129adbf6f3d251ab8323ca9e87a57a292d Author: David Dias Date: Wed Jul 8 14:51:49 2015 -0700 Initial Commit diff --git a/package.json b/package.json new file mode 100644 index 0000000..7b9cee6 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "ipfs-peer-id", + "version": "0.0.0", + "description": "IPFS Peer Id implementation in Node.js", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "IPFS" + ], + "author": "David Dias ", + "license": "MIT" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..f3b8030 --- /dev/null +++ b/src/index.js @@ -0,0 +1,79 @@ +/* + * Id is an object representation of a peer Id. a peer Id is a multihash + */ + +var multihashing = require('multihashing') +var base58 = require('bs58') +var crypto = require('crypto') + +exports = module.exports = Id + +function Id (id, pubKey, privKey) { + var self = this + + if (!(self instanceof Id)) { + throw new Error('Id must be called with new') + } + + self.privKey = privKey + self.pubKey = pubKey + self.id = id // multihash - sha256 - buffer + + // pretty print + + self.toPrint = function () { + return { + id: id.toHexString(), + privKey: privKey.toString('hex'), + pubKey: pubKey.toString('hex') + } + } + + // encode/decode functions + + self.toHexString = function () { + return self.id.toString('hex') + } + + self.toBytes = function () { + return self.id + } + + self.toB58String = function () { + return base58.encode(self.id) + } + +} + +// generation + +exports.create = function () { + var ecdh = crypto.createECDH('secp256k1') + ecdh.generateKeys() + + var mhId = multihashing(ecdh.getPublicKey(), 'sha2-256') + + return new Id(mhId, ecdh.getPrivateKey(), ecdh.getPublicKey()) +} + +exports.createFromHexString = function (str) { + return new Id(new Buffer(str), 'hex') +} + +exports.createFromBytes = function (buf) { + return new Id(buf) +} + +exports.createFromB58String = function (str) { + return new Id(new Buffer(base58.decode(str))) +} + +exports.createFromPubKey = function (pubKey) { + var mhId = multihashing(pubKey, 'sha2-256') + + return new Id(mhId, null, pubKey) +} + +exports.createFromPrivKey = function () { + // TODO(daviddias) derive PubKey from priv +}