How to use ssb-keys - 10 common examples

To help you get started, we’ve selected a few ssb-keys 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 ssbc / ssb-server / test / blobs.js View on Github external
pull(read(__filename), sbot.blobs.add(hash, function (err) {

        // connect a non-master RPC client
        var clientKeys = ssbKeys.generate()
        var rpc = client({port: 45450}, sbot.manifest)

        rpc.auth(ssbKeys.signObj(clientKeys, {
          role: 'peer',
          ts: Date.now(),
          public: clientKeys.public
        }), function (err, res) {
          if(err) throw err
          t.notEqual(res.role, 'master')
          
          // first has() call, will allow
          rpc.blobs.has(hash, function (err, res) {
            t.ok(!err)
            console.log('first has', err, res)

            // second has() call, will deny
            rpc.blobs.has(hash, function (err, res) {
              t.ok(!!err)
              console.log('second has', err.message)
github ssbc / ssb-db / test / end-to-end.js View on Github external
pull.collect(function (err, ary) {
          if (err) throw err
          console.log('ALICE', alice.id)
          console.log('SSB', ssb.id)
          var msg = ary[0].value
          var ctxt = msg.content
          var content = ssbKeys.unbox(ctxt, alice.private)
          t.deepEqual(content, { type: 'secret', okay: true }, 'alice can decrypt')

          // bob can also decrypt
          var content2 = ssbKeys.unbox(ctxt, bob.private)
          t.deepEqual(content, { type: 'secret', okay: true }, 'bob can decrypt')

          var pmsg = ssb.unbox(ary[0])
          t.notOk(msg.unbox, 'did not mutate original message')
          var unboxKey = pmsg.value.unbox
          t.equal(typeof unboxKey, 'string')
          t.ok(pmsg)
          t.deepEqual(pmsg.value.content, content2)

          console.log('boxed', ary[0].value)
          ssb2.add(ary[0].value, function (err) {
            if (err) throw err
github ssbc / ssb-db / test / end-to-end.js View on Github external
pull.collect(function (err, ary) {
          if (err) throw err
          console.log('ALICE', alice.id)
          console.log('SSB', ssb.id)
          var msg = ary[0].value
          var ctxt = msg.content
          var content = ssbKeys.unbox(ctxt, alice.private)
          t.deepEqual(content, { type: 'secret', okay: true }, 'alice can decrypt')

          // bob can also decrypt
          var content2 = ssbKeys.unbox(ctxt, bob.private)
          t.deepEqual(content, { type: 'secret', okay: true }, 'bob can decrypt')

          var pmsg = ssb.unbox(ary[0])
          t.notOk(msg.unbox, 'did not mutate original message')
          var unboxKey = pmsg.value.unbox
          t.equal(typeof unboxKey, 'string')
          t.ok(pmsg)
          t.deepEqual(pmsg.value.content, content2)

          console.log('boxed', ary[0].value)
          ssb2.add(ary[0].value, function (err) {
            if (err) throw err
            ssb2.get({ id: pmsg.key, private: true }, function (err, _msg) {
              if (err) throw err
              console.log('LOAD', _msg)
              t.deepEqual(_msg, msg) // not decrypted
github ssbc / ssb-server / test / bin.js View on Github external
t.comment('result of sbot getAddress: ' + addr)

    var ws_remotes = ma.decode(addr).filter(function(a) {
      return a.find(function(component) {
        return component.name == 'ws'
      })
    })
    t.equal(ws_remotes.length, 1, 'has one ws remote')
    var remote = ma.encode([ws_remotes[0]])
    // this breaks if multiserver address encoding changes
    t.ok(remote.indexOf('9002') > 0, 'ws address contains expected port')

    // this is a bit annoying. we can't signal ssb-client to load the secret from .path
    // it either has to be the first argument, already loaded
    var key = require('ssb-keys').loadOrCreateSync(join(path, 'secret'))
    require('ssb-client')(key, {
      path: path,
      caps: { shs: caps }, // has to be set when setting any config
      remote: remote
    }, function(err, ssb) {
      t.error(err, 'ssb-client returns no error')
      t.ok(ssb.manifest, 'got manifest from api')
      t.ok(ssb.version, 'got version from api')
      ssb.whoami(function(err, feed) {
        t.error(err, 'ssb.whoami succeeds')
        t.equal(feed.id[0], '@', 'feed.id has @ sigil')
        end()
      })
    })
  })
})
github ssbc / ssb-server / lib / cli.js View on Github external
function next (data) {
      //set $rel as key name if it's missing.
      defaultRel(data)
      console.log(data)
      rpc.auth(ssbkeys.signObj(keys, {
        role: 'client',
        ts: Date.now(),
        public: keys.public
      }), function (err) {
        if(err) throw err

        var isAsyncCmd = contains(cmd, api.manifest.async)
        if(isAsyncCmd) {
          // massage data as needed
          if (cmd == 'getPublicKey' && data && typeof data == 'object')
            data = data[1]
          if (cmd == 'add' && data && typeof data == 'object')
            data = data[1]
          // run command
          console.log(data)
          rpc[cmd](data, function (err, ret) {
github soapdog / patchfox / host_app / server.js View on Github external
const serve = require('ecstatic')
const fs = require('fs')
const path = require('path')
const ssbKeys = require('ssb-keys')
const minimist = require('minimist')
const notifier = require('node-notifier')
const SysTray = require('systray').default

let argv = process.argv.slice(2)
let i = argv.indexOf('--')
let conf = argv.slice(i + 1)
argv = ~i ? argv.slice(0, i) : argv

const config = require('ssb-config/inject')(process.env.ssb_appname, minimist(conf))

const keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
if (keys.curve === 'k256') {
  throw new Error('k256 curves are no longer supported,' +
    'please delete' + path.join(config.path, 'secret'))
}

const manifestFile = path.join(config.path, 'manifest.json')

// special server command:
// import sbot and start the server

const createSbot = require('scuttlebot')
  // .use(require('scuttlebot/plugins/plugins'))
  .use(require('scuttlebot/plugins/master'))
  .use(require('scuttlebot/plugins/gossip'))
  .use(require('scuttlebot/plugins/replicate'))
  .use(require('ssb-friends'))
github ssbc / scuttle-shell / server.js View on Github external
function start(customConfig, donecb) {
  donecb = donecb || noop
  // TODO: try { allthethings } catch(e) { donecb(e) }
  customConfig = customConfig || {}
  let appname = customConfig.appname || false
  let customPluginPaths = customConfig.plugins || false
  let argv = process.argv.slice(2)
  let i = argv.indexOf('--')
  let conf = argv.slice(i + 1)
  argv = ~i ? argv.slice(0, i) : argv
  let ssbAppName = appname || process.env.ssb_appname

  const config = require('ssb-config/inject')(ssbAppName, minimist(conf))

  const keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
  if (keys.curve === 'k256') {
    // i think this is _really_ old and could be removed
    throw new Error('k256 curves are no longer supported,' +
      'please delete' + path.join(config.path, 'secret'))
  }
  config.keys = keys
  ssbConfig = config

  const manifestFile = path.join(config.path, 'manifest.json')

  const createSbot = require('ssb-server')
    .use(require('ssb-local'))
    .use(require('ssb-logging'))
    .use(require('ssb-master'))
    .use(require('ssb-no-auth'))
    .use(require('ssb-unix-socket'))
github ssbc / ssb-peer-invites / types.js View on Github external
exports.isInvite = function (msg, caps) {
  if(!isObject(caps)) throw new Error('caps must be provided')
  //return true
  return isObject(msg) && isObject(msg.content) && (
    'user-invite' === msg.content.type &&
    ref.isFeed(msg.content.host) &&
    ref.isFeed(msg.content.invite) &&
    isMaybeBase64(msg.content.reveal) &&
    isMaybeBase64(msg.content.public) &&
    // signature must be valid !!!
    ssbKeys.verifyObj(msg.content.invite, caps.userInvite, msg.content)
  )
}
github ssbc / ssb-server / lib / cli.js View on Github external
return function(opts) {
    delete opts[0]
    delete opts._
    var keys = require('ssb-keys').loadSync(path.join(config.path, 'secret'))
    var rpc = scuttlebot.createClient({port: config.port, host: 'localhost'})

    // if there's data coming from stdin, pipe that into our command
    if(!process.stdin.isTTY) {
      pull(
        toPull.source(process.stdin),
        pull.collect(function (err, ary) {
          var str = Buffer.concat(ary).toString('utf8')
          var data = JSON.parse(str)
          next(data)
        })
      )
    }
    else
      next(opts)
github arj03 / ssb-entitydb / test / multi-write.js View on Github external
tape('multi-write', function (t) {

    var pub = createSbot({
        temp: 'test-entitydb-multi-write-pub', timeout: 200,
        allowPrivate: true,
        keys: ssbKeys.generate()
    });

    var alice = createSbot({
        temp: 'test-entitydb-multi-write-alice', timeout: 200,
        allowPrivate: true,
        keys: ssbKeys.generate(),
        seeds: [pub.getAddress()]
    });

    var bob = createSbot({
        temp: 'test-entitydb-multi-write-bob', timeout: 200,
        allowPrivate: true,
        keys: ssbKeys.generate(),
        seeds: [pub.getAddress()]
    });

    console.log("alice is: " + alice.id);
    console.log("bob is: " + bob.id);

    t.test('alice and bob follow each other', function (t) {
        t.plan(1);
        var done = multicb();