Skip to content

Commit

Permalink
Get following and handshakes working properly using node forks in the…
Browse files Browse the repository at this point in the history
… tests. To get things to work, I had to use some funky download code i found in dat-node/examples
  • Loading branch information
jayrbolton committed Apr 21, 2017
1 parent 8a0e975 commit 9367862
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 139 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
test/tmp
26 changes: 26 additions & 0 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const createDir = require('./utils/create-dir')
const path = require('path')
const mirror = require('mirror-folder')
const fs = require('fs')
const Dat = require('dat-node')
const ram = require('random-access-memory')

// Download an archive, calling cb when it is completed

module.exports = function download (key, dest, cb) {
createDir(dest)
Dat(ram, {key, sparse: true}, function (err, dat) {
if (err) throw err
const dload = () => {
const progress = mirror({fs: dat.archive, name: '/'}, dest, (err) => {
if (err) throw err
dat.close()
cb(dat)
})
}
const network = dat.joinNetwork()
dat.archive.metadata.update(dload)
})
}


34 changes: 10 additions & 24 deletions lib/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,22 @@ const fs = require('fs')
const Dat = require('dat-node')
const createDir = require('./utils/create-dir')
const json = require('./utils/json')
const download = require('./download')

// Have one user follow the public metadat for another user
function follow(userA, key, cb) {
// First, download the given user's public metadat into a temp directory
const tmpPath = userA.dirs.base + '/.tmp'
createDir(tmpPath)
console.log('key', key)
Dat(tmpPath, {key}, (err, dat) => {
if(err) throw err
const network = dat.joinNetwork((err) => {
if(err) throw err
// Now read their public user data
// XXX this doesn't seem to work. I need a way to wait on the dat to finish downloading
if(err) throw err
dat.close()
const userB = json.read(tmpPath + '/user.json')
console.log('follow id', userB.id)
const followPath = userA.dirs.follows + '/' + userB.id
fs.renameSync(tmpPath, followPath)
const jsonPath = userA.dirs.base + '/user.json'
userA.follows[userB.id] = followPath
json.write(jsonPath, omit(['publicMetadat'], userA))
Dat(followPath, {key: key}, (err, dat) => {
if(err) throw err
dat.joinNetwork(err => {
if(err) throw err
cb(dat, userA, userB)
})
})
})
download(key, tmpPath, dat => {
dat.close()
const userB = json.read(tmpPath + '/user.json')
const followPath = userA.dirs.follows + '/' + userB.id
fs.renameSync(tmpPath, followPath)
const jsonPath = userA.dirs.base + '/user.json'
userA.follows[userB.id] = followPath
json.write(jsonPath, omit(['publicMetadat'], userA))
cb(userA, userB)
})
}

Expand Down
6 changes: 3 additions & 3 deletions lib/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ openpgp.config.aead_protect = true
// The callback will be be passed the contents of the handshake file
function handshake(userA, key, passphrase, cb) {
// First, make sure userA is following userB (ie has a copy of their public metadat)
follow(userA, key, (dat, userA, userB) => {
dat.close()
follow(userA, key, (userA, userB) => {
// Create a new metadat to send private data from userA to userB
// Encrypt the dat address using userB's pubkey, signed by userA
const dir = userA.dirs.base + '/relationships/' + userB.id
createDir(dir)
Dat(dir, (err, dat) => {
if(err) throw err
const datKey = dat.key.toString('hex')
dat.close()
const options = {
data: datKey
, publicKeys: openpgp.key.readArmored(userB.pubKey).keys
Expand All @@ -30,7 +30,7 @@ function handshake(userA, key, passphrase, cb) {
// Write the the ciphertext to userA's handshakes directory with userB's id as the filename
const handshakeFilename = userA.dirs.handshakes + '/' + userB.id + '.txt'
fs.writeFileSync(handshakeFilename, ciphertext.data)
cb(dat, userA, userB)
cb(userA, userB)
}).catch(console.error.bind(console) )
})
})
Expand Down
53 changes: 21 additions & 32 deletions lib/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const merge = require('ramda/src/merge')
const fs = require('fs')
const openpgp = require('openpgp')
const Dat = require('dat-node')
Expand All @@ -21,50 +20,40 @@ function setup(options, cb) {
, pub: path + '/' + id
, handshakes: path + '/' + id + '/handshakes'
}
for(var key in dirs) {
for(var key in dirs) {
createDir(dirs[key])
}

// Create the pgp keys
const pgpOptions = {userIds: [{name}], numBits: options.numBits || 4096, passphrase}
createPgpKey(pgpOptions, (pgpKeys) => {
// Create the public metadat
createPublicMetadat(dirs.pub, (dat) => {
const createdAt = new Date()
const pubKey = pgpKeys.publicKeyArmored
const privKey = pgpKeys.privateKeyArmored

// write public data to the the user's public metadat
const pubListPath = dirs.pub + '/user.json'
const pub = { id, pubKey, dats: [] }
json.write(pubListPath, pub)
const createdAt = new Date()
const pubKey = pgpKeys.publicKeyArmored
const privKey = pgpKeys.privateKeyArmored
// write public data to the the user's public metadat
const pubListPath = dirs.pub + '/user.json'
const pub = { id, pubKey, dats: [] }
json.write(pubListPath, pub)
// write (private and local-only) user data to disk
const userJsonPath = path + '/user.json'
const userJson = { id, name, createdAt , publicDats: [], privateDats: [], relationships: {}, follows: {} , pubKey, privKey, dirs }

// write (private and local-only) user data to disk
const userJsonPath = path + '/user.json'
const userJson = {
id, name, createdAt
, publicDats: [], privateDats: [], relationships: {}, follows: {}
, pubKey, privKey
, publicMetadatKey: dat.key.toString('hex')
, dirs
}
// Create the public metadat
Dat(dirs.pub, (err, dat) => {
if(err) throw err
dat.importFiles({watch: true})
userJson.publicMetadatKey = dat.key.toString('hex')
json.write(userJsonPath, userJson)
userJson.publicMetadat = dat

// Return the new user object -- add in the actual dat object, which can't be serialized to json
cb(merge(userJson, { publicMetadat: dat }))
dat.joinNetwork({}, err => {
if (err) throw err
cb(userJson)
})
})
})
}

function createPublicMetadat(dir, cb) {
cb = cb || function(){}
createDir(dir)
Dat(dir, (err, dat) => {
if(err) throw err
cb(dat)
})
}

function createPgpKey(opt, cb) {
cb = cb || function(){}
openpgp.generateKey(opt).then(key => { cb(key) }).catch(error => console.error('error creating pgp keys', error))
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"dependencies": {
"dat-node": "2.0.0",
"openpgp": "2.5.3",
"ramda": "0.23.0",
"toiletdb": "1.2.1",
"uuid": "3.0.1"
"uuid": "3.0.1",
"mirror-folder": "2.0.0",
"random-access-memory": "2.4.0"
},
"devDependencies": {
"tap-spec": "4.1.1",
"tape": "4.6.3"
},
"scripts": {
"test": "([ -e /tmp/metadat-test ] && rm -r /tmp/metadat-test) ; tape test/index.js | tap-spec"
"test": "([ -e test/tmp ] && rm -r test/tmp) ; tape test/index.js | tap-spec"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {setup, handshake} = require('../')

const prefix = '/tmp/metadat-test'
const prefix = 'test/tmp'

const handlers = {
// Parent process has initialized user and sends key to us to make a handshake
Expand All @@ -10,14 +10,11 @@ const handlers = {
process.send({name: 'shook', data: null})
})
}
, completed: () => process.exit(1)
}

setup({path: prefix + '/follow-test/u2-base', name: 'finn', passphrase: 'arstarst', numBits: 512}, (user) => {
user.publicMetadat.importFiles()
user.publicMetadat.joinNetwork(err => {
if(err) throw err
process.send({name: 'startFollow', data: user.publicMetadat.key.toString('hex')})
})
process.send({name: 'startFollow', data: user.publicMetadat.key.toString('hex')})

process.on('message', (msg) => {
const {name, data} = msg
Expand Down
7 changes: 2 additions & 5 deletions test/child-process-handshake.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const json = require('../lib/utils/json')
const {setup, handshake, checkHandshake} = require('../')

const prefix = '/tmp/metadat-test'

const handlers = {
// Parent process has initialized user and sends key to us to make a handshake
startHandshake: (user, key) => {
Expand All @@ -14,11 +12,10 @@ const handlers = {
const otherUser = json.read(user.dirs.follows + '/' + otherUserID + '/user.json')
checkHandshake(user, otherUser)
}
, completed: () => process.exit(1)
}

setup({path: prefix + '/handshake-test-child', name: 'u2', passphrase: 'arstarst', numBits: 512}, (user) => {
user.publicMetadat.importFiles({watch: true})
user.publicMetadat.joinNetwork()
setup({path: 'test/tmp/handshake-test/u2-base', name: 'u2', passphrase: 'arstarst', numBits: 512}, (user) => {
process.send({name: 'startHandshake', data: user.publicMetadat.key.toString('hex')})

process.on('message', (msg) => {
Expand Down
Loading

0 comments on commit 9367862

Please sign in to comment.