Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.use(router.routes())
.use(router.allowedMethods());
// Handler to return bad request for all unhandled paths.
app.use((ctx, _next) => {
ctx.response.status = 400;
});
const port = process.env.SIDETREE_BITCOIN_PORT || config.port;
// initialize the blockchain service and kick-off background tasks
let server: any;
let blockchainService: SidetreeBitcoinProcessor;
try {
blockchainService = new SidetreeBitcoinProcessor(config);
// SIDETREE_TEST_MODE enables unit testing of this file by bypassing blockchain service initialization.
if (process.env.SIDETREE_TEST_MODE === 'true') {
server = app.listen(port);
} else {
blockchainService.initialize()
.then(() => {
server = app.listen(port, () => {
console.log(`Sidetree-Bitcoin node running on port: ${port}`);
});
})
.catch((error) => {
console.error(`Sidetree-Bitcoin node initialization failed with error: ${error}`);
process.exit(1);
});
}
const setKoaResponse = (response: SidetreeResponseModel, koaResponse: Koa.Response) => {
koaResponse.status = SidetreeResponse.toHttpStatus(response.status);
if (response.body) {
koaResponse.set('Content-Type', 'application/json');
koaResponse.body = response.body;
} else {
// Need to set the body explicitly to empty string, else koa will echo the request as the response.
koaResponse.body = '';
}
};
const setKoaResponse = (response: SidetreeResponseModel, koaResponse: Koa.Response, contentType?: string) => {
koaResponse.status = SidetreeResponse.toHttpStatus(response.status);
if (contentType) {
koaResponse.set('Content-Type', contentType);
} else {
koaResponse.set('Content-Type', 'application/json');
}
if (response.body) {
koaResponse.body = response.body;
} else {
// Need to set the body explicitly, otherwise Koa will return HTTP 204.
koaResponse.body = '';
}
};
SidetreeCore,
SidetreeResponse,
SidetreeResponseModel
} from '@decentralized-identity/sidetree';
import { ProtocolVersionModel } from '@decentralized-identity/sidetree/dist/lib/core/VersionManager';
/** Configuration used by this server. */
interface ServerConfig extends SidetreeConfig {
/** Port to be used by the server. */
port: number;
}
const config: ServerConfig = require('../json/core-config.json');
const protocolVersions: ProtocolVersionModel[] = require('../json/core-protocol-versioning.json');
const sidetreeCore = new SidetreeCore(config, protocolVersions);
const app = new Koa();
// Raw body parser.
app.use(async (ctx, next) => {
ctx.body = await getRawBody(ctx.req);
await next();
});
const router = new Router();
router.post('/did', async (ctx, _next) => {
const response = await sidetreeCore.handleOperationRequest(ctx.body);
setKoaResponse(response, ctx.response);
});
router.get('/did/:didOrDidDocument', async (ctx, _next) => {
const response = await sidetreeCore.handleResolveRequest(ctx.params.didOrDidDocument);
// Testing commiting and pushing branches
import {
SidetreeIpfsService,
SidetreeResponse,
SidetreeResponseModel
} from '@decentralized-identity/sidetree';
const config: {
/** Port number used by the service. */
port: number;
/** Timeout for fetch request. */
fetchTimeoutInSeconds: number;
} = require('../json/ipfs-config.json');
const requestHandler = new SidetreeIpfsService(config.fetchTimeoutInSeconds);
const app = new Koa();
// Raw body parser.
app.use(async (ctx, next) => {
ctx.body = await getRawBody(ctx.req);
await next();
});
const router = new Router();
router.get('/version', async (ctx, _next) => {
const response = await requestHandler.handleGetVersionRequest();
setKoaResponse(response, ctx.response);
});
router.get('/:hash', async (ctx, _next) => {
server = app.listen(port);
} else {
blockchainService.initialize()
.then(() => {
server = app.listen(port, () => {
console.log(`Sidetree-Bitcoin node running on port: ${port}`);
});
})
.catch((error) => {
console.error(`Sidetree-Bitcoin node initialization failed with error: ${error}`);
process.exit(1);
});
}
} catch (error) {
console.log('Is bitcoinWalletImportString valid? Consider using testnet key generated below:');
console.log(SidetreeBitcoinProcessor.generatePrivateKey('testnet'));
process.exit(1);
}
console.info('Sidetree bitcoin service configuration:');
console.info(config);
export {
server,
blockchainService
};