aboutsummaryrefslogblamecommitdiffstats
path: root/packages/subproviders/src/utils/wallet_utils.ts
blob: a37597dff402a1e73d9eb98ababb568a40e93486 (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                            
                                                                     
 

                                          

                                                                              


                                 
                                                                                                          




                                                    

                                                                              
                                            
                                                              
                                                                                                        
                                                                                
                                                              
                                                                              

                                                                 
                                              

                    
                               
                            

                                               








                                                       
                                                                    


                    
 
                            


                                                                                                               


                                               

                           
                                         
                        
                                            
                            


                                                                                                


                                               




                          







                                                                        

      
import ethUtil = require('ethereumjs-util');
import HDNode = require('hdkey');
import * as _ from 'lodash';

import { DerivedHDKeyInfo, WalletSubproviderErrors } from '../types';

const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;

class DerivedHDKeyInfoIterator implements IterableIterator<DerivedHDKeyInfo> {
    private _initialDerivedKey: DerivedHDKeyInfo;
    private _searchLimit: number;
    private _index: number;

    constructor(initialDerivedKey: DerivedHDKeyInfo, searchLimit: number = DEFAULT_ADDRESS_SEARCH_LIMIT) {
        this._searchLimit = searchLimit;
        this._initialDerivedKey = initialDerivedKey;
        this._index = 0;
    }

    public next(): IteratorResult<DerivedHDKeyInfo> {
        const baseDerivationPath = this._initialDerivedKey.baseDerivationPath;
        const derivationIndex = this._index;
        const isChildKey = this._initialDerivedKey.isChildKey;
        // If the DerivedHDKey is a child then we walk relative, if not we walk the full derivation path
        const fullDerivationPath = `m/${baseDerivationPath}/${derivationIndex}`;
        const relativeDerivationPath = `m/${derivationIndex}`;
        const path = isChildKey ? relativeDerivationPath : fullDerivationPath;
        const hdKey = this._initialDerivedKey.hdKey.derive(path);
        const address = walletUtils.addressOfHDKey(hdKey);
        const derivedKey: DerivedHDKeyInfo = {
            address,
            hdKey,
            baseDerivationPath,
            derivationIndex,
            derivationPath: fullDerivationPath,
            isChildKey,
        };
        const done = this._index === this._searchLimit;
        this._index++;
        return {
            done,
            value: derivedKey,
        };
    }

    public [Symbol.iterator](): IterableIterator<DerivedHDKeyInfo> {
        return this;
    }
}

export const walletUtils = {
    calculateDerivedHDKeyInfos(initialDerivedKey: DerivedHDKeyInfo, numberOfKeys: number): DerivedHDKeyInfo[] {
        const derivedKeys: DerivedHDKeyInfo[] = [];
        const derivedKeyIterator = new DerivedHDKeyInfoIterator(initialDerivedKey, numberOfKeys);
        for (const key of derivedKeyIterator) {
            derivedKeys.push(key);
        }
        return derivedKeys;
    },
    findDerivedKeyInfoForAddressIfExists(
        address: string,
        initialDerivedKey: DerivedHDKeyInfo,
        searchLimit: number,
    ): DerivedHDKeyInfo | undefined {
        let matchedKey: DerivedHDKeyInfo | undefined;
        const derivedKeyIterator = new DerivedHDKeyInfoIterator(initialDerivedKey, searchLimit);
        for (const key of derivedKeyIterator) {
            if (key.address === address) {
                matchedKey = key;
                break;
            }
        }
        return matchedKey;
    },
    addressOfHDKey(hdKey: HDNode): string {
        const shouldSanitizePublicKey = true;
        const derivedPublicKey = hdKey.publicKey;
        const ethereumAddressUnprefixed = ethUtil
            .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
            .toString('hex');
        const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
        return address;
    },
};