Skip to content
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

fix ledger unlocking using hdPath in accountDetails #146

Merged
merged 6 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class LedgerBridgeKeyring extends EventEmitter {
this.hdPath = hdPath
}

unlock (hdPath) {
unlock (hdPath, updateHdk = true) {
if (this.isUnlocked() && !hdPath) {
return Promise.resolve('already unlocked')
}
Expand All @@ -133,8 +133,10 @@ class LedgerBridgeKeyring extends EventEmitter {
},
({ success, payload }) => {
if (success) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a unit test for each of the true and false cases for updateHdk? (Or update existing tests of methods that call unlock to ensure that they would fail without this change)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I added test cases

this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex')
this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex')
if (updateHdk) {
this.hdk.publicKey = Buffer.from(payload.publicKey, 'hex')
this.hdk.chainCode = Buffer.from(payload.chainCode, 'hex')
}
resolve(payload.address)
} else {
reject(payload.error || new Error('Unknown error'))
Expand Down Expand Up @@ -373,7 +375,7 @@ class LedgerBridgeKeyring extends EventEmitter {
throw new Error(`Ledger: Account for address '${checksummedAddress}' not found`)
}
const { hdPath } = this.accountDetails[checksummedAddress]
const unlockedAddress = await this.unlock(hdPath)
const unlockedAddress = await this.unlock(hdPath, false)

// unlock resolves to the address for the given hdPath as reported by the ledger device
// if that address is not the requested address, then this account belongs to a different device or seed
Expand Down
46 changes: 45 additions & 1 deletion test/test-eth-ledger-bridge-keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,50 @@ describe('LedgerBridgeKeyring', function () {
done()
})
})
it('should update hdk.publicKey if updateHdk is true', function (done) {
const ledgerKeyring = new LedgerBridgeKeyring()
ledgerKeyring.hdk = { publicKey: 'ABC' }

sandbox.on(ledgerKeyring, '_sendMessage', (_, cb) => {
cb({
success: true,
payload: {
publicKey:
'04197ced33b63059074b90ddecb9400c45cbc86210a20317b539b8cae84e573342149c3384ae45f27db68e75823323e97e03504b73ecbc47f5922b9b8144345e5a',
chainCode:
'ba0fb16e01c463d1635ec36f5adeb93a838adcd1526656c55f828f1e34002a8b',
address: fakeAccounts[1],
},
})
})

ledgerKeyring.unlock(`m/44'/60'/0'/1`).then((_) => {
assert.notDeepEqual(ledgerKeyring.hdk.publicKey, 'ABC')
done()
})
})
it('should not update hdk.publicKey if updateHdk is false', function (done) {
const ledgerKeyring = new LedgerBridgeKeyring()
ledgerKeyring.hdk = { publicKey: 'ABC' }

sandbox.on(ledgerKeyring, '_sendMessage', (_, cb) => {
cb({
success: true,
payload: {
publicKey:
'04197ced33b63059074b90ddecb9400c45cbc86210a20317b539b8cae84e573342149c3384ae45f27db68e75823323e97e03504b73ecbc47f5922b9b8144345e5a',
chainCode:
'ba0fb16e01c463d1635ec36f5adeb93a838adcd1526656c55f828f1e34002a8b',
address: fakeAccounts[1],
},
})
})

ledgerKeyring.unlock(`m/44'/60'/0'/1`, false).then((_) => {
assert.deepEqual(ledgerKeyring.hdk.publicKey, 'ABC')
done()
})
})
})

describe('setHdPath', function () {
Expand Down Expand Up @@ -276,7 +320,7 @@ describe('LedgerBridgeKeyring', function () {
it('stores account details for bip44 accounts', function () {
keyring.setHdPath(`m/44'/60'/0'/0/0`)
keyring.setAccountToUnlock(1)
sandbox.on(keyring, 'unlock', (_) => Promise.resolve(fakeAccounts[1]))
sandbox.on(keyring, 'unlock', (_) => Promise.resolve(fakeAccounts[0]))
return keyring.addAccounts(1)
.then((accounts) => {
assert.deepEqual(keyring.accountDetails[accounts[0]], {
Expand Down