core-js icon indicating copy to clipboard operation
core-js copied to clipboard

Feature for Extended Private Key utility function

Open Eligioo opened this issue 6 years ago • 2 comments

New issue checklist

Question or Feature Request

Right now the path for Extended PrivateKeys is a string. Which is not really suited for manipulation. I was thinking about a utility function that makes creating new paths easier. For example you could create an iteration and generate 20 wallets for an account fast. I was wondering if there is a demand for such a method.

I created a quick demo method to illustratie what I mean


let utility = pathArray => {
    let result = new String();
    if(pathArray.length === 5) {
        result = pathArray.reduce((x, y) => `${x}/${y}'`)
    }
    else if(pathArray.length === 2) {
        result = ['m', 44, 242].concat(pathArray).reduce((x, y) => `${x}/${y}'`)
    }
    
    if(!Nimiq.ExtendedPrivateKey.isValidPath(result)) {
        throw new Error("The path is invalid.");
    }

    return result
}


let pathString = utility(['m', 44, 242, 0, 0]);
// pathString: m/44'/242'/0'/0'
let otherPathString = utility([1, 0]);
// otherPathString: m/44'/242'/1'/0'
let pathArray = Array.apply(null, {length: 5})
                            .map(Number.call, Number)
                            .map((_, i) => utility([0,i]))
/*
    pathArray = 
    [   'm/44\'/242\'/0\'/0\'',
        'm/44\'/242\'/0\'/1\'',
        'm/44\'/242\'/0\'/2\'',
        'm/44\'/242\'/0\'/3\'',
        'm/44\'/242\'/0\'/4\''  ]
*/

Eligioo avatar Feb 12 '19 16:02 Eligioo

I'm closing this stalled issue, but please feel free to reopen it if you still want to build this utility :-)

jeffesquivels avatar Sep 28 '22 21:09 jeffesquivels

I belive we will keep lots of core-js also for Albatross since everything around the protocol didn't change, or only minimally.

So I think this is something that should still be added to ExtendedPrivateKey.derivePath, to also accept an array.

sisou avatar Sep 28 '22 21:09 sisou