1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
This commit is contained in:
brunobar79 2018-08-11 19:27:24 -04:00
parent 12b41b8fc2
commit 068bf43615
2 changed files with 14 additions and 24 deletions

View File

@ -289,10 +289,10 @@ class TransactionController extends EventEmitter {
// sign tx // sign tx
const fromAddress = txParams.from const fromAddress = txParams.from
const ethTx = new Transaction(txParams) const ethTx = new Transaction(txParams)
await this.signEthTx(ethTx, fromAddress) const signedTx = await this.signEthTx(ethTx, fromAddress)
// set state to signed // set state to signed
this.txStateManager.setTxStatusSigned(txMeta.id) this.txStateManager.setTxStatusSigned(txMeta.id)
const rawTx = ethUtil.bufferToHex(ethTx.serialize()) const rawTx = ethUtil.bufferToHex(signedTx.serialize())
return rawTx return rawTx
} }

View File

@ -9,7 +9,7 @@ const Transaction = require('ethereumjs-tx')
// HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger
const hdPathString = `44'/60'/0'` const hdPathString = `44'/60'/0'`
const type = 'Ledger Hardware' const type = 'Ledger Hardware'
const ORIGIN = 'https://localhost:3000' const ORIGIN = 'https://localhost:3000'
const pathBase = 'm' const pathBase = 'm'
const MAX_INDEX = 1000 const MAX_INDEX = 1000
@ -27,21 +27,18 @@ class LedgerKeyring extends EventEmitter {
this.deserialize(opts) this.deserialize(opts)
} }
setupIframe(){ setupIframe () {
this.iframe = document.createElement('iframe') this.iframe = document.createElement('iframe')
this.iframe.src = ORIGIN this.iframe.src = ORIGIN
console.log('Injecting ledger iframe') console.log('Injecting ledger iframe')
document.head.appendChild(this.iframe) document.head.appendChild(this.iframe)
console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY')
} }
sendMessage(msg, cb) { sendMessage (msg, cb) {
console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg) console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg)
this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*')
window.addEventListener('message', ({ origin, data }) => { window.addEventListener('message', ({ origin, data }) => {
if(origin !== ORIGIN) return false if (origin !== ORIGIN) return false
if (data && data.action && data.action === `${msg.action}-reply`) { if (data && data.action && data.action === `${msg.action}-reply`) {
console.log('[LEDGER]: GOT MESAGE FROM IFRAME', data) console.log('[LEDGER]: GOT MESAGE FROM IFRAME', data)
cb(data) cb(data)
@ -79,7 +76,7 @@ class LedgerKeyring extends EventEmitter {
hdPath: this.hdPath, hdPath: this.hdPath,
}, },
}, },
({action, success, payload}) => { ({action, success, payload}) => {
if (success) { if (success) {
this.hdk.publicKey = new Buffer(payload.publicKey, 'hex') this.hdk.publicKey = new Buffer(payload.publicKey, 'hex')
this.hdk.chainCode = new Buffer(payload.chainCode, 'hex') this.hdk.chainCode = new Buffer(payload.chainCode, 'hex')
@ -177,12 +174,10 @@ class LedgerKeyring extends EventEmitter {
// tx is an instance of the ethereumjs-transaction class. // tx is an instance of the ethereumjs-transaction class.
async signTransaction (address, tx) { async signTransaction (address, tx) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.unlock() this.unlock()
.then(_ => { .then(_ => {
console.log('[LEDGER]: sending message ', 'ledger-sign-transaction')
this.sendMessage({ this.sendMessage({
action: 'ledger-sign-transaction', action: 'ledger-sign-transaction',
params: { params: {
@ -196,26 +191,22 @@ class LedgerKeyring extends EventEmitter {
gasLimit: this._normalize(tx.gasLimit), gasLimit: this._normalize(tx.gasLimit),
gasPrice: this._normalize(tx.gasPrice), gasPrice: this._normalize(tx.gasPrice),
}, },
path: this._pathFromAddress(address) path: this._pathFromAddress(address),
}, },
}, },
({action, success, payload}) => { ({action, success, payload}) => {
if (success) { if (success) {
console.log('[LEDGER]: got tx signed!', payload.txData)
const signedTx = new Transaction(payload.txData) const signedTx = new Transaction(payload.txData)
// Validate that the signature matches the right address // Validate that the signature matches the right address
const addressSignedWith = ethUtil.toChecksumAddress(`0x${signedTx.from.toString('hex')}`) const addressSignedWith = ethUtil.toChecksumAddress(`0x${signedTx.from.toString('hex')}`)
const correctAddress = ethUtil.toChecksumAddress(address) const correctAddress = ethUtil.toChecksumAddress(address)
if (addressSignedWith !== correctAddress) { if (addressSignedWith !== correctAddress) {
reject('signature doesnt match the right address') reject('signature doesnt match the right address')
} }
console.log('[LEDGER]: all good!', signedTx.toJSON())
console.log('[LEDGER]: signedTX', `0x${signedTx.serialize().toString('hex')}`)
resolve(signedTx) resolve(signedTx)
} else { } else {
reject(payload) reject(payload)
} }
}) })
}) })
}) })
@ -230,7 +221,6 @@ class LedgerKeyring extends EventEmitter {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.unlock() this.unlock()
.then(_ => { .then(_ => {
console.log('[LEDGER]: sending message ', 'ledger-sign-personal-message')
this.sendMessage({ this.sendMessage({
action: 'ledger-sign-personal-message', action: 'ledger-sign-personal-message',
params: { params: {
@ -243,7 +233,7 @@ class LedgerKeyring extends EventEmitter {
resolve(payload) resolve(payload)
} else { } else {
reject(payload) reject(payload)
} }
}) })
}) })
}) })
@ -315,8 +305,8 @@ class LedgerKeyring extends EventEmitter {
return str return str
} }
_fixNonce(nonce){ _fixNonce (nonce) {
if(nonce === '0x'){ if (nonce === '0x') {
return `${nonce}0` return `${nonce}0`
} }
return nonce return nonce