All Products
Search
Document Center

Blockchain as a Service:Deploy private smart contracts with geth

Last Updated:Jan 09, 2019
  1. Start an interactive JavaScript console to connect to a Quorum node, you can refer to the document Setup geth interactive JavaScript console to establish an interactive JavaScript console.

  2. Create an Ethereum account by entering the following instructions in the interactive JavaScript console. If you have already created an Ethereum account on your node, you can also use your existing account to complete the following steps.

    1. // create a account that use empty password
    2. var account = personal.newAccount("");
    3. // using an existing account in node
    4. // var account = eth.accounts[0]
    5. // unlock account for 300 seconds with empty password
    6. personal.unlockAccount(account, "", 300);
    7. // set it as default account
    8. web3.eth.defaultAccount = account;
  3. Use a thirdparty compilation tool such as solc or truffle to compile smart contracts and get the ABI and bytecode of the smart contracts. You can compile your contracts by referring to Compile smart contracts.

  4. You need to additionally specify the public key of the transaction manager that participates in each node of the private contract, which you can get from Node Configuration. Enter the following example command in the interactive console to deploy contracts in the quorum network. When you need to deploy your own contracts, you need to replace abi and bytecode with your contract compilation results.

    1. // tx-mangager public key list; only node in this list that can access private contract and it's data
    2. var privateFor = ["WLyCI0x1NBdy2VI4IdmxGYhRP7PKGL6wD87iN5vKp3w="];
    3. // abi for contract
    4. var abi = [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"initVal","type":"uint256"}],"payable":false,"type":"constructor"}];
    5. // compiled solidity source code using https://github.com/jpmorganchase/quorum-examples/blob/master/examples/7nodes/simplestorage.sol
    6. var bytecode = "0x6060604052341561000f57600080fd5b604051602080610149833981016040528080519060200190919050505b806000819055505b505b610104806100456000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632a1afcd914605157806360fe47b11460775780636d4ce63c146097575b600080fd5b3415605b57600080fd5b606160bd565b6040518082815260200191505060405180910390f35b3415608157600080fd5b6095600480803590602001909190505060c3565b005b341560a157600080fd5b60a760ce565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b6000805490505b905600a165627a7a72305820d5851baab720bba574474de3d09dbeaabc674a15f4dd93b974908476542c23f00029";
    7. var address = ""
    8. var simpleContract = web3.eth.contract(abi);
    9. var simple = simpleContract.new(42, {
    10. from: account,
    11. data: bytecode,
    12. gas: 0x47b760,
    13. privateFor: privateFor
    14. }, function(e, contract) {
    15. if (e) {
    16. console.log("err creating contract", e);
    17. } else {
    18. if (!contract.address) {
    19. console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
    20. } else {
    21. console.log("Contract mined! Address: " + contract.address);
    22. address = contract.address
    23. console.log(contract);
    24. }
    25. }
    26. });
  5. When a transaction deploying a contract is packaged by a consensus node, you can see the contract address after deployment in the interactive JavaScript console, and then you can send a private transaction to that address to invoke the private contract.