Skip to content

Commit 334bf68

Browse files
committed
feat: pairing devices
1 parent 1113e4f commit 334bf68

File tree

1 file changed

+87
-83
lines changed

1 file changed

+87
-83
lines changed

lib/validator.js

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function validator (node) {
2828
return {
2929
validate,
3030
checkExists,
31-
checkPrev,
31+
checkMore,
3232
checkAuthentic
3333
}
3434

@@ -47,105 +47,65 @@ module.exports = function validator (node) {
4747
tasks.unshift(done => checkExists(wrapper, done))
4848
}
4949

50-
async.series(tasks, async function (err) {
50+
async.series(tasks, function (err) {
5151
if (err) return cb(err)
52-
53-
try {
54-
await checkPrev(wrapper)
55-
} catch (err2) {
56-
return cb(err2)
57-
}
58-
59-
checkAuthentic(wrapper, cb)
52+
checkMore(wrapper, cb)
53+
// try {
54+
// checkPrev(wrapper, cb)
55+
// } catch (err2) {
56+
// return cb(err2)
57+
// }
58+
59+
// checkAuthentic(wrapper, cb)
6060
})
6161
}
6262

63-
// function validate (wrapper, cb) {
64-
// utils.addLinks(wrapper)
65-
66-
// const isMsg = wrapper.object[TYPE] === MESSAGE_TYPE
67-
// node.objects.get(wrapper.link, function (err) {
68-
// if (!err && isMsg) {
69-
// return cb(new errors.ObjectExists({ link: wrapper.link }))
70-
// }
71-
72-
// // utils.extend(node, wrapper, opts)
73-
// utils.loadBG(node, wrapper, function (err) {
74-
// if (err) return cb(err)
75-
76-
// const object = wrapper.object
77-
// const sig = object[SIG]
78-
// const link = wrapper.link
79-
// let signingKey
80-
// try {
81-
// signingKey = utils.getSigPubKey(object)
82-
// } catch (err) {
83-
// }
84-
85-
// if (!signingKey) return cb(new errors.InvalidSignature({ sig }))
86-
87-
// if (!utils.hasPubKey(wrapper.author.object, signingKey)) {
88-
// return cb(new errors.Author({
89-
// author: wrapper.author.link,
90-
// sig: sig
91-
// }))
92-
// }
93-
94-
// if (isMsg) {
95-
// try {
96-
// // TODO: msg.prev
97-
// protocol.validateMessage({ object })
98-
// } catch (err) {
99-
// return cb(err)
100-
// }
101-
// }
102-
103-
// cb()
104-
// })
105-
// })
106-
// }
107-
10863
function checkExists (wrapper, cb) {
10964
node.objects.get({ link: wrapper.link, body: false }, function (err) {
11065
cb(err ? null : new errors.ObjectExists({ link: wrapper.link }))
11166
})
11267
}
11368

114-
async function checkPrev (wrapper) {
69+
// function checkPrev (wrapper, cb) {
70+
function checkMore (wrapper, cb) {
11571
const object = wrapper.object
11672
const prev = wrapper.prev
11773
// we may not have the previous version in our db
118-
if (!object[PREVLINK] || !prev) return
119-
120-
if (prev.author === wrapper.author.permalink)
74+
if (!object[PREVLINK] ||
75+
!prev ||
76+
prev.author === wrapper.author.permalink) {
77+
checkAuthentic(wrapper, cb)
12178
return
122-
79+
}
12380
debugger
12481
// Check if the object was submitted from user's another device
125-
let { _masterAuthor } = object
126-
if (!_masterAuthor)
127-
_masterAuthor = object._author
128-
let otherAuthor
129-
try {
130-
let masterIdentity = await node.addressBook.lookupIdentity(_masterAuthor, () => {})
131-
132-
if (masterIdentity) {
133-
masterIdentity = masterIdentity.object
134-
if (masterIdentity[PERMALINK] === prev.author)
135-
otherAuthor = masterIdentity
136-
else
137-
otherAuthor = masterIdentity.pubkeys.find(pub => pub.importedFrom === prev.author)
82+
let _masterAuthor = object._masterAuthor || object._author
83+
node.addressBook.lookupIdentity(_masterAuthor, (err, identity) => {
84+
if (err || !identity) return cb(new errors.UnknownIdentity({
85+
error: 'prev version has a different author',
86+
value: {permalink: _masterAuthor}
87+
}))
88+
try {
89+
checkPrev(identity.object, object, prev)
90+
} catch (errPrev) {
91+
return cb(errPrev)
13892
}
139-
} catch(err) {
140-
console.log(`Not found identity ${_masterAuthor}`)
141-
debugger
142-
}
143-
if (!otherAuthor) {
144-
throw new errors.InvalidVersion({
145-
error: 'prev version has a different author'
146-
})
147-
}
14893

94+
checkAuthentic(wrapper, cb)
95+
})
96+
}
97+
98+
function checkPrev(masterIdentity, object, prev) {
99+
if (masterIdentity[PERMALINK] === prev.author)
100+
otherAuthor = masterIdentity
101+
else {
102+
otherAuthor = masterIdentity.pubkeys.find(pub => pub.importedFrom === prev.author)
103+
if (!otherAuthor) {
104+
throw new errors.InvalidVersion({
105+
error: 'prev version has a different author'
106+
})
107+
}
108+
}
149109
try {
150110
protocol.validateVersioning({
151111
object,
@@ -164,7 +124,6 @@ module.exports = function validator (node) {
164124
})
165125
}
166126
}
167-
168127
function checkAuthentic (wrapper, cb) {
169128
const { object, link, author } = wrapper
170129
const sig = object[SIG]
@@ -195,3 +154,48 @@ module.exports = function validator (node) {
195154
})
196155
}
197156
}
157+
// function validate (wrapper, cb) {
158+
// utils.addLinks(wrapper)
159+
160+
// const isMsg = wrapper.object[TYPE] === MESSAGE_TYPE
161+
// node.objects.get(wrapper.link, function (err) {
162+
// if (!err && isMsg) {
163+
// return cb(new errors.ObjectExists({ link: wrapper.link }))
164+
// }
165+
166+
// // utils.extend(node, wrapper, opts)
167+
// utils.loadBG(node, wrapper, function (err) {
168+
// if (err) return cb(err)
169+
170+
// const object = wrapper.object
171+
// const sig = object[SIG]
172+
// const link = wrapper.link
173+
// let signingKey
174+
// try {
175+
// signingKey = utils.getSigPubKey(object)
176+
// } catch (err) {
177+
// }
178+
179+
// if (!signingKey) return cb(new errors.InvalidSignature({ sig }))
180+
181+
// if (!utils.hasPubKey(wrapper.author.object, signingKey)) {
182+
// return cb(new errors.Author({
183+
// author: wrapper.author.link,
184+
// sig: sig
185+
// }))
186+
// }
187+
188+
// if (isMsg) {
189+
// try {
190+
// // TODO: msg.prev
191+
// protocol.validateMessage({ object })
192+
// } catch (err) {
193+
// return cb(err)
194+
// }
195+
// }
196+
197+
// cb()
198+
// })
199+
// })
200+
// }
201+

0 commit comments

Comments
 (0)