@ocap/client 1.25.3 → 1.25.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +76 -80
  2. package/dist/report.html +1 -1
  3. package/docs/_sidebar.md +22 -0
  4. package/docs/api-reference-client-methods.md +229 -0
  5. package/docs/api-reference-client-methods.zh.md +229 -0
  6. package/docs/api-reference-data-types.md +482 -0
  7. package/docs/api-reference-data-types.zh.md +482 -0
  8. package/docs/api-reference-low-level-api.md +228 -0
  9. package/docs/api-reference-low-level-api.zh.md +228 -0
  10. package/docs/api-reference-query-mutation-methods.md +814 -0
  11. package/docs/api-reference-query-mutation-methods.zh.md +814 -0
  12. package/docs/api-reference-transaction-helpers.md +649 -0
  13. package/docs/api-reference-transaction-helpers.zh.md +649 -0
  14. package/docs/api-reference.md +23 -0
  15. package/docs/api-reference.zh.md +23 -0
  16. package/docs/core-concepts-client-architecture.md +102 -0
  17. package/docs/core-concepts-client-architecture.zh.md +102 -0
  18. package/docs/core-concepts-event-subscriptions.md +123 -0
  19. package/docs/core-concepts-event-subscriptions.zh.md +123 -0
  20. package/docs/core-concepts-gas-payment.md +111 -0
  21. package/docs/core-concepts-gas-payment.zh.md +111 -0
  22. package/docs/core-concepts-transaction-lifecycle.md +183 -0
  23. package/docs/core-concepts-transaction-lifecycle.zh.md +183 -0
  24. package/docs/core-concepts.md +22 -0
  25. package/docs/core-concepts.zh.md +22 -0
  26. package/docs/getting-started-basic-usage.md +87 -0
  27. package/docs/getting-started-basic-usage.zh.md +87 -0
  28. package/docs/getting-started-installation.md +60 -0
  29. package/docs/getting-started-installation.zh.md +60 -0
  30. package/docs/getting-started.md +16 -0
  31. package/docs/getting-started.zh.md +16 -0
  32. package/docs/how-to-guides-delegate-permissions.md +167 -0
  33. package/docs/how-to-guides-delegate-permissions.zh.md +167 -0
  34. package/docs/how-to-guides-manage-accounts.md +73 -0
  35. package/docs/how-to-guides-manage-accounts.zh.md +73 -0
  36. package/docs/how-to-guides-manage-assets.md +255 -0
  37. package/docs/how-to-guides-manage-assets.zh.md +255 -0
  38. package/docs/how-to-guides-manage-tokens.md +179 -0
  39. package/docs/how-to-guides-manage-tokens.zh.md +179 -0
  40. package/docs/how-to-guides-stake-tokens-and-assets.md +205 -0
  41. package/docs/how-to-guides-stake-tokens-and-assets.zh.md +205 -0
  42. package/docs/how-to-guides-transfer-tokens-and-nfts.md +179 -0
  43. package/docs/how-to-guides-transfer-tokens-and-nfts.zh.md +179 -0
  44. package/docs/how-to-guides.md +27 -0
  45. package/docs/how-to-guides.zh.md +27 -0
  46. package/docs/overview.md +70 -0
  47. package/docs/overview.zh.md +70 -0
  48. package/package.json +14 -14
@@ -0,0 +1,87 @@
1
+ # 基本用法
2
+
3
+ 本指南将引导您完成 OCAP 客户端入门的基本步骤。您将学习如何初始化客户端、连接到 ArcBlock 测试链,并执行基本查询以确保一切设置正确。
4
+
5
+ 遵循本指南,您将在几分钟内拥有一个可与区块链交互的客户端实例。
6
+
7
+ ## 1. 初始化客户端
8
+
9
+ 首先,您需要导入 `@ocap/client` 包并创建 `GraphQLClient` 的实例。构造函数接受您要连接的区块链节点的 GraphQL 端点。在本指南中,我们将使用 ArcBlock 测试链的公共端点。
10
+
11
+ ```javascript Initialize Client icon=logos:javascript
12
+ const GraphQLClient = require('@ocap/client');
13
+
14
+ // 连接到测试链
15
+ const client = new GraphQLClient('https://beta.abtnetwork.io/api');
16
+
17
+ console.log('OCAP 客户端已初始化!');
18
+ ```
19
+
20
+ 这将创建一个客户端实例,该实例已预先配置了查询链、发送交易等方法。
21
+
22
+ ## 2. 查询区块链
23
+
24
+ 客户端初始化后,您可以执行一个简单的只读操作来验证连接。`getChainInfo` 方法非常适合此目的,因为它会获取有关所连接区块链的基本信息,且无需任何身份验证。
25
+
26
+ ```javascript Query Chain Info icon=logos:javascript
27
+ (async () => {
28
+ try {
29
+ const { info } = await client.getChainInfo();
30
+ console.log('链信息:', info);
31
+ } catch (error) {
32
+ console.error('获取链信息失败:', error);
33
+ }
34
+ })();
35
+ ```
36
+
37
+ 如果请求成功,`info` 对象将被打印到控制台,其中包含有关链的详细信息,例如其 ID、网络和支持的交易类型。这确认了您的客户端已成功与区块链节点通信。
38
+
39
+ ## 3. 准备账户
40
+
41
+ 要发送交易(写操作),您需要一个钱包。您可以在本地生成一个新钱包。此钱包代表您在链上的账户身份。
42
+
43
+ ```javascript Create a Wallet icon=logos:javascript
44
+ const { fromRandom } = require('@ocap/wallet');
45
+
46
+ // 创建一个新的随机钱包
47
+ const wallet = fromRandom();
48
+
49
+ console.log('新钱包已创建:');
50
+ console.log('地址:', wallet.address);
51
+ console.log('公钥:', wallet.publicKey);
52
+ console.log('私钥:', wallet.secretKey);
53
+ ```
54
+
55
+ **重要提示:** 新生成的钱包只是一个密钥对。它尚未存在于区块链上。当其地址收到第一笔传入交易时,链上会自动创建一个账户。
56
+
57
+ 要激活您的新账户,您需要为其充值一些测试通证。您可以从官方水龙头获取用于测试链的免费测试通证(`TBA`):
58
+
59
+ <x-card data-title="ArcBlock 水龙头" data-icon="lucide:droplet" data-href="https://faucet.abtnetwork.io/" data-cta="获取测试通证">
60
+ 访问水龙头并将一些 TBA 发送到您新生成的地址以激活它。
61
+ </x-card>
62
+
63
+ ## 4. 查询账户状态
64
+
65
+ 使用水龙头为您的新账户充值后,您可以查询其状态以验证余额。
66
+
67
+ ```javascript Query Account State icon=logos:javascript
68
+ (async () => {
69
+ try {
70
+ // 替换为您从水龙头充值的地址
71
+ const address = wallet.address;
72
+
73
+ const { state } = await client.getAccountState({ address });
74
+ console.log('账户状态:', state);
75
+ } catch (error) {
76
+ console.error('获取账户状态失败:', error);
77
+ }
78
+ })();
79
+ ```
80
+
81
+ 响应中的 `state` 对象将包含您账户的地址、余额、nonce(已发送交易的数量)以及其他详细信息。看到非零余额即确认您的账户已从水龙头收到通证,并已在区块链上激活。
82
+
83
+ ## 后续步骤
84
+
85
+ 您已成功设置 OCAP 客户端、连接到区块链并验证了您的账户。现在,您可以开始探索更高级的操作。
86
+
87
+ 请查阅 [操作指南](./how-to-guides.md) 以了解如何执行常见任务,例如转账、管理资产等。
@@ -0,0 +1,60 @@
1
+ # Installation
2
+
3
+ The OCAP Client is a JavaScript library that connects your application to an OCAP blockchain. It works in both Node.js and browser environments. This guide will walk you through installing the client using a package manager.
4
+
5
+ ## Prerequisites
6
+
7
+ Before you begin, ensure you have the following installed:
8
+
9
+ - [Node.js](https://nodejs.org/) (which includes the npm package manager)
10
+ - [pnpm](https://pnpm.io/) (optional, but recommended)
11
+
12
+ ## Install the Package
13
+
14
+ You can install the `@ocap/client` package from the npm registry using either `npm` or `pnpm`.
15
+
16
+ ### Using npm
17
+
18
+ Run the following command in your project's terminal:
19
+
20
+ ```shell npm icon=logos:npm
21
+ npm install @ocap/client
22
+ ```
23
+
24
+ ### Using pnpm
25
+
26
+ If you prefer using `pnpm`, run this command:
27
+
28
+ ```shell pnpm icon=logos:pnpm
29
+ pnpm install @ocap/client
30
+ ```
31
+
32
+ This command will download the package and add it to your project's dependencies.
33
+
34
+ ## Verify Installation
35
+
36
+ After the installation is complete, you can verify that the package is correctly installed by importing it into a JavaScript file. Create a file named `test-client.js` and add the following code:
37
+
38
+ ```javascript Verify Installation icon=logos:javascript
39
+ const OCapClient = require('@ocap/client');
40
+
41
+ if (OCapClient) {
42
+ console.log('Successfully installed and imported @ocap/client!');
43
+ } else {
44
+ console.error('Failed to import @ocap/client.');
45
+ }
46
+ ```
47
+
48
+ Run the file from your terminal:
49
+
50
+ ```shell
51
+ node test-client.js
52
+ ```
53
+
54
+ You should see the success message printed to the console, confirming that the client is ready to use.
55
+
56
+ ## Next Steps
57
+
58
+ Now that you have the client installed, you're ready to connect to the blockchain and start making requests. Continue to the next section to learn the basics.
59
+
60
+ - **[Basic Usage](./getting-started-basic-usage.md)**: Learn how to initialize the client and make your first query.
@@ -0,0 +1,60 @@
1
+ # 安装
2
+
3
+ OCAP Client 是一个 JavaScript 库,可将您的应用程序连接到 OCAP 区块链。它同时适用于 Node.js 和浏览器环境。本指南将引导您使用包管理器安装该客户端。
4
+
5
+ ## 前提条件
6
+
7
+ 在开始之前,请确保您已安装以下内容:
8
+
9
+ - [Node.js](https://nodejs.org/)(包含 npm 包管理器)
10
+ - [pnpm](https://pnpm.io/)(可选,但推荐使用)
11
+
12
+ ## 安装包
13
+
14
+ 您可以使用 `npm` 或 `pnpm` 从 npm 注册表安装 `@ocap/client` 包。
15
+
16
+ ### 使用 npm
17
+
18
+ 在您的项目终端中运行以下命令:
19
+
20
+ ```shell npm icon=logos:npm
21
+ npm install @ocap/client
22
+ ```
23
+
24
+ ### 使用 pnpm
25
+
26
+ 如果您更喜欢使用 `pnpm`,请运行此命令:
27
+
28
+ ```shell pnpm icon=logos:pnpm
29
+ pnpm install @ocap/client
30
+ ```
31
+
32
+ 此命令将下载该包并将其添加到您项目的依赖项中。
33
+
34
+ ## 验证安装
35
+
36
+ 安装完成后,您可以通过将其导入 JavaScript 文件来验证包是否已正确安装。创建一个名为 `test-client.js` 的文件并添加以下代码:
37
+
38
+ ```javascript Verify Installation icon=logos:javascript
39
+ const OCapClient = require('@ocap/client');
40
+
41
+ if (OCapClient) {
42
+ console.log('Successfully installed and imported @ocap/client!');
43
+ } else {
44
+ console.error('Failed to import @ocap/client.');
45
+ }
46
+ ```
47
+
48
+ 从您的终端运行该文件:
49
+
50
+ ```shell
51
+ node test-client.js
52
+ ```
53
+
54
+ 您应该会看到成功消息打印到控制台,确认客户端已准备就绪。
55
+
56
+ ## 后续步骤
57
+
58
+ 现在您已经安装了客户端,可以连接到区块链并开始发出请求了。请继续阅读下一部分以了解基础知识。
59
+
60
+ - **[基本用法](./getting-started-basic-usage.md)**:学习如何初始化客户端并进行您的第一次查询。
@@ -0,0 +1,16 @@
1
+ # Getting Started
2
+
3
+ Welcome! This guide will get you up and running with the OCAP Client in under 30 minutes. You'll learn how to install the client, connect your application to an OCAP-compatible blockchain, and make your first API call to verify your setup.
4
+
5
+ Follow these simple steps to begin interacting with the network.
6
+
7
+ <x-cards data-columns="2">
8
+ <x-card data-title="Installation" data-href="/getting-started/installation" data-icon="lucide:download">
9
+ Learn how to add the OCAP Client package to your project using npm or pnpm.
10
+ </x-card>
11
+ <x-card data-title="Basic Usage" data-href="/getting-started/basic-usage" data-icon="lucide:play-circle">
12
+ Initialize the client, connect to the Beta chain, and make your first API call to verify your setup.
13
+ </x-card>
14
+ </x-cards>
15
+
16
+ Once you've completed these initial steps, you'll be ready to explore more advanced features. For practical, task-oriented examples, be sure to check out the [How-to Guides](./how-to-guides.md).
@@ -0,0 +1,16 @@
1
+ # 入门指南
2
+
3
+ 欢迎!本指南将帮助您在 30 分钟内启动并运行 OCAP Client。您将学习如何安装客户端,将您的应用程序连接到与 OCAP 兼容的区块链,并进行您的第一次 API 调用以验证您的设置。
4
+
5
+ 请按照以下简单步骤开始与网络交互。
6
+
7
+ <x-cards data-columns="2">
8
+ <x-card data-title="安装" data-href="/getting-started/installation" data-icon="lucide:download">
9
+ 学习如何使用 npm 或 pnpm 将 OCAP Client 包添加到您的项目中。
10
+ </x-card>
11
+ <x-card data-title="基本用法" data-href="/getting-started/basic-usage" data-icon="lucide:play-circle">
12
+ 初始化客户端,连接到 Beta 链,并进行您的第一次 API 调用以验证您的设置。
13
+ </x-card>
14
+ </x-cards>
15
+
16
+ 完成这些初始步骤后,您就可以探索更高级的功能了。有关以任务为导向的实用示例,请务必查看[操作指南](./how-to-guides.md)。
@@ -0,0 +1,167 @@
1
+ # Delegate Permissions
2
+
3
+ Delegating permissions is a powerful feature that allows one account (the delegator) to authorize another account (the delegatee) to send specific types of transactions on its behalf. This is particularly useful for applications that need to perform actions for a user without having direct access to the user's primary private key. For example, you could delegate permission to an application-specific key to transfer certain assets or interact with a smart contract, enhancing security by limiting the scope of the key's authority.
4
+
5
+ This guide will walk you through granting, using, and revoking delegations.
6
+
7
+ ## Granting Delegation
8
+
9
+ To authorize another account, you use the `delegate` method. You must specify who you are delegating to and which specific transaction types they are allowed to send. Each permission can also have limits, such as which tokens or assets can be involved.
10
+
11
+ ### Parameters
12
+
13
+ <x-field-group>
14
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
15
+ <x-field-desc markdown>The wallet object of the delegator (the account granting permissions).</x-field-desc>
16
+ </x-field>
17
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
18
+ <x-field-desc markdown>The wallet object of the delegatee (the account receiving permissions).</x-field-desc>
19
+ </x-field>
20
+ <x-field data-name="privileges" data-type="Array<object>" data-required="true">
21
+ <x-field-desc markdown>An array of permission objects to grant. Each object defines a specific authorization.</x-field-desc>
22
+ <x-field data-name="typeUrl" data-type="string" data-required="true">
23
+ <x-field-desc markdown>The type URL of the transaction being permitted (e.g., `fg:t:transfer_v2`).</x-field-desc>
24
+ </x-field>
25
+ <x-field data-name="limit" data-type="object" data-required="false">
26
+ <x-field-desc markdown>Optional limits on the permission, such as restricting it to specific tokens or assets.</x-field-desc>
27
+ <x-field data-name="tokens" data-type="Array<string>" data-required="false" data-desc="An array of token addresses that this permission applies to."></x-field>
28
+ <x-field data-name="assets" data-type="Array<string>" data-required="false" data-desc="An array of asset addresses that this permission applies to."></x-field>
29
+ </x-field>
30
+ </x-field>
31
+ </x-field-group>
32
+
33
+ ### Returns
34
+
35
+ <x-field data-name="result" data-type="Promise<[string, string]>" data-desc="A promise that resolves to an array containing the transaction hash and the newly created delegate address."></x-field>
36
+
37
+ ### Example
38
+
39
+ Here’s how to delegate the `transfer` permission from one account to another.
40
+
41
+ ```javascript Granting transfer permission icon=logos:javascript
42
+ import Client from '@ocap/client';
43
+ import { fromRandom } from '@ocap/wallet';
44
+ import { typeUrls } from '@ocap/proto';
45
+
46
+ const client = new Client('https://beta.abtnetwork.io/api');
47
+
48
+ // The account granting permission
49
+ const delegatorWallet = fromRandom();
50
+
51
+ // The account receiving permission
52
+ const delegateeWallet = fromRandom();
53
+
54
+ async function grantPermission() {
55
+ try {
56
+ // First, ensure the delegator account has funds.
57
+ // In a real application, you would transfer funds to delegatorWallet.address
58
+ // from a faucet or another account.
59
+ console.log(`Delegator address: ${delegatorWallet.address}`);
60
+ console.log(`Delegatee address: ${delegateeWallet.address}`);
61
+ console.log('Please fund the delegator account before proceeding.');
62
+
63
+ const privileges = [
64
+ {
65
+ typeUrl: typeUrls.TransferV2Tx,
66
+ // No limits specified, allowing transfer of any token/asset
67
+ limit: { tokens: [], assets: [] },
68
+ },
69
+ ];
70
+
71
+ const [hash, delegateAddress] = await client.delegate({
72
+ from: delegatorWallet,
73
+ to: delegateeWallet,
74
+ privileges: privileges,
75
+ });
76
+
77
+ console.log('Delegation successful!');
78
+ console.log('Transaction Hash:', hash);
79
+ console.log('Delegate Address:', delegateAddress);
80
+ } catch (error) {
81
+ console.error('Error delegating permissions:', error);
82
+ }
83
+ }
84
+
85
+ grantPermission();
86
+ ```
87
+
88
+ ## Sending a Transaction as a Delegatee
89
+
90
+ Once delegated, the delegatee wallet can send the authorized transaction by specifying the `delegator`'s address in the transaction parameters. The transaction is signed by the delegatee, but executed on behalf of the delegator, and the transaction fees are paid from the delegator's account.
91
+
92
+ ### Example
93
+
94
+ ```javascript Sending a transfer as a delegatee icon=logos:javascript
95
+ // Assuming the grantPermission() function from the previous example was successful
96
+
97
+ // The recipient of the transfer
98
+ const recipientWallet = fromRandom();
99
+
100
+ async function sendDelegatedTransaction() {
101
+ try {
102
+ const hash = await client.transfer({
103
+ to: recipientWallet.address,
104
+ token: 1, // Transfer 1 of the chain's native token
105
+ wallet: delegateeWallet, // Signed by the delegatee
106
+ delegator: delegatorWallet.address, // Executed on behalf of the delegator
107
+ });
108
+
109
+ console.log('Delegated transfer successful!');
110
+ console.log('Transaction Hash:', hash);
111
+ console.log(`Check transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);
112
+ } catch (error) {
113
+ console.error('Error sending delegated transaction:', error);
114
+ }
115
+ }
116
+
117
+ // Make sure to call this after the delegation is confirmed on-chain.
118
+ // sendDelegatedTransaction();
119
+ ```
120
+
121
+ ## Revoking Delegation
122
+
123
+ The delegator can revoke any granted permissions at any time using the `revokeDelegate` method. You need to specify which transaction types to revoke for a given delegatee.
124
+
125
+ ### Parameters
126
+
127
+ <x-field-group>
128
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
129
+ <x-field-desc markdown>The wallet object of the delegator who originally granted the permissions.</x-field-desc>
130
+ </x-field>
131
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
132
+ <x-field-desc markdown>The wallet object of the delegatee whose permissions are being revoked.</x-field-desc>
133
+ </x-field>
134
+ <x-field data-name="privileges" data-type="Array<string>" data-required="true">
135
+ <x-field-desc markdown>An array of transaction `typeUrl` strings to revoke.</x-field-desc>
136
+ </x-field>
137
+ </x-field-group>
138
+
139
+ ### Returns
140
+
141
+ <x-field data-name="hash" data-type="Promise<string>" data-desc="A promise that resolves to the transaction hash for the revocation."></x-field>
142
+
143
+ ### Example
144
+
145
+ ```javascript Revoking transfer permission icon=logos:javascript
146
+ // Assuming the same delegatorWallet and delegateeWallet from previous examples
147
+
148
+ async function revokePermission() {
149
+ try {
150
+ const hash = await client.revokeDelegate({
151
+ from: delegatorWallet,
152
+ to: delegateeWallet,
153
+ privileges: [typeUrls.TransferV2Tx], // The list of permissions to revoke
154
+ });
155
+
156
+ console.log('Revocation successful!');
157
+ console.log('Transaction Hash:', hash);
158
+ console.log(`Check transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);
159
+ } catch (error) {
160
+ console.error('Error revoking delegation:', error);
161
+ }
162
+ }
163
+
164
+ // revokePermission();
165
+ ```
166
+
167
+ After this transaction is confirmed, the `delegateeWallet` will no longer be able to send `transfer` transactions on behalf of the `delegatorWallet`.
@@ -0,0 +1,167 @@
1
+ # 委托权限
2
+
3
+ 委托权限是一项强大的功能,它允许一个账户(委托人)授权另一个账户(受托人)代表其发送特定类型的交易。这对于需要为用户执行操作但又不想直接访问用户主私钥的应用程序特别有用。例如,您可以将权限委托给特定于应用程序的密钥,以转移某些资产或与智能合约进行交互,通过限制密钥的权限范围来增强安全性。
4
+
5
+ 本指南将引导您完成授予、使用和撤销委托的全过程。
6
+
7
+ ## 授予委托
8
+
9
+ 要授权另一个账户,您需要使用 `delegate` 方法。您必须指定要委托给谁以及他们被允许发送哪些特定的交易类型。每项权限还可以设置限制,例如可以涉及哪些代币或资产。
10
+
11
+ ### 参数
12
+
13
+ <x-field-group>
14
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
15
+ <x-field-desc markdown>委托人(授予权限的账户)的钱包对象。</x-field-desc>
16
+ </x-field>
17
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
18
+ <x-field-desc markdown>受托人(接收权限的账户)的钱包对象。</x-field-desc>
19
+ </x-field>
20
+ <x-field data-name="privileges" data-type="Array<object>" data-required="true">
21
+ <x-field-desc markdown>要授予的权限对象数组。每个对象定义一个特定的授权。</x-field-desc>
22
+ <x-field data-name="typeUrl" data-type="string" data-required="true">
23
+ <x-field-desc markdown>被许可交易的类型 URL(例如,`fg:t:transfer_v2`)。</x-field-desc>
24
+ </x-field>
25
+ <x-field data-name="limit" data-type="object" data-required="false">
26
+ <x-field-desc markdown>对权限的可选限制,例如将其限制为特定的代币或资产。</x-field-desc>
27
+ <x-field data-name="tokens" data-type="Array<string>" data-required="false" data-desc="此权限适用的代币地址数组。"></x-field>
28
+ <x-field data-name="assets" data-type="Array<string>" data-required="false" data-desc="此权限适用的资产地址数组。"></x-field>
29
+ </x-field>
30
+ </x-field>
31
+ </x-field-group>
32
+
33
+ ### 返回值
34
+
35
+ <x-field data-name="result" data-type="Promise<[string, string]>" data-desc="一个 promise,解析后返回一个包含交易哈希和新创建的委托地址的数组。"></x-field>
36
+
37
+ ### 示例
38
+
39
+ 以下是如何将 `transfer` 权限从一个账户委托给另一个账户。
40
+
41
+ ```javascript Granting transfer permission icon=logos:javascript
42
+ import Client from '@ocap/client';
43
+ import { fromRandom } from '@ocap/wallet';
44
+ import { typeUrls } from '@ocap/proto';
45
+
46
+ const client = new Client('https://beta.abtnetwork.io/api');
47
+
48
+ // 授予权限的账户
49
+ const delegatorWallet = fromRandom();
50
+
51
+ // 接收权限的账户
52
+ const delegateeWallet = fromRandom();
53
+
54
+ async function grantPermission() {
55
+ try {
56
+ // 首先,确保委托人账户有资金。
57
+ // 在实际应用中,您需要向 delegatorWallet.address 转账
58
+ // 资金可以来自水龙头或另一个账户。
59
+ console.log(`Delegator address: ${delegatorWallet.address}`);
60
+ console.log(`Delegatee address: ${delegateeWallet.address}`);
61
+ console.log('请在继续操作前为委托人账户注资。');
62
+
63
+ const privileges = [
64
+ {
65
+ typeUrl: typeUrls.TransferV2Tx,
66
+ // 未指定限制,允许转移任何代币/资产
67
+ limit: { tokens: [], assets: [] },
68
+ },
69
+ ];
70
+
71
+ const [hash, delegateAddress] = await client.delegate({
72
+ from: delegatorWallet,
73
+ to: delegateeWallet,
74
+ privileges: privileges,
75
+ });
76
+
77
+ console.log('委托成功!');
78
+ console.log('交易哈希:', hash);
79
+ console.log('委托地址:', delegateAddress);
80
+ } catch (error) {
81
+ console.error('委托权限时出错:', error);
82
+ }
83
+ }
84
+
85
+ grantPermission();
86
+ ```
87
+
88
+ ## 作为受托人发送交易
89
+
90
+ 一旦获得委托,受托人钱包就可以通过在交易参数中指定 `delegator` 的地址来发送已授权的交易。该交易由受托人签名,但代表委托人执行,交易费用从委托人的账户中支付。
91
+
92
+ ### 示例
93
+
94
+ ```javascript Sending a transfer as a delegatee icon=logos:javascript
95
+ // 假设前一个示例中的 grantPermission() 函数已成功执行
96
+
97
+ // 转账的接收方
98
+ const recipientWallet = fromRandom();
99
+
100
+ async function sendDelegatedTransaction() {
101
+ try {
102
+ const hash = await client.transfer({
103
+ to: recipientWallet.address,
104
+ token: 1, // 转移 1 个链的原生代币
105
+ wallet: delegateeWallet, // 由受托人签名
106
+ delegator: delegatorWallet.address, // 代表委托人执行
107
+ });
108
+
109
+ console.log('委托转账成功!');
110
+ console.log('交易哈希:', hash);
111
+ console.log(`Check transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);
112
+ } catch (error) {
113
+ console.error('发送委托交易时出错:', error);
114
+ }
115
+ }
116
+
117
+ // 确保在链上确认委托后再调用此函数。
118
+ // sendDelegatedTransaction();
119
+ ```
120
+
121
+ ## 撤销委托
122
+
123
+ 委托人可以随时使用 `revokeDelegate` 方法撤销任何已授予的权限。您需要指定要为给定受托人撤销哪些交易类型。
124
+
125
+ ### 参数
126
+
127
+ <x-field-group>
128
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
129
+ <x-field-desc markdown>最初授予权限的委托人的钱包对象。</x-field-desc>
130
+ </x-field>
131
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
132
+ <x-field-desc markdown>其权限正在被撤销的受托人的钱包对象。</x-field-desc>
133
+ </x-field>
134
+ <x-field data-name="privileges" data-type="Array<string>" data-required="true">
135
+ <x-field-desc markdown>要撤销的交易 `typeUrl` 字符串数组。</x-field-desc>
136
+ </x-field>
137
+ </x-field-group>
138
+
139
+ ### 返回值
140
+
141
+ <x-field data-name="hash" data-type="Promise<string>" data-desc="一个 promise,解析后返回撤销操作的交易哈希。"></x-field>
142
+
143
+ ### 示例
144
+
145
+ ```javascript Revoking transfer permission icon=logos:javascript
146
+ // 假设使用与前面示例中相同的 delegatorWallet 和 delegateeWallet
147
+
148
+ async function revokePermission() {
149
+ try {
150
+ const hash = await client.revokeDelegate({
151
+ from: delegatorWallet,
152
+ to: delegateeWallet,
153
+ privileges: [typeUrls.TransferV2Tx], // 要撤销的权限列表
154
+ });
155
+
156
+ console.log('撤销成功!');
157
+ console.log('交易哈希:', hash);
158
+ console.log(`Check transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);
159
+ } catch (error) {
160
+ console.error('撤销委托时出错:', error);
161
+ }
162
+ }
163
+
164
+ // revokePermission();
165
+ ```
166
+
167
+ 此交易确认后,`delegateeWallet` 将不再能够代表 `delegatorWallet` 发送 `transfer` 交易。
@@ -0,0 +1,73 @@
1
+ # Manage Accounts
2
+
3
+ On an OCAP-powered blockchain, an account is fundamentally a state associated with an address, controlled by a pair of cryptographic keys (a public key and a private key). A key concept to understand is that accounts are created implicitly. An account officially exists on-chain as soon as it receives its first incoming transaction, such as a token or asset transfer. There is no explicit `createAccount` transaction.
4
+
5
+ This guide focuses on a critical account management operation: migrating an account to a new set of keys. This is an essential security feature that allows you to change the ownership of an account without altering its address, balance, or assets.
6
+
7
+ ## Migrate an Account to New Keys
8
+
9
+ The `migrateAccount` function allows you to associate an existing account address with a new public key. This is useful in several scenarios, such as:
10
+
11
+ - **Key Compromise**: If you suspect your private key has been exposed, you can migrate the account to a new, secure key pair to prevent unauthorized access.
12
+ - **Upgrading Security**: You might want to migrate from an older key type to a newer, more secure one.
13
+
14
+ This operation effectively transfers control of the account while preserving its history and state on the blockchain.
15
+
16
+ ### Parameters
17
+
18
+ <x-field-group>
19
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
20
+ <x-field-desc markdown>The wallet object representing the current account that is being migrated. This wallet must sign the transaction to prove ownership.</x-field-desc>
21
+ </x-field>
22
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
23
+ <x-field-desc markdown>The new wallet object to which control will be transferred. The public key from this wallet will become the new authority for the account address.</x-field-desc>
24
+ </x-field>
25
+ </x-field-group>
26
+
27
+ ### Returns
28
+
29
+ The `migrateAccount` method returns a Promise that resolves to the transaction hash as a string.
30
+
31
+ ### Example
32
+
33
+ Here's how to migrate an account from an old wallet to a new one.
34
+
35
+ ```javascript Migrating an Account icon=logos:javascript
36
+ import Client from '@ocap/client';
37
+ import { fromRandom } from '@ocap/wallet';
38
+
39
+ const client = new Client('https://beta.abtnetwork.io/api');
40
+
41
+ // Assume oldWallet is the currently authorized wallet for the account
42
+ const oldWallet = fromRandom();
43
+
44
+ // Create a new wallet with a new set of keys
45
+ const newWallet = fromRandom();
46
+
47
+ async function migrate() {
48
+ try {
49
+ // Before migrating, you would typically fund the oldWallet's address.
50
+ // For this example, we'll assume it has enough funds to pay for the transaction fee.
51
+ // You can get test tokens from https://faucet.abtnetwork.io/
52
+ console.log(`Migrating account from ${oldWallet.address} to new keys associated with ${newWallet.address}`);
53
+
54
+ const hash = await client.migrateAccount({
55
+ from: oldWallet,
56
+ to: newWallet,
57
+ });
58
+
59
+ console.log('Account migration transaction sent successfully!');
60
+ console.log('Transaction Hash:', hash);
61
+ console.log(`View transaction at: https://beta.abtnetwork.io/explorer/txs/${hash}`);
62
+
63
+ } catch (err) {
64
+ console.error('Error migrating account:', err);
65
+ }
66
+ }
67
+
68
+ migrate();
69
+ ```
70
+
71
+ ---
72
+
73
+ Now that you know how to secure your account by migrating keys, you might want to learn how to grant limited permissions to other accounts without sharing your primary keys. See the [Delegate Permissions](./how-to-guides-delegate-permissions.md) guide for more details.