New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

eth-keyring-controller

Package Overview
Dependencies
Maintainers
8
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eth-keyring-controller - npm Package Compare versions

Comparing version 8.1.0 to 9.0.0

266

index.js

@@ -0,13 +1,15 @@

const encryptor = require('@metamask/browser-passworder');
const HdKeyring = require('@metamask/eth-hd-keyring');
const { normalize: normalizeAddress } = require('@metamask/eth-sig-util');
const SimpleKeyring = require('@metamask/eth-simple-keyring');
// TODO: Stop using `events`, and remove the notice about this from the README
// eslint-disable-next-line import/no-nodejs-modules
const { EventEmitter } = require('events');
const { Buffer } = require('buffer');
const bip39 = require('@metamask/bip39');
const ObservableStore = require('obs-store');
const encryptor = require('@metamask/browser-passworder');
const { normalize: normalizeAddress } = require('eth-sig-util');
const SimpleKeyring = require('eth-simple-keyring');
const HdKeyring = require('@metamask/eth-hd-keyring');
const defaultKeyringBuilders = [
keyringBuilderFactory(SimpleKeyring),
keyringBuilderFactory(HdKeyring),
];
const keyringTypes = [SimpleKeyring, HdKeyring];
const KEYRINGS_TYPE_MAP = {

@@ -19,3 +21,4 @@ HD_KEYRING: 'HD Key Tree',

/**
* Strip the hex prefix from an address, if present
* Strip the hex prefix from an address, if present.
*
* @param {string} address - The address that might be hex prefixed.

@@ -39,9 +42,11 @@ * @returns {string} The address without a hex prefix.

const initState = opts.initState || {};
this.keyringTypes = opts.keyringTypes
? keyringTypes.concat(opts.keyringTypes)
: keyringTypes;
this.keyringBuilders = opts.keyringBuilders
? defaultKeyringBuilders.concat(opts.keyringBuilders)
: defaultKeyringBuilders;
this.store = new ObservableStore(initState);
this.memStore = new ObservableStore({
isUnlocked: false,
keyringTypes: this.keyringTypes.map((keyringType) => keyringType.type),
keyringTypes: this.keyringBuilders.map(
(keyringBuilder) => keyringBuilder.type,
),
keyrings: [],

@@ -70,3 +75,3 @@ encryptionKey: null,

*
* @returns {Object} The controller state.
* @returns {object} The controller state.
*/

@@ -86,5 +91,5 @@ fullUpdate() {

*
* @emits KeyringController#unlock
* @fires KeyringController#unlock
* @param {string} password - The password to encrypt the vault with.
* @returns {Promise<Object>} A Promise that resolves to the state.
* @returns {Promise<object>} A Promise that resolves to the state.
*/

@@ -106,38 +111,21 @@ async createNewVaultAndKeychain(password) {

*
* @emits KeyringController#unlock
* @param {string} password - The password to encrypt the vault with
* @param {string|Array<number>} seedPhrase - The BIP39-compliant seed phrase,
* either as a string or an array of UTF-8 bytes that represent the string.
* @returns {Promise<Object>} A Promise that resolves to the state.
* @fires KeyringController#unlock
* @param {string} password - The password to encrypt the vault with.
* @param {Uint8Array | string} seedPhrase - The BIP39-compliant seed phrase,
* either as a string or Uint8Array.
* @returns {Promise<object>} A Promise that resolves to the state.
*/
async createNewVaultAndRestore(password, seedPhrase) {
const seedPhraseAsBuffer =
typeof seedPhrase === 'string'
? Buffer.from(seedPhrase, 'utf8')
: Buffer.from(seedPhrase);
if (typeof password !== 'string') {
throw new Error('Password must be text.');
}
this.password = password;
const wordlists = Object.values(bip39.wordlists);
if (
wordlists.every(
(wordlist) => !bip39.validateMnemonic(seedPhraseAsBuffer, wordlist),
)
) {
throw new Error('Seed phrase is invalid.');
}
await this.clearKeyrings();
const keyring = await this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING, {
mnemonic: seedPhrase,
numberOfAccounts: 1,
});
const [firstAccount] = await keyring.getAccounts();
this.password = password;
this.clearKeyrings();
const firstKeyring = await this.addNewKeyring(
KEYRINGS_TYPE_MAP.HD_KEYRING,
{
mnemonic: seedPhraseAsBuffer,
numberOfAccounts: 1,
},
);
const [firstAccount] = await firstKeyring.getAccounts();
if (!firstAccount) {

@@ -154,4 +142,4 @@ throw new Error('KeyringController - First Account not found.');

*
* @emits KeyringController#lock
* @returns {Promise<Object>} A Promise that resolves to the state.
* @fires KeyringController#lock
* @returns {Promise<object>} A Promise that resolves to the state.
*/

@@ -176,3 +164,3 @@ async setLocked() {

/**
* Submit Password
* Submit password.
*

@@ -182,8 +170,8 @@ * Attempts to decrypt the current vault and load its keyrings

*
* Temporarily also migrates any old-style vaults first, as well.
* (Pre MetaMask 3.0.0)
* Temporarily also migrates any old-style vaults first, as well
* (Pre MetaMask 3.0.0).
*
* @emits KeyringController#unlock
* @fires KeyringController#unlock
* @param {string} password - The keyring controller password.
* @returns {Promise<Object>} A Promise that resolves to the state.
* @returns {Promise<object>} A Promise that resolves to the state.
*/

@@ -198,11 +186,11 @@ async submitPassword(password) {

/**
* Submit Encryption Key
* Submit Encryption Key.
*
* Attempts to decrypt the current vault and load its keyrings
* into memory based on the vault and CryptoKey information
* into memory based on the vault and CryptoKey information.
*
* @emits KeyringController#unlock
* @param {string} encryptionKey - The encrypted key information used to decrypt the vault
* @param {string} encryptionSalt - The salt used to generate the last key
* @returns {Promise<Object>} A Promise that resolves to the state.
* @fires KeyringController#unlock
* @param {string} encryptionKey - The encrypted key information used to decrypt the vault.
* @param {string} encryptionSalt - The salt used to generate the last key.
* @returns {Promise<object>} A Promise that resolves to the state.
*/

@@ -225,3 +213,3 @@ async submitEncryptionKey(encryptionKey, encryptionSalt) {

*
* @param {string} password
* @param {string} password - The vault password.
*/

@@ -243,14 +231,14 @@ async verifyPassword(password) {

* All Keyring classes implement a unique `type` string,
* and this is used to retrieve them from the keyringTypes array.
* and this is used to retrieve them from the keyringBuilders array.
*
* @param {string} type - The type of keyring to add.
* @param {Object} opts - The constructor options for the keyring.
* @param {object} opts - The constructor options for the keyring.
* @returns {Promise<Keyring>} The new keyring.
*/
async addNewKeyring(type, opts) {
const Keyring = this.getKeyringClassForType(type);
const keyring = new Keyring(opts);
const keyring = await this._newKeyring(type, opts);
if ((!opts || !opts.mnemonic) && type === KEYRINGS_TYPE_MAP.HD_KEYRING) {
keyring.generateRandomMnemonic();
keyring.addAccounts();
await keyring.addAccounts();
}

@@ -270,6 +258,6 @@

/**
* Remove Empty Keyrings
* Remove Empty Keyrings.
*
* Loops through the keyrings and removes the ones with empty accounts
* (usually after removing the last / only account) from a keyring
* (usually after removing the last / only account) from a keyring.
*/

@@ -324,2 +312,3 @@ async removeEmptyKeyrings() {

}
default: {

@@ -332,3 +321,3 @@ return newAccountArray;

/**
* Add New Account
* Add New Account.
*

@@ -339,3 +328,3 @@ * Calls the `addAccounts` method on the given keyring,

* @param {Keyring} selectedKeyring - The currently selected keyring.
* @returns {Promise<Object>} A Promise that resolves to the state.
* @returns {Promise<object>} A Promise that resolves to the state.
*/

@@ -369,5 +358,4 @@ async addNewAccount(selectedKeyring) {

/**
* Remove Account.
*
* Remove Account
*
* Removes a specific account from a keyring

@@ -411,6 +399,6 @@ * If the account is the last/only one then it also removes the keyring.

*
* @param {Object} ethTx - The transaction to sign.
* @param {object} ethTx - The transaction to sign.
* @param {string} _fromAddress - The transaction 'from' address.
* @param {Object} opts - Signing options.
* @returns {Promise<Object>} The signed transaction object.
* @param {object} opts - Signing options.
* @returns {Promise<object>} The signed transaction object.
*/

@@ -428,3 +416,4 @@ async signTransaction(ethTx, _fromAddress, opts = {}) {

*
* @param {Object} msgParams - The message parameters to sign.
* @param {object} msgParams - The message parameters to sign.
* @param {object} opts - Additional signing options.
* @returns {Promise<Buffer>} The raw signature.

@@ -444,3 +433,4 @@ */

*
* @param {Object} msgParams - The message parameters to sign.
* @param {object} msgParams - The message parameters to sign.
* @param {object} opts - Additional signing options.
* @returns {Promise<Buffer>} The raw signature.

@@ -459,9 +449,10 @@ */

*
* @param {Object} address - The address to get the encryption public key for.
* @param {object} address - The address to get the encryption public key for.
* @param {object} opts - Additional encryption options.
* @returns {Promise<Buffer>} The public key.
*/
async getEncryptionPublicKey(_address, opts = {}) {
const address = normalizeAddress(_address);
async getEncryptionPublicKey(address, opts = {}) {
const normalizedAddress = normalizeAddress(address);
const keyring = await this.getKeyringForAccount(address);
return await keyring.getEncryptionPublicKey(address, opts);
return await keyring.getEncryptionPublicKey(normalizedAddress, opts);
}

@@ -474,3 +465,4 @@

*
* @param {Object} msgParams - The decryption message parameters.
* @param {object} msgParams - The decryption message parameters.
* @param {object} opts - Additional decryption options.
* @returns {Promise<Buffer>} The raw decryption result.

@@ -485,6 +477,7 @@ */

/**
* Sign Typed Data
* (EIP712 https://github.com/ethereum/EIPs/pull/712#issuecomment-329988454)
* Sign Typed Data.
*
* @param {Object} msgParams - The message parameters to sign.
* @see {@link https://github.com/ethereum/EIPs/pull/712#issuecomment-329988454|EIP712}.
* @param {object} msgParams - The message parameters to sign.
* @param {object} opts - Additional signing options.
* @returns {Promise<Buffer>} The raw signature.

@@ -521,2 +514,6 @@ */

const keyring = await this.getKeyringForAccount(address);
// The "in" operator is typically restricted because it also checks inherited properties,
// which can be unexpected for plain objects. We're allowing it here because `keyring` is not
// a plain object, and we explicitly want to include inherited methods in this check.
// eslint-disable-next-line no-restricted-syntax
if (!('exportAccount' in keyring)) {

@@ -535,12 +532,12 @@ throw new Error(

/**
* Create First Key Tree
* Create First Key Tree.
*
* - Clears the existing vault
* - Creates a new vault
* - Creates a random new HD Keyring with 1 account
* - Makes that account the selected account
* - Faucets that account on testnet
* - Puts the current seed words into the state tree
* - Clears the existing vault.
* - Creates a new vault.
* - Creates a random new HD Keyring with 1 account.
* - Makes that account the selected account.
* - Faucets that account on testnet.
* - Puts the current seed words into the state tree.
*
* @returns {Promise<void>} - A promise that resolves if the operation was successful.
* @returns {Promise<void>} A promise that resolves if the operation was successful.
*/

@@ -637,3 +634,3 @@ async createFirstKeyTree() {

/**
* Unlock Keyrings
* Unlock Keyrings.
*

@@ -644,4 +641,4 @@ * Attempts to unlock the persisted encrypted storage,

* @param {string} password - The keyring controller password.
* @param {string} encryptionKey - An exported key string to unlock keyrings with
* @param {string} encryptionSalt - The salt used to encrypt the vault
* @param {string} encryptionKey - An exported key string to unlock keyrings with.
* @param {string} encryptionSalt - The salt used to encrypt the vault.
* @returns {Promise<Array<Keyring>>} The keyrings.

@@ -706,3 +703,3 @@ */

*
* @param {Object} serialized - The serialized keyring.
* @param {object} serialized - The serialized keyring.
* @returns {Promise<Keyring>} The deserialized keyring.

@@ -724,3 +721,3 @@ */

*
* @param {Object} serialized - The serialized keyring.
* @param {object} serialized - The serialized keyring.
* @returns {Promise<Keyring|undefined>} The deserialized keyring or undefined if the keyring type is unsupported.

@@ -731,9 +728,8 @@ */

const Keyring = this.getKeyringClassForType(type);
if (!Keyring) {
const keyring = await this._newKeyring(type, data);
if (!keyring) {
this._unsupportedKeyrings.push(serialized);
return undefined;
}
const keyring = new Keyring();
await keyring.deserialize(data);
// getAccounts also validates the accounts for some keyrings

@@ -748,4 +744,4 @@ await keyring.getAccounts();

*
* Searches the current `keyringTypes` array
* for a Keyring class whose unique `type` property
* Searches the current `keyringBuilders` array
* for a Keyring builder whose unique `type` property
* matches the provided `type`,

@@ -757,4 +753,6 @@ * returning it if it exists.

*/
getKeyringClassForType(type) {
return this.keyringTypes.find((keyring) => keyring.type === type);
getKeyringBuilderForType(type) {
return this.keyringBuilders.find(
(keyringBuilder) => keyringBuilder.type === type,
);
}

@@ -839,4 +837,5 @@

* Is used for adding the current keyrings to the state object.
* @param {Keyring} keyring
* @returns {Promise<Object>} A keyring display object, with type and accounts properties.
*
* @param {Keyring} keyring - The keyring to display.
* @returns {Promise<object>} A keyring display object, with type and accounts properties.
*/

@@ -885,3 +884,3 @@ async displayForKeyring(keyring) {

*
* @emits KeyringController#unlock
* @fires KeyringController#unlock
*/

@@ -894,6 +893,7 @@ setUnlocked() {

/**
* Forget hardware keyring
* Forget hardware keyring.
*
* Forget hardware and update memorized state.
* @param {Keyring} keyring
*
* @param {Keyring} keyring - The keyring to forget.
*/

@@ -910,4 +910,50 @@ forgetKeyring(keyring) {

}
/**
* Instantiate, initialize and return a new keyring
*
* The keyring instantiated is of the given `type`.
*
* @param {string} type - The type of keyring to add.
* @param {object} data - The data to restore a previously serialized keyring.
* @returns {Promise<Keyring>} The new keyring.
*/
async _newKeyring(type, data) {
const keyringBuilder = this.getKeyringBuilderForType(type);
if (!keyringBuilder) {
return undefined;
}
const keyring = keyringBuilder();
await keyring.deserialize(data);
if (keyring.init) {
await keyring.init();
}
return keyring;
}
}
module.exports = KeyringController;
/**
* Get builder function for `Keyring`
*
* Returns a builder function for `Keyring` with a `type` property.
*
* @param {Keyring} Keyring - The Keyring class for the builder.
* @returns {Function} A builder function for the given Keyring.
*/
function keyringBuilderFactory(Keyring) {
const builder = () => new Keyring();
builder.type = Keyring.type;
return builder;
}
module.exports = {
KeyringController,
keyringBuilderFactory,
};
{
"name": "eth-keyring-controller",
"version": "8.1.0",
"version": "9.0.0",
"description": "A module for managing various keyrings of Ethereum accounts, encrypting them, and using them.",

@@ -26,14 +26,13 @@ "keywords": [

"scripts": {
"test": "jest",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write"
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"test": "jest"
},
"dependencies": {
"@metamask/bip39": "^4.0.0",
"@metamask/browser-passworder": "^4.0.1",
"@metamask/eth-hd-keyring": "^4.0.2",
"eth-sig-util": "^3.0.1",
"eth-simple-keyring": "^4.2.0",
"@metamask/browser-passworder": "^4.0.2",
"@metamask/eth-hd-keyring": "^5.0.1",
"@metamask/eth-sig-util": "5.0.2",
"@metamask/eth-simple-keyring": "^5.0.0",
"obs-store": "^4.0.3"

@@ -44,17 +43,20 @@ },

"@metamask/auto-changelog": "^3.0.0",
"@metamask/eslint-config": "^7.0.1",
"@metamask/eslint-config-jest": "^7.0.0",
"@metamask/eslint-config-nodejs": "^7.0.1",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jest": "^24.3.6",
"@metamask/eslint-config": "^11.1.0",
"@metamask/eslint-config-commonjs": "^11.1.0",
"@metamask/eslint-config-jest": "^11.1.0",
"@metamask/eslint-config-nodejs": "^11.1.0",
"eslint": "^8.29.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.6",
"eslint-plugin-jsdoc": "^39.6.4",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-prettier": "^4.2.1",
"ethereumjs-wallet": "^1.0.1",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"prettier-plugin-packagejson": "^2.2.11",
"prettier": "^2.8.1",
"prettier-plugin-packagejson": "^2.3.0",
"sinon": "^11.1.1"
},
"packageManager": "yarn@3.2.4",
"engines": {

@@ -69,6 +71,7 @@ "node": ">=14.0.0"

"ethereumjs-wallet>ethereum-cryptography>keccak": false,
"ethereumjs-wallet>ethereum-cryptography>secp256k1": false
"ethereumjs-wallet>ethereum-cryptography>secp256k1": false,
"@metamask/eth-hd-keyring>eth-simple-keyring>eth-sig-util>ethereumjs-util>keccak": false,
"@metamask/eth-hd-keyring>eth-simple-keyring>eth-sig-util>ethereumjs-util>secp256k1": false
}
},
"packageManager": "yarn@3.2.4"
}
}

@@ -17,2 +17,4 @@ # Eth Keyring Controller

This library uses the Node.js `events` API. If you are using this library outside of a Node.js context, ensure that you have a polyfill for the `events` API (this is built-in to `browserify`).
## Usage

@@ -22,3 +24,3 @@

const KeyringController = require('eth-keyring-controller');
const SimpleKeyring = require('eth-simple-keyring');
const SimpleKeyring = require('@metamask/eth-simple-keyring');

@@ -25,0 +27,0 @@ const keyringController = new KeyringController({

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc