@@ -276,8 +276,29 @@ export class PolycentricClient {
276276
277277 const sequence = this . core . nextSequence ( this . activeIdentityKey , collection ) ;
278278
279+ // identity_sequence must reference an identity event signed by the
280+ // current keypair.
279281 const identitySequence =
280- this . core . nextSequence ( this . activeIdentityKey , COLLECTION . IDENTITY ) - 1n ;
282+ collection === COLLECTION . IDENTITY
283+ ? sequence
284+ : this . core . getIdentitySequence (
285+ this . activeIdentityKey ,
286+ Proto . PublicKey . toBinary ( this . currentKeyPair . publicKey )
287+ . buffer as ArrayBuffer ,
288+ ) ;
289+
290+ if ( ! identitySequence ) {
291+ throw new Error (
292+ 'Cannot build event: current keypair has no identity event for the active identity (broken pairing?)' ,
293+ ) ;
294+ }
295+
296+ const previousSignature = new Uint8Array (
297+ this . core . previousSignature ( this . activeIdentityKey , collection ) ,
298+ ) ;
299+ const previousRoot = new Uint8Array (
300+ this . core . previousRoot ( this . activeIdentityKey , collection ) ,
301+ ) ;
281302
282303 const event = Proto . Event . create ( {
283304 key : Proto . EventKey . create ( {
@@ -287,7 +308,8 @@ export class PolycentricClient {
287308 sequence,
288309 } ) ,
289310 identitySequence,
290- previousSignature : new Uint8Array ( 0 ) ,
311+ previousSignature,
312+ previousRoot,
291313 contentDigest : this . contentManager . buildDigest ( content ) ,
292314 createdAt : BigInt ( Date . now ( ) ) ,
293315 } ) ;
@@ -575,18 +597,17 @@ export class PolycentricClient {
575597 */
576598 async push ( ) : Promise < void > {
577599 if ( ! this . currentKeyPair ) throw new Error ( 'No active key pair' ) ;
600+ if ( ! this . activeIdentityKey ) throw new Error ( 'No active identity' ) ;
578601
579602 const localEvents = await this . storage . events . getAll ( ) ;
580- const publicKey = this . currentKeyPair . publicKey . key ;
581603
582- // Build event bundles with content for events matching the active key
604+ // Build event bundles with content for events matching the active identity
583605 const bundles : Proto . EventBundle [ ] = [ ] ;
584606 for ( const signedEvent of localEvents ) {
585607 const event = Proto . Event . fromBinary ( signedEvent . eventBytes ) ;
586608
587- // Only push events signed by the active key
588- const signedBy = event . key ?. signedBy ;
589- if ( ! signedBy || ! this . bytesEqual ( signedBy . key , publicKey ) ) continue ;
609+ // Only push events belonging to the active identity
610+ if ( event . key ?. identity !== this . activeIdentityKey ) continue ;
590611
591612 // Look up content by digest
592613 let serializedContent : Proto . SerializedContent | undefined ;
@@ -746,14 +767,6 @@ export class PolycentricClient {
746767 this . events . emitError ( error ) ;
747768 }
748769
749- private bytesEqual ( a : Uint8Array , b : Uint8Array ) : boolean {
750- if ( a . length !== b . length ) return false ;
751- for ( let i = 0 ; i < a . length ; i ++ ) {
752- if ( a [ i ] !== b [ i ] ) return false ;
753- }
754- return true ;
755- }
756-
757770 get currentSystem ( ) : Proto . PublicKey {
758771 return this . currentKeyPair ! . publicKey ;
759772 }
0 commit comments