|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { attachPasskeyToWallet } from '../../src/attachPasskeyToWallet'; |
| 4 | +import { WebAuthnOtpDevice, PasskeyAuthResult, WebAuthnProvider } from '../../src/webAuthnTypes'; |
| 5 | + |
| 6 | +describe('attachPasskeyToWallet', function () { |
| 7 | + const coin = 'tbtc'; |
| 8 | + const walletId = 'wallet-abc123'; |
| 9 | + const keychainId = 'key-user-id'; |
| 10 | + const enterpriseId = 'enterprise-xyz'; |
| 11 | + const encryptedPrv = 'encrypted-prv-string'; |
| 12 | + const decryptedPrv = 'xprv-decrypted'; |
| 13 | + const existingPassphrase = 'correct-passphrase'; |
| 14 | + const reEncryptedPrv = 're-encrypted-prv'; |
| 15 | + |
| 16 | + const prfResultBuffer = new Uint8Array([0x1e, 0x5c, 0xb4, 0x78]).buffer; |
| 17 | + |
| 18 | + const device: WebAuthnOtpDevice = { |
| 19 | + id: 'mongo-object-id-123', |
| 20 | + credentialId: 'cred-id-456', |
| 21 | + prfSalt: 'ZqJ64M2dL65zn2-Jxd58SMN2ILc9QjbCFxUTGHd_LC8', |
| 22 | + isPasskey: true, |
| 23 | + }; |
| 24 | + |
| 25 | + const mockAuthResult: PasskeyAuthResult = { |
| 26 | + prfResult: prfResultBuffer, |
| 27 | + credentialId: 'cred-id-456', |
| 28 | + otpCode: '123456', |
| 29 | + }; |
| 30 | + |
| 31 | + const updatedKeychain = { |
| 32 | + id: keychainId, |
| 33 | + pub: 'xpub-123', |
| 34 | + type: 'independent' as const, |
| 35 | + encryptedPrv, |
| 36 | + }; |
| 37 | + |
| 38 | + let mockWallet: { |
| 39 | + type: sinon.SinonStub; |
| 40 | + toJSON: sinon.SinonStub; |
| 41 | + getEncryptedUserKeychain: sinon.SinonStub; |
| 42 | + }; |
| 43 | + |
| 44 | + let mockWallets: { |
| 45 | + get: sinon.SinonStub; |
| 46 | + }; |
| 47 | + |
| 48 | + let mockBaseCoin: { |
| 49 | + wallets: sinon.SinonStub; |
| 50 | + }; |
| 51 | + |
| 52 | + let mockBitGo: { |
| 53 | + url: sinon.SinonStub; |
| 54 | + coin: sinon.SinonStub; |
| 55 | + put: sinon.SinonStub; |
| 56 | + decrypt: sinon.SinonStub; |
| 57 | + encrypt: sinon.SinonStub; |
| 58 | + }; |
| 59 | + |
| 60 | + let mockProvider: { |
| 61 | + create: sinon.SinonStub; |
| 62 | + get: sinon.SinonStub; |
| 63 | + }; |
| 64 | + |
| 65 | + beforeEach(function () { |
| 66 | + mockWallet = { |
| 67 | + type: sinon.stub().returns('hot'), |
| 68 | + toJSON: sinon.stub().returns({ enterprise: enterpriseId }), |
| 69 | + getEncryptedUserKeychain: sinon.stub().resolves({ id: keychainId, encryptedPrv }), |
| 70 | + }; |
| 71 | + |
| 72 | + mockWallets = { |
| 73 | + get: sinon.stub().resolves(mockWallet), |
| 74 | + }; |
| 75 | + |
| 76 | + mockBaseCoin = { |
| 77 | + wallets: sinon.stub().returns(mockWallets), |
| 78 | + }; |
| 79 | + |
| 80 | + mockBitGo = { |
| 81 | + url: sinon |
| 82 | + .stub<[path: string, version?: number], string>() |
| 83 | + .callsFake((path, version) => `/api/v${version ?? 1}${path}`), |
| 84 | + coin: sinon.stub().returns(mockBaseCoin), |
| 85 | + put: sinon.stub(), |
| 86 | + decrypt: sinon.stub(), |
| 87 | + encrypt: sinon.stub(), |
| 88 | + }; |
| 89 | + |
| 90 | + mockProvider = { |
| 91 | + create: sinon.stub(), |
| 92 | + get: sinon.stub(), |
| 93 | + }; |
| 94 | + |
| 95 | + mockBitGo.decrypt.returns(decryptedPrv); |
| 96 | + mockBitGo.encrypt.returns(reEncryptedPrv); |
| 97 | + |
| 98 | + const putSendStub = sinon.stub().returns({ result: sinon.stub().resolves(updatedKeychain) }); |
| 99 | + mockBitGo.put.returns({ send: putSendStub }); |
| 100 | + |
| 101 | + mockProvider.get.resolves(mockAuthResult); |
| 102 | + }); |
| 103 | + |
| 104 | + afterEach(function () { |
| 105 | + sinon.restore(); |
| 106 | + }); |
| 107 | + |
| 108 | + async function callAttach(overrides?: Partial<Parameters<typeof attachPasskeyToWallet>[0]>) { |
| 109 | + return attachPasskeyToWallet({ |
| 110 | + bitgo: mockBitGo as unknown as Parameters<typeof attachPasskeyToWallet>[0]['bitgo'], |
| 111 | + coin, |
| 112 | + walletId, |
| 113 | + device, |
| 114 | + existingPassphrase, |
| 115 | + provider: mockProvider as unknown as WebAuthnProvider, |
| 116 | + ...overrides, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + it('should attach a passkey and return the updated keychain', async function () { |
| 121 | + const result = await callAttach(); |
| 122 | + |
| 123 | + sinon.assert.calledWith(mockBitGo.coin, coin); |
| 124 | + sinon.assert.calledWith(mockWallets.get, { id: walletId }); |
| 125 | + sinon.assert.calledOnce(mockWallet.type); |
| 126 | + sinon.assert.calledOnce(mockWallet.getEncryptedUserKeychain); |
| 127 | + sinon.assert.calledOnce(mockBitGo.decrypt); |
| 128 | + sinon.assert.calledWithExactly(mockBitGo.decrypt, { password: existingPassphrase, input: encryptedPrv }); |
| 129 | + |
| 130 | + // provider.get called with evalByCredential keyed on device.credentialId |
| 131 | + sinon.assert.calledOnce(mockProvider.get); |
| 132 | + const getArgs = mockProvider.get.firstCall.args[0]; |
| 133 | + assert.ok(getArgs.evalByCredential); |
| 134 | + assert.strictEqual(typeof getArgs.evalByCredential[device.credentialId], 'string'); |
| 135 | + |
| 136 | + // allowCredentials must be populated with the credential ID as an ArrayBuffer |
| 137 | + assert.ok(Array.isArray(getArgs.publicKey.allowCredentials)); |
| 138 | + assert.strictEqual(getArgs.publicKey.allowCredentials.length, 1); |
| 139 | + assert.strictEqual(getArgs.publicKey.allowCredentials[0].type, 'public-key'); |
| 140 | + assert.ok(getArgs.publicKey.allowCredentials[0].id instanceof ArrayBuffer); |
| 141 | + |
| 142 | + // PUT called with correct shape |
| 143 | + sinon.assert.calledOnce(mockBitGo.put); |
| 144 | + sinon.assert.calledWith(mockBitGo.put, `/api/v2/${coin}/key/${keychainId}`); |
| 145 | + const sendStub = mockBitGo.put.firstCall.returnValue.send; |
| 146 | + sinon.assert.calledOnce(sendStub); |
| 147 | + const putBody = sendStub.firstCall.args[0]; |
| 148 | + assert.ok(putBody.webauthnInfo); |
| 149 | + assert.strictEqual(putBody.webauthnInfo.otpDeviceId, device.id); |
| 150 | + // prfSalt must be base64url (URL-safe, no padding) as required by server validation |
| 151 | + assert.match(putBody.webauthnInfo.prfSalt, /^[A-Za-z0-9\-_]+$/); |
| 152 | + assert.strictEqual(typeof putBody.webauthnInfo.encryptedPrv, 'string'); |
| 153 | + |
| 154 | + assert.strictEqual(result.id, keychainId); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should decode credentialId containing base64url-specific characters (- and _)', async function () { |
| 158 | + const deviceWithUrlChars: WebAuthnOtpDevice = { |
| 159 | + ...device, |
| 160 | + credentialId: 'abc-def_ghi+jkl', |
| 161 | + }; |
| 162 | + |
| 163 | + const result = await callAttach({ device: deviceWithUrlChars }); |
| 164 | + assert.ok(result); |
| 165 | + |
| 166 | + const getArgs = mockProvider.get.firstCall.args[0]; |
| 167 | + assert.ok(getArgs.publicKey.allowCredentials[0].id instanceof ArrayBuffer); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should throw if device.prfSalt is undefined', async function () { |
| 171 | + const deviceNoPrf: WebAuthnOtpDevice = { ...device, prfSalt: undefined }; |
| 172 | + |
| 173 | + await assert.rejects( |
| 174 | + () => callAttach({ device: deviceNoPrf }), |
| 175 | + (err: Error) => { |
| 176 | + assert.strictEqual(err.message, 'PRF extension not supported by this device. Please use a different passkey.'); |
| 177 | + return true; |
| 178 | + } |
| 179 | + ); |
| 180 | + |
| 181 | + sinon.assert.notCalled(mockBitGo.coin); |
| 182 | + sinon.assert.notCalled(mockBitGo.put); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should throw if wallet is not a hot wallet', async function () { |
| 186 | + mockWallet.type.returns('cold'); |
| 187 | + |
| 188 | + await assert.rejects( |
| 189 | + () => callAttach(), |
| 190 | + (err: Error) => { |
| 191 | + assert.ok(err.message.includes('not a hot wallet')); |
| 192 | + return true; |
| 193 | + } |
| 194 | + ); |
| 195 | + |
| 196 | + sinon.assert.notCalled(mockBitGo.put); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should throw if wallet has no enterprise', async function () { |
| 200 | + mockWallet.toJSON.returns({ enterprise: undefined }); |
| 201 | + |
| 202 | + await assert.rejects( |
| 203 | + () => callAttach(), |
| 204 | + (err: Error) => { |
| 205 | + assert.ok(err.message.includes('has no enterprise')); |
| 206 | + return true; |
| 207 | + } |
| 208 | + ); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should throw if PRF assertion returns no result', async function () { |
| 212 | + mockProvider.get.resolves({ ...mockAuthResult, prfResult: undefined }); |
| 213 | + |
| 214 | + await assert.rejects( |
| 215 | + () => callAttach(), |
| 216 | + (err: Error) => { |
| 217 | + assert.ok(err.message.includes('PRF assertion did not return a result')); |
| 218 | + return true; |
| 219 | + } |
| 220 | + ); |
| 221 | + |
| 222 | + sinon.assert.notCalled(mockBitGo.put); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should propagate decrypt errors', async function () { |
| 226 | + mockBitGo.decrypt.throws(new Error('decryption failed')); |
| 227 | + |
| 228 | + await assert.rejects( |
| 229 | + () => callAttach(), |
| 230 | + (err: Error) => { |
| 231 | + assert.ok(err.message.includes('decryption failed')); |
| 232 | + return true; |
| 233 | + } |
| 234 | + ); |
| 235 | + |
| 236 | + sinon.assert.notCalled(mockBitGo.put); |
| 237 | + }); |
| 238 | + |
| 239 | + it('should use device.credentialId as the key in evalByCredential', async function () { |
| 240 | + await callAttach(); |
| 241 | + |
| 242 | + const getArgs = mockProvider.get.firstCall.args[0]; |
| 243 | + const evalKeys = Object.keys(getArgs.evalByCredential); |
| 244 | + assert.strictEqual(evalKeys.length, 1); |
| 245 | + assert.strictEqual(evalKeys[0], device.credentialId); |
| 246 | + }); |
| 247 | +}); |
0 commit comments