Skip to content

Commit

Permalink
Added function check, browser property, json return for in-browser ex…
Browse files Browse the repository at this point in the history
…portToFile
  • Loading branch information
tinybike committed Dec 14, 2015
1 parent 0ff004e commit c3bd48c
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 162 deletions.
127 changes: 57 additions & 70 deletions dist/keythereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ function hex2utf16le(input) {
return JSON.parse('"' + output + '"');
}

function isFunction(f) {
return Object.prototype.toString.call(f) === "[object Function]";
}

module.exports = {

browser: !NODE_JS,

constants: {

// Suppress logging
Expand Down Expand Up @@ -192,7 +198,7 @@ module.exports = {

try {

if (cb && cb.constructor === Function) {
if (isFunction(cb)) {

setTimeout(function () {
cb(new Buffer(
Expand Down Expand Up @@ -223,7 +229,7 @@ module.exports = {
}

} catch (ex) {
if (cb && cb.constructor === Function) {
if (isFunction(cb)) {
cb(ex);
} else {
return ex;
Expand All @@ -236,7 +242,7 @@ module.exports = {
var prf = options.kdfparams.prf || this.constants.pbkdf2.prf;
if (prf === "hmac-sha256") prf = "sha256";

if (cb && cb.constructor === Function) {
if (isFunction(cb)) {

crypto.pbkdf2(
password,
Expand Down Expand Up @@ -284,7 +290,7 @@ module.exports = {
var ivBytes = params.ivBytes || this.constants.ivBytes;

// asynchronous key generation if callback is provided
if (cb && cb.constructor === Function) {
if (isFunction(cb)) {

// generate private key
crypto.randomBytes(keyBytes, function (ex, privateKey) {
Expand Down Expand Up @@ -413,7 +419,7 @@ module.exports = {
if (privateKey.constructor === String) privateKey = str2buf(privateKey);

// asynchronous if callback provided
if (cb && cb.constructor === Function) {
if (isFunction(cb)) {

this.deriveKey(
password,
Expand Down Expand Up @@ -494,7 +500,7 @@ module.exports = {
}

// derive secret key from password
if (cb && cb.constructor === Function) {
if (isFunction(cb)) {
this.deriveKey(password, salt, keyObject.Crypto, function (derivedKey) {
cb(verifyAndDecrypt(derivedKey, salt, iv, ciphertext));
});
Expand All @@ -510,53 +516,44 @@ module.exports = {

/**
* Export formatted JSON to keystore file.
* (Note: Node.js only!)
* @param {Object} keyObject Keystore object.
* @param {string=} keystore Path to keystore folder (default: "keystore").
* @param {function=} cb Callback function (optional).
* @return {string} JSON filename.
* @return {string} JSON filename (Node.js) or JSON string (browser).
*/
exportToFile: function (keyObject, keystore, cb) {
keystore = keystore || "keystore";
var self = this;
var outfile, outpath, json;

var instructions = function (outpath) {
if (!this.constants.quiet) {
function instructions(outpath) {
if (!self.constants.quiet) {
console.log(
"Saved to file:\n" + outpath + "\n"+
"To use with geth, copy this file to your Ethereum "+
"keystore folder (usually ~/.ethereum/keystore)."
);
}
}.bind(this);

var outfile = "UTC--" + new Date().toISOString() + "--" + keyObject.address;
var outpath = path.join(keystore, outfile);
var json = JSON.stringify(keyObject);

if (NODE_JS) {

if (cb && cb.constructor === Function) {

fs.writeFile(outpath, json, function (ex) {
if (ex) throw ex;
instructions(outpath);
cb(outpath);
});

} else {

fs.writeFileSync(outpath, json);
instructions(outpath);
return outpath;
}

} else {
if (cb && cb.constructor === Function) {
cb(outfile);
} else {
return outfile;
}
}

keystore = keystore || "keystore";
outfile = "UTC--" + new Date().toISOString() + "--" + keyObject.address;
outpath = path.join(keystore, outfile);
json = JSON.stringify(keyObject);

if (this.browser) {
if (!isFunction(cb)) return json;
return cb(json);
}
if (!isFunction(cb)) {
fs.writeFileSync(outpath, json);
instructions(outpath);
return outpath;
}
return fs.writeFile(outpath, json, function (ex) {
if (ex) throw ex;
instructions(outpath);
cb(outpath);
});
},

/**
Expand Down Expand Up @@ -584,39 +581,29 @@ module.exports = {
return filepath;
}

if (NODE_JS) {

datadir = datadir || path.join(process.env.HOME, ".ethereum");
var keystore = path.join(datadir, "keystore");

if (cb && cb.constructor === Function) {
fs.readdir(keystore, function (ex, files) {
if (ex) throw ex;
var filepath = findKeyfile(keystore, address, files);
if (filepath) {
cb(JSON.parse(fs.readFileSync(filepath)));
} else {
throw new Error(
"could not find key file for address " + address
);
}
});

if (this.browser) {
throw new Error("method only available in Node.js");
}
datadir = datadir || path.join(process.env.HOME, ".ethereum");
var keystore = path.join(datadir, "keystore");
if (!isFunction(cb)) {
var filepath = findKeyfile(keystore, address, fs.readdirSync(keystore));
if (filepath) {
return JSON.parse(fs.readFileSync(filepath));
} else {

var filepath = findKeyfile(keystore, address, fs.readdirSync(keystore));
if (filepath) {
return JSON.parse(fs.readFileSync(filepath));
} else {
throw new Error(
"could not find key file for address " + address
);
}
throw new Error(
"could not find key file for address " + address
);
}

} else {
throw new Error("method only available in Node.js");
}
fs.readdir(keystore, function (ex, files) {
if (ex) return cb(ex);
var filepath = findKeyfile(keystore, address, files);
if (filepath) return cb(JSON.parse(fs.readFileSync(filepath)));
return new Error(
"could not find key file for address " + address
);
});
}

};
Expand Down
22 changes: 11 additions & 11 deletions dist/keythereum.min.js

Large diffs are not rendered by default.

0 comments on commit c3bd48c

Please sign in to comment.