简体   繁体   English

Web3:访问 MetaMask 钱包中的私钥

[英]Web3: accessing private key in MetaMask wallet

I have a simple Dapp and I want to sign a transaction but I don't have the private key as a string.我有一个简单的 Dapp,我想签署一笔交易,但我没有私钥作为字符串。

The user is using a MetaMask wallet.用户正在使用 MetaMask 钱包。 After they grant web3 access to their account, how can I access the private key to sign a transaction?在他们授予 web3 对其帐户的访问权限后,我如何访问私钥来签署交易?

const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY);
// PRIVATE_KEY is what I'm trying to get.

Metamask doesn't share the private key directly for security reasons.出于安全原因,Metamask 不直接共享私钥。 And sometimes it doesn't even have the key - for instance when the user is using the Metamask UI to operate a hardware wallet account.有时它甚至没有密钥——例如当用户使用 Metamask UI 操作硬件钱包帐户时。

You'll need to construct the transaction object and pass it to the ethereum.request() method.您需要构建事务 object 并将其传递给ethereum.request()方法。 This will open the Metamask window where the user can sign or decline the transaction request.这将打开 Metamask window,用户可以在其中签署或拒绝交易请求。

Code example is pretty straightforward and is in the linked documentation.代码示例非常简单,并且在链接的文档中。

Here's an example of how you would sign your Metamask transaction:以下是如何签署 Metamask 交易的示例:

export const mintNFT = async(url, name, description) => {
    
    //error handling
    if (url.trim() == "" || (name.trim() == "" || description.trim() == "")) { 
        return {
            success: false,
            status: "❗Please make sure all fields are completed before minting.",
        }
    }
  
    //make metadata
    const metadata = new Object();
    metadata.name = name;
    metadata.image = url;
    metadata.description = description;

    //pinata pin request
    const pinataResponse = await pinJSONToIPFS(metadata);
    if (!pinataResponse.success) {
        return {
            success: false,
            status: "😢 Something went wrong while uploading your tokenURI.",
        }
    } 
    const tokenURI = pinataResponse.pinataUrl;  

    //load smart contract
    window.contract = await new web3.eth.Contract(contractABI, contractAddress);//loadContract();

    //set up your Ethereum transaction
    const transactionParameters = {
        to: contractAddress, // Required except during contract publications.
        from: window.ethereum.selectedAddress, // must match user's active address.
        'data': window.contract.methods.mintNFT(window.ethereum.selectedAddress, tokenURI).encodeABI() //make call to NFT smart contract 
    };
  
    //sign transaction via Metamask
    try {
        const txHash = await window.ethereum
            .request({
                method: 'eth_sendTransaction',
                params: [transactionParameters],
            });
        return {
            success: true,
            status: "✅ Check out your transaction on Etherscan: https://ropsten.etherscan.io/tx/" + txHash
        }
    } catch (error) {
        return {
            success: false,
            status: "😥 Something went wrong: " + error.message
        }
    }
}

In this example, we're signing a transaction to mint an NFT.在此示例中,我们正在签署交易以铸造 NFT。 You can check out more details here: https://docs.alchemyapi.io/alchemy/tutorials/nft-minter#step-8-implement-the-mintnft-function您可以在此处查看更多详细信息: https://docs.alchemyapi.io/alchemy/tutorials/nft-minter#step-8-implement-the-mintnft-function

Best of luck: :)祝你好运::)

MetaMask does not give you access to a private key and never will. MetaMask 不会让您访问私钥,而且永远不会。

The whole point of the wallet is to protect the user against malicious Dapps.钱包的全部意义在于保护用户免受恶意 Dapps 的侵害。

it is possible to obtain private key from your 12 words passphrase, inserting the 12 words passphrase in the field "BIP39 Mnemonic" of site https://iancoleman.io/bip39/ you'll obtain every private and public key of all your accounts under the passphrase you inserted可以从您的 12 字密码短语中获取私钥,将 12 字密码短语插入站点https://iancoleman.io/bip39/的“BIP39 助记符”字段中,您将获得所有帐户的每个私钥和公钥在您插入的密码下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM