|
| 1 | +import { |
| 2 | + BitgoMpcGpgPubKeys, |
| 3 | + common, |
| 4 | + ECDSAUtils, |
| 5 | + EddsaUtils, |
| 6 | + EnvironmentName, |
| 7 | + IRequestTracer, |
| 8 | + Wallet, |
| 9 | +} from '@bitgo/sdk-core'; |
| 10 | +import { TestBitGo } from '@bitgo/sdk-test'; |
| 11 | + |
| 12 | +import { BitGo } from '../../../../../src'; |
| 13 | +import * as openpgp from 'openpgp'; |
| 14 | +import nock = require('nock'); |
| 15 | +import assert = require('assert'); |
| 16 | + |
| 17 | +class TestEcdsaMpcv2Utils extends ECDSAUtils.EcdsaMPCv2Utils { |
| 18 | + public async testPickBitgoPubGpgKeyForSigning( |
| 19 | + isMpcv2: boolean, |
| 20 | + reqId?: IRequestTracer, |
| 21 | + enterpriseId?: string |
| 22 | + ): Promise<openpgp.Key> { |
| 23 | + return this.pickBitgoPubGpgKeyForSigning(isMpcv2, reqId, enterpriseId); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class TestEddsaMpcv1Utils extends EddsaUtils { |
| 28 | + public async testPickBitgoPubGpgKeyForSigning( |
| 29 | + isMpcv2: boolean, |
| 30 | + reqId?: IRequestTracer, |
| 31 | + enterpriseId?: string |
| 32 | + ): Promise<openpgp.Key> { |
| 33 | + return this.pickBitgoPubGpgKeyForSigning(isMpcv2, reqId, enterpriseId); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +describe('TSS MPC Pick BitGo GPG Pub Key Utils:', function () { |
| 38 | + const walletId = '5b34252f1bf349930e34020a00000000'; |
| 39 | + const enterpriseId = '6449153a6f6bc20006d66771cdbe15d3'; |
| 40 | + const ecdsaCoinName = 'hteth'; |
| 41 | + const eddsaCoinName = 'tsol'; |
| 42 | + const ecdsaWalletData = { |
| 43 | + id: walletId, |
| 44 | + enterprise: enterpriseId, |
| 45 | + coin: ecdsaCoinName, |
| 46 | + coinSpecific: {}, |
| 47 | + multisigType: 'tss', |
| 48 | + keys: ['key1', 'key2', 'key3'], |
| 49 | + }; |
| 50 | + const eddsaWalletData = { |
| 51 | + id: walletId, |
| 52 | + enterprise: enterpriseId, |
| 53 | + coin: eddsaCoinName, |
| 54 | + coinSpecific: {}, |
| 55 | + multisigType: 'tss', |
| 56 | + keys: ['key1', 'key2', 'key3'], |
| 57 | + }; |
| 58 | + const envs: EnvironmentName[] = ['test', 'staging', 'prod']; |
| 59 | + const ecdsaMpcv2Utils: TestEcdsaMpcv2Utils[] = []; |
| 60 | + const eddsaMpcv1Utils: TestEddsaMpcv1Utils[] = []; |
| 61 | + |
| 62 | + before(async function () { |
| 63 | + nock.cleanAll(); |
| 64 | + for (const env of envs) { |
| 65 | + const bitgoInstance = TestBitGo.decorate(BitGo, { env }); |
| 66 | + bitgoInstance.initializeTestVars(); |
| 67 | + let coinInstance = bitgoInstance.coin(ecdsaCoinName); |
| 68 | + ecdsaMpcv2Utils.push( |
| 69 | + new TestEcdsaMpcv2Utils(bitgoInstance, coinInstance, new Wallet(bitgoInstance, coinInstance, ecdsaWalletData)) |
| 70 | + ); |
| 71 | + coinInstance = bitgoInstance.coin(eddsaCoinName); |
| 72 | + eddsaMpcv1Utils.push( |
| 73 | + new TestEddsaMpcv1Utils(bitgoInstance, coinInstance, new Wallet(bitgoInstance, coinInstance, eddsaWalletData)) |
| 74 | + ); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + beforeEach(async function () { |
| 79 | + for (const env of envs) { |
| 80 | + const bgUrl = common.Environments[env].uri; |
| 81 | + nock(bgUrl).get(`/api/v2/${ecdsaCoinName}/key/key3`).times(envs.length).reply(200, { hsmType: 'onprem' }); |
| 82 | + nock(bgUrl).get(`/api/v2/${eddsaCoinName}/key/key3`).times(envs.length).reply(200, { hsmType: 'nitro' }); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + envs.forEach(async function (env, index) { |
| 87 | + it(`should pick correct Mpcv2 BitGo GPG Pub Key for ${env} env`, async function () { |
| 88 | + const bitgoGpgPubKey = await ecdsaMpcv2Utils[index].testPickBitgoPubGpgKeyForSigning(true); |
| 89 | + bitgoGpgPubKey |
| 90 | + .armor() |
| 91 | + .should.equal(BitgoMpcGpgPubKeys.bitgoMpcGpgPubKeys['mpcv2']['onprem'][env === 'staging' ? 'test' : env]); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + envs.forEach(async function (env, index) { |
| 96 | + it(`should pick correct Mpcv1 BitGo GPG Pub Key for ${env} env`, async function () { |
| 97 | + const bitgoGpgPubKey = await eddsaMpcv1Utils[index].testPickBitgoPubGpgKeyForSigning(false); |
| 98 | + bitgoGpgPubKey |
| 99 | + .armor() |
| 100 | + .should.equal(BitgoMpcGpgPubKeys.bitgoMpcGpgPubKeys['mpcv1']['nitro'][env === 'staging' ? 'test' : env]); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it(`should pick BitGo GPG Pub Key based on enterprise flag for mock env`, async function () { |
| 105 | + const bgUrl = common.Environments['mock'].uri; |
| 106 | + const testBitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); |
| 107 | + const testCoin = testBitgo.coin(ecdsaCoinName); |
| 108 | + const bitgoGPGKey = await openpgp.generateKey({ |
| 109 | + userIDs: [ |
| 110 | + { |
| 111 | + name: 'bitgo', |
| 112 | + |
| 113 | + }, |
| 114 | + ], |
| 115 | + }); |
| 116 | + nock(bgUrl) |
| 117 | + .get(`/api/v2/${ecdsaCoinName}/tss/pubkey`) |
| 118 | + .query({ enterpriseId }) |
| 119 | + .reply(200, { mpcv2PublicKey: bitgoGPGKey.publicKey }); |
| 120 | + const ecdsaMpcv2Util = new TestEcdsaMpcv2Utils( |
| 121 | + testBitgo, |
| 122 | + testCoin, |
| 123 | + new Wallet(testBitgo, testCoin, ecdsaWalletData) |
| 124 | + ); |
| 125 | + const bitgoGpgPubKey = await ecdsaMpcv2Util.testPickBitgoPubGpgKeyForSigning(true, undefined, enterpriseId); |
| 126 | + bitgoGpgPubKey.armor().should.equal(bitgoGPGKey.publicKey); |
| 127 | + }); |
| 128 | + |
| 129 | + it(`should pick BitGo GPG Pub Key based on constants api for mock env if enterprise flag based fetch fails`, async function () { |
| 130 | + nock.cleanAll(); |
| 131 | + const bgUrl = common.Environments['mock'].uri; |
| 132 | + const testBitgo = TestBitGo.decorate(BitGo, { env: 'mock' }); |
| 133 | + const testCoin = testBitgo.coin(ecdsaCoinName); |
| 134 | + const bitgoGPGKey = await openpgp.generateKey({ |
| 135 | + userIDs: [ |
| 136 | + { |
| 137 | + name: 'bitgo', |
| 138 | + |
| 139 | + }, |
| 140 | + ], |
| 141 | + }); |
| 142 | + const constants = { |
| 143 | + mpc: { |
| 144 | + bitgoMPCv2PublicKey: bitgoGPGKey.publicKey, |
| 145 | + bitgoPublicKey: bitgoGPGKey.publicKey, |
| 146 | + }, |
| 147 | + }; |
| 148 | + nock(bgUrl).get('/api/v1/client/constants').times(2).reply(200, { ttl: 3600, constants }); |
| 149 | + const ecdsaMpcv2Util = new TestEcdsaMpcv2Utils( |
| 150 | + testBitgo, |
| 151 | + testCoin, |
| 152 | + new Wallet(testBitgo, testCoin, ecdsaWalletData) |
| 153 | + ); |
| 154 | + const bitgoGpgPubKey = await ecdsaMpcv2Util.testPickBitgoPubGpgKeyForSigning(true, undefined, enterpriseId); |
| 155 | + bitgoGpgPubKey.armor().should.equal(bitgoGPGKey.publicKey); |
| 156 | + }); |
| 157 | + |
| 158 | + it(`should throw an error if config is not available in one of test, staging, or prod`, async function () { |
| 159 | + nock.cleanAll(); |
| 160 | + const testBitgo = TestBitGo.decorate(BitGo, { env: 'test' }); |
| 161 | + const testCoin = testBitgo.coin(ecdsaCoinName); |
| 162 | + const ecdsaMpcv2Util = new TestEcdsaMpcv2Utils( |
| 163 | + testBitgo, |
| 164 | + testCoin, |
| 165 | + new Wallet(testBitgo, testCoin, ecdsaWalletData) |
| 166 | + ); |
| 167 | + await assert.rejects(async () => await ecdsaMpcv2Util.testPickBitgoPubGpgKeyForSigning(true)); |
| 168 | + }); |
| 169 | +}); |
0 commit comments