Skip to content

Commit

Permalink
allow passphrase entry on Trezor (#731)
Browse files Browse the repository at this point in the history
* allow passphrase entry on Trezor

* use constants
  • Loading branch information
mholtzman authored Feb 11, 2022
1 parent 1aad4e3 commit 15803c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
44 changes: 36 additions & 8 deletions app/flex/trezor/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const TrezorConnect = require('trezor-connect').default
const { DEVICE_EVENT, DEVICE } = require('trezor-connect')
const EventEmitter = require('events')
const {
default: TrezorConnect,
DEVICE_EVENT,
DEVICE,
UI_EVENT,
UI
} = require('trezor-connect')

const events = new EventEmitter()
events.setMaxListeners(128)
let ready = false
Expand Down Expand Up @@ -39,13 +45,22 @@ class Device {
this.emit('trezor:needPhrase', this.device)
}

enteringPhrase () {
this.emit('trezor:enteringPhrase', this.device)

TrezorConnect.uiResponse({
type: UI.RECEIVE_PASSPHRASE,
payload: { value: '', passphraseOnDevice: true }
})
}

inputPin (pin, cb) {
TrezorConnect.uiResponse({ device: this.device, type: 'ui-receive_pin', payload: pin })
TrezorConnect.uiResponse({ device: this.device, type: UI.RECEIVE_PIN, payload: pin })
cb()
}

inputPhrase (phrase, cb) {
TrezorConnect.uiResponse({ device: this.device, type: 'ui-receive_passphrase', payload: { value: phrase } })
TrezorConnect.uiResponse({ device: this.device, type: UI.RECEIVE_PASSPHRASE, payload: { value: phrase } })
cb()
}

Expand Down Expand Up @@ -103,6 +118,7 @@ class Trezor {
constructor (emit) {
this.emit = emit
this.devices = {}

TrezorConnect.on(DEVICE_EVENT, e => {
if (e.type === DEVICE.CONNECT || e.type === DEVICE.CHANGED) {
// when plugging in the Trezor, the first event can sometimes be "unacquired" which
Expand All @@ -120,17 +136,29 @@ class Trezor {
delete this.devices[e.payload.path]
}
})
TrezorConnect.on('UI_EVENT', e => {
if (e.type === 'ui-request_pin') {

TrezorConnect.on(UI_EVENT, e => {
if (e.type === UI.REQUEST_PIN) {
const device = this.devices[e.payload.device.path]
if (device) device.needPin()
} else if (e.type === 'ui-request_passphrase') {
} else if (e.type === UI.REQUEST_PASSPHRASE) {
const device = this.devices[e.payload.device.path]
if (device) device.needPhrase()

if (device) {
const capabilities = (device.device.features || {}).capabilities || []

if (capabilities.includes('Capability_PassphraseEntry')) {
device.enteringPhrase()
} else {
device.needPhrase()
}
}
}
})

const manifest = { email: 'jordan@frame.sh', appUrl: 'https://frame.sh' }
const config = { manifest, popup: false, webusb: false, debug: false, lazyLoad: false }

try {
TrezorConnect.init(config).then(() => {
ready = true
Expand Down
3 changes: 2 additions & 1 deletion dash/App/Signer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function isHardwareSigner (type = '') {
}

function isLoading (status = '') {
return ['loading', 'connecting', 'addresses', 'pairing'].includes(status.toLowerCase())
const statusToCheck = status.toLowerCase()
return ['loading', 'connecting', 'addresses', 'input', 'pairing'].some(s => statusToCheck.includes(s))
}

class Signer extends React.Component {
Expand Down
10 changes: 10 additions & 0 deletions main/signers/trezor/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export default class TrezorSignerAdapter extends SignerAdapter {
})
}

const enteringPhraseListener = (device: TrezorDevice) => {
log.debug(`Trezor ${device.id} waiting for passphrase entry on device`)

this.withSigner(device, signer => {
signer.status = 'waiting for input on device'
signer.update()
})
}

const scanListener = (err: any) => {
if (err) return log.error(err)
}
Expand All @@ -96,6 +105,7 @@ export default class TrezorSignerAdapter extends SignerAdapter {
'trezor:update': updateListener,
'trezor:needPin': needPinListener,
'trezor:needPhrase': needPhraseListener,
'trezor:enteringPhrase': enteringPhraseListener,
'trezor:scan': scanListener
}

Expand Down

0 comments on commit 15803c6

Please sign in to comment.