import { getWallet } from "../lib/config" ; import * as solarium from "solarium-js" ; import { Observable } from "rxjs" ; import { Channel , Message } from "solarium-js" ; import { Keypair } from "@solana/web3.js" ; async function getOrCreateDirectChannel ( wallet , withDID ) { try { return ( await solarium . getDirect ( { decryptionKey : wallet . secretKey , partnerDID : withDID , } ) ) as Channel ; } catch ( error ) { if ( error . message === "Channel not found" ) { return solarium . createDirect ( { payer : wallet . secretKey , owner : wallet . secretKey , inviteeDID : withDID , } ) ; } else { throw error ; } } } export const chat = async ( withDID , incomingMessages ) : Promise < Observable < Message > > => { const wallet = await getWallet ( ) ; const channel = await getOrCreateDirectChannel ( wallet , withDID ) ; const channelAddress = channel . address . toBase58 ( ) ; incomingMessages . subscribe ( ( message ) => { return solarium . post ( { payer : wallet . secretKey , message , signer : wallet . secretKey , channel : channelAddress , } ) ; } ) ; return solarium . readStream ( { channel : channelAddress , decryptionKey : wallet . secretKey , } ) ; } ;