首页 > 解决方案 > 它如何管理私钥松露?

问题描述

我正在尝试在部署在 ropsten 中的 mi 自定义合同中发送令牌。另外,我正在使用 truffle 和 truffle-contract 1.1.11 库。我的疑问是,我应该如何在松露环境中签署交易?

另一方面,如果可能的话,我想知道 truffle 是如何管理私钥的,因为当我在本地 ganache 区块链上设置项目时,我所有的东西都可以工作。当我尝试从与合约部署地址不同的另一个地址签署交易时,它只是神奇的访客私钥。这当然是在 ganache 中,但问题在于 ropsten。


pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol";

contract CustomToken is ERC20Pausable, ERC20Burnable, ERC20Mintable, ERC20Detailed {

    constructor () public ERC20Detailed("CustomToken", "CT", 2) { }

    // some functions who call super.function(), this reproduces default behavior of a base ERC20 token

}


const contract = require('truffle-contract');

const customtoken_artifact = require('../build/contracts/CustomToken.json');
let CustomToken = contract(customtoken_artifact);

module.exports = {

    sendCoin: function(amount, sender, receiver, callback) {
        let self = this;

        CustomToken.setProvider(self.web3.currentProvider);

        let custom;

        CustomToken.deployed().then(function(instance) {

            custom = instance;

            return custom.transfer(receiver, amount, {from: sender});

        }).then(() => callback("202"))
            .catch(function(e) {
                console.log(e);
                callback("400 " + e);
            });
    },
};

最后,我希望知道 truffle 如何管理私钥或何时签署交易。

标签: solidityweb3truffle

解决方案


要在 Truffle 中签署交易,您可以使用HDWalletProvider

https://www.npmjs.com/package/@truffle/hdwallet-provider

你可以在你的truffle-config.js


推荐阅读