@ocap/client 1.25.3 → 1.25.5

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 (92) 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.ja.md +229 -0
  5. package/docs/api-reference-client-methods.md +229 -0
  6. package/docs/api-reference-client-methods.zh-TW.md +229 -0
  7. package/docs/api-reference-client-methods.zh.md +229 -0
  8. package/docs/api-reference-data-types.ja.md +482 -0
  9. package/docs/api-reference-data-types.md +482 -0
  10. package/docs/api-reference-data-types.zh-TW.md +482 -0
  11. package/docs/api-reference-data-types.zh.md +482 -0
  12. package/docs/api-reference-low-level-api.ja.md +228 -0
  13. package/docs/api-reference-low-level-api.md +228 -0
  14. package/docs/api-reference-low-level-api.zh-TW.md +228 -0
  15. package/docs/api-reference-low-level-api.zh.md +228 -0
  16. package/docs/api-reference-query-mutation-methods.ja.md +814 -0
  17. package/docs/api-reference-query-mutation-methods.md +814 -0
  18. package/docs/api-reference-query-mutation-methods.zh-TW.md +814 -0
  19. package/docs/api-reference-query-mutation-methods.zh.md +814 -0
  20. package/docs/api-reference-transaction-helpers.ja.md +649 -0
  21. package/docs/api-reference-transaction-helpers.md +649 -0
  22. package/docs/api-reference-transaction-helpers.zh-TW.md +649 -0
  23. package/docs/api-reference-transaction-helpers.zh.md +649 -0
  24. package/docs/api-reference.ja.md +23 -0
  25. package/docs/api-reference.md +23 -0
  26. package/docs/api-reference.zh-TW.md +23 -0
  27. package/docs/api-reference.zh.md +23 -0
  28. package/docs/core-concepts-client-architecture.ja.md +102 -0
  29. package/docs/core-concepts-client-architecture.md +102 -0
  30. package/docs/core-concepts-client-architecture.zh-TW.md +102 -0
  31. package/docs/core-concepts-client-architecture.zh.md +102 -0
  32. package/docs/core-concepts-event-subscriptions.ja.md +123 -0
  33. package/docs/core-concepts-event-subscriptions.md +123 -0
  34. package/docs/core-concepts-event-subscriptions.zh-TW.md +123 -0
  35. package/docs/core-concepts-event-subscriptions.zh.md +123 -0
  36. package/docs/core-concepts-gas-payment.ja.md +111 -0
  37. package/docs/core-concepts-gas-payment.md +111 -0
  38. package/docs/core-concepts-gas-payment.zh-TW.md +111 -0
  39. package/docs/core-concepts-gas-payment.zh.md +111 -0
  40. package/docs/core-concepts-transaction-lifecycle.ja.md +183 -0
  41. package/docs/core-concepts-transaction-lifecycle.md +183 -0
  42. package/docs/core-concepts-transaction-lifecycle.zh-TW.md +183 -0
  43. package/docs/core-concepts-transaction-lifecycle.zh.md +183 -0
  44. package/docs/core-concepts.ja.md +22 -0
  45. package/docs/core-concepts.md +22 -0
  46. package/docs/core-concepts.zh-TW.md +22 -0
  47. package/docs/core-concepts.zh.md +22 -0
  48. package/docs/getting-started-basic-usage.ja.md +87 -0
  49. package/docs/getting-started-basic-usage.md +87 -0
  50. package/docs/getting-started-basic-usage.zh-TW.md +87 -0
  51. package/docs/getting-started-basic-usage.zh.md +87 -0
  52. package/docs/getting-started-installation.ja.md +60 -0
  53. package/docs/getting-started-installation.md +60 -0
  54. package/docs/getting-started-installation.zh-TW.md +60 -0
  55. package/docs/getting-started-installation.zh.md +60 -0
  56. package/docs/getting-started.ja.md +16 -0
  57. package/docs/getting-started.md +16 -0
  58. package/docs/getting-started.zh-TW.md +16 -0
  59. package/docs/getting-started.zh.md +17 -0
  60. package/docs/how-to-guides-delegate-permissions.ja.md +167 -0
  61. package/docs/how-to-guides-delegate-permissions.md +167 -0
  62. package/docs/how-to-guides-delegate-permissions.zh-TW.md +167 -0
  63. package/docs/how-to-guides-delegate-permissions.zh.md +166 -0
  64. package/docs/how-to-guides-manage-accounts.ja.md +73 -0
  65. package/docs/how-to-guides-manage-accounts.md +73 -0
  66. package/docs/how-to-guides-manage-accounts.zh-TW.md +73 -0
  67. package/docs/how-to-guides-manage-accounts.zh.md +73 -0
  68. package/docs/how-to-guides-manage-assets.ja.md +255 -0
  69. package/docs/how-to-guides-manage-assets.md +255 -0
  70. package/docs/how-to-guides-manage-assets.zh-TW.md +255 -0
  71. package/docs/how-to-guides-manage-assets.zh.md +255 -0
  72. package/docs/how-to-guides-manage-tokens.ja.md +179 -0
  73. package/docs/how-to-guides-manage-tokens.md +179 -0
  74. package/docs/how-to-guides-manage-tokens.zh-TW.md +179 -0
  75. package/docs/how-to-guides-manage-tokens.zh.md +179 -0
  76. package/docs/how-to-guides-stake-tokens-and-assets.ja.md +205 -0
  77. package/docs/how-to-guides-stake-tokens-and-assets.md +205 -0
  78. package/docs/how-to-guides-stake-tokens-and-assets.zh-TW.md +205 -0
  79. package/docs/how-to-guides-stake-tokens-and-assets.zh.md +205 -0
  80. package/docs/how-to-guides-transfer-tokens-and-nfts.ja.md +179 -0
  81. package/docs/how-to-guides-transfer-tokens-and-nfts.md +179 -0
  82. package/docs/how-to-guides-transfer-tokens-and-nfts.zh-TW.md +179 -0
  83. package/docs/how-to-guides-transfer-tokens-and-nfts.zh.md +179 -0
  84. package/docs/how-to-guides.ja.md +27 -0
  85. package/docs/how-to-guides.md +27 -0
  86. package/docs/how-to-guides.zh-TW.md +27 -0
  87. package/docs/how-to-guides.zh.md +27 -0
  88. package/docs/overview.ja.md +70 -0
  89. package/docs/overview.md +70 -0
  90. package/docs/overview.zh-TW.md +70 -0
  91. package/docs/overview.zh.md +70 -0
  92. package/package.json +14 -14
@@ -0,0 +1,111 @@
1
+ # Gas 支付
2
+
3
+ 在大多數區塊鏈系統中,每筆交易都需要發起人支付一筆費用,通常稱為「gas」。對於可能沒有原生貨幣來支付這些費用的新使用者來說,這可能是一個重大的障礙。OCAP Client 引入了 Gas 支付功能,允許應用程式開發者贊助這些費用,為其使用者創造無縫的「無 gas」體驗。
4
+
5
+ 這個強大的功能使 dApps 能夠將 gas 的複雜性抽離,從而改善使用者引導流程和應用程式的整體可用性。
6
+
7
+ ## 運作原理
8
+
9
+ Gas 支付機制是透過標準的 HTTP 標頭實現的。當您在您的客戶端實例上配置一個 gas 支付者錢包時,客戶端會自動將兩個特殊的標頭附加到它發送到區塊鏈節點的每個 `sendTx` 變異中:
10
+
11
+ - `x-gas-payer-pk`:將支付 gas 費用的錢包的公鑰。
12
+ - `x-gas-payer-sig`:由 gas 支付者的私鑰簽署的 JSON Web Token (JWT)。此權杖包含交易的雜湊值,作為贊助商為該特定交易支付費用的可驗證授權。
13
+
14
+ 當區塊鏈節點收到交易時,它會執行兩項關鍵的驗證:
15
+
16
+ 1. 它會驗證使用者在交易本身上的簽名。
17
+ 2. 它會根據交易雜湊值,驗證標頭中 gas 支付者的簽名。
18
+
19
+ 如果兩個簽名都有效,交易將以使用者的權限執行,但相應的 gas 費用會從 gas 支付者的帳戶餘額中扣除。
20
+
21
+ ### 工作流程圖
22
+
23
+ 下圖說明了從交易發起到執行的整個 gas 支付流程:
24
+
25
+ ```d2 Gas 支付工作流程 icon=mdi:gas-station
26
+ direction: down
27
+
28
+ User-App: {
29
+ label: "使用者應用程式"
30
+ shape: rectangle
31
+ }
32
+
33
+ Gas-Payer-Wallet: {
34
+ label: "Gas 支付者錢包\n(應用程式後端錢包)"
35
+ shape: cylinder
36
+ }
37
+
38
+ OCAP-Client: {
39
+ label: "OCAP Client 實例"
40
+ }
41
+
42
+ Blockchain-Node: {
43
+ label: "區塊鏈節點"
44
+ shape: rectangle
45
+ }
46
+
47
+ User-App -> OCAP-Client: "1. 使用者發起交易"
48
+ OCAP-Client -> OCAP-Client: "2. 使用使用者錢包\n準備並簽署交易"
49
+ Gas-Payer-Wallet -> OCAP-Client: "3. 為 gas 簽署交易雜湊"
50
+ OCAP-Client -> Blockchain-Node: "4. 發送交易 + Gas 支付者標頭"
51
+ Blockchain-Node -> Blockchain-Node: "5. 驗證雙方簽名"
52
+ Blockchain-Node: "6. 執行交易"
53
+ Blockchain-Node -> Gas-Payer-Wallet: "7. 扣除 gas 費用"
54
+ ```
55
+
56
+ ## 使用範例
57
+
58
+ 要啟用無 gas 交易,您只需為贊助帳戶創建一個錢包實例,並使用 `setGasPayer` 方法在客戶端上設定它。
59
+
60
+ ```javascript Gas 支付者設定與使用 icon=logos:javascript
61
+ import Client from '@ocap/client';
62
+ import Wallet, { fromRandom } from '@ocap/wallet';
63
+
64
+ // 1. 初始化客戶端以連接到 Beta 鏈
65
+ const client = new Client('https://beta.abtnetwork.io/api');
66
+
67
+ // 2. 為 gas 支付者(您的應用程式錢包)創建一個錢包
68
+ // 此錢包必須有足夠的原生代幣(TBA)來支付交易費用。
69
+ // 您可以從水龍頭獲取測試代幣:https://faucet.abtnetwork.io/
70
+ const gasPayerWallet = Wallet.fromJSON({
71
+ sk: '...your_sponsor_secret_key...',
72
+ pk: '...your_sponsor_public_key...',
73
+ address: '...your_sponsor_address...',
74
+ });
75
+
76
+ // 3. 在客戶端實例上設定 gas 支付者
77
+ client.setGasPayer(gasPayerWallet);
78
+
79
+ // 4. 為終端使用者創建一個錢包。
80
+ const userWallet = fromRandom();
81
+
82
+ // 5. 使用使用者的錢包發送一筆交易。
83
+ // 該交易由使用者簽署,但 gas 費用由 gasPayerWallet 支付。
84
+ async function performGaslessTransaction() {
85
+ try {
86
+ // 注意:雖然新帳戶在收到第一筆傳入交易時會在鏈上創建,
87
+ // 但它必須先存在於鏈上,才能*發送*交易。
88
+ // 您可以透過從水龍頭向其發送少量代幣來進行初始化。
89
+
90
+ const receiverAddress = 'z1...'; // 一個有效的收款人地址
91
+ const hash = await client.transfer({
92
+ to: receiverAddress,
93
+ tokens: [{ value: '0.1' }], // 發送 0.1 TBA
94
+ wallet: userWallet,
95
+ });
96
+
97
+ console.log('無 gas 交易成功。雜湊值:', hash);
98
+ console.log(`在區塊鏈瀏覽器上查看:https://beta.abtnetwork.io/explorer/txs/${hash}`);
99
+ } catch (error) {
100
+ console.error('交易失敗:', error);
101
+ }
102
+ }
103
+
104
+ performGaslessTransaction();
105
+ ```
106
+
107
+ 在此範例中,`userWallet` 即使原生代幣餘額為零,也可以成功發送交易,因為 `gasPayerWallet` 支付了費用。這創造了一種順暢的體驗,特別是對於加入您應用程式的新使用者。
108
+
109
+ ---
110
+
111
+ 要了解交易從創建到最終確定的完整過程,請參閱 [交易生命週期](./core-concepts-transaction-lifecycle.md) 文件。
@@ -0,0 +1,111 @@
1
+ # Gas 支付
2
+
3
+ 在大多数区块链系统中,每笔交易都需要发起者支付一笔费用,通常称为“gas”。对于没有原生货币来支付这些费用的新用户来说,这可能是一个巨大的障碍。OCAP Client 引入了 Gas 支付功能,允许应用程序开发者代付这些费用,从而为用户创造无缝的、“无 gas”体验。
4
+
5
+ 这一强大功能使 dApp 能够屏蔽 gas 的复杂性,从而改善用户引导流程和应用的整体易用性。
6
+
7
+ ## 工作原理
8
+
9
+ gas 支付机制通过标准的 HTTP 标头实现。当您在客户端实例上配置 gas 代付方钱包时,客户端会自动将两个特殊的标头附加到它发送给区块链节点的每个 `sendTx` mutation 请求中:
10
+
11
+ - `x-gas-payer-pk`:将支付 gas 费用的钱包的公钥。
12
+ - `x-gas-payer-sig`:由 gas 代付方私钥签名的 JSON Web Token (JWT)。此令牌包含交易的哈希值,作为代付方为该特定交易支付费用的可验证授权。
13
+
14
+ 当区块链节点收到交易时,它会执行两项关键验证:
15
+
16
+ 1. 验证用户在交易本身的签名。
17
+ 2. 根据交易哈希验证标头中 gas 代付方的签名。
18
+
19
+ 如果两个签名都有效,交易将在用户的授权下执行,但相应的 gas 费用将从 gas 代付方的账户余额中扣除。
20
+
21
+ ### 工作流程图
22
+
23
+ 下图说明了从交易发起 Gas 支付到执行的整个流程:
24
+
25
+ ```d2 Gas 支付工作流 icon=mdi:gas-station
26
+ direction: down
27
+
28
+ User-App: {
29
+ label: "用户应用"
30
+ shape: rectangle
31
+ }
32
+
33
+ Gas-Payer-Wallet: {
34
+ label: "Gas 代付方钱包\n(应用后端的钱包)"
35
+ shape: cylinder
36
+ }
37
+
38
+ OCAP-Client: {
39
+ label: "OCAP Client 实例"
40
+ }
41
+
42
+ Blockchain-Node: {
43
+ label: "区块链节点"
44
+ shape: rectangle
45
+ }
46
+
47
+ User-App -> OCAP-Client: "1. 用户发起交易"
48
+ OCAP-Client -> OCAP-Client: "2. 使用用户钱包\n准备并签署交易"
49
+ Gas-Payer-Wallet -> OCAP-Client: "3. 为 gas 签署交易哈希"
50
+ OCAP-Client -> Blockchain-Node: "4. 发送交易 + Gas 代付方标头"
51
+ Blockchain-Node -> Blockchain-Node: "5. 验证双方签名"
52
+ Blockchain-Node: "6. 执行交易"
53
+ Blockchain-Node -> Gas-Payer-Wallet: "7. 扣除 gas 费用"
54
+ ```
55
+
56
+ ## 用法示例
57
+
58
+ 要启用无 gas 交易,您只需为代付账户创建一个钱包实例,并使用 `setGasPayer` 方法在客户端上进行设置。
59
+
60
+ ```javascript Gas Payer 设置与使用 icon=logos:javascript
61
+ import Client from '@ocap/client';
62
+ import Wallet, { fromRandom } from '@ocap/wallet';
63
+
64
+ // 1. 初始化客户端以连接到 Beta 链
65
+ const client = new Client('https://beta.abtnetwork.io/api');
66
+
67
+ // 2. 为 gas 代付方(您的应用程序的钱包)创建一个钱包
68
+ // 此钱包必须有足够的原生代币(TBA)来支付交易费用。
69
+ // 您可以从水龙头获取测试代币:https://faucet.abtnetwork.io/
70
+ const gasPayerWallet = Wallet.fromJSON({
71
+ sk: '...your_sponsor_secret_key...',
72
+ pk: '...your_sponsor_public_key...',
73
+ address: '...your_sponsor_address...',
74
+ });
75
+
76
+ // 3. 在客户端实例上设置 gas 代付方
77
+ client.setGasPayer(gasPayerWallet);
78
+
79
+ // 4. 为最终用户创建一个钱包。
80
+ const userWallet = fromRandom();
81
+
82
+ // 5. 使用用户的钱包发送一笔交易。
83
+ // 交易由用户签名,但 gas 费用由 gasPayerWallet 支付。
84
+ async function performGaslessTransaction() {
85
+ try {
86
+ // 注意:虽然新账户在收到第一笔入账交易时会在链上创建,
87
+ // 但它必须在链上存在才能 *发送* 交易。
88
+ // 您可以通过从水龙头向其发送少量代币来初始化它。
89
+
90
+ const receiverAddress = 'z1...'; // 一个有效的接收者地址
91
+ const hash = await client.transfer({
92
+ to: receiverAddress,
93
+ tokens: [{ value: '0.1' }], // 发送 0.1 TBA
94
+ wallet: userWallet,
95
+ });
96
+
97
+ console.log('无 gas 交易成功。哈希:', hash);
98
+ console.log(`在浏览器上查看: https://beta.abtnetwork.io/explorer/txs/${hash}`);
99
+ } catch (error) {
100
+ console.error('交易失败:', error);
101
+ }
102
+ }
103
+
104
+ performGaslessTransaction();
105
+ ```
106
+
107
+ 在此示例中,`userWallet` 即使原生代币余额为零也能成功发送交易,因为 `gasPayerWallet` 承担了费用。这创造了无摩擦的体验,特别是对于加入您应用程序的新用户而言。
108
+
109
+ ---
110
+
111
+ 要了解从创建到最终确认的完整交易过程,请参阅[交易生命周期](./core-concepts-transaction-lifecycle.md)文档。
@@ -0,0 +1,183 @@
1
+ # トランザクションのライフサイクル
2
+
3
+ トークンの転送やアセットの作成など、ブロックチェーンの状態を変更するすべてのアクションは、トランザクションを通じて実行されます。トランザクションのライフサイクルを理解することは、OCAP Clientを使用してアプリケーションを構築するための基本です。このプロセスには、準備、エンコード、署名、送信の4つの主要なステージが含まれます。
4
+
5
+ OCAP Clientは、最大限の制御のためにこれらのステップを個別に実行するか、利便性のためにそれらを組み合わせた高レベルのヘルパーを使用することができる、柔軟なメソッドセットを提供します。
6
+
7
+ このガイドでは、各ステージを分解し、単一署名と複数署名の両方のワークフローを説明します。
8
+
9
+ ## ライフサイクルの概要
10
+
11
+ 以下の図は、準備からブロックチェーンへの最終的な送信までのトランザクションの完全な流れを示しています。
12
+
13
+ ```d2 Transaction Lifecycle Diagram
14
+ direction: down
15
+
16
+ Start: {
17
+ label: "1. Itxの準備"
18
+ shape: oval
19
+ }
20
+
21
+ Encode-Tx: {
22
+ label: "2. トランザクションのエンコード\n(encode<Type>Tx)"
23
+ shape: rectangle
24
+ }
25
+
26
+ Sign-Tx: {
27
+ label: "3. トランザクションの署名"
28
+ shape: diamond
29
+ }
30
+
31
+ Single-Sign: {
32
+ label: "単一署名\n(sign<Type>Tx)"
33
+ shape: rectangle
34
+ }
35
+
36
+ Multi-Sign-Flow: {
37
+ label: "複数署名"
38
+ shape: rectangle
39
+
40
+ Multi-Sign-1: {
41
+ label: "署名者1\n(multiSign<Type>Tx)"
42
+ }
43
+
44
+ Multi-Sign-2: {
45
+ label: "署名者2\n(multiSign<Type>Tx)"
46
+ }
47
+
48
+ Multi-Sign-N: {
49
+ label: "..."
50
+ }
51
+
52
+ Multi-Sign-1 -> Multi-Sign-2: "署名済みTXを渡す"
53
+ Multi-Sign-2 -> Multi-Sign-N: "署名済みTXを渡す"
54
+ }
55
+
56
+ Send-Tx: {
57
+ label: "4. トランザクションの送信\n(send<Type>Tx)"
58
+ shape: rectangle
59
+ }
60
+
61
+ End: {
62
+ label: "トランザクションハッシュ"
63
+ shape: oval
64
+ }
65
+
66
+ Start -> Encode-Tx
67
+ Encode-Tx -> Sign-Tx
68
+ Sign-Tx -> Single-Sign: "単一署名者"
69
+ Sign-Tx -> Multi-Sign-Flow: "複数署名者"
70
+ Single-Sign -> Send-Tx
71
+ Multi-Sign-Flow -> Send-Tx
72
+ Send-Tx -> End
73
+ ```
74
+
75
+ ## ステージ1:準備(`itx`の作成)
76
+
77
+ すべてのトランザクションは`itx`(内部トランザクション)から始まります。これは、実行したいアクションの特定のデータを含むプレーンなJavaScriptオブジェクトです。例えば、転送`itx`には受信者のアドレスと金額が含まれます。
78
+
79
+ ```javascript Preparing the itx icon=logos:javascript
80
+ // 転送トランザクションのコアデータ
81
+ const itx = {
82
+ to: 'z2C8j81aL2oXpA5t42s2h4g9o8p1k6m3n7b', // 受信者のアドレス
83
+ value: await client.fromTokenToUnit(10), // 送信する量、チェーンの基本単位に変換済み
84
+ };
85
+ ```
86
+
87
+ ## ステージ2:エンコード
88
+
89
+ エンコードは、`itx`と他のメタデータを、暗号学的に署名できる標準化されたバイナリ形式に変換します。クライアントは、各トランザクションタイプに対して`encode{TxType}Tx`メソッド(例:`encodeTransferV2Tx`)を提供します。
90
+
91
+ このステージで、クライアントは自動的に必須のメタデータを追加します。
92
+
93
+ * **`from`**:送信者のアドレス、提供されたウォレットから導出されます。
94
+ * **`chainId`**:ターゲットブロックチェーンの識別子、自動的に取得されます。
95
+ * **`nonce`**:リプレイ攻撃を防ぐためのユニークな番号で、デフォルトは現在のタイムスタンプ(`Date.now()`)です。
96
+ * **`pk`**:送信者のウォレットの公開鍵。
97
+
98
+ エンコード関数は、完全なトランザクションオブジェクトと、署名に使用されるシリアライズされたデータの`Buffer`の両方を返します。
99
+
100
+ ```javascript Encoding the Transaction icon=logos:javascript
101
+ const { object: encodedTx, buffer: txBuffer } = await client.encodeTransferV2Tx({
102
+ tx: { itx },
103
+ wallet: senderWallet,
104
+ });
105
+
106
+ console.log('エンコードされたTXオブジェクト:', encodedTx);
107
+ console.log('署名されるバッファ:', txBuffer);
108
+ ```
109
+
110
+ ## ステージ3:署名
111
+
112
+ 署名はアカウントの所有権を証明し、トランザクションを承認します。このプロセスは単一署名と複数署名のワークフローで異なります。
113
+
114
+ ### 単一署名ワークフロー
115
+
116
+ これは最も一般的なシナリオで、単一のユーザーがトランザクションに署名します。`sign{TxType}Tx`メソッドは、エンコードされたトランザクションを受け取り、ユーザーの秘密鍵でバイナリバッファに署名し、`signature`フィールドに値を設定します。
117
+
118
+ ```javascript Signing with a Single Signature icon=logos:javascript
119
+ const signedTx = await client.signTransferV2Tx({
120
+ tx: encodedTx, // エンコードステップからのオブジェクト
121
+ wallet: senderWallet,
122
+ });
123
+
124
+ console.log('署名:', signedTx.signature);
125
+ ```
126
+
127
+ ### 複数署名ワークフロー
128
+
129
+ 複数署名(マルチシグ)トランザクションは、複数の関係者からの承認が必要です。これは一般的にアトミックスワップや共有アカウントで使用されます。プロセスは順次実行されます。
130
+
131
+ 1. **準備**:初期トランザクションは、必要なすべての署名者を定義する`signaturesList`と共に作成されます。
132
+ 2. **順次署名**:トランザクションは署名者から次の署名者へと渡されます。各署名者は対応する`multiSign{TxType}Tx`メソッドを使用して自身の署名を追加します。
133
+
134
+ 内部的に、`multiSign`メソッドは、署名のためにトランザクションをエンコードする前に既存のすべての署名を一時的に削除することで、各関係者が全く同じトランザクションダイジェストに署名することを保証します。
135
+
136
+ 以下は、二者がアセットを交換する`ExchangeTx`の例です。
137
+
138
+ ```javascript Multi-Signature Signing Example icon=logos:javascript
139
+ // ステップ1:Alice(オファー側)が交換トランザクションを準備し、署名します。
140
+ const txFromAlice = await client.prepareExchange({
141
+ offerToken: 10,
142
+ demandToken: 20,
143
+ receiver: bobWallet.address,
144
+ wallet: aliceWallet,
145
+ });
146
+
147
+ // ステップ2:トランザクションがBobに送信されます。
148
+ // Bob(要求側)が署名を追加して最終化します。
149
+ const txFromBob = await client.finalizeExchange({
150
+ tx: txFromAlice, // Aliceによって署名されたトランザクション
151
+ wallet: bobWallet,
152
+ });
153
+
154
+ console.log('Aliceの署名:', txFromBob.signaturesList[0].signature);
155
+ console.log('Bobの署名:', txFromBob.signaturesList[1].signature);
156
+ ```
157
+
158
+ ## ステージ4:送信
159
+
160
+ トランザクションが完全に署名されると、処理のためにブロックチェーンノードに送信できます。`send{TxType}Tx`メソッドがこの最終ステップを処理します。
161
+
162
+ 便宜上、ウォレットと未署名のトランザクションを提供すれば、これらのメソッドは署名ステップを暗黙的に実行することもできます。メソッドは、送信が成功するとトランザクションハッシュで解決されるプロミスを返します。
163
+
164
+ ```javascript Sending the Signed Transaction icon=logos:javascript
165
+ // 事前に署名されたトランザクションを使用
166
+ const hash = await client.sendTransferV2Tx({ tx: signedTx, wallet: senderWallet });
167
+
168
+ // または、sendメソッドに署名を自動的に処理させる
169
+ const hash2 = await client.sendTransferV2Tx({
170
+ tx: { itx }, // 内部トランザクションのみ
171
+ wallet: senderWallet,
172
+ });
173
+
174
+ console.log('トランザクション送信完了! ハッシュ:', hash);
175
+ ```
176
+
177
+ `commit: true`オプションを含めることで、プロミスが解決される前に、トランザクションが完全に確認され、ブロックに含まれるまでクライアントを待機させることもできます。
178
+
179
+ ## まとめ
180
+
181
+ トランザクションのライフサイクルは、ブロックチェーンと対話するための堅牢で柔軟なフレームワークを提供します。プロセスを準備、エンコード、署名、送信という明確なステージに分けることで、OCAP Clientは開発者にトランザクション作成に対するきめ細かな制御を提供すると同時に、一般的なユースケースのためのシンプルで高レベルなヘルパーも提供します。
182
+
183
+ トランザクション手数料の処理方法についての詳細は、[ガス支払い](./core-concepts-gas-payment.md)ガイドを参照してください。利用可能なさまざまなクライアントタイプを理解するには、[クライアントアーキテクチャ](./core-concepts-client-architecture.md)ドキュメントを参照してください。
@@ -0,0 +1,183 @@
1
+ # Transaction Lifecycle
2
+
3
+ Every action that modifies the state of the blockchain, such as transferring tokens or creating an asset, is executed through a transaction. Understanding the lifecycle of a transaction is fundamental to building applications with the OCAP Client. This process involves four main stages: Preparation, Encoding, Signing, and Sending.
4
+
5
+ The OCAP Client provides a flexible set of methods that allow you to either perform these steps individually for maximum control or use high-level helpers that combine them for convenience.
6
+
7
+ This guide breaks down each stage and illustrates both single-signature and multi-signature workflows.
8
+
9
+ ## Lifecycle Overview
10
+
11
+ The following diagram illustrates the complete journey of a transaction from preparation to its final submission to the blockchain.
12
+
13
+ ```d2 Transaction Lifecycle Diagram
14
+ direction: down
15
+
16
+ Start: {
17
+ label: "1. Prepare Itx"
18
+ shape: oval
19
+ }
20
+
21
+ Encode-Tx: {
22
+ label: "2. Encode Transaction\n(encode<Type>Tx)"
23
+ shape: rectangle
24
+ }
25
+
26
+ Sign-Tx: {
27
+ label: "3. Sign Transaction"
28
+ shape: diamond
29
+ }
30
+
31
+ Single-Sign: {
32
+ label: "Single Signature\n(sign<Type>Tx)"
33
+ shape: rectangle
34
+ }
35
+
36
+ Multi-Sign-Flow: {
37
+ label: "Multi-Signature"
38
+ shape: rectangle
39
+
40
+ Multi-Sign-1: {
41
+ label: "Signer 1\n(multiSign<Type>Tx)"
42
+ }
43
+
44
+ Multi-Sign-2: {
45
+ label: "Signer 2\n(multiSign<Type>Tx)"
46
+ }
47
+
48
+ Multi-Sign-N: {
49
+ label: "..."
50
+ }
51
+
52
+ Multi-Sign-1 -> Multi-Sign-2: "Pass signed TX"
53
+ Multi-Sign-2 -> Multi-Sign-N: "Pass signed TX"
54
+ }
55
+
56
+ Send-Tx: {
57
+ label: "4. Send Transaction\n(send<Type>Tx)"
58
+ shape: rectangle
59
+ }
60
+
61
+ End: {
62
+ label: "Transaction Hash"
63
+ shape: oval
64
+ }
65
+
66
+ Start -> Encode-Tx
67
+ Encode-Tx -> Sign-Tx
68
+ Sign-Tx -> Single-Sign: "Single Signer"
69
+ Sign-Tx -> Multi-Sign-Flow: "Multiple Signers"
70
+ Single-Sign -> Send-Tx
71
+ Multi-Sign-Flow -> Send-Tx
72
+ Send-Tx -> End
73
+ ```
74
+
75
+ ## Stage 1: Preparation (Creating the `itx`)
76
+
77
+ Every transaction begins with an `itx` (inner transaction). This is a plain JavaScript object that contains the specific data for the action you want to perform. For example, a transfer `itx` would include the recipient's address and the amount.
78
+
79
+ ```javascript Preparing the itx icon=logos:javascript
80
+ // The core data for our transfer transaction
81
+ const itx = {
82
+ to: 'z2C8j81aL2oXpA5t42s2h4g9o8p1k6m3n7b', // Recipient's address
83
+ value: await client.fromTokenToUnit(10), // Amount to send, converted to the chain's base unit
84
+ };
85
+ ```
86
+
87
+ ## Stage 2: Encoding
88
+
89
+ Encoding transforms the `itx` and other metadata into a standardized, binary format that can be cryptographically signed. The client provides an `encode{TxType}Tx` method for each transaction type (e.g., `encodeTransferV2Tx`).
90
+
91
+ During this stage, the client automatically adds essential metadata:
92
+
93
+ * **`from`**: The sender's address, derived from the provided wallet.
94
+ * **`chainId`**: The identifier of the target blockchain, fetched automatically.
95
+ * **`nonce`**: A unique number to prevent replay attacks, which defaults to the current timestamp (`Date.now()`).
96
+ * **`pk`**: The public key of the sender's wallet.
97
+
98
+ The encoding function returns both the full transaction object and a `Buffer` of the serialized data, which is used for signing.
99
+
100
+ ```javascript Encoding the Transaction icon=logos:javascript
101
+ const { object: encodedTx, buffer: txBuffer } = await client.encodeTransferV2Tx({
102
+ tx: { itx },
103
+ wallet: senderWallet,
104
+ });
105
+
106
+ console.log('Encoded TX Object:', encodedTx);
107
+ console.log('Buffer to be signed:', txBuffer);
108
+ ```
109
+
110
+ ## Stage 3: Signing
111
+
112
+ Signing proves ownership of the account and authorizes the transaction. The process differs for single-signature and multi-signature workflows.
113
+
114
+ ### Single-Signature Workflow
115
+
116
+ This is the most common scenario, where a single user signs a transaction. The `sign{TxType}Tx` methods take the encoded transaction, sign the binary buffer with the user's private key, and populate the `signature` field.
117
+
118
+ ```javascript Signing with a Single Signature icon=logos:javascript
119
+ const signedTx = await client.signTransferV2Tx({
120
+ tx: encodedTx, // The object from the encoding step
121
+ wallet: senderWallet,
122
+ });
123
+
124
+ console.log('Signature:', signedTx.signature);
125
+ ```
126
+
127
+ ### Multi-Signature Workflow
128
+
129
+ Multi-signature (multisig) transactions require approval from multiple parties. This is commonly used for atomic swaps or shared accounts. The process is sequential:
130
+
131
+ 1. **Preparation**: The initial transaction is created with a `signaturesList` that defines all required signers.
132
+ 2. **Sequential Signing**: The transaction is passed from one signer to the next. Each signer uses the corresponding `multiSign{TxType}Tx` method to add their signature.
133
+
134
+ Internally, the `multiSign` method ensures that each party signs the exact same transaction digest by temporarily stripping all existing signatures before encoding the transaction for signing.
135
+
136
+ Here’s an example for an `ExchangeTx`, where two parties swap assets.
137
+
138
+ ```javascript Multi-Signature Signing Example icon=logos:javascript
139
+ // Step 1: Alice (the offerer) prepares and signs the exchange transaction.
140
+ const txFromAlice = await client.prepareExchange({
141
+ offerToken: 10,
142
+ demandToken: 20,
143
+ receiver: bobWallet.address,
144
+ wallet: aliceWallet,
145
+ });
146
+
147
+ // Step 2: The transaction is sent to Bob.
148
+ // Bob (the demander) adds his signature to finalize it.
149
+ const txFromBob = await client.finalizeExchange({
150
+ tx: txFromAlice, // Transaction signed by Alice
151
+ wallet: bobWallet,
152
+ });
153
+
154
+ console.log('Alice\'s Signature:', txFromBob.signaturesList[0].signature);
155
+ console.log('Bob\'s Signature:', txFromBob.signaturesList[1].signature);
156
+ ```
157
+
158
+ ## Stage 4: Sending
159
+
160
+ Once a transaction is fully signed, it can be sent to the blockchain node for processing. The `send{TxType}Tx` methods handle this final step.
161
+
162
+ For convenience, these methods can also perform the signing step implicitly if you provide a wallet and an unsigned transaction. The method returns a promise that resolves with the transaction hash upon successful submission.
163
+
164
+ ```javascript Sending the Signed Transaction icon=logos:javascript
165
+ // Using a pre-signed transaction
166
+ const hash = await client.sendTransferV2Tx({ tx: signedTx, wallet: senderWallet });
167
+
168
+ // Or, letting the send method handle the signing automatically
169
+ const hash2 = await client.sendTransferV2Tx({
170
+ tx: { itx }, // Just the inner transaction
171
+ wallet: senderWallet,
172
+ });
173
+
174
+ console.log('Transaction sent! Hash:', hash);
175
+ ```
176
+
177
+ You can also include a `commit: true` option to make the client wait until the transaction is fully confirmed and included in a block before resolving the promise.
178
+
179
+ ## Summary
180
+
181
+ The transaction lifecycle provides a robust and flexible framework for interacting with the blockchain. By breaking the process into distinct stages—Preparation, Encoding, Signing, and Sending—the OCAP Client offers developers granular control over transaction creation while also providing simple, high-level helpers for common use cases.
182
+
183
+ For more details on how transaction fees are handled, see the [Gas Payment](./core-concepts-gas-payment.md) guide. To understand the different client types available, refer to the [Client Architecture](./core-concepts-client-architecture.md) documentation.