1

I have testnet public key like(0x3581cAe15410AB5673B6A58152Af9a081394Cf41) my usage of public key in modifier function ex:

pragma solidity ^0.4.24;

contract FirstContract {
address owner;

function FirstContract() public {    
     owner = msg.sender;
}

modifier ownerOnly()  {     
      require(msg.sender == owner);
      _;
}

function checkOwner(address ownerAddress) public view ownerOnly returns(string) {
      return("you are owner");
}
}

and what is the use of private key in solidity with example. Thanks!

  • Are you asking why you need a private key to interact with a contract like the one above? Or are you asking how to make a contract similar to the one above, but involving the private key directly in the code? – Shawn Tabrizi Jun 26 '18 at 20:13
1

When you execute a function in your smart contract you are executing a transaction. You sign this with your private key, the network verify that the signature corresponds with your address. After that, the network knows that you are "legit". If the verification fails your attempt to execute the function will fail. For this reason, the contract doesn't have to verify your signature (again) and it accepts that the person who executes the transaction is the holder of the correct private key.

Hope this helps

  • any code example? – Alex Surya Jun 26 '18 at 20:09
  • 1
    as I said you do not use your private key other than to sign the transactions. I recommend you this post about how transactions are build. – Jaime Jun 26 '18 at 20:17
0

Coding example, to your request in a comment on the previous answer (using web3 v1.0):

let fs = require("fs");
let Web3 = require("web3");
let web3 = new Web3(MY_ETHEREUM_NODE_ADDRESS);

async function send(transaction) {
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : await transaction.estimateGas()
    };
    let signedTransaction = await web3.eth.accounts.signTransaction(options, MY_PRIVATE_KEY);
    let receipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
    return receipt;
}

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync(contractName + ".abi").toString();
    let bin = fs.readFileSync(contractName + ".bin").toString();
    let contract = new web3.eth.Contract(JSON.parse(abi));
    let receipt = await send(contract.deploy({data: "0x" + bin, arguments: contractArgs}));
    console.log(`${contractName} contract deployed at address ${receipt.contractAddress}`);
    return new web3.eth.Contract(JSON.parse(abi), receipt.contractAddress);
}

async function run(arg1, arg2, arg3) {
    let myContract = await deploy("MyContract", [arg1, arg2]);
    let myRetVal = await send(myContract.methods.myFunc(arg3));
    ...
}

Side note #1:

You might wanna put some of those remote-procedure calls inside try/catch clauses.

Side note #2:

On a public network, you might wanna avoid waiting for the receipt returned from function sendSignedTransaction. Instead, you can pass several callbacks to this function, which will be invoked with a hash (immediately), with a receipt + confirmation (when the transaction is mined), and with 24 additional confirmations (one at a time).

See here for the official documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.