1

My Solidity contract has a function like so function newOrg(bytes32 orgId, bytes32[] members, bytes32 head) public payable. In my javascript file, I have this:

var Web3 = require('web3');
var fs = require('fs');
var path = require('path');
//
var ethereumHttpEndpoint = "http://52.x.x.x:8545";
var contractHome = "poc";
var contractName = "myContr";
var contractAddress = 0x3772385d8649.......e12e2ac2694653f79fc;
//
var abiFilePath = path.join(contractHome, 'bin', 'sol', contractName) + '.abi';
var bytecodeFilePath = path.join(contractHome, 'bin', 'sol', contractName) + '.bin';
//
var ethHttpProvider = new Web3.providers.HttpProvider(ethereumHttpEndpoint);
var web3 = new Web3(ethHttpProvider);
//
var abiFile = fs.readFileSync(abiFilePath).toString();
var abiDef = JSON.parse(abiFile);
var byteCode = fs.readFileSync(bytecodeFilePath).toString();
//
var accountAddr = '0xb16ee696fcc86065.......77d67fca2df1b2';
var myContrContract = web3.eth.contract(abiDef);
var contractInstance = myContrContract.at(contractAddress);
var orgId = '0x4100000000000000000000000000000000000000000000000000000000000000';
var members = [
    '0x4200000000000000000000000000000000000000000000000000000000000000',
    '0x4300000000000000000000000000000000000000000000000000000000000000',
    '0x4400000000000000000000000000000000000000000000000000000000000000'
];
var head = '0x4500000000000000000000000000000000000000000000000000000000000000';
//
web3.personal.unlockAccount(accountAddr, "pwd", 600, (err,res) => {
    if (err) {
        console.log("Unlock account error");
        console.log(err);
    } else {
        console.log("Account unlocked");
        web3.eth.defaultAccount = accountAddr;
        contractInstance.newOrg.sendTransaction(orgId, members, head,
            function(err,res) {
                if (err) {
                    console.log("newOrg sendTransaction failed.");
                    console.log(err);
                } else {
                    console.log("newOrg added");
                }
            }
        );
    }
});

When I run this, I get this:

Account unlocked
/Users/nsubrahm/node_modules/web3/lib/web3/formatters.js:274
    throw new Error('invalid address');
    ^

Error: invalid address
    at inputAddressFormatter (/Users/nsubrahm/node_modules/web3/lib/web3/formatters.js:274:11)
    at inputTransactionFormatter (/Users/nsubrahm/node_modules/web3/lib/web3/formatters.js:103:22)
    at /Users/nsubrahm/node_modules/web3/lib/web3/method.js:89:28
    at Array.map (<anonymous>)
    at Method.formatInput (/Users/nsubrahm/node_modules/web3/lib/web3/method.js:88:32)
    at Method.toPayload (/Users/nsubrahm/node_modules/web3/lib/web3/method.js:114:23)
    at Eth.send [as sendTransaction] (/Users/nsubrahm/node_modules/web3/lib/web3/method.js:139:30)
    at SolidityFunction.sendTransaction (/Users/nsubrahm/node_modules/web3/lib/web3/function.js:173:15)
    at Object.web3.personal.unlockAccount [as callback] (/Users/nsubrahm/GitHub/poc/js/new-consortium.js:38:40)
    at /Users/nsubrahm/node_modules/web3/lib/web3/method.js:142:25

In the array, I want to pass a byte array. Why is it getting interpreted as address?

UPDATE

Digging some more, the error comes from an invalid address in the input to sendTransaction. This is raised by inputTransactionFormatter in formatters.js where, line number 103 checks for to address. I did try sendTransaction with a {from:accountAddr} - leaving out to because web3.eth.sendTransaction has to as optional. Same error.

Why is there a check for options.to?

/**
 * Formats the input of a transaction and converts all values to HEX
 *
 * @method inputTransactionFormatter
 * @param {Object} transaction options
 * @returns object
*/
var inputTransactionFormatter = function (options){

    options.from = options.from || config.defaultAccount;
    options.from = inputAddressFormatter(options.from);

    if (options.to) { // it might be contract creation
        options.to = inputAddressFormatter(options.to); //line 103
    }

    ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) {
        return options[key] !== undefined;
    }).forEach(function(key){
        options[key] = utils.fromDecimal(options[key]);
    });

    return options;
};

2 Answers 2

0

Are you not taking this

var accountAddr = '0xb16ee696fcc86065.......77d67fca2df1b2';

and plugging into this

web3.personal.unlockAccount(accountAddr, "pwd", 600, (err,res) => {

and getting this?

throw new Error('invalid address');

I imagine it's obfuscated for our benefit (not really needed), but the issue is still simply the input itself.

Instead of hard-coding the account, you might consider using

var account;
web3.eth.getAccounts(function(err, accounts) { 
  account = accounts[0]; // the first one
}

Hope it helps.

1
  • I thought so too. But I can see console output for account gett8ng unlocked. Hence the confusion. BTW the account does get unlocked successfully from geth console. Aug 19, 2018 at 3:46
0

The var contractAddr should have its value in double quotes. That fixed the issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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