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 | 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 11562x 11562x 11562x 11562x 11562x 1x 1x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 23x 23x 23x 5x 5x 5x 5x 5x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 9x 9x 11x 2x 2x 2x 2x 2x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 10x 12x 2x 2x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x | import { SigningKey } from "../crypto/index.js"; import { assertArgument } from "../utils/index.js"; import { BaseWallet } from "./base-wallet.js"; import { HDNodeWallet } from "./hdwallet.js"; import { decryptCrowdsaleJson, isCrowdsaleJson } from "./json-crowdsale.js"; import { decryptKeystoreJson, decryptKeystoreJsonSync, encryptKeystoreJson, encryptKeystoreJsonSync, isKeystoreJson } from "./json-keystore.js"; import { Mnemonic } from "./mnemonic.js"; import type { ProgressCallback } from "../crypto/index.js"; import type { Provider } from "../providers/index.js"; import type { CrowdsaleAccount } from "./json-crowdsale.js"; import type { KeystoreAccount } from "./json-keystore.js"; function stall(duration: number): Promise<void> { return new Promise((resolve) => { setTimeout(() => { resolve(); }, duration); }); } /** * A **Wallet** manages a single private key which is used to sign * transactions, messages and other common payloads. * * This class is generally the main entry point for developers * that wish to use a private key directly, as it can create * instances from a large variety of common sources, including * raw private key, [[link-bip-39]] mnemonics and encrypte JSON * wallets. */ export class Wallet extends BaseWallet { /** * Create a new wallet for the private %%key%%, optionally connected * to %%provider%%. */ constructor(key: string | SigningKey, provider?: null | Provider) { if (typeof(key) === "string" && !key.startsWith("0x")) { key = "0x" + key; } let signingKey = (typeof(key) === "string") ? new SigningKey(key): key; super(signingKey, provider); } connect(provider: null | Provider): Wallet { return new Wallet(this.signingKey, provider); } /** * 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> { const account = { address: this.address, privateKey: this.privateKey }; return await encryptKeystoreJson(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 { const account = { address: this.address, privateKey: this.privateKey }; return encryptKeystoreJsonSync(account, password); } static #fromAccount(account: null | CrowdsaleAccount | KeystoreAccount): HDNodeWallet | Wallet { assertArgument(account, "invalid JSON wallet", "json", "[ REDACTED ]"); if ("mnemonic" in account && account.mnemonic && account.mnemonic.locale === "en") { const mnemonic = Mnemonic.fromEntropy(account.mnemonic.entropy); const wallet = HDNodeWallet.fromMnemonic(mnemonic, account.mnemonic.path); if (wallet.address === account.address && wallet.privateKey === account.privateKey) { return wallet; } console.log("WARNING: JSON mismatch address/privateKey != mnemonic; fallback onto private key"); } const wallet = new Wallet(account.privateKey); assertArgument(wallet.address === account.address, "address/privateKey mismatch", "json", "[ REDACTED ]"); return wallet; } /** * Creates (asynchronously) a **Wallet** by decrypting the %%json%% * with %%password%%. * * If %%progress%% is provided, it is called periodically during * decryption so that any UI can be updated. */ static async fromEncryptedJson(json: string, password: Uint8Array | string, progress?: ProgressCallback): Promise<HDNodeWallet | Wallet> { let account: null | CrowdsaleAccount | KeystoreAccount = null; if (isKeystoreJson(json)) { account = await decryptKeystoreJson(json, password, progress); } else if (isCrowdsaleJson(json)) { if (progress) { progress(0); await stall(0); } account = decryptCrowdsaleJson(json, password); if (progress) { progress(1); await stall(0); } } return Wallet.#fromAccount(account); } /** * Creates a **Wallet** by decrypting the %%json%% with %%password%%. * * The [[fromEncryptedJson]] method is preferred, as this method * will lock up and freeze the UI during decryption, which may take * some time. */ static fromEncryptedJsonSync(json: string, password: Uint8Array | string): HDNodeWallet | Wallet { let account: null | CrowdsaleAccount | KeystoreAccount = null; if (isKeystoreJson(json)) { account = decryptKeystoreJsonSync(json, password); } else if (isCrowdsaleJson(json)) { account = decryptCrowdsaleJson(json, password); } else { assertArgument(false, "invalid JSON wallet", "json", "[ REDACTED ]"); } return Wallet.#fromAccount(account); } /** * Creates a new random [[HDNodeWallet]] using the available * [cryptographic random source](randomBytes). * * If there is no crytographic random source, this will throw. */ static createRandom(provider?: null | Provider): HDNodeWallet { const wallet = HDNodeWallet.createRandom(); if (provider) { return wallet.connect(provider); } return wallet; } /** * Creates a [[HDNodeWallet]] for %%phrase%%. */ static fromPhrase(phrase: string, provider?: Provider): HDNodeWallet { const wallet = HDNodeWallet.fromPhrase(phrase); if (provider) { return wallet.connect(provider); } return wallet; } } |