Bitcoin Forum

Bitcoin => Electrum => Topic started by: xilcho on December 12, 2017, 11:11:29 PM



Title: Generating Litecoin Addresses from Electrum-LTC MPK
Post by: xilcho on December 12, 2017, 11:11:29 PM
I have created code that generates BTC addresses from an Electrum MPK. I am trying to build something similar that generates LTC addresses instead of BTC addresses. Obviously, the addresses are different, but I am struggling to determine what needs to be changed. Can my code from the BTC address generation be repurposed to generate LTC addresses since they are both hashed with ECDSHA algorithm? If so, what do I need to modify in order to generate LTC addresses instead of BTC addresses? If anyone has any existing code samples or any other information regarding this topic, I would be very appreciative. I can't seem to find any information on generating LTC addresses from an Electrum-LTC mpk.

Thanks!


Title: Re: Generating Litecoin Addresses from Electrum-LTC MPK
Post by: HCP on December 13, 2017, 06:51:09 AM
I think it uses the same hex private keys etc, so you probably just need to adjust the "network bytes" when generating the addresses...

I'd suggest that you check the code for liteaddress.org (https://github.com/litecoin-project/liteaddress.org) or maybe the BIP39 mnemonic code convertor which can generate the addresses (https://github.com/iancoleman/bip39)


Title: Re: Generating Litecoin Addresses from Electrum-LTC MPK
Post by: alivespirit on January 11, 2018, 10:43:54 PM
Does anyone have a solution to this?  Does a library exist?

There is a solution library in PHP for this in Bitcoin.  It's here:  https://plugins.svn.wordpress.org/bitcoin-address/

It would need to be updated to support Litecoin.

Any takers to develop this?  Let me know!


Title: Re: Generating Litecoin Addresses from Electrum-LTC MPK
Post by: HCP on January 12, 2018, 10:49:35 AM
As per my PM...

You need to change the part where it adds the "version" bytes to the beginning of the address and then hashes it... Bitcoin version is 0x00 and Litecoin is 0x30

I *THINK* it is in ElectrumHelper.php file... in the hash_160_to_bc_address() function:

unfortunately, I cannot include the exact code here... as it seems when I try to do that, the website thinks I am trying to inject PHP code and blocks me with a big security warning! :P

So, please DO NOT just copy paste this! ;)

    public static function hash_160_to_bc_address($h160, $addrtype = 0) {
        $vh160 = chr ($addrtype) . $h160;
...


Edited Code:
Quote
    public static function hash_160_to_bc_address($h160, $addrtype = 30) {
        $vh160 = hex2bin("30") . $h160;
...
    }

Basically... all I did was set the default addrtype to  hex value of "30"... and then convert this from "hex" to binary and prepend it to the $hash160 instead of the "chr"(0)... that it was using for Bitcoin.

This seems to start spitting out the LTC addresses that match the xpub from Electrum-LTC... and the addresses that match my xpub from my Ledger Nano S LTC wallet. So, while I am fairly confident it works, I would test it with some small amounts first! ;)