分散式网络节点( DWN) SDK
研究预览 ——Enbox正在积极开发. API可不经通知而更改.
类型脚本执行 分散式网络节点 ——由模块驱动的用户控制的个人数据存储.
概览
这个包是Enbox堆栈其余部分使用的核心 DWN 引擎. 它处理消息处理,模块授权,记录存储,以及加密. 大多数应用程序应当使用 @enbox/api 而不是这个包直接.
安装
bun add @enbox/dwn-sdk-js
使用量
import { Dwn, DataStream, Jws, RecordsWrite } from '@enbox/dwn-sdk-js';
import { DataStoreLevel, MessageStoreLevel, ResumableTaskStoreLevel } from '@enbox/dwn-sdk-js/stores/level';
const messageStore = new MessageStoreLevel();
const dataStore = new DataStoreLevel();
const resumableTaskStore = new ResumableTaskStoreLevel();
const dwn = await Dwn.create({ messageStore, dataStore, resumableTaskStore });
// Create and process a RecordsWrite message
const data = new TextEncoder().encode('Hello, World!');
const recordsWrite = await RecordsWrite.create({
data,
dataFormat : 'application/json',
published : true,
schema : 'example/post',
signer : Jws.createSigner(persona),
});
const dataStream = DataStream.fromBytes(data);
const result = await dwn.processMessage(persona.did, recordsWrite.message, { dataStream });
console.log(result.status); // { code: 202, detail: 'Accepted' }
await dwn.close();
级别支持的存储执行从 @enbox/dwn-sdk-js/stores/level 因此仅需要信件的应用程序和模块 API 默认不会加载 L等值存储模块图. 可供替代商店执行时使用的共享块状辅助工具来自 @enbox/dwn-sdk-js/stores/blockstore。 。 。 .
背景角色偏好
resolveModuleContextRoles(graph, contextPath) 返回根
提供直接儿童角色路径 read输入 $actions 数组顺序。 这个顺序
选择此精确根后, 声明客户端; 对象属性顺序
不影响它。 它不会改变DWN的授权,意味着角色继承,
或修改角色分配。 高级共享文本 API产生其作用
同一解析模块图中的候选人.
见 上下文角色偏好 定义公约,以及一个带有编辑和查看器角色的笔记本示例.
自定义租户图
默认情况下,所有委托人都可以作为租户。 提供自定义 TenantGate 限制访问:
import { ActiveTenantCheckResult, Dwn, TenantGate } from '@enbox/dwn-sdk-js';
class CustomTenantGate implements TenantGate {
public async isActiveTenant(did: string): Promise<ActiveTenantCheckResult> {
// custom logic
}
}
const dwn = await Dwn.create({ messageStore, dataStore, resumableTaskStore, tenantGate: new CustomTenantGate() });
自定义签名符
使用 PrivateKeySigner 如果您有可用的密钥,或者执行 Signer 与外部签名服务整合的接口 HSM等 :
import type { Signer } from '@enbox/dwn-sdk-js';
class CustomSigner implements Signer {
public keyId = 'did:example:alice#key1';
public algorithm = 'EdDSA';
public async sign(content: Uint8Array): Promise<Uint8Array> {
// custom signing logic
}
}
建筑
该图是一个概念性观点;来源中的实际成分名称可能不同.
发展
# Build
bun run build
# Test
bun run test:node
# Test with grep filter (from this package directory)
GREP="ModulesConfigure" bun run test:node-grep
# Lint
bun run lint
许可证
阿帕奇 - 2.0