How to create a new Metamask Wallet by web3.js from node.js?

Hi Engineers,
Could you guys please help me by giving an info? Basically i have a Node.js App. I want a way / some code of line by which i can create a fresh MetaMask Account in Node.js app.

After creating that fresh MetaMask Wallet from JS code i will get the bellow things:

  • 12 words seed phrase of MetaMask Wallet
  • The newly created Blockchain EOA account Public key & Private key

Maybe there is a NPM node.js library or anything from which i will get these above things :point_up_2:

@KBeeTheCapybara @snwlprd.eth @nakedwinnie @Chinzilla @TEMHA9l could you boss please help me if you have some time

@KBeeTheCapybara @snwlprd.eth @nakedwinnie @Chinzilla @TEMHA9l please boss

Hello @anisur072

Have you tried going through the MetaMask Docs and the MetaMask Github repository?

1 Like

@anisur072 hello!

The web3.js library can help you:

web3.eth.accounts

web3.eth.accounts.create();

Generates an account object with private key and public key. The method can also take entropy as a parameter. 1. If given it should be at least 32 characters. If none is given a random string will be generated using randomhex.

privateKeyToAccount

web3.eth.accounts.privateKeyToAccount(privateKey [, ignoreLength ]);

Creates an account object from a private key. Private key is 32 bytes of random data. If you are supplying a hexadecimal number, it must have 0x prefix in order to be in line with other Ethereum libraries

4 Likes

Hi @Chinzilla,
Thanks for your suggestions. I actually passed the whole day today on those 2 links (MetaMask doc + MetaMask Github repos) but didn’t find any solution.

At the end of the day R&D, i finally understand that MetaMask has no API reference for Node.js. I used @metamask/detect-provider npm library from node.js but it gave me error like this "Window is not defined".

Hi @snwlprd.eth,
Thanks a lot boss but sorry to say, actually I already used these 2 methods from my node.js App previously to create an EOA account in Blockchain. For your convenience, there are 2 account in blockchain world:

  1. MetaMask/Coinbase/etc account → named as Hd Wallet Account
  2. Blockchain account → named as EOA account

Now the point is the methods you provided above are used for 2nd one. That means to create an EOA account in blockchain we need to use these methods & i already used these in my node.js app previously.

Now, i basically need a way to create 1st one (MetaMask Hd Wallet Account) from Node.js App. I spent the whole day today on how to create a MetaMask wallet from Node.js app but no solution found :smiling_face_with_tear:

I used @metamask/detect-provider in my Node.js app but it returns me “Window is not defined” Error which is predicted. Could you please help me in this boss if you have some time? @snwlprd.eth

3 Likes

@anisur072

show the code of your library usage @metamask/detect-provider

Also for more advanced hierarchial address derivation, see @truffle/hdwallet-provider

2 Likes

MetaMask uses BIP39 standard for seed phrase.

This BIP describes the implementation of a mnemonic code or mnemonic sentence – a group of easy to remember words – for the generation of deterministic wallets.

It consists of two parts: generating the mnemonic and converting it into a binary seed.

Theoretically, you can generate the mnemonic yourself and pass it as a parameter to:

const HDWalletProvider = require("@truffle/hdwallet-provider");

let provider = new HDWalletProvider({
  mnemonic: {
    phrase: mnemonicPhrase
  },
  providerOrUrl: "http://localhost:8545"
});

How is the creation of mnemonics under the hood?

  1. Generate 128 bits of entropy (a random hash).

  2. Each character can then be represented in binary form.

  3. We then generate a check sum by running our above entropy through the SHA256 algorithm (this hashing algorithm is [also used in bitcoin address generation]

  4. Taking the first *n* bits where n is calculated as *the length of our entropy/32.
    S* o as 128/32=4 we’re taking the first 4 bits which is equivalent to the first character *f*. This is represented as *1111* in binary, so we append this to the end of our entropy.

  5. We then need to split into groups of 11 and convert to decimal as this is going to allow us to match these up to our wordlist!

  6. Each of these 12 11-bit groups are now represented by a number between 0 and 2047 which acts as an index to a word list of 2048 unique words which make up the seed phrase.

tip: You can find the English dictionary on github in the bitcoin repository

We have created the 12 word mnemonic which can be used to restore access to our wallet. In the case that we need to regenerate the entropy from the seed phrase we simply do the whole process backwards!

3 Likes

Also you can look at the library:
@metamask/bip39

But be careful!

This is a temporary fork of the bip39 package created by the MetaMask team. Please do not use this package for other projects, use bip39 directly instead. We are not interested in maintaining this long-term. We will abandon this as soon as we find a suitable replacement, and will not be accepting community issues or PRs.

Using it you can generate a phrase automatically:

// Generate a random mnemonic (uses crypto.randomBytes under the hood), defaults to 128-bits of entropy
const mnemonic = bip39.generateMnemonic()



>how to create a MetaMask wallet from Node.js app but no solution found

@anisur072
Don’t forget that MetaMask is an open source product and you can always make your own commit with new features. As for Node.js, if you can’t find a suitable solution or library for your needs, you can create it yourself :wink:

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.