import { CeramicClient } from "@ceramicnetwork/http-client"; import { encodeDIDWithLit, Secp256k1ProviderWithLit } from "key-did-provider-secp256k1-with-lit"; import { DID } from "dids"; import { getResolver } from "key-did-resolver"; import { TileDocument } from "@ceramicnetwork/stream-tile"; const createStreamCode = ` const go = async () => { // this requests a signature share from the Lit Node // the signature share will be automatically returned in the response from the node // and combined into a full signature by the LitJsSdk for you to use on the client // all the params (toSign, publicKey, sigName) are passed in from the LitJsSdk.executeJs() function const sigShare = await LitActions.signEcdsa({ toSign, publicKey, sigName }); }; go();` const litActionCode = ` const go = async () => { // this requests a signature share from the Lit Node // the signature share will be automatically returned in the HTTP response from the node // all the params (toSign, publicKey, sigName) are passed in from the LitJsSdk.executeJs() function const sigShare = await LitActions.ethPersonalSignMessageEcdsa({ message, publicKey , sigName }); }; go(); `; export async function createCeramicStream({payload, ipfsId, userPkp}: any): Promise { const ceramicNetwork = process.env?.REACT_APP_CERAMIC_NETWORK ?? "https://ceramic-clay.3boxlabs.com"; const ceramic = new CeramicClient(ceramicNetwork); console.log('@@@@@ -> check args', {payload, ipfsId, userPkp}) const encodedDID = await encodeDIDWithLit({ pkpPublicKey: userPkp }); const provider = new Secp256k1ProviderWithLit({ did: encodedDID, ipfsId: ipfsId }); const did = new DID({provider, resolver: getResolver()}); // -- authenticate await did.authenticate(); ceramic.did = did; console.log("DID:", did); // -- write to ceramic stream const doc = await TileDocument.create(ceramic, []); console.log("Doc/StreamID:", doc.id.toString()); const docId = doc.id.toString(); return { docId, userPkp }; } export async function loadCeramicStream({streamId, ipfsId, userPkp}: any): Promise { let encodedDID: string = ''; let doc: TileDocument; let docId: string = ''; const ceramicNetwork = process.env?.REACT_APP_CERAMIC_NETWORK ?? "https://ceramic-clay.3boxlabs.com"; console.log('CERAMIC NETWORK', ceramicNetwork) const ceramic = new CeramicClient(ceramicNetwork); try { encodedDID = await encodeDIDWithLit({ pkpPublicKey: userPkp }); } catch (err) { console.log('Error', err); } console.log('------. TRACK loader before lit') const provider = new Secp256k1ProviderWithLit({ did: encodedDID, ipfsId, // litCode: litActionCode }); console.log('------. TRACK loader before new DID') const did = new DID({provider, resolver: getResolver()}); console.log('------. TRACK before DID.authenticate', did) // -- authenticate try { await did.authenticate(); ceramic.did = did; } catch (err) { console.log('Error', err); } console.log('------. TRACK before TileDocument.load') const loadDoc = await TileDocument.load(ceramic, streamId); const recoveredCredential: any = loadDoc.content; console.log('------. recoveredCredential', recoveredCredential) return recoveredCredential; } export async function updateCeramicStream({updatedPayload, streamId, ipfsId, userPkp}: any) { let encodedDID: string = ''; let doc: any; let docId: string = ''; let stringifiedPayload = ''; console.log('^^^^^ -> updateCeramicStream args', {updatedPayload, streamId, ipfsId, userPkp}) const ceramicNetwork = process.env?.REACT_APP_CERAMIC_NETWORK ?? "https://ceramic-clay.3boxlabs.com"; const ceramic = new CeramicClient(ceramicNetwork); try { encodedDID = await encodeDIDWithLit({ pkpPublicKey: userPkp }); } catch (err) { console.log('Error', err); } const provider = new Secp256k1ProviderWithLit({ did: encodedDID, ipfsId, // litCode: litActionCode }); const did = new DID({provider, resolver: getResolver()}); // -- authenticate try { await did.authenticate(); ceramic.did = did; } catch (err) { console.log('Error', err); } try { // -- write to ceramic stream doc = await TileDocument.load(ceramic, streamId); await doc.update(updatedPayload) } catch (err) { console.log('Error', err); } return doc; }