Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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) {
function test_invalid(t, state, keys, hmac_key, content, timestamp) {
var msg = ssbKeys.signObj(keys, hmac_key, {
previous: state ? state.id : null,
sequence: state ? state.sequence + 1 : 1,
author: keys.id,
timestamp: +timestamp,
hash: 'sha256',
content: content,
})
if(!Buffer.isBuffer(content))
data.push({state: state, msg: msg, cap: hmac_key, valid: false, id: id(msg)})
t.throws(function () {
console.log(state, v.create(state, keys, hmac_key, content, timestamp))
})
t.throws(function () {
var _state = {feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
var dbBob = u.createDB('followtest-bob')
var bob = dbBob.feed
var db = u.createDB('feed-test', {port: 1234, host: 'localhost'})
var feed = db.feed
var server = db.use(require('../plugins/easy'))
console.log(server.getManifest())
var client = sbot.createClient(
db.config,
server.getManifest()
)
var signed = ssbkeys.signObj(feed.keys, {
role: 'client',
ts: Date.now(),
public: feed.keys.public
})
client.auth(signed, function(err) {
if (err) throw err
client.easy.follow(bob.id, function(err, msg) {
if (err) throw err
client.easy.isFollowing(bob.id, function(err, isFollowing) {
if (err) throw err
t.equal(isFollowing, true)
client.easy.isFollowing(alice.id, function(err, isFollowing) {
function test_invalid_msg(t, state, keys, hmac_key, _msg) {
var msg = ssbKeys.signObj(keys, hmac_key, _msg)
if(!state)
state = {
queue: [],
feeds: {},
validated: 0
}
data.push({state: state, msg: msg, cap: hmac_key, valid: false, id: id(msg)})
t.throws(function () {
console.log(v.append(state, hmac_key, msg))
})
t.throws(function () {
var _state = {feeds: {}}
_state.feeds[keys.id] = state
v.append(_state, hmac_key, msg)
})
tape('wrong invite', function (t) {
var seed = hash('seed1')
var invite = v.create(null, alice, caps.sign, i.createInvite(seed, alice.id, 'REVEAL', null, caps), new Date('2018-03-14T06:14:18.377Z'))
var accept_content = ssbKeys.signObj(ssbKeys.generate(null, seed), caps.peerInvite, {
type: 'peer-invite/accept',
receipt: '%'+ssbKeys.hash(JSON.stringify(invite, null, 2)),
id: bob.id,
//key is missing!
})
var accept2 = v.create(null, bob, caps.sign, accept_content, new Date('2018-03-14T06:32:18.377Z'))
//just test we do not verify the incorrect invite
throws(t, function () {
i.verifyAccept(accept2, invite, caps)
}, 'peer-invites:accept-must-reveal-key')
t.end()
})
tape('invalid - wrong invitee', function (t) {
//construct a message where host does not match
var seed = hash('seed2')
var keys = ssbKeys.generate(null, seed)
var invalid = ssbKeys.signObj(keys, caps.peerInvite, {
type: 'peer-invite',
invite: ssbKeys.generate(null, hash('seed3')),
host: alice.id
})
var msg = v.create(null, alice, caps.sign, invalid, new Date('2018-03-26T06:14:18.377Z'))
throws(t, function () {
i.verifyInvitePublic(msg, caps)
}, 'peer-invites:invite-signature-failed')
throws(t, function () {
i.verifyInvitePrivate(msg, seed, caps)
}, 'peer-invites:invite-signature-failed')
t.end()
function (err) {
if (err) throw explain(err, 'init failed')
ssb.add(
prev = require('ssb-keys').signObj(keys, null, {
previous: ('%' + hash(JSON.stringify(prev, null, 2))),
sequence: prev ? prev.sequence + 1 : 1,
author: keys.id,
timestamp: require('monotonic-timestamp')(),
hash: 'sha256',
content: { type: 'msg', value: 'hello' }
}),
function (err) {
if (err) throw explain(err, 'hello failed')
ssb.add(
prev = create(keys, 'msg', 'hello2', prev),
function (err) {
if (err) throw explain(err, 'hello2 failed')
var state = {
state = flatState(state)
if(state && +timestamp <= state.timestamp) throw new Error('timestamp must be increasing')
var msg = {
previous: state ? state.id : null,
sequence: state ? state.sequence + 1 : 1,
author: keys.id,
timestamp: +timestamp,
hash: 'sha256',
content: content
}
var err = isInvalidShape(msg)
if(err) throw err
return ssbKeys.signObj(keys, hmac_key, msg)
}
if(!isObject(caps)) throw new Error('caps *must* be provided')
seed = toBuffer(seed)
exports.verifyInvitePrivate(msg, seed, caps)
var keys = ssbKeys.generate(null, seed) //K
if(keys.id != msg.content.invite)
throw code(new Error('seed does not match invite'), 'seed-must-match-invite')
var inviteId = toMsgId(msg)
var content = {
type: 'peer-invite/accept',
receipt: inviteId,
id: id
}
if(msg.content.reveal)
content.key = hash2(seed).toString('base64')
return ssbKeys.signObj(keys, caps.peerInvite, content)
}