All files / ethers.js/src.ts/wallet hdwallet.ts

96.75% Statements 566/585
83.67% Branches 41/49
80% Functions 24/30
96.75% Lines 566/585

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 5871x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 36000x 36000x 36000x 71600x 71600x 71600x 36000x 36000x 36000x 1x 18000x 18000x 18000x 18000x 18000x 18000x 1x 1x 1x 15295x 15295x 15295x 15295x 5257x 5257x 5257x 5257x 5257x 5257x 5257x 15295x 10038x 10038x 10038x 14095x 14095x 15295x 14095x 14095x 14095x 14095x 1x 1x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 7619x 15295x 15295x 15295x 5257x 5257x 5257x 5257x 15295x 10038x 10038x 10038x 10038x 10038x     15295x 6419x 6419x 6419x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 5x 5x 5x 16919x 16919x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x     16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 1x 1x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 7200x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 7600x 7600x 7600x 7600x 16919x 16919x 16919x 16919x 16919x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 11295x 16919x 16919x 16919x 16919x 16919x 5219x 5219x 16919x 16919x 3219x 3219x 3219x 3219x 3219x 3219x 3219x 3219x 3219x 3219x 3219x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 16919x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 4800x 2400x 2400x 2400x 2400x 4800x 4800x 4800x 2400x 2400x 2400x 4800x         16919x 16919x 16919x 16919x 16919x 14x 14x 14x 14x 14x 14x 16919x 16919x 16919x 16919x 16919x 5x 5x 5x 16919x 16919x 16919x 16919x 16919x 2800x 2800x 2800x 2800x 2800x 2800x 16919x 16919x 16919x 16919x 16919x 400x 400x 16919x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x       12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 10800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 12800x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 4000x 12800x 12800x 12800x 12800x 12800x 2400x 2400x 12800x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x            
/**
 *  Explain HD Wallets..
 *
 *  @_subsection: api/wallet:HD Wallets  [hd-wallets]
 */
import { computeHmac, randomBytes, ripemd160, SigningKey, sha256 } from "../crypto/index.js";
import { VoidSigner } from "../providers/index.js";
import { computeAddress } from "../transaction/index.js";
import {
    concat, dataSlice, decodeBase58, defineProperties, encodeBase58,
    getBytes, hexlify, isBytesLike,
    getNumber, toBeArray, toBigInt, toBeHex,
    assertPrivate, assert, assertArgument
} from "../utils/index.js";
import { LangEn } from "../wordlists/lang-en.js";
 
import { BaseWallet } from "./base-wallet.js";
import { Mnemonic } from "./mnemonic.js";
import {
    encryptKeystoreJson, encryptKeystoreJsonSync,
} from "./json-keystore.js";
 
import type { ProgressCallback } from "../crypto/index.js";
import type { Provider } from "../providers/index.js";
import type { BytesLike, Numeric } from "../utils/index.js";
import type { Wordlist } from "../wordlists/index.js";
 
import type { KeystoreAccount } from "./json-keystore.js";
 
/**
 *  The default derivation path for Ethereum HD Nodes. (i.e. ``"m/44'/60'/0'/0/0"``)
 */
export const defaultPath: string = "m/44'/60'/0'/0/0";
 
 
// "Bitcoin seed"
const MasterSecret = new Uint8Array([ 66, 105, 116, 99, 111, 105, 110, 32, 115, 101, 101, 100 ]);
 
const HardenedBit = 0x80000000;
 
const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
 
const Nibbles = "0123456789abcdef";
function zpad(value: number, length: number): string {
    let result = "";
    while (value) {
        result = Nibbles[value % 16] + result;
        value = Math.trunc(value / 16);
    }
    while (result.length < length * 2) { result = "0" + result; }
    return "0x" + result;
}
 
function encodeBase58Check(_value: BytesLike): string {
    const value = getBytes(_value);
    const check = dataSlice(sha256(sha256(value)), 0, 4);
    const bytes = concat([ value, check ]);
    return encodeBase58(bytes);
}
 
const _guard = { };
 
function ser_I(index: number, chainCode: string, publicKey: string, privateKey: null | string): { IL: Uint8Array, IR: Uint8Array } {
    const data = new Uint8Array(37);
 
    if (index & HardenedBit) {
        assert(privateKey != null, "cannot derive child of neutered node", "UNSUPPORTED_OPERATION", {
            operation: "deriveChild"
        });
 
        // Data = 0x00 || ser_256(k_par)
        data.set(getBytes(privateKey), 1);
 
    } else {
        // Data = ser_p(point(k_par))
        data.set(getBytes(publicKey));
    }
 
    // Data += ser_32(i)
    for (let i = 24; i >= 0; i -= 8) { data[33 + (i >> 3)] = ((index >> (24 - i)) & 0xff); }
    const I = getBytes(computeHmac("sha512", chainCode, data));
 
    return { IL: I.slice(0, 32), IR: I.slice(32) };
}
 
type HDNodeLike<T> = { depth: number, deriveChild: (i: number) => T };
function derivePath<T extends HDNodeLike<T>>(node: T, path: string): T {
    const components = path.split("/");
 
    assertArgument(components.length > 0, "invalid path", "path", path);
 
    if (components[0] === "m") {
        assertArgument(node.depth === 0, `cannot derive root path (i.e. path starting with "m/") for a node at non-zero depth ${ node.depth }`, "path", path);
        components.shift();
    }
 
    let result: T = node;
    for (let i = 0; i < components.length; i++) {
        const component = components[i];
 
        if (component.match(/^[0-9]+'$/)) {
            const index = parseInt(component.substring(0, component.length - 1));
            assertArgument(index < HardenedBit, "invalid path index", `path[${ i }]`, component);
            result = result.deriveChild(HardenedBit + index);
 
        } else if (component.match(/^[0-9]+$/)) {
            const index = parseInt(component);
            assertArgument(index < HardenedBit, "invalid path index", `path[${ i }]`, component);
            result = result.deriveChild(index);
 
        } else {
            assertArgument(false, "invalid path component", `path[${ i }]`, component);
        }
    }
 
    return result;
}
 
/**
 *  An **HDNodeWallet** is a [[Signer]] backed by the private key derived
 *  from an HD Node using the [[link-bip-32]] stantard.
 *
 *  An HD Node forms a hierarchal structure with each HD Node having a
 *  private key and the ability to derive child HD Nodes, defined by
 *  a path indicating the index of each child.
 */
export class HDNodeWallet extends BaseWallet {
    /**
     *  The compressed public key.
     */
    readonly publicKey!: string;
 
    /**
     *  The fingerprint.
     *
     *  A fingerprint allows quick qay to detect parent and child nodes,
     *  but developers should be prepared to deal with collisions as it
     *  is only 4 bytes.
     */
    readonly fingerprint!: string;
 
    /**
     *  The parent fingerprint.
     */
    readonly parentFingerprint!: string;
 
    /**
     *  The mnemonic used to create this HD Node, if available.
     *
     *  Sources such as extended keys do not encode the mnemonic, in
     *  which case this will be ``null``.
     */
    readonly mnemonic!: null | Mnemonic;
 
    /**
     *  The chaincode, which is effectively a public key used
     *  to derive children.
     */
    readonly chainCode!: string;
 
    /**
     *  The derivation path of this wallet.
     *
     *  Since extended keys do not provide full path details, this
     *  may be ``null``, if instantiated from a source that does not
     *  encode it.
     */
    readonly path!: null | string;
 
    /**
     *  The child index of this wallet. Values over ``2 *\* 31`` indicate
     *  the node is hardened.
     */
    readonly index!: number;
 
    /**
     *  The depth of this wallet, which is the number of components
     *  in its path.
     */
    readonly depth!: number;
 
    /**
     *  @private
     */
    constructor(guard: any, signingKey: SigningKey, parentFingerprint: string, chainCode: string, path: null | string, index: number, depth: number, mnemonic: null | Mnemonic, provider: null | Provider) {
        super(signingKey, provider);
        assertPrivate(guard, _guard, "HDNodeWallet");
 
        defineProperties<HDNodeWallet>(this, { publicKey: signingKey.compressedPublicKey });
 
        const fingerprint = dataSlice(ripemd160(sha256(this.publicKey)), 0, 4);
        defineProperties<HDNodeWallet>(this, {
            parentFingerprint, fingerprint,
            chainCode, path, index, depth
        });
 
        defineProperties<HDNodeWallet>(this, { mnemonic });
    }
 
    connect(provider: null | Provider): HDNodeWallet {
        return new HDNodeWallet(_guard, this.signingKey, this.parentFingerprint,
            this.chainCode, this.path, this.index, this.depth, this.mnemonic, provider);
    }
 
    #account(): KeystoreAccount {
        const account: KeystoreAccount = { address: this.address, privateKey: this.privateKey };
        const m = this.mnemonic;
        if (this.path && m && m.wordlist.locale === "en" && m.password === "") {
            account.mnemonic = {
                path: this.path,
                locale: "en",
                entropy: m.entropy
            };
        }
 
        return account;
    }
 
    /**
     *  Resolves to a [JSON Keystore Wallet](json-wallets) encrypted with
     *  %%password%%.
     *
     *  If %%progressCallback%% is specified, it will receive periodic
     *  updates as the encryption process progreses.
     */
    async encrypt(password: Uint8Array | string, progressCallback?: ProgressCallback): Promise<string> {
        return await encryptKeystoreJson(this.#account(), password, { progressCallback });
    }
 
    /**
     *  Returns a [JSON Keystore Wallet](json-wallets) encryped with
     *  %%password%%.
     *
     *  It is preferred to use the [async version](encrypt) instead,
     *  which allows a [[ProgressCallback]] to keep the user informed.
     *
     *  This method will block the event loop (freezing all UI) until
     *  it is complete, which may be a non-trivial duration.
     */
    encryptSync(password: Uint8Array | string): string {
        return encryptKeystoreJsonSync(this.#account(), password);
    }
 
    /**
     *  The extended key.
     *
     *  This key will begin with the prefix ``xpriv`` and can be used to
     *  reconstruct this HD Node to derive its children.
     */
    get extendedKey(): string {
        // We only support the mainnet values for now, but if anyone needs
        // testnet values, let me know. I believe current sentiment is that
        // we should always use mainnet, and use BIP-44 to derive the network
        //   - Mainnet: public=0x0488B21E, private=0x0488ADE4
        //   - Testnet: public=0x043587CF, private=0x04358394
 
        assert(this.depth < 256, "Depth too deep", "UNSUPPORTED_OPERATION", { operation: "extendedKey" });
 
        return encodeBase58Check(concat([
            "0x0488ADE4", zpad(this.depth, 1), this.parentFingerprint,
            zpad(this.index, 4), this.chainCode,
            concat([ "0x00", this.privateKey ])
        ]));
    }
 
    /**
     *  Returns true if this wallet has a path, providing a Type Guard
     *  that the path is non-null.
     */
    hasPath(): this is { path: string } { return (this.path != null); }
 
    /**
     *  Returns a neutered HD Node, which removes the private details
     *  of an HD Node.
     *
     *  A neutered node has no private key, but can be used to derive
     *  child addresses and other public data about the HD Node.
     */
    neuter(): HDNodeVoidWallet {
        return new HDNodeVoidWallet(_guard, this.address, this.publicKey,
            this.parentFingerprint, this.chainCode, this.path, this.index,
            this.depth, this.provider);
    }
 
    /**
     *  Return the child for %%index%%.
     */
    deriveChild(_index: Numeric): HDNodeWallet {
        const index = getNumber(_index, "index");
        assertArgument(index <= 0xffffffff, "invalid index", "index", index);
 
        // Base path
        let path = this.path;
        if (path) {
            path += "/" + (index & ~HardenedBit);
            if (index & HardenedBit) { path += "'"; }
        }
 
        const { IR, IL } = ser_I(index, this.chainCode, this.publicKey, this.privateKey);
        const ki = new SigningKey(toBeHex((toBigInt(IL) + BigInt(this.privateKey)) % N, 32));
 
        return new HDNodeWallet(_guard, ki, this.fingerprint, hexlify(IR),
            path, index, this.depth + 1, this.mnemonic, this.provider);
 
    }
 
    /**
     *  Return the HDNode for %%path%% from this node.
     */
    derivePath(path: string): HDNodeWallet {
        return derivePath<HDNodeWallet>(this, path);
    }
 
    static #fromSeed(_seed: BytesLike, mnemonic: null | Mnemonic): HDNodeWallet {
        assertArgument(isBytesLike(_seed), "invalid seed", "seed", "[REDACTED]");
 
        const seed = getBytes(_seed, "seed");
        assertArgument(seed.length >= 16 && seed.length <= 64 , "invalid seed", "seed", "[REDACTED]");
 
        const I = getBytes(computeHmac("sha512", MasterSecret, seed));
        const signingKey = new SigningKey(hexlify(I.slice(0, 32)));
 
        return new HDNodeWallet(_guard, signingKey, "0x00000000", hexlify(I.slice(32)),
            "m", 0, 0, mnemonic, null);
    }
 
    /**
     *  Creates a new HD Node from %%extendedKey%%.
     *
     *  If the %%extendedKey%% will either have a prefix or ``xpub`` or
     *  ``xpriv``, returning a neutered HD Node ([[HDNodeVoidWallet]])
     *  or full HD Node ([[HDNodeWallet) respectively.
     */
    static fromExtendedKey(extendedKey: string): HDNodeWallet | HDNodeVoidWallet {
        const bytes = toBeArray(decodeBase58(extendedKey)); // @TODO: redact
 
        assertArgument(bytes.length === 82 || encodeBase58Check(bytes.slice(0, 78)) === extendedKey,
            "invalid extended key", "extendedKey", "[ REDACTED ]");
 
        const depth = bytes[4];
        const parentFingerprint = hexlify(bytes.slice(5, 9));
        const index = parseInt(hexlify(bytes.slice(9, 13)).substring(2), 16);
        const chainCode = hexlify(bytes.slice(13, 45));
        const key = bytes.slice(45, 78);
 
        switch (hexlify(bytes.slice(0, 4))) {
            // Public Key
            case "0x0488b21e": case "0x043587cf": {
                const publicKey = hexlify(key);
                return new HDNodeVoidWallet(_guard, computeAddress(publicKey), publicKey,
                    parentFingerprint, chainCode, null, index, depth, null);
            }
 
            // Private Key
            case "0x0488ade4": case "0x04358394 ":
                if (key[0] !== 0) { break; }
                return new HDNodeWallet(_guard, new SigningKey(key.slice(1)),
                    parentFingerprint, chainCode, null, index, depth, null, null);
        }


        assertArgument(false, "invalid extended key prefix", "extendedKey", "[ REDACTED ]");
    }
 
    /**
     *  Creates a new random HDNode.
     */
    static createRandom(password?: string, path?: string, wordlist?: Wordlist): HDNodeWallet {
        if (password == null) { password = ""; }
        if (path == null) { path = defaultPath; }
        if (wordlist == null) { wordlist = LangEn.wordlist(); }
        const mnemonic = Mnemonic.fromEntropy(randomBytes(16), password, wordlist)
        return HDNodeWallet.#fromSeed(mnemonic.computeSeed(), mnemonic).derivePath(path);
    }
 
    /**
     *  Create an HD Node from %%mnemonic%%.
     */
    static fromMnemonic(mnemonic: Mnemonic, path?: string): HDNodeWallet {
        if (!path) { path = defaultPath; }
        return HDNodeWallet.#fromSeed(mnemonic.computeSeed(), mnemonic).derivePath(path);
    }
 
    /**
     *  Creates an HD Node from a mnemonic %%phrase%%.
     */
    static fromPhrase(phrase: string, password?: string, path?: string, wordlist?: Wordlist): HDNodeWallet {
        if (password == null) { password = ""; }
        if (path == null) { path = defaultPath; }
        if (wordlist == null) { wordlist = LangEn.wordlist(); }
        const mnemonic = Mnemonic.fromPhrase(phrase, password, wordlist)
        return HDNodeWallet.#fromSeed(mnemonic.computeSeed(), mnemonic).derivePath(path);
    }
 
    /**
     *  Creates an HD Node from a %%seed%%.
     */
    static fromSeed(seed: BytesLike): HDNodeWallet {
        return HDNodeWallet.#fromSeed(seed, null);
    }
}
 
/**
 *  A **HDNodeVoidWallet** cannot sign, but provides access to
 *  the children nodes of a [[link-bip-32]] HD wallet addresses.
 *
 *  The can be created by using an extended ``xpub`` key to
 *  [[HDNodeWallet_fromExtendedKey]] or by 
 *  [nuetering](HDNodeWallet-neuter) a [[HDNodeWallet]].
 */
export class HDNodeVoidWallet extends VoidSigner {
    /**
     *  The compressed public key.
     */
    readonly publicKey!: string;
 
    /**
     *  The fingerprint.
     *
     *  A fingerprint allows quick qay to detect parent and child nodes,
     *  but developers should be prepared to deal with collisions as it
     *  is only 4 bytes.
     */
    readonly fingerprint!: string;
 
    /**
     *  The parent node fingerprint.
     */
    readonly parentFingerprint!: string;
 
    /**
     *  The chaincode, which is effectively a public key used
     *  to derive children.
     */
    readonly chainCode!: string;
 
    /**
     *  The derivation path of this wallet.
     *
     *  Since extended keys do not provider full path details, this
     *  may be ``null``, if instantiated from a source that does not
     *  enocde it.
     */
    readonly path!: null | string;
 
    /**
     *  The child index of this wallet. Values over ``2 *\* 31`` indicate
     *  the node is hardened.
     */
    readonly index!: number;
 
    /**
     *  The depth of this wallet, which is the number of components
     *  in its path.
     */
    readonly depth!: number;
 
    /**
     *  @private
     */
    constructor(guard: any, address: string, publicKey: string, parentFingerprint: string, chainCode: string, path: null | string, index: number, depth: number, provider: null | Provider) {
        super(address, provider);
        assertPrivate(guard, _guard, "HDNodeVoidWallet");
 
        defineProperties<HDNodeVoidWallet>(this, { publicKey });
 
        const fingerprint = dataSlice(ripemd160(sha256(publicKey)), 0, 4);
        defineProperties<HDNodeVoidWallet>(this, {
            publicKey, fingerprint, parentFingerprint, chainCode, path, index, depth
        });
    }
 
    connect(provider: null | Provider): HDNodeVoidWallet {
        return new HDNodeVoidWallet(_guard, this.address, this.publicKey,
            this.parentFingerprint, this.chainCode, this.path, this.index, this.depth, provider);
    }
 
    /**
     *  The extended key.
     *
     *  This key will begin with the prefix ``xpub`` and can be used to
     *  reconstruct this neutered key to derive its children addresses.
     */
    get extendedKey(): string {
        // We only support the mainnet values for now, but if anyone needs
        // testnet values, let me know. I believe current sentiment is that
        // we should always use mainnet, and use BIP-44 to derive the network
        //   - Mainnet: public=0x0488B21E, private=0x0488ADE4
        //   - Testnet: public=0x043587CF, private=0x04358394
 
        assert(this.depth < 256, "Depth too deep", "UNSUPPORTED_OPERATION", { operation: "extendedKey" });
 
        return encodeBase58Check(concat([
            "0x0488B21E",
            zpad(this.depth, 1),
            this.parentFingerprint,
            zpad(this.index, 4),
            this.chainCode,
            this.publicKey,
        ]));
    }
 
    /**
     *  Returns true if this wallet has a path, providing a Type Guard
     *  that the path is non-null.
     */
    hasPath(): this is { path: string } { return (this.path != null); }
 
    /**
     *  Return the child for %%index%%.
     */
    deriveChild(_index: Numeric): HDNodeVoidWallet {
        const index = getNumber(_index, "index");
        assertArgument(index <= 0xffffffff, "invalid index", "index", index);
 
        // Base path
        let path = this.path;
        if (path) {
            path += "/" + (index & ~HardenedBit);
            if (index & HardenedBit) { path += "'"; }
        }
 
        const { IR, IL } = ser_I(index, this.chainCode, this.publicKey, null);
        const Ki = SigningKey.addPoints(IL, this.publicKey, true);
 
        const address = computeAddress(Ki);
 
        return new HDNodeVoidWallet(_guard, address, Ki, this.fingerprint, hexlify(IR),
            path, index, this.depth + 1, this.provider);
 
    }
 
    /**
     *  Return the signer for %%path%% from this node.
     */
    derivePath(path: string): HDNodeVoidWallet {
        return derivePath<HDNodeVoidWallet>(this, path);
    }
}
 
/*
export class HDNodeWalletManager {
    #root: HDNodeWallet;
 
    constructor(phrase: string, password?: null | string, path?: null | string, locale?: null | Wordlist) {
        if (password == null) { password = ""; }
        if (path == null) { path = "m/44'/60'/0'/0"; }
        if (locale == null) { locale = LangEn.wordlist(); }
        this.#root = HDNodeWallet.fromPhrase(phrase, password, path, locale);
    }
 
    getSigner(index?: number): HDNodeWallet {
        return this.#root.deriveChild((index == null) ? 0: index);
    }
}
*/
 
/**
 *  Returns the [[link-bip-32]] path for the account at %%index%%.
 *
 *  This is the pattern used by wallets like Ledger.
 *
 *  There is also an [alternate pattern](getIndexedAccountPath) used by
 *  some software.
 */
export function getAccountPath(_index: Numeric): string {
    const index = getNumber(_index, "index");
    assertArgument(index >= 0 && index < HardenedBit, "invalid account index", "index", index);
    return `m/44'/60'/${ index }'/0/0`;
}
 
/**
 *  Returns the path using an alternative pattern for deriving accounts,
 *  at %%index%%.
 *
 *  This derivation path uses the //index// component rather than the
 *  //account// component to derive sequential accounts.
 *
 *  This is the pattern used by wallets like MetaMask.
 */
export function getIndexedAccountPath(_index: Numeric): string {
    const index = getNumber(_index, "index");
    assertArgument(index >= 0 && index < HardenedBit, "invalid account index", "index", index);
    return `m/44'/60'/0'/0/${ index}`;
}