Skip to content

Commit

Permalink
Merge pull request #117 from kaleido-io/optional-publish
Browse files Browse the repository at this point in the history
Add support for optionally publishing token pools, interfaces, and APIs
  • Loading branch information
nguyer authored Mar 26, 2024
2 parents d00c0a5 + 9d2d70a commit 81b11ac
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 70 deletions.
14 changes: 7 additions & 7 deletions server/package-lock.json

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

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/hyperledger/firefly-sandbox#readme",
"dependencies": {
"@hyperledger/firefly-sdk": "^1.2.3",
"@hyperledger/firefly-sdk": "^1.2.10",
"body-parser": "^1.20.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.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 @@ -10,7 +10,7 @@ import {
} from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
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 @@ -51,7 +51,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 @@ -65,16 +65,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 @@ -88,17 +91,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 @@ -213,7 +219,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 @@ -232,7 +238,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 @@ -18,6 +18,7 @@ import {
formatTemplate,
FormDataSchema,
getBroadcastMessageBody,
getFireflyOptions,
getPrivateMessageBody,
mapPool,
quoteAndEscape as q,
Expand Down Expand Up @@ -70,15 +71,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 @@ -291,7 +295,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 @@ -153,6 +153,10 @@ export class TokenPoolInput {
@IsString()
@IsOptional()
blockNumber?: string;

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

export class TokenPool extends TokenPoolInput {
Expand Down Expand Up @@ -241,6 +245,10 @@ export class ContractInterface {
@IsDefined()
@JSONSchema({ type: 'object' })
schema: any;

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

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

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

export class ContractAPIURLs {
Expand Down
29 changes: 24 additions & 5 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { getMetadataArgsStorage, RoutingControllersOptions } from 'routing-contr
import { OpenAPI, routingControllersToSpec } from 'routing-controllers-openapi';
import { WebSocketServer } from 'ws';
import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
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 @@ -20,6 +24,13 @@ export enum FF_MESSAGES {
GROUP_INIT = 'groupinit',
}

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

export function genOpenAPI(options: RoutingControllersOptions) {
return routingControllersToSpec(getMetadataArgsStorage(), options, {
info: {
Expand Down Expand Up @@ -111,25 +122,33 @@ export function quoteAndEscape(varName: string, options?: QuoteOptions) {
return result;
}

export function getBroadcastMessageBody(body: BroadcastValue, blobId?: string, messageType?: FF_MESSAGES) {
export function getBroadcastMessageBody(
body: BroadcastValue,
blobId?: string,
messageType?: FF_MESSAGES,
) {
const dataBody = blobId ? { id: blobId } : getMessageBody(body);
return {
header: {
tag: body.tag || undefined,
topics: body.topic ? [body.topic] : undefined,
type: messageType || undefined
type: messageType || undefined,
},
data: [dataBody],
};
}

export function getPrivateMessageBody(body: PrivateValue, blobId?: string, messageType?: FF_MESSAGES) {
export function getPrivateMessageBody(
body: PrivateValue,
blobId?: string,
messageType?: FF_MESSAGES,
) {
const dataBody = blobId ? { id: blobId } : getMessageBody(body);
return {
header: {
tag: body.tag || undefined,
topics: body.topic ? [body.topic] : undefined,
type: messageType || undefined
type: messageType || undefined,
},
group: {
members: body.recipients.map((r) => ({ identity: r })),
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
Loading

0 comments on commit 81b11ac

Please sign in to comment.