Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secRnd method #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qubic-lib/qubic-ts-library",
"version": "0.1.2",
"version": "0.1.3",
"author": "J0ET0M",
"license": "Qubic's Anti Military License",
"description": "Typescript Library to communicate with the Qubic Network. http://qubic.li",
Expand Down
23 changes: 23 additions & 0 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ const allocU16 = function (l, v) {
return chunk;
};

/**
* Create a random number between 0 and 1 using a secure random number generator.
* @returns {number} Random number between 0 and 1
*/
const random = function () {
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
// Browser environment
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return array[0] / (0xFFFFFFFF + 1);
} else if (typeof require !== 'undefined') {
// Node.js environment
const crypto = require('crypto');
const buffer = crypto.randomBytes(4);
const randomInt = buffer.readUInt32BE(0);
return randomInt / (0xFFFFFFFF + 1);
} else {
throw new Error('No secure random number generator available.');
}
}

/**
* @namespace Crypto
*/
Expand Down Expand Up @@ -199,6 +220,7 @@ const crypto = new Promise(function (resolve) {
K12,
keccakP160012,
KECCAK_STATE_LENGTH: 200,
secRnd: random,
});
};
});
Expand All @@ -211,5 +233,6 @@ export const PUBLIC_KEY_LENGTH = 32;
export const DIGEST_LENGTH = 32;
export const NONCE_LENGTH = 32;
export const CHECKSUM_LENGTH = 3;
export const secRnd = random;

export default crypto;
16 changes: 8 additions & 8 deletions src/qubicHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class QubicHelper {

public async createProposal(protocol: number, computorIndex: number, operatorSeed: string, url: string): Promise<Uint8Array> {

return crypto.then(({ schnorrq, K12 }) => {
return crypto.then(({ schnorrq, K12, secRnd }) => {

// operator
const operatorPrivateKey = this.privateKey(operatorSeed, 0, K12);
Expand Down Expand Up @@ -405,9 +405,9 @@ export class QubicHelper {
offset++;

// byte[3] dejavu (we let it empty)
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);

// byte type deprecated
proposal[offset] = this.PROCESS_SPECIAL_COMMAND;
Expand Down Expand Up @@ -488,7 +488,7 @@ export class QubicHelper {

public async createBallotRequests(protocol: number, operatorSeed: string, computorIndices: number[], votes: number[]): Promise<Uint8Array[]> {

return crypto.then(({ schnorrq, K12 }) => {
return crypto.then(({ schnorrq, K12, secRnd }) => {

const output: Uint8Array[] = [];

Expand All @@ -515,9 +515,9 @@ export class QubicHelper {
offset++;

// byte[3] dejavu (we let it empty)
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(Math.random() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);
proposal[offset++] = Math.floor(secRnd() * 255);

// byte type (depcrecated)
proposal[offset] = this.PROCESS_SPECIAL_COMMAND;
Expand Down
12 changes: 12 additions & 0 deletions test/createProposal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { QubicHelper } from "../dist/qubicHelper";

test('Generate Proposal', async () => {
const helper = new QubicHelper();

const seed = "wqbdupxgcaimwdsnchitjmsplzclkqokhadgehdxqogeeiovzvadstt";
const publicId = "SUZFFQSCVPHYYBDCQODEMFAOKRJDDDIRJFFIWFLRDDJQRPKMJNOCSSKHXHGK";

const proposal = await helper.createProposal(1,1,seed, 'localhost');

expect(proposal).toBeInstanceOf(Uint8Array);
});
9 changes: 9 additions & 0 deletions test/useSecRndTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const crypto = require('../dist/crypto')

async function main() {
const {secRnd} = await crypto
const randomNum = secRnd()
console.log(randomNum)
}

main()