question archive class Ethereum {   constructor () {     this

class Ethereum {   constructor () {     this

Subject:Computer SciencePrice: Bought3

class Ethereum {
  constructor () {
    this.engine = new ProviderEngine();
    this.web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
    //this.web3 = new Web3(this.engine);
    //this.utils = new Utils();
    this.pendingTransactions = [];
    this.addWeb3Commands();
    this.initWeb3Provider();
    //convert the contracts into usable objects
    this.Agent = contract(agentJson);
    this.AgentRegistry = contract(agentRegistryJson);
    this.Relationship = contract(relationshipJson);


    //Provision the contract with a web3 provider
    this.Agent.setProvider(this.engine);
    this.AgentRegistry.setProvider(this.engine);
    this.Relationship.setProvider(this.engine);


    //the value stores access to privateKeys and addresses
    this.vault = null;


    //saves the user password so they don't have to enter it for every transaction
    this.pwDerivedKey = null;
  }


  waitForVault () {
    return new Promise((resolve, reject) => {
      let check = () => {
        if(this.vault && this.pwDerivedKey) {
          resolve(this.vault);
        }else {
          setTimeout(check, 200);
        }
      };
      check();
    });
  }


  waitForRPCConn () {
    return new Promise((resolve, reject) => {
      let timer;
      let check = () => {
        this.web3.eth.net.isListening().then(listening => {
          if(listening) {
            clearTimeout(timer);
            resolve();
          }
        }).catch(() => {});
      };
      timer = setInterval(check, 2000);
      check();
    });
  }


  //add some non standard web3 commands to the web3 api
  addWeb3Commands () {
    function toStringVal (val) {
      return String(val);
    }


    function toBoolVal (val) {
      if(String(val) == 'true') {
        return true;
      }
      return false;


    }


    function toIntVal (val) {
      return parseInt(val);
    }


    this.web3.extend({
      property: 'personal',
      methods: [new this.web3.extend.Method({
        name: 'unlockAccount',
        call: 'personal_unlockAccount',
        params: 3,
        inputFormatter: [this.web3.extend.utils.toAddress, toStringVal, toIntVal],
        outputFormatter: toBoolVal,
      })],
    });


    this.web3.extend({
      property: 'personal',
      methods: [new this.web3.extend.Method({
        name: 'importRawKey',
        call: 'personal_importRawKey',
        params: 2,
        inputFormatter: [this.web3.extend.utils.toAddress, toStringVal],
      })],
    });


    this.web3.extend({
      property: 'miner',
      methods: [new this.web3.extend.Method({
        name: 'start',
        call: 'miner_start',
        params: 0,
      })],
    });
  }


  //initialize the modular Web3 API provider
  initWeb3Provider () {
    //filters
    //this.engine.addProvider(new FilterSubprovider());


    //pending nonce
    this.engine.addProvider(new NonceSubprovider());


    //accounts management
    this.engine.addProvider(new HookedWalletSubprovider({
      getAccounts: (cb) => {
        this.getVault()
          .then(vault => {
            if(vault) {
              cb(null, vault.getAddresses());
            }else {
              cb('no keys available, login frst', null);
            }
          });
      },
      signMessage: (options, cb) => {
        this.getVault().then(vault => {
          var secretKey = vault.exportPrivateKey(options.from, this.pwDerivedKey);
          var msg = new Buffer(options.data.replace('0x', ''), 'hex');


          let msgHash = Utils.hashPersonalMessage(msg);
          let sgn = Utils.ecsign(msgHash, new Buffer(secretKey, 'hex'));


          let signedMsgHex = Utils.bufferToHex(Buffer.concat([
            Utils.setLengthLeft(sgn.r, 32),
            Utils.setLengthLeft(sgn.s, 32),
            Utils.toBuffer(sgn.v),
          ]));


          //Utils.toRpcSig(sgn.v, sgn.r, sgn.s);
          cb(null, signedMsgHex);
        });
      },
      signTransaction: (txObject, cb) => {
        this.getVault().then(vault => {
          txObject.gas = '0x1000000';
          txObject.gasLimit = '0x1000000';


          let tx = new Transaction(txObject);
          let rawTX = tx.serialize().toString('hex');


          let signedTx = '0x' + signing.signTx(
    }
}

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Related Questions