Skip to content

Commit

Permalink
Fixed codeQL warnings (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaMineJP authored Jan 12, 2024
1 parent 8fcdb5e commit cfb09ed
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 51 deletions.
18 changes: 5 additions & 13 deletions js-bindings/tests/PrivateKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,12 @@ before((done) => {
});
});

function makehash(msg) {
return crypto
.createHash('sha256')
.update(msg)
.digest();
}

describe('PrivateKey', () => {
it('Should sign and verify', () => {
const {AugSchemeMPL, PrivateKey} = blsSignatures;
const {AugSchemeMPL} = blsSignatures;

const message1 = Uint8Array.from([1, 65, 254, 88, 90, 45, 22]);

const seed = Uint8Array.from([28, 20, 102, 229, 1, 157]);
const sk1 = AugSchemeMPL.key_gen(getPkSeed());
const pk1 = AugSchemeMPL.sk_to_g1(sk1);
const sig1 = AugSchemeMPL.sign(sk1, message1);
Expand All @@ -75,14 +67,14 @@ describe('PrivateKey', () => {

describe('.fromBytes', () => {
it('Should create a private key from a Buffer', () => {
const {PrivateKey, Util} = blsSignatures;
const {PrivateKey} = blsSignatures;

const pk = PrivateKey.from_bytes(getPkBuffer(), false);
assert(pk instanceof PrivateKey);
assert.deepStrictEqual(pk.serialize(), getPkBuffer());
});
it('Should create a private key from a Uint8Array', () => {
const {PrivateKey, Util} = blsSignatures;
const {PrivateKey} = blsSignatures;

const pk = PrivateKey.from_bytes(getPkUint8Array(), false);
assert(pk instanceof PrivateKey);
Expand All @@ -92,7 +84,7 @@ describe('PrivateKey', () => {

describe('#serialize', () => {
it('Should serialize key to a Buffer', () => {
const {AugSchemeMPL, PrivateKey} = blsSignatures;
const {AugSchemeMPL} = blsSignatures;

const pk = AugSchemeMPL.key_gen(getPkSeed());
const serialized = pk.serialize();
Expand All @@ -117,7 +109,7 @@ describe('PrivateKey', () => {

describe('#getPublicKey', () => {
it('Should return a public key with a verifiable fingerprint', () => {
const {AugSchemeMPL, PrivateKey, G1Element} = blsSignatures;
const {AugSchemeMPL, G1Element} = blsSignatures;

const pk = AugSchemeMPL.key_gen(getPkSeed());
const publicKey = AugSchemeMPL.sk_to_g1(pk);
Expand Down
2 changes: 1 addition & 1 deletion js-bindings/tests/PublicKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ before((done) => {
describe('G1Element', () => {
describe('.from_bytes', () => {
it('Should create a public key from bytes', () => {
const {G1Element, Util} = blsSignatures;
const {G1Element} = blsSignatures;

const pk = G1Element.from_bytes(getPublicKeyFixture().buffer);
assert(pk instanceof G1Element);
Expand Down
12 changes: 6 additions & 6 deletions js-bindings/tests/Signature.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ before((done) => {
describe('Signature', () => {
describe('Integration', () => {
it('Should verify signatures', function () {
const {BasicSchemeMPL, G2Element, G1Element, PrivateKey} = blsSignatures;
const {BasicSchemeMPL} = blsSignatures;

this.timeout(10000);
const message = Uint8Array.from([100, 2, 254, 88, 90, 45, 23]);
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('Signature', () => {
});
describe('.fromBytes', () => {
it('Should create verifiable signature from bytes', () => {
const {AugSchemeMPL, G2Element, Util} = blsSignatures;
const {G2Element} = blsSignatures;

const sig = G2Element.fromBytes(getSignatureBytes());

Expand All @@ -87,7 +87,7 @@ describe('Signature', () => {
});
describe('.aggregateSigs', () => {
it('Should aggregate signature', () => {
const {AugSchemeMPL, G2Element, PrivateKey} = blsSignatures;
const {AugSchemeMPL, G2Element} = blsSignatures;

const sk = AugSchemeMPL.key_gen(makehash(Uint8Array.from([1, 2, 3])));
const pk = AugSchemeMPL.sk_to_g1(sk);
Expand All @@ -107,7 +107,7 @@ describe('Signature', () => {
});
describe('#serialize', () => {
it('Should serialize signature to Buffer', () => {
const {AugSchemeMPL, G2Element, PrivateKey} = blsSignatures;
const {AugSchemeMPL, G2Element} = blsSignatures;

const sk = AugSchemeMPL.key_gen(makehash(Uint8Array.from([1, 2, 3, 4, 5])));
const sig = AugSchemeMPL.sign(sk, Uint8Array.from([100, 2, 254, 88, 90, 45, 23]));
Expand All @@ -120,7 +120,7 @@ describe('Signature', () => {
});
describe('#verify', () => {
it('Should return true if signature can be verified', () => {
const {AugSchemeMPL, G2Element, G1Element} = blsSignatures;
const {AugSchemeMPL} = blsSignatures;

const message = Uint8Array.from(Buffer.from('Message'));
const seed1 = makehash(Buffer.from([1, 2, 3, 4, 5]));
Expand All @@ -144,7 +144,7 @@ describe('Signature', () => {
sig.delete();
});
it("Should return false if signature can't be verified", () => {
const {AugSchemeMPL, G1Element, PrivateKey} = blsSignatures;
const {AugSchemeMPL} = blsSignatures;

const message1 = Uint8Array.from(Buffer.from('Message'));
const message2 = Uint8Array.from(Buffer.from('Nessage'));
Expand Down
1 change: 0 additions & 1 deletion js-bindings/tests/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const testSource = './karma.test.js';
const mime = require('mime');
const fs = require('fs');

function webpackConfig() {
Expand Down
30 changes: 14 additions & 16 deletions js-bindings/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ blsjs().then((blsjs) => {
G2Element,
PopSchemeMPL,
PrivateKey,
Util
} = blsjs;

function test_schemes() {
Expand Down Expand Up @@ -155,7 +154,7 @@ blsjs().then((blsjs) => {
invalid_inputs_1.map((s) => {
const bytes_ = binascii.unhexlify(s);
try {
const g1 = G1Element(bytes_);
G1Element(bytes_);
console.log(`Failed to disallow creation of G1 element for string ${s}`);
assert(false);
} catch(e) {
Expand All @@ -166,7 +165,7 @@ blsjs().then((blsjs) => {
invalid_inputs_2.map((s) => {
const bytes_ = binascii.unhexlify(s);
try {
const g2 = G2Element(bytes_);
G2Element(bytes_);
console.log(`Failed to disallow creation of G2 element for string ${s}`);
assert(false);
} catch(e) {
Expand Down Expand Up @@ -361,7 +360,6 @@ blsjs().then((blsjs) => {

const master_sk = AugSchemeMPL.key_gen(seed);
const child = AugSchemeMPL.derive_child_sk(master_sk, 152);
const grandchild = AugSchemeMPL.derive_child_sk(child, 952);

const master_pk = master_sk.get_g1();
const child_u = AugSchemeMPL.derive_child_sk_unhardened(master_sk, 22);
Expand All @@ -387,15 +385,15 @@ blsjs().then((blsjs) => {
console.log("\nAll tests passed.");
});

const copyright = [
'Copyright 2020 Chia Network Inc',
'Licensed under the Apache License, Version 2.0 (the "License");',
'you may not use this file except in compliance with the License.',
'You may obtain a copy of the License at',
'http://www.apache.org/licenses/LICENSE-2.0',
'Unless required by applicable law or agreed to in writing, software',
'distributed under the License is distributed on an "AS IS" BASIS,',
'WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.',
'See the License for the specific language governing permissions and',
'limitations under the License.'
];
/*
Copyright 2020 Chia Network Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
23 changes: 9 additions & 14 deletions js-bindings/tests/typings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This file is used to check if the typescript typings are working
import createBlsSignaturesModule from '..';
import {deepStrictEqual, ok, strictEqual} from 'assert';
import {createHash} from 'crypto';
import {ok, strictEqual} from 'assert';

createBlsSignaturesModule().then((blsSignatures) => {
const {
Expand All @@ -11,17 +10,14 @@ createBlsSignaturesModule().then((blsSignatures) => {
G2Element
} = blsSignatures;

function makehash(msg : Uint8Array) : Uint8Array {
return createHash('sha256').update(msg).digest();
}

function getSkSeed(): Uint8Array {
return Uint8Array.from([
0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192, 19, 18, 12, 89, 6, 220,
18, 102, 58, 209, 82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22
]);
}

/*
function getSkBytes(): Uint8Array {
return Uint8Array.from([
55, 112, 145, 240, 231, 40, 70, 59,
Expand All @@ -30,6 +26,7 @@ createBlsSignaturesModule().then((blsSignatures) => {
41, 197, 144, 139, 113, 81, 163, 45
]);
}
*/

function getPkBytes(): Uint8Array {
return Uint8Array.from([
Expand All @@ -46,10 +43,6 @@ createBlsSignaturesModule().then((blsSignatures) => {
return Uint8Array.from([1, 2, 3]);
}

function getMessageHash(): Uint8Array {
return Uint8Array.from(createHash('sha256').update(getMessageBytes()).digest());
}

function getSignatureBytes(): Uint8Array {
return Uint8Array.from([
150, 18, 129, 243, 101, 246, 44, 55, 26, 188, 24, 50,
Expand All @@ -63,17 +56,19 @@ createBlsSignaturesModule().then((blsSignatures) => {
]);
}

/*
function getChainCodeBytes(): Uint8Array {
return Uint8Array.from([137, 75, 79, 148, 193, 235, 158, 172, 163, 41, 102, 134, 72, 161, 187, 104, 97, 202, 38, 27, 206, 125, 64, 60, 149, 248, 29, 53, 180, 23, 253, 255]);
}
*/

describe('typings', () => {
it('PrivateKey', () => {
strictEqual(PrivateKey.PRIVATE_KEY_SIZE, 32);
const sk = AugSchemeMPL.key_gen(getSkSeed());
const aggSk = PrivateKey.aggregate([sk]);
const pk = sk.get_g1();
const bytes: Uint8Array = sk.serialize();
sk.serialize();
const sig = AugSchemeMPL.sign(sk, getMessageBytes());
ok(AugSchemeMPL.verify(pk, getMessageBytes(), sig));
aggSk.delete();
Expand All @@ -85,8 +80,8 @@ createBlsSignaturesModule().then((blsSignatures) => {
strictEqual(G1Element.SIZE, 48);
const pk = G1Element.from_bytes(getPkBytes());
const aggPk = pk.add(pk);
const fingerprint: number = pk.get_fingerprint();
const bytes: Uint8Array = pk.serialize();
pk.get_fingerprint();
pk.serialize();
pk.delete();
aggPk.delete();
});
Expand All @@ -99,7 +94,7 @@ createBlsSignaturesModule().then((blsSignatures) => {
const sig2 = G2Element.from_bytes(getSignatureBytes());
const isValid: boolean =
AugSchemeMPL.verify(pk, getMessageBytes(), sig);
const serialized: Uint8Array = sig.serialize();
sig.serialize();
ok(isValid);
sig.delete();
aggSig.delete();
Expand Down

0 comments on commit cfb09ed

Please sign in to comment.