ethereum / solc-js

Javascript bindings for the Solidity compiler
https://soliditylang.org
MIT License
1.45k stars 465 forks source link

Solc module not found errors after npm installing solc #135

Closed sivachaitanya closed 6 years ago

sivachaitanya commented 7 years ago

I have installed solc using "npm install solc" in my project directory, however after refreshing my webpage I see a bunch of Module not found related errors,following is my web3js code

ecrecTestSource = "big string of my contract"; ecrecTestCompiled = solc.compile(ecrecTestSource, 1);

    ecrecTestCode = ecrecTestCompiled.contracts['EcrecTest'].bytecode;
    ecrecTestABI =  JSON.parse(ecrecTestCompiled.contracts['EcrecTest'].interface);
    ecrecTestContract = web3.eth.contract(ecrecTestABI);
    deployedecrecTest = ecrecTestContract.at(contractAddress);

The following errors are seen on my webpage, can somebody please help me resolve this issue

chrome_2017-08-19_21-34-58

axic commented 7 years ago

Would you mind sharing your code? It seems you are not loading the code correctly.

sivachaitanya commented 7 years ago
import "../stylesheets/app.css";
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract';
 const util = require('ethereumjs-util');
 var HookedWeb3Provider = require("hooked-web3-provider");
 var lightwallet = require('eth-lightwallet');
 var signing = require('eth-lightwallet/lib/signing');
 var solc = require('solc');
 const fs = require('fs');
 var keystore= lightwallet.keystore;
 var ks_temp;
 var seed;
 var pvtkey;
 var pwDerivedKey_store;
var accounts_node;
var account;
var address = "0x692a70d2e424a56d2c6c27aa97d1a86395877b3a";
var web3;
var ecrecTestABI;
var ecrecTestContract;
var ecrecTestCode;
var deployedecrecTest;
var org_index;
window.App = {
  start: function() {
    var self = this;
    web3.eth.getAccounts(function(err, accs) {
      if (err != null) {
        alert("There was an error fetching your accounts.");
        return;
      }
      if (accs.length == 0) {
        alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
        return;
      }
      console.log(accs);
      accounts_node = accs;
      account = accounts_node[0];
      web3.eth.defaultAccount= account;
      var ecrecTestSource = 'pragma solidity ^0.4.6; contract EcrecTest{ struct Attestation{ string filename; bytes32 hash; string tag; string institution_name; string additional_data; bytes32 signature; } struct Organisation{ string org_name; address org_address; bool is_agency; bytes32 signature; mapping(uint=>Attestation) attestations; uint number_attestations; } mapping(uint=>Organisation) public organisations; uint public number_organisations = 0; function verify( bytes32 hash, uint8 v, bytes32 r, bytes32 s) constant returns(address) { bytes memory prefix = "\x19Ethereum Signed Message:\n32"; bytes32 prefixedHash = sha3(prefix, hash); return ecrecover(prefixedHash, v, r, s); } function register_org(string org_name, address org_address, bool is_agency, bytes32 signature) returns(uint) { organisations[number_organisations] = Organisation({ org_name: org_name, org_address: org_address, is_agency: is_agency, signature: signature, number_attestations: 0 }); number_organisations = number_organisations + 1; return number_organisations -1 ; } function get_organisation_details(uint index) constant returns (string org_name, address org_address, bool is_agency, bytes32 signature) { if(index >= number_organisations) { throw; } Organisation organisation = organisations[index]; org_name = organisation.org_name; org_address = organisation.org_address; is_agency = organisation.is_agency; signature = organisation.signature; } function get_number_organisations() constant returns (uint number_orgs) { number_orgs = number_organisations; } function attest(uint org_index, string filename, bytes32 hash, string tag, string institution_name, string additional_data, bytes32 signature){ if(org_index >= number_organisations){ throw; } Organisation organisation = organisations[org_index]; uint number_attestations = organisation.number_attestations; organisation.attestations[number_attestations] = Attestation({ filename: filename, hash: hash, tag: tag, institution_name: institution_name, additional_data: additional_data, signature: signature }); organisation.number_attestations = organisation.number_attestations + 1; } function get_number_attestations(uint org_index) constant returns (uint number_attestations) { if(org_index >= number_organisations) { throw; } Organisation organisation = organisations[org_index]; number_attestations = organisation.number_attestations; } function get_attestation_details(uint org_index, uint attestation_index) constant returns (string filename, bytes32 hash, string tag, string institution_name, string additional_data, bytes32 signature) { if(org_index >= number_organisations) { throw; } Organisation organisation = organisations[org_index]; uint number_attestations = organisation.number_attestations; if(attestation_index >= number_attestations) { throw; } Attestation attestation = organisation.attestations[attestation_index]; filename = attestation.filename; hash = attestation.hash; tag = attestation.tag; institution_name = attestation.institution_name; additional_data = attestation.additional_data; signature = attestation.signature; } }';

      ecrecTestCompiled  = solc.compile(ecrecTestSource, 1);
      solc.loadRemoteVersion('latest', function (err, solcSnapshot) {
          if (err) {
            console.log('Solc snapshot error - '+err);
          }
           ecrecTestCompiled = solcSnapshot.compile(ecrecTestSource, 1)
        })

        ecrecTestCode = ecrecTestCompiled.contracts['EcrecTest'].bytecode;
        ecrecTestABI =  JSON.parse(ecrecTestCompiled.contracts['EcrecTest'].interface);
        ecrecTestContract = web3.eth.contract(ecrecTestABI);
        deployedecrecTest = ecrecTestContract.at(contractAddress);

       const contractInstance = deployedecrecTest.new({
              data: '0x' + ecrecTestCode,
              from: web3.eth.accounts[0],
              gas: 90000*2
          }, (err, res) => {
              if (err) {
                  console.log(err);
                  return;
              }
              console.log('Tx hash - '+res.transactionHash);

              // If we have an address property, the contract was deployed
              if (res.address) {
                  console.log('Contract address: ' + res.address);
                  // Let's test the deployed contract
                  contractAddress = res.address;
              }
          });

       $("#acaddress").val(account);
       console.log('On load Keystore addresses - '+keystore.getAddresses);

  });
  },
    createWallet: function()
  {
        var password = document.getElementById("password").value;
        var msgResult;

        if (!password) {
        $("#password").val("PASSWORD NEEDED").css("color", "red");
        $("#password").click(function() { 
          $("#password").val("").css("color", "black"); 
        });
        } else {

    var secretSeed = lightwallet.keystore.generateRandomSeed();
   seed = secretSeed;
  $("#seed").html(secretSeed);

  // the seed is stored encrypted by a user-defined password
var password = prompt('Enter password for encryption', 'password');

keystore.createVault({
  password: password,
   seedPhrase: secretSeed, // Optionally provide a 12-word seed phrase
  // salt: fixture.salt,     // Optionally provide a salt.
                             // A unique salt will be generated otherwise.
  // hdPathString: hdPath    // Optional custom HD Path String
}, function (err, ks) {

  // Some methods will require providing the `pwDerivedKey`,
  // Allowing you to only decrypt private keys on an as-needed basis.
  // You can generate that value with this convenient method:
  ks.keyFromPassword(password, function (err, pwDerivedKey) {
           if (err) throw err;

          // generate five new address/private key pairs
          // the corresponding private keys are also encrypted
          ks = new lightwallet.keystore(seed, pwDerivedKey);
        console.log('Ks at create wallet is -'+ks);
          ks.generateNewAddress(pwDerivedKey, 5);
          var address = ks.getAddresses()[0];

            console.log("createWallet");
          pwDerivedKey_store = pwDerivedKey;
          console.log('PWDerived Key -'+pwDerivedKey);

          var privateKey = ks.exportPrivateKey(address, pwDerivedKey);
          pvtkey = privateKey;

          console.log(address);

          $("#acaddress").val("0x"+address);
          $("#acadress_attest").val("0x"+address);
          account = "0x"+address;
          $("#acaddress_attest").val("0x"+address);
          $("#privateKey").val(privateKey);

          //$("#balance").html(getBalance(address));
        keystore = ks;
          // Now set ks as transaction_signer in the hooked web3 provider
          console.log("switchToHooked3");

        var web3Provider = new HookedWeb3Provider({
          host: "http://localhost:8545", // check what using in truffle.js
          transaction_signer: keystore
        });

        newprov= web3.setProvider(web3Provider);
        window.web3 = newprov;

          // and you can start using web3 using the keys/addresses in ks!
        });
      });

   }
  },
   getBalance: function()
  {

        var password = document.getElementById("password").value;
        var bal= web3.fromWei(web3.eth.getBalance(address).toNumber(), 'ether');
        $("#balance").html(bal);
  }, 

  fundEth: function() {

      console.log("fundEth");

      var fromAddr = account; // default owner address of client
      var toAddr = address;
      var valueEth = 100;
      var value = parseFloat(valueEth)*1.0e18;
      var gasPrice = 1000000000000;
      var gas = 50000;
      web3.eth.sendTransaction({from: fromAddr, to: toAddr, value: value}, function (err, txhash) {
        if (err) console.log('ERROR: ' + err)
        console.log('txhash: ' + txhash + " (" + amt + " in ETH sent)");
        $("#balance").html(getBalance());
      });
  },

  verifyUser: function()
  {

    var message = document.getElementById("message").value;
    var givenuseraddr = document.getElementById("acaddress").value;

    // client side code
     var msg = web3.sha3(message) ;
     console.log('SHA3 - '+msg);
     var sig = web3.eth.sign(web3.eth.accounts[0], msg); 
     var r = sig.substr(0,66) ;
     var s = "0x" + sig.substr(66,64) ;
     var v = 28 ;
      console.log('V,R,S at client -'+v,r,s);

    // Quorum recover address at server side 
     var res = util.fromRpcSig(sig);
     console.log('V at server'+res.v);
     console.log('R at server'+util.bufferToHex( res.r));
     console.log('S at server'+util.bufferToHex(res.s));

    deployedecrecTest.verify.call(msg,res.v,util.bufferToHex(res.r),util.bufferToHex(res.s),function (error, data){
      console.log('Address from Quorum - '+data);
       if(data == givenuseraddr){
      $("#registrationresult").html('<font  color="green">Onboarding Successsful</font>');
      // if the onboarding is successfull, register the organisation on blockchain

       EcrecTest.register_org("Test Organisation Name",data,false,sig,{from: web3.eth.accounts[0]}, (err, res) => {

           org_index = res;
          console.log('If error -'+err);
          console.log('Index is - '+res);
           $('#org_index').html(res);
      });
      }
      if(data != givenuseraddr){
        $("#registrationresult").html('<font  color="red">Onboarding Failed</font>');
      }

    })

  }
};

window.addEventListener('load', function() {

    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  App.start();
});
captDaylight commented 6 years ago

I've installed solc with npm i -S solc and then tried importing it like: import solc from 'solc'. I'm getting the same error thrown in solc/wrapper.js. In fact it's happening because of this line here.

In the README I see this:

version strings must match the version substring of the files available in /bin/soljson-*.js. See below for an example.

What's happened to that file? It's not in the repo, I'm missing something :)

axic commented 6 years ago

@sivachaitanya closing this in favour of #143 which has a bit more detail regarding the same issue.

axic commented 6 years ago

Possibly this is also the same issue as #110 ?

goburhan commented 3 years ago

you should try command : npm install --save solc to get solidity compiler