|
| 1 | +import { Key, type Messages, SpaceEvents, SpaceInfo, Utils } from '@graphprotocol/hypergraph'; |
| 2 | +import * as Effect from 'effect/Effect'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { useHypergraphApp, useHypergraphAuth } from '../HypergraphAppContext.js'; |
| 5 | + |
| 6 | +type CreatePrivateSpaceParams = { |
| 7 | + name: string; |
| 8 | +}; |
| 9 | + |
| 10 | +export const usePrivyAuthCreatePrivateSpace = () => { |
| 11 | + const [isLoading, setIsLoading] = useState(false); |
| 12 | + const { privyIdentity } = useHypergraphAuth(); |
| 13 | + const { syncServerUri, listSpaces } = useHypergraphApp(); |
| 14 | + |
| 15 | + const createPrivateSpace = async ({ name }: CreatePrivateSpaceParams) => { |
| 16 | + const accountAddress = privyIdentity?.accountAddress; |
| 17 | + if (!accountAddress) { |
| 18 | + setIsLoading(false); |
| 19 | + throw new Error('No account address found'); |
| 20 | + } |
| 21 | + const encryptionPrivateKey = privyIdentity?.encryptionPrivateKey; |
| 22 | + const encryptionPublicKey = privyIdentity?.encryptionPublicKey; |
| 23 | + const signaturePrivateKey = privyIdentity?.signaturePrivateKey; |
| 24 | + const signaturePublicKey = privyIdentity?.signaturePublicKey; |
| 25 | + if (!encryptionPrivateKey || !encryptionPublicKey || !signaturePrivateKey || !signaturePublicKey) { |
| 26 | + setIsLoading(false); |
| 27 | + throw new Error('No keys found'); |
| 28 | + } |
| 29 | + const privyIdentityToken = privyIdentity?.privyIdentityToken; |
| 30 | + if (!privyIdentityToken) { |
| 31 | + setIsLoading(false); |
| 32 | + throw new Error('No Privy identity token found'); |
| 33 | + } |
| 34 | + setIsLoading(true); |
| 35 | + |
| 36 | + try { |
| 37 | + const spaceId = Utils.generateId(); |
| 38 | + |
| 39 | + const spaceEvent = await Effect.runPromise( |
| 40 | + SpaceEvents.createSpace({ |
| 41 | + author: { |
| 42 | + accountAddress, |
| 43 | + encryptionPublicKey, |
| 44 | + signaturePrivateKey, |
| 45 | + signaturePublicKey, |
| 46 | + }, |
| 47 | + spaceId, |
| 48 | + }), |
| 49 | + ); |
| 50 | + const result = Key.createKey({ |
| 51 | + privateKey: Utils.hexToBytes(encryptionPrivateKey), |
| 52 | + publicKey: Utils.hexToBytes(encryptionPublicKey), |
| 53 | + }); |
| 54 | + |
| 55 | + const { infoContent, signature } = SpaceInfo.encryptAndSignSpaceInfo({ |
| 56 | + accountAddress, |
| 57 | + name, |
| 58 | + secretKey: Utils.bytesToHex(result.key), |
| 59 | + signaturePrivateKey, |
| 60 | + spaceId, |
| 61 | + }); |
| 62 | + |
| 63 | + const message: Messages.RequestConnectCreateSpaceEvent = { |
| 64 | + type: 'connect-create-space-event', |
| 65 | + accountAddress, |
| 66 | + event: spaceEvent, |
| 67 | + spaceId: spaceEvent.transaction.id, |
| 68 | + keyBox: { |
| 69 | + accountAddress, |
| 70 | + ciphertext: Utils.bytesToHex(result.keyBoxCiphertext), |
| 71 | + nonce: Utils.bytesToHex(result.keyBoxNonce), |
| 72 | + authorPublicKey: encryptionPublicKey, |
| 73 | + id: Utils.generateId(), |
| 74 | + }, |
| 75 | + infoContent: Utils.bytesToHex(infoContent), |
| 76 | + infoSignature: signature, |
| 77 | + name, |
| 78 | + }; |
| 79 | + |
| 80 | + const response = await fetch(`${syncServerUri}/connect/spaces`, { |
| 81 | + headers: { |
| 82 | + 'privy-id-token': privyIdentityToken, |
| 83 | + 'Content-Type': 'application/json', |
| 84 | + }, |
| 85 | + method: 'POST', |
| 86 | + body: JSON.stringify(message), |
| 87 | + }); |
| 88 | + const data = await response.json(); |
| 89 | + if (data.space) { |
| 90 | + listSpaces(); // list spaces to update the list of private spaces |
| 91 | + } else { |
| 92 | + throw new Error('Failed to create space'); |
| 93 | + } |
| 94 | + return data.space; |
| 95 | + } finally { |
| 96 | + setIsLoading(false); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + return { createPrivateSpace, isLoading }; |
| 101 | +}; |
0 commit comments