|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import 'should'; |
| 4 | +import { Wallets } from '../../../../src/bitgo/wallet/wallets'; |
| 5 | + |
| 6 | +describe('Wallets - WebAuthn wallet creation', function () { |
| 7 | + let wallets: Wallets; |
| 8 | + let mockBitGo: any; |
| 9 | + let mockBaseCoin: any; |
| 10 | + let mockKeychains: any; |
| 11 | + |
| 12 | + const userPrv = 'xprvSomeUserPrivateKey'; |
| 13 | + const userPub = |
| 14 | + 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'; |
| 15 | + const backupPub = |
| 16 | + 'xpub661MyMwAqRbcGczjuMoRm6dXaLDEhW1u34gKenbeYqAix21mdUKJyuyu5F1rzYGVxyL6tmgBUAEPrEz92mBXjByMRiJdba9wpnN37RLLAXa'; |
| 17 | + const bitgoPub = |
| 18 | + 'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6LBpB85b3D2yc8sfvZU521AAwdZafEz7mnzBBsz4wKY5fTtTQBm'; |
| 19 | + |
| 20 | + beforeEach(function () { |
| 21 | + mockKeychains = { |
| 22 | + create: sinon.stub().returns({ pub: userPub, prv: userPrv }), |
| 23 | + add: sinon.stub().resolves({ id: 'user-key-id', pub: userPub, encryptedPrv: 'encrypted-prv' }), |
| 24 | + createBackup: sinon.stub().resolves({ id: 'backup-key-id', pub: backupPub }), |
| 25 | + createBitGo: sinon.stub().resolves({ id: 'bitgo-key-id', pub: bitgoPub }), |
| 26 | + }; |
| 27 | + |
| 28 | + const mockWalletData = { id: 'wallet-id', keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'] }; |
| 29 | + |
| 30 | + mockBitGo = { |
| 31 | + post: sinon.stub().returns({ |
| 32 | + send: sinon.stub().returns({ |
| 33 | + result: sinon.stub().resolves(mockWalletData), |
| 34 | + }), |
| 35 | + }), |
| 36 | + encrypt: sinon |
| 37 | + .stub() |
| 38 | + .callsFake(({ password, input }: { password: string; input: string }) => `encrypted:${password}:${input}`), |
| 39 | + setRequestTracer: sinon.stub(), |
| 40 | + }; |
| 41 | + |
| 42 | + mockBaseCoin = { |
| 43 | + isEVM: sinon.stub().returns(false), |
| 44 | + supportsTss: sinon.stub().returns(false), |
| 45 | + getFamily: sinon.stub().returns('btc'), |
| 46 | + getDefaultMultisigType: sinon.stub().returns('onchain'), |
| 47 | + keychains: sinon.stub().returns(mockKeychains), |
| 48 | + url: sinon.stub().returns('/test/url'), |
| 49 | + isValidMofNSetup: sinon.stub().returns(true), |
| 50 | + getConfig: sinon.stub().returns({ features: [] }), |
| 51 | + supplementGenerateWallet: sinon.stub().callsFake((params: any) => Promise.resolve(params)), |
| 52 | + signMessage: sinon.stub().resolves(Buffer.from('aabbcc', 'hex')), |
| 53 | + }; |
| 54 | + |
| 55 | + wallets = new Wallets(mockBitGo, mockBaseCoin); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(function () { |
| 59 | + sinon.restore(); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('generateWallet with webauthnInfo', function () { |
| 63 | + it('should add webauthnDevices to keychain params when webauthnInfo is provided', async function () { |
| 64 | + const webauthnInfo = { |
| 65 | + otpDeviceId: 'device-123', |
| 66 | + prfSalt: 'salt-abc', |
| 67 | + passphrase: 'prf-derived-passphrase', |
| 68 | + }; |
| 69 | + |
| 70 | + await wallets.generateWallet({ |
| 71 | + label: 'Test Wallet', |
| 72 | + passphrase: 'wallet-passphrase', |
| 73 | + webauthnInfo, |
| 74 | + }); |
| 75 | + |
| 76 | + assert.strictEqual(mockKeychains.add.calledOnce, true); |
| 77 | + const addParams = mockKeychains.add.firstCall.args[0]; |
| 78 | + addParams.should.have.property('webauthnDevices'); |
| 79 | + addParams.webauthnDevices.should.have.length(1); |
| 80 | + addParams.webauthnDevices[0].should.have.property('otpDeviceId', webauthnInfo.otpDeviceId); |
| 81 | + addParams.webauthnDevices[0].should.have.property('prfSalt', webauthnInfo.prfSalt); |
| 82 | + addParams.webauthnDevices[0].should.have.property('encryptedPrv'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should encrypt user private key with the webauthn passphrase', async function () { |
| 86 | + const webauthnPassphrase = 'prf-derived-passphrase'; |
| 87 | + |
| 88 | + await wallets.generateWallet({ |
| 89 | + label: 'Test Wallet', |
| 90 | + passphrase: 'wallet-passphrase', |
| 91 | + webauthnInfo: { |
| 92 | + otpDeviceId: 'device-123', |
| 93 | + prfSalt: 'salt-abc', |
| 94 | + passphrase: webauthnPassphrase, |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + const addParams = mockKeychains.add.firstCall.args[0]; |
| 99 | + const expectedEncryptedPrv = `encrypted:${webauthnPassphrase}:${userPrv}`; |
| 100 | + addParams.webauthnDevices[0].should.have.property('encryptedPrv', expectedEncryptedPrv); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should also encrypt user private key with wallet passphrase when webauthnInfo is provided', async function () { |
| 104 | + const walletPassphrase = 'wallet-passphrase'; |
| 105 | + |
| 106 | + await wallets.generateWallet({ |
| 107 | + label: 'Test Wallet', |
| 108 | + passphrase: walletPassphrase, |
| 109 | + webauthnInfo: { |
| 110 | + otpDeviceId: 'device-123', |
| 111 | + prfSalt: 'salt-abc', |
| 112 | + passphrase: 'prf-derived-passphrase', |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + const addParams = mockKeychains.add.firstCall.args[0]; |
| 117 | + const expectedEncryptedPrv = `encrypted:${walletPassphrase}:${userPrv}`; |
| 118 | + addParams.should.have.property('encryptedPrv', expectedEncryptedPrv); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should use separate encrypt calls for wallet passphrase and webauthn passphrase', async function () { |
| 122 | + const walletPassphrase = 'wallet-passphrase'; |
| 123 | + const webauthnPassphrase = 'prf-derived-passphrase'; |
| 124 | + |
| 125 | + await wallets.generateWallet({ |
| 126 | + label: 'Test Wallet', |
| 127 | + passphrase: walletPassphrase, |
| 128 | + webauthnInfo: { |
| 129 | + otpDeviceId: 'device-123', |
| 130 | + prfSalt: 'salt-abc', |
| 131 | + passphrase: webauthnPassphrase, |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + const encryptCalls = mockBitGo.encrypt.getCalls(); |
| 136 | + const passwordsUsed = encryptCalls.map((call: sinon.SinonSpyCall) => call.args[0].password); |
| 137 | + passwordsUsed.should.containEql(walletPassphrase); |
| 138 | + passwordsUsed.should.containEql(webauthnPassphrase); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should not add webauthnDevices when webauthnInfo is not provided', async function () { |
| 142 | + await wallets.generateWallet({ |
| 143 | + label: 'Test Wallet', |
| 144 | + passphrase: 'wallet-passphrase', |
| 145 | + }); |
| 146 | + |
| 147 | + assert.strictEqual(mockKeychains.add.calledOnce, true); |
| 148 | + const addParams = mockKeychains.add.firstCall.args[0]; |
| 149 | + addParams.should.not.have.property('webauthnDevices'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should not add webauthnDevices when userKey is explicitly provided (no prv available)', async function () { |
| 153 | + // When a user-provided public key is used, there is no private key to encrypt, so webauthnDevices is skipped |
| 154 | + await wallets.generateWallet({ |
| 155 | + label: 'Test Wallet', |
| 156 | + userKey: userPub, |
| 157 | + backupXpub: backupPub, |
| 158 | + webauthnInfo: { |
| 159 | + otpDeviceId: 'device-123', |
| 160 | + prfSalt: 'salt-abc', |
| 161 | + passphrase: 'prf-derived-passphrase', |
| 162 | + }, |
| 163 | + }); |
| 164 | + |
| 165 | + // add is called for both user keychain (pub-only) and backup keychain - neither should have webauthnDevices |
| 166 | + const allAddCalls = mockKeychains.add.getCalls(); |
| 167 | + assert.ok(allAddCalls.length > 0, 'expected keychains().add to be called at least once'); |
| 168 | + for (const call of allAddCalls) { |
| 169 | + call.args[0].should.not.have.property('webauthnDevices'); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + it('should return wallet with keychains when webauthnInfo is provided', async function () { |
| 174 | + const result = await wallets.generateWallet({ |
| 175 | + label: 'Test Wallet', |
| 176 | + passphrase: 'wallet-passphrase', |
| 177 | + webauthnInfo: { |
| 178 | + otpDeviceId: 'device-123', |
| 179 | + prfSalt: 'salt-abc', |
| 180 | + passphrase: 'prf-derived-passphrase', |
| 181 | + }, |
| 182 | + }); |
| 183 | + |
| 184 | + result.should.have.property('wallet'); |
| 185 | + result.should.have.property('userKeychain'); |
| 186 | + result.should.have.property('backupKeychain'); |
| 187 | + result.should.have.property('bitgoKeychain'); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments