Search code examples
pythonblockchainethereumsolidity

How to create ether wallet?


I want to create users ether wallet through code. is there any api or something to create ether wallet which they can use to transfer & receive ether ?


Solution

  • First of all Ethereum network doesn't provide any API to create wallet. Any 40 digit hexadecimal string is a valid ethereum wallet with 0x as prefix. Each wallet is created using some private key. Private key is 64 digit hexadecimal string.

    Ex: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is a valid private key.

    And you will find address corresponding to this private key to be 0x8fd379246834eac74B8419FfdA202CF8051F7A03

    Private keys should be very strong. So practically it is created by key derivative function.

    For Node.js i use keythereum

    function createWallet(password) {
      const params = {keyBytes: 32, ivBytes: 16};
      const dk = keythereum.create(params);
    
      const options = {
        kdf: 'pbkdf2',
        cipher: 'aes-128-ctr',
        kdfparams: { c: 262144, dklen: 32, prf: 'hmac-sha256' }
      };
      const keyObject = keythereum.dump(password, dk.privateKey, dk.salt, dk.iv, options);
      return keyObject;
    }
     /* for getting private key from keyObject */
    function getPrivateKey(password, keyObject) {
      return keythereum.recover(password, keyObject);
    }
    
    const keyObject = createWallet("My Super secret password");
    const walletAddress = '0x' + keyObject.address;
    
    const privateKey = getPrivateKey("My Super secret password", keyObject);