How to use the libp2p-crypto.keys function in libp2p-crypto

To help you get started, we’ve selected a few libp2p-crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libp2p / js-libp2p-keychain / test / peerid.js View on Github external
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()
  })
github libp2p / js-libp2p-crypto-secp256k1 / test / secp256k1.spec.js View on Github external
/* 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()
github ipfs / js-ipfs / src / core / ipns / republisher.js View on Github external
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)
      }
    }
  }
github libp2p / js-libp2p-record / test / validator.spec.js View on Github external
before(async () => {
    key = await crypto.keys.generateKeyPair('rsa', 1024)
    hash = await key.public.hash()
    cases = generateCases(hash)
  })
github libp2p / js-libp2p-websocket-star / src / listener.js View on Github external
_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) {
github peer-base / peer-base / src / keys / uri-decode.js View on Github external
}
    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)
    }
  })
}
github ipfs-shipyard / peer-pad-core / src / backend / keys / parse.js View on Github external
      read: (callback) => callback(null, crypto.keys.unmarshalPublicKey(readKey)),
      write: (callback) => writeKey ? crypto.keys.unmarshalPrivateKey(writeKey, callback) : callback(null, null),
github cloudflare / ipfs-ext / src / verify.js View on Github external
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)
github peer-base / peer-base / src / keys / generate.js View on Github external
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)
    })
  })
}
github ipfs / js-ipfs / src / core / components / name.js View on Github external
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')
  }
}