1- import * as blst from "@chainsafe/blst" ;
1+ import blst from "@chainsafe/blst" ;
22import { bytesToHex , hexToBytes } from "../helpers/index.js" ;
3- import { PointFormat , Signature as ISignature } from "../types.js" ;
3+ import { CoordType , PointFormat , Signature as ISignature } from "../types.js" ;
44import { PublicKey } from "./publicKey.js" ;
55import { EmptyAggregateError , ZeroSignatureError } from "../errors.js" ;
66
7- export class Signature extends blst . Signature implements ISignature {
8- constructor ( value : ConstructorParameters < typeof blst . Signature > [ 0 ] ) {
9- super ( value ) ;
10- }
7+ export class Signature implements ISignature {
8+ private constructor ( private readonly value : blst . Signature ) { }
119
1210 /** @param type Defaults to `CoordType.affine` */
13- static fromBytes ( bytes : Uint8Array , type ?: blst . CoordType , validate = true ) : Signature {
14- const sig = blst . Signature . fromBytes ( bytes , type ) ;
11+ static fromBytes ( bytes : Uint8Array , type ?: CoordType , validate = true ) : Signature {
12+ // need to hack the CoordType so @chainsafe /blst is not a required dep
13+ const sig = blst . Signature . deserialize ( bytes , ( type as unknown ) as blst . CoordType ) ;
1514 if ( validate ) sig . sigValidate ( ) ;
16- return new Signature ( sig . value ) ;
15+ return new Signature ( sig ) ;
1716 }
1817
1918 static fromHex ( hex : string ) : Signature {
@@ -25,53 +24,75 @@ export class Signature extends blst.Signature implements ISignature {
2524 throw new EmptyAggregateError ( ) ;
2625 }
2726
28- const agg = blst . aggregateSignatures ( signatures ) ;
29- return new Signature ( agg . value ) ;
27+ const agg = blst . aggregateSignatures ( signatures . map ( ( { value } ) => value ) ) ;
28+ return new Signature ( agg ) ;
3029 }
3130
3231 static verifyMultipleSignatures ( sets : { publicKey : PublicKey ; message : Uint8Array ; signature : Signature } [ ] ) : boolean {
3332 return blst . verifyMultipleAggregateSignatures (
34- sets . map ( ( s ) => ( { msg : s . message , pk : s . publicKey , sig : s . signature } ) )
33+ // @ts -expect-error Need to hack type to get access to the private `value`
34+ sets . map ( ( s ) => ( { message : s . message , publicKey : s . publicKey . value , signature : s . signature . value } ) )
3535 ) ;
3636 }
3737
38+ /**
39+ * Implemented for SecretKey to be able to call .sign()
40+ */
41+ private static friendBuild ( sig : blst . Signature ) : Signature {
42+ return new Signature ( sig ) ;
43+ }
44+
3845 verify ( publicKey : PublicKey , message : Uint8Array ) : boolean {
3946 // Individual infinity signatures are NOT okay. Aggregated signatures MAY be infinity
40- if ( this . value . is_inf ( ) ) {
47+ if ( this . value . isInfinity ( ) ) {
4148 throw new ZeroSignatureError ( ) ;
4249 }
4350
44- return blst . verify ( message , publicKey , this ) ;
51+ // @ts -expect-error Need to hack type to get access to the private `value`
52+ return blst . verify ( message , publicKey . value , this . value ) ;
4553 }
4654
4755 verifyAggregate ( publicKeys : PublicKey [ ] , message : Uint8Array ) : boolean {
48- return blst . fastAggregateVerify ( message , publicKeys , this ) ;
56+ return blst . fastAggregateVerify (
57+ message ,
58+ // @ts -expect-error Need to hack type to get access to the private `value`
59+ publicKeys . map ( ( pk ) => pk . value ) ,
60+ this . value
61+ ) ;
4962 }
5063
5164 verifyMultiple ( publicKeys : PublicKey [ ] , messages : Uint8Array [ ] ) : boolean {
52- return blst . aggregateVerify ( messages , publicKeys , this ) ;
65+ return this . aggregateVerify (
66+ messages ,
67+ // @ts -expect-error Need to hack type to get access to the private `value`
68+ publicKeys . map ( ( pk ) => pk . value )
69+ ) ;
5370 }
5471
5572 toBytes ( format ?: PointFormat ) : Uint8Array {
5673 if ( format === PointFormat . uncompressed ) {
57- return this . value . serialize ( ) ;
74+ return this . value . serialize ( false ) ;
5875 } else {
59- return this . value . compress ( ) ;
76+ return this . value . serialize ( true ) ;
6077 }
6178 }
6279
6380 toHex ( format ?: PointFormat ) : string {
6481 return bytesToHex ( this . toBytes ( format ) ) ;
6582 }
6683
84+ multiplyBy ( bytes : Uint8Array ) : Signature {
85+ return new Signature ( this . value . multiplyBy ( bytes ) ) ;
86+ }
87+
6788 private aggregateVerify ( msgs : Uint8Array [ ] , pks : blst . PublicKey [ ] ) : boolean {
6889 // If this set is simply an infinity signature and infinity publicKey then skip verification.
6990 // This has the effect of always declaring that this sig/publicKey combination is valid.
7091 // for Eth2.0 specs tests
71- if ( this . value . is_inf ( ) && pks . length === 1 && pks [ 0 ] . value . is_inf ( ) ) {
92+ if ( this . value . isInfinity ( ) && pks . length === 1 && pks [ 0 ] . isInfinity ( ) ) {
7293 return true ;
7394 }
7495
75- return blst . aggregateVerify ( msgs , pks , this ) ;
96+ return blst . aggregateVerify ( msgs , pks , this . value ) ;
7697 }
7798}
0 commit comments