Skip to content

Commit

Permalink
feat(examples): add example01-car-trade and its validatorDriver
Browse files Browse the repository at this point in the history
Signed-off-by: Takuma TAKEUCHI <takeuchi.takuma@fujitsu.com>
  • Loading branch information
takeutak committed Aug 24, 2020
1 parent 22b5f5c commit c8a84f4
Show file tree
Hide file tree
Showing 23 changed files with 1,915 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/example01-car-trade/validatorDriver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
package-lock.json
wallet
dist/core/wallet
dist/core/*.js
dist/core/bin/*.js
dist/core/config/*.js
80 changes: 80 additions & 0 deletions examples/example01-car-trade/validatorDriver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--
Copyright 2019-2020 Fujitsu Laboratories Ltd.
SPDX-License-Identifier: Apache-2.0
README.md
-->
# validatorDriver

## Abstract
This is the WebAPI version of Driver that calls Validator. Driver is called as WebAPI over HTTP.

## Premise
- Validators for Ethereum and Fabric are already running
- Before running this driver, copy the "wallet" of fabcar into the following direcotries:
- `sample01-car-trade/validatorDriver`
- `sample01-car-trade/validatorDriver/dist/core`
- (NOTICE): The above operation means that the wallet copied for using the unit-test of Fabric validator has to copied the above directories:
- The current directory is the following
- `sample01-car-trade/validatorDriver`

## How to launch

### 1) Execute after git-clone
- `npm install`

### 2) Build TypeScript codes
- `npm run build`

### 3) Launch Driver(WebAPI)
- `cd dist/core/`
- `node ./bin/www.js`
- The `www.js` script uses the port 5000.

## How to call Driver(WebAPI)

### a) Start and stop ledger monitors on Validator
- Execute the following curl commands:
```
curl "http://localhost:5000/validatorDriver?validator=fabric&func=getApiList"
curl "http://localhost:5000/validatorDriver?validator=fabric&func=startMonitor"
curl "http://localhost:5000/validatorDriver?validator=fabric&func=stopMonitor&param=x"
(*) Specify the parameter "x" of "param=x" as "id" of the response of startMonitor.
```

```
curl "http://localhost:5000/validatorDriver?validator=ethereum&func=getApiList"
curl "http://localhost:5000/validatorDriver?validator=ethereum&func=startMonitor"
curl "http://localhost:5000/validatorDriver?validator=ethereum&func=stopMonitor&param=x"
(*) Specify the parameter "x" of "param=x" as "id" of the response of startMonitor.
```

### b) Make signed transactions and send it

- Script: `testDriver_sendRawTransaction.js` (for `sendRawTransaction` on Ethereum Validator)
- Specify the following parameters when this script is launched
```
var fromAddress = "ec709e1774f0ce4aba47b52a499f9abaaa159f71";
var fromAddressPkey = "40d7e5931a6e0807d3ebd70518f635dbf575975d3bb564ff34c99be416067c89";
var toAddress = "9d624f7995e8bd70251f8265f2f9f2b49f169c55";
var amount = 50;
```
- Script: `testDriver_sendSignedProposal.js` (for `sendSignedProposal` on Fabric Validator)
- Specify the following parameters when this script is launched
```
var carId = "CAR101";
var newOwner = "Charlie111";
```

#### Prepare before executing the scripts

- Change the following environment values

```
const basic_network_path = "/xxxxx/xxxx/unit-test/fabric-docker/basic-network";
(*) the path of basic-network of fabric/sample
const privateKeyPath0 = basic_network_path + '/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/c75bd6911aca808941c3557ee7c97e90f3952e379497dc55eb903f31b50abc83_sk';
(*) it is needed to change the part "c75bd6911aca808941c3557ee7c97e90f3952e379497dc55eb903f31b50abc83_sk" as your environment
const privateKeyPath = walletPath + '/admin/6872965e4870c3ab435769ceef8ecc5783b7c670a2c0572b6345cf91af1e43aa-priv';
(*) it is needed to change the part "6872965e4870c3ab435769ceef8ecc5783b7c670a2c0572b6345cf91af1e43aa-priv" as your environment
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "basic-network",
"version": "1.0.0",
"client": {
"organization": "Org1",
"connection": {
"timeout": {
"peer": {
"endorser": "300"
},
"orderer": "300"
}
}
},
"channels": {
"mychannel": {
"orderers": [
"orderer.example.com"
],
"peers": {
"peer0.org1.example.com": {}
}
}
},
"organizations": {
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
],
"certificateAuthorities": [
"ca.example.com"
]
}
},
"orderers": {
"orderer.example.com": {
"url": "grpc://localhost:7050"
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpc://localhost:7051"
}
},
"certificateAuthorities": {
"ca.example.com": {
"url": "http://localhost:7054",
"caName": "ca.example.com"
}
}
}
41 changes: 41 additions & 0 deletions examples/example01-car-trade/validatorDriver/config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
* SPDX-License-Identifier: Apache-2.0
*
* default.js
*/

module.exports = {
// URL to validator
//"validatorUrl" : 'https://localhost:5040',
"validatorUrl" : 'https://localhost:5041',

// for fabric
"fabric":{
"mspid": "Org1MSP",
"keystore":"./wallet/admin", // TODO:
"connUserName":"user1", // TODO:
"contractName":"fabcar", // TODO:
"peers":[
{
"name":"peer0.org1.example.com",
"requests": "grpc://localhost:7051",
}
],

"orderer":{
"name":"orderer.example.com",
"url":"grpc://localhost:7050",
},
"ca":{
"name":"ca.example.com",
"url":"http://localhost:7054"
},
"submitter":{
"name":"admin",
"secret":"adminpw"
},
"channelName":"mychannel",
"chaincodeId":"easy_sample_ec1"
}
};
29 changes: 29 additions & 0 deletions examples/example01-car-trade/validatorDriver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "webapi",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "tsc",
"watch": "tsc --watch"
},
"devDependencies": {
"typescript": "^3.9.3"
},
"dependencies": {
"@types/node": "^14.0.18",
"body-parser": "~1.17.1",
"config": "^1.26.1",
"cookie-parser": "~1.4.3",
"debug": "~4.1.1",
"express": "~4.15.2",
"log4js": "^3.0.6",
"morgan": "~1.8.1",
"serve-favicon": "~2.4.2",
"socket.io": "^2.0.4",
"ethereumjs-common": "^1.5.1",
"ethereumjs-tx": "^2.1.2",
"web3": "^1.2.9",
"fabric-ca-client": "~1.4.0",
"fabric-network": "~1.4.0"
}
}
74 changes: 74 additions & 0 deletions examples/example01-car-trade/validatorDriver/queryCar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2019-2020 Fujitsu Laboratories Ltd.
* SPDX-License-Identifier: Apache-2.0
*
* queryCar.js
*/

////////
// Usage
//
////////

'use strict';

const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const path = require('path');

const ccpPath = path.resolve(__dirname, 'config', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);

const walletPath = path.resolve(__dirname, 'wallet');


const userName = 'user1';
const channelName = 'mychannel';

async function main() {
try {

// Create a new file system based wallet for managing identities.
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(userName);
if (!userExists) {
console.log('An identity for the user "' + userName + '" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}

// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: userName, discovery: { enabled: false } });

// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');

// Get the contract from the network.
const contract = network.getContract('fabcar');

// Evaluate the specified transaction.
if (process.argv.length > 2) {
const key = process.argv[2];
//console.log('##queryCar Params: ' + key);
console.log(`##queryCar Params: ${key}`);
const result = await contract.evaluateTransaction('queryCar', key);
console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
}
else {
console.log('##queryAllCars: ');
const result = await contract.evaluateTransaction('queryAllCars');
console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
}

} catch (error) {
console.error(`Failed to evaluate transaction: ${error}`);
process.exit(1);
}
}

main();
10 changes: 10 additions & 0 deletions examples/example01-car-trade/validatorDriver/src/CA/connector.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIBdTCCARoCCQC/F+Mh551QzDAKBggqhkjOPQQDAjBCMQswCQYDVQQGEwJKUDEQ
MA4GA1UECAwHZXNqbXMxMjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkg
THRkMB4XDTE4MDYyNzA3MjIzNVoXDTI4MDYyNDA3MjIzNVowQjELMAkGA1UEBhMC
SlAxEDAOBgNVBAgMB2Vzam1zMTIxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMg
UHR5IEx0ZDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABDPpSD2w0zrqJKraGD1b
5Jq2sDuacThSUqi7fvz8oyrWtuKDjZ15zIaSOtak6XRxFh9V9Gokdg5GNbW/pTZc
TuowCgYIKoZIzj0EAwIDSQAwRgIhAKH6ERsyd5bpEMIkY4clPqguwDWoTLk2VKq6
ONEhUqotAiEA4yJxGmZpFdRScG2gDUIF2VDeX+XfHdJI2J41hyW9/zI=
-----END CERTIFICATE-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN CERTIFICATE REQUEST-----
MIH9MIGkAgEAMEIxCzAJBgNVBAYTAkpQMRAwDgYDVQQIDAdlc2ptczEyMSEwHwYD
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwWTATBgcqhkjOPQIBBggqhkjO
PQMBBwNCAAQz6Ug9sNM66iSq2hg9W+SatrA7mnE4UlKou378/KMq1rbig42decyG
kjrWpOl0cRYfVfRqJHYORjW1v6U2XE7qoAAwCgYIKoZIzj0EAwIDSAAwRQIgCUA1
B5mZK7Hx79J1xBb0MGwuoUkt4bGPXbHqWEMZXQMCIQCRgadPkrNw56+pT5MVxA5K
vV6xTgmxUYrYnpkR4tptqQ==
-----END CERTIFICATE REQUEST-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEICIlCfK3zMTFzUgdaj01LAHjJmHlbg6Xql9+i70iPz5EoAoGCCqGSM49
AwEHoUQDQgAEM+lIPbDTOuokqtoYPVvkmrawO5pxOFJSqLt+/PyjKta24oONnXnM
hpI61qTpdHEWH1X0aiR2DkY1tb+lNlxO6g==
-----END EC PRIVATE KEY-----

0 comments on commit c8a84f4

Please sign in to comment.