-
Notifications
You must be signed in to change notification settings - Fork 8
create space #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create space #540
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphprotocol/hypergraph-react": patch | ||
--- | ||
|
||
add privy auth based createSpace hooks | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Key, type Messages, SpaceEvents, SpaceInfo, Utils } from '@graphprotocol/hypergraph'; | ||
import * as Effect from 'effect/Effect'; | ||
import { useState } from 'react'; | ||
import { useHypergraphApp, useHypergraphAuth } from '../HypergraphAppContext.js'; | ||
|
||
type CreatePrivateSpaceParams = { | ||
name: string; | ||
}; | ||
|
||
export const usePrivyAuthCreatePrivateSpace = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const { privyIdentity } = useHypergraphAuth(); | ||
const { syncServerUri, listSpaces } = useHypergraphApp(); | ||
|
||
const createPrivateSpace = async ({ name }: CreatePrivateSpaceParams) => { | ||
const accountAddress = privyIdentity?.accountAddress; | ||
if (!accountAddress) { | ||
setIsLoading(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
throw new Error('No account address found'); | ||
} | ||
const encryptionPrivateKey = privyIdentity?.encryptionPrivateKey; | ||
const encryptionPublicKey = privyIdentity?.encryptionPublicKey; | ||
const signaturePrivateKey = privyIdentity?.signaturePrivateKey; | ||
const signaturePublicKey = privyIdentity?.signaturePublicKey; | ||
if (!encryptionPrivateKey || !encryptionPublicKey || !signaturePrivateKey || !signaturePublicKey) { | ||
setIsLoading(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
throw new Error('No keys found'); | ||
} | ||
const privyIdentityToken = privyIdentity?.privyIdentityToken; | ||
if (!privyIdentityToken) { | ||
setIsLoading(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
throw new Error('No Privy identity token found'); | ||
} | ||
setIsLoading(true); | ||
|
||
try { | ||
const spaceId = Utils.generateId(); | ||
|
||
const spaceEvent = await Effect.runPromise( | ||
SpaceEvents.createSpace({ | ||
author: { | ||
accountAddress, | ||
encryptionPublicKey, | ||
signaturePrivateKey, | ||
signaturePublicKey, | ||
}, | ||
spaceId, | ||
}), | ||
); | ||
const result = Key.createKey({ | ||
privateKey: Utils.hexToBytes(encryptionPrivateKey), | ||
publicKey: Utils.hexToBytes(encryptionPublicKey), | ||
}); | ||
|
||
const { infoContent, signature } = SpaceInfo.encryptAndSignSpaceInfo({ | ||
accountAddress, | ||
name, | ||
secretKey: Utils.bytesToHex(result.key), | ||
signaturePrivateKey, | ||
spaceId, | ||
}); | ||
|
||
const message: Messages.RequestConnectCreateSpaceEvent = { | ||
type: 'connect-create-space-event', | ||
accountAddress, | ||
event: spaceEvent, | ||
spaceId: spaceEvent.transaction.id, | ||
keyBox: { | ||
accountAddress, | ||
ciphertext: Utils.bytesToHex(result.keyBoxCiphertext), | ||
nonce: Utils.bytesToHex(result.keyBoxNonce), | ||
authorPublicKey: encryptionPublicKey, | ||
id: Utils.generateId(), | ||
}, | ||
infoContent: Utils.bytesToHex(infoContent), | ||
infoSignature: signature, | ||
name, | ||
}; | ||
|
||
const response = await fetch(`${syncServerUri}/connect/spaces`, { | ||
headers: { | ||
'privy-id-token': privyIdentityToken, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(message), | ||
}); | ||
const data = await response.json(); | ||
if (data.space) { | ||
listSpaces(); // list spaces to update the list of private spaces | ||
} else { | ||
throw new Error('Failed to create space'); | ||
} | ||
return data.space; | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { createPrivateSpace, isLoading }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Graph } from '@graphprotocol/grc-20'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useState } from 'react'; | ||
import { useHypergraphAuth } from '../HypergraphAppContext.js'; | ||
|
||
type CreatePublicSpaceParams = { | ||
name: string; | ||
}; | ||
|
||
export const usePrivyAuthCreatePublicSpace = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const queryClient = useQueryClient(); | ||
const { privyIdentity } = useHypergraphAuth(); | ||
|
||
const createPublicSpace = async ({ name }: CreatePublicSpaceParams) => { | ||
const accountAddress = privyIdentity?.accountAddress; | ||
if (!accountAddress) { | ||
throw new Error('No account address found'); | ||
} | ||
try { | ||
setIsLoading(true); | ||
const { id } = await Graph.createSpace({ | ||
editorAddress: accountAddress, | ||
name, | ||
network: 'TESTNET', | ||
}); | ||
queryClient.invalidateQueries({ queryKey: ['hypergraph-public-spaces'] }); | ||
setIsLoading(false); | ||
return id; | ||
} catch (error) { | ||
setIsLoading(false); | ||
throw error; | ||
} | ||
}; | ||
return { createPublicSpace, isLoading }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug console.log statement should be removed before merging to production.
Copilot uses AI. Check for mistakes.