-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: delegate access to spaces #293
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
029000a
feat: import and delegate access to spaces
olizilla 8962eee
chore: move createDelegation to keyring-core
olizilla 01c1bf1
chore: lint
olizilla 8a20830
chore: lint more
olizilla 176a994
chore: remove console.log
olizilla 7f47f54
fix: correct IANA media type for car
olizilla 04c7f6f
Merge branch 'main' into share-space
olizilla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ChangeEvent, useState } from 'react' | ||
import { useKeyring } from '@w3ui/react-keyring' | ||
import * as DID from '@ipld/dag-ucan/did' | ||
import { CarWriter } from '@ipld/car/writer' | ||
import type { PropsWithChildren } from 'react' | ||
import type { Delegation } from '@ucanto/interface' | ||
|
||
function Header (props: PropsWithChildren): JSX.Element { | ||
return <h3 className='font-semibold text-xs font-mono uppercase tracking-wider mb-4 text-gray-400'>{props.children}</h3> | ||
} | ||
|
||
export async function toCarBlob (delegation: Delegation): Promise<Blob> { | ||
const { writer, out } = CarWriter.create() | ||
for (const block of delegation.export()) { | ||
// @ts-expect-error | ||
void writer.put(block) | ||
} | ||
void writer.close() | ||
|
||
const carParts = [] | ||
for await (const chunk of out) { | ||
carParts.push(chunk) | ||
} | ||
const car = new Blob(carParts, { | ||
type: 'application/vnd.ipld.car' | ||
}) | ||
return car | ||
} | ||
|
||
export function SpaceShare (): JSX.Element { | ||
const [, { createDelegation }] = useKeyring() | ||
const [value, setValue] = useState('') | ||
const [downloadUrl, setDownloadUrl] = useState('') | ||
|
||
async function makeDownloadLink (input: string): Promise<void> { | ||
let audience | ||
try { | ||
audience = DID.parse(input.trim()) | ||
} catch (err) { | ||
setDownloadUrl('') | ||
return | ||
} | ||
|
||
try { | ||
const delegation = await createDelegation(audience, ['*'], { expiration: Infinity }) | ||
const blob = await toCarBlob(delegation) | ||
const url = URL.createObjectURL(blob) | ||
setDownloadUrl(url) | ||
} catch (err) { | ||
throw new Error('failed to register', { cause: err }) | ||
} | ||
} | ||
|
||
function onSubmit (e: React.FormEvent<HTMLFormElement>): void { | ||
e.preventDefault() | ||
void makeDownloadLink(value) | ||
} | ||
|
||
function onChange (e: ChangeEvent<HTMLInputElement>): void { | ||
const input = e.target.value | ||
void makeDownloadLink(input) | ||
setValue(input) | ||
} | ||
|
||
function downloadName (ready: boolean, inputDid: string): string { | ||
if (!ready || inputDid === '') return '' | ||
const [, method = '', id = ''] = inputDid.split(':') | ||
return `did-${method}-${id?.substring(0, 10)}.ucan` | ||
} | ||
|
||
return ( | ||
<div className='pt-12'> | ||
<div className=''> | ||
<Header>Share your space</Header> | ||
<p className='mb-4'>Ask your friend for their Decentralized Identifier (DID) and paste it in below</p> | ||
<form onSubmit={(e: React.FormEvent<HTMLFormElement>) => { void onSubmit(e) }}> | ||
<input | ||
className='text-black px-2 py-2 rounded block mb-4 w-full max-w-4xl font-mono text-sm' | ||
type='pattern' pattern='did:.+' placeholder='did:' | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
<a className='w3ui-button text-center block w-52 opacity-30' style={{ opacity: downloadUrl !== '' ? '1' : '0.2' }} href={downloadUrl ?? ''} download={downloadName(downloadUrl !== '', value)}>Download UCAN</a> | ||
</form> | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
do we want to stash this error somewhere so we can tell the user why the DID they pasted is wrong or is there some other mechanism for that? either way not urgent, but probably useful for quality of life at some point
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.
Yes, I'll circle back in another PR and improve this!