mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-05-02 05:32:24 +00:00
time out increased
This commit is contained in:
parent
61c0c663d6
commit
58fe038e07
@ -4,7 +4,6 @@ module.exports = function (config) {
|
|||||||
var deps = [
|
var deps = [
|
||||||
'deps/forge.bundle.js'
|
'deps/forge.bundle.js'
|
||||||
]
|
]
|
||||||
|
|
||||||
config.set({
|
config.set({
|
||||||
basePath: '',
|
basePath: '',
|
||||||
frameworks: ['mocha'],
|
frameworks: ['mocha'],
|
||||||
@ -20,7 +19,7 @@ module.exports = function (config) {
|
|||||||
webpack: {
|
webpack: {
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['', '.js', '.json'],
|
extensions: ['', '.js', '.json'],
|
||||||
alias: {'node-forge': __dirname+'/deps/forge.bundle.js' }
|
alias: { 'node-forge': __dirname + '/deps/forge.bundle.js' }
|
||||||
},
|
},
|
||||||
externals: {
|
externals: {
|
||||||
fs: '{}'
|
fs: '{}'
|
||||||
@ -33,7 +32,7 @@ module.exports = function (config) {
|
|||||||
{ test: /\.json$/, loader: 'json' }
|
{ test: /\.json$/, loader: 'json' }
|
||||||
],
|
],
|
||||||
noParse: []
|
noParse: []
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
webpackMiddleware: {
|
webpackMiddleware: {
|
||||||
|
65
src/index.js
65
src/index.js
@ -10,15 +10,15 @@ var protobuf = require('protocol-buffers')
|
|||||||
|
|
||||||
var isNode = !global.window
|
var isNode = !global.window
|
||||||
|
|
||||||
//protobuf read from file
|
// protobuf read from file
|
||||||
var messages = isNode ? protobuf(fs.readFileSync(__dirname+'/../pb/crypto.proto')) : protobuf(require('buffer!./../pb/crypto.proto'))
|
var messages = isNode ? protobuf(fs.readFileSync(__dirname + '/../pb/crypto.proto')) : protobuf(require('buffer!./../pb/crypto.proto'))
|
||||||
|
|
||||||
//for some reason webpack can only find forge at forge.forge().someFunction()...
|
// for some reason webpack can only find forge at forge.forge().someFunction()
|
||||||
//browser should be able to just use forge.someFunction()
|
// browser should be able to just use forge.someFunction()
|
||||||
//this is only happening when js-ipfs bundles peer-id module
|
// this is only happening when js-ipfs bundles peer-id module
|
||||||
/*if(!isNode){
|
/* if(!isNode){
|
||||||
forge = forge.forge()
|
forge = forge.forge()
|
||||||
}*/
|
} */
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = Id
|
||||||
|
|
||||||
@ -60,23 +60,24 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//unwrap the private key protobuf stream
|
// unwrap the private key protobuf stream
|
||||||
function unmarshal (key) {
|
function unmarshal (key) {
|
||||||
var dpb = messages.PrivateKey.decode(key)
|
var dpb = messages.PrivateKey.decode(key)
|
||||||
return dpb
|
return dpb
|
||||||
}
|
}
|
||||||
|
|
||||||
//create a public key protobuf to be base64 string stored in config
|
// create a public key protobuf to be base64 string stored in config
|
||||||
function marshal (data, type) {
|
function marshal (data, type) {
|
||||||
if(type === 'Public'){
|
var epb
|
||||||
var epb = messages.PublicKey.encode({
|
if (type === 'Public') {
|
||||||
|
epb = messages.PublicKey.encode({
|
||||||
Type: 0,
|
Type: 0,
|
||||||
Data: data
|
Data: data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if(type === 'Private'){
|
if (type === 'Private') {
|
||||||
var epb = messages.PrivateKey.encode({
|
epb = messages.PrivateKey.encode({
|
||||||
Type: 0,
|
Type: 0,
|
||||||
Data: data
|
Data: data
|
||||||
})
|
})
|
||||||
@ -85,35 +86,35 @@ function marshal (data, type) {
|
|||||||
return epb
|
return epb
|
||||||
}
|
}
|
||||||
|
|
||||||
//this returns a base64 encoded protobuf of the public key
|
// this returns a base64 encoded protobuf of the public key
|
||||||
function formatKey(key, type) {
|
function formatKey (key, type) {
|
||||||
//create der buffer of public key asn.1 object
|
// create der buffer of public key asn.1 object
|
||||||
var der = forge.asn1.toDer(key)
|
var der = forge.asn1.toDer(key)
|
||||||
|
|
||||||
//create forge buffer of der public key buffer
|
// create forge buffer of der public key buffer
|
||||||
var fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
var fDerBuf = forge.util.createBuffer(der.data, 'binary')
|
||||||
|
|
||||||
//convert forge buffer to node buffer public key
|
// convert forge buffer to node buffer public key
|
||||||
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
var nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
|
||||||
|
|
||||||
//protobuf the new DER bytes to the PublicKey Data: field
|
// protobuf the new DER bytes to the PublicKey Data: field
|
||||||
var marshalKey = marshal(nDerBuf, type)
|
var marshalKey = marshal(nDerBuf, type)
|
||||||
|
|
||||||
//encode the protobuf public key to base64 string
|
// encode the protobuf public key to base64 string
|
||||||
var b64 = marshalKey.toString('base64')
|
var b64 = marshalKey.toString('base64')
|
||||||
return b64
|
return b64
|
||||||
}
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
exports.create = function () {
|
exports.create = function () {
|
||||||
//generate keys
|
// generate keys
|
||||||
var pair = forge.rsa.generateKeyPair({bits:2048, e: 0x10001})
|
var pair = forge.rsa.generateKeyPair({ bits: 2048, e: 0x10001 })
|
||||||
|
|
||||||
//return the RSA public/private key to asn1 object
|
// return the RSA public/private key to asn1 object
|
||||||
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
var asnPub = forge.pki.publicKeyToAsn1(pair.publicKey)
|
||||||
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
var asnPriv = forge.pki.privateKeyToAsn1(pair.privateKey)
|
||||||
|
|
||||||
//format the keys to protobuf base64 encoded string
|
// format the keys to protobuf base64 encoded string
|
||||||
var protoPublic64 = formatKey(asnPub, 'Public')
|
var protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
var protoPrivate64 = formatKey(asnPriv, 'Private')
|
var protoPrivate64 = formatKey(asnPriv, 'Private')
|
||||||
|
|
||||||
@ -141,28 +142,28 @@ exports.createFromPubKey = function (pubKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPrivKey = function (privKey) {
|
exports.createFromPrivKey = function (privKey) {
|
||||||
//create a buffer from the base64 encoded string
|
// create a buffer from the base64 encoded string
|
||||||
var buf = new Buffer(privKey, 'base64')
|
var buf = new Buffer(privKey, 'base64')
|
||||||
|
|
||||||
//get the private key data from the protobuf
|
// get the private key data from the protobuf
|
||||||
var mpk = unmarshal(buf)
|
var mpk = unmarshal(buf)
|
||||||
|
|
||||||
//create a forge buffer
|
// create a forge buffer
|
||||||
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
var fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
|
||||||
|
|
||||||
//create an asn1 object from the private key bytes saved in the protobuf Data: field
|
// create an asn1 object from the private key bytes saved in the protobuf Data: field
|
||||||
var asnPriv = forge.asn1.fromDer(fbuf)
|
var asnPriv = forge.asn1.fromDer(fbuf)
|
||||||
|
|
||||||
//get the RSA privatekey data from the asn1 object
|
// get the RSA privatekey data from the asn1 object
|
||||||
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
var privateKey = forge.pki.privateKeyFromAsn1(asnPriv)
|
||||||
|
|
||||||
//set the RSA public key to the modulus and exponent of the private key
|
// set the RSA public key to the modulus and exponent of the private key
|
||||||
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
var publicKey = forge.pki.rsa.setPublicKey(privateKey.n, privateKey.e)
|
||||||
|
|
||||||
//return the RSA public key to asn1 object
|
// return the RSA public key to asn1 object
|
||||||
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
var asnPub = forge.pki.publicKeyToAsn1(publicKey)
|
||||||
|
|
||||||
//format the public key
|
// format the public key
|
||||||
var protoPublic64 = formatKey(asnPub, 'Public')
|
var protoPublic64 = formatKey(asnPub, 'Public')
|
||||||
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
var mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||||
return new Id(mhId, privKey, protoPublic64)
|
return new Id(mhId, privKey, protoPublic64)
|
||||||
|
@ -15,37 +15,37 @@ const testIdBytes = new Buffer('1220151ab1658d8294ab34b71d5582cfe20d06414212f440
|
|||||||
|
|
||||||
const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
const testIdB58String = 'QmQ2zigjQikYnyYUSXZydNXrDRhBut2mubwJBaLXobMt3A'
|
||||||
|
|
||||||
describe('id', function(done) {
|
describe('id', function (done) {
|
||||||
this.timeout(10000)
|
this.timeout(30000)
|
||||||
it('create a new id', done => {
|
it('create a new id', done => {
|
||||||
//this will always be randomly generated. is there something i can test it against?
|
// this will always be randomly generated. is there something i can test it against?
|
||||||
var id = PeerId.create()
|
var id = PeerId.create()
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
it('recreate an Id from Hex string', done =>{
|
it('recreate an Id from Hex string', done => {
|
||||||
var id = PeerId.createFromHexString(testIdHex)
|
var id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(testIdBytes).to.deep.equal(id.id)
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
it('Recreate an Id from a Buffer', done =>{
|
it('Recreate an Id from a Buffer', done => {
|
||||||
var id = PeerId.createFromBytes(testIdBytes)
|
var id = PeerId.createFromBytes(testIdBytes)
|
||||||
expect(testId.id).to.equal(id.toHexString())
|
expect(testId.id).to.equal(id.toHexString())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
it('Recreate an B58 String', done =>{
|
it('Recreate an B58 String', done => {
|
||||||
var id = PeerId.createFromB58String(testIdB58String)
|
var id = PeerId.createFromB58String(testIdB58String)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
it('Recreate from a Public Key', done =>{
|
it('Recreate from a Public Key', done => {
|
||||||
var id = PeerId.createFromPubKey(testId.pubKey)
|
var id = PeerId.createFromPubKey(testId.pubKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
it('Recreate from a Private Key', done =>{
|
it('Recreate from a Private Key', done => {
|
||||||
var id = PeerId.createFromPrivKey(testId.privKey)
|
var id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
expect(testIdB58String).to.equal(id.toB58String())
|
expect(testIdB58String).to.equal(id.toB58String())
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user