0

Like BTC, ETH etc has address, do EOS has address or username etc, I am using below code to generate the public and private key, we do transaction in bitcoin,ethereum etc with a unique address sample address: mkhc2dpxmAMBqwN36TD2upqZL98v9AgP5r how to generate address/username for EOS in nodejs?

const hdkey = require('hdkey')
const wif = require('wif')
const ecc = require('eosjs-ecc')
const bip39 = require('bip39')

const mnemonic = 'butter decrease'
const seed = bip39.mnemonicToSeed(mnemonic).toString()
const master = hdkey.fromMasterSeed(Buffer(seed, 'hex'))
const node = master.derive("m/44/194/0/0/13")

console.log("publicKey: "+ecc.PublicKey(node._publicKey).toString())
console.log("privateKey: "+wif.encode(128, node._privateKey, false))
console.log("chainCode: "+wif.encode(239, node.chainCode, false))

publicKey: EOS5KvrNrXQM96jWaQmJW6QYCU28Q6p7QktcQzSCrKHe68bxXxk4R
privateKey: 5JBNSUbD4anpFFe3CbURTBtbpbUGLGtVUatDd1uL1i1HbjZjqGd
chainCode: 91wwTYUhb5szXqhwHLKgNgZ9t89Cn1DraPRnfF1FipnDjKRJWZQ
0

In EOS the account name (address) is a separate concept from the pub/priv keys. In EOS an account can have multiple keys associate with it, by default there is an Owner and Active key that all accounts need when they are created. Additionally the keys can be changed if the user suspects they have been compromised.

Standard account names are 12 digits and only the following characters are allowed:

'.12345abcdefghijklmnopqrstuvwxyz'

This is done so that all account names can be encoded into a uint64_t If you wanted to tie the public key into the account name, one idea would be to take the first 8 bytes of the pub key buffer and use that as your account name.

You can look at decodeName and decodeNameHex methods from eosjs format.test as an example for converting a 64 byte value into an EOS account name

==================

Update Answer

To create an Account requires three actions:

  1. newaccount
  2. buyrambytes
  3. delegatebw

Below is an example using eosjs

    const wif = '5JBNSUbD4anpFFe3CbURTBtbpbUGLGtVUatDd1uL1i1HbjZjqGd';

    const fetch = require('node-fetch');
    const { TextDecoder, TextEncoder } = require('util');
    const { Api, JsonRpc } = require('eosjs');

    const rpc = new JsonRpc('https://eos.greymass.com', { fetch });
    const signatureProvider = new JsSignatureProvider([wif]);
    const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });

    const transaction: IEosTransactionResult = await api.transact({
        actions: [{
            account: 'eosio',
            name: 'newaccount',
            authorization: [{
                actor: accountCreator,
                permission: 'active',
            }],
            data: {
                creator: accountCreator,
                name: accountName,
                owner: {
                    threshold: 1,
                    keys: [{
                        key: pubKeyOwner,
                        weight: 1
                    }],
                    accounts: [],
                    waits: []
                },
                active: {
                    threshold: 1,
                    keys: [{
                        key: pubKeyActive,
                        weight: 1
                    }],
                    accounts: [],
                    waits: []
                },
            },
        },
        {
            account: 'eosio',
            name: 'buyrambytes',
            authorization: [{
                actor: accountCreator,
                permission: 'active',
            }],
            data: {
                payer: accountCreator,
                receiver: accountName,
                bytes: 8192,
            },
        },
        {
            account: 'eosio',
            name: 'delegatebw',
            authorization: [{
                actor: accountCreator,
                permission: 'active',
            }],
            data: {
                from: accountCreator,
                receiver: accountName,
                stake_net_quantity: '1.0000 EOS',
                stake_cpu_quantity: '1.0000 EOS',
                transfer: false,
            }
        }]
    }, {
        blocksBehind: 3,
        expireSeconds: 30,
    });
  • How to create an EOS account with pairing of public key in nodejs? – Er Deepak Garg Nov 22 '19 at 16:16
  • EOS works differently. Account Name and Keys are separate. The Public Key is not the account address. It is not like ETH or BTC. In EOS you create an account name and then set a public key to that account. Hope this helps, I might need to know more about what you are trying to do to answer your question better – Thomas55777 Nov 22 '19 at 16:33
  • I want to make different accounts of EOS and wants to do transaction. Initial, I want to run everything on testnet server in nodejs. write now I am able to generate Private and public key pair. I want to make ACCOUNT using nodejs and then using those account and I want to do TRANSACTION, SIGN and BROADCAST in nodejs – Er Deepak Garg Nov 22 '19 at 16:52
  • See updated answer – Thomas55777 Nov 22 '19 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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