@decaf-ts/for-fabric 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +647 -0
  3. package/dist/for-fabric.cjs +6223 -0
  4. package/dist/for-fabric.esm.cjs +6180 -0
  5. package/lib/client/FabricClientAdapter.cjs +760 -0
  6. package/lib/client/FabricClientAdapter.d.ts +381 -0
  7. package/lib/client/FabricClientDispatch.cjs +186 -0
  8. package/lib/client/FabricClientDispatch.d.ts +125 -0
  9. package/lib/client/FabricClientRepository.cjs +131 -0
  10. package/lib/client/FabricClientRepository.d.ts +100 -0
  11. package/lib/client/erc20/erc20ClientRepository.cjs +343 -0
  12. package/lib/client/erc20/erc20ClientRepository.d.ts +254 -0
  13. package/lib/client/fabric-fs.cjs +234 -0
  14. package/lib/client/fabric-fs.d.ts +92 -0
  15. package/lib/client/index.cjs +30 -0
  16. package/lib/client/index.d.ts +13 -0
  17. package/lib/client/logging.cjs +102 -0
  18. package/lib/client/logging.d.ts +60 -0
  19. package/lib/client/services/LoggedService.cjs +47 -0
  20. package/lib/client/services/LoggedService.d.ts +42 -0
  21. package/lib/client/services/constants.cjs +3 -0
  22. package/lib/client/services/constants.d.ts +15 -0
  23. package/lib/client/services/enrollementService.cjs +344 -0
  24. package/lib/client/services/enrollementService.d.ts +176 -0
  25. package/lib/client/services/index.cjs +18 -0
  26. package/lib/client/services/index.d.ts +1 -0
  27. package/lib/contracts/ContractAdapter.cjs +730 -0
  28. package/lib/contracts/ContractAdapter.d.ts +296 -0
  29. package/lib/contracts/ContractContext.cjs +85 -0
  30. package/lib/contracts/ContractContext.d.ts +64 -0
  31. package/lib/contracts/ContractPrivateDataAdapter.cjs +281 -0
  32. package/lib/contracts/ContractPrivateDataAdapter.d.ts +74 -0
  33. package/lib/contracts/FabricConstruction.cjs +441 -0
  34. package/lib/contracts/FabricConstruction.d.ts +304 -0
  35. package/lib/contracts/FabricContractRepository.cjs +306 -0
  36. package/lib/contracts/FabricContractRepository.d.ts +162 -0
  37. package/lib/contracts/FabricContractRepositoryObservableHandler.cjs +85 -0
  38. package/lib/contracts/FabricContractRepositoryObservableHandler.d.ts +62 -0
  39. package/lib/contracts/FabricContractSequence.cjs +139 -0
  40. package/lib/contracts/FabricContractSequence.d.ts +61 -0
  41. package/lib/contracts/FabricContractStatement.cjs +119 -0
  42. package/lib/contracts/FabricContractStatement.d.ts +34 -0
  43. package/lib/contracts/PrivateSequence.cjs +36 -0
  44. package/lib/contracts/PrivateSequence.d.ts +15 -0
  45. package/lib/contracts/crud/crud-contract.cjs +257 -0
  46. package/lib/contracts/crud/crud-contract.d.ts +168 -0
  47. package/lib/contracts/crud/index.cjs +19 -0
  48. package/lib/contracts/crud/index.d.ts +2 -0
  49. package/lib/contracts/crud/serialized-crud-contract.cjs +172 -0
  50. package/lib/contracts/crud/serialized-crud-contract.d.ts +37 -0
  51. package/lib/contracts/erc20/erc20contract.cjs +569 -0
  52. package/lib/contracts/erc20/erc20contract.d.ts +151 -0
  53. package/lib/contracts/erc20/index.cjs +21 -0
  54. package/lib/contracts/erc20/index.d.ts +2 -0
  55. package/lib/contracts/erc20/models.cjs +209 -0
  56. package/lib/contracts/erc20/models.d.ts +114 -0
  57. package/lib/contracts/index.cjs +32 -0
  58. package/lib/contracts/index.d.ts +15 -0
  59. package/lib/contracts/logging.cjs +96 -0
  60. package/lib/contracts/logging.d.ts +49 -0
  61. package/lib/contracts/private-data.cjs +121 -0
  62. package/lib/contracts/private-data.d.ts +16 -0
  63. package/lib/contracts/types.cjs +3 -0
  64. package/lib/contracts/types.d.ts +26 -0
  65. package/lib/esm/client/FabricClientAdapter.d.ts +381 -0
  66. package/lib/esm/client/FabricClientAdapter.js +723 -0
  67. package/lib/esm/client/FabricClientDispatch.d.ts +125 -0
  68. package/lib/esm/client/FabricClientDispatch.js +182 -0
  69. package/lib/esm/client/FabricClientRepository.d.ts +100 -0
  70. package/lib/esm/client/FabricClientRepository.js +127 -0
  71. package/lib/esm/client/erc20/erc20ClientRepository.d.ts +254 -0
  72. package/lib/esm/client/erc20/erc20ClientRepository.js +339 -0
  73. package/lib/esm/client/fabric-fs.d.ts +92 -0
  74. package/lib/esm/client/fabric-fs.js +191 -0
  75. package/lib/esm/client/index.d.ts +13 -0
  76. package/lib/esm/client/index.js +14 -0
  77. package/lib/esm/client/logging.d.ts +60 -0
  78. package/lib/esm/client/logging.js +98 -0
  79. package/lib/esm/client/services/LoggedService.d.ts +42 -0
  80. package/lib/esm/client/services/LoggedService.js +43 -0
  81. package/lib/esm/client/services/constants.d.ts +15 -0
  82. package/lib/esm/client/services/constants.js +2 -0
  83. package/lib/esm/client/services/enrollementService.d.ts +176 -0
  84. package/lib/esm/client/services/enrollementService.js +337 -0
  85. package/lib/esm/client/services/index.d.ts +1 -0
  86. package/lib/esm/client/services/index.js +2 -0
  87. package/lib/esm/contracts/ContractAdapter.d.ts +296 -0
  88. package/lib/esm/contracts/ContractAdapter.js +724 -0
  89. package/lib/esm/contracts/ContractContext.d.ts +64 -0
  90. package/lib/esm/contracts/ContractContext.js +81 -0
  91. package/lib/esm/contracts/ContractPrivateDataAdapter.d.ts +74 -0
  92. package/lib/esm/contracts/ContractPrivateDataAdapter.js +277 -0
  93. package/lib/esm/contracts/FabricConstruction.d.ts +304 -0
  94. package/lib/esm/contracts/FabricConstruction.js +433 -0
  95. package/lib/esm/contracts/FabricContractRepository.d.ts +162 -0
  96. package/lib/esm/contracts/FabricContractRepository.js +302 -0
  97. package/lib/esm/contracts/FabricContractRepositoryObservableHandler.d.ts +62 -0
  98. package/lib/esm/contracts/FabricContractRepositoryObservableHandler.js +81 -0
  99. package/lib/esm/contracts/FabricContractSequence.d.ts +61 -0
  100. package/lib/esm/contracts/FabricContractSequence.js +135 -0
  101. package/lib/esm/contracts/FabricContractStatement.d.ts +34 -0
  102. package/lib/esm/contracts/FabricContractStatement.js +115 -0
  103. package/lib/esm/contracts/PrivateSequence.d.ts +15 -0
  104. package/lib/esm/contracts/PrivateSequence.js +33 -0
  105. package/lib/esm/contracts/crud/crud-contract.d.ts +168 -0
  106. package/lib/esm/contracts/crud/crud-contract.js +253 -0
  107. package/lib/esm/contracts/crud/index.d.ts +2 -0
  108. package/lib/esm/contracts/crud/index.js +3 -0
  109. package/lib/esm/contracts/crud/serialized-crud-contract.d.ts +37 -0
  110. package/lib/esm/contracts/crud/serialized-crud-contract.js +168 -0
  111. package/lib/esm/contracts/erc20/erc20contract.d.ts +151 -0
  112. package/lib/esm/contracts/erc20/erc20contract.js +565 -0
  113. package/lib/esm/contracts/erc20/index.d.ts +2 -0
  114. package/lib/esm/contracts/erc20/index.js +4 -0
  115. package/lib/esm/contracts/erc20/models.d.ts +114 -0
  116. package/lib/esm/contracts/erc20/models.js +206 -0
  117. package/lib/esm/contracts/index.d.ts +15 -0
  118. package/lib/esm/contracts/index.js +16 -0
  119. package/lib/esm/contracts/logging.d.ts +49 -0
  120. package/lib/esm/contracts/logging.js +92 -0
  121. package/lib/esm/contracts/private-data.d.ts +16 -0
  122. package/lib/esm/contracts/private-data.js +113 -0
  123. package/lib/esm/contracts/types.d.ts +26 -0
  124. package/lib/esm/contracts/types.js +2 -0
  125. package/lib/esm/index.d.ts +8 -0
  126. package/lib/esm/index.js +9 -0
  127. package/lib/esm/shared/ClientSerializer.d.ts +52 -0
  128. package/lib/esm/shared/ClientSerializer.js +80 -0
  129. package/lib/esm/shared/DeterministicSerializer.d.ts +40 -0
  130. package/lib/esm/shared/DeterministicSerializer.js +50 -0
  131. package/lib/esm/shared/SimpleDeterministicSerializer.d.ts +7 -0
  132. package/lib/esm/shared/SimpleDeterministicSerializer.js +42 -0
  133. package/lib/esm/shared/constants.d.ts +39 -0
  134. package/lib/esm/shared/constants.js +42 -0
  135. package/lib/esm/shared/crypto.d.ts +107 -0
  136. package/lib/esm/shared/crypto.js +331 -0
  137. package/lib/esm/shared/decorators.d.ts +24 -0
  138. package/lib/esm/shared/decorators.js +98 -0
  139. package/lib/esm/shared/erc20/erc20-constants.d.ts +25 -0
  140. package/lib/esm/shared/erc20/erc20-constants.js +27 -0
  141. package/lib/esm/shared/errors.d.ts +116 -0
  142. package/lib/esm/shared/errors.js +132 -0
  143. package/lib/esm/shared/events.d.ts +39 -0
  144. package/lib/esm/shared/events.js +47 -0
  145. package/lib/esm/shared/fabric-types.d.ts +33 -0
  146. package/lib/esm/shared/fabric-types.js +2 -0
  147. package/lib/esm/shared/index.d.ts +13 -0
  148. package/lib/esm/shared/index.js +14 -0
  149. package/lib/esm/shared/interfaces/Checkable.d.ts +21 -0
  150. package/lib/esm/shared/interfaces/Checkable.js +2 -0
  151. package/lib/esm/shared/math.d.ts +34 -0
  152. package/lib/esm/shared/math.js +61 -0
  153. package/lib/esm/shared/model/Identity.d.ts +42 -0
  154. package/lib/esm/shared/model/Identity.js +78 -0
  155. package/lib/esm/shared/model/IdentityCredentials.d.ts +41 -0
  156. package/lib/esm/shared/model/IdentityCredentials.js +74 -0
  157. package/lib/esm/shared/model/index.d.ts +1 -0
  158. package/lib/esm/shared/model/index.js +2 -0
  159. package/lib/esm/shared/model/utils.d.ts +60 -0
  160. package/lib/esm/shared/model/utils.js +108 -0
  161. package/lib/esm/shared/types.d.ts +79 -0
  162. package/lib/esm/shared/types.js +2 -0
  163. package/lib/esm/shared/utils.d.ts +55 -0
  164. package/lib/esm/shared/utils.js +148 -0
  165. package/lib/index.cjs +25 -0
  166. package/lib/index.d.ts +8 -0
  167. package/lib/shared/ClientSerializer.cjs +84 -0
  168. package/lib/shared/ClientSerializer.d.ts +52 -0
  169. package/lib/shared/DeterministicSerializer.cjs +54 -0
  170. package/lib/shared/DeterministicSerializer.d.ts +40 -0
  171. package/lib/shared/SimpleDeterministicSerializer.cjs +46 -0
  172. package/lib/shared/SimpleDeterministicSerializer.d.ts +7 -0
  173. package/lib/shared/constants.cjs +45 -0
  174. package/lib/shared/constants.d.ts +39 -0
  175. package/lib/shared/crypto.cjs +369 -0
  176. package/lib/shared/crypto.d.ts +107 -0
  177. package/lib/shared/decorators.cjs +105 -0
  178. package/lib/shared/decorators.d.ts +24 -0
  179. package/lib/shared/erc20/erc20-constants.cjs +30 -0
  180. package/lib/shared/erc20/erc20-constants.d.ts +25 -0
  181. package/lib/shared/errors.cjs +142 -0
  182. package/lib/shared/errors.d.ts +116 -0
  183. package/lib/shared/events.cjs +51 -0
  184. package/lib/shared/events.d.ts +39 -0
  185. package/lib/shared/fabric-types.cjs +4 -0
  186. package/lib/shared/fabric-types.d.ts +33 -0
  187. package/lib/shared/index.cjs +30 -0
  188. package/lib/shared/index.d.ts +13 -0
  189. package/lib/shared/interfaces/Checkable.cjs +3 -0
  190. package/lib/shared/interfaces/Checkable.d.ts +21 -0
  191. package/lib/shared/math.cjs +66 -0
  192. package/lib/shared/math.d.ts +34 -0
  193. package/lib/shared/model/Identity.cjs +81 -0
  194. package/lib/shared/model/Identity.d.ts +42 -0
  195. package/lib/shared/model/IdentityCredentials.cjs +77 -0
  196. package/lib/shared/model/IdentityCredentials.d.ts +41 -0
  197. package/lib/shared/model/index.cjs +18 -0
  198. package/lib/shared/model/index.d.ts +1 -0
  199. package/lib/shared/model/utils.cjs +114 -0
  200. package/lib/shared/model/utils.d.ts +60 -0
  201. package/lib/shared/types.cjs +3 -0
  202. package/lib/shared/types.d.ts +79 -0
  203. package/lib/shared/utils.cjs +185 -0
  204. package/lib/shared/utils.d.ts +55 -0
  205. package/package.json +166 -0
package/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Tiago Venceslau and Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,647 @@
1
+ [![Banner](./workdocs/assets/Banner.png)](https://decaf-ts.github.io/ts-workspace/)
2
+ ## Hyperledger Fabric Contracts for DECAF
3
+
4
+ Chaincode-side building blocks for Hyperledger Fabric written in TypeScript. This module provides repositories, CRUD contracts (object and serialized JSON), an ERC20 sample contract, Fabric-backed sequences, event emission, and context-aware logging. It adapts DECAF’s data-access patterns to Fabric’s world-state so you can write smart contracts with the same Repository/Model abstractions used elsewhere in DECAF.
5
+
6
+
7
+ ![Licence](https://img.shields.io/github/license/decaf-ts/for-fabric.svg?style=plastic)
8
+ ![GitHub language count](https://img.shields.io/github/languages/count/decaf-ts/for-fabric?style=plastic)
9
+ ![GitHub top language](https://img.shields.io/github/languages/top/decaf-ts/for-fabric?style=plastic)
10
+
11
+ [![Build & Test](https://github.com/decaf-ts/for-fabric/actions/workflows/nodejs-build-prod.yaml/badge.svg)](https://github.com/decaf-ts/for-fabric/actions/workflows/nodejs-build-prod.yaml)
12
+ [![CodeQL](https://github.com/decaf-ts/for-fabric/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/decaf-ts/for-fabric/actions/workflows/codeql-analysis.yml)[![Snyk Analysis](https://github.com/decaf-ts/for-fabric/actions/workflows/snyk-analysis.yaml/badge.svg)](https://github.com/decaf-ts/for-fabric/actions/workflows/snyk-analysis.yaml)
13
+ [![Pages builder](https://github.com/decaf-ts/for-fabric/actions/workflows/pages.yaml/badge.svg)](https://github.com/decaf-ts/for-fabric/actions/workflows/pages.yaml)
14
+ [![.github/workflows/release-on-tag.yaml](https://github.com/decaf-ts/for-fabric/actions/workflows/release-on-tag.yaml/badge.svg?event=release)](https://github.com/decaf-ts/for-fabric/actions/workflows/release-on-tag.yaml)
15
+
16
+ ![Open Issues](https://img.shields.io/github/issues/decaf-ts/for-fabric.svg)
17
+ ![Closed Issues](https://img.shields.io/github/issues-closed/decaf-ts/for-fabric.svg)
18
+ ![Pull Requests](https://img.shields.io/github/issues-pr-closed/decaf-ts/for-fabric.svg)
19
+ ![Maintained](https://img.shields.io/badge/Maintained%3F-yes-green.svg)
20
+
21
+ ![Forks](https://img.shields.io/github/forks/decaf-ts/for-fabric.svg)
22
+ ![Stars](https://img.shields.io/github/stars/decaf-ts/for-fabric.svg)
23
+ ![Watchers](https://img.shields.io/github/watchers/decaf-ts/for-fabric.svg)
24
+
25
+ ![Node Version](https://img.shields.io/badge/dynamic/json.svg?url=https%3A%2F%2Fraw.githubusercontent.com%2Fbadges%2Fshields%2Fmaster%2Fpackage.json&label=Node&query=$.engines.node&colorB=blue)
26
+ ![NPM Version](https://img.shields.io/badge/dynamic/json.svg?url=https%3A%2F%2Fraw.githubusercontent.com%2Fbadges%2Fshields%2Fmaster%2Fpackage.json&label=NPM&query=$.engines.npm&colorB=purple)
27
+
28
+ Documentation available [here](https://decaf-ts.github.io/for-fabric/)
29
+
30
+ ### Description
31
+
32
+ This module focuses on the chaincode (contracts) side of @decaf-ts/for-fabric. It adapts DECAF’s Repository/Model/Adapter abstractions to Hyperledger Fabric’s world state and execution context so you can implement smart contracts with familiar patterns and minimal boilerplate.
33
+
34
+ Key ideas:
35
+ - Keep your domain models as annotated classes (using @decaf-ts/decorator-validation).
36
+ - Use a Repository to persist/read/query models through a Fabric-aware Adapter.
37
+ - Compose reusable CRUD contracts and utilities instead of hand-writing stub calls.
38
+ - Emit first-class Fabric events from repository operations.
39
+ - Leverage context-aware logging and typed flags during execution.
40
+
41
+ Contracts building blocks identified in src/contracts:
42
+
43
+ 1) Core context and types
44
+ - FabricContractContext: Extends the generic Context to expose Fabric-specific properties (stub, clientIdentity, logger) and timestamp resolution from the ledger.
45
+ - FabricContractFlags: Interface extending RepositoryFlags with stub, clientIdentity and logger for contract calls.
46
+
47
+ 2) Logging
48
+ - ContractLogger: MiniLogger-compatible logger bound to the Fabric contract Context. It honors log levels and forwards to the underlying Fabric logger.
49
+
50
+ 3) Adapter and repository
51
+ - FabricContractAdapter: Chaincode-side Adapter that implements CRUD, bulk operations, raw Mango queries, result iteration, model preparation/reversion, composite-key prefixes, and sequence creation. Bridges DECAF abstractions to Fabric (ChaincodeStub, ClientIdentity) and CouchDB-style queries.
52
+ - FabricContractRepository<M>: Repository for models inside chaincode. Supports create, update, createAll, updateAll, read/readAll, raw queries (Mango), select projections, prefix-based bulk ops, and event emission through an ObserverHandler.
53
+ - FabricContractRepositoryObservableHandler: ObserverHandler that emits Fabric events via stub.setEvent using names generated by generateFabricEventName.
54
+
55
+ 4) Sequences
56
+ - FabricContractDBSequence: Fabric-backed implementation of Sequence with current, next and range. Stores values in the world state via FabricContractRepository and supports Number or BigInt sequences with configurable startWith and incrementBy.
57
+
58
+ 5) CRUD contracts
59
+ - FabricCrudContract<M>: Base smart contract exposing CRUD endpoints (create, read, update, delete, createAll, readAll, updateAll, deleteAll, raw, init, healthcheck) for a model type. Uses DeterministicSerializer and the FabricContractAdapter/Repository behind the scenes and provides logFor(ctx).
60
+ - SerializedCrudContract<M>: Same endpoints as FabricCrudContract but takes/returns JSON strings (de)serialized to the model class. This simplifies client interactions and is used in tests.
61
+
62
+ 6) ERC20 sample
63
+ - ERC20Token, ERC20Wallet, Allowance: Sample domain models for an ERC20-like token, wallets and allowances.
64
+ - FabricStatement<M,R>: A CouchDBStatement bridge that runs Mango queries through FabricContractAdapter, handling primary key projection when needed.
65
+ - FabricERC20Contract: A full ERC20 smart contract showcasing repository-based persistence and arithmetic helpers. Implements Initialize, CheckInitialized, TokenName, Symbol, Decimals, TotalSupply, BalanceOf, Transfer, TransferFrom, Approve, Allowance, Mint, Burn, BurnFrom, ClientAccountBalance, ClientAccountID and an internal _transfer helper.
66
+
67
+ Design notes:
68
+ - Deterministic serialization is used to ensure stable bytes for world-state writes.
69
+ - onCreate/onCreateUpdate hooks from db-decorators are leveraged by the adapter to set primary keys and creator/owner metadata.
70
+ - Mango queries (CouchDB) are used for rich queries via getQueryResultWithPagination.
71
+ - Event emission is opt-in per operation type through FabricContractRepositoryObservableHandler’s supportedEvents list.
72
+
73
+ With these components you can build robust chaincode while keeping code concise, testable, and aligned with DECAF’s architecture.
74
+
75
+
76
+ ### How to Use
77
+
78
+ ## Installation
79
+
80
+ ```bash
81
+ npm install @decaf-ts/for-fabric
82
+ ```
83
+
84
+ ## Client-Side Usage
85
+
86
+ ### Connecting to a Fabric Network
87
+
88
+ ```typescript
89
+ import { FabricAdapter, PeerConfig } from '@decaf-ts/for-fabric';
90
+
91
+ // Configure connection to a Fabric peer
92
+ const config: PeerConfig = {
93
+ mspId: 'Org1MSP',
94
+ peerEndpoint: 'localhost:7051',
95
+ channelName: 'mychannel',
96
+ chaincodeName: 'mycc',
97
+ contractName: 'mycontract',
98
+ tlsCertPath: '/path/to/tls/cert',
99
+ certDirectoryPath: '/path/to/cert/dir',
100
+ keyDirectoryPath: '/path/to/key/dir',
101
+ cryptoPath: '/path/to/crypto',
102
+ peerHostAlias: 'peer0.org1.example.com',
103
+ caEndpoint: 'localhost:7054',
104
+ caTlsCertificate: '/path/to/ca/tls/cert',
105
+ caCert: '/path/to/ca/cert',
106
+ caKey: '/path/to/ca/key',
107
+ ca: 'ca.org1.example.com'
108
+ };
109
+
110
+ // Create an adapter instance
111
+ const adapter = new FabricAdapter(config, 'org1-adapter');
112
+
113
+ // Use the adapter to interact with the Fabric network
114
+ async function createAsset() {
115
+ const asset = { id: 'asset1', value: 'Asset 1 Value' };
116
+ return await adapter.create('assets', 'asset1', asset, {}, mySerializer);
117
+ }
118
+
119
+ async function readAsset(id: string) {
120
+ return await adapter.read('assets', id, mySerializer);
121
+ }
122
+
123
+ async function updateAsset(id: string, newValue: string) {
124
+ const asset = await readAsset(id);
125
+ asset.value = newValue;
126
+ return await adapter.update('assets', id, asset, {}, mySerializer);
127
+ }
128
+
129
+ async function deleteAsset(id: string) {
130
+ return await adapter.delete('assets', id, mySerializer);
131
+ }
132
+
133
+ async function queryAssets(owner: string) {
134
+ const query = {
135
+ selector: {
136
+ owner: owner
137
+ }
138
+ };
139
+ return await adapter.raw(query, true);
140
+ }
141
+ ```
142
+
143
+ ### Listening for Chaincode Events
144
+
145
+ ```typescript
146
+ import { FabricAdapter, FabricDispatch } from '@decaf-ts/for-fabric';
147
+
148
+ async function setupEventListener(config: PeerConfig) {
149
+ // Create a client
150
+ const client = await FabricAdapter.getClient(config);
151
+
152
+ // Create a dispatch instance
153
+ const dispatch = new FabricDispatch(client);
154
+
155
+ // Configure the dispatch with peer configuration
156
+ dispatch.configure(config);
157
+
158
+ // Register an observer for a specific table and event
159
+ dispatch.observe('assets', 'create', (id) => {
160
+ console.log(`Asset created: ${id}`);
161
+ // Fetch the new asset or update UI
162
+ });
163
+
164
+ // Start listening for events
165
+ await dispatch.start();
166
+
167
+ // When done, close the connection
168
+ // await dispatch.close();
169
+ }
170
+ ```
171
+
172
+ ### Working with Identities and Certificates
173
+
174
+ ```typescript
175
+ import {
176
+ getIdentity,
177
+ getSigner,
178
+ readFile,
179
+ getCAUser
180
+ } from '@decaf-ts/for-fabric';
181
+
182
+ async function setupIdentity() {
183
+ // Read a certificate file
184
+ const tlsCert = await readFile('/path/to/tls/cert');
185
+
186
+ // Get an identity from a certificate directory
187
+ const identity = await getIdentity('Org1MSP', '/path/to/cert/dir');
188
+
189
+ // Get a signer from a key directory
190
+ const signer = await getSigner('/path/to/key/dir');
191
+
192
+ // Create a CA user
193
+ const user = await getCAUser(
194
+ 'user1',
195
+ privateKeyPem,
196
+ certificatePem,
197
+ 'Org1MSP'
198
+ );
199
+
200
+ return { identity, signer, user };
201
+ }
202
+ ```
203
+
204
+ ## Chaincode Development
205
+
206
+ ### Creating a Model
207
+
208
+ ```typescript
209
+ import { Model, id, property, table } from '@decaf-ts/decorator-validation';
210
+
211
+ @table('assets')
212
+ export class Asset extends Model {
213
+ @id()
214
+ id: string;
215
+
216
+ @property()
217
+ value: string;
218
+
219
+ @property()
220
+ owner: string;
221
+
222
+ @property()
223
+ createdAt: number;
224
+ }
225
+ ```
226
+
227
+ ### Creating a CRUD Contract
228
+
229
+ ```typescript
230
+ import { FabricCrudContract } from '@decaf-ts/for-fabric';
231
+ import { Context, Contract, Info, Transaction } from 'fabric-contract-api';
232
+ import { Asset } from './asset';
233
+
234
+ @Info({ title: 'AssetContract', description: 'Smart contract for trading assets' })
235
+ export class AssetContract extends FabricCrudContract<Asset> {
236
+ constructor() {
237
+ super('AssetContract', Asset);
238
+ }
239
+
240
+ // The base class already provides standard CRUD operations:
241
+ // create, read, update, delete, createAll, readAll, updateAll, deleteAll
242
+
243
+ // Add custom methods as needed
244
+ @Transaction()
245
+ async getAssetHistory(ctx: Context, id: string): Promise<any[]> {
246
+ const stub = ctx.stub;
247
+ const iterator = await stub.getHistoryForKey(id);
248
+
249
+ const results = [];
250
+ let result = await iterator.next();
251
+
252
+ while (!result.done) {
253
+ const value = result.value;
254
+ results.push({
255
+ txId: value.txId,
256
+ timestamp: value.timestamp,
257
+ value: JSON.parse(value.value.toString('utf8'))
258
+ });
259
+
260
+ result = await iterator.next();
261
+ }
262
+
263
+ await iterator.close();
264
+ return results;
265
+ }
266
+
267
+ @Transaction()
268
+ async transferAsset(ctx: Context, id: string, newOwner: string): Promise<Asset> {
269
+ const asset = await this.read(ctx, id);
270
+ asset.owner = newOwner;
271
+ return await this.update(ctx, asset);
272
+ }
273
+ }
274
+ ```
275
+
276
+ ### Using the Contract Adapter Directly
277
+
278
+ ```typescript
279
+ import { FabricContractAdapter } from '@decaf-ts/for-fabric';
280
+ import { Context, Contract, Transaction } from 'fabric-contract-api';
281
+
282
+ export class CustomContract extends Contract {
283
+ private adapter: FabricContractAdapter;
284
+
285
+ constructor() {
286
+ super('CustomContract');
287
+ this.adapter = new FabricContractAdapter();
288
+ }
289
+
290
+ @Transaction()
291
+ async createRecord(ctx: Context, id: string, data: string): Promise<any> {
292
+ const record = { id, data, timestamp: Date.now() };
293
+ return await this.adapter.create(
294
+ 'records',
295
+ id,
296
+ record,
297
+ {},
298
+ { stub: ctx.stub, logger: ctx.logging }
299
+ );
300
+ }
301
+
302
+ @Transaction(false)
303
+ async queryRecords(ctx: Context, owner: string): Promise<any[]> {
304
+ const query = {
305
+ selector: {
306
+ owner: owner
307
+ }
308
+ };
309
+
310
+ return await this.adapter.raw(
311
+ query,
312
+ true,
313
+ { stub: ctx.stub, logger: ctx.logging }
314
+ );
315
+ }
316
+ }
317
+ ```
318
+
319
+ ### Emitting and Handling Events
320
+
321
+ ```typescript
322
+ import {
323
+ FabricContractRepositoryObservableHandler,
324
+ generateFabricEventName,
325
+ parseEventName
326
+ } from '@decaf-ts/for-fabric';
327
+ import { Context } from 'fabric-contract-api';
328
+ import { OperationKeys } from '@decaf-ts/db-decorators';
329
+
330
+ // In chaincode: Emit an event
331
+ async function emitEvent(ctx: Context, tableName: string, id: string) {
332
+ const handler = new FabricContractRepositoryObservableHandler();
333
+ const logger = ctx.logging.getLogger('EventHandler');
334
+
335
+ await handler.updateObservers(
336
+ logger,
337
+ tableName,
338
+ OperationKeys.CREATE,
339
+ id,
340
+ { stub: ctx.stub }
341
+ );
342
+ }
343
+
344
+ // In client: Parse an event name
345
+ function handleEvent(eventName: string, payload: Buffer) {
346
+ const { table, event, owner } = parseEventName(eventName);
347
+ const data = JSON.parse(payload.toString());
348
+
349
+ console.log(`Received ${event} event for ${table} with ID ${data.id}`);
350
+ if (owner) {
351
+ console.log(`Event owner: ${owner}`);
352
+ }
353
+ }
354
+ ```
355
+
356
+ For more detailed examples and API documentation, refer to the [API Reference](./docs/api/index.html).
357
+
358
+
359
+ ## Contracts APIs (Chaincode)
360
+
361
+ The following examples are based on the contracts in for-fabric/src/contracts and reflect the patterns used by the unit/integration tests.
362
+
363
+ ### FabricCrudContract<M>
364
+
365
+ Description: Base contract exposing CRUD endpoints for a model class. It uses Repository and DeterministicSerializer under the hood.
366
+
367
+ ```typescript
368
+ import { Context, Transaction, Contract } from 'fabric-contract-api';
369
+ import { model, ModelArg, required } from '@decaf-ts/decorator-validation';
370
+ import { BaseModel, pk } from '@decaf-ts/core';
371
+ import { FabricCrudContract } from '@decaf-ts/for-fabric/contracts';
372
+
373
+ @model()
374
+ class Person extends BaseModel {
375
+ @pk({ type: 'Number' })
376
+ id!: number;
377
+ @required() name!: string;
378
+ constructor(arg?: ModelArg<Person>) { super(arg); }
379
+ }
380
+
381
+ export class PersonContract extends FabricCrudContract<Person> {
382
+ constructor() {
383
+ super('PersonContract', Person);
384
+ }
385
+
386
+ @Transaction(false)
387
+ async ping(ctx: Context): Promise<string> {
388
+ // Uses FabricCrudContract.logFor
389
+ this.logFor(ctx).info('ping');
390
+ return 'pong';
391
+ }
392
+ }
393
+ ```
394
+
395
+ Usage in tests: see tests/unit/contracts.test.ts pattern where a SerializedCrudContract subclass is exercised; FabricCrudContract is similar but takes/returns objects instead of JSON strings.
396
+
397
+ ### SerializedCrudContract<M>
398
+
399
+ Description: Same endpoints as FabricCrudContract but takes and returns JSON strings. Useful for simple clients. Based on tests/unit/contracts.test.ts.
400
+
401
+ ```typescript
402
+ import { Context } from 'fabric-contract-api';
403
+ import { model, ModelArg, required } from '@decaf-ts/decorator-validation';
404
+ import { BaseModel, pk } from '@decaf-ts/core';
405
+ import { SerializedCrudContract } from '@decaf-ts/for-fabric/contracts';
406
+
407
+ @model()
408
+ class TestModel extends BaseModel {
409
+ @pk({ type: 'Number' }) id!: number;
410
+ @required() name!: string;
411
+ @required() nif!: string;
412
+ constructor(arg?: ModelArg<TestModel>) { super(arg); }
413
+ }
414
+
415
+ export class TestModelContract extends SerializedCrudContract<TestModel> {
416
+ constructor() {
417
+ super('TestModelContract', TestModel);
418
+ }
419
+ }
420
+
421
+ // Example invocation (mirrors unit test usage)
422
+ async function createExample(contract: TestModelContract, ctx: Context) {
423
+ const payload = new TestModel({ name: 'Alice', nif: '123456789' }).serialize();
424
+ const resultJson = await contract.create(ctx, payload);
425
+ const created = new TestModel(JSON.parse(resultJson));
426
+ return created;
427
+ }
428
+ ```
429
+
430
+ ### FabricContractRepository<M>
431
+
432
+ Description: Chaincode-side repository used inside contract methods to persist and query models.
433
+
434
+ ```typescript
435
+ import { Context } from 'fabric-contract-api';
436
+ import { Repo } from '@decaf-ts/core';
437
+ import { model, required, ModelArg } from '@decaf-ts/decorator-validation';
438
+ import { BaseModel, pk } from '@decaf-ts/core';
439
+ import { FabricContractRepository } from '@decaf-ts/for-fabric/contracts';
440
+
441
+ @model()
442
+ class Asset extends BaseModel {
443
+ @pk() id!: string;
444
+ @required() owner!: string;
445
+ constructor(arg?: ModelArg<Asset>) { super(arg); }
446
+ }
447
+
448
+ export class AssetContract extends Contract {
449
+ private repo: Repo<Asset, any, any, any, any>;
450
+ constructor() {
451
+ super('AssetContract');
452
+ this.repo = new FabricContractRepository<Asset>(new (require('@decaf-ts/for-fabric').contracts.FabricContractAdapter)(), Asset);
453
+ }
454
+
455
+ @Transaction()
456
+ async Create(ctx: Context, id: string, owner: string): Promise<void> {
457
+ const m = new Asset({ id, owner });
458
+ await this.repo.create(m, ctx as any);
459
+ }
460
+
461
+ @Transaction(false)
462
+ async Read(ctx: Context, id: string): Promise<Asset> {
463
+ return this.repo.read(id, ctx as any);
464
+ }
465
+
466
+ @Transaction(false)
467
+ async QueryByOwner(ctx: Context, owner: string): Promise<Asset[]> {
468
+ return this.repo.raw({ selector: { owner } } as any, true, ctx as any);
469
+ }
470
+ }
471
+ ```
472
+
473
+ ### FabricContractDBSequence
474
+
475
+ Description: World-state backed sequences for generating incremental values.
476
+
477
+ ```typescript
478
+ import { Context } from 'fabric-contract-api';
479
+ import { FabricContractDBSequence } from '@decaf-ts/for-fabric/contracts';
480
+ import { FabricContractAdapter } from '@decaf-ts/for-fabric/contracts';
481
+
482
+ const adapter = new FabricContractAdapter();
483
+
484
+ export class OrderContract extends Contract {
485
+ private orderSeq = new FabricContractDBSequence({
486
+ name: 'orderSeq',
487
+ type: 'Number',
488
+ startWith: 1,
489
+ incrementBy: 1,
490
+ }, adapter);
491
+
492
+ @Transaction()
493
+ async CreateOrder(ctx: Context): Promise<number> {
494
+ const next = await this.orderSeq.next(ctx as any);
495
+ // use next as order id
496
+ return next as number;
497
+ }
498
+
499
+ @Transaction(false)
500
+ async NextRange(ctx: Context, count: number): Promise<number[]> {
501
+ return (await this.orderSeq.range(count, ctx as any)) as number[];
502
+ }
503
+ }
504
+ ```
505
+
506
+ ### FabricStatement<M,R>
507
+
508
+ Description: Bridge to run Mango queries through the Fabric adapter and get typed models back; used internally by repositories and also directly in advanced cases. See tests/unit/erc20conttract.test.ts mocking CouchDBStatement processing.
509
+
510
+ ```typescript
511
+ import { FabricStatement } from '@decaf-ts/for-fabric/contracts';
512
+ import { FabricContractAdapter } from '@decaf-ts/for-fabric/contracts';
513
+ import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
514
+ import { MangoQuery } from '@decaf-ts/for-couchdb';
515
+ import { Model } from '@decaf-ts/decorator-validation';
516
+
517
+ class MyModel extends Model {}
518
+
519
+ const adapter = new FabricContractAdapter();
520
+
521
+ async function query(ctx: FabricContractContext) {
522
+ const stmt = new FabricStatement<MyModel, MyModel[]>(adapter, ctx);
523
+ const models = await stmt.raw<MyModel[]>({ selector: { type: 'MyModel' } } as MangoQuery);
524
+ return models;
525
+ }
526
+ ```
527
+
528
+ ### ContractLogger
529
+
530
+ Description: Context-aware logger bound to Fabric’s Context, honoring log levels.
531
+
532
+ ```typescript
533
+ import { Context, Transaction } from 'fabric-contract-api';
534
+ import { Contract } from 'fabric-contract-api';
535
+ import { ContractLogger } from '@decaf-ts/for-fabric/contracts';
536
+
537
+ export class LoggableContract extends Contract {
538
+ @Transaction()
539
+ async DoWork(ctx: Context): Promise<void> {
540
+ const log = new ContractLogger('LoggableContract', { level: 'info' }, ctx as any);
541
+ log.info('Starting work');
542
+ // ... work ...
543
+ log.debug('Finished');
544
+ }
545
+ }
546
+ ```
547
+
548
+ ### FabricContractRepositoryObservableHandler
549
+
550
+ Description: Emits Fabric events for repository operations. You can also use it directly to emit a custom event.
551
+
552
+ ```typescript
553
+ import { FabricContractRepositoryObservableHandler } from '@decaf-ts/for-fabric/contracts';
554
+ import { OperationKeys } from '@decaf-ts/db-decorators';
555
+ import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
556
+ import { MiniLogger } from '@decaf-ts/logging';
557
+
558
+ async function emitExample(ctx: FabricContractContext) {
559
+ const handler = new FabricContractRepositoryObservableHandler();
560
+ const log = new MiniLogger('obs');
561
+ await handler.updateObservers(log as any, 'assets', OperationKeys.CREATE, 'asset1', ctx);
562
+ }
563
+ ```
564
+
565
+ ### FabricContractContext
566
+
567
+ Description: Access Fabric-specific context inside contracts.
568
+
569
+ ```typescript
570
+ import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
571
+
572
+ function readContext(ctx: FabricContractContext) {
573
+ const ts = ctx.timestamp; // Date from stub.getDateTimestamp()
574
+ const id = ctx.identity.getID();
575
+ ctx.logger.info(`Tx by ${id} at ${ts.toISOString()}`);
576
+ }
577
+ ```
578
+
579
+ ### FabricERC20Contract (sample)
580
+
581
+ Description: Full ERC20 implementation used in tests (see tests/unit/erc20conttract.test.ts).
582
+
583
+ ```typescript
584
+ import { FabricERC20Contract } from '@decaf-ts/for-fabric/contracts';
585
+ import { FabricContractContext } from '@decaf-ts/for-fabric/contracts';
586
+
587
+ const contract = new FabricERC20Contract('TestToken');
588
+
589
+ async function initAndRead(ctx: FabricContractContext) {
590
+ const created = await contract.Initialize(ctx, 'TestToken', 'TT', 18);
591
+ if (created) {
592
+ const name = await contract.TokenName(ctx);
593
+ const decimals = await contract.Decimals(ctx);
594
+ return { name, decimals };
595
+ }
596
+ throw new Error('Init failed');
597
+ }
598
+ ```
599
+
600
+ ### Notes on tests as examples
601
+
602
+ - tests/unit/contracts.test.ts shows creating a SerializedCrudContract and calling create(ctx, jsonPayload) with a mocked Fabric Context.
603
+ - tests/unit/erc20conttract.test.ts demonstrates initializing the ERC20 contract and reading TokenName.
604
+ - tests/integration/Serialized-Contract.test.ts shows end-to-end JSON-based CRUD flows via the serialized contract, including create, read, update and rich queries.
605
+
606
+ These patterns are mirrored in the examples above to ensure correctness and consistency with the repository’s test suite.
607
+
608
+
609
+ ### Related
610
+
611
+ [![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=decaf-ts&repo=ts-workspace)](https://github.com/decaf-ts/ts-workspace)
612
+
613
+ ### Social
614
+
615
+ [![LinkedIn](https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/decaf-ts/)
616
+
617
+
618
+
619
+
620
+ #### Languages
621
+
622
+ ![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white)
623
+ ![JavaScript](https://img.shields.io/badge/JavaScript-F7DF1E?style=for-the-badge&logo=javascript&logoColor=black)
624
+ ![NodeJS](https://img.shields.io/badge/Node.js-43853D?style=for-the-badge&logo=node.js&logoColor=white)
625
+ ![ShellScript](https://img.shields.io/badge/Shell_Script-121011?style=for-the-badge&logo=gnu-bash&logoColor=white)
626
+
627
+ ## Getting help
628
+
629
+ If you have bug reports, questions or suggestions please [create a new issue](https://github.com/decaf-ts/ts-workspace/issues/new/choose).
630
+
631
+ ## Contributing
632
+
633
+ I am grateful for any contributions made to this project. Please read [this](./workdocs/98-Contributing.md) to get started.
634
+
635
+ ## Supporting
636
+
637
+ The first and easiest way you can support it is by [Contributing](./workdocs/98-Contributing.md). Even just finding a typo in the documentation is important.
638
+
639
+ Financial support is always welcome and helps keep both me and the project alive and healthy.
640
+
641
+ So if you can, if this project in any way. either by learning something or simply by helping you save precious time, please consider donating.
642
+
643
+ ## License
644
+
645
+ This project is released under the [MIT License](./LICENSE.md).
646
+
647
+ By developers, for developers...