11import { binding } from "./binding.js" ;
2- import { MAX_AGGREGATE_PER_JOB } from "./const.js" ;
3- import { PublicKey , writePublicKeysReference } from "./publicKey.js" ;
4- import { Signature , writeSignaturesReference } from "./signature.js" ;
2+ import { MAX_AGGREGATE_PER_JOB , PUBLIC_KEY_SIZE , SIGNATURE_LENGTH } from "./const.js" ;
3+ import { PublicKey } from "./publicKey.js" ;
4+ import { writeSignaturesReference , writePublicKeysReference , writeUint8ArrayArray } from "./writers.ts" ;
5+ import { writePublicKeys , pksU8 , writeSignatures , sigsU8 } from "./buffer.ts" ;
6+ import { Signature } from "./signature.js" ;
57
68// global public keys reference to be reused across multiple calls
79// each 2 items are 8 bytes, store the reference of each public key
810const publicKeysRef = new Uint32Array ( MAX_AGGREGATE_PER_JOB * 2 ) ;
9-
1011const signaturesRef = new Uint32Array ( MAX_AGGREGATE_PER_JOB * 2 ) ;
1112
1213/**
@@ -15,6 +16,7 @@ const signaturesRef = new Uint32Array(MAX_AGGREGATE_PER_JOB * 2);
1516 * If `pks_validate` is `true`, the public keys will be infinity and group checked.
1617 */
1718export function aggregatePublicKeys ( pks : Array < PublicKey > , pksValidate ?: boolean | undefined | null ) : PublicKey {
19+
1820 if ( pks . length === 0 ) {
1921 throw new Error ( "At least one public key is required" ) ;
2022 }
@@ -23,16 +25,17 @@ export function aggregatePublicKeys(pks: Array<PublicKey>, pksValidate?: boolean
2325
2426 for ( let i = 0 ; i < pks . length ; i += MAX_AGGREGATE_PER_JOB ) {
2527 const pksBatch = pks . slice ( i , Math . min ( pks . length , i + MAX_AGGREGATE_PER_JOB ) ) ;
26- const pksRef = writePublicKeysReference ( pksBatch ) ;
27- const outPk = PublicKey . defaultPublicKey ( ) ;
28- const res = binding . aggregatePublicKeys ( outPk . ptr , pksRef , pksBatch . length , pksValidate ?? false ) ;
28+ writePublicKeys ( pksBatch ) ;
29+ const outPk = new PublicKey ( new Uint8Array ( PUBLIC_KEY_SIZE ) ) ;
30+ const res = binding . publicKeyAggregate ( outPk . ptr , pksU8 , pksBatch . length , pksValidate ?? false ) ;
2931
3032 if ( res !== 0 ) {
3133 throw new Error ( `Failed to aggregate public keys: ${ res } ` ) ;
3234 }
3335 resultPks . push ( outPk ) ;
3436 }
3537
38+
3639 return resultPks . length === 1 ? resultPks [ 0 ] : aggregatePublicKeys ( resultPks , pksValidate ) ;
3740}
3841
@@ -50,9 +53,9 @@ export function aggregateSignatures(sigs: Array<Signature>, sigsGroupcheck?: boo
5053
5154 for ( let i = 0 ; i < sigs . length ; i += MAX_AGGREGATE_PER_JOB ) {
5255 const sigsBatch = sigs . slice ( i , Math . min ( sigs . length , i + MAX_AGGREGATE_PER_JOB ) ) ;
53- const sigsRef = writeSignaturesReference ( sigsBatch ) ;
54- const outSig = Signature . defaultSignature ( ) ;
55- const res = binding . aggregateSignatures ( outSig . ptr , sigsRef , sigsBatch . length , sigsGroupcheck ?? false ) ;
56+ writeSignatures ( sigsBatch ) ;
57+ const outSig = new Signature ( new Uint8Array ( SIGNATURE_LENGTH ) ) ;
58+ const res = binding . signatureAggregate ( outSig . ptr , sigsU8 , sigsBatch . length , sigsGroupcheck ?? false ) ;
5659
5760 if ( res !== 0 ) {
5861 throw new Error ( `Failed to aggregate signatures: ${ res } ` ) ;
@@ -81,8 +84,8 @@ export function aggregateSerializedPublicKeys(
8184 for ( let i = 0 ; i < pks . length ; i += MAX_AGGREGATE_PER_JOB ) {
8285 const pksBatch = pks . slice ( i , Math . min ( pks . length , i + MAX_AGGREGATE_PER_JOB ) ) ;
8386 const pksRef = writeSerializedPublicKeysReference ( pksBatch ) ;
84- const outPk = PublicKey . defaultPublicKey ( ) ;
85- const res = binding . aggregateSerializedPublicKeys (
87+ const outPk = new PublicKey ( new Uint8Array ( PUBLIC_KEY_SIZE ) ) ;
88+ const res = binding . aggregatePublicKeys (
8689 outPk . ptr ,
8790 pksRef ,
8891 pksBatch . length ,
@@ -117,12 +120,11 @@ export function aggregateSerializedSignatures(
117120 for ( let i = 0 ; i < sigs . length ; i += MAX_AGGREGATE_PER_JOB ) {
118121 const sigsBatch = sigs . slice ( i , Math . min ( sigs . length , i + MAX_AGGREGATE_PER_JOB ) ) ;
119122 const sigsRef = writeSerializedSignaturesReference ( sigsBatch ) ;
120- const outSig = Signature . defaultSignature ( ) ;
121- const res = binding . aggregateSerializedSignatures (
123+ const outSig = new Signature ( new Uint8Array ( SIGNATURE_LENGTH ) ) ;
124+ const res = binding . signatureAggregate (
122125 outSig . ptr ,
123126 sigsRef ,
124127 sigsBatch . length ,
125- sigs [ 0 ] . length ,
126128 sigsGroupcheck ?? false
127129 ) ;
128130
@@ -132,7 +134,7 @@ export function aggregateSerializedSignatures(
132134 resultSignatures . push ( outSig ) ;
133135 }
134136
135- return resultSignatures . length === 1 ? resultSignatures [ 0 ] : aggregateSignatures ( resultSignatures , sigsGroupcheck ) ;
137+ return resultSignatures . length === 1 ? resultSignatures [ 0 ] : aggregateSerializedSignatures ( resultSignatures , sigsGroupcheck ) ;
136138}
137139
138140function writeSerializedPublicKeysReference ( pks : Uint8Array [ ] ) : Uint32Array {
0 commit comments