polkadot-js / common

Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers.
Apache License 2.0
254 stars 147 forks source link

create a wallet like trustwallet #1920

Closed divyangkhatri closed 6 months ago

divyangkhatri commented 7 months ago

I have to create a wallet of polkadot like trustwallet. I tried to create a wallet with "type: ed25519" but it also return different address.

Here is the trust wallet configuration

  {
    "id": "polkadot",
    "name": "Polkadot",
    "coinId": 354,
    "symbol": "DOT",
    "decimals": 10,
    "blockchain": "Polkadot",
    "derivation": [
      {
        "path": "m/44'/354'/0'/0'/0'"
      }
    ],
    "curve": "ed25519",
    "publicKeyType": "ed25519",
    "addressHasher": "keccak256",
    "ss58Prefix": 0,
}
TarikGul commented 7 months ago

This is lacking a lot of detail.

Is this trustwallet related? If so you need to ask them support questions.

divyangkhatri commented 7 months ago

Hello @TarikGul So, below is my mnemonics

enhance chef explain another extend parade heart total brave escape together hobby

Trustwallet Polkadot address from above mnemonic

16aWaS7en2Ly2CrqFGJaixLXnwUfDThAPAeaoCh4bqAewMnK

Below is code snippet of generating wallet from mnemonic but it will return the different address.

const {Keyring} = require('@polkadot/keyring');

async function createPolkadotWalletFromMnemonic(mnemonic) {
  try {
    const keyring = new Keyring({ss58Format: 0, type: 'ed25519'});
    const keypair = keyring.addFromMnemonic(mnemonic);
    console.log('address', keypair.address);
    return {
      address: keypair.address,
    };
  } catch (e) {
    console.error('Error in polkadot', e);
    throw e;
  }
}
createPolkadotWalletFromMnemonic(
  'enhance chef explain another extend parade heart total brave escape together hobby',
);

the address generated from above code is

14n7xKyjrYkNcLnbdzTEERaBCeeRgNspMaioUAQHhszVxuRt

So, my main question is how to generate a same address like a trustwallet. So, please help me.

divyangkhatri commented 7 months ago

Hello @TarikGul , another question is below The below is the wallet details which i can get from trustwallet

mnemonic : enhance chef explain another extend parade heart total brave escape together hobby
address: 16aWaS7en2Ly2CrqFGJaixLXnwUfDThAPAeaoCh4bqAewMnK
privateKeyHex: a61c949af59758398a10f2c32c43ac08f83bd7e4a395c6467f18628625ef21a5

I can generate a same wallet from below code

     createWalletByPrivateKey: async ({privateKey}) => {
      const keyring = new Keyring({ss58Format: 0});
      const keypair = keyring.addFromSeed(
        // eslint-disable-next-line no-undef
        Buffer.from(privateKey, 'hex'),
      );
      return {
        address: keypair.address,
        privateKey: privateKey,
      };
    }

    createWalletByPrivateKey({privateKey: 'a61c949af59758398a10f2c32c43ac08f83bd7e4a395c6467f18628625ef21a5' })

So, my main question is: how get privateKeyHex or privateKey Buffer from "keypair" object? In our wallet we are allow users to export the private key and also create a wallet from import from private key

divyangkhatri commented 6 months ago

I solved the issues like this. below is code for create a wallet for specific derivePath

import {mnemonicToSeed} from 'bip39';
import {derivePath} from 'ed25519-hd-key';
import {Keyring} from '@polkadot/keyring';

const createPolkadotWallet = async mnemonic => {
  try {
    const seed = await mnemonicToSeed(mnemonic);
    const privateKey = derivePath("m/44'/354'/0'/0'/0'", seed).key;
    const keyring = new Keyring({ss58Format: 0});
    const keypair = keyring.addFromSeed(privateKey);
    return {
      address: keypair.address,
      privateKey: privateKey?.toString('hex'),
    };
  } catch (e) {
    console.error('Error in createPolkadotWallet', e);
    throw e;
  }
};
polkadot-js-bot commented 6 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.