Skip to content

Commit

Permalink
Accept private key file
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Maia committed Apr 11, 2017
1 parent aa4e761 commit 66d7775
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
23 changes: 14 additions & 9 deletions examples/run_node.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
const Swarm = require("./../src/swarm.js");
const fs = require("fs");
const path = require("path");
const privateKeyPath = path.join(process.cwd(), "swarmPrivateKey");

// Writes a temporary private key file to disk
fs.writeFileSync(privateKeyPath, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");

// To run Swarm locally, you need a running Geth
// node and an Ethereum account/password
const config = {
account: "d849168d52ea5c40de1b0b973cfd96873c961963",
password: "sap",
dataDir: process.env.HOME+"/Library/Ethereum/testnet",
ethApi: process.env.HOME+"/Library/Ethereum/testnet/geth.ipc"
privateKey: privateKeyPath,
dataDir: process.env.HOME + "/Library/Ethereum/testnet",
ethApi: process.env.HOME + "/Library/Ethereum/testnet/geth.ipc"
};

// Magically starts a local Swarm node
// Downloads binaries if necessary
Swarm.local(config)(swarm => new Promise((resolve, reject) => {
console.log("running");
console.log(swarm);
// Removes the temporary private key file
fs.unlinkSync(privateKeyPath);

// Uploads data using the local node
swarm.upload(new Buffer("test")).then(hash => {
console.log("Uploaded data. Address:", hash);

// Closes the Swarm process.
resolve();
});

}).catch(e => console.log(e));
}))
.then(() => console.log("Done!"));
.then(() => console.log("Done!"))
.catch(e => console.log(e));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swarm-js",
"version": "0.1.9",
"version": "0.1.10",
"description": "Swarm tools for JavaScript.",
"main": "src/swarm.js",
"scripts": {
Expand Down
19 changes: 10 additions & 9 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const uploadDirectoryFromDisk = swarmUrl => defaultPath => dirPath =>
const upload = swarmUrl => arg => {
// Upload raw data from browser
if (arg.pick === "data") {
return pick.data().then(uploadData(swarmUrl));
return pick.data().then(uploadData(swarmUrl));

// Upload a file from browser
} else if (arg.pick === "file") {
Expand Down Expand Up @@ -257,7 +257,7 @@ const downloadBinary = path => {
// Starts the Swarm process.
const startProcess = swarmSetup => new Q((resolve, reject) => {
const hasString = str => buffer => ('' + buffer).indexOf(str) !== -1;
const {account, password, dataDir, ethApi} = swarmSetup;
const {account, password, dataDir, ethApi, privateKey} = swarmSetup;
const binPath = path.join(swarmSetup.dataDir, "bin", "swarm");

const STARTUP_TIMEOUT_SECS = 3;
Expand All @@ -266,11 +266,11 @@ const startProcess = swarmSetup => new Q((resolve, reject) => {
const LISTENING = 2;
const PASSWORD_PROMPT_HOOK = "Passphrase";
const LISTENING_HOOK = "Swarm HTTP proxy started";

let state = WAITING_PASSWORD;

const swarmProcess = spawn(binPath, [
'--bzzaccount', account,
'--bzzaccount', account || privateKey,
'--datadir', dataDir,
'--ethapi', ethApi]);

Expand All @@ -280,7 +280,7 @@ const startProcess = swarmSetup => new Q((resolve, reject) => {
state = STARTING;
swarmProcess.stdin.write(password + '\n');
}, 500);
} else if (state === STARTING && hasString (LISTENING_HOOK) (data)) {
} else if (hasString (LISTENING_HOOK) (data)) {
state = LISTENING;
clearTimeout(timeout);
resolve(swarmProcess);
Expand All @@ -289,10 +289,11 @@ const startProcess = swarmSetup => new Q((resolve, reject) => {

swarmProcess.stdout.on('data', handleProcessOutput);
swarmProcess.stderr.on('data', handleProcessOutput);
swarmProcess.on('close', () => startProcess(swarmSetup).then(resolve).catch(reject));
const timeout = setTimeout(() =>
reject(new Error("Couldn't start swarm process.")),
20000);
//swarmProcess.on('close', () => setTimeout(restart, 2000));

let restart = () => startProcess(swarmSetup).then(resolve).catch(reject);
let error = () => reject(new Error("Couldn't start swarm process."));
let timeout = setTimeout(error, 20000);
});

// Process ~> Promise ()
Expand Down

0 comments on commit 66d7775

Please sign in to comment.