Introduce .getV3Filename() to return a suggested filename for V3 keystores

This commit is contained in:
Alex Beregszaszi 2016-03-16 13:43:47 +00:00
parent 395f01a809
commit 9e9fd6cd6f
3 changed files with 29 additions and 0 deletions

View File

@ -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()`.

View File

@ -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))
}

View File

@ -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')