Skip to content

Commit

Permalink
Swarm-Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTaelin committed Aug 9, 2017
1 parent f42cef1 commit d52c111
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
31 changes: 31 additions & 0 deletions lib/swarm-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Thanks https://github.com/axic/swarmhash

var keccak = require("eth-lib/lib/hash").keccak256;
var Bytes = require("eth-lib/lib/bytes");

var swarmHashBlock = function swarmHashBlock(length, data) {
return keccak(Bytes.flatten([Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length))), "0x0000", data]));
};

var swarmHash = function swarmHash(data) {
var length = Bytes.length(data);

if (length <= 4096) {
return swarmHashBlock(length, data);
}

var maxSize = 4096;
while (maxSize * (4096 / 32) < length) {
maxSize *= 4096 / 32;
}

var innerNodes = [];
for (var i = 0; i < length; i += maxSize) {
var size = maxSize < length - i ? maxSize : length - i;
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size)));
}

return swarmHashBlock(length, Bytes.flatten(innerNodes));
};

module.exports = swarmHash;
4 changes: 3 additions & 1 deletion lib/swarm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var mimetype = require('mime-types');
var hash = require("./swarm-hash.js");
var pick = require("./pick.js");
var request = require("xhr-request-promise");
var downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/";
Expand Down Expand Up @@ -583,6 +584,7 @@ module.exports = function (_ref) {
uploadDirectory: uploadDirectory,
uploadDirectoryFromDisk: uploadDirectoryFromDisk,
uploadToManifest: uploadToManifest,
pick: pick
pick: pick,
hash: hash
};
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
"url": "https://github.com/maiavictor/swarm-js"
},
"dependencies": {
"bluebird": "^3.5.0",
"buffer": "^5.0.5",
"decompress": "^4.0.0",
"eth-lib": "^0.1.25",
"fs-extra": "^2.1.2",
"fs-promise": "^2.0.0",
"got": "^7.1.0",
Expand Down
33 changes: 33 additions & 0 deletions src/swarm-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Thanks https://github.com/axic/swarmhash

const keccak = require("eth-lib/lib/hash").keccak256;
const Bytes = require("eth-lib/lib/bytes");

const swarmHashBlock = (length, data) =>
keccak(Bytes.flatten([
Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length))),
"0x0000",
data]));

const swarmHash = (data) => {
const length = Bytes.length(data);

if (length <= 4096) {
return swarmHashBlock(length, data);
}

let maxSize = 4096;
while ((maxSize * (4096 / 32)) < length) {
maxSize *= (4096 / 32);
}

let innerNodes = [];
for (let i = 0; i < length; i += maxSize) {
const size = (maxSize < (length - i)) ? maxSize : (length - i);
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size)));
}

return swarmHashBlock(length, Bytes.flatten(innerNodes));
};

module.exports = swarmHash;
4 changes: 3 additions & 1 deletion src/swarm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mimetype = require('mime-types');
const hash = require("./swarm-hash.js");
const pick = require("./pick.js");
const request = require("xhr-request-promise");
const downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/";
Expand Down Expand Up @@ -438,7 +439,8 @@ module.exports = ({fsp, files, os, path, child_process}) => {
uploadDirectory,
uploadDirectoryFromDisk,
uploadToManifest,
pick
pick,
hash
};

};

0 comments on commit d52c111

Please sign in to comment.