Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CryptoPassGitClient = require('./git');
  2. const CryptoPassIPFSClient = require('./ipfs');
  3. const FSE = require("fs-extra"),
  4.       FS = require("fs"),
  5.       Path = require("path"),
  6.       Shell = require('shelljs');
  7.  
  8. const homedir = require('os').homedir();
  9.  
  10. const defaultPWStore = homedir + "/.password-store/";
  11.  
  12. const defaultPubKey = defaultPWStore + "pubkey"
  13. const defaultPrivKey = defaultPWStore + "privkey"
  14.  
  15. const defaultIPFSOpts = {
  16.   "passwordStore": defaultPWStore
  17. };
  18. const defaultGitOpts = {
  19.   "remoteUrl": "",
  20.   "repoDir": defaultPWStore,
  21.   "username": "",
  22.   "password": "",
  23.   "pubKeyLoc": defaultPubKey,
  24.   "privKeyLoc": defaultPrivKey
  25. }
  26.  
  27. class CryptoPassClient {
  28.  
  29.   /**
  30.    * Creates an instance of CryptoPassClient
  31.    *
  32.    * @param {Object} [ipfsOpts={}]
  33.    * @param {Object} [gitOpts={}]
  34.    */
  35.   constructor (ipfsOpts = defaultIPFSOpts, gitOpts = defaultGitOpts) {
  36.  
  37.     if (gitOpts.remoteUrl === "") throw "You haven't specified a remote repository!";
  38.  
  39.     this.passwordStore = ipfsOpts.passwordStore;
  40.     this.repoDir = gitOpts.repoDir;
  41.     this.ipfsOpts = ipfsOpts;
  42.     this.gitOpts = gitOpts;
  43.  
  44.     this.ipfs = new CryptoPassIPFSClient(this.ipfsOpts);
  45.     this.git = new CryptoPassGitClient(this.gitOpts);
  46.  
  47.  
  48.     this.ipns;
  49.  
  50.   }
  51.  
  52.   /**
  53.    * Initializes the APIs we're using
  54.    *
  55.    * @returns {Promise}
  56.    */
  57.   async init () {
  58.  
  59.     return new Promise(function(resolve, reject) {
  60.  
  61.       this._CryptoPassSanityCheck()
  62.         .then(data => { return this.ipfs.init(); })
  63.         .then(data => { return this.git.init(); })
  64.         .catch(e => { reject(e) });
  65.  
  66.         resolve();
  67.  
  68.     });
  69.   }
  70.  
  71.   /**
  72.    * Initializes the APIs we're using
  73.    *
  74.    * @param {Object} password_json - JSON with username, password, URL, and any attributes you wanna save
  75.    * @returns {Promise}
  76.    */
  77.   async addPassword (password_json) {
  78.  
  79.     var username = password_json.login;
  80.     var passHash = password_json.passwordHash;
  81.     var urlDir = homedir + this.passwordStore + password_json.url;
  82.     var userFd = urlDir + "/user";
  83.     var passFd = urlDir + "/password";
  84.     var jsonFd = urlDir + "/json";
  85.  
  86.     var fdArray = [
  87.       [passFd, passHash],
  88.       [userFd, username],
  89.       [jsonFd, password_json]
  90.     ];
  91.  
  92.     return new Promise(function(resolve, reject) {
  93.  
  94.       FSE.ensureDir(urlDir)
  95.         .then(data => {
  96.           for(var i = 0; i < fdArray.length; i++) {
  97.  
  98.             FS.writeFile(fdArray[i][0], fdArray[i][1], function(e) {
  99.               if(e) reject(e);
  100.             });
  101.  
  102.           }
  103.         })
  104.         .catch(e => { reject(e) });
  105.  
  106.       this._CryptoPassUpdateIPFS()
  107.         .then(data => { resolve() })
  108.         .catch(e => { reject(e) });
  109.  
  110.     });
  111.   }
  112.  
  113.   /**
  114.    * Ensures we can access the filesystem and network
  115.    *
  116.    * @returns {Promise}
  117.    */
  118.   async _CryptoPassSanityCheck () {
  119.  
  120.     return new Promise(function(resolve, reject) {
  121.  
  122.       FSE.access(Path.resolve(homedir), FS.constants.W_OK)
  123.         .then(data => {
  124.  
  125.           Shell.exec("ping 8.8.8.8 -c 1", (code, out, err) => {
  126.  
  127.             if (code != 0) reject(err);
  128.             else resolve();
  129.  
  130.           });
  131.         })
  132.         .catch(e => { reject(e) });
  133.     });
  134.  
  135.   }
  136.  
  137.   /**
  138.    * Publishes zipped directory to IPNS
  139.    *
  140.    * @returns {Promise}
  141.    */
  142.   async _CryptoPassUpdateIPFS () {
  143.  
  144.     return new Promise(function(resolve, reject) {
  145.  
  146.       this.ipfs.CryptoPassUpdateIPFS()
  147.         .then((ipns_data, zipPath) => {
  148.           this.ipns = ipns_data;
  149.           return this.git.CryptoPassGitUploadZip(zipPath);
  150.         })
  151.         .then(data => { resolve() })
  152.         .catch(e => { reject(e) });
  153.  
  154.     });
  155.  
  156.   }
  157.  
  158.   getIPNS () { return this.ipns }
  159.   getGit () { return this.git }
  160.  
  161. }
  162.  
  163. module.exports = CryptoPassClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement