Hello. Does anyone know / can show an example of how to get the wallet address from the mnemonic?
Jul 18, 2022, 8:54 PM
Start with this library to get the public and private (secret) key from the mnemonic: https://github.com/toncenter/tonweb-mnemonic
Then use this library to get the wallet address from the keys: https://github.com/toncenter/tonweb
If you just want to read data, the public key is enough. If you want to write data (like send transaction) then you will need the secret key
Jul 18, 2022, 9:00 PM
thank you very much.Ill try this
Jul 18, 2022, 9:03 PM
Notice that there is one nuance, the wallet address is derived from the public key + the code of the wallet
There are multiple wallet codes that people use (V2, V3, V4) so each of them would have a different address
Normally you see code trying all of them to see which one is deployed
Jul 18, 2022, 9:05 PM
Maybe that's why I didn't get the right result.
const keyPair = await tonMnemonic.mnemonicToKeyPair(mnemonic);
const nacl = TonWeb.utils.nacl; // use nacl library for key pairs
const tonweb = new TonWeb();
const walletm = tonweb.wallet.create({publicKey: keyPair.publicKey, wc: 0});
address = await walletm.getAddress();
const nacl = TonWeb.utils.nacl; // use nacl library for key pairs
const tonweb = new TonWeb();
const walletm = tonweb.wallet.create({publicKey: keyPair.publicKey, wc: 0});
address = await walletm.getAddress();
this code returns incorrect address
how to change the wallet code?
i change wc: 0 to wc:3, but it return "if (wc !== 0 && wc !== -1) throw new Error('Invalid address wc ' + anyForm);"
Jul 18, 2022, 9:13 PM
First, let’s make sure it’s the incorrect address because there are several ways to encode the same address
Jul 18, 2022, 9:13 PM
address.toString(true, true, true, false)
Jul 18, 2022, 9:14 PM
Put the address you got in a block explorer and see it leads to the correct wallet
Or put the address here https://ton.org/address/ to compare the public key hex, that’s unique
If you don’t have the correct address, then probably the wallet code is from a different version
Jul 18, 2022, 9:15 PM
public keys are different😕
Jul 18, 2022, 9:16 PM
Open the real wallet address in TonWhales block explorer, it will show you the code. For example: https://tonwhales.com/explorer/address/Ef-KJp9REa0zDgaM6f3JW--e9gKDg1_udGBtzUVtTahmoY3t
You see on top it says “Wallet V3 (r2)”
That’s the code the wallet is using
When you put your correct wallet address in the explorer, what wallet code do you see?
Jul 18, 2022, 9:18 PM
V3 (r2)
Jul 18, 2022, 9:19 PM
What chain? Master chain or work chain?
Jul 18, 2022, 9:19 PM
Basic Workchain
and the address that returns with the address = await walletm.getAddress(); shows Contract Type unknown contract
🤯
Jul 18, 2022, 9:22 PM
Are you sure this is the correct mnemonic?
Jul 18, 2022, 9:22 PM
100%. thats my address
Jul 18, 2022, 9:22 PM
Ahh I see
It’s indeed not the default of TonWeb
The default for TonWeb is V3r1
https://github.com/toncenter/tonweb/blob/master/src/contract/wallet/index.js#L32
Ok, let’s fix it
You need this guy: https://github.com/toncenter/tonweb/blob/master/src/contract/wallet/index.js#L17
Create instance not of wallet, but of WalletV3ContractR2 instead and let’s see if it works
TonWeb API is super confusing. 🤦♂️ Personally I’m using ton npm which has a slightly more friendly API
If you want the slightly friendlier API that handles the multi version mess automatically, you can switch from TonWeb to this: https://github.com/tonwhales/ton
Jul 18, 2022, 9:29 PM
Im new in node.js, but I think that ill win it. 😁 thank you very much.
Jul 18, 2022, 9:31 PM
The last package has code that goes over all the versions and tries to find which one you have: https://github.com/tonwhales/ton/blob/master/src/client/Wallet.ts#L113
So you don’t need to do the manual check we did in the block explorer in the beginning
I think I’m going to write a post about it, it’s confusing AF
@prim_user write here if you can’t manage to get it working
Jul 18, 2022, 9:38 PM
Yes please & please let us know with a link ))🙏
Jul 19, 2022, 2:32 AM
@prim_user @abc9e @triangoloisoscele
I wrote the promised blog post! It's pretty cool. It explains all the weird stuff about wallets and it shows how to use a wallet from JavaScript both with TonWeb JS library and with npm ton JS library (they are both very similar, you need to choose one):
https://gist.github.com/talkol/c5e8275ff5bcb87c96d1e78135c5b12b
I submitted it right now to society.ton.org, I hope they will publish it soon. In the meantime, you can read the gist on GitHub.
Let me know if you have any feedback or anything is unclear.
I wrote the promised blog post! It's pretty cool. It explains all the weird stuff about wallets and it shows how to use a wallet from JavaScript both with TonWeb JS library and with npm ton JS library (they are both very similar, you need to choose one):
https://gist.github.com/talkol/c5e8275ff5bcb87c96d1e78135c5b12b
I submitted it right now to society.ton.org, I hope they will publish it soon. In the meantime, you can read the gist on GitHub.
Let me know if you have any feedback or anything is unclear.
Jul 19, 2022, 4:11 PM
Good read! What is tonweb sendmode?
Jul 20, 2022, 4:20 AM