Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する

Using @blocklet/did-space-js to read and write DID Space.


Overview#

In most cases, applications need to read and write user data on DID Spaces. Integrating the @blocklet/did-space-js SDK enables these read and write operations once the DID Space endpoint is obtained.

Prerequisites#

  • You already know how to guide users to connect to their DID Spaces in your application and successfully retrieve the DID Space endpoint. See: Get the endpoint of DID Space

Code Sample#

You can use the blocklet create command locally to create a to-do list template integrated with DID Spaces, or refer to the code repository https://github.com/blocklet/create-blocklet/tree/main/packages/create-app/templates/todo-list-example.

╰─➤ blocklet create
blocklet create v1.16.37-beta-20250108-010153-e4bcd256
Need to install the following packages:
create-blocklet@0.9.14
Ok to proceed? (y) y

(node:82979) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

Powered By
_ ____ _ _
/ \ _ __ ___| __ )| | ___ ___| | __
/ _ \ | '__/ __| _ \| |/ _ \ / __| |/ /
/ ___ \| | | (__| |_) | | (_) | (__| <
/_/ \_\_| \___|____/|_|\___/ \___|_|\_\

Create Blocklet v0.9.14

✔ Project name: … todo-list
? Choose one or more blocklet templates:
Instructions:
/: Highlight option
//[space]: Toggle selection
[a,b,c]/delete: Filter choices
enter/return: Complete answer

Filtered results for: Enter something to filter

[dapp] react + express.js
[dapp] react + express + typescript
[dapp: did-wallet] Full stack app (react.js + express.js) with DID Wallet integration
[dapp: todo-list] react + express + typescript + DID Spaces // select did spaces template
[dapp: did-connect] Full stack app (react.js + express.js) with DID Connect integration
[dapp] solid + express.js
[dapp] vue3 + express.js
[dapp] svelte + express.js
[static] react
[static] solidjs

Procedure#

Install the DID Space JS Blocklet#

yarn add @blocklet/did-space-js
// pnpm install @blocklet/did-space-js
// npm install @blocklet/did-space-js

Data Writing#

You can write data to the user's DID Space using the following code:

const { SpaceClient, PutObjectCommand } = require('@blocklet/did-space-js');

const spaceClient = new SpaceClient({
endpoint,
wallet,
});

await spaceClient.send(
new PutObjectCommand({
key: 'test.txt',
data: 'hello world!',
})
);

After the data is successfully written, you can access the root directory of the application's component (z8iZmWXt56EZqA5Y2VcmUX6qFhZaj5w7pFQx9) within the DID Space to view the newly written data.

image.png

Retrieving Data#

You can access user data from the DID Space using the following code:

const { SpaceClient, GetObjectCommand } = require('@blocklet/did-space-js');

const spaceClient = new SpaceClient({
endpoint, // DID Space endpoint
wallet, // Blocklet Wallet
});

const { data } = await spaceClient.send(
new GetObjectCommand({
key: 'test.txt',
})
);

console.log(await streamToString(data)); // 'hello world!'

After successfully reading the data, a data object is returned, through which you can access the data.

Data Deletion#

You can remove user data from a DID Space using the following code:

const { SpaceClient, DeleteObjectCommand } = require('@blocklet/did-space-js');

const spaceClient = new SpaceClient({
endpoint,
wallet,
});

await spaceClient.send(
new DeleteObjectCommand({
key: 'test.txt',
})
);

For more information on using the API, see the API Reference.

你获得 0 积分