SDK 软件
概览
Dialect SDK是一个单一的打字库,用于与Dialect的上下链资源进行交互. 这些资源包括:
通讯
所有消息,用户给用户或Dapp给用户,都由同一Dialect消息协议提供动力,协议支持链上和链外消息. 有了SDK的讯息,你可以:
- 查看所有信件线索
- 创建并删除线索,( d)
- 在线索内发送消息( a) .
Dapp 通知和订阅
除了通訊外,Dialect还支持一个API,用于Dapp通过Dialect通訊协议以及包括电子邮件,Telegram,和SMS在内的web2频道向用户发送通知.
为了支持这一点,Dialect SDK包括了API用于:
- 应用他们的留言键盘来注册自己.
- 钱包选择从 dapps 接收信件.
- 钱包来配置他们想要接收的 Dapp 消息的频道,包括钱包(使用 Dialect 协议),电子邮件,Telegram, & SMS.
认证
- 对于所有链上交易,认证和授权被烤入,因为钱包必须签入所有交易.
- 对所有脱链请求, Dialect API 要求使用钱包认证所有请求
signMessage方法,证明提出请求的实体对所涉的钱包钥匙包拥有保管权.
通讯
创建 SDK 客户端连接您的钱包( C)
运行 create-solana-sdk 示例脚本。 。 。 .
要使用 Dialect SDK , 您需要创建 SDK 客户端, 并使用指定的钱包 :
import {
Backend,
ConfigProps,
Dialect,
DialectSolanaWalletAdapterWrapper,
EncryptionKeysStore,
NodeDialectSolanaWalletAdapter,
TokenStore,
} from '@dialectlabs/sdk';
import type {
DialectSdk,
} from '@dialectlabs/sdk';
// Backends tell the SDK where messages can be sent and received to, & in what priority.
const backends = [Backend.DialectCloud, Backend.Solana];
// Provide configuration options for how to target the DialectCloud backend
const dialectCloud = {
url: 'https://dialectapi.to',
// Provide a keystore for storing authentication headers, which are expiring tokens generated from the wallet's signMessage method
tokenStore: TokenStore.createInMemory(),
};
// Convenience for setting smart defaults for backends. Variables above are optional if this is provided.
const environment = 'production';
// Provide a keystore for storing e.g. encryption & decryption keys.
const encryptionKeysStore = EncryptionKeysStore.createInMemory();
// Provide override parameters for Solana backend configuration, including RPC url
const solana = {
rpcUrl: 'https://api.mainnet-beta.solana.com',
};
// A node wallet for running in a node environment. N.b. this would be a mobile or web wallet object instead. The Wrapper class handles conversion to the API expected by the Dialect SDK, but is mostly consistent with expected wallet APIs.
const wallet = DialectSolanaWalletAdapterWrapper.create(
// IMPORTANT: must set environment variable DIALECT_SDK_CREDENTIALS
// to your dapp's Solana messaging wallet keypair e.g. [170,23, . . . ,300]
NodeDialectSolanaWalletAdapter.create(),
);
// Now construct the SDK client from all of the above
const sdk: DialectSdk = Dialect.sdk({
backends,
dialectCloud,
environment,
encryptionKeysStore,
solana,
wallet,
} as ConfigProps);
获取钱包的现有线索
运行 get-threads 示例脚本。 。 。 .
import type {
// ... previous imports
Thread,
ThreadId,
} from '@dialectlabs/sdk';
// ... code from previous examples
// Fetch all threads the wallet is a part of, across all provided backends
const threads: Thread[] = await sdk.threads.findAll();
注意这里返回的线程是从链上和链外后端获取的 : Backends.Solana 打开 Backends.DialectCloud。 。 。 .
以 id 读取线索消息获取线索( R)
运行 get-messages 示例脚本。 。 。 .
import type {
// ... previous imports
Message,
} from '@dialectlabs/sdk';
// ... code from previous examples
// Choose a given thread
const thread = threads[0];
// Fetch for a single thread by its id. N.b. the ThreadId type specifies both the address of the thread *as well as* the specified backend; threads of a given id may exist in any kind of backend. See the ThreadId type.
const query: FindThreadByIdQuery = {
id: thread.id,
}
const thread = await sdk.threads.find(query);
// Call the messages() method to read messages
const messages = await thread.messages();
创建新线索
运行 create-thread 示例脚本。 。 。 .
import type {
// ... previous imports
ThreadMemberScope,
} from '@dialectlabs/sdk';
const recipient = new PublicKey('3vuCFLbVWsNeWgyxkb2xiLQuxKDW83HWiTMmodT8gmtk') // Make this arbitrary
const command: CreateThreadCommand = {
encrypted: false,
me: {
scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
},
otherMembers: [
{
publicKey: recipient,
scopes: [ThreadMemberScope.ADMIN, ThreadMemberScope.WRITE],
},
],
};
// Uses the default backend.DialectCloud offchain. The command above may optionally take a preferred backend.
const thread = await sdk.threads.create(command);
发送消息
运行 send-message 示例脚本。 。 。 .
// ... code from previous examples
const text = 'gm world';
const command: SendMessageCommand = {
text,
}
await thread.send(command);
删除线索
运行 delete-thread 示例脚本。 。 。 .
const threadId = thread.id; // Keep for re-querying to confirm deletion
await thread.delete();
const query: FindThreadByIdQuery = {
id: threadId,
};
const refetchedThread = await sdk.threads.find(query); // Will be null
Dapp 通知和订阅
以下例子描述了如何使用 Dialect SDK 来管理 dapp并通知相关的资源.
注册一个 dapp
要让用户订阅您的通知, 您首先需要在 Dialect API 中创建 dapp .
// Construct the SDK client from all of the above. Let's use 'development' for the environment, and pass
const environment = 'development';
const dialectCloud = {
// N.b. we omit the url value here, since 'development' will default to https://dev.dialectapi.to.
tokenStore: TokenStore.createInMemory(),
};
// Use ConfigProps values from example above.
const sdk: DialectSdk = Dialect.sdk({
backends,
dialectCloud,
environment,
encryptionKeysStore,
solana,
wallet,
} as ConfigProps);
// N.b. this created dapp is associated with the wallet public key connected to the sdk instance.
const dapp = await sdk.dapps.create({
name: 'My test dapp',
description: `My test dapp's description.`
});
请注意,所有与 Dialect 创建的 dapp 都必须与钱包键盘连接 。 此键盘将被用于所有对 Dapp 的认证请求, 以及所有用户信件 .
这个 sdk.dapps.create 上方方法使用连接到 sdk 客户实例.
您每个公钥只能创建一个 dapp .
应用白名单
Dialect管理一个与Dialect合并的已核实的Dapp列表. 虽然所有消息都是通过钱包和密钥夹完成的, 并沿着核心消息链发送.
是否继续维持这一清单有待确定.
请联系我们 推特上说 或者给我们发邮件 哈啰 @ dialect.to。 。 。 .
拿起你的电话
如果你的钱包与你的 sdk 客户端实例与 dapp 相关, 您可以通过简单化获取 dapp 记录 find 电话.
const dapp = await sdk.dapps.find();
拿起所有的达普
您可以在 Dialect API DB 中查询所有注册的 Dapp .
const dapps = await sdk.dapps.findAll();
为给定钱包获取所有白列的 dapp 信件
Dialect SDK 提供了一个端点,用于查询从白列 Dapps 中获取的用于给定钱包的所有信件. 这对提供单一的通知是方便的.
// ... code from previous examples
const messages = await sdk.wallet.dappMessages.findAll({
dappVerified: true,
});