Skip to content

Commit

Permalink
Merge branch 'main' into dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Nicko Guyer <nicko.guyer@kaleido.io>
  • Loading branch information
nguyer committed Mar 26, 2024
2 parents 565e5c8 + a3ff4bb commit 5deeeea
Show file tree
Hide file tree
Showing 16 changed files with 913 additions and 1,183 deletions.
2 changes: 1 addition & 1 deletion server/jest.config.js → server/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
Expand Down
1,754 changes: 643 additions & 1,111 deletions server/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.1",
"@types/jest": "^29.5.12",
"@types/multer": "^1.4.7",
"@types/supertest": "^2.0.12",
"@types/swagger-ui-express": "^4.1.3",
Expand All @@ -54,14 +54,14 @@
"eslint": "^8.12.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^26.1.3",
"jest": "^27.5.1",
"jest": "^29.7.0",
"prettier": "^2.6.1",
"rimraf": "^3.0.2",
"supertest": "^6.2.2",
"swagger-jsdoc": "^6.1.0",
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"ts-node-dev": "^1.1.8",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.6.3",
"underscore": "^1.13.2"
}
Expand Down
52 changes: 29 additions & 23 deletions server/src/controllers/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
QueryParam,
} from 'routing-controllers';
import { getFireflyClient } from '../clients/fireflySDKWrapper';
import { formatTemplate, quoteAndEscape as q } from '../utils';
import { formatTemplate, getFireflyOptions, quoteAndEscape as q } from '../utils';
import {
AsyncResponse,
ContractAPI,
Expand Down Expand Up @@ -42,7 +42,7 @@ export class ContractsController {
input: { abi: body.schema },
})
: body.schema;
const result = await firefly.createContractInterface(ffi);
const result = await firefly.createContractInterface(ffi, getFireflyOptions(body.publish));
return { type: 'message', id: result.message };
}

Expand All @@ -54,16 +54,19 @@ export class ContractsController {
): Promise<AsyncResponse> {
const firefly = getFireflyClient(namespace);
// See ContractsTemplateController and keep template code up to date.
const api = await firefly.createContractAPI({
name: body.name,
interface: {
name: body.interfaceName,
version: body.interfaceVersion,
},
location: {
address: body.address,
const api = await firefly.createContractAPI(
{
name: body.name,
interface: {
name: body.interfaceName,
version: body.interfaceVersion,
},
location: {
address: body.address,
},
},
});
getFireflyOptions(body.publish),
);
return { type: 'message', id: api.message };
}

Expand All @@ -75,17 +78,20 @@ export class ContractsController {
): Promise<AsyncResponse> {
const firefly = getFireflyClient(namespace);
// See ContractsTemplateController and keep template code up to date.
const api = await firefly.createContractAPI({
name: body.name,
interface: {
name: body.interfaceName,
version: body.interfaceVersion,
},
location: {
chaincode: body.chaincode,
channel: body.channel,
const api = await firefly.createContractAPI(
{
name: body.name,
interface: {
name: body.interfaceName,
version: body.interfaceVersion,
},
location: {
chaincode: body.chaincode,
channel: body.channel,
},
},
});
getFireflyOptions(body.publish),
);
return { type: 'message', id: api.message };
}

Expand Down Expand Up @@ -189,7 +195,7 @@ export class ContractsTemplateController {
abi: <%= ${q('schema', { isObject: true, truncate: true })} %>,
},
})<% } else { %><%= ${q('schema', { isObject: true, truncate: true })} %><% } %>;
const result = await firefly.createContractInterface(ffi);
const result = await firefly.createContractInterface(ffi<% if (publish !== undefined) { %>, { publish: <%= publish %> }<% }%>);
return { type: 'message', id: result.message };
`);
}
Expand All @@ -208,7 +214,7 @@ export class ContractsTemplateController {
chaincode: <%= ${q('chaincode')} %>,
channel: <%= ${q('channel')} %>,<% } %>
},
});
}<% if (publish !== undefined) { %>, { publish: <%= publish %> }<% }%>);
return { type: 'message', id: api.message };
`);
}
Expand Down
22 changes: 13 additions & 9 deletions server/src/controllers/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FF_MESSAGES,
formatTemplate,
getBroadcastMessageBody,
getFireflyOptions,
getPrivateMessageBody,
mapPool,
quoteAndEscape as q,
Expand Down Expand Up @@ -61,15 +62,18 @@ export class TokensController {
): Promise<AsyncResponse> {
const firefly = getFireflyClient(namespace);
// See TokensTemplateController and keep template code up to date.
const pool = await firefly.createTokenPool({
name: body.name,
symbol: body.symbol,
type: body.type,
config: {
address: body.address,
blockNumber: body.blockNumber,
const pool = await firefly.createTokenPool(
{
name: body.name,
symbol: body.symbol,
type: body.type,
config: {
address: body.address,
blockNumber: body.blockNumber,
},
},
});
getFireflyOptions(body.publish),
);
return { type: 'token_pool', id: pool.id };
}

Expand Down Expand Up @@ -264,7 +268,7 @@ export class TokensTemplateController {
<% print('address: ' + ${q('address')} + ',') } %>
blockNumber: <%= ${q('blockNumber')} %>,
}
});
}<% if (publish !== undefined) { %>, { publish: <%= publish %> }<% }%>);
return { type: 'token_pool', id: pool.id };
`);
}
Expand Down
12 changes: 12 additions & 0 deletions server/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export class TokenPoolInput {
@IsString()
@IsOptional()
blockNumber?: string;

@IsBoolean()
@IsOptional()
publish?: boolean;
}

export class TokenPool extends TokenPoolInput {
Expand Down Expand Up @@ -235,6 +239,10 @@ export class ContractInterface {

@IsDefined()
schema: any;

@IsBoolean()
@IsOptional()
publish?: boolean;
}

export class ContractInterfaceEvent {
Expand Down Expand Up @@ -263,6 +271,10 @@ export class ContractAPI {
@IsString()
@IsOptional()
chaincode?: string;

@IsBoolean()
@IsOptional()
publish?: boolean;
}

export class ContractAPIURLs {
Expand Down
13 changes: 12 additions & 1 deletion server/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as http from 'http';
import { Duplex } from 'stream';
import { WebSocketServer } from 'ws';
import { FireFlyDataRequest, FireFlyTokenPoolResponse } from '@hyperledger/firefly-sdk';
import {
FireFlyCreateOptions,
FireFlyDataRequest,
FireFlyTokenPoolResponse,
} from '@hyperledger/firefly-sdk';
import stripIndent = require('strip-indent');
import { BroadcastValue, PrivateValue } from './interfaces';

Expand All @@ -17,6 +21,13 @@ export enum FF_MESSAGES {
GROUP_INIT = 'groupinit',
}

export function getFireflyOptions(publish?: boolean): FireFlyCreateOptions {
if (publish === undefined) {
return {};
}
return { publish: publish };
}

export class WebsocketHandler {
websockets = new Map<string, WebSocketServer>();

Expand Down
42 changes: 42 additions & 0 deletions server/test/contracts.template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Templates: Smart Contracts', () => {
name: 'simple-storage',
version: '1.0',
schema: [{ name: 'method1' }, { name: 'event1' }],
publish: undefined,
}),
).toBe(
formatTemplate(`
Expand All @@ -36,6 +37,7 @@ describe('Templates: Smart Contracts', () => {
compiled({
format: 'ffi',
schema: { methods: [{ name: 'method1' }] },
publish: undefined,
}),
).toBe(
formatTemplate(`
Expand All @@ -44,6 +46,20 @@ describe('Templates: Smart Contracts', () => {
return { type: 'message', id: result.message };
`),
);

expect(
compiled({
format: 'ffi',
schema: { methods: [{ name: 'method1' }] },
publish: true,
}),
).toBe(
formatTemplate(`
const ffi = {"methods" ... ethod1"}]};
const result = await firefly.createContractInterface(ffi, { publish: true });
return { type: 'message', id: result.message };
`),
);
});
});

Expand All @@ -60,6 +76,7 @@ describe('Templates: Smart Contracts', () => {
interfaceName: 'simple-storage',
interfaceVersion: '1.0',
address: '0x123',
publish: undefined,
}),
).toBe(
formatTemplate(`
Expand All @@ -76,6 +93,30 @@ describe('Templates: Smart Contracts', () => {
return { type: 'message', id: api.message };
`),
);

expect(
compiled({
name: 'api1',
interfaceName: 'simple-storage',
interfaceVersion: '1.0',
address: '0x123',
publish: false,
}),
).toBe(
formatTemplate(`
const api = await firefly.createContractAPI({
name: 'api1',
interface: {
name: 'simple-storage',
version: '1.0',
},
location: {
address: '0x123',
},
}, { publish: false });
return { type: 'message', id: api.message };
`),
);
});
});

Expand All @@ -94,6 +135,7 @@ describe('Templates: Smart Contracts', () => {
chaincode: 'chaincode123',
channel: 'channel123',
address: undefined,
publish: undefined,
}),
).toBe(
formatTemplate(`
Expand Down
30 changes: 18 additions & 12 deletions server/test/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Smart Contracts', () => {
.expect(202)
.expect({ type: 'message', id: 'msg1' });

expect(mockFireFly.createContractInterface).toHaveBeenCalledWith(req.schema);
expect(mockFireFly.createContractInterface).toHaveBeenCalledWith(req.schema, {});
});

test('Create contract interface from ABI', async () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('Smart Contracts', () => {
version: '1.0',
input: { abi: req.schema },
});
expect(mockFireFly.createContractInterface).toHaveBeenCalledWith(int);
expect(mockFireFly.createContractInterface).toHaveBeenCalledWith(int, {});
});

test('Create contract API', async () => {
Expand All @@ -94,11 +94,14 @@ describe('Smart Contracts', () => {
.expect(202)
.expect({ type: 'message', id: 'msg1' });

expect(mockFireFly.createContractAPI).toHaveBeenCalledWith({
interface: { name: 'my-contract', version: '1.0' },
location: { address: '0x123' },
name: 'my-api',
});
expect(mockFireFly.createContractAPI).toHaveBeenCalledWith(
{
interface: { name: 'my-contract', version: '1.0' },
location: { address: '0x123' },
name: 'my-api',
},
{},
);
});

test('Create contract API with Fabric', async () => {
Expand All @@ -122,11 +125,14 @@ describe('Smart Contracts', () => {
.expect(202)
.expect({ type: 'message', id: 'msg1' });

expect(mockFireFly.createContractAPI).toHaveBeenCalledWith({
interface: { name: 'my-contract', version: '1.0' },
location: { chaincode: 'chaincode', channel: '0x123' },
name: 'my-api-fabric',
});
expect(mockFireFly.createContractAPI).toHaveBeenCalledWith(
{
interface: { name: 'my-contract', version: '1.0' },
location: { chaincode: 'chaincode', channel: '0x123' },
name: 'my-api-fabric',
},
{},
);
});

test('Get contract Interfaces', async () => {
Expand Down
8 changes: 0 additions & 8 deletions server/test/misc.test.ts

This file was deleted.

Loading

0 comments on commit 5deeeea

Please sign in to comment.