minenepal-votifier 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/index.js +107 -0
- package/package.json +26 -0
- package/test/vote.test.js +142 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Biraj Rai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# MineNepal Votifier
|
|
2
|
+
|
|
3
|
+
# MineNepal Votifier
|
|
4
|
+
|
|
5
|
+
Lightweight Votifier v2 client helper used by MineNepal to send signed vote messages to a Votifier v2 server.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install from npm:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install --save minenepal-votifier
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
The module exports a function `vote(options, cb)` and also supports a Promise-based call when the callback is omitted.
|
|
18
|
+
|
|
19
|
+
Example (callback):
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
const vote = require('minenepal-votifier');
|
|
23
|
+
|
|
24
|
+
const options = {
|
|
25
|
+
host: '127.0.0.1',
|
|
26
|
+
port: 8192,
|
|
27
|
+
token: 'MYTOKEN',
|
|
28
|
+
vote: {
|
|
29
|
+
username: 'Herobrine',
|
|
30
|
+
address: '127.0.0.1',
|
|
31
|
+
timestamp: Date.now(),
|
|
32
|
+
serviceName: 'MineNepal',
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
vote(options, err => {
|
|
37
|
+
if (err) return console.error('Vote failed:', err);
|
|
38
|
+
console.log('Vote delivered');
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Example (Promise):
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
const vote = require('minenepal-votifier');
|
|
46
|
+
|
|
47
|
+
(async () => {
|
|
48
|
+
try {
|
|
49
|
+
await vote(options);
|
|
50
|
+
console.log('Vote delivered');
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.error('Vote failed:', err);
|
|
53
|
+
}
|
|
54
|
+
})();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Options
|
|
58
|
+
|
|
59
|
+
- `host` (string) - Votifier server host
|
|
60
|
+
- `port` (number) - Votifier server port
|
|
61
|
+
- `token` (string) - Shared secret/token for HMAC signing
|
|
62
|
+
- `vote` (object) - Vote payload containing `username`, `address`, `timestamp`, `serviceName`
|
|
63
|
+
- `timeout` (number, optional) - Socket timeout in milliseconds (default 2000)
|
|
64
|
+
|
|
65
|
+
## Behavior & Errors
|
|
66
|
+
|
|
67
|
+
- Throws or rejects on invalid arguments.
|
|
68
|
+
- Calls back with an Error or resolves the Promise on failure.
|
|
69
|
+
- Returns successfully (callback with null / resolved Promise) when the server accepts the vote.
|
|
70
|
+
|
|
71
|
+
## Example file
|
|
72
|
+
|
|
73
|
+
See `example.js` for a concrete runnable example.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
78
|
+
|
|
79
|
+
## Server frameworks
|
|
80
|
+
|
|
81
|
+
These short examples show how to use the package from server-side code. Never call this from browser/client code — the `token` is a secret and must stay on the server.
|
|
82
|
+
|
|
83
|
+
### Express (route example)
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
const express = require('express');
|
|
87
|
+
const vote = require('minenepal-votifier');
|
|
88
|
+
|
|
89
|
+
const app = express();
|
|
90
|
+
app.use(express.json());
|
|
91
|
+
|
|
92
|
+
app.post('/api/vote', async (req, res) => {
|
|
93
|
+
const { username, address } = req.body;
|
|
94
|
+
const options = {
|
|
95
|
+
host: process.env.VOTIFIER_HOST,
|
|
96
|
+
port: Number(process.env.VOTIFIER_PORT),
|
|
97
|
+
token: process.env.VOTIFIER_TOKEN,
|
|
98
|
+
vote: {
|
|
99
|
+
username,
|
|
100
|
+
address,
|
|
101
|
+
timestamp: Date.now(),
|
|
102
|
+
serviceName: process.env.SERVICE_NAME || 'MineNepal',
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await vote(options);
|
|
108
|
+
res.status(200).json({ ok: true });
|
|
109
|
+
} catch (err) {
|
|
110
|
+
res.status(502).json({ error: err.message });
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
app.listen(3000);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Next.js (API Route)
|
|
118
|
+
|
|
119
|
+
For Next.js pages/api route (Next 12/13):
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
// pages/api/vote.js
|
|
123
|
+
import vote from 'minenepal-votifier';
|
|
124
|
+
|
|
125
|
+
export default async function handler(req, res) {
|
|
126
|
+
if (req.method !== 'POST') return res.status(405).end();
|
|
127
|
+
const { username, address } = req.body;
|
|
128
|
+
|
|
129
|
+
const options = {
|
|
130
|
+
host: process.env.VOTIFIER_HOST,
|
|
131
|
+
port: Number(process.env.VOTIFIER_PORT),
|
|
132
|
+
token: process.env.VOTIFIER_TOKEN,
|
|
133
|
+
vote: { username, address, timestamp: Date.now(), serviceName: process.env.SERVICE_NAME },
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
await vote(options);
|
|
138
|
+
res.status(200).json({ ok: true });
|
|
139
|
+
} catch (err) {
|
|
140
|
+
res.status(502).json({ error: err.message });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
If you're using the App Router or server components, the same server-side logic applies — ensure the code runs only on the server and environment variables are configured.
|
|
146
|
+
|
|
147
|
+
### Security & Deployment notes
|
|
148
|
+
|
|
149
|
+
- Keep `VOTIFIER_TOKEN` in server-side environment variables or a secrets manager.
|
|
150
|
+
- Do not log the token or full signed payload in production.
|
|
151
|
+
- Consider running your Votifier connection from a backend worker or queue if you expect high volume, to avoid delaying HTTP responses.
|
package/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const net = require('net');
|
|
5
|
+
|
|
6
|
+
function createMessage(header, vote, options) {
|
|
7
|
+
const data = header.trim().split(/\s+/);
|
|
8
|
+
|
|
9
|
+
if (data.length < 3) {
|
|
10
|
+
throw new Error('Not a Votifier v2 protocol server');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// data[2] is the challenge token (may include newline)
|
|
14
|
+
vote.challenge = data[2].replace(/\r?\n$/, '');
|
|
15
|
+
const voteAsJson = JSON.stringify(vote);
|
|
16
|
+
const digest = crypto.createHmac('sha256', options.token);
|
|
17
|
+
digest.update(voteAsJson);
|
|
18
|
+
const sig = digest.digest('base64');
|
|
19
|
+
|
|
20
|
+
const message = JSON.stringify({ payload: voteAsJson, signature: sig });
|
|
21
|
+
const messageLength = Buffer.byteLength(message, 'utf8');
|
|
22
|
+
const messageBuffer = Buffer.alloc(4 + messageLength);
|
|
23
|
+
messageBuffer.writeUInt16BE(0x733a, 0);
|
|
24
|
+
messageBuffer.writeUInt16BE(messageLength, 2);
|
|
25
|
+
messageBuffer.write(message, 4, 'utf8');
|
|
26
|
+
return messageBuffer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _vote(options, cb) {
|
|
30
|
+
if (!options || typeof options !== 'object') return cb(new Error('options must be an object'));
|
|
31
|
+
if (!options.host || !options.port || !options.token || !options.vote) {
|
|
32
|
+
return cb(new Error("missing host, port, token, or vote in 'options'"));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const vote = options.vote;
|
|
36
|
+
if (!vote.username || !vote.address || !vote.timestamp || !vote.serviceName) {
|
|
37
|
+
return cb(new Error("missing username, address, timestamp, or serviceName in 'vote'"));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const timeoutMs = typeof options.timeout === 'number' ? options.timeout : 2000;
|
|
41
|
+
|
|
42
|
+
const socket = net.createConnection({ port: options.port, host: options.host });
|
|
43
|
+
let finished = false;
|
|
44
|
+
|
|
45
|
+
function done(err) {
|
|
46
|
+
if (finished) return;
|
|
47
|
+
finished = true;
|
|
48
|
+
try {
|
|
49
|
+
socket.setTimeout(0);
|
|
50
|
+
} catch (e) {}
|
|
51
|
+
try {
|
|
52
|
+
socket.removeAllListeners();
|
|
53
|
+
} catch (e) {}
|
|
54
|
+
try {
|
|
55
|
+
socket.end();
|
|
56
|
+
} catch (e) {}
|
|
57
|
+
cb(err || null);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
socket.setTimeout(timeoutMs, function () {
|
|
61
|
+
done(new Error('Socket timeout'));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
socket.on('error', function (err) {
|
|
65
|
+
done(new Error('Socket error: ' + (err && err.message ? err.message : err)));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
socket.once('data', function (buf) {
|
|
69
|
+
try {
|
|
70
|
+
const message = createMessage(buf.toString('utf8'), vote, options);
|
|
71
|
+
socket.write(message);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
return done(e);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
socket.once('data', function (respBuf) {
|
|
77
|
+
try {
|
|
78
|
+
const respText = respBuf.toString('utf8');
|
|
79
|
+
const resp = JSON.parse(respText);
|
|
80
|
+
if (resp && resp.status === 'error') {
|
|
81
|
+
const cause = resp.cause || 'error';
|
|
82
|
+
const msg = resp.errorMessage || resp.message || 'unknown';
|
|
83
|
+
return done(new Error(cause + ': ' + msg));
|
|
84
|
+
}
|
|
85
|
+
return done(null);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
return done(e);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
socket.on('end', function () {
|
|
93
|
+
if (!finished) done(new Error('Socket ended unexpectedly'));
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = function vote(options, cb) {
|
|
98
|
+
if (typeof cb !== 'function') {
|
|
99
|
+
return new Promise(function (resolve, reject) {
|
|
100
|
+
_vote(options, function (err) {
|
|
101
|
+
if (err) return reject(err);
|
|
102
|
+
resolve();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return _vote(options, cb);
|
|
107
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "minenepal-votifier",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A Votifier protocol v2 client for MineNepal",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test/vote.test.js",
|
|
8
|
+
"pretest": "node -e \"console.log('Running tests...')\""
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/birajrai/minenepal-votifier.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"votifier",
|
|
16
|
+
"minecraft",
|
|
17
|
+
"votifier-v2"
|
|
18
|
+
],
|
|
19
|
+
"author": "birajrai",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/birajrai/minenepal-votifier/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/birajrai/minenepal-votifier",
|
|
25
|
+
"dependencies": {}
|
|
26
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const net = require('net');
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const vote = require('../index');
|
|
5
|
+
|
|
6
|
+
function timeout(ms) {
|
|
7
|
+
return new Promise(r => setTimeout(r, ms));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function testSendSignedVote() {
|
|
11
|
+
const server = net.createServer();
|
|
12
|
+
await new Promise(res => server.listen(0, '127.0.0.1', res));
|
|
13
|
+
const port = server.address().port;
|
|
14
|
+
const token = 'MYTOKEN';
|
|
15
|
+
|
|
16
|
+
server.on('connection', socket => {
|
|
17
|
+
socket.write('VOTIFIER 2 challenge\n');
|
|
18
|
+
let buf = Buffer.alloc(0);
|
|
19
|
+
socket.on('data', data => {
|
|
20
|
+
buf = Buffer.concat([buf, data]);
|
|
21
|
+
if (buf.length >= 4) {
|
|
22
|
+
const magic = buf.readUInt16BE(0);
|
|
23
|
+
assert.strictEqual(magic, 0x733a);
|
|
24
|
+
const len = buf.readUInt16BE(2);
|
|
25
|
+
if (buf.length >= 4 + len) {
|
|
26
|
+
const msg = buf.slice(4, 4 + len).toString('utf8');
|
|
27
|
+
const obj = JSON.parse(msg);
|
|
28
|
+
assert.ok(obj.payload);
|
|
29
|
+
assert.ok(obj.signature);
|
|
30
|
+
const payload = JSON.parse(obj.payload);
|
|
31
|
+
const expected = crypto
|
|
32
|
+
.createHmac('sha256', token)
|
|
33
|
+
.update(JSON.stringify(payload))
|
|
34
|
+
.digest('base64');
|
|
35
|
+
assert.strictEqual(obj.signature, expected);
|
|
36
|
+
socket.write(JSON.stringify({ status: 'ok' }));
|
|
37
|
+
socket.end();
|
|
38
|
+
server.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const options = {
|
|
45
|
+
host: '127.0.0.1',
|
|
46
|
+
port,
|
|
47
|
+
token,
|
|
48
|
+
vote: {
|
|
49
|
+
username: 'tester',
|
|
50
|
+
address: '127.0.0.1',
|
|
51
|
+
timestamp: Date.now(),
|
|
52
|
+
serviceName: 'MineNepal',
|
|
53
|
+
},
|
|
54
|
+
timeout: 2000,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
await vote(options);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function testInvalidOptions() {
|
|
61
|
+
try {
|
|
62
|
+
await vote(null);
|
|
63
|
+
throw new Error('expected rejection');
|
|
64
|
+
} catch (err) {
|
|
65
|
+
assert.ok(err instanceof Error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function testCallbackAPI() {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const server = net.createServer();
|
|
72
|
+
server.listen(0, '127.0.0.1', () => {
|
|
73
|
+
const port = server.address().port;
|
|
74
|
+
const token = 'MYTOKEN';
|
|
75
|
+
|
|
76
|
+
server.on('connection', socket => {
|
|
77
|
+
socket.write('VOTIFIER 2 c\n');
|
|
78
|
+
let buf = Buffer.alloc(0);
|
|
79
|
+
socket.on('data', data => {
|
|
80
|
+
buf = Buffer.concat([buf, data]);
|
|
81
|
+
if (buf.length >= 4) {
|
|
82
|
+
const len = buf.readUInt16BE(2);
|
|
83
|
+
if (buf.length >= 4 + len) {
|
|
84
|
+
socket.write(JSON.stringify({ status: 'ok' }));
|
|
85
|
+
socket.end();
|
|
86
|
+
server.close();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const options = {
|
|
93
|
+
host: '127.0.0.1',
|
|
94
|
+
port,
|
|
95
|
+
token,
|
|
96
|
+
vote: {
|
|
97
|
+
username: 'cbuser',
|
|
98
|
+
address: '127.0.0.1',
|
|
99
|
+
timestamp: Date.now(),
|
|
100
|
+
serviceName: 'MineNepal',
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
vote(options, function (err) {
|
|
105
|
+
if (err) return reject(err);
|
|
106
|
+
resolve();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function run() {
|
|
113
|
+
const tests = [
|
|
114
|
+
{ name: 'sends a signed vote (Promise API)', fn: testSendSignedVote },
|
|
115
|
+
{ name: 'rejects when options are invalid', fn: testInvalidOptions },
|
|
116
|
+
{ name: 'works with callback API', fn: testCallbackAPI },
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
for (const t of tests) {
|
|
120
|
+
try {
|
|
121
|
+
await t.fn();
|
|
122
|
+
console.log('✔', t.name);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
console.error('✖', t.name);
|
|
125
|
+
console.error(err && err.stack ? err.stack : err);
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
// small pause to let sockets close cleanly
|
|
130
|
+
await timeout(20);
|
|
131
|
+
}
|
|
132
|
+
console.log('\nAll tests passed');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (require.main === module) {
|
|
136
|
+
run().catch(e => {
|
|
137
|
+
console.error(e);
|
|
138
|
+
process.exit(1);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = { testSendSignedVote, testInvalidOptions, testCallbackAPI };
|