ton-org / ton

Most popular TON Typescript Library
MIT License
159 stars 37 forks source link

How to get correct wallet from derivePath for ed25519 #35

Closed divyangkhatri closed 5 months ago

divyangkhatri commented 5 months ago

Hello, I want to create same wallet like Trustwallet. Below is the trust-wallet config

 {
    "id": "ton",
    "name": "TON",
    "coinId": 607,
    "symbol": "TON",
    "decimals": 9,
    "blockchain": "The Open Network",
    "derivation": [
      {
        "path": "m/44'/607'/0'"
      }
    ],
    "curve": "ed25519",
    "publicKeyType": "ed25519",
    "explorer": {
      "url": "https://tonscan.org",
      "txPath": "/tx/",
      "accountPath": "/address/",
      "sampleTx": "fJXfn0EVhV09HFuEgUHu4Cchb24nUQtIMwSzmzk2tLs=",
      "sampleAccount": "EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N"
    },
    "info": {
      "url": "https://ton.org",
      "source": "https://github.com/ton-blockchain",
      "rpc": "https://toncenter.com/api/v2/jsonRPC",
      "documentation": "https://ton.org/docs"
    }
  },

So, below is my mnemonics

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

Trustwallet Ton wallet details

ton_private_key 10fe2073d299e085bd1309247bbdca30f487dcdbe5d1f929c06dc3f5fa9a49f4 // (hex string)
ton_public_key c36d8045860b919a386fc42fe8d6919316d932c4a55615f02cd0a27000afa470 // (hex string)
ton_address EQC6S3Sq4WZOC2skmvZ_kshjP1YOlIb-vDPEqoBYNRxYY0V6

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

const {
  mnemonicToHDSeed,
  keyPairFromSeed,
  deriveEd25519Path,
} = require('@ton/crypto');

const main = async () => {
  let buffer = await mnemonicToHDSeed([
    'enhance',
    'chef',
    'explain',
    'another',
    'extend',
    'parade',
    'heart',
    'total',
    'brave',
    'escape',
    'together',
    'hobby',
  ]);
  const keyBuffer = await deriveEd25519Path(buffer, [44, 607, 0]);
  const keyPair = keyPairFromSeed(keyBuffer);
  const wallet = WalletContractV4.create({
    publicKey: keyPair.publicKey,
    workchain: 0,
  });
  console.log('ton_public_key', keyPair.publicKey.toString('hex'));
  console.log('ton_private_key', keyPair.secretKey.toString('hex'));
  console.log('ton_address', wallet.address);
};

Above code generate below wallet

ton_public_key 66771b76746d9226057c7e62c3763be1f45f9d2840460d432ff7584895f1bcdf
ton_private_key d2908fd20f18939601fcdb645542e17ad91d38a0146a040934253eb92c1f99c366771b76746d9226057c7e62c3763be1f45f9d2840460d432ff7584895f1bcdf
ton_address EQCxpkwUlNK9t6yixPQ2RKQlWzTpZ9NrxeDymzliDLcSPuXT

So, my main question is how to generate a same wallet like a Trustwallet? So, please help me.

fereshteh-jvz commented 5 months ago

I have the same problem.. Have you find any solution?

divyangkhatri commented 5 months ago

@fereshteh-jvz No

fereshteh-jvz commented 5 months ago

You can ask your question in this group of ton developers: https://t.me/tondev_eng They might help

divyangkhatri commented 5 months ago

@fereshteh-jvz Thank you! I just asked on the Telegram channel

divyangkhatri commented 5 months ago

@fereshteh-jvz I found the solution. Thank you so much!, A guy from Telegram group help me.

const {WalletContractV4} = require('@ton/ton');
const {keyPairFromSeed} = require('@ton/crypto');
const {mnemonicToSeed} = require('bip39');
const {derivePath} = require('ed25519-hd-key');

const main = async () => {
  const seed = await mnemonicToSeed(
    'enhance chef explain another extend parade heart total brave escape together hobby',
  );
  const privateKey = derivePath("m/44'/607'/0'", seed).key;

  const keyPair = keyPairFromSeed(Buffer.from(privateKey, 'hex'));

  const wallet = WalletContractV4.create({
    publicKey: keyPair.publicKey,
    workchain: 0,
  });
  console.log('address', wallet.address);
};
main();