aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/utils/wallet_utils.ts
blob: 097d2b82f3ae2d39bb4cae19a1ab25b174c65d99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import ethUtil = require('ethereumjs-util');
import HDNode = require('hdkey');
import * as _ from 'lodash';

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

const DEFAULT_ADDRESS_SEARCH_OFFSET = 0;
const BATCH_SIZE = 10;
const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;

class DerivedHDKeyIterator implements IterableIterator<DerivedHDKey> {
    private _initialDerivedKey: DerivedHDKey;
    private _searchLimit: number;
    private _index: number;

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

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

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

export const walletUtils = {
    DEFAULT_ADDRESS_SEARCH_LIMIT,
    DEFAULT_NUM_ADDRESSES_TO_FETCH: 10,
    calculateDerivedHDKeys(initialDerivedKey: DerivedHDKey, numberOfKeys: number): DerivedHDKey[] {
        const derivedKeys: DerivedHDKey[] = [];
        const derivedKeyIterator = new DerivedHDKeyIterator(initialDerivedKey, numberOfKeys);
        for (const key of derivedKeyIterator) {
            derivedKeys.push(key);
        }
        return derivedKeys;
    },
    findDerivedKeyByAddress(
        address: string,
        initialDerivedKey: DerivedHDKey,
        searchLimit: number,
    ): DerivedHDKey | undefined {
        let matchedKey: DerivedHDKey | undefined;
        const derivedKeyIterator = new DerivedHDKeyIterator(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;
    },
};