From 53e7b951745541dc57723f36ec31285e64be6e0a Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 19 Jun 2018 16:38:02 +0100 Subject: [PATCH] wip --- package.json | 2 +- src/cli/commands/name/publish.js | 2 +- src/core/components/name.js | 114 ++++++---------- src/core/index.js | 4 +- src/core/{namesys => ipns}/index.js | 23 ++-- src/core/ipns/path.js | 71 ++++++++++ src/core/{namesys => ipns}/pb/ipns.proto.js | 0 src/core/{namesys => ipns}/pb/ipnsEntry.js | 15 ++- src/core/ipns/publisher.js | 140 ++++++++++++++++++++ src/core/ipns/resolver.js | 66 +++++++++ src/core/ipns/utils.js | 15 +++ src/core/ipns/validator.js | 50 +++++++ src/core/namesys/publisher.js | 138 ------------------- src/core/namesys/resolver.js | 53 -------- src/core/namesys/utils.js | 9 -- src/http/api/resources/name.js | 12 +- src/http/api/routes/name.js | 3 +- 17 files changed, 418 insertions(+), 299 deletions(-) rename src/core/{namesys => ipns}/index.js (68%) create mode 100644 src/core/ipns/path.js rename src/core/{namesys => ipns}/pb/ipns.proto.js (100%) rename src/core/{namesys => ipns}/pb/ipnsEntry.js (70%) create mode 100644 src/core/ipns/publisher.js create mode 100644 src/core/ipns/resolver.js create mode 100644 src/core/ipns/utils.js create mode 100644 src/core/ipns/validator.js delete mode 100644 src/core/namesys/publisher.js delete mode 100644 src/core/namesys/resolver.js delete mode 100644 src/core/namesys/utils.js diff --git a/package.json b/package.json index 2c4a72567b..8a11cfdc9c 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ }, "dependencies": { "async": "^2.6.0", + "base32-encode": "^1.0.0", "big.js": "^5.0.3", "binary-querystring": "~0.1.2", "bl": "^1.2.2", @@ -164,7 +165,6 @@ "pull-stream": "^3.6.7", "pull-stream-to-stream": "^1.3.4", "pull-zip": "^2.0.1", - "quick-lru": "^1.1.0", "read-pkg-up": "^3.0.0", "readable-stream": "2.3.6", "stream-to-pull-stream": "^1.7.2", diff --git a/src/cli/commands/name/publish.js b/src/cli/commands/name/publish.js index 860e62c49b..c725eb5827 100644 --- a/src/cli/commands/name/publish.js +++ b/src/cli/commands/name/publish.js @@ -19,7 +19,7 @@ module.exports = { throw err } - print(`Published to ${result.value}: /ipfs/${result.name}`) + print(`Published to ${result.name}: ${result.value}`) }) } } diff --git a/src/core/components/name.js b/src/core/components/name.js index 489fe9aa59..abbe9042f8 100644 --- a/src/core/components/name.js +++ b/src/core/components/name.js @@ -1,8 +1,11 @@ 'use strict' const promisify = require('promisify-es6') -const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR +const series = require('async/series') const human = require('human-to-milliseconds') +const path = require('../ipns/path') + +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR const keyLookup = (ipfsNode, kname, cb) => { if (kname === 'self') { @@ -18,29 +21,6 @@ const keyLookup = (ipfsNode, kname, cb) => { }) } -const publish = (ipfsNode, privateKey, ipfsPath, publishOptions, callback) => { - // Should verify if exists ? - if (publishOptions.verifyIfExists) { - // TODO resolve - // https://github.com/ipfs/go-ipfs/blob/master/core/commands/publish.go#L172 - } - - // Add pubValidTime - - // Publish - const eol = new Date(Date.now()) - - ipfsNode._namesys.publishWithEOL(privateKey, ipfsPath, eol, (err, res) => { - if (err) { - callback(err) - } - - // TODO HERE HERE HERE - - callback(null, res) - }) -} - module.exports = function name (self) { return { /** @@ -51,64 +31,55 @@ module.exports = function name (self) { * * Examples: TODO such as in go * - * @param {String} ipfsPath - * @param {Object} options + * @param {String} value ipfs path of the object to be published. + * @param {boolean} resolve resolve given path before publishing. + * @param {String} lifetime time duration that the record will be valid for. + This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are + "ns", "us" (or "µs"), "ms", "s", "m", "h". + * @param {String} ttl time duration this record should be cached for (caution: experimental). + * @param {String} key name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'. * @param {function(Error)} [callback] * @returns {Promise|void} */ - publish: promisify((ipfsPath, callback) => { - // https://github.com/ipfs/go-ipfs/blob/master/core/commands/publish.go + publish: promisify((value, resolve = true, lifetime = '24h', ttl, key = 'self', callback) => { if (!self.isOnline()) { return callback(new Error(OFFLINE_ERROR)) } - // TODO Validate Mounts IPNS - cannot manually publish while IPNS is mounted - - // TODO Validate Node identity not validated - - // TODO Parse options and create object - const options = { - resolve: true, - d: '24h', - ttl: undefined, - key: 'self' + // Parse ipfs path value + try { + value = path.parsePath(value) + } catch (err) { + return callback(err) } - // TODO Create waterfall - /* waterfall([ - (cb) => human(options.d || '1s', cb), - ], callback) */ - - human(options.d || '1s', (err, value) => { + series([ + (cb) => human(lifetime || '1s', cb), + // (cb) => ttl ? human(ttl, cb) : cb(), + (cb) => keyLookup(self, key, cb), + (cb) => resolve ? path.resolvePath(self, value, cb) : cb() // if not resolved, and error will stop the execution + ], (err, results) => { if (err) { - return callback(new Error('Error parsing lifetime option')) + return callback(err) } - const publishOptions = { - verifyIfExists: options.resolve, - pubValidTime: value - } + const pubValidTime = results[0] + const privateKey = results[1] - // TODO Date.now() + value + // TODO IMPROVEMENT - Handle ttl for cache + // const ttl = results[1] + // const privateKey = results[2] - // TODO TTL integration + // Calculate eol + const eol = new Date(Date.now() + pubValidTime) - // Get Key - keyLookup(self, options.key, (err, key) => { + // Start publishing process + self._ipns.publish(privateKey, value, eol, (err, res) => { if (err) { - return callback(err) + callback(err) } - // TODO ParsePath - // https://github.com/ipfs/go-ipfs/blob/master/path/path.go - - publish(self, key, ipfsPath, publishOptions, (err, result) => { - if (err) { - callback(err) - } - - return callback(null, result) - }) + callback(null, res) }) }) }), @@ -125,17 +96,10 @@ module.exports = function name (self) { resolve: promisify((name, nocache, recursive, callback) => { const local = true - if (typeof name === 'function') { - callback = name - name = undefined - } - if (!self.isOnline()) { return callback(new Error(OFFLINE_ERROR)) } - // let resolver = self._namesys.ipnsResolver - if (local && nocache) { return callback(new Error('Cannot specify both local and nocache')) } @@ -149,9 +113,15 @@ module.exports = function name (self) { name = `/ipns/${name}` } + // TODO local public key? const pubKey = self._peerInfo.id.pubKey + const options = { + local: local, + nocache: nocache, + recursive: recursive + } - self._namesys.resolve(name, pubKey, (err, result) => { + self._ipns.resolve(name, pubKey, options, (err, result) => { if (err) { return callback(err) } diff --git a/src/core/index.js b/src/core/index.js index 8b871b79af..cc88fde571 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -20,7 +20,7 @@ const EventEmitter = require('events') const config = require('./config') const boot = require('./boot') const components = require('./components') -const Namesys = require('./namesys') +const IPNS = require('./ipns') // replaced by repo-browser when running in the browser const defaultRepo = require('./runtime/repo-nodejs') @@ -80,7 +80,7 @@ class IPFS extends EventEmitter { this._blockService = new BlockService(this._repo) this._ipld = new Ipld(this._blockService) this._pubsub = undefined - this._namesys = new Namesys(null, this._repo) + this._ipns = new IPNS(null, this._repo) // IPFS Core exposed components // - for booting up a node diff --git a/src/core/namesys/index.js b/src/core/ipns/index.js similarity index 68% rename from src/core/namesys/index.js rename to src/core/ipns/index.js index 770836b41c..34a376c469 100644 --- a/src/core/namesys/index.js +++ b/src/core/ipns/index.js @@ -6,10 +6,11 @@ const series = require('async/series') const IpnsPublisher = require('./publisher') const IpnsResolver = require('./resolver') +const path = require('./path') // const defaultRecordTtl = 60 * 1000 -class Namesys { +class IPNS { constructor (routing, repo, peerInfo) { this.ipnsPublisher = new IpnsPublisher(routing, repo) this.ipnsResolver = new IpnsResolver(repo) @@ -17,10 +18,8 @@ class Namesys { } // Resolve - resolve (name, pubKey, callback) { - // this.ipnsResolver.resolve() - - this.ipnsResolver.resolve(name, pubKey, (err, result) => { + resolve (name, pubKey, options, callback) { + this.ipnsResolver.resolve(name, pubKey, options, (err, result) => { if (err) { return callback(err) } @@ -29,13 +28,8 @@ class Namesys { }) } - // publish (value = ipfsPath) - publish (privKey, value) { - // TODO https://github.com/ipfs/go-ipfs/blob/master/namesys/namesys.go#L111 - } - - // publish with EOL (value = ipfsPath) - publishWithEOL (privKey, value, eol, callback) { + // Publish + publish (privKey, value, eol, callback) { series([ (cb) => peerId.createFromPrivKey(privKey.bytes.toString('base64'), cb), (cb) => this.ipnsPublisher.publishWithEOL(privKey, value, eol, cb) @@ -44,7 +38,7 @@ class Namesys { return callback(err) } - // TODO Add to cache + // TODO IMPROVEMENT - Add to cache // this.cache.set(id.toB58String(), { // val: value, // eol: Date.now() + ttl @@ -55,4 +49,5 @@ class Namesys { } } -exports = module.exports = Namesys +exports = module.exports = IPNS +exports.path = path diff --git a/src/core/ipns/path.js b/src/core/ipns/path.js new file mode 100644 index 0000000000..c06366d444 --- /dev/null +++ b/src/core/ipns/path.js @@ -0,0 +1,71 @@ +'use strict' + +const CID = require('cids') + +const BAD_PATH_ERROR = new Error('invalid \'ipfs ref\' path') +const NO_COMPONENTS_ERROR = new Error('path must contain at least one component') + +// Should verify if the value exists before publishing it +const resolvePath = (ipfsNode, value, callback) => { + if (value.startsWith('/ipns/')) { + // TODO resolve local? + // TODO Resolve from DHT + return callback(new Error('not implemented yet')) + } + + ipfsNode.dag.get(value.substring('/ipfs/'.length), (err, value) => { + if (err) { + return callback(err) + } + + return callback(null, value) + }) +} + +// parsePath returns a well-formed ipfs Path. +// The returned path will always be prefixed with /ipfs/ or /ipns/. +// If the received string is not a valid ipfs path, an error will be returned +const parsePath = (pathStr) => { + const parts = pathStr.split('/') + + if (parts.length === 1) { + return parseCidToPath(pathStr) + } + + // if the path does not begin with a slash, we expect this to start with a hash and be an ipfs path + if (parts[0] !== '') { + if (parseCidToPath(parts[0])) { + return `/ipfs/${pathStr}` + } + } + + if (parts.length < 3) { + throw BAD_PATH_ERROR + } + + if (parts[1] === 'ipfs') { + if (!parseCidToPath(parts[2])) { + throw BAD_PATH_ERROR + } + } else if (parts[1] !== 'ipns') { + throw BAD_PATH_ERROR + } + return pathStr +} + +// parseCidToPath takes a CID in string form and returns a valid ipfs Path. +const parseCidToPath = (value) => { + if (value === '') { + throw NO_COMPONENTS_ERROR + } + + const cid = new CID(value) + CID.validateCID(cid) + + return `/ipfs/${value}` +} + +module.exports = { + resolvePath: (ipfsNode, value, callback) => resolvePath(ipfsNode, value, callback), + parsePath: (pathStr) => parsePath(pathStr) +} diff --git a/src/core/namesys/pb/ipns.proto.js b/src/core/ipns/pb/ipns.proto.js similarity index 100% rename from src/core/namesys/pb/ipns.proto.js rename to src/core/ipns/pb/ipns.proto.js diff --git a/src/core/namesys/pb/ipnsEntry.js b/src/core/ipns/pb/ipnsEntry.js similarity index 70% rename from src/core/namesys/pb/ipnsEntry.js rename to src/core/ipns/pb/ipnsEntry.js index 0ea7c39ebe..b11afb0a32 100644 --- a/src/core/namesys/pb/ipnsEntry.js +++ b/src/core/ipns/pb/ipnsEntry.js @@ -10,7 +10,7 @@ module.exports = { validityType = ipnsEntryProto.ValidityType.EOL } - return { + const entry = { value: value, signature: signature, validityType: validityType, @@ -19,6 +19,16 @@ module.exports = { ttl: ttl, pubKey: pubKey } + + return Object.keys(entry).reduce((acc, key) => { + const reducedEntry = acc + + if (entry[key] !== undefined) { + reducedEntry[key] = entry[key] + } + + return reducedEntry + }, {}) }, // Marshal marshal: (ipnsEntry) => { @@ -27,5 +37,6 @@ module.exports = { // Unmarshal unmarshal: (marsheled) => { return ipnsEntryProto.decode(marsheled) - } + }, + validityType: ipnsEntryProto.ValidityType } diff --git a/src/core/ipns/publisher.js b/src/core/ipns/publisher.js new file mode 100644 index 0000000000..18be87177c --- /dev/null +++ b/src/core/ipns/publisher.js @@ -0,0 +1,140 @@ +'use strict' + +const peerId = require('peer-id') +const waterfall = require('async/waterfall') + +const IpnsEntry = require('./pb/ipnsEntry') +const utils = require('./utils') +const validator = require('./validator') + +const defaultRecordTtl = 60 * 60 * 1000 + +/* + IpnsPublisher is capable of publishing and resolving names to the IPFS + routing system. + */ +class IpnsPublisher { + constructor (routing, repo) { + this.routing = routing + this.repo = repo + } + + // publish record with a eol + publishWithEOL (privKey, value, eol, callback) { + this.updateOrCreateRecord(privKey, value, eol, (err, record) => { + if (err) { + return callback(err) + } + + // TODO ROUTING - Add record + callback(null, record) + }) + } + + // Accepts a keypair, as well as a value (ipfsPath), and publishes it out to the routing system + publish (privKey, value, callback) { + const eol = new Date(Date.now() + defaultRecordTtl) + + this.publishWithEOL(privKey, value, eol, (err, res) => { + if (err) { + return callback(err) + } + + callback(res) + }) + } + + // Returns the record this node has published corresponding to the given peer ID. + // If `checkRouting` is true and we have no existing record, this method will check the routing system for any existing records. + getPublished (peerId, checkRouting, callback) { + this.repo.datastore.get(utils.generateIpnsDsKey(peerId.id), (err, dsVal) => { + let result + + if (!err) { + if (Buffer.isBuffer(dsVal)) { + result = dsVal + } else { + return callback(new Error('found ipns record that we couldn\'t convert to a value')) + } + } else if (err.notFound) { + if (!checkRouting) { + return callback(null, { + peerId: peerId + }) + } + // TODO ROUTING + } else { + return callback(err) + } + + // unmarshal data + result = IpnsEntry.unmarshal(dsVal) + + return callback(null, { + peerId: peerId, + record: result + }) + }) + } + + createEntryRecord (privKey, value, seqNumber, eol, callback) { + const validity = eol.toISOString() + const validityType = IpnsEntry.validityType.EOL + const sequence = seqNumber + + validator.sign(privKey, value, validityType, validity, (err, signature) => { + if (err) { + return callback(err) + } + + // TODO confirm private key format compliance with go-ipfs + + // Create IPNS entry record + const ipnsEntry = IpnsEntry.create(value, signature, validityType, validity, sequence) + + return callback(null, ipnsEntry) + }) + } + + updateOrCreateRecord (privKey, value, eol, callback) { + waterfall([ + (cb) => peerId.createFromPrivKey(privKey.bytes.toString('base64'), cb), + (id, cb) => this.getPublished(id, false, cb) + ], (err, result) => { + if (err) { + callback(err) + } + + const { peerId, record } = result + + // Determinate the record sequence number + let seqNumber = 0 + if (record && record.sequence !== undefined) { + seqNumber = record.value.toString() !== value ? record.sequence + 1 : record.sequence + } + + // Create record + this.createEntryRecord(privKey, value, seqNumber, eol, (err, entryData) => { + if (err) { + return callback(err) + } + + // TODO IMPROVEMENT - set ttl (still experimental feature for go) + + // Marshal record + const data = IpnsEntry.marshal(entryData) + + // Store the new record + this.repo.datastore.put(utils.generateIpnsDsKey(peerId.id), data, (err, res) => { + if (err) { + return callback(err) + } + + return callback(null, entryData) + }) + }) + }) + } +} + +exports = module.exports = IpnsPublisher diff --git a/src/core/ipns/resolver.js b/src/core/ipns/resolver.js new file mode 100644 index 0000000000..3bde9b32ba --- /dev/null +++ b/src/core/ipns/resolver.js @@ -0,0 +1,66 @@ +'use strict' + +const IpnsEntry = require('./pb/ipnsEntry') +const utils = require('./utils') +const validator = require('./validator') + +const multihash = require('multihashes') + +class IpnsResolver { + constructor (repo) { + this.repo = repo + } + + resolve (name, publicKey, options, callback) { + const nameSegments = name.split('/') + + if (nameSegments.length !== 3 || nameSegments[0] !== '') { + return callback(new Error(`invalid name syntax for ${name}`)) + } + + const key = nameSegments[2] + + // TODO recursive + // TODO nocache + + if (options.local) { + this.resolveLocal(key, publicKey, (err, res) => { + if (err) { + return callback(err) + } + + return callback(null, res) + }) + } else { + return callback(new Error('not implemented yet')) + } + } + + // https://github.com/ipfs/go-ipfs-routing/blob/master/offline/offline.go + resolveLocal (name, publicKey, callback) { + const ipnsKey = utils.generateIpnsDsKey(multihash.fromB58String(name)) + + this.repo.datastore.get(ipnsKey, (err, dsVal) => { + if (err) { + return callback(err) + } + + if (!Buffer.isBuffer(dsVal)) { + return callback(new Error('found ipns record that we couldn\'t convert to a value')) + } + + const ipnsEntry = IpnsEntry.unmarshal(dsVal) + + // Record validation + validator.verify(publicKey, ipnsEntry, (err) => { + if (err) { + return callback(err) + } + + return callback(null, ipnsEntry.value.toString()) + }) + }) + } +} + +exports = module.exports = IpnsResolver diff --git a/src/core/ipns/utils.js b/src/core/ipns/utils.js new file mode 100644 index 0000000000..9664a38335 --- /dev/null +++ b/src/core/ipns/utils.js @@ -0,0 +1,15 @@ +'use strict' + +const base32Encode = require('base32-encode') + +// rawStdEncoding as go +// Created issue for allowing this inside base32-encode https://github.com/LinusU/base32-encode/issues/2 +const rawStdEncoding = (key) => base32Encode(key, 'RFC4648').replace('=', '') + +module.exports = { + generateIpnsDsKey: (key) => `/ipns/${rawStdEncoding(key)}`, + buildIpnsKeysForId: (peerId) => ({ + nameKey: `/pk/${peerId.toB58String()}`, + ipnsKey: `/ipns/${peerId.toB58String()}` + }) +} diff --git a/src/core/ipns/validator.js b/src/core/ipns/validator.js new file mode 100644 index 0000000000..952a9f1a18 --- /dev/null +++ b/src/core/ipns/validator.js @@ -0,0 +1,50 @@ +'use strict' + +// Create IPNS Entry data for being signed +const getDataForSignature = (value, validityType, validity) => { + const valueBuffer = Buffer.from(value) + const validityTypeBuffer = Buffer.from(validityType.toString()) + const validityBuffer = Buffer.from(validity) + + return Buffer.concat([valueBuffer, validityTypeBuffer, validityBuffer]) +} + +// Sign IPNS Entry for publish +const sign = (privateKey, value, validityType, validity, callback) => { + const dataForSignature = getDataForSignature(value, validityType, validity) + + privateKey.sign(dataForSignature, (err, signature) => { + if (err) { + return callback(err) + } + return callback(null, signature) + }) +} + +// Verify IPNS entry on resolve +const verify = (publicKey, entry, callback) => { + const { value, validityType, validity } = entry + const dataForSignature = getDataForSignature(value, validityType, validity) + + // Validate Signature + publicKey.verify(dataForSignature, entry.signature, (err, result) => { + if (err) { + return callback(err) + } + + // Validate EOL + const validityDate = Date.parse(validity.toString()) + + if (validityDate < Date.now()) { + return callback(new Error('record has expired')) + } + + return callback(null, null) + }) +} + +module.exports = { + getDataForSignature: (value, validityType, validity) => getDataForSignature(value, validityType, validity), + sign: (privateKey, value, validityType, validity, callback) => sign(privateKey, value, validityType, validity, callback), + verify: (publicKey, entry, callback) => verify(publicKey, entry, callback) +} diff --git a/src/core/namesys/publisher.js b/src/core/namesys/publisher.js deleted file mode 100644 index 5e0debf967..0000000000 --- a/src/core/namesys/publisher.js +++ /dev/null @@ -1,138 +0,0 @@ -'use strict' - -const peerId = require('peer-id') - -const IpnsEntry = require('./pb/ipnsEntry') -const utils = require('./utils') - -const defaultRecordTtl = 60 * 60 * 1000 - -/* - IpnsPublisher is capable of publishing and resolving names to the IPFS - routing system. - */ -class IpnsPublisher { - constructor (routing, repo) { - this.routing = routing - this.repo = repo - } - - // publish record with a eol - publishWithEOL (privKey, value, eol, callback) { - this.updateRecord(privKey, value, eol, (err, record) => { - if (err) { - return callback(err) - } - - // TODO FUTURE Put record to routing - callback(null, record) - }) - } - - // Accepts a keypair, as well as a value (ipfsPath), and publishes it out to the routing system - publish (privKey, value, callback) { - const eol = new Date(Date.now() + defaultRecordTtl) - - this.publishWithEOL(privKey, value, eol, (err, res) => { - if (err) { - return callback(err) - } - - callback(res) - }) - } - - // Returns the record this node has published corresponding to the given peer ID. - // If `checkRouting` is true and we have no existing record, this method will check the routing system for any existing records. - getPublished (peerId, checkRouting, callback) { - // TODO https://github.com/ipfs/go-ipfs/blob/master/namesys/publisher.go#L117 - this.repo.datastore.get(utils.generateIpnsDsKey(peerId), (err, dsVal) => { - let result - - if (!err) { - if (Buffer.isBuffer(dsVal)) { - result = dsVal - } else { - return callback(new Error('found ipns record that we couldn\'t convert to a value')) - } - } else if (err.notFound) { - if (!checkRouting) { - return callback(null, null) - } - // TODO Implement Routing - } else { - return callback(err) - } - - // unmarshal data - result = IpnsEntry.unmarshal(dsVal) - - return callback(null, result) - }) - } - - createEntryData (privKey, value, seqNumber, eol, callback) { - const valueBytes = Buffer.from(value) - const validityType = 0 - const sequence = seqNumber - const validity = Buffer.from(eol.toISOString()) - - const dataForSignature = this.getIpnsEntryDataForSig(valueBytes, validityType, validity) - privKey.sign(dataForSignature, (err, signature) => { - if (err) { - return callback(err) - } - - // Create IPNS entry record - const ipnsEntry = IpnsEntry.create(valueBytes, signature, validityType, validity, sequence) - - callback(null, ipnsEntry) - }) - } - - getIpnsEntryDataForSig (valueBuffer, validityType, validity) { - return Buffer.concat([valueBuffer, Buffer.from(validityType.toString()), validity]) - } - - updateRecord (privKey, value, eol, callback) { - peerId.createFromPrivKey(privKey.bytes.toString('base64'), (error, id) => { - if (error) { - callback(error) - } - - this.getPublished(id, false, (error, record) => { - if (error) { - callback(error) - } - - // Determinate the record sequence number - let seqNumber = 0 - if (record && record.sequence !== undefined) { - seqNumber = record.value.toString() !== value ? record.sequence + 1 : record.sequence - } - - // Create record - this.createEntryData(privKey, value, seqNumber, eol, (err, entryData) => { - if (err) { - return callback(err) - } - // TODO set ttl (still experimental feature for go) - - // Marshal record - const data = IpnsEntry.marshal(entryData) - - // Store the new record - this.repo.datastore.put(utils.generateIpnsDsKey(id), data, (err, res) => { - if (err) { - return callback(err) - } - - return callback(null, entryData) - }) - }) - }) - }) - } -} - -exports = module.exports = IpnsPublisher diff --git a/src/core/namesys/resolver.js b/src/core/namesys/resolver.js deleted file mode 100644 index 0cb6a7d149..0000000000 --- a/src/core/namesys/resolver.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict' - -const IpnsEntry = require('./pb/ipnsEntry') -// const utils = require('./utils') - -class IpnsResolver { - constructor (repo) { - this.repo = repo - } - - resolve (name, pubKey, callback) { - this.repo.datastore.get(name, (err, dsVal) => { - if (err) { - return callback(err) - } - - if (!Buffer.isBuffer(dsVal)) { - return callback(new Error('found ipns record that we couldn\'t convert to a value')) - } - - const ipnsEntry = IpnsEntry.unmarshal(dsVal) - - console.log('entry', ipnsEntry) - - return callback(null, 'entry') - - /* - let result - - if (!err) { - if (Buffer.isBuffer(dsVal)) { - result = dsVal - } else { - return callback(new Error('found ipns record that we couldn\'t convert to a value')) - } - } else if (err.notFound) { - if (!checkRouting) { - return callback(null, null) - } - // TODO Implement Routing - } else { - return callback(err) - } - - // unmarshal data - result = IpnsEntry.unmarshal(dsVal) - - return callback(null, result) */ - }) - } -} - -exports = module.exports = IpnsResolver diff --git a/src/core/namesys/utils.js b/src/core/namesys/utils.js deleted file mode 100644 index 745520fdae..0000000000 --- a/src/core/namesys/utils.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -module.exports = { - generateIpnsDsKey: (peerId) => `/ipns/${peerId.toB58String()}`, // TODO I think it should be base 32, according to go - buildIpnsKeysForId: (peerId) => ({ - nameKey: `/pk/${peerId.toB58String()}`, - ipnsKey: `/ipns/${peerId.toB58String()}` - }) -} diff --git a/src/http/api/resources/name.js b/src/http/api/resources/name.js index 2231563ef1..8159234873 100644 --- a/src/http/api/resources/name.js +++ b/src/http/api/resources/name.js @@ -14,9 +14,9 @@ exports.resolve = { }, handler: (request, reply) => { const ipfs = request.server.app.ipfs - const { name, nocache, recursive } = request.query + const { arg, nocache, recursive } = request.query - ipfs.name.resolve(name, nocache, recursive, (err, res) => { + ipfs.name.resolve(arg, nocache, recursive, (err, res) => { if (err) { return reply({ Message: err.toString(), @@ -43,9 +43,9 @@ exports.publish = { }, handler: (request, reply) => { const ipfs = request.server.app.ipfs - const path = request.query.arg + const { arg, resolve, lifetime, ttl, key } = request.query - ipfs.name.publish(path, (err, res) => { + ipfs.name.publish(arg, resolve, lifetime, ttl, key, (err, res) => { if (err) { return reply({ Message: err.toString(), @@ -54,8 +54,8 @@ exports.publish = { } return reply({ - Name: path, - Value: res + Name: res, + Value: arg }).code(200) }) } diff --git a/src/http/api/routes/name.js b/src/http/api/routes/name.js index 8df794f222..6ac5d155d0 100644 --- a/src/http/api/routes/name.js +++ b/src/http/api/routes/name.js @@ -18,7 +18,8 @@ module.exports = (server) => { method: '*', path: '/api/v0/name/publish', config: { - handler: resources.name.publish.handler + handler: resources.name.publish.handler, + validate: resources.name.resolve.validate } }) }