provider - update wallet hooks for new wallet middleware

feature/default_network_editable
kumavis 7 years ago
parent 68aa1cce5f
commit aab9691c42
  1. 31
      app/scripts/lib/message-manager.js
  2. 35
      app/scripts/lib/personal-message-manager.js
  3. 31
      app/scripts/lib/typed-message-manager.js
  4. 77
      app/scripts/metamask-controller.js
  5. 6
      package-lock.json
  6. 2
      package.json

@ -69,10 +69,39 @@ module.exports = class MessageManager extends EventEmitter {
* new Message to this.messages, and to save the unapproved Messages from that list to this.memStore. * new Message to this.messages, and to save the unapproved Messages from that list to this.memStore.
* *
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object possibly containing the origin
* @returns {promise} after signature has been
*
*/
addUnapprovedMessageAsync (msgParams, req) {
return new Promise((resolve, reject) => {
const msgId = this.addUnapprovedMessage(msgParams, req)
// await finished
this.once(`${msgId}:finished`, (data) => {
switch (data.status) {
case 'signed':
return resolve(data.rawSig)
case 'rejected':
return reject(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
})
}
/**
* Creates a new Message with an 'unapproved' status using the passed msgParams. this.addMsg is called to add the
* new Message to this.messages, and to save the unapproved Messages from that list to this.memStore.
*
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object where the origin may be specificied
* @returns {number} The id of the newly created message. * @returns {number} The id of the newly created message.
* *
*/ */
addUnapprovedMessage (msgParams) { addUnapprovedMessage (msgParams, req) {
// add origin from request
if (req) msgParams.origin = req.origin
msgParams.data = normalizeMsgData(msgParams.data) msgParams.data = normalizeMsgData(msgParams.data)
// create txData obj with parameters and meta data // create txData obj with parameters and meta data
var time = (new Date()).getTime() var time = (new Date()).getTime()

@ -73,11 +73,43 @@ module.exports = class PersonalMessageManager extends EventEmitter {
* this.memStore. * this.memStore.
* *
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object possibly containing the origin
* @returns {promise} When the message has been signed or rejected
*
*/
addUnapprovedMessageAsync (msgParams, req) {
return new Promise((resolve, reject) => {
if (!msgParams.from) {
reject(new Error('MetaMask Message Signature: from field is required.'))
}
const msgId = this.addUnapprovedMessage(msgParams, req)
this.once(`${msgId}:finished`, (data) => {
switch (data.status) {
case 'signed':
return resolve(data.rawSig)
case 'rejected':
return reject(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
})
}
/**
* Creates a new PersonalMessage with an 'unapproved' status using the passed msgParams. this.addMsg is called to add
* the new PersonalMessage to this.messages, and to save the unapproved PersonalMessages from that list to
* this.memStore.
*
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object possibly containing the origin
* @returns {number} The id of the newly created PersonalMessage. * @returns {number} The id of the newly created PersonalMessage.
* *
*/ */
addUnapprovedMessage (msgParams) { addUnapprovedMessage (msgParams, req) {
log.debug(`PersonalMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`) log.debug(`PersonalMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`)
// add origin from request
if (req) msgParams.origin = req.origin
msgParams.data = this.normalizeMsgData(msgParams.data) msgParams.data = this.normalizeMsgData(msgParams.data)
// create txData obj with parameters and meta data // create txData obj with parameters and meta data
var time = (new Date()).getTime() var time = (new Date()).getTime()
@ -257,4 +289,3 @@ module.exports = class PersonalMessageManager extends EventEmitter {
} }
} }

@ -72,11 +72,40 @@ module.exports = class TypedMessageManager extends EventEmitter {
* this.memStore. Before any of this is done, msgParams are validated * this.memStore. Before any of this is done, msgParams are validated
* *
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object possibly containing the origin
* @returns {promise} When the message has been signed or rejected
*
*/
addUnapprovedMessageAsync (msgParams, req) {
return new Promise((resolve, reject) => {
const msgId = this.addUnapprovedMessage(msgParams, req)
this.once(`${msgId}:finished`, (data) => {
switch (data.status) {
case 'signed':
return resolve(data.rawSig)
case 'rejected':
return reject(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
})
}
/**
* Creates a new TypedMessage with an 'unapproved' status using the passed msgParams. this.addMsg is called to add
* the new TypedMessage to this.messages, and to save the unapproved TypedMessages from that list to
* this.memStore. Before any of this is done, msgParams are validated
*
* @param {Object} msgParams The params for the eth_sign call to be made after the message is approved.
* @param {Object} req (optional) The original request object possibly containing the origin
* @returns {number} The id of the newly created TypedMessage. * @returns {number} The id of the newly created TypedMessage.
* *
*/ */
addUnapprovedMessage (msgParams) { addUnapprovedMessage (msgParams, req) {
this.validateParams(msgParams) this.validateParams(msgParams)
// add origin from request
if (req) msgParams.origin = req.origin
log.debug(`TypedMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`) log.debug(`TypedMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`)
// create txData obj with parameters and meta data // create txData obj with parameters and meta data

@ -234,28 +234,22 @@ module.exports = class MetamaskController extends EventEmitter {
static: { static: {
eth_syncing: false, eth_syncing: false,
web3_clientVersion: `MetaMask/v${version}`, web3_clientVersion: `MetaMask/v${version}`,
eth_sendTransaction: (payload, next, end) => {
const origin = payload.origin
const txParams = payload.params[0]
nodeify(this.txController.newUnapprovedTransaction, this.txController)(txParams, { origin }, end)
},
}, },
// account mgmt // account mgmt
getAccounts: async () => { getAccounts: async () => {
const isUnlocked = this.keyringController.memStore.getState().isUnlocked const isUnlocked = this.keyringController.memStore.getState().isUnlocked
const result = []
const selectedAddress = this.preferencesController.getSelectedAddress() const selectedAddress = this.preferencesController.getSelectedAddress()
// only show address if account is unlocked // only show address if account is unlocked
if (isUnlocked && selectedAddress) { if (isUnlocked && selectedAddress) {
result.push(selectedAddress) return [selectedAddress]
} else {
return []
} }
return result
}, },
// tx signing // tx signing
// old style msg signing processTransaction: this.txController.newUnapprovedTransaction.bind(this.txController),
processMessage: this.newUnsignedMessage.bind(this), // msg signing
// personal_sign msg signing processEthSignMessage: this.newUnsignedMessage.bind(this),
processPersonalMessage: this.newUnsignedPersonalMessage.bind(this), processPersonalMessage: this.newUnsignedPersonalMessage.bind(this),
processTypedMessage: this.newUnsignedTypedMessage.bind(this), processTypedMessage: this.newUnsignedTypedMessage.bind(this),
} }
@ -634,20 +628,11 @@ module.exports = class MetamaskController extends EventEmitter {
* @param {Object} msgParams - The params passed to eth_sign. * @param {Object} msgParams - The params passed to eth_sign.
* @param {Function} cb = The callback function called with the signature. * @param {Function} cb = The callback function called with the signature.
*/ */
newUnsignedMessage (msgParams, cb) { newUnsignedMessage (msgParams, req) {
const msgId = this.messageManager.addUnapprovedMessage(msgParams) const promise = this.messageManager.addUnapprovedMessageAsync(msgParams, req)
this.sendUpdate() this.sendUpdate()
this.opts.showUnconfirmedMessage() this.opts.showUnconfirmedMessage()
this.messageManager.once(`${msgId}:finished`, (data) => { return promise
switch (data.status) {
case 'signed':
return cb(null, data.rawSig)
case 'rejected':
return cb(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return cb(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
} }
/** /**
@ -701,24 +686,11 @@ module.exports = class MetamaskController extends EventEmitter {
* @param {Function} cb - The callback function called with the signature. * @param {Function} cb - The callback function called with the signature.
* Passed back to the requesting Dapp. * Passed back to the requesting Dapp.
*/ */
newUnsignedPersonalMessage (msgParams, cb) { async newUnsignedPersonalMessage (msgParams, req) {
if (!msgParams.from) { const promise = this.personalMessageManager.addUnapprovedMessageAsync(msgParams, req)
return cb(new Error('MetaMask Message Signature: from field is required.'))
}
const msgId = this.personalMessageManager.addUnapprovedMessage(msgParams)
this.sendUpdate() this.sendUpdate()
this.opts.showUnconfirmedMessage() this.opts.showUnconfirmedMessage()
this.personalMessageManager.once(`${msgId}:finished`, (data) => { return promise
switch (data.status) {
case 'signed':
return cb(null, data.rawSig)
case 'rejected':
return cb(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return cb(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
} }
/** /**
@ -767,26 +739,11 @@ module.exports = class MetamaskController extends EventEmitter {
* @param {Object} msgParams - The params passed to eth_signTypedData. * @param {Object} msgParams - The params passed to eth_signTypedData.
* @param {Function} cb - The callback function, called with the signature. * @param {Function} cb - The callback function, called with the signature.
*/ */
newUnsignedTypedMessage (msgParams, cb) { newUnsignedTypedMessage (msgParams, req) {
let msgId const promise = this.typedMessageManager.addUnapprovedMessageAsync(msgParams, req)
try { this.sendUpdate()
msgId = this.typedMessageManager.addUnapprovedMessage(msgParams) this.opts.showUnconfirmedMessage()
this.sendUpdate() return promise
this.opts.showUnconfirmedMessage()
} catch (e) {
return cb(e)
}
this.typedMessageManager.once(`${msgId}:finished`, (data) => {
switch (data.status) {
case 'signed':
return cb(null, data.rawSig)
case 'rejected':
return cb(new Error('MetaMask Message Signature: User denied message signature.'))
default:
return cb(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))
}
})
} }
/** /**

6
package-lock.json generated

@ -8210,9 +8210,9 @@
} }
}, },
"eth-json-rpc-middleware": { "eth-json-rpc-middleware": {
"version": "2.1.1", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/eth-json-rpc-middleware/-/eth-json-rpc-middleware-2.1.1.tgz", "resolved": "https://registry.npmjs.org/eth-json-rpc-middleware/-/eth-json-rpc-middleware-2.2.0.tgz",
"integrity": "sha512-uIMBZXMVN/ntnvNAN/o20gOFR7Ya23gg7S3jytdejpt/F6FKl25Y4zbyFrGhbt0oCKiYfECGln8pOlu6Zc8znw==", "integrity": "sha512-P5TRtVRWYIVdhGFzLZoUFwOoS2r4rGhh2qxwPpeI8AcF+qj0EqgFHn22ts20XXqALTq+f7bmkwGEIE75e83pcw==",
"requires": { "requires": {
"async": "2.6.0", "async": "2.6.0",
"eth-query": "2.1.2", "eth-query": "2.1.2",

@ -97,7 +97,7 @@
"eth-hd-keyring": "^1.2.1", "eth-hd-keyring": "^1.2.1",
"eth-json-rpc-filters": "^2.1.1", "eth-json-rpc-filters": "^2.1.1",
"eth-json-rpc-infura": "^3.0.0", "eth-json-rpc-infura": "^3.0.0",
"eth-json-rpc-middleware": "^2.1.1", "eth-json-rpc-middleware": "^2.2.0",
"eth-keyring-controller": "^3.1.1", "eth-keyring-controller": "^3.1.1",
"eth-phishing-detect": "^1.1.4", "eth-phishing-detect": "^1.1.4",
"eth-query": "^2.1.2", "eth-query": "^2.1.2",

Loading…
Cancel
Save