Skip to content

Commit

Permalink
feat(sdk): adds partially auto-generated SDK
Browse files Browse the repository at this point in the history
The OpenAPI spec is used to auto generate a cross-platform (universal) SDK that can be used to hit the REST API endpoints from both a browser and NodeJS environments.

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed May 19, 2020
1 parent 05a2e0b commit 6527870
Show file tree
Hide file tree
Showing 26 changed files with 848 additions and 22 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
"scripts": {
"configure": "lerna clean --yes && lerna bootstrap && npm run build && node ./tools/generate-api-server-config.js",
"start:api-server": "node ./packages/bif-cmd-api-server/dist/lib/main/typescript/cmd/bif-api.js --config-file=.config.json",
"generate-sdk": "openapi-generator generate -i x.json -g typescript-axios -o packages/bif-sdk/src/main/typescript/generated/openapi/typescript-axios/",
"tsc": "lerna exec --stream --ignore '*/*cockpit' -- tsc --project ./tsconfig.json",
"clean": "lerna exec --stream --ignore '*/*cockpit' -- del-cli dist/**",
"build": "npm-run-all --parallel build:frontend build:backend",
"clean": "lerna exec --stream --ignore '*/*cockpit' -- del-cli dist/** && del-cli packages/bif-sdk/src/main/typescript/generated/openapi/typescript-axios/*",
"build": "npm-run-all build:backend build:frontend",
"build:frontend": "lerna exec --stream --scope '*/*cockpit' -- ng build --prod",
"build:backend": "npm-run-all clean tsc webpack",
"build:backend": "npm-run-all lint clean generate-sdk tsc webpack",
"build:dev:pkg:cmd-api-server": "lerna exec --stream --scope '*/*api-server' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
"build:dev:pkg:sdk": "lerna exec --stream --scope '*/*sdk' -- 'del-cli dist/** && tsc --project ./tsconfig.json && webpack --env=dev --target=node --config ../../webpack.config.js'",
"webpack": "npm-run-all webpack:web:dev webpack:node:dev webpack:web:prod webpack:node:prod",
"webpack:web:prod": "lerna exec --stream --ignore '*/*{cockpit,server}' -- webpack --env=prod --target=web --config ../../webpack.config.js",
"webpack:web:dev": "lerna exec --stream --ignore '*/*{cockpit,server}' -- webpack --env=dev --target=web --config ../../webpack.config.js",
Expand All @@ -19,7 +21,7 @@
"commit": "git-cz",
"setup": "lerna exec --stream --ignore '*/*cockpit' -- rm -rf node_modules package-lock.json",
"lint": "lerna exec --stream --ignore '*/*cockpit' -- cross-env DEBUG= tslint --project tsconfig.json",
"pretest": "npm run lint && npm run build",
"pretest": "npm run build",
"test": "lerna exec --stream --ignore '*/*cockpit' -- tap --timeout=600 src/test/typescript/unit/",
"test-coverage": "lerna exec --stream --ignore '*/*cockpit' -- tap --timeout=600 src/test/typescript/unit/ --cov",
"test-coverage-html": "lerna exec --stream --ignore '*/*cockpit' -- tap --timeout=600 src/test/typescript/unit/ --cov --coverage-report=lcov",
Expand All @@ -29,6 +31,7 @@
"devDependencies": {
"@commitlint/cli": "8.1.0",
"@commitlint/config-conventional": "8.0.0",
"@openapitools/openapi-generator-cli": "1.0.10-4.2.3",
"@types/node-fetch": "2.5.4",
"@types/uuid": "3.4.6",
"commitizen": "4.0.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@ export class CreateConsortiumEndpointV1 {
const consortium: IConsortium = req.body;
const idAlreadyExists = await this.options.storage.has(consortium.id);
if (idAlreadyExists) {
res.json({ success: false, message: `Consortium with ID ${consortium.id} already exists.` });
res.status(400);
res.json({ success: false, message: `Consortium with ID ${consortium.id} already exists.` });
} else {
// FIXME: We need a library handling the crypto, how about NodeJS bindings for Ursa?
const privateKey = this.options.config.get('privateKey');
const privateKeyBytes = Uint8Array.from(Buffer.from(privateKey, 'hex'));
const consortiumJson: string = JSON.stringify(consortium);
const consortiumBytesHash = Uint8Array.from(keccak256.array(consortiumJson));
const signatureWrapper = secp256k1.ecdsaSign(consortiumBytesHash, privateKeyBytes);
const signature = Buffer.from(signatureWrapper.signature).toString('hex');
const consortiumWrapper: IConsortiumWrapper = {
signature,
consortiumJson,
};
const wrapperJson = JSON.stringify(consortiumWrapper);
// tslint:disable-next-line: no-console
await this.options.storage.set(consortium.id, wrapperJson);
res.status(201);
res.json({ success: true, consortiumWrapper });
}
// FIXME: We need a library handling the crypto, how about NodeJS bindings for Ursa?
const privateKey = this.options.config.get('privateKey');
const privateKeyBytes = Uint8Array.from(Buffer.from(privateKey, 'hex'));
const consortiumJson: string = JSON.stringify(consortium);
const consortiumBytesHash = Uint8Array.from(keccak256.array(consortiumJson));
const signatureWrapper = secp256k1.ecdsaSign(consortiumBytesHash, privateKeyBytes);
const signature = Buffer.from(signatureWrapper.signature).toString('hex');
const consortiumWrapper: IConsortiumWrapper = {
signature,
consortiumJson,
};
const wrapperJson = JSON.stringify(consortiumWrapper);
// tslint:disable-next-line: no-console
await this.options.storage.set(consortium.id, wrapperJson);
res.json({ success: true, consortiumWrapper });
res.status(201);
}
}
1 change: 1 addition & 0 deletions packages/bif-cockpit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@angular/router": "8.2.14",
"@capacitor/core": "1.5.1",
"@hyperledger-labs/bif-common": "0.2.0",
"@hyperledger-labs/bif-sdk": "0.2.0",
"@ionic-native/core": "5.0.0",
"@ionic-native/splash-screen": "5.0.0",
"@ionic-native/status-bar": "5.0.0",
Expand Down
18 changes: 18 additions & 0 deletions packages/bif-cockpit/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { LoggerProvider, Logger } from '@hyperledger-labs/bif-common';
import { DefaultApi, Configuration } from '@hyperledger-labs/bif-sdk';

@Component({
selector: 'app-root',
Expand Down Expand Up @@ -72,5 +73,22 @@ export class AppComponent implements OnInit {
if (path !== undefined) {
this.selectedIndex = this.appPages.findIndex(page => page.title.toLowerCase() === path.toLowerCase());
}
this.testApi();
}

async testApi(): Promise<void> {
const BIF_API_HOST = 'http://localhost:4000';
const configuration = new Configuration({ basePath: BIF_API_HOST,});
const api = new DefaultApi(configuration);
const response = await api.apiV1ConsortiumPost({
configurationEndpoint: 'domain-and-an-http-endpoint',
id: 'asdf',
name: 'asdf',
bifNodes: [
{
host: 'BIF-NODE-HOST-1', publicKey: 'FAKE-PUBLIC-KEY'
}
]
});
}
}
9 changes: 9 additions & 0 deletions packages/bif-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@hyperledger-labs/bif-sdk`

> TODO: description
## Usage

```
// TODO: DEMONSTRATE API
```
37 changes: 37 additions & 0 deletions packages/bif-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions packages/bif-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@hyperledger-labs/bif-sdk",
"version": "0.2.0",
"description": "Universal library used by both front end and back end components of BIF. Aims to be a developer swiss army knife.",
"main": "dist/bif-sdk.node.umd.js",
"mainMinified": "dist/bif-sdk.node.umd.min.js",
"browser": "dist/bif-sdk.web.umd.js",
"browserMinified": "dist/bif-sdk.web.umd.min.js",
"module": "dist/lib/main/typescript/index.js",
"types": "dist/types/main/typescript/index.d.ts",
"files": [
"dist/*"
],
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=10",
"npm": ">=6"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hyperledger-labs/blockchain-integration-framework.git"
},
"keywords": [
"Hyperledger",
"Blockchain",
"Interoperability",
"Integration"
],
"author": {
"name": "Peter Somogyvari",
"email": "peter.somogyvari@accenture.com",
"url": "https://accenture.com"
},
"contributors": [
{
"name": "Please add yourself to the list of contributors",
"email": "your.name@example.com",
"url": "https://example.com"
}
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/hyperledger-labs/blockchain-integration-framework/issues"
},
"homepage": "https://github.com/hyperledger-labs/blockchain-integration-framework#readme",
"dependencies": {
"@hyperledger-labs/bif-common": "0.2.0",
"axios": "0.19.2"
}
}
19 changes: 19 additions & 0 deletions packages/bif-sdk/src/main/typescript/api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Logger, LoggerProvider } from '@hyperledger-labs/bif-common';

export interface IApiClientOptions {
apiHost: string;
apiPort: number;
}

export class ApiClient {

private readonly log: Logger;

constructor(public readonly options: IApiClientOptions) {
this.log = LoggerProvider.getOrCreate({ label: 'api-client ' });
}

public async call(): Promise<void> {
this.log.debug(`call()`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiClient } from "../api-client";

export interface IConsortiumServiceOptions {
apiClient: ApiClient;
}

export class ConsortiumService {
constructor(public readonly options: IConsortiumServiceOptions) {
}

async create(): Promise<void> {
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wwwroot/*.js
node_modules
typings
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.2.3
Loading

0 comments on commit 6527870

Please sign in to comment.