Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CryptoPassGitClient = require('./git');
- const CryptoPassIPFSClient = require('./ipfs');
- const FSE = require("fs-extra"),
- FS = require("fs"),
- Path = require("path"),
- Shell = require('shelljs');
- const homedir = require('os').homedir();
- const defaultPWStore = homedir + "/.password-store/";
- const defaultPubKey = defaultPWStore + "pubkey"
- const defaultPrivKey = defaultPWStore + "privkey"
- const defaultIPFSOpts = {
- "passwordStore": defaultPWStore
- };
- const defaultGitOpts = {
- "remoteUrl": "",
- "repoDir": defaultPWStore,
- "username": "",
- "password": "",
- "pubKeyLoc": defaultPubKey,
- "privKeyLoc": defaultPrivKey
- }
- class CryptoPassClient {
- /**
- * Creates an instance of CryptoPassClient
- *
- * @param {Object} [ipfsOpts={}]
- * @param {Object} [gitOpts={}]
- */
- constructor (ipfsOpts = defaultIPFSOpts, gitOpts = defaultGitOpts) {
- if (gitOpts.remoteUrl === "") throw "You haven't specified a remote repository!";
- this.passwordStore = ipfsOpts.passwordStore;
- this.repoDir = gitOpts.repoDir;
- this.ipfsOpts = ipfsOpts;
- this.gitOpts = gitOpts;
- this.ipfs = new CryptoPassIPFSClient(this.ipfsOpts);
- this.git = new CryptoPassGitClient(this.gitOpts);
- this.ipns;
- }
- /**
- * Initializes the APIs we're using
- *
- * @returns {Promise}
- */
- async init () {
- return new Promise(function(resolve, reject) {
- this._CryptoPassSanityCheck()
- .then(data => { return this.ipfs.init(); })
- .then(data => { return this.git.init(); })
- .catch(e => { reject(e) });
- resolve();
- });
- }
- /**
- * Initializes the APIs we're using
- *
- * @param {Object} password_json - JSON with username, password, URL, and any attributes you wanna save
- * @returns {Promise}
- */
- async addPassword (password_json) {
- var username = password_json.login;
- var passHash = password_json.passwordHash;
- var urlDir = homedir + this.passwordStore + password_json.url;
- var userFd = urlDir + "/user";
- var passFd = urlDir + "/password";
- var jsonFd = urlDir + "/json";
- var fdArray = [
- [passFd, passHash],
- [userFd, username],
- [jsonFd, password_json]
- ];
- return new Promise(function(resolve, reject) {
- FSE.ensureDir(urlDir)
- .then(data => {
- for(var i = 0; i < fdArray.length; i++) {
- FS.writeFile(fdArray[i][0], fdArray[i][1], function(e) {
- if(e) reject(e);
- });
- }
- })
- .catch(e => { reject(e) });
- this._CryptoPassUpdateIPFS()
- .then(data => { resolve() })
- .catch(e => { reject(e) });
- });
- }
- /**
- * Ensures we can access the filesystem and network
- *
- * @returns {Promise}
- */
- async _CryptoPassSanityCheck () {
- return new Promise(function(resolve, reject) {
- FSE.access(Path.resolve(homedir), FS.constants.W_OK)
- .then(data => {
- Shell.exec("ping 8.8.8.8 -c 1", (code, out, err) => {
- if (code != 0) reject(err);
- else resolve();
- });
- })
- .catch(e => { reject(e) });
- });
- }
- /**
- * Publishes zipped directory to IPNS
- *
- * @returns {Promise}
- */
- async _CryptoPassUpdateIPFS () {
- return new Promise(function(resolve, reject) {
- this.ipfs.CryptoPassUpdateIPFS()
- .then((ipns_data, zipPath) => {
- this.ipns = ipns_data;
- return this.git.CryptoPassGitUploadZip(zipPath);
- })
- .then(data => { resolve() })
- .catch(e => { reject(e) });
- });
- }
- getIPNS () { return this.ipns }
- getGit () { return this.git }
- }
- module.exports = CryptoPassClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement