|
1 | 1 | import 'should'; |
2 | 2 | import { BitGoAPI } from '../../src/bitgoAPI'; |
3 | 3 | import { ProxyAgent } from 'proxy-agent'; |
| 4 | +import * as sinon from 'sinon'; |
4 | 5 |
|
5 | 6 | describe('Constructor', function () { |
6 | 7 | describe('cookiesPropagationEnabled argument', function () { |
@@ -125,4 +126,183 @@ describe('Constructor', function () { |
125 | 126 | result.should.equal(expectedUrl); |
126 | 127 | }); |
127 | 128 | }); |
| 129 | + |
| 130 | + describe('decryptKeys', function () { |
| 131 | + let bitgo: BitGoAPI; |
| 132 | + |
| 133 | + beforeEach(function () { |
| 134 | + bitgo = new BitGoAPI({ |
| 135 | + env: 'test', |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + afterEach(function () { |
| 140 | + sinon.restore(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should throw if no params are provided', function () { |
| 144 | + try { |
| 145 | + // @ts-expect-error - intentionally calling with no params for test |
| 146 | + bitgo.decryptKeys(); |
| 147 | + throw new Error('Expected error but got none'); |
| 148 | + } catch (e) { |
| 149 | + e.message.should.containEql('Missing parameter'); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + it('should throw if walletIdEncryptedKeyPairs is missing', function () { |
| 154 | + try { |
| 155 | + // @ts-expect-error - intentionally missing required param |
| 156 | + bitgo.decryptKeys({ password: 'password123' }); |
| 157 | + throw new Error('Expected error but got none'); |
| 158 | + } catch (e) { |
| 159 | + e.message.should.containEql('Missing parameter: walletIdEncryptedKeyPairs'); |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + it('should throw if password is missing', function () { |
| 164 | + try { |
| 165 | + // @ts-expect-error - intentionally missing required param |
| 166 | + bitgo.decryptKeys({ walletIdEncryptedKeyPairs: [] }); |
| 167 | + throw new Error('Expected error but got none'); |
| 168 | + } catch (e) { |
| 169 | + e.message.should.containEql('Missing parameter: password'); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + it('should throw if walletIdEncryptedKeyPairs is not an array', function () { |
| 174 | + try { |
| 175 | + // @ts-expect-error - intentionally providing wrong type |
| 176 | + bitgo.decryptKeys({ walletIdEncryptedKeyPairs: 'not an array', password: 'password123' }); |
| 177 | + throw new Error('Expected error but got none'); |
| 178 | + } catch (e) { |
| 179 | + e.message.should.equal('walletIdEncryptedKeyPairs must be an array'); |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + it('should return empty array for empty walletIdEncryptedKeyPairs', function () { |
| 184 | + const result = bitgo.decryptKeys({ walletIdEncryptedKeyPairs: [], password: 'password123' }); |
| 185 | + result.should.be.an.Array(); |
| 186 | + result.should.be.empty(); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should throw if any walletId is missing or not a string', function () { |
| 190 | + try { |
| 191 | + bitgo.decryptKeys({ |
| 192 | + walletIdEncryptedKeyPairs: [ |
| 193 | + // @ts-expect-error - intentionally missing walletId |
| 194 | + { |
| 195 | + encryptedPrv: 'encrypted-data', |
| 196 | + }, |
| 197 | + ], |
| 198 | + password: 'password123', |
| 199 | + }); |
| 200 | + throw new Error('Expected error but got none'); |
| 201 | + } catch (e) { |
| 202 | + e.message.should.equal('each key pair must have a string walletId'); |
| 203 | + } |
| 204 | + |
| 205 | + try { |
| 206 | + bitgo.decryptKeys({ |
| 207 | + walletIdEncryptedKeyPairs: [ |
| 208 | + { |
| 209 | + // @ts-expect-error - intentionally providing wrong type |
| 210 | + walletId: 123, |
| 211 | + encryptedPrv: 'encrypted-data', |
| 212 | + }, |
| 213 | + ], |
| 214 | + password: 'password123', |
| 215 | + }); |
| 216 | + throw new Error('Expected error but got none'); |
| 217 | + } catch (e) { |
| 218 | + e.message.should.equal('each key pair must have a string walletId'); |
| 219 | + } |
| 220 | + }); |
| 221 | + |
| 222 | + it('should throw if any encryptedPrv is missing or not a string', function () { |
| 223 | + try { |
| 224 | + bitgo.decryptKeys({ |
| 225 | + walletIdEncryptedKeyPairs: [ |
| 226 | + // @ts-expect-error - intentionally missing encryptedPrv |
| 227 | + { |
| 228 | + walletId: 'wallet-id-1', |
| 229 | + }, |
| 230 | + ], |
| 231 | + password: 'password123', |
| 232 | + }); |
| 233 | + throw new Error('Expected error but got none'); |
| 234 | + } catch (e) { |
| 235 | + e.message.should.equal('each key pair must have a string encryptedPrv'); |
| 236 | + } |
| 237 | + |
| 238 | + try { |
| 239 | + bitgo.decryptKeys({ |
| 240 | + walletIdEncryptedKeyPairs: [ |
| 241 | + { |
| 242 | + walletId: 'wallet-id-1', |
| 243 | + // @ts-expect-error - intentionally providing wrong type |
| 244 | + encryptedPrv: 123, |
| 245 | + }, |
| 246 | + ], |
| 247 | + password: 'password123', |
| 248 | + }); |
| 249 | + throw new Error('Expected error but got none'); |
| 250 | + } catch (e) { |
| 251 | + e.message.should.equal('each key pair must have a string encryptedPrv'); |
| 252 | + } |
| 253 | + }); |
| 254 | + |
| 255 | + it('should return walletIds of keys that failed to decrypt', function () { |
| 256 | + // Create a stub for the decrypt method |
| 257 | + const decryptStub = sinon.stub(bitgo, 'decrypt'); |
| 258 | + |
| 259 | + // Make it succeed for first wallet and fail for second wallet |
| 260 | + decryptStub.onFirstCall().returns('decrypted-key-1'); |
| 261 | + decryptStub.onSecondCall().throws(new Error('decryption failed')); |
| 262 | + |
| 263 | + const result = bitgo.decryptKeys({ |
| 264 | + walletIdEncryptedKeyPairs: [ |
| 265 | + { walletId: 'wallet-id-1', encryptedPrv: 'encrypted-data-1' }, |
| 266 | + { walletId: 'wallet-id-2', encryptedPrv: 'encrypted-data-2' }, |
| 267 | + ], |
| 268 | + password: 'password123', |
| 269 | + }); |
| 270 | + |
| 271 | + result.should.be.an.Array(); |
| 272 | + result.should.have.length(1); |
| 273 | + result[0].should.equal('wallet-id-2'); |
| 274 | + }); |
| 275 | + |
| 276 | + it('should correctly process multiple wallet keys', function () { |
| 277 | + // Create a spy on the decrypt method |
| 278 | + const decryptStub = sinon.stub(bitgo, 'decrypt'); |
| 279 | + |
| 280 | + // Configure the stub to throw for specific wallets |
| 281 | + decryptStub |
| 282 | + .withArgs({ input: 'encrypted-data-2', password: 'password123' }) |
| 283 | + .throws(new Error('decryption failed')); |
| 284 | + decryptStub |
| 285 | + .withArgs({ input: 'encrypted-data-4', password: 'password123' }) |
| 286 | + .throws(new Error('decryption failed')); |
| 287 | + decryptStub.returns('success'); // Default return for other calls |
| 288 | + |
| 289 | + const result = bitgo.decryptKeys({ |
| 290 | + walletIdEncryptedKeyPairs: [ |
| 291 | + { walletId: 'wallet-id-1', encryptedPrv: 'encrypted-data-1' }, |
| 292 | + { walletId: 'wallet-id-2', encryptedPrv: 'encrypted-data-2' }, |
| 293 | + { walletId: 'wallet-id-3', encryptedPrv: 'encrypted-data-3' }, |
| 294 | + { walletId: 'wallet-id-4', encryptedPrv: 'encrypted-data-4' }, |
| 295 | + ], |
| 296 | + password: 'password123', |
| 297 | + }); |
| 298 | + |
| 299 | + // Should be called once for each wallet |
| 300 | + decryptStub.callCount.should.equal(4); |
| 301 | + |
| 302 | + // Should include only the failed wallet IDs |
| 303 | + result.should.be.an.Array(); |
| 304 | + result.should.have.length(2); |
| 305 | + result.should.containDeep(['wallet-id-2', 'wallet-id-4']); |
| 306 | + }); |
| 307 | + }); |
128 | 308 | }); |
0 commit comments