Skip to content

Commit

Permalink
FABN-1238 NodeSDK add TS for lifecycle
Browse files Browse the repository at this point in the history
Add typescript definitions for the new lifecycle API's

Change-Id: I6b58613329d2ec46102a7c86b9ff5f5f1790707e
Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
  • Loading branch information
harrisob committed May 14, 2019
1 parent 71e371b commit 4d5ff98
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 25 deletions.
40 changes: 25 additions & 15 deletions fabric-client/lib/Chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const Chaincode = class {
/**
* Get the source code package
*
* @returns {number} The package of this chaincode
* @returns {byte[]} The package of this chaincode
*/
getPackage() {

Expand All @@ -195,12 +195,25 @@ const Chaincode = class {

/**
* Get the chaincode type
*
* @returns {string} The type of this chaincode
*/
getType() {

return this._type;
}

/**
* Set the chaincode type
* @param {string} type The type of this chaincode. Must be "golang",
* "node", "java" or "car".
*/
setType(type) {
this._type = Chaincode.checkType(type);

return this;
}

/**
* Set if the chaincode initialize is required
* @param {boolean} required Indicates if this chaincode must be initialized
Expand All @@ -213,25 +226,18 @@ const Chaincode = class {

/**
* Get the initialize required setting
*
* @returns {boolean}
*/
getInitRequired() {

return this._init_required;
}

/**
* Set the chaincode type
* @param {string} type The type of this chaincode. Must be "golang",
* "node", "java" or "car".
*/
setType(type) {
this._type = Chaincode.checkType(type);

return this;
}

/**
* Get the chaincode path
*
* @returns {string}
*/
getChaincodePath() {

Expand All @@ -250,6 +256,8 @@ const Chaincode = class {

/**
* Get the chaincode path
*
* @returns {string}
*/
getMetadataPath() {

Expand All @@ -268,6 +276,8 @@ const Chaincode = class {

/**
* Get the goLang path
*
* @returns {string}
*/
getGoLangPath() {

Expand Down Expand Up @@ -731,7 +741,7 @@ const Chaincode = class {
}


/**
/*
* Build a ApproveChaincodeDefinitionForMyOrgArgs protobuf object
* based on this Chaincode definition
*/
Expand All @@ -758,7 +768,7 @@ const Chaincode = class {
return arg;
}

/**
/*
* Build a QueryApprovalStatusArgs protobuf object
* based on this Chaincode definition
*/
Expand All @@ -774,7 +784,7 @@ const Chaincode = class {
return arg;
}

/**
/*
* Build a CommitChaincodeDefinitionArgs protobuf object
* based on this Chaincode definition
*/
Expand Down
14 changes: 10 additions & 4 deletions fabric-client/lib/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3932,11 +3932,17 @@ const Channel = class {
* query. Required when using the admin idendity.
*/

/**
* @typedef {Object} QueryInstalledChaincodeResult
* @property {string} package_id - The package ID of the installed chaincode
* @property {string} label - The label as provided by the client application
*/

/**
* Sends a QueryInstalledChaincode request to one peer.
*
* @param {InstalledChaincodeRequest} request
* @returns {Promise} A Promise for a {@link Chaincode} instance
* @returns {Promise} A Promise for a {@link QueryInstalledChaincodeResult}
*/
async queryInstalledChaincode(request) {
const method = 'queryInstalledChaincode';
Expand Down Expand Up @@ -4003,7 +4009,7 @@ const Channel = class {
throw results;
}

return results;
return JSON.parse(results.encodeJSON());
}

/**
Expand All @@ -4019,7 +4025,7 @@ const Channel = class {
* Sends a QueryInstalledChaincodes request to one peer.
*
* @param {InstalledChaincodesRequest} request
* @returns {Promise} A Promise for a {@link Chaincode} instance
* @returns {Promise} A Promise for a {@link QueryInstalledChaincodeResult[]}
*/
async queryInstalledChaincodes(request) {
const method = 'queryInstalledChaincodes';
Expand Down Expand Up @@ -4082,7 +4088,7 @@ const Channel = class {
throw results;
}

return results;
return JSON.parse(results.encodeJSON());
}

/**
Expand Down
111 changes: 108 additions & 3 deletions fabric-client/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*/

/* tslint:disable:max-classes-per-file */
/* tslint:disable:ordered-imports */

import FabricCAServices = require('fabric-ca-client');
import { BaseClient } from './base';
import FabricCAServices = require('fabric-ca-client');
import { lstatSync } from 'fs';

interface ProtoBufObject {
toBuffer(): Buffer;
Expand Down Expand Up @@ -48,7 +50,7 @@ declare class Client extends BaseClient {
public queryPeers(request: Client.PeerQueryRequest): Promise<Client.PeerQueryResponse>;
public queryChannels(peer: Client.Peer | string, useAdmin?: boolean): Promise<Client.ChannelQueryResponse>;
public queryInstalledChaincodes(peer: Client.Peer | string, useAdmin?: boolean): Promise<Client.ChaincodeQueryResponse>;
public installChaincode(request: Client.ChaincodeInstallRequest, timeout?: number): Promise<Client.ProposalResponseObject>;
public installChaincode(request: Client.ChaincodeInstallRequestv1, timeout?: number): Promise<Client.ProposalResponseObject>;
public initCredentialStores(): Promise<boolean>;
public setStateStore(store: Client.IKeyValueStore): void;
public setAdminSigningIdentity(privateKey: string, certificate: string, mspid: string): void;
Expand Down Expand Up @@ -155,6 +157,44 @@ declare namespace Client { // tslint:disable-line:no-namespace
configUpdate?: Buffer;
}

export interface ChaincodeDefinitionQueryRequest {
target: Peer;
chaincodeId: string;
request_timeout?: number;
txId?: TransactionId;
}

export interface NamespaceDefinitionsRequest {
target: Peer;
request_timeout?: number;
txId?: TransactionId;
}

export interface ApprovalStatusRequest {
target: Peer;
request_timeout?: number;
txId?: TransactionId;
chaincode: Chaincode;
}

export interface InstalledChaincodeRequest {
target: Peer;
request_timeout?: number;
txId?: TransactionId;
package_id: string;
}

export interface InstalledChaincodesRequest {
target: Peer;
request_timeout?: number;
txId?: TransactionId;
}

export interface QueryInstalledChaincodeResult {
package_id: string;
label: string;
}

export class Channel {
public static sendSignedProposal(request: SignedProposal, timeout?: number): Promise<ProposalResponseObject>;

Expand Down Expand Up @@ -222,6 +262,14 @@ declare namespace Client { // tslint:disable-line:no-namespace
public queryByChaincode(request: ChaincodeQueryRequest, useAdmin?: boolean): Promise<Buffer[]>;
public verifyProposalResponse(proposalResponse: ProposalResponse): boolean;
public compareProposalResponseResults(proposalResponses: ProposalResponse[]): boolean;

public approveChaincodeForOrg(request: ChaincodeRequest): Promise<object>;
public commitChaincode(request: ChaincodeRequest): Promise<object>;
public queryChaincodeDefinition(request: ChaincodeDefinitionQueryRequest): Promise<Chaincode>;
public queryNamespaceDefinitions(request: NamespaceDefinitionsRequest): Promise<object>;
public queryApprovalStatus(request: ApprovalStatusRequest): Promise<object>;
public queryInstalledChaincode(request: InstalledChaincodeRequest): Promise<QueryInstalledChaincodeResult>;
public queryInstalledChaincodes(request: InstalledChaincodesRequest): Promise<QueryInstalledChaincodeResult[]>;
}

export interface ChannelPeerRoles {
Expand Down Expand Up @@ -456,7 +504,7 @@ declare namespace Client { // tslint:disable-line:no-namespace
metadataPath?: string;
}

export type ChaincodeInstallRequest = ChaincodePackageInstallRequest | ChaincodePathInstallRequest;
export type ChaincodeInstallRequestv1 = ChaincodePackageInstallRequest | ChaincodePathInstallRequest;

export interface ChaincodeInstantiateUpgradeRequest {
targets?: Peer[] | string[];
Expand Down Expand Up @@ -492,6 +540,12 @@ declare namespace Client { // tslint:disable-line:no-namespace
txId?: TransactionId;
}

export interface ChaincodeRequest {
targets?: Peer[] | string[];
chaincode: Chaincode;
txId?: TransactionId;
}

export interface KeyOpts {
ephemeral: boolean;
}
Expand Down Expand Up @@ -727,4 +781,55 @@ declare namespace Client { // tslint:disable-line:no-namespace
public getFileNames(): string[];
public toBuffer(): Promise<Buffer>;
}

export interface ChaincodePackageRequest {
label?: string;
chaincodeType: ChaincodeType;
chaincodePath: string;
metadataPath?: string;
goPath?: string;
}

export interface ChaincodeInstallRequest {
target: Peer;
request_timeout?: number;
txId: TransactionId;
}

export class Chaincode {
public static fromQueryResult(name: string, payload: ByteBuffer, client: Client): Chaincode;
constructor(name: string, version: string, client: Client);
public getName(): string;
public getVersion(): string;
public setVersion(version: string): Chaincode;
public getSequence(): Long;
public setSequence(sequence: Long): Chaincode;
public getPackage(): Buffer;
public setPackage(packagedChaincode: Buffer): Chaincode;
public getType(): ChaincodeType;
public setType(type: ChaincodeType): Chaincode;
public getInitRequired(): boolean;
public setInitRequired(required: boolean): Chaincode;
public getChaincodePath(): string;
public setChaincodePath(path: string): Chaincode;
public getMetadataPath(): string;
public setMetadataPath(path: string): Chaincode;
public getGoLangPath(): string;
public setGoLangPath(path: string): Chaincode;
public getLabel(): string;
public setLabel(label: string): Chaincode;
public getPackageId(): string;
public setPackageId(packageId: string): Chaincode;
public getEndorsementPolicyDefinition(): object;
public setEndorsementPolicyDefinition(policy: object): Chaincode;
public getEndorsementPolicy(): object;
public setEndorsementPolicy(policy: object): Chaincode;
public getCollectionConfigPackageDefinition(): object;
public setCollectionConfigPackageDefinition(configPackage: object): Chaincode;
public getCollectionConfigPackage(): object;
public validate(): void;
public package(request: ChaincodePackageRequest): Promise<Buffer>;
public install(request: ChaincodeInstallRequest): Promise<string>;
public toString(): string;
}
}
6 changes: 3 additions & 3 deletions test/typescript/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Block,
BlockchainInfo,
BroadcastResponse,
ChaincodeInstallRequest,
ChaincodeInstallRequestv1,
ChaincodeInstantiateUpgradeRequest,
ChaincodeInvokeRequest,
ChaincodeQueryRequest,
Expand Down Expand Up @@ -248,7 +248,7 @@ test('use the connection profile file', async (t: any) => {
logger.debug(`Set GOPATH to ${process.env.GOPATH}`);
const txId: TransactionId = client.newTransactionID(true);
// send proposal to endorser
const request: ChaincodeInstallRequest = {
const request: ChaincodeInstallRequestv1 = {
chaincodeId: 'examplets',
chaincodePath: 'github.com/example_cc',
chaincodeVersion: 'v1',
Expand All @@ -274,7 +274,7 @@ test('use the connection profile file', async (t: any) => {
t.pass('Successfully loaded the client configuration for org2');
const txId: TransactionId = client.newTransactionID(true);
// send proposal to endorser
const request: ChaincodeInstallRequest = {
const request: ChaincodeInstallRequestv1 = {
chaincodeId: 'examplets',
chaincodePath: 'github.com/example_cc',
chaincodeVersion: 'v1',
Expand Down

0 comments on commit 4d5ff98

Please sign in to comment.