Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 9e9fd6c

Browse files
committed
Introduce .getV3Filename() to return a suggested filename for V3 keystores
1 parent 395f01a commit 9e9fd6c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Instance methods:
4646
* `getPublicKey()` - return the public key
4747
* `getAddress()` - return the address
4848
* `getChecksumAddressString()` - return the [address with checksum](https://github.com/ethereum/EIPs/issues/55)
49+
* `getV3Filename([timestamp])` - return the suggested filename for V3 keystores
4950
* `toV3(password, [options])` - return the wallet as a JSON string (Version 3 of the Ethereum wallet format)
5051

5152
All of the above instance methods return a Buffer or JSON. Use the `String` suffixed versions for a string output, such as `getPrivateKeyString()`.

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ Wallet.prototype.toV3 = function (password, opts) {
111111
}
112112
}
113113

114+
Wallet.prototype.getV3Filename = function (timestamp) {
115+
/*
116+
* We want a timestamp like 2016-03-15T17-11-33.007598288Z. Date formatting
117+
* is a pain in Javascript, everbody knows that. We could use moment.js,
118+
* but decide to do it manually in order to save space.
119+
*
120+
* toJSON() returns a pretty close version, so let's use it. It is not UTC though,
121+
* but does it really matter?
122+
*
123+
* Alternative manual way with padding and Date fields: http://stackoverflow.com/a/7244288/4964819
124+
*
125+
*/
126+
var ts = timestamp ? new Date(timestamp) : new Date()
127+
128+
return [
129+
'UTC--',
130+
ts.toJSON().replace(/:/g, '-'),
131+
'--',
132+
this.getAddress().toString('hex')
133+
].join('')
134+
}
135+
114136
Wallet.prototype.toV3String = function (password, opts) {
115137
return JSON.stringify(this.toV3(password, opts))
116138
}

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ describe('.generate()', function () {
5858
})
5959
})
6060

61+
describe('.getV3Filename()', function () {
62+
it('should work', function () {
63+
assert.equal(fixturewallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
64+
})
65+
})
66+
6167
describe('.toV3()', function () {
6268
var salt = new Buffer('dc9e4a98886738bd8aae134a1f89aaa5a502c3fbd10e336136d4d5fe47448ad6', 'hex')
6369
var iv = new Buffer('cecacd85e9cb89788b5aab2f93361233', 'hex')

0 commit comments

Comments
 (0)