Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('decoded public key', async () => {
// get protobuf version of the public key
const publicKeyProtobuf = peer.marshalPubKey()
const publicKey = crypto.keys.unmarshalPublicKey(publicKeyProtobuf)
publicKeyDer = publicKey.marshal()
// get protobuf version of the private key
const privateKeyProtobuf = peer.marshalPrivKey()
const key = await promisify(crypto.keys.unmarshalPrivateKey, {
context: crypto.keys
})(privateKeyProtobuf)
expect(key).to.exist()
})
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const libp2pCrypto = require('libp2p-crypto')
const keysPBM = libp2pCrypto.keys.keysPBM
const randomBytes = libp2pCrypto.randomBytes
const crypto = require('../src/crypto')(randomBytes)
describe('secp256k1 keys', () => {
let key
const secp256k1 = require('../src')(keysPBM, randomBytes)
before(async () => {
key = await secp256k1.generateKeyPair()
})
it('generates a valid key', async () => {
expect(key).to.be.an.instanceof(secp256k1.Secp256k1PrivateKey)
expect(key.public).to.be.an.instanceof(secp256k1.Secp256k1PublicKey)
const digest = await key.hash()
await this._republishEntry(privateKey)
} catch (err) {
const errMsg = 'cannot republish entry for the node\'s private key'
log.error(errMsg)
return
}
// keychain needs pass to get the cryptographic keys
if (pass) {
try {
const keys = await this._keychain.listKeys()
for (const key in keys) {
const pem = await this._keychain.exportKey(key.name, pass)
const privKey = await crypto.keys.import(pem, pass)
await this._republishEntry(privKey)
}
} catch (err) {
log.error(err)
}
}
}
before(async () => {
key = await crypto.keys.generateKeyPair('rsa', 1024)
hash = await key.public.hash()
cases = generateCases(hash)
})
_cryptoChallenge (callback) {
if (!this.io) {
return callback(new Error('Not connected'))
}
const pubKeyStr = this.canCrypto ? crypto.keys.marshalPublicKey(this.id.pubKey).toString('hex') : ''
const maStr = this.ma.toString()
this.io.emit('ss-join', maStr, pubKeyStr, (err, sig, peers) => {
if (err) { return callback(err) }
if (sig) {
if (!this.canCrypto) {
this._down()
return callback(new Error("Can't sign cryptoChallenge: No id provided"))
}
this.log('performing cryptoChallenge')
this.id.privKey.sign(Buffer.from(sig), (err, signature) => {
if (err) {
}
const readEncoded = keyComponents[0]
const read = bs58.decode(readEncoded)
const writeEncoded = keyComponents[1]
const write = writeEncoded && bs58.decode(writeEncoded)
const readKey = crypto.keys.unmarshalPublicKey(read)
const keys = {
read: readKey
}
keys.cipher = deriveCipherFromKeys(keys)
if (write) {
crypto.keys.unmarshalPrivateKey(write, (err, writeKey) => {
if (err) {
return reject(err)
}
keys.write = writeKey
resolve(keys)
})
} else {
resolve(keys)
}
})
}
read: (callback) => callback(null, crypto.keys.unmarshalPublicKey(readKey)),
write: (callback) => writeKey ? crypto.keys.unmarshalPrivateKey(writeKey, callback) : callback(null, null),
const CID = require("cids")
const dagPB = require("ipld-dag-pb")
const ipns = require("ipns")
const keys = require("libp2p-crypto").keys
const multihash = require("multihashes")
const PeerId = require("peer-id")
const protons = require("protons")
const unixfsData = protons(require("ipfs-unixfs/src/unixfs.proto.js")).Data
const dns = require("./dns/")
const util = require("./util.js")
function join(a, b) {
let c = []
for (let x of a) {
c.push(x)
}
for (let y of b) {
c.push(y)
return new Promise((resolve, reject) => {
options = Object.assign({}, defaultOptions, options)
crypto.keys.generateKeyPair(options.algo, options.bits, (err, key) => {
if (err) { return reject(err) }
const keys = {
read: key.public,
write: key
}
keys.cipher = deriveCipherFromKeys(keys)
resolve(keys)
})
})
}
const keyLookup = async (ipfsNode, kname) => {
if (kname === 'self') {
return ipfsNode._peerInfo.id.privKey
}
try {
const pass = ipfsNode._options.pass
const pem = await ipfsNode._keychain.exportKey(kname, pass)
const privateKey = await promisify(crypto.keys.import.bind(crypto.keys))(pem, pass)
return privateKey
} catch (err) {
log.error(err)
throw errcode(err, 'ERR_CANNOT_GET_KEY')
}
}