1

I have code working with Ethereum using nodejs and a send transaction. Is there an equivalent using eos? The basic task under ethereum is that A pays B with the server.js file making the transfer and the keys are stored in an .env variable, then web3 enables the txn.

The key line is in ethereum -

  web3js.eth.sendRawTransaction

I assume eos has some kind of equivalent payment? Any tips are welcome.

Here is my ethereum code (testnet) -

app.post("/api/makePayment",function(req,res){

   var payAmount = req.body.payAmount; //n//
   var payeeAccount = req.body.payeeAccount;  //n
   var freqPayment = req.body.freqPayment; //n

   var myAddress = "0xd3a20af365538c2090f7b81496a83365c2489b3b";

  localContractABI= [ { "constant": false, "inputs": [ { "name": "payeeWallet", "type": "address" } ], "name": "addPayeeWalle
t", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ 
{ "name": "payeeWallet", "type": "address" } ], "name": "payWallet", "outputs": [ { "name": "success", "type": "bool" } ], "p
ayable": true, "stateMutability": "payable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonp
ayable", "type": "constructor" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "addr
ess" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "t
ype": "address" } ], "name": "PayeeWallets", "outputs": [ { "name": "PayeeAllowed", "type": "bool" } ], "payable": false, "st
ateMutability": "view", "type": "function" } ]

   localContractAddress="0xe28497515f9a57b16b92102eaa0b407f7f20d6d4";

   //var infuraApiKey =process.env.INFURA_API_KEY;
  // var privateKey = process.env.PRIVATE_KEY;

   var web3js = new web3(new web3.providers.HttpProvider("http://178.128.172.167:8501"));

   web3js.eth.defaultAccount = myAddress;
   var privateKey=new Buffer(process.env.PAYMENT_PRIVATE_KEY, 'hex');

//   var toAddress = 'ADRESS_TO_SEND_TRANSACTION';

   //contract abi is the array that you can get from the ethereum wallet or etherscan
   var contractABI =localContractABI;
   var contractAddress =localContractAddress;
   //creating contract object
   var contract =  web3js.eth.contract(contractABI).at(contractAddress);
   var count;
   var nounce;
   var errcode="";

//   var apiKeyHash = web3js.sha3(authKey);

   var chainId = 1515;

   web3js.eth.getTransactionCount(myAddress, function(err, result) {
      nounce=result;
      var nounceHex = web3js.toHex(nounce);
      var rawTransaction = {
      "from":myAddress,
      "gasPrice":web3js.toHex(2*1e9),
      "gasLimit":web3js.toHex(90000),
      "to":payeeAccount,
      "value": web3js.toHex(web3js.toWei(payAmount, 'wei')), // must be in hex
      "chainId": web3js.toHex(chainId), // must be in hex
    //  "data":contract.payWallet.getData(payeeAccount),
      "nonce":nounceHex}

      var transaction = new Tx(rawTransaction);
      transaction.sign(privateKey);

      var serializedTx = transaction.serialize();
      web3js.eth.sendRawTransaction('0x'+serializedTx.toString('hex'), function(err1, hash) {
          if (!err1) {
             res.json({ message:hash});
          } else res.json({ message:err1});
      }); // raw
   }) //get

});


  • 1
    Did you check out eosjs? – TeeAttack42 Dec 7 '18 at 16:00
  • How is the permission handled? They have an example there - but what where is the key stored? It just says "active" under permission. – Trevor Oakley Dec 8 '18 at 8:33
1

Here's how I implemented eosjs in a NodeJS application. Node package used: "eosjs": "^16.0.6":

Link to version of my repo with this

const Eos = require('eosjs');
config = {
    chainId: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f', // 32 byte (64 char) hex string
    keyProvider: ['PRIVATE_KEY_HERE'], // WIF string or array of keys..
    httpEndpoint: 'http://127.0.0.1:8888',
    authorization: 'test@active'
};

const eos = Eos(config);
eos.contract('eosio', (error, eosio) => { // or .then(
    console.log(Object.keys(eosio))
    eosio.getplayer('test') //specifying action
})

I also have an example of integrating Scatter in the same application to handle keys: https://github.com/NatPDeveloper/eos-chess/blob/master/js/lib/scatter-js/scatter.js


I used eosjs's legacy docs for this which you can find here:

https://github.com/EOSIO/eosjs/tree/v16.0.9

Here, you hard code your private key. You could also store it in a .env file. If you want your contract to sign a transaction itself, you will need to add the eosio.code permission level to your account, more on this in the docs

Eos = require('eosjs')

// Default configuration
config = {
  chainId: null, // 32 byte (64 char) hex string
  keyProvider: ['PrivateKeys...'], // WIF string or array of keys..
  httpEndpoint: 'http://127.0.0.1:8888',
  expireInSeconds: 60,
  broadcast: true,
  verbose: false, // API activity
  sign: true
}

eos = Eos(config)

eos.transfer('alice', 'bob', '1.0000 SYS', '', options)

Adding @active per your comment:

If a keyProvider is not provided here, one may be provided on a per-action or per-transaction basis in Options.

options = {
  authorization: 'alice@active',
  broadcast: true,
  sign: true
}

eos.transfer('alice', 'bob', '1.0000 SYS', '', options)
  • authorization [array<auth>|auth] - identifies the signing account and permission typically in a multisig configuration. Authorization may be a string formatted as account@permission or an object<{actor: account, permission}>.

  • If missing default authorizations will be calculated.

  • If provided additional authorizations will not be added.

  • Performs deterministicsorting by account name

If a default authorization is calculated the action's 1st field must be an account_name. The account_name in the 1st field gets added as the active key authorization for the action.

  • broadcast [boolean=true] - post the transaction to the blockchain. Use false to obtain a fully signed transaction.

  • sign [boolean=true] - sign the transaction with a private key. Leaving a transaction unsigned avoids the need to provide a private key.

  • keyProvider [array<string>|string|function] - just like the global keyProvider except this provides a temporary key for a single action or transaction.

await eos.anyAction('args', {keyProvider})

await eos.transaction(tr => { tr.anyAction() }, {keyProvider})

https://github.com/EOSIO/eosjs/tree/v16.0.9#options

  • worked but I needed to add this in config - authorization: 'john11@active' - john11 is the account in eos. – Trevor Oakley Dec 10 '18 at 3:58
  • I've added more to my answer. If a key provider is not given, you can pass options which will allow you to specify the permission as you outlined. – Nat Dec 10 '18 at 12:01
  • I had some permission errors even when the key was given and the auth line solved it. – Trevor Oakley Dec 10 '18 at 13:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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