Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0586baa

Browse filesBrowse files
committed
Added revealTickets method
Added the new revealTickets method and the accompanying error (426). Also includes tests for the method (revealing a single ticket and multiple tickets) and the error. npm release version 1.0.4 -> 1.1.0
1 parent c6f6470 commit 0586baa
Copy full SHA for 0586baa

9 files changed

+264-14Lines changed: 264 additions & 14 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/RandomOrgClient.js‎

Copy file name to clipboardExpand all lines: lib/RandomOrgClient.js
+46-1Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = class RandomOrgClient {
5555
static #GET_RESULT_METHOD = 'getResult';
5656
static #CREATE_TICKET_METHOD = 'createTickets';
5757
static #LIST_TICKET_METHOD = 'listTickets';
58+
static #REVEAL_TICKET_METHOD = 'revealTickets';
5859
static #GET_TICKET_METHOD = 'getTicket';
5960
static #VERIFY_SIGNATURE_METHOD = 'verifySignature';
6061

@@ -117,7 +118,7 @@ module.exports = class RandomOrgClient {
117118

118119
static #ERROR_CODES = [ 100, 101, 200, 201, 202, 203, 204, 300,
119120
301, 302, 303, 304, 305, 306, 307, 400, 401, 402, 403, 404,
120-
405, 420, 421, 422, 423, 424, 425, 500, 32000 ];
121+
405, 420, 421, 422, 423, 424, 425, 426, 500, 32000 ];
121122

122123
/**
123124
* Constructor. Ensures only one instance of RandomOrgClient exists per API
@@ -1278,6 +1279,50 @@ module.exports = class RandomOrgClient {
12781279
return this.#extractResult(this.#sendRequest(request));
12791280
}
12801281

1282+
1283+
/**
1284+
* This method marks a specific ticket and all its predecessors in its chain
1285+
* as being revealed, meaning that subsequent calls to {@link getTicket} will
1286+
* return the full details of the tickets, including the random values produced
1287+
* when the tickets were used. Using this method effectively changes the value
1288+
* of **showResult** (which was specified when the first ticket in the chain
1289+
* was created using {@link createTickets}) from *false* to *true*. The reason
1290+
* that not only the ticket specified (but also its predecessors in its chain)
1291+
* are revealed is to ensure maximum transparency. The method does not affect
1292+
* any successors to the ticket in the chain.
1293+
*
1294+
* If this method is used on a ticket that has not yet been used, a
1295+
* RandomOrgRANDOMORGError (426) will be thrown.
1296+
*
1297+
* See: https://api.random.org/json-rpc/4/signed#revealTickets
1298+
* @param {string} ticketId A string value that uniquely identifies the ticket.
1299+
* @returns {Promise<{ticketCount: int}>} A Promise which, if resolved successfully,
1300+
* represents the number of tickets revealed. This will include the ticket specified
1301+
* as well as all its predecessors. If this method is invoked on a ticket that is
1302+
* already revealed (or which was created with **showResult** set to *true*),
1303+
* then the value returned will be zero.
1304+
* @throws {RandomOrgSendTimeoutError} Thrown when blocking timeout is exceeded
1305+
* before the request can be sent.
1306+
* @throws {RandomOrgKeyNotRunningError} Thrown when the API key has been
1307+
* stopped.
1308+
* @throws {RandomOrgInsufficientRequestsError} Thrown when the API key's server
1309+
* requests allowance has been exceeded.
1310+
* @throws {RandomOrgInsufficientBitsError} Thrown when the API key's server
1311+
* bits allowance has been exceeded.
1312+
* @throws {RandomOrgBadHTTPResponseError} Thrown when a HTTP 200 OK response
1313+
* is not received.
1314+
* @throws {RandomOrgRANDOMORGError} Thrown when the server returns a RANDOM.ORG
1315+
* Error.
1316+
* @throws {RandomOrgJSONRPCError} Thrown when the server returns a JSON-RPC Error.
1317+
*/
1318+
async revealTickets(ticketId) {
1319+
let params = {
1320+
ticketId: ticketId,
1321+
};
1322+
let request = this.#generateKeyedRequest(RandomOrgClient.#REVEAL_TICKET_METHOD, params);
1323+
return this.#extractResult(this.#sendRequest(request));
1324+
}
1325+
12811326
/**
12821327
* Obtains information about tickets linked with your API key.
12831328
*
Collapse file

‎lib/bundles/rdocore.es.js‎

Copy file name to clipboardExpand all lines: lib/bundles/rdocore.es.js
+47-2Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎lib/bundles/rdocore.es.min.js‎

Copy file name to clipboardExpand all lines: lib/bundles/rdocore.es.min.js
+2-2Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎lib/bundles/rdocore.iife.js‎

Copy file name to clipboardExpand all lines: lib/bundles/rdocore.iife.js
+47-2Lines changed: 47 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎lib/bundles/rdocore.iife.min.js‎

Copy file name to clipboardExpand all lines: lib/bundles/rdocore.iife.min.js
+2-2Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎package.json‎

Copy file name to clipboardExpand all lines: package.json
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@randomorg/core",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "The official library to access the RANDOM.ORG JSON-RPC API",
55
"main": "./lib/index.js",
66
"module": "./lib/esm/index.js",
77
"scripts": {
8-
"test": "mocha ./test/test.js --timeout 9000",
9-
"test-coverage": "nyc --reporter=text mocha ./test/test.js --timeout 9000",
8+
"test": "mocha ./test/test.js --timeout 15000",
9+
"test-coverage": "nyc --reporter=text mocha ./test/test.js --timeout 15000",
1010
"build": "npm run build-ts && npm run build-browser && npm run build-test",
1111
"build-ts": "tsc",
1212
"build-browser": "rollup --config --mode='browser' && rollup --config --mode='browser' --minify",
Collapse file

‎test/test.bundle.js‎

Copy file name to clipboardExpand all lines: test/test.bundle.js
+2-2Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎test/test.js‎

Copy file name to clipboardExpand all lines: test/test.js
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ describe('Errors', function() {
173173
&& e.getCode() == 422);
174174
}
175175
});
176+
177+
it('RandomOrgRANDOMORGError 426: ticket has not yet been used', async function() {
178+
let ticket = await roc.createTickets(1, false);
179+
180+
try {
181+
let result = await roc.revealTickets(ticket[0].ticketId);
182+
assert.fail('Should have thrown RandomOrgRANDOMORGError 426.');
183+
} catch (e) {
184+
if (logResponses) {
185+
console.log(e.message);
186+
}
187+
188+
assert(e instanceof errors.RandomOrgRANDOMORGError
189+
&& e.getCode() == 426, 'Should have thrown RandomOrgRANDOMORGError '
190+
+ '426, instead threw ' + e.message);
191+
}
192+
});
176193
});
177194

178195
describe('General', function() {
@@ -980,6 +997,65 @@ describe('Signed', function() {
980997
}
981998
});
982999

1000+
it('revealTickets(), single reveal', async function() {
1001+
let response = await roc.createTickets(1, false);
1002+
let ticket = response[0].ticketId;
1003+
1004+
let result = await roc.generateSignedIntegers(5, 0, 10, {ticketId: ticket});
1005+
1006+
let getTicket = await roc.getTicket(ticket);
1007+
1008+
assert(!getTicket.result, 'Result should not be shown pre-reveal.');
1009+
1010+
let reveal = await roc.revealTickets(ticket);
1011+
1012+
assert(reveal.ticketCount == 1, 'A single ticket should be revealed.');
1013+
1014+
getTicket = await roc.getTicket(ticket);
1015+
1016+
assert(getTicket.result, 'Result should be available post-reveal.');
1017+
1018+
assert.deepEqual(result.data, getTicket.result.random.data,
1019+
'Result should be the same as was generated initially.');
1020+
});
1021+
1022+
it('revealTickets(), multiple reveals', async function() {
1023+
let n = 2;
1024+
1025+
let response = await roc.createTickets(1, false);
1026+
let ticket = response[0].ticketId;
1027+
1028+
let tickets = [];
1029+
let results = [];
1030+
1031+
for (var i = 0; i < n; i++) {
1032+
tickets.push(ticket);
1033+
results.push(await roc.generateSignedIntegers(5, 0, 10, {ticketId: ticket}));
1034+
1035+
var getTicket = await roc.getTicket(ticket);
1036+
1037+
assert(!getTicket.result, 'Result should not be shown pre-reveal.');
1038+
1039+
ticket = getTicket.nextTicketId;
1040+
1041+
assert(ticket != null, 'Next ticket ID should not be null.');
1042+
}
1043+
1044+
let reveal = await roc.revealTickets(tickets[n - 1]);
1045+
1046+
assert(reveal.ticketCount == n, `${n} tickets should be revealed,
1047+
instead ticketCount was ${reveal.ticketCount}.`);
1048+
1049+
for (var i = 0; i < n; i++) {
1050+
getTicket = await roc.getTicket(tickets[i]);
1051+
1052+
assert(getTicket.result, 'Result should be available post-reveal.');
1053+
1054+
assert.deepEqual(results[i].data, getTicket.result.random.data,
1055+
'Result should be the same as was generated initially.');
1056+
}
1057+
});
1058+
9831059
it('listTickets(), singleton', async function() {
9841060
let response = await roc.listTickets('singleton');
9851061

Collapse file

‎types/RandomOrgClient.d.ts‎

Copy file name to clipboardExpand all lines: types/RandomOrgClient.d.ts
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare class RandomOrgClient {
1818
static "__#5@#GET_RESULT_METHOD": string;
1919
static "__#5@#CREATE_TICKET_METHOD": string;
2020
static "__#5@#LIST_TICKET_METHOD": string;
21+
static "__#5@#REVEAL_TICKET_METHOD": string;
2122
static "__#5@#GET_TICKET_METHOD": string;
2223
static "__#5@#VERIFY_SIGNATURE_METHOD": string;
2324
/** Blob format literal, base64 encoding (default). */
@@ -1208,6 +1209,44 @@ declare class RandomOrgClient {
12081209
*/
12091210
nextTicketId: string;
12101211
}[]>;
1212+
/**
1213+
* This method marks a specific ticket and all its predecessors in its chain
1214+
* as being revealed, meaning that subsequent calls to {@link getTicket} will
1215+
* return the full details of the tickets, including the random values produced
1216+
* when the tickets were used. Using this method effectively changes the value
1217+
* of **showResult** (which was specified when the first ticket in the chain
1218+
* was created using {@link createTickets}) from *false* to *true*. The reason
1219+
* that not only the ticket specified (but also its predecessors in its chain)
1220+
* are revealed is to ensure maximum transparency. The method does not affect
1221+
* any successors to the ticket in the chain.
1222+
*
1223+
* If this method is used on a ticket that has not yet been used, a
1224+
* RandomOrgRANDOMORGError (426) will be thrown.
1225+
*
1226+
* See: https://api.random.org/json-rpc/4/signed#revealTickets
1227+
* @param {string} ticketId A string value that uniquely identifies the ticket.
1228+
* @returns {Promise<{ticketCount: int}>} A Promise which, if resolved successfully,
1229+
* represents the number of tickets revealed. This will include the ticket specified
1230+
* as well as all its predecessors. If this method is invoked on a ticket that is
1231+
* already revealed (or which was created with **showResult** set to *true*),
1232+
* then the value returned will be zero.
1233+
* @throws {RandomOrgSendTimeoutError} Thrown when blocking timeout is exceeded
1234+
* before the request can be sent.
1235+
* @throws {RandomOrgKeyNotRunningError} Thrown when the API key has been
1236+
* stopped.
1237+
* @throws {RandomOrgInsufficientRequestsError} Thrown when the API key's server
1238+
* requests allowance has been exceeded.
1239+
* @throws {RandomOrgInsufficientBitsError} Thrown when the API key's server
1240+
* bits allowance has been exceeded.
1241+
* @throws {RandomOrgBadHTTPResponseError} Thrown when a HTTP 200 OK response
1242+
* is not received.
1243+
* @throws {RandomOrgRANDOMORGError} Thrown when the server returns a RANDOM.ORG
1244+
* Error.
1245+
* @throws {RandomOrgJSONRPCError} Thrown when the server returns a JSON-RPC Error.
1246+
*/
1247+
revealTickets(ticketId: string): Promise<{
1248+
ticketCount: int;
1249+
}>;
12111250
/**
12121251
* Obtains information about tickets linked with your API key.
12131252
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.