Address generation using getAddress and getCoinInfo
Closed this issue · 11 comments
EDITED:
Hi, I am a dev working with the implementation of trezor as a way to send funds using the trezor directly on our crypto exchange.
I recently added trezor-connect as a means to detect the trezor and generate the receiving account for a given coin by using getAccountInfo
with the param of coin
for example:
TrezorConnect.getAccountInfo({
coin: 'ltc',
});
Next, I use the path returned from the above method which will be in the form of: m/49'/2/0'
I then feed this path in to getAddress
:
TrezorConnect.getAddress({
path: "m/49'/2'/0'",
coin: "ltc"
});
which generates for me a public address.
I then used the public address for the user to send funds to, but now I cannot access the lost LTC I transferred as I do not see the transferred LTC nor the new address in my wallet.
I now have two questions,:
- Is it possible for me to retrieve the missing LTC back?
- What should be the appropriate and fastest way for me to generate a public address using trezor-connect and a coin param of example {LTC}
Hi Benjamin,
Could you share the transaction detail please? I'm not sure if it's even possible to send coins to a public address (I'll have to try it for myself on testnet).
If you want to get your address, you can use
TrezorConnect.getAddress({
path: "m/49'/2'/0'/0/0",
coin: "ltc"
});
This is the transaction. It was successful.
https://insight.litecore.io/address/MH5GaoH9rq38vFW22P8HdvPtvFeGETEp8L
Also, I made an edit to the topic, I remembered my code wrongly. It was sent to an address generated using getAddress
instead of getPublicKey
(which will generate a public key instead of public address)
This is what I ran:
TrezorConnect.getAddress({
path: "m/49'/2'/0'",
coin: "ltc"
});
However, that generated address as seen in litecore and the transferred LTC does not show up in my Trezor wallet
The path doesn't look correct and the device will show a warning. If you provide the correct path, it should return the correct address. You have to make sure that the address it returns is the same as on your device.
Regarding your issue, you can still recover the coins. The reason why they don't show up is because we don't run discovery on incomplete paths (it would take too long). The easiest way to get your coins back is to sign a new transaction from the invalid path to a valid one. You can do this the following way (please verify and adapt the code before using it):
TrezorConnect.signTransaction({
coin: "ltc",
inputs: [
{
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000
// Removing the last two values = incomplete path
],
amount: "137155065", // Full amount
prev_hash: "fb38ea9e3a74e9289e406f1e5fcf2dad44a5819452937a0c02d7bc3aae6f7a72",
prev_index: 0,
script_type: "SPENDADDRESS"
}
],
outputs: [
{
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000,
1, // Replace this and
0 // this with the correct path
],
amount: "137055065", // Amount - fee (0.01)
script_type: "PAYTOP2SHWITNESS"
}
],
push: true
});
The transaction will appear on your device. Again, double check and make sure everything is alright before validating it!
As a general advice when developing with cryptos, experiment on the testnets first before playing with the mainnet. It makes losses less painful 🙂
Thanks, I will try that out.
I have tested
TrezorConnect.getAccountInfo({
coin: 'ltc',
});
multiple times and it consistently returns me
{
id: 1
payload: availableBalance: "34866089"
balance: "34866089"
descriptor: "Mtub2tFpmDngyMWhCYCa937aubY6dMzxoBzwMoth6PaMN8d3doYhEeJWhtUzzHNKTHJgxZqUpfLX9DMmp5EKE3zE7nBEzBkU8bxreAubXThvq5c"
empty: false
history: {
total: 25,
unconfirmed: 0
}
path: "m/49'/2'/0'"
utxo: undefined
__proto__: Object
success: true
__proto__: Object
}
As you can see, the path is m/49'/2'/0'.
I am a little unclear on what the output address should be
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000,
1, // Replace this and
0 // this with the correct path
],
So if I want to send it to my first ltc address, is this correct?
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000,
1 | 0x80000000,
0 | 0x80000000
],
or
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000,
0 | 0x80000000,
0 | 0x80000000
],
Hi @benjaminlim00
TrezorConnect.getAccountInfo
will not work for you since this method is looking up on your account only on VALID BIP44
paths and your coins aren't there so this is expected behaviour (coins are not found)
the penultimate number in the output address_n
specifies whenever it's change or receive address on your account read more about bip 44
so basically in your case it doesn't matter where it will be sent, what matters is to send it into a VALID path
and btw be aware address_n
in your example is still not valid! the last two digits should not be hardended (0 | 0x8000000
)
so:
address_n: [
49 | 0x80000000,
2 | 0x80000000,
0 | 0x80000000,
0, // or 1 doesnt matter
0,
],
Hi, i ran the code and this is the response I got:
code: "blockchain_link/websocket_error_message"
error: "-26: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) (code 16)"
Hey devs, anyone able to explain to me what that error code means?
It could be because the input parameter is not correct. Make sure that the address you get for the given path matches the one from the transaction. If they don't match, you will need to find the correct path for the address.
Or script_type
from the input could be SPENDP2SHWITNESS
.
Or you can also try to change the output to use an address rather than a path. Something like:
outputs: [
{
address: "youraddress",
amount: "137055065", // Amount - fee (0.01)
script_type: "PAYTOADDRESS"
}
],
It's just a matter of getting the parameters right for this method. I would recommend to check out the documentation, source code and also tests to get more examples.
Thanks @keraf ! I managed to get my funds back after changing the input script_type
to SPENDP2SHWITNESS