From 9e9fd6cd6f0fd98c9878e0df0fdb87f622cf29da Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 16 Mar 2016 13:43:47 +0000 Subject: [PATCH] Introduce .getV3Filename() to return a suggested filename for V3 keystores --- README.md | 1 + index.js | 22 ++++++++++++++++++++++ test/index.js | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 08761e9..64974cc 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Instance methods: * `getPublicKey()` - return the public key * `getAddress()` - return the address * `getChecksumAddressString()` - return the [address with checksum](https://github.com/ethereum/EIPs/issues/55) +* `getV3Filename([timestamp])` - return the suggested filename for V3 keystores * `toV3(password, [options])` - return the wallet as a JSON string (Version 3 of the Ethereum wallet format) All of the above instance methods return a Buffer or JSON. Use the `String` suffixed versions for a string output, such as `getPrivateKeyString()`. diff --git a/index.js b/index.js index 27e6b89..e63ecaa 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,28 @@ Wallet.prototype.toV3 = function (password, opts) { } } +Wallet.prototype.getV3Filename = function (timestamp) { + /* + * We want a timestamp like 2016-03-15T17-11-33.007598288Z. Date formatting + * is a pain in Javascript, everbody knows that. We could use moment.js, + * but decide to do it manually in order to save space. + * + * toJSON() returns a pretty close version, so let's use it. It is not UTC though, + * but does it really matter? + * + * Alternative manual way with padding and Date fields: http://stackoverflow.com/a/7244288/4964819 + * + */ + var ts = timestamp ? new Date(timestamp) : new Date() + + return [ + 'UTC--', + ts.toJSON().replace(/:/g, '-'), + '--', + this.getAddress().toString('hex') + ].join('') +} + Wallet.prototype.toV3String = function (password, opts) { return JSON.stringify(this.toV3(password, opts)) } diff --git a/test/index.js b/test/index.js index 9ae2f57..91f5077 100644 --- a/test/index.js +++ b/test/index.js @@ -58,6 +58,12 @@ describe('.generate()', function () { }) }) +describe('.getV3Filename()', function () { + it('should work', function () { + assert.equal(fixturewallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c') + }) +}) + describe('.toV3()', function () { var salt = new Buffer('dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6', 'hex') var iv = new Buffer('cecacd85e9cb89788b5aab2f93361233', 'hex')