I'm currently developing an API for my company which utilizes xpub and path to generate an address.
Thing is, I managed to do that for all cryptocurrencies we require except for Zcash. We're using trezor hardware wallet and Trezor connect doesn't work for us, since we need the flow to be detached from device itself.
Generally, I'm using bitcoinjs library for all other coins, yet I couldn't derive the right address format for zcash. Spent quite a lot of time searching in github issues, but no luck. A word of help would be really great, thanks guys! My code currently looks like this:
xpub = process.env.ZEC_PUB_KEY;
network = {
messagePrefix: '\x18ZCash Signed Message:\n',
bech32: 't1',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4
},
pubKeyHash: 0x1cb8,
scriptHash: 0x1cbd,
wif: 0x80
};
p2wpkh = bjs.payments.p2wpkh({ pubkey: bjs.bip32.fromBase58(xpub, network).derive(0).derive(pathNumber).publicKey, network });
payment = bjs.payments.p2sh({ redeem: p2wpkh, network });
address = payment.address;
So, turns out I've found the answer, required some reading on weird resources. Hope this will help you.
First thing, zcash is not SegWit compatible, therefore the p2wpkh function won't work for this, you should use p2pkh. The p2sh method call is also redundant here, so the code will look something like this:
Also, bitcoinjs-lib library doesn't support altcoins in some ways. In this case, the p2pkh function utilizes toUint8 in it's core, so you can basically fork the repo and update that method to use a data type of broader boundaries, toUint16LE.
Afterwards, the function will return a bitcoin address, but we need zcash. We can easily convert it by using the following function:
That's it.