Skip to content

Commit

Permalink
plugin: Cache keypairs, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Apr 22, 2017
1 parent 8212a3a commit 4340206
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"babel-runtime": "^6.20.0",
"find-cache-dir": "^0.1.1",
"got": "^6.7.1",
"ms": "^1.0.0",
"sodium-signatures": "^2.0.0",
Expand Down
30 changes: 29 additions & 1 deletion plugin/src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import fs from 'fs';
import got from 'got';
import ms from 'ms';
import { keyPair as createKeyPair, sign } from 'sodium-signatures';
import stripIndent from 'strip-indent';
import findCacheDir from 'find-cache-dir';

const meta = require('../package.json');

function stripSlashes(url) {
return url.replace(/\/+$/, '');
}

function getKeyPair(seed) {
const keyPairPath = findCacheDir({
name: meta.name,
create: true,
thunk: true
})('keypair.json');
try {
const { publicKey, secretKey } = JSON.parse(
fs.readFileSync(keyPairPath, 'utf8')
);
return {
publicKey: Buffer.from(publicKey, 'base64'),
secretKey: Buffer.from(publicKey, 'base64')
};
} catch (err) {
const { publicKey, secretKey } = createKeyPair(seed);
fs.writeFileSync(keyPairPath, JSON.stringify({
publicKey: publicKey.toString('base64'),
secretKey: secretKey.toString('base64')
}, null, 2), 'utf8');
return { publicKey, secretKey };
}
}

async function getAnnounceData(uw, options) {
const url = stripSlashes(options.url);

Expand Down Expand Up @@ -56,7 +84,7 @@ async function getAnnounceData(uw, options) {

module.exports = function announcePlugin(options) {
const hubHost = options.hub || 'https://announce.u-wave.net';
const { publicKey, secretKey } = createKeyPair(options.seed);
const { publicKey, secretKey } = getKeyPair(options.seed);

const announceUrl = `${stripSlashes(hubHost)}/announce/${publicKey.toString('hex')}`;

Expand Down

0 comments on commit 4340206

Please sign in to comment.