-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate KeyringController
v10
#17056
Changes from all commits
0ce2727
b822771
8e0a03f
48a3dc3
d2bde20
5c49f55
f886657
187e701
8cc60b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ import { JsonRpcEngine } from 'json-rpc-engine'; | |
import { debounce } from 'lodash'; | ||
import createEngineStream from 'json-rpc-middleware-stream/engineStream'; | ||
import { providerAsMiddleware } from 'eth-json-rpc-middleware'; | ||
import KeyringController from 'eth-keyring-controller'; | ||
import { | ||
KeyringController, | ||
keyringBuilderFactory, | ||
} from '@metamask/eth-keyring-controller'; | ||
import { | ||
errorCodes as rpcErrorCodes, | ||
EthereumRpcError, | ||
|
@@ -57,6 +60,8 @@ import { | |
} from '@metamask/snaps-controllers'; | ||
///: END:ONLY_INCLUDE_IN | ||
|
||
import { wordlist as englishWordlist } from '@metamask/scure-bip39/dist/wordlists/english'; | ||
|
||
import browser from 'webextension-polyfill'; | ||
import { | ||
AssetType, | ||
|
@@ -641,15 +646,19 @@ export default class MetamaskController extends EventEmitter { | |
|
||
let additionalKeyrings = []; | ||
if (!isManifestV3) { | ||
additionalKeyrings = [ | ||
const additionalKeyringTypes = [ | ||
TrezorKeyring, | ||
LedgerBridgeKeyring, | ||
LatticeKeyring, | ||
QRHardwareKeyring, | ||
]; | ||
additionalKeyrings = additionalKeyringTypes.map((keyringType) => | ||
keyringBuilderFactory(keyringType), | ||
); | ||
} | ||
|
||
this.keyringController = new KeyringController({ | ||
keyringTypes: additionalKeyrings, | ||
keyringBuilders: additionalKeyrings, | ||
initState: initState.KeyringController, | ||
encryptor: opts.encryptor || undefined, | ||
cacheEncryptionKey: isManifestV3, | ||
|
@@ -2568,7 +2577,11 @@ export default class MetamaskController extends EventEmitter { | |
if (!keyring.mnemonic) { | ||
throw new Error('Primary keyring mnemonic unavailable.'); | ||
} | ||
return keyring.mnemonic; | ||
|
||
const recoveredIndices = Array.from( | ||
new Uint16Array(new Uint8Array(keyring.mnemonic).buffer), | ||
); | ||
return recoveredIndices.map((i) => englishWordlist[i]).join(' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,19 @@ import { strict as assert } from 'assert'; | |
import sinon from 'sinon'; | ||
import { cloneDeep } from 'lodash'; | ||
import nock from 'nock'; | ||
import { pubToAddress, bufferToHex } from 'ethereumjs-util'; | ||
import { obj as createThoughStream } from 'through2'; | ||
import EthQuery from 'eth-query'; | ||
import proxyquire from 'proxyquire'; | ||
import browser from 'webextension-polyfill'; | ||
import { wordlist as englishWordlist } from '@metamask/scure-bip39/dist/wordlists/english'; | ||
import { TransactionStatus } from '../../shared/constants/transaction'; | ||
import createTxMeta from '../../test/lib/createTxMeta'; | ||
import { NETWORK_TYPES } from '../../shared/constants/network'; | ||
import { | ||
HardwareDeviceNames, | ||
HardwareKeyringTypes, | ||
} from '../../shared/constants/hardware-wallets'; | ||
import { addHexPrefix, deferredPromise } from './lib/util'; | ||
import { deferredPromise } from './lib/util'; | ||
|
||
const Ganache = require('../../test/e2e/ganache'); | ||
|
||
|
@@ -209,13 +209,15 @@ describe('MetaMaskController', function () { | |
metamaskController.keyringController.getKeyringsByType( | ||
HardwareKeyringTypes.imported, | ||
); | ||
const privKeyBuffer = simpleKeyrings[0].wallets[0].privateKey; | ||
const pubKeyBuffer = simpleKeyrings[0].wallets[0].publicKey; | ||
const addressBuffer = pubToAddress(pubKeyBuffer); | ||
const privKey = bufferToHex(privKeyBuffer); | ||
const pubKey = bufferToHex(addressBuffer); | ||
assert.equal(privKey, addHexPrefix(importPrivkey)); | ||
assert.equal(pubKey, '0xe18035bf8712672935fdb4e5e431b1a0183d2dfc'); | ||
const pubAddressHexArr = await simpleKeyrings[0].getAccounts(); | ||
const privKeyHex = await simpleKeyrings[0].exportAccount( | ||
pubAddressHexArr[0], | ||
); | ||
Comment on lines
+212
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new pattern for getting public and private keys for an account. |
||
assert.equal(privKeyHex, importPrivkey); | ||
assert.equal( | ||
pubAddressHexArr[0], | ||
'0xe18035bf8712672935fdb4e5e431b1a0183d2dfc', | ||
); | ||
}); | ||
|
||
it('adds 1 account', async function () { | ||
|
@@ -511,6 +513,31 @@ describe('MetaMaskController', function () { | |
}); | ||
}); | ||
|
||
describe('getPrimaryKeyringMnemonic', function () { | ||
it('should return a mnemonic as a string', function () { | ||
const mockMnemonic = | ||
'above mercy benefit hospital call oval domain student sphere interest argue shock'; | ||
const mnemonicIndices = mockMnemonic | ||
.split(' ') | ||
.map((word) => englishWordlist.indexOf(word)); | ||
const uint8ArrayMnemonic = new Uint8Array( | ||
new Uint16Array(mnemonicIndices).buffer, | ||
); | ||
|
||
const mockHDKeyring = { | ||
type: 'HD Key Tree', | ||
mnemonic: uint8ArrayMnemonic, | ||
}; | ||
sinon | ||
.stub(metamaskController.keyringController, 'getKeyringsByType') | ||
.returns([mockHDKeyring]); | ||
Comment on lines
+518
to
+533
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mocks the way |
||
|
||
const recoveredMnemonic = metamaskController.getPrimaryKeyringMnemonic(); | ||
|
||
assert.equal(recoveredMnemonic, mockMnemonic); | ||
}); | ||
}); | ||
|
||
describe('checkHardwareStatus', function () { | ||
it('should throw if it receives an unknown device name', async function () { | ||
try { | ||
|
@@ -691,11 +718,8 @@ describe('MetaMaskController', function () { | |
} | ||
}); | ||
|
||
beforeEach(async function () { | ||
await metamaskController.createNewVaultAndKeychain('password'); | ||
}); | ||
|
||
it('#addNewAccount', async function () { | ||
await metamaskController.createNewVaultAndKeychain('password'); | ||
await metamaskController.addNewAccount(1); | ||
const getAccounts = | ||
await metamaskController.keyringController.getAccounts(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
KeyringController
constructor now takes keyring builder functions rather than classes.keyringBuilderFactory
takes the class types and makes them into these builder functions.