import { gun2 , SharedAESKeyGun2 } from './gun2' ; import { hashPostSalt , hashCryptoKey , hashCryptoKeyUnstable , calculatePostKeyPartition } from './hash' ; import type { PublishedAESKeyRecordV39OrV38 } from '../../../crypto/crypto-alpha-38' ; import type { EC_Public_JsonWebKey } from '@masknet/shared-base' ; import { EventIterator } from 'event-iterator' ; param version param postIV param partitionByCryptoKey export async function queryPostKeysOnGun2 ( version , postIV , partitionByCryptoKey , networkHint ) { const { postHash , keyHash } = await calculatePostKeyPartition ( version , postIV , partitionByCryptoKey , networkHint ) ; const internalNodes = ( await gun2 . get ( postHash ) . get ( keyHash ) . then ! ( ) ) || { } ; const internalKeys = Object . keys ( internalNodes ) . filter ( ( x ) => x !== '_' ) ; const resultPromise = internalKeys . map ( ( key ) => gun2 . get ( key ) . then ! ( ) ) ; const result = ( await Promise . all ( resultPromise ) ) as SharedAESKeyGun2 [ ] ; console . info ( `await gun2[${ postHash }][${ keyHash }]\n` , result ) ; return result ; } param version param postSalt param partitionByCryptoKey export async function * subscribePostKeysOnGun2 ( version , postSalt , partitionByCryptoKey , networkHint ) { const iter = new EventIterator < SharedAESKeyGun2 > ( ( { push : callback } ) => { hashPostSalt ( postSalt , networkHint ) . then ( ( postHash ) => { ; ( version <= - 39 ? hashCryptoKeyUnstable : hashCryptoKey ) ( partitionByCryptoKey ) . then ( ( keyHash ) => { gun2 . get ( postHash ) . get ( keyHash ) . map ( ) . on ( ( data ) => { const { _ , ... data2 } = Object . assign ( { } , data ) ; callback ( data2 ) ; } ) ; } ) ; } ) ; } ) ; yield * iter ; } param version param postSalt param receiversKeys export async function publishPostAESKeyOnGun2 ( version , postSalt , networkHint , receiversKeys ) { const postHash = await hashPostSalt ( postSalt , networkHint ) ; receiversKeys . forEach ( async ( { aesKey , receiverKey } ) => { const keyHash = await ( version <= - 39 ? hashCryptoKeyUnstable : hashCryptoKey ) ( receiverKey ) ; console . log ( `gun[${ postHash }][${ keyHash }].push(` , aesKey , ')' ) ; gun2 . get ( postHash ) . get ( keyHash ) . set ( aesKey ) ; } ) ; }