|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug') |
| 4 | +const promisify = require('promisify-es6') |
| 5 | +const waterfall = require('async/waterfall') |
| 6 | +const parallel = require('async/parallel') |
| 7 | +const human = require('human-to-milliseconds') |
| 8 | +const crypto = require('libp2p-crypto') |
| 9 | +const errcode = require('err-code') |
| 10 | + |
| 11 | +const log = debug('jsipfs:name') |
| 12 | +log.error = debug('jsipfs:name:error') |
| 13 | + |
| 14 | +const utils = require('../utils') |
| 15 | +const path = require('../ipns/path') |
| 16 | + |
| 17 | +const keyLookup = (ipfsNode, kname, callback) => { |
| 18 | + if (kname === 'self') { |
| 19 | + return callback(null, ipfsNode._peerInfo.id.privKey) |
| 20 | + } |
| 21 | + |
| 22 | + const pass = ipfsNode._options.pass |
| 23 | + |
| 24 | + waterfall([ |
| 25 | + (cb) => ipfsNode._keychain.exportKey(kname, pass, cb), |
| 26 | + (pem, cb) => crypto.keys.import(pem, pass, cb) |
| 27 | + ], (err, privateKey) => { |
| 28 | + if (err) { |
| 29 | + log.error(err) |
| 30 | + return callback(errcode(err, 'ERR_CANNOT_GET_KEY')) |
| 31 | + } |
| 32 | + |
| 33 | + return callback(null, privateKey) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +module.exports = function name (self) { |
| 38 | + return { |
| 39 | + /** |
| 40 | + * IPNS is a PKI namespace, where names are the hashes of public keys, and |
| 41 | + * the private key enables publishing new (signed) values. In both publish |
| 42 | + * and resolve, the default name used is the node's own PeerID, |
| 43 | + * which is the hash of its public key. |
| 44 | + * |
| 45 | + * @param {String} value ipfs path of the object to be published. |
| 46 | + * @param {Object} options ipfs publish options. |
| 47 | + * @param {boolean} options.resolve resolve given path before publishing. |
| 48 | + * @param {String} options.lifetime time duration that the record will be valid for. |
| 49 | + This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are |
| 50 | + "ns", "ms", "s", "m", "h". Default is 24h. |
| 51 | + * @param {String} options.ttl time duration this record should be cached for (NOT IMPLEMENTED YET). |
| 52 | + * This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are |
| 53 | + "ns", "ms", "s", "m", "h" (caution: experimental). |
| 54 | + * @param {String} options.key name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. |
| 55 | + * @param {function(Error)} [callback] |
| 56 | + * @returns {Promise|void} |
| 57 | + */ |
| 58 | + publish: promisify((value, options, callback) => { |
| 59 | + if (typeof options === 'function') { |
| 60 | + callback = options |
| 61 | + options = {} |
| 62 | + } |
| 63 | + |
| 64 | + options = options || {} |
| 65 | + const resolve = !(options.resolve === false) |
| 66 | + const lifetime = options.lifetime || '24h' |
| 67 | + const key = options.key || 'self' |
| 68 | + |
| 69 | + if (!self.isOnline()) { |
| 70 | + const errMsg = utils.OFFLINE_ERROR |
| 71 | + |
| 72 | + log.error(errMsg) |
| 73 | + return callback(errcode(errMsg, 'OFFLINE_ERROR')) |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: params related logic should be in the core implementation |
| 77 | + |
| 78 | + // Normalize path value |
| 79 | + try { |
| 80 | + value = utils.normalizePath(value) |
| 81 | + } catch (err) { |
| 82 | + log.error(err) |
| 83 | + return callback(err) |
| 84 | + } |
| 85 | + |
| 86 | + parallel([ |
| 87 | + (cb) => human(lifetime, cb), |
| 88 | + // (cb) => ttl ? human(ttl, cb) : cb(), |
| 89 | + (cb) => keyLookup(self, key, cb), |
| 90 | + // verify if the path exists, if not, an error will stop the execution |
| 91 | + (cb) => resolve.toString() === 'true' ? path.resolvePath(self, value, cb) : cb() |
| 92 | + ], (err, results) => { |
| 93 | + if (err) { |
| 94 | + log.error(err) |
| 95 | + return callback(err) |
| 96 | + } |
| 97 | + |
| 98 | + // Calculate lifetime with nanoseconds precision |
| 99 | + const pubLifetime = results[0].toFixed(6) |
| 100 | + const privateKey = results[1] |
| 101 | + |
| 102 | + // TODO IMPROVEMENT - Handle ttl for cache |
| 103 | + // const ttl = results[1] |
| 104 | + // const privateKey = results[2] |
| 105 | + |
| 106 | + // Start publishing process |
| 107 | + self._ipns.publish(privateKey, value, pubLifetime, callback) |
| 108 | + }) |
| 109 | + }), |
| 110 | + |
| 111 | + /** |
| 112 | + * Given a key, query the DHT for its best value. |
| 113 | + * |
| 114 | + * @param {String} name ipns name to resolve. Defaults to your node's peerID. |
| 115 | + * @param {Object} options ipfs resolve options. |
| 116 | + * @param {boolean} options.nocache do not use cached entries. |
| 117 | + * @param {boolean} options.recursive resolve until the result is not an IPNS name. |
| 118 | + * @param {function(Error)} [callback] |
| 119 | + * @returns {Promise|void} |
| 120 | + */ |
| 121 | + resolve: promisify((name, options, callback) => { |
| 122 | + if (typeof options === 'function') { |
| 123 | + callback = options |
| 124 | + options = {} |
| 125 | + } |
| 126 | + |
| 127 | + options = options || {} |
| 128 | + const nocache = options.nocache && options.nocache.toString() === 'true' |
| 129 | + const recursive = options.recursive && options.recursive.toString() === 'true' |
| 130 | + |
| 131 | + const local = true // TODO ROUTING - use self._options.local |
| 132 | + |
| 133 | + if (!self.isOnline() && !local) { |
| 134 | + const errMsg = utils.OFFLINE_ERROR |
| 135 | + |
| 136 | + log.error(errMsg) |
| 137 | + return callback(errcode(errMsg, 'OFFLINE_ERROR')) |
| 138 | + } |
| 139 | + |
| 140 | + // TODO: params related logic should be in the core implementation |
| 141 | + |
| 142 | + if (local && nocache) { |
| 143 | + const error = 'cannot specify both local and nocache' |
| 144 | + |
| 145 | + log.error(error) |
| 146 | + return callback(errcode(new Error(error), 'ERR_NOCACHE_AND_LOCAL')) |
| 147 | + } |
| 148 | + |
| 149 | + // Set node id as name for being resolved, if it is not received |
| 150 | + if (!name) { |
| 151 | + name = self._peerInfo.id.toB58String() |
| 152 | + } |
| 153 | + |
| 154 | + if (!name.startsWith('/ipns/')) { |
| 155 | + name = `/ipns/${name}` |
| 156 | + } |
| 157 | + |
| 158 | + const resolveOptions = { |
| 159 | + nocache, |
| 160 | + recursive, |
| 161 | + local |
| 162 | + } |
| 163 | + |
| 164 | + self._ipns.resolve(name, self._peerInfo.id, resolveOptions, callback) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments