-
Notifications
You must be signed in to change notification settings - Fork 0
feat(mbe): accelerate utxo #55
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
5 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,286 @@ | ||
| import 'should'; | ||
| import sinon from 'sinon'; | ||
| import * as request from 'supertest'; | ||
| import nock from 'nock'; | ||
| import { app as expressApp } from '../../../masterExpressApp'; | ||
| import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types'; | ||
| import { Environments, Wallet } from '@bitgo/sdk-core'; | ||
|
|
||
| describe('POST /api/:coin/wallet/:walletId/accelerate', () => { | ||
| let agent: request.SuperAgentTest; | ||
| const coin = 'tbtc'; | ||
| const walletId = 'test-wallet-id'; | ||
| const accessToken = 'test-access-token'; | ||
| const bitgoApiUrl = Environments.test.uri; | ||
| const enclavedExpressUrl = 'https://test-enclaved-express.com'; | ||
|
|
||
| before(() => { | ||
| nock.disableNetConnect(); | ||
| nock.enableNetConnect('127.0.0.1'); | ||
|
|
||
| const config: MasterExpressConfig = { | ||
| appMode: AppMode.MASTER_EXPRESS, | ||
| port: 0, // Let OS assign a free port | ||
| bind: 'localhost', | ||
| timeout: 30000, | ||
| logFile: '', | ||
| env: 'test', | ||
| disableEnvCheck: true, | ||
| authVersion: 2, | ||
| enclavedExpressUrl: enclavedExpressUrl, | ||
| enclavedExpressCert: 'test-cert', | ||
| tlsMode: TlsMode.DISABLED, | ||
| mtlsRequestCert: false, | ||
| allowSelfSigned: true, | ||
| }; | ||
|
|
||
| const app = expressApp(config); | ||
| agent = request.agent(app); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('should accelerate transaction by calling the enclaved express service', async () => { | ||
| // Mock wallet get request | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: walletId, | ||
| type: 'cold', | ||
| subType: 'onPrem', | ||
| keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], | ||
| }); | ||
|
|
||
| // Mock keychain get request | ||
| const keychainGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/key/user-key-id`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: 'user-key-id', | ||
| pub: 'xpub_user', | ||
| }); | ||
|
|
||
| // Mock accelerateTransaction | ||
| const accelerateTransactionStub = sinon | ||
| .stub(Wallet.prototype, 'accelerateTransaction') | ||
| .resolves({ | ||
| txid: 'accelerated-tx-id', | ||
| tx: 'accerated-transaction-hex', | ||
| status: 'signed', | ||
| }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'user', | ||
| pubkey: 'xpub_user', | ||
| cpfpTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| cpfpFeeRate: 50, | ||
| maxFee: 10000, | ||
| }); | ||
|
|
||
| response.status.should.equal(200); | ||
| response.body.should.have.property('txid', 'accelerated-tx-id'); | ||
| response.body.should.have.property('status', 'signed'); | ||
|
|
||
| walletGetNock.done(); | ||
| keychainGetNock.done(); | ||
| sinon.assert.calledOnce(accelerateTransactionStub); | ||
| }); | ||
|
|
||
| it('should handle acceleration with backup key signing', async () => { | ||
| // Mock wallet get request | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: walletId, | ||
| type: 'cold', | ||
| subType: 'onPrem', | ||
| keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], | ||
| }); | ||
|
|
||
| // Mock keychain get request for backup key | ||
| const keychainGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/key/backup-key-id`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: 'backup-key-id', | ||
| pub: 'xpub_backup', | ||
| }); | ||
|
|
||
| // Mock accelerateTransaction | ||
| const accelerateTransactionStub = sinon | ||
| .stub(Wallet.prototype, 'accelerateTransaction') | ||
| .resolves({ | ||
| txid: 'accelerated-tx-id', | ||
| status: 'signed', | ||
| tx: 'accelerated-transaction-hex', | ||
| }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'backup', | ||
| pubkey: 'xpub_backup', | ||
| rbfTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| feeMultiplier: 1.5, | ||
| }); | ||
|
|
||
| response.status.should.equal(200); | ||
| response.body.should.have.property('txid', 'accelerated-tx-id'); | ||
|
|
||
| walletGetNock.done(); | ||
| keychainGetNock.done(); | ||
| sinon.assert.calledOnce(accelerateTransactionStub); | ||
| }); | ||
|
|
||
| it('should throw error when wallet not found', async () => { | ||
| // Mock wallet get request to return 404 | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(404, { error: 'Wallet not found' }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'user', | ||
| pubkey: 'xpub_user', | ||
| cpfpTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| }); | ||
|
|
||
| response.status.should.equal(500); | ||
| response.body.should.have.property('error'); | ||
|
|
||
| walletGetNock.done(); | ||
| }); | ||
|
|
||
| it('should throw error when signing keychain not found', async () => { | ||
| // Mock wallet get request | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: walletId, | ||
| type: 'cold', | ||
| subType: 'onPrem', | ||
| keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], | ||
| }); | ||
|
|
||
| // Mock keychain get request to return 404 | ||
| const keychainGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/key/user-key-id`) | ||
| .matchHeader('any', () => true) | ||
| .reply(404, { error: 'Keychain not found' }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'user', | ||
| pubkey: 'xpub_user', | ||
| cpfpTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| }); | ||
|
|
||
| response.status.should.equal(500); | ||
| response.body.should.have.property('error'); | ||
|
|
||
| walletGetNock.done(); | ||
| keychainGetNock.done(); | ||
| }); | ||
|
|
||
| it('should throw error when provided pubkey does not match wallet keychain', async () => { | ||
| // Mock wallet get request | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: walletId, | ||
| type: 'cold', | ||
| subType: 'onPrem', | ||
| keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], | ||
| }); | ||
|
|
||
| // Mock keychain get request | ||
| const keychainGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/key/user-key-id`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: 'user-key-id', | ||
| pub: 'xpub_user', | ||
| }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'user', | ||
| pubkey: 'wrong_pubkey', | ||
| cpfpTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| }); | ||
|
|
||
| response.status.should.equal(500); | ||
| response.body.should.have.property('error'); | ||
|
|
||
| walletGetNock.done(); | ||
| keychainGetNock.done(); | ||
| }); | ||
|
|
||
| it('should handle acceleration with additional parameters', async () => { | ||
| // Mock wallet get request | ||
| const walletGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/wallet/${walletId}`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: walletId, | ||
| type: 'cold', | ||
| subType: 'onPrem', | ||
| keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'], | ||
| }); | ||
|
|
||
| // Mock keychain get request | ||
| const keychainGetNock = nock(bitgoApiUrl) | ||
| .get(`/api/v2/${coin}/key/user-key-id`) | ||
| .matchHeader('any', () => true) | ||
| .reply(200, { | ||
| id: 'user-key-id', | ||
| pub: 'xpub_user', | ||
| }); | ||
|
|
||
| // Mock accelerateTransaction | ||
| const accelerateTransactionStub = sinon | ||
| .stub(Wallet.prototype, 'accelerateTransaction') | ||
| .resolves({ | ||
| txid: 'accelerated-tx-id', | ||
| status: 'signed', | ||
| tx: 'accelerated-transaction-hex', | ||
| }); | ||
|
|
||
| const response = await agent | ||
| .post(`/api/${coin}/wallet/${walletId}/accelerate`) | ||
| .set('Authorization', `Bearer ${accessToken}`) | ||
| .send({ | ||
| source: 'user', | ||
| pubkey: 'xpub_user', | ||
| cpfpTxIds: ['b8a828b98dbf32d9fd1875cbace9640ceb8c82626716b4a64203fdc79bb46d26'], | ||
| cpfpFeeRate: 100, | ||
| maxFee: 20000, | ||
| feeMultiplier: 2.0, | ||
| }); | ||
|
|
||
| response.status.should.equal(200); | ||
| response.body.should.have.property('txid', 'accelerated-tx-id'); | ||
|
|
||
| walletGetNock.done(); | ||
| keychainGetNock.done(); | ||
| sinon.assert.calledOnce(accelerateTransactionStub); | ||
| }); | ||
| }); |
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
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,49 @@ | ||
| import { RequestTracer, KeyIndices } from '@bitgo/sdk-core'; | ||
| import logger from '../../../logger'; | ||
| import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec'; | ||
| import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../../../shared/coinUtils'; | ||
|
|
||
| export async function handleAccelerate( | ||
| req: MasterApiSpecRouteRequest<'v1.wallet.accelerate', 'post'>, | ||
| ) { | ||
| const enclavedExpressClient = req.enclavedExpressClient; | ||
| const reqId = new RequestTracer(); | ||
| const bitgo = req.bitgo; | ||
| const params = req.decoded; | ||
| const walletId = req.params.walletId; | ||
| const coin = req.params.coin; | ||
|
|
||
| const { wallet, signingKeychain } = await getWalletAndSigningKeychain({ | ||
| bitgo, | ||
| coin, | ||
| walletId, | ||
| params, | ||
| reqId, | ||
| KeyIndices, | ||
| }); | ||
|
|
||
| try { | ||
| // Create custom signing function that delegates to EBE | ||
| const customSigningFunction = makeCustomSigningFunction({ | ||
| enclavedExpressClient, | ||
| source: params.source, | ||
| pub: signingKeychain.pub!, | ||
| }); | ||
|
|
||
| // Prepare acceleration parameters | ||
| const accelerationParams = { | ||
| ...params, | ||
| customSigningFunction, | ||
| reqId, | ||
| }; | ||
|
|
||
| // Accelerate transaction | ||
| const result = await wallet.accelerateTransaction(accelerationParams); | ||
|
|
||
| return result; | ||
| } catch (error) { | ||
| const err = error as Error; | ||
| logger.error('Failed to accelerate transaction: %s', err.message); | ||
| throw err; | ||
| } | ||
| } | ||
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 is the only difference i'm seeing
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.
fixed