Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

Commit

Permalink
bls basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avive committed Dec 11, 2018
1 parent dced01f commit 70b5e69
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .babelrc
@@ -1,3 +1,3 @@
{
"presets": ["react-native"]
"presets": ["env", "react-native"]
}
24 changes: 19 additions & 5 deletions __tests__/services/bls.ts
@@ -1,6 +1,20 @@
describe('bls sigs', () => {
it('should be able to sign and verify', () => {
const b = true
expect(b).toEqual(true)
})
import * as Sigs from '../../src/services/signatures'
import * as bls from 'bls-wasm';

test('bls', async () => {

const sigs: Sigs.ISignatures = await Sigs.InitSignaturesLib();

const sk = sigs.NewSecretKey();
sk.dump('Private key: ');

const pk = sk.getPublicKey();
pk.dump('Public key: ');
const msg = "Spacemesh rocks";

const sig = sk.sign(msg);
sig.dump('Signature: ');

expect(pk.verify(sig, msg)).toEqual(true);
});
})
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -49,8 +49,9 @@
"@types/react-router-native": "^4.2.3",
"@types/react-test-renderer": "^16.0.1",
"@types/redux-logger": "^3.0.6",
"babel-core": "^6.0.0",
"babel-jest": "23.0.1",
"babel-core": "^6.26.3",
"babel-jest": "^23.6.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react-native": "4.0.0",
"babel-runtime": "^6.23.0",
"concurrently": "^3.5.1",
Expand All @@ -74,8 +75,7 @@
"wait-on": "^2.0.2"
},
"babel": {
"presets": [
"react-native"
"presets": ["env", "react-native"
]
},
"jest": {
Expand Down
24 changes: 17 additions & 7 deletions src/services/signatures.ts
@@ -1,7 +1,10 @@
import {bls} from 'bls-wasm';
import * as bls from 'bls-wasm';

export interface ISignatures {

// Returns a new random bls.SecretKey
NewSecretKey() : bls.SecretKey;

// Returns a new bls.SecretKey from the provided random seed
NewSecretKeyFromSeed(hexStr: string) : bls.SecretKey;

Expand All @@ -18,7 +21,7 @@ export interface ISignatures {
Verify(pub: bls.PublicKey, sig: bls.Signature, m: string) : boolean;
}

export async function InitSignaturesLib() : Promise<ISignatures> {
export async public function InitSignaturesLib() : Promise<ISignatures> {
const sig = new Signatures();
await sig.Init();
return sig;
Expand All @@ -35,25 +38,32 @@ export class Signatures implements ISignatures {
// Returns a new BLS secret key from provided random seed
// Hex string of random seed. e.g. 0x0214aef
public NewSecretKeyFromSeed(hexStr: string) : bls.SecretKey {
const sec = bls.SecretKey();
const sec = new bls.SecretKey();
const a = bls.fromHexStr(hexStr);
sec.setLittleEndian(a);
return sec;
}

// Returns a new secret key from its hex string serielized form
public NewSecretKeyFromHex(hexStr: string) : bls.SecretKey {
const sec = bls.SecretKey();
public NewSecretKeyFromHex(hexStr: string): bls.SecretKey {
const sec = new bls.SecretKey();
sec.deserialize(hexStr);
return sec;
}

// Returns a new secret key from the provided crypto prng
public NewSecretKey(): bls.SecretKey {
const sec: bls.SecretKey = new bls.SecretKey();
sec.setByCSPRNG();
return sec;
}

// Returns a new private key determinstically derived from sec and index idx
// Used in HW wallet implementation. See relevant BIPs
public GenerateSecretDerivedKey(sec: bls.SecretKey, idx: number) : bls.SecretKey {
// todo: implement key deriviation
const sec1 = bls.SecretKey();
sec1.setByCPRNG();
const sec: bls.SecretKey = new bls.SecretKey();
sec.setByCPRNG();
return sec1;
}

Expand Down

0 comments on commit 70b5e69

Please sign in to comment.