Beta 7 with some repo names changed.
This commit is contained in:
1 parent
0f7b40cca7
commit
963c19493a
3408 files changed
+462217
-15516
No files matched your search
+28
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es6": true,
|
||||
"node": true,
|
||||
"mocha": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
"no-console": "off"
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["test/*.js"],
|
||||
"rules": {
|
||||
"no-unused-vars": "off"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
|
||||
window.onload = () => {
|
||||
console.log('contentHash ✅')
|
||||
|
||||
const ipfsInputElem = document.getElementById('ipfs-input')
|
||||
const ipfsButtonElem = document.getElementById('ipfs-encode')
|
||||
const ipfsResultElem = document.getElementById('ipfs-result')
|
||||
ipfsButtonElem.addEventListener('click', () => {
|
||||
ipfsResultElem.innerHTML = contentHash.fromIpfs(ipfsInputElem.value)
|
||||
})
|
||||
|
||||
const swarmInputElem = document.getElementById('swarm-input')
|
||||
const swarmButtonElem = document.getElementById('swarm-encode')
|
||||
const swarmResultElem = document.getElementById('swarm-result')
|
||||
swarmButtonElem.addEventListener('click', () => {
|
||||
swarmResultElem.innerHTML = contentHash.fromSwarm(swarmInputElem.value)
|
||||
})
|
||||
|
||||
const contentInputElem = document.getElementById('content-input')
|
||||
const contentButtonElem = document.getElementById('content-decode')
|
||||
const contentResultElem = document.getElementById('content-result')
|
||||
const codecResultElem = document.getElementById('codec-result')
|
||||
contentButtonElem.addEventListener('click', () => {
|
||||
let cth = contentHash.decode(contentInputElem.value)
|
||||
|
||||
let codec = 'unknown'
|
||||
|
||||
if(contentHash.getCodec(contentInputElem.value) === 'ipfs-ns')codec = 'ipfs'
|
||||
else if(contentHash.getCodec(contentInputElem.value) === 'swarm-ns')codec = 'swarm'
|
||||
|
||||
let url = 'https://'
|
||||
if(codec === 'ipfs') url += 'gateway.ipfs.io/ipfs/' + cth + '/'
|
||||
else if(codec === 'swarm') url += 'swarm-gateways.net/bzz:/' + cth + '/'
|
||||
else url = '#'
|
||||
|
||||
codecResultElem.innerHTML = 'codec : ' + codec
|
||||
contentResultElem.innerHTML = cth
|
||||
contentResultElem.href = url
|
||||
})
|
||||
}
|
||||
+6160
File diff suppressed because it is too large.
Load diff
+75
@@ -0,0 +1,75 @@
|
||||
{
|
||||
"_from": "content-hash@^2.5.2",
|
||||
"_id": "content-hash@2.5.2",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-FvIQKy0S1JaWV10sMsA7TRx8bpU+pqPkhbsfvOJAdjRXvYxEckAwQWGwtRjiaJfh+E0DvcWUGqcdjwMGFjsSdw==",
|
||||
"_location": "/content-hash",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "content-hash@^2.5.2",
|
||||
"name": "content-hash",
|
||||
"escapedName": "content-hash",
|
||||
"rawSpec": "^2.5.2",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^2.5.2"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/web3-eth-ens"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/content-hash/-/content-hash-2.5.2.tgz",
|
||||
"_shasum": "bbc2655e7c21f14fd3bfc7b7d4bfe6e454c9e211",
|
||||
"_spec": "content-hash@^2.5.2",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\web3-eth-ens",
|
||||
"author": {
|
||||
"name": "pldespaigne"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/pldespaigne/content-hash/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"cids": "^0.7.1",
|
||||
"multicodec": "^0.5.5",
|
||||
"multihashes": "^0.4.15"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "simple tool to encode/decode content hash for EIP 1577 compliant ENS Resolvers",
|
||||
"devDependencies": {
|
||||
"browserify": "^16.5.0",
|
||||
"chai": "^4.2.0",
|
||||
"eslint": "^5.16.0",
|
||||
"mocha": "^5.2.0",
|
||||
"node-static": "^0.7.11"
|
||||
},
|
||||
"homepage": "https://github.com/pldespaigne/content-hash#readme",
|
||||
"keywords": [
|
||||
"ethereum",
|
||||
"ens",
|
||||
"eip-1577",
|
||||
"resolver",
|
||||
"ipfs",
|
||||
"swarm",
|
||||
"content-hash",
|
||||
"content",
|
||||
"hash",
|
||||
"contenthash",
|
||||
"contentHash"
|
||||
],
|
||||
"license": "ISC",
|
||||
"main": "./src/index.js",
|
||||
"name": "content-hash",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/pldespaigne/content-hash.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "browserify ./src/index.js --s contentHash > ./dist/index.js",
|
||||
"demo": "static .",
|
||||
"deploy-demo": "echo \"surge demo https://content-hash.surge.sh\"",
|
||||
"lint": "npx eslint .",
|
||||
"test": "mocha"
|
||||
},
|
||||
"version": "2.5.2"
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2019, Pierre-Louis Despaigne
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
const CID = require('cids');
|
||||
|
||||
/**
|
||||
* Take any ipfsHash and convert it to a CID v1 encoded in base32.
|
||||
* @param {string} ipfsHash a regular ipfs hash either a cid v0 or v1 (v1 will remain unchanged)
|
||||
* @return {string} the resulting ipfs hash as a cid v1
|
||||
*/
|
||||
const cidV0ToV1Base32 = (ipfsHash) => {
|
||||
let cid = new CID(ipfsHash);
|
||||
if (cid.version === 0) {
|
||||
cid = cid.toV1();
|
||||
}
|
||||
return cid.toString('base32');
|
||||
}
|
||||
|
||||
exports.cidV0ToV1Base32 = cidV0ToV1Base32;
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2019, Pierre-Louis Despaigne
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
const multiC = require('multicodec');
|
||||
|
||||
const { hexStringToBuffer, profiles } = require('./profiles');
|
||||
const { cidV0ToV1Base32 } = require('./helpers');
|
||||
|
||||
module.exports = {
|
||||
|
||||
//export some helpers functions
|
||||
helpers: {
|
||||
cidV0ToV1Base32,
|
||||
},
|
||||
|
||||
/**
|
||||
* Decode a Content Hash.
|
||||
* @param {string} hash an hex string containing a content hash
|
||||
* @return {string} the decoded content
|
||||
*/
|
||||
decode: function (contentHash) {
|
||||
const buffer = hexStringToBuffer(contentHash);
|
||||
const codec = multiC.getCodec(buffer);
|
||||
const value = multiC.rmPrefix(buffer);
|
||||
let profile = profiles[codec];
|
||||
if (!profile) profile = profiles['default'];
|
||||
return profile.decode(value);
|
||||
},
|
||||
|
||||
/**
|
||||
* Encode an IPFS address into a content hash
|
||||
* @param {string} ipfsHash string containing an IPFS address
|
||||
* @return {string} the resulting content hash
|
||||
*/
|
||||
fromIpfs: function (ipfsHash) {
|
||||
return this.encode('ipfs-ns', ipfsHash);
|
||||
},
|
||||
|
||||
/**
|
||||
* Encode a Swarm address into a content hash
|
||||
* @param {string} swarmHash string containing a Swarm address
|
||||
* @return {string} the resulting content hash
|
||||
*/
|
||||
fromSwarm: function (swarmHash) {
|
||||
return this.encode('swarm-ns', swarmHash);
|
||||
},
|
||||
|
||||
/**
|
||||
* General purpose encoding function
|
||||
* @param {string} codec
|
||||
* @param {string} value
|
||||
*/
|
||||
encode: function (codec, value) {
|
||||
let profile = profiles[codec];
|
||||
if (!profile) profile = profiles['default'];
|
||||
const encodedValue = profile.encode(value);
|
||||
return multiC.addPrefix(codec, encodedValue).toString('hex');
|
||||
},
|
||||
|
||||
/**
|
||||
* Extract the codec of a content hash
|
||||
* @param {string} hash hex string containing a content hash
|
||||
* @return {string} the extracted codec
|
||||
*/
|
||||
getCodec: function (hash) {
|
||||
let buffer = hexStringToBuffer(hash);
|
||||
return multiC.getCodec(buffer);
|
||||
},
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2019, Pierre-Louis Despaigne
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
const CID = require('cids');
|
||||
const multiH = require('multihashes');
|
||||
|
||||
/**
|
||||
* Convert an hexadecimal string to a Buffer, the string can start with or without '0x'
|
||||
* @param {string} hex an hexadecimal value
|
||||
* @return {Buffer} the resulting Buffer
|
||||
*/
|
||||
const hexStringToBuffer = (hex) => {
|
||||
let prefix = hex.slice(0, 2);
|
||||
let value = hex.slice(2);
|
||||
let res = '';
|
||||
if (prefix === '0x') res = value;
|
||||
else res = hex;
|
||||
return multiH.fromHexString(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* list of known encoding,
|
||||
* encoding should be a function that takes a `string` input,
|
||||
* and return a `Buffer` result
|
||||
*/
|
||||
const encodes = {
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {Buffer}
|
||||
*/
|
||||
swarm: (value) => {
|
||||
const multihash = multiH.encode(hexStringToBuffer(value), 'keccak-256');
|
||||
return new CID(1, 'swarm-manifest', multihash).buffer;
|
||||
},
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {Buffer}
|
||||
*/
|
||||
ipfs: (value) => {
|
||||
const multihash = multiH.fromB58String(value);
|
||||
return new CID(1, 'dag-pb', multihash).buffer;
|
||||
},
|
||||
/**
|
||||
* @param {string} value
|
||||
* @return {Buffer}
|
||||
*/
|
||||
utf8: (value) => {
|
||||
return Buffer.from(value, 'utf8');
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* list of known decoding,
|
||||
* decoding should be a function that takes a `Buffer` input,
|
||||
* and return a `string` result
|
||||
*/
|
||||
const decodes = {
|
||||
/**
|
||||
* @param {Buffer} value
|
||||
*/
|
||||
hexMultiHash: (value) => {
|
||||
const cid = new CID(value);
|
||||
return multiH.decode(cid.multihash).digest.toString('hex');
|
||||
},
|
||||
/**
|
||||
* @param {Buffer} value
|
||||
*/
|
||||
b58MultiHash: (value) => {
|
||||
const cid = new CID(value);
|
||||
return multiH.toB58String(cid.multihash);
|
||||
},
|
||||
/**
|
||||
* @param {Buffer} value
|
||||
*/
|
||||
utf8: (value) => {
|
||||
return value.toString('utf8');
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* list of known encoding/decoding for a given codec,
|
||||
* `encode` should be chosen among the `encodes` functions
|
||||
* `decode` should be chosen among the `decodes` functions
|
||||
*/
|
||||
const profiles = {
|
||||
'swarm-ns': {
|
||||
encode: encodes.swarm,
|
||||
decode: decodes.hexMultiHash,
|
||||
},
|
||||
'ipfs-ns': {
|
||||
encode: encodes.ipfs,
|
||||
decode: decodes.b58MultiHash,
|
||||
},
|
||||
'ipns-ns': {
|
||||
encode: encodes.ipfs,
|
||||
decode: decodes.b58MultiHash,
|
||||
},
|
||||
'default': {
|
||||
encode: encodes.utf8,
|
||||
decode: decodes.utf8,
|
||||
},
|
||||
};
|
||||
|
||||
exports.hexStringToBuffer = hexStringToBuffer;
|
||||
exports.profiles = profiles;
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
|
||||
const should = require('chai').should()
|
||||
const expect = require('chai').expect
|
||||
const contentHash = require('../src/index.js')
|
||||
|
||||
const ipfs = 'QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4'
|
||||
const ipfs_contentHash = 'e3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
|
||||
const ipns_contentHash = 'e5010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f'
|
||||
const swarm = 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
|
||||
const swarm_contentHash = 'e40101fa011b20d1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162'
|
||||
const onion = 'zqktlwi4fecvo6ri'
|
||||
const onion_contentHash = 'bc037a716b746c776934666563766f367269';
|
||||
const onion3 = 'p53lf57qovyuvwsc6xnrppyply3vtqm7l6pcobkmyqsiofyeznfu5uqd';
|
||||
const onion3_contentHash = 'bd037035336c663537716f7679757677736336786e72707079706c79337674716d376c3670636f626b6d797173696f6679657a6e667535757164';
|
||||
const ipfsBase32DagPb = 'bafybeibj6lixxzqtsb45ysdjnupvqkufgdvzqbnvmhw2kf7cfkesy7r7d4';
|
||||
const ipfsBase32Libp2pKey = 'bafzbeie5745rpv2m6tjyuugywy4d5ewrqgqqhfnf445he3omzpjbx5xqxe';
|
||||
|
||||
describe('content-hash (legacy tests)', () =>
|
||||
{
|
||||
it('should decode a content hash', () => {
|
||||
const actual_0 = contentHash.decode(ipfs_contentHash);
|
||||
const actual_1 = contentHash.decode(swarm_contentHash);
|
||||
let actual_2 = contentHash.decode(onion_contentHash);
|
||||
|
||||
actual_0.should.be.equal(ipfs);
|
||||
actual_1.should.be.equal(swarm);
|
||||
actual_2.should.be.equal(onion);
|
||||
});
|
||||
it('should encode an ipfs address', () => {
|
||||
const actual = contentHash.fromIpfs(ipfs);
|
||||
actual.should.be.equal(ipfs_contentHash);
|
||||
});
|
||||
it('should encode a swarm address', () => {
|
||||
const actual = contentHash.fromSwarm(swarm);
|
||||
actual.should.be.equal(swarm_contentHash);
|
||||
});
|
||||
it('should encode an onion address', () => {
|
||||
const actual = contentHash.encode('onion', onion);
|
||||
actual.should.be.equal(onion_contentHash);
|
||||
});
|
||||
it('should get a codec from a content hash', () => {
|
||||
const actual_0 = contentHash.getCodec(ipfs_contentHash);
|
||||
const actual_1 = contentHash.getCodec(swarm_contentHash);
|
||||
const actual_2 = contentHash.getCodec(onion_contentHash);
|
||||
|
||||
actual_0.should.be.equal('ipfs-ns');
|
||||
actual_1.should.be.equal('swarm-ns');
|
||||
actual_2.should.be.equal('onion');
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
describe('content-hash', () => {
|
||||
describe('swarm', () => {
|
||||
it('should encode', () => {
|
||||
const actual = contentHash.encode('swarm-ns', swarm);
|
||||
actual.should.be.equal(swarm_contentHash);
|
||||
});
|
||||
it('should getCodec', () => {
|
||||
const actual = contentHash.getCodec(swarm_contentHash);
|
||||
actual.should.be.equal('swarm-ns');
|
||||
});
|
||||
it('should decode', () => {
|
||||
const actual = contentHash.decode(swarm_contentHash);
|
||||
actual.should.be.equal(swarm);
|
||||
});
|
||||
});
|
||||
describe('ipfs', () => {
|
||||
it('should encode', () => {
|
||||
const actual = contentHash.encode('ipfs-ns', ipfs);
|
||||
actual.should.be.equal(ipfs_contentHash);
|
||||
});
|
||||
it('should getCodec', () => {
|
||||
const actual = contentHash.getCodec(ipfs_contentHash);
|
||||
actual.should.be.equal('ipfs-ns');
|
||||
});
|
||||
it('should decode', () => {
|
||||
const actual = contentHash.decode(ipfs_contentHash);
|
||||
actual.should.be.equal(ipfs);
|
||||
});
|
||||
});
|
||||
describe('ipns', () => {
|
||||
it('should encode', () => {
|
||||
const actual = contentHash.encode('ipns-ns', ipfs); // ipns & ipfs are the same hash and same encoding, only codec differ
|
||||
actual.should.be.equal(ipns_contentHash);
|
||||
});
|
||||
it('should getCodec', () => {
|
||||
const actual = contentHash.getCodec(ipns_contentHash);
|
||||
actual.should.be.equal('ipns-ns');
|
||||
});
|
||||
it('should decode', () => {
|
||||
const actual = contentHash.decode(ipns_contentHash);
|
||||
actual.should.be.equal(ipfs); // ipns & ipfs are the same hash and same encoding, only codec differ
|
||||
});
|
||||
});
|
||||
describe('onion', () => {
|
||||
it('should encode', () => {
|
||||
const actual = contentHash.encode('onion', onion);
|
||||
actual.should.be.equal(onion_contentHash);
|
||||
});
|
||||
it('should getCodec', () => {
|
||||
const actual = contentHash.getCodec(onion_contentHash);
|
||||
actual.should.be.equal('onion');
|
||||
});
|
||||
it('should decode', () => {
|
||||
const actual = contentHash.decode(onion_contentHash);
|
||||
actual.should.be.equal(onion);
|
||||
});
|
||||
});
|
||||
describe('onion3', () => {
|
||||
it('should encode', () => {
|
||||
const actual = contentHash.encode('onion3', onion3);
|
||||
actual.should.be.equal(onion3_contentHash);
|
||||
});
|
||||
it('should getCodec', () => {
|
||||
const actual = contentHash.getCodec(onion3_contentHash);
|
||||
actual.should.be.equal('onion3');
|
||||
});
|
||||
it('should decode', () => {
|
||||
const actual = contentHash.decode(onion3_contentHash);
|
||||
actual.should.be.equal(onion3);
|
||||
});
|
||||
});
|
||||
describe('helpers', () => {
|
||||
it('should convert CID v0 into v1', () => {
|
||||
const actual = contentHash.helpers.cidV0ToV1Base32(ipfs);
|
||||
actual.should.be.equal(ipfsBase32DagPb);
|
||||
});
|
||||
it('should keep CID v1 Base32 as-is', () => {
|
||||
const dagPbCid = contentHash.helpers.cidV0ToV1Base32(ipfsBase32DagPb);
|
||||
dagPbCid.should.be.equal(ipfsBase32DagPb);
|
||||
const libp2pKeyCid = contentHash.helpers.cidV0ToV1Base32(ipfsBase32Libp2pKey);
|
||||
libp2pKeyCid.should.be.equal(ipfsBase32Libp2pKey);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user