tatumio / tatum-js

🚀 Tatum SDK: A 💪 powerful, 🌟 feature-rich TypeScript/JavaScript 📚 library that streamlines the 🛠️ development of 🌐 blockchain applications.
https://docs.tatum.io
MIT License
365 stars 113 forks source link

Custom Derivation Path for Bip44 HD Accounts #180

Closed CITGuru closed 1 year ago

CITGuru commented 3 years ago

Currently Tatum does not have a way to specify custom derivation path. For people who want to have multi-hierarchical deterministic wallets. You can find more information here: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki.

Tatum current derivation paths conforms to bip44 but you can only have one root account (0) for every seed phrase generated. See below:

m/44'/0'/0'/0

44 - Bip 44 constants 0 - coin type for BTC Mainnet, BTC testnet is 1, ETH is 60, BCH is 145 0 - account index. can increment to create more hd root accounts 0 - change, 0 should be constant for receiving address, 1 for an internal address like an address for receiving transaction change for UTXO blockchain wallets.

Adding the /0-n will generate all the addresses for 0 account index which would result to derivation of m/44'/0'/0'/0/{0-n} . Tatum is already doing this using the derivePath function when generating addresses and private keys

We need to specify a way to use a custom derivation path, or basically add support for HD accounts for Tatum.

Here's a function we could use to generate derivation path making sure there's no chance for input error:

const changeMap = {
    external: 0,
    internal: 1,
    nested: 2
}

const generateDerivationPath = (coinType: number, account: number, change: "external" | "internal" | "nested", testnet: boolean = true): string => {
    const changeIndex = changeMap[change]
    const accountIndex = testnet ? 0 : account
    const bip44m = `m/44'/${coinType}'/${accountIndex}'/${changeIndex}`
    return bip44m
}

If this is something Tatum would like to add to the library, I can find enough time this week to have the HD account supports

CITGuru commented 3 years ago

@samuelsramko @isra67 is this something Tatum is looking at?