Skip to content

Commit

Permalink
Merge pull request #27 from ChainSafe/mpetrunic/secretkey-utility-met…
Browse files Browse the repository at this point in the history
…hods

Utility methods for private key
  • Loading branch information
mpetrunic committed May 14, 2019
2 parents 84f224e + 44c5bb0 commit c7fd9d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/bls-js",
"version": "0.1.1",
"version": "0.1.2",
"description": "Implementation of bls signature verification for ethereum 2.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
21 changes: 13 additions & 8 deletions src/privateKey.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {BIG} from "@chainsafe/amcl/ctx";
import {SECRET_KEY_LENGTH} from "./constants";
import {FP_POINT_LENGTH, SECRET_KEY_LENGTH} from "./constants";
import assert from "assert";
import ctx from "./ctx";
import {padLeft} from "./helpers/utils";
import {G2point} from "./helpers/g2point";
import * as random from "secure-random";
import {BLSDomain, bytes32} from "./types";
import {BLSDomain, BLSSecretKey, bytes32} from "./types";

export class PrivateKey {

Expand All @@ -27,6 +27,16 @@ export class PrivateKey {
return G2point.hashToG2(message, domain).mul(this.value);
}

public toBytes(): BLSSecretKey {
const buffer = Buffer.alloc(FP_POINT_LENGTH, 0);
this.value.tobytearray(buffer, 0);
return buffer.slice(FP_POINT_LENGTH - SECRET_KEY_LENGTH);
}

public toHexString(): string {
return `0x${this.toBytes().toString('hex')}`;
}

public static fromBytes(bytes: Uint8Array): PrivateKey {
assert(bytes.length === SECRET_KEY_LENGTH, 'Private key should have 32 bytes');
const value = Buffer.from(bytes);
Expand All @@ -48,12 +58,7 @@ export class PrivateKey {
}

public static random(): PrivateKey {
return new PrivateKey(
ctx.BIG.frombytearray(
padLeft(random.randomBuffer(SECRET_KEY_LENGTH), 48),
0
)
)
return PrivateKey.fromBytes(random.randomBuffer(SECRET_KEY_LENGTH));
}

}
22 changes: 22 additions & 0 deletions tests/unit/privateKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {PrivateKey} from "../../src/privateKey";
import {expect} from "chai";
import {SECRET_KEY_LENGTH} from "../../src/constants";

describe('privateKey', function() {

it('should generate random private key', function () {
const privateKey1 = PrivateKey.random();
const privateKey2 = PrivateKey.random();
expect(privateKey1).to.not.be.equal(privateKey2);
});

it('should export private key to hex string', function () {
const privateKey = '0x9a88071ff0634f6515c7699c97d069dc4b2fa28455f6b457e92d1c1302f0c6bb';
expect(PrivateKey.fromHexString(privateKey).toHexString()).to.be.equal(privateKey);
});

it('should export private key to bytes', function () {
expect(PrivateKey.random().toBytes().length).to.be.equal(SECRET_KEY_LENGTH);
});

});

0 comments on commit c7fd9d7

Please sign in to comment.