Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 159 additions & 2 deletions masterBitgoExpress.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,171 @@
}
}
},
"202": {
"description": "Accepted",
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string"
},
"details": {
"type": "string"
}
},
"required": [
"error",
"details"
]
}
}
}
}
}
}
},
"/api/{coin}/wallet/{walletId}/consolidateunspents": {
"post": {
"parameters": [
{
"name": "walletId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "coin",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"pubkey": {
"type": "string"
},
"source": {
"type": "string",
"enum": [
"user",
"backup"
]
},
"walletPassphrase": {
"type": "string"
},
"feeRate": {
"type": "number"
},
"maxFeeRate": {
"type": "number"
},
"maxFeePercentage": {
"type": "number"
},
"feeTxConfirmTarget": {
"type": "number"
},
"bulk": {
"type": "boolean"
},
"minValue": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
}
]
},
"maxValue": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
}
]
},
"minHeight": {
"type": "number"
},
"minConfirms": {
"type": "number"
},
"enforceMinConfirmsForChange": {
"type": "boolean"
},
"limit": {
"type": "number"
},
"numUnspentsToMake": {
"type": "number"
},
"targetAddress": {
"type": "string"
},
"txFormat": {
"type": "string",
"enum": [
"legacy",
"psbt",
"psbt-lite"
]
}
},
"required": [
"pubkey",
"source"
]
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"tx": {
"type": "string"
},
"txid": {
"type": "string"
}
},
"required": [
"tx",
"txid"
]
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test:watch": "mocha --require ts-node/register --watch 'src/**/__tests__/**/*.test.ts'",
"test:coverage": "nyc mocha --require ts-node/register 'src/**/__tests__/**/*.test.ts'",
"lint": "eslint --quiet .",
"lint:fix": "eslint --quiet . --fix",
"generate-test-ssl": "openssl req -x509 -newkey rsa:2048 -keyout test-ssl-key.pem -out test-ssl-cert.pem -days 365 -nodes -subj '/CN=localhost'",
"generate:openapi:masterExpress": "npx @api-ts/openapi-generator --name @bitgo/master-bitgo-express ./src/api/master/routers/index.ts > masterBitgoExpress.json",
"container:build": "podman build -t bitgo-onprem-express ."
Expand Down
192 changes: 192 additions & 0 deletions src/__tests__/api/master/consolidateUnspents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
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/consolidateunspents', () => {
let agent: request.SuperAgentTest;
const coin = 'btc';
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,
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 return transfer, txid, tx, and status on success', async () => {
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'],
});

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 mockResult = {
transfer: {
entries: [
{ address: 'tb1qu...', value: -4000 },
{ address: 'tb1qle...', value: -4000 },
{ address: 'tb1qtw...', value: 2714, isChange: true },
],
id: '685ac2f3c2f8a2a5d9cc18d3593f1751',
coin: 'tbtc',
wallet: '685abbf19ca95b79f88e0b41d9337109',
txid: '239d143cdfc6d6c83a935da4f3d610b2364a956c7b6dcdc165eb706f62c4432a',
status: 'signed',
},
txid: '239d143cdfc6d6c83a935da4f3d610b2364a956c7b6dcdc165eb706f62c4432a',
tx: '01000000000102580b...',
status: 'signed',
};

const consolidateUnspentsStub = sinon
.stub(Wallet.prototype, 'consolidateUnspents')
.resolves(mockResult);

const response = await agent
.post(`/api/${coin}/wallet/${walletId}/consolidateunspents`)
.set('Authorization', `Bearer ${accessToken}`)
.send({
source: 'user',
pubkey: 'xpub_user',
feeRate: 1000,
});

response.status.should.equal(200);
response.body.should.have.property('transfer');
response.body.should.have.property('txid', mockResult.txid);
response.body.should.have.property('tx', mockResult.tx);
response.body.should.have.property('status', mockResult.status);
response.body.transfer.should.have.property('txid', mockResult.transfer.txid);
response.body.transfer.should.have.property('status', mockResult.transfer.status);
response.body.transfer.should.have.property('entries').which.is.Array();

walletGetNock.done();
keychainGetNock.done();
sinon.assert.calledOnce(consolidateUnspentsStub);
});

it('should return error, name, and details on failure', async () => {
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'],
});

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 mockError = {
error: 'Internal Server Error',
name: 'ApiResponseError',
details:
'There are too few unspents that meet the given parameters to consolidate (1 available).',
};

const consolidateUnspentsStub = sinon
.stub(Wallet.prototype, 'consolidateUnspents')
.throws(Object.assign(new Error(mockError.details), mockError));

const response = await agent
.post(`/api/${coin}/wallet/${walletId}/consolidateunspents`)
.set('Authorization', `Bearer ${accessToken}`)
.send({
source: 'user',
pubkey: 'xpub_user',
feeRate: 1000,
});

response.status.should.equal(500);
response.body.should.have.property('error', mockError.error);
response.body.should.have.property('name', mockError.name);
response.body.should.have.property('details', mockError.details);

walletGetNock.done();
keychainGetNock.done();
sinon.assert.calledOnce(consolidateUnspentsStub);
});

it('should throw error when provided pubkey does not match wallet keychain', async () => {
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'],
});

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}/consolidateunspents`)
.set('Authorization', `Bearer ${accessToken}`)
.send({
source: 'user',
pubkey: 'wrong_pubkey',
feeRate: 1000,
});

response.status.should.equal(500);

walletGetNock.done();
keychainGetNock.done();
});
});
Loading