Refactor Hash to Root/bytes32

This commit is contained in:
Tuyen 2019-12-22 15:46:52 +07:00
parent 87a4a962f5
commit 1cf1c27b94
4 changed files with 11 additions and 11 deletions

View File

@ -2,7 +2,7 @@ import {Keypair} from "./keypair";
import {PrivateKey} from "./privateKey"; import {PrivateKey} from "./privateKey";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
import {Signature} from "./signature"; import {Signature} from "./signature";
import {BLSPubkey, BLSSecretKey, BLSSignature, Domain, Hash} from "@chainsafe/eth2.0-types"; import {BLSPubkey, BLSSecretKey, BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types";
import {PUBLIC_KEY_LENGTH} from "./constants"; import {PUBLIC_KEY_LENGTH} from "./constants";
export {Keypair, PrivateKey, PublicKey, Signature}; export {Keypair, PrivateKey, PublicKey, Signature};
@ -31,7 +31,7 @@ export function generatePublicKey(secretKey: BLSSecretKey): BLSPubkey {
* @param messageHash * @param messageHash
* @param domain * @param domain
*/ */
export function sign(secretKey: BLSSecretKey, messageHash: Hash, domain: Domain): BLSSignature { export function sign(secretKey: BLSSecretKey, messageHash: bytes32, domain: Domain): BLSSignature {
const privateKey = PrivateKey.fromBytes(secretKey); const privateKey = PrivateKey.fromBytes(secretKey);
return privateKey.signMessage(messageHash, domain).toBytesCompressed(); return privateKey.signMessage(messageHash, domain).toBytesCompressed();
} }
@ -73,7 +73,7 @@ export function aggregatePubkeys(publicKeys: BLSPubkey[]): BLSPubkey {
* @param signature * @param signature
* @param domain * @param domain
*/ */
export function verify(publicKey: BLSPubkey, messageHash: Hash, signature: BLSSignature, domain: Domain): boolean { export function verify(publicKey: BLSPubkey, messageHash: bytes32, signature: BLSSignature, domain: Domain): boolean {
try { try {
return PublicKey return PublicKey
.fromBytes(publicKey) .fromBytes(publicKey)
@ -92,7 +92,7 @@ export function verify(publicKey: BLSPubkey, messageHash: Hash, signature: BLSSi
*/ */
export function verifyMultiple( export function verifyMultiple(
publicKeys: BLSPubkey[], publicKeys: BLSPubkey[],
messageHashes: Hash[], messageHashes: bytes32[],
signature: BLSSignature, signature: BLSSignature,
domain: Domain domain: Domain
): boolean { ): boolean {

View File

@ -1,6 +1,6 @@
import {SECRET_KEY_LENGTH} from "./constants"; import {SECRET_KEY_LENGTH} from "./constants";
import assert from "assert"; import assert from "assert";
import {BLSSecretKey, Domain, Hash} from "@chainsafe/eth2.0-types"; import {BLSSecretKey, Domain, bytes32} from "@chainsafe/eth2.0-types";
import {SecretKeyType} from "@chainsafe/eth2-bls-wasm"; import {SecretKeyType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context"; import {getContext} from "./context";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
@ -52,7 +52,7 @@ export class PrivateKey {
// return Signature.fromValue(this.value.sign(message)); // return Signature.fromValue(this.value.sign(message));
// } // }
public signMessage(message: Hash, domain: Domain): Signature { public signMessage(message: bytes32, domain: Domain): Signature {
domain = padLeft(domain, 8); domain = padLeft(domain, 8);
return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain]))); return Signature.fromValue(this.value.signHashWithDomain(Buffer.concat([message, domain])));
} }

View File

@ -1,5 +1,5 @@
import {PrivateKey} from "./privateKey"; import {PrivateKey} from "./privateKey";
import {BLSPubkey, Domain, Hash} from "@chainsafe/eth2.0-types"; import {BLSPubkey, Domain, bytes32} from "@chainsafe/eth2.0-types";
import {PublicKeyType} from "@chainsafe/eth2-bls-wasm"; import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context"; import {getContext} from "./context";
import {PUBLIC_KEY_LENGTH} from "./constants"; import {PUBLIC_KEY_LENGTH} from "./constants";
@ -49,7 +49,7 @@ export class PublicKey {
return agg; return agg;
} }
public verifyMessage(signature: Signature, messageHash: Hash, domain: Domain): boolean { public verifyMessage(signature: Signature, messageHash: bytes32, domain: Domain): boolean {
return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain])); return this.value.verifyHashWithDomain(signature.getValue(), Buffer.concat([messageHash, domain]));
} }

View File

@ -1,6 +1,6 @@
import assert from "assert"; import assert from "assert";
import {FP_POINT_LENGTH} from "./constants"; import {FP_POINT_LENGTH} from "./constants";
import {BLSSignature, Domain, Hash} from "@chainsafe/eth2.0-types"; import {BLSSignature, Domain, bytes32} from "@chainsafe/eth2.0-types";
import {SignatureType} from "@chainsafe/eth2-bls-wasm"; import {SignatureType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context"; import {getContext} from "./context";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
@ -43,12 +43,12 @@ export class Signature {
return this.value; return this.value;
} }
public verify(publicKey: PublicKey, message: Hash, domain: Domain): boolean { public verify(publicKey: PublicKey, message: bytes32, domain: Domain): boolean {
domain = padLeft(domain, 8); domain = padLeft(domain, 8);
return publicKey.verifyMessage(this, message, domain); return publicKey.verifyMessage(this, message, domain);
} }
public verifyMultiple(publicKeys: PublicKey[], messages: Hash[], domain: Domain): boolean { public verifyMultiple(publicKeys: PublicKey[], messages: bytes32[], domain: Domain): boolean {
domain = padLeft(domain, 8); domain = padLeft(domain, 8);
return this.value.verifyAggregatedHashWithDomain( return this.value.verifyAggregatedHashWithDomain(
publicKeys.map((key) => key.getValue()), publicKeys.map((key) => key.getValue()),