0

I am doing raw transaction to send token from one account to another. But I don't have private key of my account but i do have the password. How can I send raw transaction using my password only. Here is my code:

var Tx = require('ethereumjs-tx')
var Web3 = require('web3');
var web3 = new Web3(new 
Web3.providers.HttpProvider('https://rinkeby.infura.io/infurakey'));
var Personal = require('web3-eth-personal')
var personal = new Personal(Personal.givenProvider)

var ABI =[{"constant":true,......","type":"event"}]

var contract =new web3.eth.Contract(ABI, '0x9...5bf3f4f')

const account1 = "0xd94acc...5071240364364c86f45";
const account2 = "0xf7689d04139...ad668CBF2Af2D29";

web3.eth.getTransactionCount(account1,(err, txCount)=>{
const txObject ={
from: account1, 
nonce : web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(800000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
to: '0x9...a42255bf3f4f',
data: contract.methods.transfer(account2, 1000).encodeABI()
 }
 var password = new Buffer("mypassword", 'hex');
 const tx = new Tx(txObject)

tx.sign(password);
const serializedTransaction = tx.serialize();

const raw = '0x' + serializedTransaction.toString('hex')
web3.eth.sendSignedTransaction(raw, (err, txHash)=> {
    console.log('err:', err, 'txHash:', txHash)
  })
})

I am getting error:

RangeError: private key length is invalid

crypto S.
  • 345
  • 8
  • 17

1 Answers1

1

Ethereum does not understand your password and cannot use it. Your password is only used within your client - it's typically used to encrypt the file which contains your private key. Password is therefore not directly linked to the private key in any way.

So whenever you want to use your private key the process goes something like this (at least in Geth, probably similar in other clients):

1) You unlock your account with password

2) Your client decrypts the file on-the-fly with your private key with the given password- The file itself remains always encrypted - the private key is only decrypted in memory.

3) Your client uses the private key to sign a transaction

So you also need access to the (enrypted) private key file. The password is useless by itself.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • So, What can I do to get my private key?@Lauri Peltonen – crypto S. Feb 01 '19 at 13:05
  • You need the private key file or a seed phrase which was possibly used to generate the private key. Depends on your settings. Some more info: https://ethereum.stackexchange.com/questions/12830/how-to-get-private-key-from-account-address-and-password – Lauri Peltonen Feb 01 '19 at 13:09