ICP Rust CDK学習メモ
  • ICP Rust CDK学習メモ
  • ⚙️開発環境準備
  • 開発
    • テンプレート生成 (dfx new)
    • Frontend開発
      • 1. シンプルなHTML
      • 2. Canister呼び出し
    • Backend開発
      • 1. Hello
      • 2. データ更新/参照
      • 3. ic_cdk::caller()
      • 4. ic_cdk::call()
      • 5. 動的画像生成
      • 6. staticファイル参照
      • 7. HTTPS outcalls
      • 8. Threshold ECDSA
    • Bootcamp
      • Calculator ➕✖️➖➗
      • Homework diary 📔
      • Student wall 🎨
      • MotoCoin 🪙
      • The Verifier 👨‍🏫
      • The Dapp 🚀
  • サンプル
    • 📔Diary
  • 技術情報
    • 🪪Principal
    • 🪪Internet Identity
    • 🪙Token
    • 🖼️NFT
    • 🗝️VetKey
  • ツール
    • dfx
      • dfx identity
    • quill
  • 参考情報
    • 🔗リンク集
GitBook提供
このページ内
  • Canister呼び出し
  • (1) Candid UIからの呼び出し
  • (2) dfxコマンドからの呼び出し
  • 別のidentityによる呼び出し
  • 別のidentity作成
  • a. 登録済identity一覧表示
  • b. dfxコマンドによる新しいidentity作成

役に立ちましたか?

GitHubで編集
  1. 開発
  2. Backend開発

3. ic_cdk::caller()

前へ2. データ更新/参照次へ4. ic_cdk::call()

最終更新 1 年前

役に立ちましたか?

Canisterが誰から呼ばれたかという情報はシステムやデータへのアクセス制御を行う上で必要です。

Rust CDKでは、ic_cdk::caller()関数を呼び出すことにより呼び出し元を取得することができます。

『』のサンプルは、パラメータで渡されたname値を「Hello, 〇〇!」の形で返すものでしたが、名前の部分をパラメータで渡された値ではなく、ic_cdk::caller()関数から返ってきた値にしてみましょう。

以下の2ファイルを編集しましょう。(とは同じでOK)

service : {
    "greet": () -> (text) query;
}

#[ic_cdk::query]
fn greet() -> String {
    let caller = ic_cdk::caller();
    format!("Hello, {}!", caller)
}

Canister呼び出し

Canisterに配置して呼び出してみましょう。

(1) Candid UIからの呼び出し

まずは、dfx deployコマンドを実行した際に表示されるCandid UIのURLをWebブラウザに指定して、Candid UIページから呼び出してみます。

Hello, {}の{}の部分に、callerを識別子らしき値が表示されていることが確認できます。

(2) dfxコマンドからの呼び出し

dfx canister callコマンドを使って、PCのターミナル上からCanisterを呼び出すことができます。

使い方は以下の通りです。

$ dfx canister call --help
dfx-canister-call 
Calls a method on a deployed canister

USAGE:
    dfx canister call [OPTIONS] <CANISTER_NAME> <METHOD_NAME> [ARGUMENT]

ARGS:
    <CANISTER_NAME>    Specifies the name of the canister to build. You must specify either a
                       canister name or the --all option
    <METHOD_NAME>      Specifies the method name to call on the canister
    <ARGUMENT>         Specifies the argument to pass to the method
︙
$ dfx canister call backend greet
("Hello, xxxxx-xxxxx-…-xxxxx!")

別のidentityによる呼び出し

使用している環境に複数のidentityが登録されている場合、--identityオプションで指定したidentityでcanisterを呼び出すことができます。

$ dfx canister call --identity <identitiy名> backend greet

別のidentity作成

Candidからの呼び出した場合とdfxコマンドからの呼び出した場合で、callerが異なることを確認できました。

dfxコマンドでは、複数のidentityを管理することができます。

a. 登録済identity一覧表示

以下のコマンドでidentity一覧を表示することができます。

$ dfx identity list
anonymous
default *

b. dfxコマンドによる新しいidentity作成

以下のコマンドで作成することができます。

$ dfx identity new [オプション] <identitiy名>

コマンドの詳細や、オプションは以下のように、dfx identity new --helpを実行すれば確認できます。

$ dfx identity new --help

USAGE:
    dfx identity new [OPTIONS] <NEW_IDENTITY>

ARGS:
    <NEW_IDENTITY>    The name of the identity to create
︙

以下のコマンド実行例は、「test」という名のidentityをつくるものです。

$ dfx identity new test
Please enter a passphrase for your identity: [hidden]
Encryption complete.
Your seed phrase for identity 'test': XXXX XXXX XXXX …
This can be used to reconstruct your key in case of emergency, so write it down in a safe place.
Created identity: "test".

identityの実体はECDSA秘密鍵です。

コマンドを実行すると、自動で秘密鍵が生成され、その秘密鍵を導出できるSeed Phrase (24 words)が表示されます。

また、ユーザーディレクトリにpem (Privacy-Enhanced Mail)ファイルが、passphraseで暗号化された形で保存されます。

~/.config/dfx/identity/<identity名>/
├── identity.json
└── identity.pem.encrypted

Passphraseは秘密鍵を参照する際に必要なパスワードのようなもので、dfxコマンドなどで秘密鍵を使う場合に毎回入力を求められます。

なお、「--storage-mode=plaintext」オプションを指定すれば、passphraseなしのpemファイルを出力することも可能です。

ローカルPC環境向けにテスト用にidentityをつくるだけならとくに問題にはなりませんが、本番稼働しているIC Network上でidentityを使用する場合には、秘密鍵の紛失や流出にはくれぐれもご注意下さい。

今回の例では、に記述した「backend」がCanister名となりますので、以下のように呼び出すと良いでしょう。

1. Hello
dfx.json
Cargo.toml
backend.did
src/lib.rs
dfx.json