Skip to content

Commit

Permalink
Cid V1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
xmaysonnave committed Dec 9, 2019
1 parent bdafc2a commit 6378aba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/ens-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ EnsLibrary
\*/

import CID from "cids";
import contentHash from "content-hash";

( function() {
Expand Down Expand Up @@ -76,21 +77,30 @@ EnsLibrary.prototype.decodeContenthash = function(encoded) {
};
}

// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1577.md
EnsLibrary.prototype.encodeContenthash = function(text) {
let content, contentType;
let type;
let content;
let encoded = false;
if (!!text) {
const matched = text.match(/^(ipfs|bzz|onion|onion3):\/\/(.*)/) || text.match(/\/(ipfs)\/(.*)/);
if (matched) {
contentType = matched[1];
type = matched[1];
content = matched[2];
}
if (contentType === "ipfs") {
if (type === "ipfs") {
if (content.length >= 4) {
var cid = new CID(content);
if (cid.version === 1) {
if (cid.codec !== "dag-pb") {
throw new Error("This Cid V1 is not 'dag-pb' encoded: " + content);
}
content = cid.toV0().toString();
}
encoded = "0x" + contentHash.fromIpfs(content);
}
} else {
throw new Error("Unsupported Ens domain protocol: " + contentType);
throw new Error("Unsupported Ens domain protocol: " + type);
}
}
return encoded;
Expand Down
2 changes: 2 additions & 0 deletions src/ipfs-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ IpfsLibrary.prototype.add = async function(client, content) {
// chunker: "size-262144"
// chunker: "rabin-262144-524288-1048576"
const result = await client.add(buffer, {
cidVersion: 1,
hash: "keccak-256",
chunker: "rabin-262144-524288-1048576",
pin: false
});
Expand Down

0 comments on commit 6378aba

Please sign in to comment.