-
Notifications
You must be signed in to change notification settings - Fork 1
feat(mbe): add MPCv2 signing support to master/sendMany #57
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| import 'should'; | ||
| import nock from 'nock'; | ||
| import * as sinon from 'sinon'; | ||
| import { | ||
| BitGoBase, | ||
| Wallet, | ||
| TxRequest, | ||
| IRequestTracer, | ||
| TxRequestVersion, | ||
| Environments, | ||
| RequestTracer, | ||
| EcdsaMPCv2Utils, | ||
| openpgpUtils, | ||
| SignatureShareRecord, | ||
| SignatureShareType, | ||
| TransactionState, | ||
| } from '@bitgo/sdk-core'; | ||
| import { EnclavedExpressClient } from '../../../../src/api/master/clients/enclavedExpressClient'; | ||
| import { handleEcdsaSigning } from '../../../../src/api/master/handlers/ecdsa'; | ||
| import { BitGo } from 'bitgo'; | ||
| import { readKey } from 'openpgp'; | ||
|
|
||
| describe('Ecdsa Signing Handler', () => { | ||
| let bitgo: BitGoBase; | ||
| let wallet: Wallet; | ||
| let enclavedExpressClient: EnclavedExpressClient; | ||
| let reqId: IRequestTracer; | ||
| const bitgoApiUrl = Environments.local.uri; | ||
| const enclavedExpressUrl = 'http://enclaved.invalid'; | ||
| const coin = 'hteth'; // Use hteth for ECDSA testing | ||
| const walletId = 'test-wallet-id'; | ||
|
|
||
| before(() => { | ||
| // Disable all real network connections | ||
| nock.disableNetConnect(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| bitgo = new BitGo({ env: 'local' }); | ||
| wallet = { | ||
| id: () => 'test-wallet-id', | ||
| baseCoin: { | ||
| getMPCAlgorithm: () => 'ecdsa', | ||
| }, | ||
| multisigTypeVersion: () => 2, | ||
| } as unknown as Wallet; | ||
| enclavedExpressClient = new EnclavedExpressClient( | ||
| { | ||
| enclavedExpressUrl, | ||
| enclavedExpressCert: 'dummy-cert', | ||
| tlsMode: 'disabled', | ||
| allowSelfSigned: true, | ||
| } as any, | ||
| coin, | ||
| ); | ||
| reqId = new RequestTracer(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| // Re-enable network connections after tests | ||
| nock.enableNetConnect(); | ||
| }); | ||
|
|
||
| it('should successfully sign an ECDSA MPCv2 transaction', async () => { | ||
| const txRequest: TxRequest = { | ||
| txRequestId: 'test-tx-request-id', | ||
| apiVersion: '2.0.0' as TxRequestVersion, | ||
| enterpriseId: 'test-enterprise-id', | ||
| transactions: [], | ||
| state: 'pendingUserSignature', | ||
| walletId: 'test-wallet-id', | ||
| walletType: 'hot', | ||
| version: 2, | ||
| date: new Date().toISOString(), | ||
| userId: 'test-user-id', | ||
| intent: {}, | ||
| policiesChecked: true, | ||
| unsignedTxs: [], | ||
| latest: true, | ||
| }; | ||
| const userPubKey = 'test-user-pub-key'; | ||
|
|
||
| const bitgoGpgKey = await openpgpUtils.generateGPGKeyPair('secp256k1'); | ||
| const pgpKey = await readKey({ armoredKey: bitgoGpgKey.publicKey }); | ||
| sinon.stub(EcdsaMPCv2Utils.prototype, 'getBitgoMpcv2PublicGpgKey').resolves(pgpKey); | ||
|
|
||
| // Mock getTxRequest call | ||
| const getTxRequestNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/wallet/${walletId}/txrequests`) | ||
| .query({ txRequestIds: 'test-tx-request-id', latest: true }) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| txRequests: [txRequest], | ||
| }); | ||
|
|
||
| // Mock sendSignatureShareV2 calls for each round | ||
| const round1SignatureShare: SignatureShareRecord = { | ||
| from: SignatureShareType.USER, | ||
| to: SignatureShareType.BITGO, | ||
| share: JSON.stringify({ | ||
| type: 'round1Input', | ||
| data: { | ||
| msg1: { | ||
| from: 1, | ||
| message: 'round1-message', | ||
| }, | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| const round1TxRequest: TxRequest = { | ||
| ...txRequest, | ||
| transactions: [ | ||
| { | ||
| unsignedTx: { | ||
| derivationPath: 'm/0', | ||
| signableHex: 'testMessage', | ||
| serializedTxHex: 'testMessage', | ||
| }, | ||
| signatureShares: [round1SignatureShare], | ||
| state: 'pendingSignature' as TransactionState, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const sendSignatureShareV2Round1Nock = nock(bitgoApiUrl) | ||
| .post(`/api/v2/wallet/${walletId}/txrequests/test-tx-request-id/transactions/0/sign`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| txRequest: round1TxRequest, | ||
| }); | ||
|
|
||
| const round2SignatureShare: SignatureShareRecord = { | ||
| from: SignatureShareType.USER, | ||
| to: SignatureShareType.BITGO, | ||
| share: JSON.stringify({ | ||
| type: 'round2Input', | ||
| data: { | ||
| msg2: { | ||
| from: 1, | ||
| to: 3, | ||
| encryptedMessage: 'round2-encrypted-message', | ||
| signature: 'round2-signature', | ||
| }, | ||
| msg3: { | ||
| from: 1, | ||
| to: 3, | ||
| encryptedMessage: 'round3-encrypted-message', | ||
| signature: 'round3-signature', | ||
| }, | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| const round2TxRequest: TxRequest = { | ||
| ...round1TxRequest, | ||
| transactions: [ | ||
| { | ||
| ...round1TxRequest.transactions![0], | ||
| signatureShares: [round1SignatureShare, round2SignatureShare], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const sendSignatureShareV2Round2Nock = nock(bitgoApiUrl) | ||
| .post(`/api/v2/wallet/${walletId}/txrequests/test-tx-request-id/transactions/0/sign`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| txRequest: round2TxRequest, | ||
| }); | ||
|
|
||
| const round3SignatureShare: SignatureShareRecord = { | ||
| from: SignatureShareType.USER, | ||
| to: SignatureShareType.BITGO, | ||
| share: JSON.stringify({ | ||
| type: 'round3Input', | ||
| data: { | ||
| msg4: { | ||
| from: 1, | ||
| message: 'round4-message', | ||
| signature: 'round4-signature', | ||
| signatureR: 'round4-signature-r', | ||
| }, | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| const sendSignatureShareV2Round3Nock = nock(bitgoApiUrl) | ||
| .post(`/api/v2/wallet/${walletId}/txrequests/test-tx-request-id/transactions/0/sign`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| txRequest: { | ||
| ...round2TxRequest, | ||
| transactions: [ | ||
| { | ||
| ...round2TxRequest.transactions![0], | ||
| signatureShares: [round1SignatureShare, round2SignatureShare, round3SignatureShare], | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| // Mock sendTxRequest call | ||
| const sendTxRequestNock = nock(bitgoApiUrl) | ||
| .post(`/api/v2/wallet/${walletId}/txrequests/test-tx-request-id/transactions/0/send`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| ...txRequest, | ||
| state: 'signed', | ||
| }); | ||
|
|
||
| // Mock MPCv2 Round 1 signing | ||
| const signMpcV2Round1NockEbe = nock(enclavedExpressUrl) | ||
| .post(`/api/${coin}/mpc/sign/mpcv2round1`) | ||
| .reply(200, { | ||
| signatureShareRound1: round1SignatureShare, | ||
| userGpgPubKey: bitgoGpgKey.publicKey, | ||
| encryptedRound1Session: 'encrypted-round1-session', | ||
| encryptedUserGpgPrvKey: 'encrypted-user-gpg-prv-key', | ||
| encryptedDataKey: 'test-encrypted-data-key', | ||
| }); | ||
|
|
||
| // Mock MPCv2 Round 2 signing | ||
| const signMpcV2Round2NockEbe = nock(enclavedExpressUrl) | ||
| .post(`/api/${coin}/mpc/sign/mpcv2round2`) | ||
| .reply(200, { | ||
| signatureShareRound2: round2SignatureShare, | ||
| encryptedRound2Session: 'encrypted-round2-session', | ||
| }); | ||
|
|
||
| // Mock MPCv2 Round 3 signing | ||
| const signMpcV2Round3NockEbe = nock(enclavedExpressUrl) | ||
| .post(`/api/${coin}/mpc/sign/mpcv2round3`) | ||
| .reply(200, { | ||
| signatureShareRound3: round3SignatureShare, | ||
| }); | ||
|
|
||
| const result = await handleEcdsaSigning( | ||
| bitgo, | ||
| wallet, | ||
| txRequest.txRequestId, | ||
| enclavedExpressClient, | ||
| 'user', | ||
| userPubKey, | ||
| reqId, | ||
| ); | ||
|
|
||
| result.should.eql({ | ||
| ...txRequest, | ||
| state: 'signed', | ||
| }); | ||
|
|
||
| getTxRequestNock.done(); | ||
| sendSignatureShareV2Round1Nock.done(); | ||
| sendSignatureShareV2Round2Nock.done(); | ||
| sendSignatureShareV2Round3Nock.done(); | ||
| sendTxRequestNock.done(); | ||
| signMpcV2Round1NockEbe.done(); | ||
| signMpcV2Round2NockEbe.done(); | ||
| signMpcV2Round3NockEbe.done(); | ||
| }); | ||
| }); | ||
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
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.
this should be calling the express endpoint directly.