0

I have already run this code few weeks back and back then it was working perfectly but now it's giving me an error:

UnhandledPromiseRejectionWarning: Error: account names can only be 12 chars long

I guess I haven't even change the code, but this error is coming, please let me know if I have made any mistake. Also, I'm giving the name and public key of a new account through Postman and I have also printed it in console, it's coming without any mistakes.

Code:

const express = require('express');
const path= require('path');
const app =express();

const { Api, JsonRpc, RpcError } = require('eosjs');
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig');
const fetch = require('node-fetch');   
const defaultPrivateKey = "creator account private key"; // bob
const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);

const rpc = new JsonRpc('http://jungle2.cryptolions.io:80', { fetch });

const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

//Body parser Middleware
app.use(express.json());
app.use(express.urlencoded({extended:false}));      



app.post('/', (req,res) => {
            
  var existingaccount = 'microfinanc1'
  var mynewaccount=req.body.eosname;
  var  pubkey=req.body.publickey;

  console.log('Is it working');
  console.log(pubkey);
  console.log(mynewaccount);


  (async () => {
    await api.transact({
      actions: [{
        account: 'eosio',
        name: 'newaccount',
        authorization: [{
          actor: existingaccount,
          permission: 'active',
        }],
        data: {
          creator: existingaccount,
          name: mynewaccount,
          owner: {
            threshold: 1,
            keys: [{
              key: pubkey,
              weight: 1
            }],
            accounts: [],
            waits: []
          },
          active: {
            threshold: 1,
            keys: [{
              key: pubkey,
              weight: 1
            }],
            accounts: [],
            waits: []
          },
        },
      },
      {
        account: 'eosio',
        name: 'buyrambytes',
        authorization: [{
          actor: existingaccount,
          permission: 'active',
        }],
        data: {
          payer: existingaccount,
          receiver: mynewaccount,
          bytes: 8192,
        },
      },
      {
        account: 'eosio',
        name: 'delegatebw',
        authorization: [{
          actor: existingaccount,
          permission: 'active',
        }],
        data: {
          from: existingaccount,
          receiver: mynewaccount,
          stake_net_quantity: '1.0000 EOS',
          stake_cpu_quantity: '1.0000 EOS',
          transfer: false,
        }
      }]
    }, {
      blocksBehind: 3,
      expireSeconds: 30,
    });
  })
  
  ();


  
});

const port = process.env.port || 5000;
app.listen(port ,()=> console.log(`server got started on port ${port} `));

Error in console:

guru@ubuntu:~/nodejs/express js$ node index.js
server got started on port 5000 
Is it working
EOS5nktguDXboGwb2YfXu6AW7bgZ2eCd1LnEie8ogJYgDyiHScDw6
 guru12341111
(node:12713) UnhandledPromiseRejectionWarning: Error: account names can only be 12 chars long
    at new RpcError (/home/guru/nodejs/express js/node_modules/eosjs/dist/eosjs-rpcerror.js:26:28)
    at JsonRpc.<anonymous> (/home/guru/nodejs/express js/node_modules/eosjs/dist/eosjs-jsonrpc.js:118:35)
    at step (/home/guru/nodejs/express js/node_modules/eosjs/dist/eosjs-jsonrpc.js:36:23)
    at Object.next (/home/guru/nodejs/express js/node_modules/eosjs/dist/eosjs-jsonrpc.js:17:53)
    at fulfilled (/home/guru/nodejs/express js/node_modules/eosjs/dist/eosjs-jsonrpc.js:8:58)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
(node:12713) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12713) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 Answer 1

0

Well, the error message implies the account name you provided is not 12 characters, and it does seem like the account name that was sent to the server has a preceding whitespace character.

Your Answer

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

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