# prepareして署名しexecuteする

> Ledger APIのInteractiveSubmissionServiceが持つprepareと3つのexecuteのRPCの要求と応答の欄を、2つの図と表で追います。

- URL: https://cardene.dev/diagrams/canton-lapiint-sign-flow
- 公開日: 2026-11-23
- 更新日: 2026-11-23
- トピック: Blockchain
- タグ: Canton
- 親の図解: [interactive submission](https://cardene.dev/diagrams/canton-interactive-submission.md): 秘密鍵を自分で持つexternal partyが、準備と署名と実行の3段でトランザクションを送る流れを、Walletの手順とLedger APIの呼び出しで追います。

前の頁は、external partyのtopologyを、Ledger APIの`PartyManagementService`で生成して割り当てるRPCを追いました。
この頁は、external partyのトランザクションを、Ledger APIの`InteractiveSubmissionService`で、準備して署名して実行するRPCを追います。
RPCは、ネットワーク越しに、別のプログラムの処理を呼び出す操作です。
署名して送る6つの段の全体は、「external partyで署名して送る」の頁が追っています。

https://cardene.dev/diagrams/canton-external-sign-flow

## 参照に載る6件のRPCのうち4つを追う

`com.daml.ledger.api.v2.interactive`のパッケージは、1つのサービスと、6件のエンドポイントと、27のメッセージと、1つの列挙型を持つgRPCのパッケージで、2つのソースファイルから成ります。
`interactive_submission_common_data.proto`は、サービスを持たず、2つのメッセージを持ち、列挙型を持ちません。
`interactive_submission_service.proto`は、1つのサービスと、20のメッセージと、1つの列挙型を持ちます。

:::details gRPC
gRPCは、Googleが開発した、ネットワーク越しに別のプログラムの処理を呼び出すための通信の仕組みです。
:::

サービスの`InteractiveSubmissionService`は、参照の頁に6件のRPCが載ります。
6件のうち`GetPreferredPackageVersion`は、版3.7.0で消えました。
最後に使えた版は3.6.4で、参照の頁には履歴として残るだけです。

今の環境で呼べるRPCは、残る5つです。
この頁は、準備する`PrepareSubmission`と、3つの実行の`ExecuteSubmission`、`ExecuteSubmissionAndWait`、`ExecuteSubmissionAndWaitForTransaction`を追います。
残る1つの`GetPreferredPackages`は、優先するパッケージを問うRPCで、履歴の1件と合わせて、「外部署名の送信で渡す欄と制約」の頁が追っています。

https://cardene.dev/diagrams/canton-interactive-submission-fields

4つのRPCは、どれもクライアントもサーバーもストリーミングを使わず、パスは`/com.daml.ledger.api.v2.interactive.InteractiveSubmissionService/`の後にRPCの名前を続けた形です。
次の表は、4つのRPCの要求と応答のメッセージです。

| RPC | 要求 | 応答 |
|---|---|---|
| `PrepareSubmission` | `PrepareSubmissionRequest` | `PrepareSubmissionResponse` |
| `ExecuteSubmission` | `ExecuteSubmissionRequest` | `ExecuteSubmissionResponse` |
| `ExecuteSubmissionAndWait` | `ExecuteSubmissionAndWaitRequest` | `ExecuteSubmissionAndWaitResponse` |
| `ExecuteSubmissionAndWaitForTransaction` | `ExecuteSubmissionAndWaitForTransactionRequest` | `ExecuteSubmissionAndWaitForTransactionResponse` |

## prepareの要求は15の欄を持つ

`PrepareSubmissionRequest`は、次の15の欄を持ちます。
欄の意味と、断られる条件は、「外部署名の送信で渡す欄と制約」の頁が追っています。
外部署名のガイドのサービスの定義では、`command_id`と、空でない`commands`と、空でない`act_as`が必須で、`user_id`は、利用者のトークンで認証する時は不要です。
ほかの欄は任意です。

| 欄 | 型 |
|---|---|
| `user_id` | 文字列 |
| `command_id` | 文字列 |
| `commands` | `Command`の列 |
| `min_ledger_time` | `MinLedgerTime` |
| `max_record_time` | `Timestamp` |
| `act_as` | 文字列の列 |
| `read_as` | 文字列の列 |
| `disclosed_contracts` | `DisclosedContract`の列 |
| `synchronizer_id` | 文字列 |
| `package_id_selection_preference` | 文字列の列 |
| `verbose_hashing` | bool |
| `prefetch_contract_keys` | `PrefetchContractKey`の列 |
| `estimate_traffic_cost` | `CostEstimationHints` |
| `hashing_scheme_version` | `HashingSchemeVersion` |
| `taps_max_passes` | uint32 |

列は、Protobufの定義で`repeated`と書かれた、同じ型の値を並べた欄です。

:::details Protobuf
Protobufは、Protocol Buffersの略で、Googleが開発した、データを決まった形式のバイト列に変換してやり取りするための仕組みです。
送るデータの形を先に定義しておき、その定義から送る側と受ける側のコードを作ります。
:::

`PrepareSubmissionResponse`は、`PreparedTransaction`型の`prepared_transaction`、bytesの`prepared_transaction_hash`、`HashingSchemeVersion`型の`hashing_scheme_version`、文字列の`hashing_details`、`CostEstimation`型の`cost_estimation`の5つの項目を定義します。
毎回設定されるのは、トランザクションとハッシュとハッシュ方式の版です。
`hashing_details`は、要求で`verbose_hashing`を有効にした時だけ設定され、`cost_estimation`は任意です。
`PreparedTransaction`の形は、次の頁「署名前のトランザクションを読む」が追っています。

https://cardene.dev/diagrams/canton-lapiint-tx-shape

## 署名の渡し方と、3つの実行の応答の違い

実行の要求は、署名を`PartySignatures`型の`party_signatures`で渡します。
`PartySignatures`は、`SinglePartySignatures`の列の`signatures`を持ち、`SinglePartySignatures`は、文字列の`party`と、`Signature`の列の`signatures`の2つを持ちます。
`HashingSchemeVersion`の列挙型は、Ledger APIの参照の今の定義で、`HASHING_SCHEME_VERSION_UNSPECIFIED`、`HASHING_SCHEME_VERSION_V2`、`HASHING_SCHEME_VERSION_V3`、`HASHING_SCHEME_VERSION_V4`の4つの値を持ちます。

外部署名のガイドが載せる定義の例は、V4を持たない3つの値で、V1は予約済みで扱わないと書きます。
そのガイドの定義の例は、準備の要求の`hashing_scheme_version`の既定をV2と書きます。
`MinLedgerTime`は、`Timestamp`型の`min_ledger_time_abs`か、`Duration`型の`min_ledger_time_rel`を持ちます。

3つの実行のRPCは、共通の8つの欄の要求を受け取り、`ExecuteSubmissionAndWaitForTransaction`だけが、`transaction_format`を加えた9つの欄を受け取ります。
`ExecuteSubmissionRequest`は、次の8つの欄を持ちます。

| 欄 | 型 |
|---|---|
| `prepared_transaction` | `PreparedTransaction` |
| `party_signatures` | `PartySignatures` |
| `deduplication_duration` | `Duration` |
| `deduplication_offset` | int64 |
| `submission_id` | 文字列 |
| `user_id` | 文字列 |
| `hashing_scheme_version` | `HashingSchemeVersion` |
| `min_ledger_time` | `MinLedgerTime` |

`deduplication_duration`と`deduplication_offset`は、重複排除の期間の指定で、どちらか一方だけを指定します。
外部署名のガイドのサービスの定義では、準備されたトランザクションと署名と送信のIDとハッシュ方式の版が必須で、重複排除の指定と利用者のIDと最小の台帳時刻は任意です。
そのため、署名と準備されたトランザクションだけでは、実行を頼めません。

`ExecuteSubmissionAndWaitRequest`は、同じ8つの欄を持ちます。
`ExecuteSubmissionAndWaitForTransactionRequest`だけが、`TransactionFormat`型の`transaction_format`を加えた9つの欄を持ちます。
応答は、`ExecuteSubmissionResponse`が欄を持たず、`ExecuteSubmissionAndWaitResponse`が文字列の`update_id`とint64の`completion_offset`を持ち、`ExecuteSubmissionAndWaitForTransactionResponse`が`Transaction`型の`transaction`を持ちます。

図Aは、3つの実行のRPCの、共通の8つの欄への追加の欄と、応答の違いを、RPCごとに広げます。

```dragon
mind 3つの実行のRPCの違い

step 実行のRPCを置く
  say 段1では、3つの実行のRPCを、署名したトランザクションを受け取る実行のRPCとして、真ん中から3つの枝に分けます。要求の追加の欄と応答が違います。
  idea root as "実行のRPC" [color: slate]
  idea e1 as "ExecuteSubmission" of root [color: blue]
  idea e2 as "ExecuteSubmissionAndWait" of root [color: rose]
  idea e3 as "ExecuteSubmissionAndWaitForTransaction" of root [color: blue]
  source CantonのInteractiveSubmissionService [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive]

step 欄の無い応答と、更新のIDと位置の応答を足す
  say 段2では、ExecuteSubmissionは、共通の8つの欄の要求を受け取り、欄の無い空の応答を返します。ExecuteSubmissionAndWaitは、同じ要求を受け取り、完了した更新のIDと台帳上の位置を返します。
  idea a1 as "要求は共通の8つの欄" of e1 [color: blue]
  idea a2 as "応答は欄が無い" of e1 [color: blue]
  idea b1 as "要求は共通の8つの欄" of e2 [color: rose]
  idea b2 as "応答は更新のIDと台帳上の位置" of e2 [color: rose]
  source CantonのExecuteSubmissionAndWait [url: https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmissionandwait]

step トランザクションを返す実行を足す
  say 段3では、ExecuteSubmissionAndWaitForTransactionは、共通の8つの欄に、取得するトランザクションの形式の欄を足した9つの欄の要求を受け取り、トランザクションを返します。
  idea c1 as "要求は形式の欄を足した9つの欄" of e3 [color: blue]
  idea c2 as "応答はトランザクション" of e3 [color: blue]
  source CantonのExecuteSubmissionAndWaitForTransaction [url: https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmissionandwaitfortransaction]
```

3つの実行のRPCは、トランザクションの完了を待つかと、トランザクションを返すかが違います。
`ExecuteSubmission`は、準備したトランザクションを台帳で非同期に実行します。
`ExecuteSubmissionAndWait`は、`ExecuteSubmission`に似て、トランザクションの完了を同期で待ちます。
`ExecuteSubmissionAndWaitForTransaction`は、`ExecuteSubmissionAndWait`に加えて、トランザクションも返します。

| RPC | 応答の項目 |
|---|---|
| `ExecuteSubmission` | 項目なし |
| `ExecuteSubmissionAndWait` | `update_id`と`completion_offset` |
| `ExecuteSubmissionAndWaitForTransaction` | `transaction` |

送信の要求への応答と、処理の完了と、台帳への反映は、別です。
`ExecuteSubmission`の応答は、実行の要求を受け付けた応答で、トランザクションの完了や台帳への反映を示しません。
完了は、`ExecuteSubmissionAndWait`の応答か、完了のストリームで確かめ、台帳への反映は、コミットされたトランザクションで確かめます。

## prepareから実行までを3つの段で追う

署名する主体は利用者で、署名する対象は、準備したトランザクションのハッシュです。
外部署名のガイドの例は、準備の応答の`prepared_transaction_hash`に署名します。
準備したノードを信頼できない時は、署名の前に、トランザクションの内容を利用者に示して確かめ、クライアントがハッシュを計算し直します。

その時の署名は、計算し直したハッシュに対して行います。
署名する主体、計算し直す主体、署名を送る主体は、分けて考えます。
署名を付けたトランザクションを、3つの実行のRPCのどれかで送ります。

図Bは、利用者とLedger APIのサーバーの間で、準備、署名、実行を追います。
図Bの段2は、外部署名のガイドの3段目と4段目にあたります。

```dragon
sequence prepareして署名しexecuteする

box U as 利用者 [note: 外の鍵を持ち、署名する, color: slate, icon: user]
box S as Ledger APIのサーバー [note: 準備と実行を受ける, color: rose, icon: server]

step 準備を頼む
  say 段1では、利用者は、15の欄を持つ準備の要求を送ります。サーバーは、準備したトランザクションと、そのハッシュと、ハッシュ方式の版を返します。ハッシュの詳細は、要求で求めた時だけ、トラフィックコストの見積もりは、任意で返ります。
  U -> S: 準備の要求
  S -> U: トランザクションとハッシュと方式の版
  source CantonのPrepareSubmission [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/preparesubmission]

step 署名を付ける
  say 段2では、利用者は、準備したトランザクションの内容を確かめ、外の鍵で署名します。準備したノードを信頼できない時は、ハッシュ方式を確かめ、ハッシュを計算し直して応答のハッシュと比べ、計算し直したハッシュに署名します。署名は、partyごとにまとめ、署名の形式、署名したバイト列、署名した鍵のfingerprint、署名の方式の4つの欄で表します。
  act U: 内容を確かめ、署名して、partyごとの署名を作る
  source CantonのExecuteSubmission [url: https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmission]
  source Cantonの外部署名のガイド [url: https://docs.canton.network/appdev/deep-dives/external-signing-transactions-part-1]

step 実行を頼む
  say 段3では、利用者は、準備したトランザクションと署名に加えて、送信のIDとハッシュ方式の版を、3つの実行のRPCのどれかへ送ります。重複排除の指定は任意で、期間かoffsetの一方だけです。応答は、選んだRPCで違います。
  U -> S: トランザクションと署名と送信のID
  S -> U: 実行の応答
  source CantonのExecuteSubmission [url: https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmission]
```

## 要求から辿れる型

Ledger APIの参照の頁は、要求や応答から辿れる型を、`Related Schemas`に並べます。

`ExecuteSubmission`の頁は、要求から辿れる形を、用途ごとに次のとおり並べます。

| 用途 | 並ぶ型 |
|---|---|
| 準備したトランザクション | `PreparedTransaction`、`DamlTransaction`とその`NodeSeed`と`Node`、`transaction.v1`の`Node`と`Create`と`Fetch`と`Exercise`と`ExternalCallResult`と`Rollback`と`QueryByKey` |
| 値と識別子 | `Identifier`と`Value`の仲間、`GlobalKeyWithMaintainers`、`GlobalKey` |
| metadata | `Metadata`とその`SubmitterInfo`と`GlobalKeyMappingEntry`と`InputContract` |
| 署名 | `PartySignatures`、`SinglePartySignatures`、`Signature`、`SignatureFormat`、`SigningAlgorithmSpec` |
| 実行の要求の欄 | `HashingSchemeVersion`、`MinLedgerTime` |

`ExecuteSubmissionAndWait`の頁は、同じ要求から辿れる形を並べ、応答の形だけが、`update_id`と`completion_offset`を持つ`ExecuteSubmissionAndWaitResponse`に替わります。
`PrepareSubmission`の頁は、44の形を、次の用途ごとに並べます。

| 用途 | 並ぶ型 |
|---|---|
| 準備の入力 | `Command`と4種のコマンド、`Identifier`と`Value`の仲間、`MinLedgerTime`、`DisclosedContract`、`PrefetchContractKey`、`CostEstimationHints` |
| 署名とハッシュ | `SigningAlgorithmSpec`、`HashingSchemeVersion` |
| 準備したトランザクション | `PreparedTransaction`、`DamlTransaction`、`transaction.v1`の節点の型、`Metadata`とその内側の型 |
| 見積もり | `CostEstimation`、`ReassignmentCost` |

## grpcurlの呼び出し例の送る物と返る物

4つのRPCの頁は、`grpcurl`の呼び出しの例を載せます。
`grpcurl`は、gRPCのサーバーのRPCを、JSONで呼び出すための道具です。

:::details JSON
JSONは、データを文字で表すための形式で、Webのプログラムどうしのデータのやり取りに広く使われています。
:::

例は、`<HOST:PORT>`に、サービスの名前とRPCの名前を続けた形で、`-d @`で標準入力からJSONの本文を渡します。
サーバーがTLSを使わない時は、`-plaintext`を指定します。

:::details TLS
TLSは、ネットワークの通信を暗号化し、通信の相手が本物かを証明書で確かめるための規格です。
WebサイトのHTTPSの通信も、TLSを使っています。
:::

JSONの例は、欄をcamelCaseで書き、bytesの欄は`BASE64_ENCODED_BYTES`、uint64の欄は`"0"`の文字列、列挙型は`SIGNATURE_FORMAT_UNSPECIFIED`のような名前の文字列で書きます。
例の値は型の記入例で、既定の値でも、そのまま送れる値でもありません。
準備の要求の例は、`userId`、`commandId`、`commands`、`minLedgerTime`の`minLedgerTimeAbs`、`maxRecordTime`、`actAs`、`readAs`、`disclosedContracts`を渡します。

`commands`の例は、`create`のコマンドを1つ入れ、`templateId`を`packageId`と`moduleName`と`entityName`の3つで、`createArguments`を`recordId`と`fields`で書きます。
`disclosedContracts`の例は、`templateId`、`contractId`、`createdEventBlob`、`synchronizerId`を持ち、`createdEventBlob`の例の値は`BASE64_ENCODED_BYTES`です。

準備の応答の例は、`preparedTransaction`の`transaction`と`metadata`、`preparedTransactionHash`、`hashingSchemeVersion`、`hashingDetails`、`costEstimation`を返します。
`preparedTransaction`の例の欄の並びは、実行の3つの要求の例に渡す`preparedTransaction`と同じです。
`preparedTransaction`の中は、`transaction`と`metadata`の2つです。
`transaction`は、`version`、`roots`、`nodes`、`nodeSeeds`を持ち、`metadata`は、`submitterInfo`、`synchronizerId`、`mediatorGroup`、`transactionUuid`、`preparationTime`、`inputContracts`、`minLedgerEffectiveTime`、`maxLedgerEffectiveTime`を持ちます。

`costEstimation`の例は、見積もりの数を文字列の`"0"`で書きます。
実行の要求の例は、`preparedTransaction`と、`partySignatures`の`signatures`の配列に`party`と署名の配列を入れた形を送ります。
署名の配列の要素は、`format`と`signature`と`signedBy`と`signingAlgorithmSpec`の4つの欄を持ちます。
そのほかに、`deduplicationDuration`、`submissionId`、`userId`、`hashingSchemeVersion`、`minLedgerTime`の`minLedgerTimeAbs`を送ります。

応答の例は、`ExecuteSubmission`が空のオブジェクト`{}`、`ExecuteSubmissionAndWait`が`{"updateId": "string", "completionOffset": "0"}`の形でoffsetを文字列で返します。
空のオブジェクトの`{}`は、欄を持たない応答を表すJSONの書き方です。

`ExecuteSubmissionAndWait`の例は、呼ぶ先を`InteractiveSubmissionService/ExecuteSubmissionAndWait`にして、`ExecuteSubmission`の例と同じJSONを渡します。
`ExecuteSubmissionAndWaitForTransaction`の例の応答は、`transaction`の`updateId`、`commandId`、`workflowId`、`effectiveAt`、`created`のイベント、`offset`、`synchronizerId`、`traceContext`を返します。
`created`のイベントは、`contractKeyHash`と`createdEventBlob`を、base64で持ちます。

## 次に読む頁

次の頁は、準備の応答で返るトランザクションの節点と、metadataと、traffic costの見積もりの形を追います。

https://cardene.dev/diagrams/canton-lapiint-tx-shape

## 出典

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmission

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/preparesubmission

https://docs.canton.network/reference/grpc-ledger-api-reference/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmissionandwait

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmission

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmissionandwait

https://docs.canton.network/reference/protobuf/operations/com-daml-ledger-api-v2-interactive/interactivesubmissionservice/executesubmissionandwaitfortransaction

https://docs.canton.network/appdev/deep-dives/external-signing-transactions-part-1
