0

I'm new at Web3.js.

In geth, I need to unlock an account before I use this account to send some transactions. This does make sense.

But in web3.js, I don't know how to specify the account. According to the document, I tried to send a transaction as follows:

mycontract.methods.myfunc(params).send({from: pk})

Here I just specified the from address, and it worked. But I never unlock the account or sign this transaction. Who can tell me why..

  • If you pass account address from web3js and contract will recognize that address then Such transaction is raw transaction. Now if you want to sign such raw transaction then please see here exact answer what you need. ethereum.stackexchange.com/questions/36358/… – Gopal ojha Jul 7 '18 at 10:11
  • Thanks, but I still don't know why the raw transaction can be committed without signature? ( I tried this...only specified the account address but not signed it...and it worked....) – Yuan Rui Jul 7 '18 at 10:37
  • @Yun I have added in belo answer about your question , web3.js converts the JSON transaction {from:..., to:..., value:...} to the raw bytes and signs it for you automatically. – Gopal ojha Jul 22 '18 at 13:36
1

Signing a transaction means providing private key. In geth unlocking means making private key available for a transaction. So, you dont need to explicitly define private key.

0

Adding an information of @shrestha Bikesh, If you unlock the account means you have your own private key which was encrypted and you are decrypting it with your unlock password. I think you got it.
For information,

In testrpc 10 account address are given with private key separately. Here you don't need to unlock the account because this is already unlokced.   

lets come to geth now, When you unluck the account from geth and use that account address from web3js library then ya! you can do transaction successfully.

From Dapp you can use private key, you can sign transaction with given private key
web3.eth.accounts.signTransaction(tx, privateKey [, callback]);

tx - Object: The transaction object as follows:
        nonce - String: (optional) The nonce to use when signing this transaction. Default will use web3.eth.getTransactionCount().
        chainId - String: (optional) The chain id to use when signing this transaction. Default will use web3.eth.net.getId().
        to - String: (optional) The recevier of the transaction, can be empty when deploying a contract.
        data - String: (optional) The call data of the transaction, can be empty for simple value transfers.
        value - String: (optional) The value of the transaction in wei.
        gasPrice - String: (optional) The gas price set by this transaction, if empty, it will use web3.eth.gasPrice()
        gas - String: The gas provided by the transaction.

privateKey - String: The private key to sign with.
callback - Function: (optional) Optional callback, returns an error object as first parameter and the result as second.

Return as:

Promise returning Object: The signed data RLP encoded transaction, or if returnSignature is true the signature values as follows:

    messageHash - String: The hash of the given message.
    r - String: First 32 bytes of the signature
    s - String: Next 32 bytes of the signature
    v - String: Recovery value + 27
    rawTransaction - String: The RLP encoded transaction, ready to be send using web3.eth.sendSignedTransaction.

Here @eth defined as below

All transactions need to be signed. Otherwise they are considered invalid transactions and will not be included in the blockchain.

A raw transaction is a transaction in raw bytes. If one has the raw bytes of a valid transaction, they can use sendRawTransaction. Otherwise, web3.js creates the signed transaction's bytes for you automatically as part of sendTransaction(). web3.js converts the JSON transaction {from:..., to:..., value:...} to the raw bytes and signs it for you automatically.

I think now you came to conclusion,

Every transaction need to be signed.

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.