Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(exists, cb) => {
if (exists === true) {
return cb(new Error('repo already exists'))
}
// Generate peer identity keypair + transform to desired format + add to config.
opts.log(`generating ${opts.bits}-bit RSA keypair...`, false)
if (opts.privKey) {
peerId.createFromPrivKey(opts.privKey, cb)
} else {
peerId.create({bits: opts.bits}, cb)
}
},
(keys, cb) => {
describe('cyclon-peer', function () {
this.timeout(20000)
const AliceId = PeerId.createFromPrivKey(alice.privKey)
const Bob = new CyclonPeer({peers: [new PeerInfo(PeerId.createFromB58String(AliceId.toB58String()))]})
const Charles = new CyclonPeer()
const Eve = new CyclonPeer()
it('create with bootstrap peers', (done) => {
const Alice = new CyclonPeer({peer: new PeerInfo(AliceId)})
expect(Alice).to.exist
expect(Bob).to.exist
expect(Bob.partialView).to.have.lengthOf(1)
expect(Object.keys(Bob.partialView.peers)[0]).to.eql(Bob.partialView.peerToId(Alice.peer))
expect(Bob.partialView.get(Alice.peer)).to.exist
done()
})
it('partialView is within the maxPeers limit', (done) => {
const Alice = new CyclonPeer({peer: new PeerInfo(AliceId), maxPeers: 1})
before(function (done) {
const encoded = Buffer.from(alicePrivKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id) => {
alice = id
done()
})
})
before(function (done) {
const encoded = Buffer.from(alicePrivKey, 'base64')
PeerId.createFromPrivKey(encoded, (err, id) => {
expect(err).to.not.exist()
alice = id
done()
})
})
before((done) => {
const ma = '/ip4/127.0.0.1/tcp/9200/ws/ipfs/' + rawPeer.id
PeerId.createFromPrivKey(rawPeer.privKey, (err, id) => {
if (err) {
return done(err)
}
peerB = new PeerInfo(id)
peerB.multiaddrs.add(ma)
done()
})
})
const makeRecord = (key, k, callback) => {
PeerId.createFromPrivKey(key.bytes, (err, id) => {
if (err) {
return callback(err)
}
let rec
try {
rec = new Record(k, crypto.randomBytes(10), id)
} catch (err) {
return callback(err)
}
callback(null, rec)
})
}
new Promise(async (resolve, reject) => {
let serializedKeyPair
try {
serializedKeyPair = await db.get('key-pair')
resolve(deserializeKeyPair(serializedKeyPair))
} catch (err) {
if (!err.notFound) return reject(err)
const key = await generateKeyPair('secp256k1', 256)
const peerId = await PeerId.createFromPrivKey(key.bytes)
const serializedKeyPair = await serializeKeyPair(peerId)
console.log(serializedKeyPair)
await db.put('key-pair', serializedKeyPair)
resolve(peerId)
}
})
function bootNode (next) {
PeerId.createFromPrivKey(privateKey, (err, idPublisher) => {
if (err) {
throw err
}
const peerPublisher = new PeerInfo(idPublisher)
peerPublisher.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/12345'))
nodePublisher = new Node(peerPublisher)
nodePublisher.start((err) => {
console.log('Publisher listening on:')
peerPublisher.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/ipfs/' + idPublisher.toB58String())
})
next(err)
})
})
}
(config, cb) => {
const privKey = config.Identity.PrivKey
peerId.createFromPrivKey(privKey, (err, id) => {
cb(err, config, id)
})
},
(config, id, cb) => {
return new Promise((resolve, reject) => {
const handler = (err, peerInfo) => {
if (err) {
return reject(err)
}
this.multiaddrs.forEach(ma => peerInfo.multiaddrs.add(ma))
resolve(peerInfo)
}
if (this.key) {
PeerId.createFromPrivKey(this.key, (err, id) => {
if (err) {
return reject(err)
}
PeerInfo.create(id, handler)
})
} else {
PeerInfo.create(handler)
}
})
}