Last active
December 4, 2018 22:49
-
-
Save joaoaguiam/59c5914c76e86375b527164250dbbdfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const verifiers = require("./utils/verifiers"); | |
class VerifiedAccounts { | |
constructor(box) { | |
this._box = box; | |
this._did = box._muportDID.getDid(); | |
} | |
/** | |
* Internal function to prevent using the methods without having been initialized with a 3Box | |
*/ | |
_requireBox() { | |
if (!this._box) throw new Error("_requireBox: 3Box is not available"); | |
} | |
/** | |
* Internal method used to call the verification function which will then store the username for the verified | |
* account in the public profile for the user if the account is verified | |
* @param {sting} key - Account key to be stored in the public profile | |
* @param {string/object} proof - This param will be sent t the verification function and stored in the 3box | |
* profile for future verification | |
* @param {function} verificationFunction - Function receiving the user DID and the proof received as param. | |
* This function should return the username to be stored in the 3box profile on the .data entry of the returned object. | |
*/ | |
async _addVerifiedPublicAccount(key, proof, verificationFunction) { | |
this._requireBox(); | |
const verifiedResult = await verificationFunction(this._did, proof); | |
if (verifiedResult.error) { | |
return verifiedResult; | |
} | |
const githubUsername = verifiedResult.data; | |
this._box.public.set("verified_" + key, githubUsername); | |
this._box.public.set("proof_" + key, githubUsername); | |
return; | |
} | |
/** | |
* Internal method to retrieve the verified value for a given key. It will verifiy if the proof is still valid | |
* @param {sting} key - Account key to be retireved from the public profile | |
* @param {function} verificationFunction - Function receiving the user DID and the proof received as param. | |
* This function should return the username of the user that will be compared to the value stored in the 3box profile. | |
*/ | |
async _getVerifiedPublicAccount(key, verificationFunction) { | |
this._requireBox(); | |
const proof = this._box.public.get("proof_" + key); | |
const verifiedAccount = this._box.public.get("verified_" + key); | |
const verifiedResult = await verificationFunction(this._did, proof); | |
if (verifiedAccount !== verifiedResult.data) { | |
throw new Error( | |
`_getVerifiedPublicAccount: ${key} account stored on the 3Box profile is no longer verified with the proof stored on 3Box` | |
); | |
} | |
return verifiedAccount; | |
} | |
// Github related methods | |
/** | |
* Will get the username from the gist file passed as param and store it on the 3Box profile | |
* @param {string} gistUrl - URL for the gist file containing the DID of the user | |
*/ | |
async addGithub(gistUrl) { | |
return this._addVerifiedPublicAccount("github", gistUrl, verifiers.verifyGithub); | |
} | |
/** | |
* Will return the verified github account. If the proof stored on the 3Box profile no longer contains the DID of the user, | |
* this method will throw. | |
*/ | |
async github() { | |
return await this._getVerifiedPublicAccount("github", verifiers.verifyGithub); | |
} | |
} | |
module.exports = VerifiedAccounts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment