-
Notifications
You must be signed in to change notification settings - Fork 302
feat(passkey-crypto): add removePasskeyFromWallet function #8646
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
Open
derranW26
wants to merge
1
commit into
master
Choose a base branch
from
passkey/ticket-5-remove-passkey-from-wallet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { BitGoBase, decryptKeychainPrivateKey } from '@bitgo/sdk-core'; | ||
| import { WebAuthnOtpDevice } from './webAuthnTypes'; | ||
|
|
||
| export async function removePasskeyFromWallet(params: { | ||
| bitgo: BitGoBase; | ||
| coin: string; | ||
| walletId: string; | ||
| device: WebAuthnOtpDevice; | ||
| walletPassphrase: string; | ||
| }): Promise<void> { | ||
| const { bitgo, coin: coinName, walletId, device, walletPassphrase } = params; | ||
|
|
||
| if (!device.id) { | ||
| throw new Error('device.id is required to remove a passkey from the wallet'); | ||
| } | ||
|
|
||
| const baseCoin = bitgo.coin(coinName); | ||
| const wallet = await baseCoin.wallets().get({ id: walletId }); | ||
| const keychainId = wallet.keyIds()[0]; | ||
| const keychain = await baseCoin.keychains().get({ id: keychainId }); | ||
|
|
||
| // Verify passphrase before any mutation | ||
| const decrypted = decryptKeychainPrivateKey(bitgo, keychain, walletPassphrase); | ||
| if (!decrypted) { | ||
| throw new Error('Incorrect wallet passphrase. Passkey removal aborted to prevent lockout.'); | ||
| } | ||
|
|
||
| // No sdk-core abstraction for this endpoint; raw DELETE is required | ||
| await bitgo.del(bitgo.url(`/key/${keychainId}/webauthndevice/${device.id}`, 2)).result(); | ||
| } |
211 changes: 211 additions & 0 deletions
211
modules/passkey-crypto/test/unit/removePasskeyFromWallet.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| import * as assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import * as proxyquire from 'proxyquire'; | ||
|
|
||
| /** | ||
| * Faithful reimplementation of sdk-core's decryptKeychainPrivateKey. | ||
| * We inline it here to avoid pulling in the entire sdk-core barrel import, | ||
| * which requires dozens of built transitive dependencies. | ||
| * | ||
| * @see modules/sdk-core/src/bitgo/keychain/decryptKeychain.ts | ||
| */ | ||
| function decryptKeychainPrivateKey( | ||
| bitgo: { decrypt(opts: { input: string; password: string }): string }, | ||
| keychain: { encryptedPrv?: string; webauthnDevices?: Array<{ encryptedPrv?: string }> }, | ||
| password: string | ||
| ): string | undefined { | ||
| const prvs = [keychain.encryptedPrv, ...(keychain.webauthnDevices ?? []).map((d) => d.encryptedPrv)].filter( | ||
| (v): v is string => v != null | ||
| ); | ||
| for (const prv of prvs) { | ||
| try { | ||
| const decrypted = bitgo.decrypt({ input: prv, password }); | ||
| if (decrypted) return decrypted; | ||
| } catch { | ||
| // passphrase did not match this prv — try the next one | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| const { removePasskeyFromWallet } = proxyquire.noCallThru()('../../src/removePasskeyFromWallet', { | ||
| '@bitgo/sdk-core': { decryptKeychainPrivateKey }, | ||
| }); | ||
|
|
||
| describe('removePasskeyFromWallet', function () { | ||
| const coinName = 'tbtc'; | ||
| const walletId = 'wallet-abc123'; | ||
| const keychainId = 'key-user-id'; | ||
| const encryptedPrv = 'encrypted-prv-string'; | ||
| const walletPassphrase = 'correct-passphrase'; | ||
|
|
||
| const device = { | ||
| id: 'mongo-object-id-123', | ||
| credentialId: 'cred-id-456', | ||
| prfSalt: 'some-salt', | ||
| isPasskey: true, | ||
| }; | ||
|
|
||
| let mockBitGo: any; | ||
| let mockWallet: any; | ||
| let mockKeychains: any; | ||
| let mockWallets: any; | ||
|
|
||
| beforeEach(function () { | ||
| mockWallet = { | ||
| keyIds: sinon.stub().returns([keychainId, 'backup-key-id', 'bitgo-key-id']), | ||
| }; | ||
|
|
||
| mockWallets = { | ||
| get: sinon.stub().resolves(mockWallet), | ||
| }; | ||
|
|
||
| mockKeychains = { | ||
| get: sinon.stub().resolves({ id: keychainId, encryptedPrv }), | ||
| }; | ||
|
|
||
| mockBitGo = { | ||
| coin: sinon.stub().returns({ | ||
| wallets: sinon.stub().returns(mockWallets), | ||
| keychains: sinon.stub().returns(mockKeychains), | ||
| }), | ||
| decrypt: sinon.stub().returns('xprv-decrypted'), | ||
| del: sinon.stub().returns({ | ||
| result: sinon.stub().resolves({}), | ||
| }), | ||
| url: sinon.stub().callsFake((path: string, version?: number) => `/api/v${version ?? 1}${path}`), | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('should successfully remove a passkey device', async function () { | ||
| await removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device, | ||
| walletPassphrase, | ||
| }); | ||
|
|
||
| // Verify coin was initialized | ||
| sinon.assert.calledWithExactly(mockBitGo.coin, coinName); | ||
|
|
||
| // Verify wallet was fetched | ||
| sinon.assert.calledWithExactly(mockWallets.get, { id: walletId }); | ||
|
|
||
| // Verify keychain was fetched with correct ID | ||
| sinon.assert.calledWithExactly(mockKeychains.get, { id: keychainId }); | ||
|
|
||
| // Verify DELETE was called with device.id (not credentialId) | ||
| sinon.assert.calledOnce(mockBitGo.del); | ||
| sinon.assert.calledWithExactly(mockBitGo.del, `/api/v2/key/${keychainId}/webauthndevice/${device.id}`); | ||
| }); | ||
|
|
||
| it('should throw and not call DELETE if passphrase is wrong', async function () { | ||
| mockBitGo.decrypt = sinon.stub().throws(new Error('decryption failed')); | ||
|
|
||
| await assert.rejects( | ||
| () => | ||
| removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device, | ||
| walletPassphrase: 'wrong-passphrase', | ||
| }), | ||
| (err: Error) => { | ||
| assert.ok(err.message.includes('Incorrect wallet passphrase')); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| sinon.assert.notCalled(mockBitGo.del); | ||
| }); | ||
|
|
||
| it('should throw descriptively if keychain has no encryptedPrv', async function () { | ||
| mockKeychains.get = sinon.stub().resolves({ id: keychainId }); | ||
|
|
||
| await assert.rejects( | ||
| () => | ||
| removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device, | ||
| walletPassphrase, | ||
| }), | ||
| (err: Error) => { | ||
| assert.ok(err.message.includes('Incorrect wallet passphrase')); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| sinon.assert.notCalled(mockBitGo.del); | ||
| }); | ||
|
|
||
| it('should throw if device.id is empty', async function () { | ||
| const deviceNoId = { ...device, id: '' }; | ||
|
|
||
| await assert.rejects( | ||
| () => | ||
| removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device: deviceNoId, | ||
| walletPassphrase, | ||
| }), | ||
| (err: Error) => { | ||
| assert.ok(err.message.includes('device.id is required')); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| sinon.assert.notCalled(mockBitGo.coin); | ||
| }); | ||
|
|
||
| it('should propagate wallet fetch errors', async function () { | ||
| mockWallets.get = sinon.stub().rejects(new Error('404 Not Found')); | ||
|
|
||
| await assert.rejects( | ||
| () => | ||
| removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device, | ||
| walletPassphrase, | ||
| }), | ||
| (err: Error) => { | ||
| assert.ok(err.message.includes('404 Not Found')); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| sinon.assert.notCalled(mockBitGo.del); | ||
| }); | ||
|
|
||
| it('should propagate DELETE errors after passphrase verification', async function () { | ||
| mockBitGo.del = sinon.stub().returns({ | ||
| result: sinon.stub().rejects(new Error('500 Internal Server Error')), | ||
| }); | ||
|
|
||
| await assert.rejects( | ||
| () => | ||
| removePasskeyFromWallet({ | ||
| bitgo: mockBitGo, | ||
| coin: coinName, | ||
| walletId, | ||
| device, | ||
| walletPassphrase, | ||
| }), | ||
| (err: Error) => { | ||
| assert.ok(err.message.includes('500 Internal Server Error')); | ||
| return true; | ||
| } | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
you shouldn't need this, you should be able to mock responses via sinon and the other testing utilities we have.