Metamaskに接続

Chat GPTへのお願いの仕方(プロンプトエンジニアリング)がうまくなるため、投げ込んだお願い事をメモしていってます。

目的

Metamaskに接続してパブリックアドレスを取得してくれるWebページをChatGPTに作成依頼。

ポイント

特になし!一発で取得できました。

Chat GPTに投げかけた言葉

最初のお願い

以下のプログラムを作ってください。

プログラム: """
* CGIとPythonで作る。
* ユーザーがアクセスするページはindex.cgi
* そのページには、テキストボックスとボタンがある。
* ボタンの名前は「get public address」
* ボタンを押すと、metamaskの拡張機能に接続し、パブリックアドレスの取得を依頼する
* ユーザーがMetamaskの承認ボタンを押したら、パブリックアドレスを取得し、テキストボックスに表示する
"""


作ってくれたプログラム

2つあります。

<index.cgi>

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cgi
import cgitb
cgitb.enable()

print("Content-Type: text/html; charset=utf-8")
print("")

html = '''
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Metamask Public Address</title>
    <script src="https://cdn.jsdelivr.net/npm/web3@1.6.0/dist/web3.min.js"></script>
    <script src="metamask.js"></script>
</head>
<body>
    <form>
        <input type="text" id="public_address">
        <input type="button" value="get public address" onclick="getPublicAddress()">
    </form>
</body>
</html>
'''

print(html)

<metamask.js>

async function getPublicAddress() {
    if (typeof window.ethereum !== 'undefined') {
        const web3 = new Web3(window.ethereum);
        try {
            await window.ethereum.request({ method: 'eth_requestAccounts' });
            const accounts = await web3.eth.getAccounts();
            const publicAddress = accounts[0];
            document.getElementById("public_address").value = publicAddress; // 変数名を修正
        } catch (error) {
            console.error(error);
        }
    } else {
        alert("Metamask extension not detected. Please install it and try again.");
    }
}


この記事が気に入ったらサポートをしてみませんか?