Skip to content

Commit e422f2b

Browse files
PsychoPunkSagembrandenburger
authored andcommitted
feat(samples): add Confidential Escrow sample
This commit adds a new FPC sample chaincode implementing a confidential escrow application using cc-tools. Signed-off-by: Abhinav Prakash <abhinav.prakash319@gmail.com>
1 parent e60af08 commit e422f2b

38 files changed

+9383
-1
lines changed

samples/chaincode/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
TOP = ../..
77
include $(TOP)/build.mk
88

9-
SUB_DIRS = auction auction-go echo echo-go kv-test kv-test-go
9+
SUB_DIRS = auction auction-go echo echo-go kv-test kv-test-go confidential-escrow
1010

1111
build test clean clobber:
1212
$(foreach DIR, $(SUB_DIRS), $(MAKE) -C $(DIR) $@ || exit ;)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export CC_ID=confidential-escrow
2+
export CHANNEL_NAME=mychannel
3+
export CORE_PEER_ADDRESS=localhost:7051
4+
export CORE_PEER_ID=peer0.org1.example.com
5+
export CORE_PEER_ORG_NAME=org1
6+
export CORE_PEER_LOCALMSPID=Org1MSP
7+
export CORE_PEER_MSPCONFIGPATH=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
8+
export CORE_PEER_TLS_CERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
9+
export CORE_PEER_TLS_ENABLED="true"
10+
export CORE_PEER_TLS_KEY_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
11+
export CORE_PEER_TLS_ROOTCERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
12+
export ORDERER_CA=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
13+
export GATEWAY_CONFIG=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml
14+
export FPC_ENABLED=true
15+
export RUN_CCAAS=true
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export CC_ID=confidential-escrow
2+
export CHANNEL_NAME=mychannel
3+
export CORE_PEER_ADDRESS=localhost:9051
4+
export CORE_PEER_ID=peer0.org2.example.com
5+
export CORE_PEER_ORG_NAME=org2
6+
export CORE_PEER_LOCALMSPID=Org2MSP
7+
export CORE_PEER_MSPCONFIGPATH=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
8+
export CORE_PEER_TLS_CERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.crt
9+
export CORE_PEER_TLS_ENABLED="true"
10+
export CORE_PEER_TLS_KEY_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/server.key
11+
export CORE_PEER_TLS_ROOTCERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
12+
export ORDERER_CA=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
13+
export GATEWAY_CONFIG=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/connection-org2.yaml
14+
export FPC_ENABLED=true
15+
export RUN_CCAAS=true
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export CC_ID=confidential-escrow
2+
export CHANNEL_NAME=mychannel
3+
export CORE_PEER_ADDRESS=localhost:7051
4+
export CORE_PEER_ID=peer0.org1.example.com
5+
export CORE_PEER_ORG_NAME=org1
6+
export CORE_PEER_LOCALMSPID=Org1MSP
7+
export CORE_PEER_MSPCONFIGPATH=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
8+
export CORE_PEER_TLS_CERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
9+
export CORE_PEER_TLS_ENABLED="true"
10+
export CORE_PEER_TLS_KEY_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
11+
export CORE_PEER_TLS_ROOTCERT_FILE=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
12+
export ORDERER_CA=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
13+
export GATEWAY_CONFIG=$FPC_PATH/samples/deployment/test-network/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml
14+
export FPC_ENABLED=true
15+
export RUN_CCAAS=true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ecc
2+
ecc-bundle
3+
enclave.json
4+
private.pem
5+
public.pem
6+
mrenclave
7+
details.env
8+
9+
.env
10+
*.bak
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TOP = ../../..
2+
include $(TOP)/ecc_go/build.mk
3+
4+
CC_NAME ?= confidential-escrow
5+
6+
EGO_CONFIG_FILE = $(FPC_PATH)/samples/chaincode/confidential-escrow/confidentialEscrowEnclave.json
7+
ECC_MAIN_FILES=$(FPC_PATH)/samples/chaincode/confidential-escrow
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Confidential Escrow Chaincode
2+
3+
A privacy-preserving escrow system built on Hyperledger Fabric Private Chaincode (FPC) that enables secure digital asset management with programmable conditional payments.
4+
5+
## Overview
6+
7+
This chaincode implements a confidential escrow mechanism for digital assets, combining:
8+
9+
- **Privacy-Preserving Transactions**: All transaction data is encrypted within Intel SGX enclaves
10+
- **Programmable Escrow Contracts**: Automated conditional fund releases based on cryptographic verification
11+
- **Multi-Asset Support**: Manage multiple token types within individual wallets
12+
- **Certificate-Based Authorization**: Fine-grained access control using X.509 certificate hashes
13+
14+
## Architecture
15+
16+
### Core Components
17+
18+
**Assets**
19+
20+
- `DigitalAsset`: Fungible tokens with controlled supply (CBDC, stablecoins, etc.)
21+
- `Wallet`: User accounts supporting multiple asset types with separate available and escrowed balances
22+
- `Escrow`: Smart contracts holding funds pending condition fulfillment
23+
- `UserDirectory`: Privacy-preserving public key to wallet UUID mapping
24+
25+
**Transaction Operations**
26+
27+
- Asset lifecycle: Create, mint, transfer, burn
28+
- Wallet management: Create wallets, query balances
29+
- Escrow workflow: Lock funds, verify conditions, release or refund
30+
31+
## Project Structure
32+
33+
```
34+
confidential-escrow/
35+
├── chaincode/
36+
│ ├── assets/ # Asset type definitions
37+
│ ├── transactions/ # Transaction handlers
38+
│ ├── header/ # Chaincode metadata
39+
│ ├── escrow.go # Main chaincode implementation
40+
│ ├── server.go # CCaaS server setup
41+
│ └── setup.go # Component registration
42+
├── main.go # Entry point
43+
├── main.sh # Deployment and test automation
44+
└── README.md # This file
45+
```
46+
47+
### Security Model
48+
49+
1. **Access Control**: All operations require valid certificate hash verification
50+
2. **Atomic Escrow**: Funds move from available to escrowed balance during lock, preventing double-spending
51+
3. **Condition Verification**: SHA-256 hash of `(secret + parcelId)` ensures only authorized parties can release funds
52+
4. **Confidential Execution**: FPC ensures transaction details remain private within SGX enclaves
53+
54+
## Running Procedure
55+
56+
### Prerequisites
57+
58+
- FPC is properly set up and built
59+
- `multi_user_dashboard.sh ` script is placed in the chaincode directory
60+
- `.env.alice` and `.env.bob` file is present
61+
62+
### Setup Files
63+
64+
**1. Set FPC_PATH:**
65+
66+
```bash
67+
export FPC_PATH=/project/src/github.com/hyperledger/fabric-private-chaincode
68+
```
69+
70+
### Running Procedure
71+
72+
#### 1. In 1st terminal window - Setup and Deploy
73+
74+
```bash
75+
# Get inside dev env
76+
make -C $FPC_PATH/utils/docker run-dev
77+
cd samples/chaincode/confidential-escrow
78+
79+
# Interactive menu
80+
./multi_user_dashboard.sh
81+
82+
# Choose Option 1. or 2. as per your setup condn
83+
```
84+
85+
#### 2. In 2nd terminal window - Docker Environment (`Alice`)
86+
87+
```bash
88+
# Enter docker container
89+
docker exec -it fpc-development-main /bin/bash
90+
cd samples/chaincode/confidential-escrow
91+
92+
# Interactive menu
93+
./multi_user_dashboard.sh
94+
95+
# Setup Alice using Option 3.
96+
```
97+
98+
#### 3. In 3rd terminal window - Docker Environment (`Bob`)
99+
100+
```bash
101+
# Enter docker container
102+
docker exec -it fpc-development-main /bin/bash
103+
cd samples/chaincode/confidential-escrow
104+
105+
# Interactive menu
106+
./multi_user_dashboard.sh
107+
108+
# Setup Bob using Option 4.
109+
```
110+
111+
#### 4. In 4th terminal window - Docker Environment (`Monitor`)
112+
113+
```bash
114+
# Enter docker container
115+
docker exec -it fpc-development-main /bin/bash
116+
cd samples/chaincode/confidential-escrow
117+
118+
# Interactive menu
119+
./multi_user_dashboard.sh
120+
121+
# Setup Monitor using Option 5.
122+
```
123+
124+
#### 5. Run Tests
125+
126+
```bash
127+
# Run all basic tests
128+
./multi_user_dashboard.sh
129+
130+
# Chosing Option 7. (One can run it from any terminal)
131+
```
132+
133+
## Escrow Workflow
134+
135+
### Step 1: Create Wallets
136+
137+
Before any escrow operations, both parties must have wallets:
138+
139+
1. **Alice** creates her wallet via Terminal 2 (Option 3 - currently created automatically)
140+
2. **Bob** creates his wallet via Terminal 3 (Option 4 - currently created automatically)
141+
3. **Monitor** (Terminal 4, Option 5) can observe all wallet creations and transactions in real-time
142+
143+
### Step 2: Create Escrow
144+
145+
Once both wallets exist, either party can create an escrow using `createAndLockEscrow`. The buyer locks funds by specifying:
146+
147+
- Buyer/seller public keys
148+
- Amount and asset type
149+
- `parcelId` and `secret` (used for condition verification)
150+
151+
### Step 3: Complete Escrow
152+
153+
Two possible outcomes:
154+
155+
| Operation | Who Calls | When | Result |
156+
| ----------- | --------- | ------------------------------------------- | ------------------------ |
157+
| **Release** | Seller | Condition fulfilled (e.g., goods delivered) | Funds transfer to seller |
158+
| **Refund** | Buyer | Condition not met or cancelled | Funds return to buyer |
159+
160+
### Release vs Refund
161+
162+
- **Release** (`releaseEscrow`): Seller provides correct `secret + parcelId` to prove fulfillment. Funds move from buyer's escrow balance to seller's available balance. Status → `Released`.
163+
164+
- **Refund** (`refundEscrow`): Buyer cancels an active escrow. No secret needed. Funds return to buyer's available balance. Status → `Refunded`.
165+
166+
Both operations only work on `Active` escrows.
167+
168+
## Troubleshooting
169+
170+
**Network already running?**
171+
If your Fabric test-network is already up, comment out the `network.sh down` and `network.sh up` lines in `test.sh` to avoid restarting it:
172+
173+
```bash
174+
# In test.sh, comment these lines:
175+
# run_cmd "./network.sh down" "Bringing down network"
176+
# run_cmd "./network.sh up createChannel -ca" "Starting network"
177+
```
178+
179+
## Contributing
180+
181+
When adding new features:
182+
183+
1. Define asset types in `chaincode/assets/`
184+
2. Implement transaction logic in `chaincode/transactions/`
185+
3. Register new components in `chaincode/setup.go`
186+
4. Add test cases to `main.sh`
187+
5. Update this README with usage examples
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package assets
2+
3+
import (
4+
"github.com/hyperledger-labs/cc-tools/assets"
5+
)
6+
7+
// DigitalAssetToken defines the asset type for fungible digital tokens.
8+
// This represents confidential digital currencies such as Central Bank Digital Currencies (CBDC)
9+
// or tokenized assets. Each token type has a fixed supply controlled by the issuer.
10+
//
11+
// Security: The issuerHash ensures only authorized entities can mint/burn tokens.
12+
var DigitalAssetToken = assets.AssetType{
13+
Tag: "digitalAsset",
14+
Label: "Digital Asset Token",
15+
Description: "Confidential digital currency token (e.g., CBDC)",
16+
17+
Props: []assets.AssetProp{
18+
{
19+
Tag: "name",
20+
Label: "Token Name",
21+
DataType: "string",
22+
Required: true,
23+
},
24+
{
25+
Tag: "symbol",
26+
Label: "Token Symbol",
27+
DataType: "string",
28+
Required: true,
29+
IsKey: true,
30+
},
31+
{
32+
Tag: "decimals",
33+
Label: "Decimal Places",
34+
DataType: "number",
35+
Required: true,
36+
},
37+
{
38+
Tag: "totalSupply",
39+
Label: "Total Supply",
40+
DataType: "number",
41+
Required: true,
42+
},
43+
{
44+
Tag: "issuerHash",
45+
Label: "Issuer Certificate Hash",
46+
DataType: "string",
47+
Required: true,
48+
},
49+
{
50+
Tag: "owner",
51+
Label: "Owner Identity",
52+
DataType: "string",
53+
Required: true,
54+
},
55+
{
56+
Tag: "issuedAt",
57+
Label: "Issued At",
58+
DataType: "datetime",
59+
Required: false,
60+
},
61+
},
62+
}

0 commit comments

Comments
 (0)