refactor writeLoginBlock to account for more asynchronous flow

pull/1/head
ansuz 2021-04-27 17:13:31 +05:30
parent 2436cd2b2f
commit 8896391536
1 changed files with 72 additions and 35 deletions

View File

@ -86,35 +86,76 @@ var createLoginBlockPath = function (Env, publicKey) { // FIXME BLOCKS
return Path.join(Env.paths.block, safeKey.slice(0, 2), safeKey); return Path.join(Env.paths.block, safeKey.slice(0, 2), safeKey);
}; };
var validateAncestorProof = function (Env, proof, newPubKey, cb) {
/* prove that you own an existing block by signing for its publicKey
we will need:
1. the publicKey
2. for the old key's block to exist
* path = createLoginBlockPath(Env, oldPublicKey)
* path && FS.readFile(path, err => { !err })
3. a message signed with that publicKey
*/
cb("E_RESTRICTED");
};
Block.writeLoginBlock = function (Env, safeKey, msg, _cb) { // FIXME BLOCKS Block.writeLoginBlock = function (Env, safeKey, msg, _cb) { // FIXME BLOCKS
var cb = Util.once(Util.mkAsync(_cb)); var cb = Util.once(Util.mkAsync(_cb));
//console.log(msg); //console.log(msg);
var publicKey = msg[0]; var publicKey = msg[0];
var signature = msg[1]; var signature = msg[1];
var block = msg[2]; var block = msg[2];
var registrationProof = msg[3];
if (Env.restrictRegistration /* && notAlreadyRegistered */) { // XXX restricted-registration var validatedBlock, parsed, path;
return void cb("E_RESTRICTED"); nThen(function (w) {
if (!Env.restrictRegistration) { return; }
if (!registrationProof) {
// we allow users with existing blocks to create new ones
// call back with error if registration is restricted and no proof of an existing block was provided
w.abort();
return cb("E_RESTRICTED");
} }
validateLoginBlock(Env, publicKey, signature, block, function (e, validatedBlock) { // TODO check that the provided proof was valid
if (e) { return void cb(e); } // XXX restricted-registration check whether proof of an existing block was provided
if (!(validatedBlock instanceof Uint8Array)) { return void cb('E_INVALID_BLOCK'); } validateAncestorProof(Env, void 0, w(function (err) {
if (err) {
w.abort();
cb(err);
}
}));
}).nThen(function (w) {
validateLoginBlock(Env, publicKey, signature, block, w(function (e, _validatedBlock) {
if (e) {
w.abort();
return void cb(e);
}
if (!(_validatedBlock instanceof Uint8Array)) {
w.abort();
return void cb('E_INVALID_BLOCK');
}
validatedBlock = _validatedBlock;
// derive the filepath // derive the filepath
var path = createLoginBlockPath(Env, publicKey); path = createLoginBlockPath(Env, publicKey);
// make sure the path is valid // make sure the path is valid
if (typeof(path) !== 'string') { if (typeof(path) !== 'string') {
return void cb('E_INVALID_BLOCK_PATH'); return void cb('E_INVALID_BLOCK_PATH');
} }
var parsed = Path.parse(path); parsed = Path.parse(path);
if (!parsed || typeof(parsed.dir) !== 'string') { if (!parsed || typeof(parsed.dir) !== 'string') {
w.abort();
return void cb("E_INVALID_BLOCK_PATH_2"); return void cb("E_INVALID_BLOCK_PATH_2");
} }
}));
nThen(function (w) { }).nThen(function (w) {
// make sure the path to the file exists // make sure the path to the file exists
Fse.mkdirp(parsed.dir, w(function (e) { Fse.mkdirp(parsed.dir, w(function (e) {
if (e) { if (e) {
@ -124,16 +165,12 @@ Block.writeLoginBlock = function (Env, safeKey, msg, _cb) { // FIXME BLOCKS
})); }));
}).nThen(function () { }).nThen(function () {
// actually write the block // actually write the block
// flow is dumb and I need to guard against this which will never happen
/*:: if (typeof(validatedBlock) === 'undefined') { throw new Error('should never happen'); } */
/*:: if (typeof(path) === 'undefined') { throw new Error('should never happen'); } */
Fs.writeFile(path, Buffer.from(validatedBlock), { encoding: "binary", }, function (err) { Fs.writeFile(path, Buffer.from(validatedBlock), { encoding: "binary", }, function (err) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
// XXX log the safeKey to map publicKey <=> block
cb(); cb();
}); });
}); });
});
}; };
/* /*