uport-project / ethr-did

Create ethr DIDs
Apache License 2.0
259 stars 53 forks source link

Can i use the privatekey to call the methods of the contract ? #42

Closed relaxbao closed 4 years ago

relaxbao commented 4 years ago

ethjs-contract

Issue Type

Description

Hi,i had searched and tried for a long time , i really really want get help.

i want to call my contract with my local account or privatekey, is there some fuction i can use to do this ?

for example :

pragma solidity ^0.4.4;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public returns (address){
        storedData = x;
        return msg.sender;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

i can get call the methods this way

DidReg = new EthContract(provider)(abi)
registry = DidReg.at(registryAddress)
const hash = registry.set(10)

but in this way , the msg.sender is the eth.account[0], i want to use my local account.

i tried the addWallet ,but it doesn't work

web3.eth.accounts.wallet.add(account)

so Can i use the privatekey to call the methods of the contract with ethjs-contract?

mirceanis commented 4 years ago

Hi, can you share some sample-code that shows the problem you're facing? It is unclear which contract you want to call. From your code above it seems like you are trying to call set(10) on the did-registry contract, and I can only assume you mean the ethr-did-registry. That contract does not have a set() method so it would be impossible to call.

mirceanis commented 4 years ago

To use a private key with ethjs it would seem that you need a provider that can sign using a key. In that ecosystem, https://github.com/ethjs/ethjs-provider-signer seems to be needed.

The use of ethjs is out of the scope of this library. If I have misunderstood your question, please rephrase or share more details about how this library is causing issues.

mehranshakeri commented 4 years ago

Thanks, ethjs-provider-signer must be used per DID creation. This code worked for me:

const SignerProvider = require('ethjs-provider-signer');
const sign = require('ethjs-signer').sign;

  const didOwner = web3.eth.accounts.create();

  const provider = new SignerProvider('http://localhost:8545', {
    signTransaction: (rawTx, cb) => cb(null, sign(rawTx, didOwner.privateKey)),
  });

  const ethrDid = new EthrDid({
    address: didOwner.address,
    provider: provider});
mirceanis commented 4 years ago

I believe @mehranshakeri answered the query. I'm closing this, reopen if there is still an issue regarding this