-
Notifications
You must be signed in to change notification settings - Fork 88
Feature/jss 133 naga feature request #969
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bcf520b
feat(lit-client): add ipfs helper subpath for cidv0 generation
Ansonhkg aedfa59
chore(lit-client): add changeset for `getIpfsId` method for CIDv0 ha…
Ansonhkg 6b1f5f6
docs(lit-client): update `getIpfsId` doc to include support for binar…
Ansonhkg da97285
fix(docs): correct formatting in `getIpfsId` return type description
Ansonhkg 71e51ed
fix(docs): correct markdown syntax for CIDv0 return type in `getIpfsI…
Ansonhkg 01ae7ed
Update packages/lit-client/src/ipfs/getIpfsId.ts
Ansonhkg f40d281
Update packages/lit-client/src/ipfs/getIpfsId.ts
Ansonhkg b5eefa9
fix(docs): update return type formatting in `getIpfsId` documentation
Ansonhkg 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 hidden or 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,10 @@ | ||
| --- | ||
| '@lit-protocol/lit-client': minor | ||
| --- | ||
|
|
||
| LitClient now offers `getIpfsId` via `@lit-protocol/lit-client/ipfs`, letting apps compute CIDv0 hashes (e.g., `await getIpfsId('hello')`) while keeping bundles lean. | ||
|
|
||
| ```ts | ||
| import { getIpfsId } from '@lit-protocol/lit-client/ipfs'; | ||
| const cid = await getIpfsId('hello'); | ||
| ``` |
This file contains hidden or 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 hidden or 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,32 @@ | ||
| --- | ||
| title: getIpfsId | ||
| description: Generate CIDv0 hashes with the Lit Client IPFS helper | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| `getIpfsId` is an optional helper exported via `@lit-protocol/lit-client/ipfs`. It produces CIDv0 IPFS identifiers identical to the Lit SDK v7 utility while remaining fully tree-shakeable from the main client bundle. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { getIpfsId } from '@lit-protocol/lit-client/ipfs'; | ||
|
|
||
| const cid = await getIpfsId('console.log("hello lit")'); | ||
| // cid === 'Qm...' | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Name | Type | Description | | ||
| | ---- | ---- | ----------- | | ||
| | input | `string` | Source content to hash. Mirrors the v7 helper contract by accepting plain strings. | | ||
|
|
||
| ## Returns | ||
|
|
||
| - `Promise<\`Qm${string}\`>` – A CIDv0 string beginning with `Qm`. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The helper delegates to the same hashing routine used in v7 (`typestub-ipfs-only-hash`) and can be imported without pulling the entire Lit Client into your bundle. | ||
| - If you need to hash binary data, convert it to a string representation (for example, base64) before passing it to `getIpfsId`. | ||
Ansonhkg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or 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,2 @@ | ||
| export { getIpfsId } from '../src/ipfs/getIpfsId'; | ||
| export type { IpfsCidV0 } from '../src/ipfs/getIpfsId'; |
This file contains hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import { getIpfsId } from './getIpfsId'; | ||
|
|
||
| const encoder = new TextEncoder(); | ||
|
|
||
| describe('getIpfsId', () => { | ||
| it('creates a CIDv0 for string input', async () => { | ||
| await expect(getIpfsId('hello')).resolves.toBe( | ||
| 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' | ||
| ); | ||
| }); | ||
|
|
||
| it('creates the same CIDv0 for byte input', async () => { | ||
| const bytes = encoder.encode('hello'); | ||
| await expect(getIpfsId(bytes)).resolves.toBe( | ||
| 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' | ||
| ); | ||
| }); | ||
|
|
||
| it('throws when input type is unsupported', async () => { | ||
| await expect( | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| getIpfsId(123 as any) | ||
| ).rejects.toThrow(/ArrayBufferView/); | ||
| }); | ||
| }); |
This file contains hidden or 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,71 @@ | ||
| import * as Hash from 'typestub-ipfs-only-hash'; | ||
|
|
||
| export type IpfsCidV0 = `Qm${string}`; | ||
|
|
||
| type SupportedInput = string | Uint8Array | ArrayBuffer | ArrayBufferView; | ||
|
|
||
| // Ensure string inputs are encoded without relying on Node's global Buffer, falling back to util.TextEncoder when needed. | ||
| const encodeString = (value: string): Uint8Array => { | ||
| if (typeof TextEncoder !== 'undefined') { | ||
| return new TextEncoder().encode(value); | ||
| } | ||
|
|
||
| try { | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const { TextEncoder: NodeTextEncoder } = require('util'); | ||
| return new NodeTextEncoder().encode(value); | ||
| } catch (error) { | ||
| throw new Error('TextEncoder is not available in this environment'); | ||
| } | ||
| }; | ||
|
|
||
| const toUint8Array = (value: ArrayBuffer | ArrayBufferView): Uint8Array => { | ||
| // TypedArray/DataView instances expose a shared buffer; slice the relevant window into a standalone Uint8Array. | ||
| if (ArrayBuffer.isView(value)) { | ||
| return new Uint8Array(value.buffer, value.byteOffset, value.byteLength); | ||
| } | ||
|
|
||
| return new Uint8Array(value); | ||
| }; | ||
|
|
||
| const normalizeInput = (input: SupportedInput): Uint8Array => { | ||
| // Accommodate all portable input shapes while keeping the helper tree-shakeable and browser-friendly. | ||
| if (typeof input === 'string') { | ||
| return toUint8Array(encodeString(input)); | ||
Ansonhkg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (input instanceof Uint8Array) { | ||
| return toUint8Array(input); | ||
Ansonhkg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (typeof ArrayBuffer !== 'undefined') { | ||
| if (input instanceof ArrayBuffer) { | ||
| return toUint8Array(input); | ||
| } | ||
|
|
||
| if (ArrayBuffer.isView(input)) { | ||
| return toUint8Array(input); | ||
| } | ||
| } | ||
|
|
||
| throw new TypeError( | ||
| 'Input must be provided as a string, Uint8Array, ArrayBuffer or ArrayBufferView' | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Generate a CIDv0 IPFS identifier for the provided content. | ||
| */ | ||
| export const getIpfsId = async (input: SupportedInput): Promise<IpfsCidV0> => { | ||
| const normalizedInput = normalizeInput(input); | ||
| const hashOf = Hash.of as unknown as ( | ||
| value: string | Uint8Array | ||
| ) => Promise<string>; | ||
| const cid = await hashOf(normalizedInput); | ||
|
|
||
| if (!cid.startsWith('Qm')) { | ||
| throw new Error('Generated IPFS CID is not CIDv0'); | ||
| } | ||
|
|
||
| return cid as IpfsCidV0; | ||
| }; | ||
This file contains hidden or 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,2 @@ | ||
| export { getIpfsId } from './getIpfsId'; | ||
| export type { IpfsCidV0 } from './getIpfsId'; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.