Skip to content

Commit

Permalink
[FAB-16527] Split fabric-shim into API and impl
Browse files Browse the repository at this point in the history
Pull the API definition - currently a TypeScript
ambient declaration file - into it's own module
named fabric-shim-api. This way, users of the contract
API or shim API can depend upon it without having
to depend on the shim implementation as well.

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
Change-Id: I46bb973862431b63a3664b1af937d3e8ec32251f
  • Loading branch information
Simon Stone authored and mbwhite committed Oct 3, 2019
1 parent b36992a commit d4121e1
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 11,990 deletions.
1 change: 1 addition & 0 deletions build/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gulp.task('lint', function () {
'**/*.js',
'!fabric-contract-api/node_modules/**',
'!fabric-shim/node_modules/**',
'!fabric-shim-api/node_modules/**',
'!fabric-shim-crypto/node_modules/**',
'!test/node_modules/**',
'!**/typescript/*.js',
Expand Down
1 change: 1 addition & 0 deletions fabric-contract-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"fabric-shim-api": "unstable",
"get-params": "^0.1.2",
"reflect-metadata": "^0.1.12",
"winston": "^3.2.1"
Expand Down
2 changes: 1 addition & 1 deletion fabric-contract-api/test/typescript/smartcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Contract, Context } from 'fabric-contract-api';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim-api';

export class ScenarioContext extends Context{

Expand Down
8 changes: 4 additions & 4 deletions fabric-contract-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

declare module 'fabric-contract-api' {
import { Logger } from 'winston';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim';
import { LoggerInstance } from 'winston';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim-api';

export class Context {
stub: ChaincodeStub;
clientIdentity: ClientIdentity;
Expand All @@ -17,7 +17,7 @@ declare module 'fabric-contract-api' {
getLogger: (name?: string) => Logger
}
}

export class Contract {
constructor(name?: string);

Expand Down
5 changes: 5 additions & 0 deletions fabric-shim-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
5 changes: 5 additions & 0 deletions fabric-shim-api/package-lock.json

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

22 changes: 22 additions & 0 deletions fabric-shim-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "fabric-shim-api",
"version": "2.0.0-snapshot",
"tag": "unstable",
"description": "A node.js API of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/hyperledger/fabric-chaincode-node"
},
"keywords": [
"fabric-shim",
"Hyperledger Fabric",
"Fabric Shim"
],
"engines": {
"node": "10.15.2",
"npm": "^6.4.1"
},
"types": "./types/index.d.ts",
"license": "Apache-2.0"
}
187 changes: 187 additions & 0 deletions fabric-shim-api/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
Copyright 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
declare module 'fabric-shim-api' {

interface Timestamp {
seconds: number;
nanos: number;
}

interface ChaincodeResponse {
status: number;
message: string;
payload: Uint8Array;
}

interface ClientIdentity {
assertAttributeValue(attrName: string, attrValue: string): boolean;
getAttributeValue(attrName: string): string | null;
getID(): string;
getIDBytes(): Uint8Array;
getMSPID(): string;
}

interface SerializedIdentity {
mspid: string;
idBytes: Uint8Array;
}

interface QueryResponseMetadata {
fetchedRecordsCount: number;
bookmark: string;
}

interface StateQueryResponse<T> {
iterator: T;
metadata: QueryResponseMetadata;
}

enum RESPONSE_CODE {
OK = 200,
ERRORTHRESHOLD = 400,
ERROR = 500
}

interface ChaincodeStub {
getArgs(): string[];
getStringArgs(): string[];
getFunctionAndParameters(): { params: string[], fcn: string };

getTxID(): string;
getChannelID(): string;
getCreator(): SerializedIdentity;
getTransient(): Map<string, Uint8Array>;

getSignedProposal(): ChaincodeProposal.SignedProposal;
getTxTimestamp(): Timestamp;
getBinding(): string;

getState(key: string): Promise<Uint8Array>;
putState(key: string, value: Uint8Array): Promise<void>;
deleteState(key: string): Promise<void>;
setStateValidationParameter(key: string, ep: Uint8Array): Promise<void>;
getStateValidationParameter(key: string): Promise<Uint8Array>;
getStateByRange(startKey: string, endKey: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;

getQueryResult(query: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
getHistoryForKey(key: string): Promise<Iterators.HistoryQueryIterator> & AsyncIterable<Iterators.KeyModification>;

invokeChaincode(chaincodeName: string, args: string[], channel: string): Promise<ChaincodeResponse>;
setEvent(name: string, payload: Uint8Array): void;

createCompositeKey(objectType: string, attributes: string[]): string;
splitCompositeKey(compositeKey: string): SplitCompositekey;

getPrivateData(collection: string, key: string): Promise<Uint8Array>;
getPrivateDataHash(collection: string, key: string): Promise<Uint8Array>;
putPrivateData(collection: string, key: string, value: Uint8Array): Promise<void>;
deletePrivateData(collection: string, key: string): Promise<void>;
setPrivateDataValidationParameter(collection: string, key: string, ep: Uint8Array): Promise<void>;
getPrivateDataValidationParameter(collection: string, key: string): Promise<Uint8Array>;
getPrivateDataByRange(collection: string, startKey: string, endKey: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getPrivateDataByPartialCompositeKey(collection: string, objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getPrivateDataQueryResult(collection: string, query: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
}

interface SplitCompositekey {
objectType: string;
attributes: string[];
}

interface ChaincodeInterface {
Init(stub: ChaincodeStub): Promise<ChaincodeResponse>;
Invoke(stub: ChaincodeStub): Promise<ChaincodeResponse>;
}

namespace Iterators {

interface CommonIterator<T> {
close(): Promise<void>;
next(): Promise<NextResult<T>>;
}

interface NextResult<T> {
value: T;
done: boolean;
}

type HistoryQueryIterator = CommonIterator<KeyModification>;
type StateQueryIterator = CommonIterator<KV>;

interface NextKeyModificationResult {
value: KeyModification;
done: boolean;
}

interface KV {
namespace: string;
key: string;
value: Uint8Array;
}

interface KeyModification {
isDelete: boolean;
value: Uint8Array;
timestamp: Timestamp;
txId: string;
}
}

namespace ChaincodeProposal {
interface SignedProposal {
proposal: Proposal;
signature: Uint8Array;
}

interface Proposal {
header: Header;
payload: ChaincodeProposalPayload;
}

interface Header {
channelHeader: ChannelHeader;
signatureHeader: SignatureHeader;
}

interface ChannelHeader {
type: HeaderType;
version: number;
timestamp: Timestamp;
channelId: string;
txId: string;
epoch: number;
extension: Uint8Array;
tlsCertHash: Uint8Array;
}

interface SignatureHeader {
creator: SerializedIdentity;
nonce: Uint8Array;
}

interface ChaincodeProposalPayload {
input: Uint8Array;
transientMap: Map<string, Uint8Array>;
}

enum HeaderType {
MESSAGE = 0,
CONFIG = 1,
CONFIG_UPDATE = 2,
ENDORSER_TRANSACTION = 3,
ORDERER_TRANSACTION = 4,
DELIVER_SEEK_INFO = 5,
CHAINCODE_PACKAGE = 6,
PEER_ADMIN_OPERATION = 8
}
}

}
37 changes: 37 additions & 0 deletions fabric-shim-api/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2018 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
{
"compilerOptions": {
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"declaration": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"suppressImplicitAnyIndexErrors": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "es6",
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
},
"lib": [
"esnext"
]
},
"files": [
"index.d.ts"
],
"formatCodeOptions": {
"indentSize": 2,
"tabSize": 2
}
}
1 change: 1 addition & 0 deletions fabric-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/node": "^8.9.4",
"ajv": "^6.5.5",
"fabric-contract-api": "unstable",
"fabric-shim-api": "unstable",
"fs-extra": "^7.0.1",
"grpc": "^1.23.3",
"reflect-metadata": "^0.1.12",
Expand Down
Loading

0 comments on commit d4121e1

Please sign in to comment.