diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 00000000..6fce7c45 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,64 @@ +# +# SPDX-License-Identifier: Apache-2.0 +# +name: Lint +run-name: ${{ github.actor }} is linting fabric-samples + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +env: + GO_VER: 1.18.3 + NODE_VER: 16.x + JAVA_VER: 11.x + +jobs: + go: + runs-on: ubuntu-20.04 + steps: + - uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VER }} + - uses: actions/checkout@v3 + - run: go install golang.org/x/tools/cmd/goimports@latest + - run: ci/scripts/lint-go.sh + + typescript: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ env.NODE_VER }} + - run: ci/scripts/lint-typescript.sh + + javascript: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ env.NODE_VER }} + - run: ci/scripts/lint-javascript.sh + + java: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: ${{ env.JAVA_VER }} + - run: ci/scripts/lint-java.sh + + shell: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - run: ci/scripts/lint-shell.sh diff --git a/.github/workflows/test-fsat-appdev.yaml b/.github/workflows/test-fsat-appdev.yaml index 88d225bb..1849ec84 100644 --- a/.github/workflows/test-fsat-appdev.yaml +++ b/.github/workflows/test-fsat-appdev.yaml @@ -9,34 +9,39 @@ on: paths: - "full-stack-asset-transfer-guide/**" +env: + GO_VER: 1.18.3 + NODE_VER: 16.x + K9S_VERSION: v0.25.3 + JUST_VERSION: 1.2.0 + jobs: - test-fsat-appdev: - runs-on: ubuntu-latest + appdev: + runs-on: ubuntu-20.04 steps: - - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" - - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." + - uses: actions/checkout@v3 - - name: checkout - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VER }} - - name: k9s - env: - K9S_VERSION: v0.25.3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ env.NODE_VER }} + + - name: Install k9s run: | curl --fail --silent --show-error -L https://github.com/derailed/k9s/releases/download/${K9S_VERSION}/k9s_Linux_x86_64.tar.gz -o /tmp/k9s_Linux_x86_64.tar.gz tar -zxf /tmp/k9s_Linux_x86_64.tar.gz -C /usr/local/bin k9s sudo chown root /usr/local/bin/k9s sudo chmod 755 /usr/local/bin/k9s - - name: just - env: - JUST_VERSION: 1.2.0 + - name: Install just run: | curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --tag ${JUST_VERSION} --to /usr/local/bin - - name: weft + - name: Install weft run: | npm install -g @hyperledger-labs/weft diff --git a/.github/workflows/test-network-basic.yaml b/.github/workflows/test-network-basic.yaml new file mode 100644 index 00000000..916ba1e6 --- /dev/null +++ b/.github/workflows/test-network-basic.yaml @@ -0,0 +1,45 @@ +name: Test Network Basic +run-name: ${{ github.actor }} is running the test-network basic tests + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + basic: + runs-on: ubuntu-20.04 + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Pull Fabric Docker Images + run: curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- docker + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Run Test Network Basic + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-basic.sh + diff --git a/.github/workflows/test-network-gateway.yaml b/.github/workflows/test-network-gateway.yaml new file mode 100644 index 00000000..3fdb4727 --- /dev/null +++ b/.github/workflows/test-network-gateway.yaml @@ -0,0 +1,45 @@ +name: Test Network Gateway +run-name: ${{ github.actor }} is running the test-network gateway tests + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + gateway: + runs-on: ubuntu-20.04 + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Pull Fabric Docker Images + run: curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- docker + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Run Test Network Off Chain + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-gateway.sh + diff --git a/.github/workflows/test-network-hsm.yaml b/.github/workflows/test-network-hsm.yaml new file mode 100644 index 00000000..da7c6ef2 --- /dev/null +++ b/.github/workflows/test-network-hsm.yaml @@ -0,0 +1,54 @@ +name: Test Network HSM +run-name: ${{ github.actor }} is running the test-network HSM tests + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + hsm: + runs-on: ubuntu-20.04 + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Pull Fabric Docker Images + run: curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- docker + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Install SoftHSM + run: sudo apt install -y softhsm2 + + - name: Set up SoftHSM + run: | + echo directories.tokendir = /tmp > $HOME/softhsm2.conf + export SOFTHSM2_CONF=$HOME/softhsm2.conf + softhsm2-util --init-token --slot 0 --label "ForFabric" --pin 98765432 --so-pin 1234 + + - name: Run Test Network HSM + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-hsm.sh + diff --git a/.github/workflows/test-network-off-chain-data.yaml b/.github/workflows/test-network-off-chain-data.yaml new file mode 100644 index 00000000..b6e011ae --- /dev/null +++ b/.github/workflows/test-network-off-chain-data.yaml @@ -0,0 +1,45 @@ +name: Test Network Off Chain +run-name: ${{ github.actor }} is running the test-network off chain data tests + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + off-chain: + runs-on: ubuntu-20.04 + + strategy: + matrix: + chaincode-language: + - go + - javascript + - typescript + - java + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Install fabric CLI + run: | + curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary + echo ${PWD}/bin >> $GITHUB_PATH + + - name: Pull Fabric Docker Images + run: curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- docker + + - name: Install retry CLI + run: curl -sSL https://raw.githubusercontent.com/kadwanev/retry/master/retry -o ./bin/retry && chmod +x ./bin/retry + + - name: Run Test Network Off Chain + working-directory: test-network + env: + CHAINCODE_LANGUAGE: ${{ matrix.chaincode-language }} + run: ../ci/scripts/run-test-network-off-chain.sh + diff --git a/asset-transfer-basic/application-gateway-go/assetTransfer.go b/asset-transfer-basic/application-gateway-go/assetTransfer.go index b20f043b..e9ccbe5a 100755 --- a/asset-transfer-basic/application-gateway-go/assetTransfer.go +++ b/asset-transfer-basic/application-gateway-go/assetTransfer.go @@ -34,8 +34,6 @@ const ( tlsCertPath = cryptoPath + "/peers/peer0.org1.example.com/tls/ca.crt" peerEndpoint = "localhost:7051" gatewayPeer = "peer0.org1.example.com" - channelName = "mychannel" - chaincodeName = "basic" ) var now = time.Now() @@ -67,6 +65,17 @@ func main() { } defer gw.Close() + // Override default values for chaincode and channel name as they may differ in testing contexts. + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + network := gw.GetNetwork(channelName) contract := network.GetContract(chaincodeName) diff --git a/asset-transfer-basic/application-gateway-java/src/main/java/App.java b/asset-transfer-basic/application-gateway-java/src/main/java/App.java index 83cbb55b..7fcf3962 100644 --- a/asset-transfer-basic/application-gateway-java/src/main/java/App.java +++ b/asset-transfer-basic/application-gateway-java/src/main/java/App.java @@ -34,22 +34,22 @@ import java.time.Instant; import java.util.concurrent.TimeUnit; public final class App { - private static final String mspID = "Org1MSP"; - private static final String channelName = "mychannel"; - private static final String chaincodeName = "basic"; + private static final String MSP_ID = System.getenv().getOrDefault("MSP_ID", "Org1MSP"); + private static final String CHANNEL_NAME = System.getenv().getOrDefault("CHANNEL_NAME", "mychannel"); + private static final String CHAINCODE_NAME = System.getenv().getOrDefault("CHAINCODE_NAME", "basic"); // Path to crypto materials. - private static final Path cryptoPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com"); + private static final Path CRYPTO_PATH = Paths.get("../../test-network/organizations/peerOrganizations/org1.example.com"); // Path to user certificate. - private static final Path certPath = cryptoPath.resolve(Paths.get("users", "User1@org1.example.com", "msp", "signcerts", "cert.pem")); + private static final Path CERT_PATH = CRYPTO_PATH.resolve(Paths.get("users/User1@org1.example.com/msp/signcerts/cert.pem")); // Path to user private key directory. - private static final Path keyDirPath = cryptoPath.resolve(Paths.get("users", "User1@org1.example.com", "msp", "keystore")); + private static final Path KEY_DIR_PATH = CRYPTO_PATH.resolve(Paths.get("users/User1@org1.example.com/msp/keystore")); // Path to peer tls certificate. - private static final Path tlsCertPath = cryptoPath.resolve(Paths.get("peers", "peer0.org1.example.com", "tls", "ca.crt")); + private static final Path TLS_CERT_PATH = CRYPTO_PATH.resolve(Paths.get("peers/peer0.org1.example.com/tls/ca.crt")); // Gateway peer end point. - private static final String peerEndpoint = "localhost:7051"; - private static final String overrideAuth = "peer0.org1.example.com"; + private static final String PEER_ENDPOINT = "localhost:7051"; + private static final String OVERRIDE_AUTH = "peer0.org1.example.com"; private final Contract contract; private final String assetId = "asset" + Instant.now().toEpochMilli(); @@ -75,19 +75,19 @@ public final class App { } private static ManagedChannel newGrpcConnection() throws IOException, CertificateException { - var tlsCertReader = Files.newBufferedReader(tlsCertPath); + var tlsCertReader = Files.newBufferedReader(TLS_CERT_PATH); var tlsCert = Identities.readX509Certificate(tlsCertReader); - return NettyChannelBuilder.forTarget(peerEndpoint) - .sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(overrideAuth) + return NettyChannelBuilder.forTarget(PEER_ENDPOINT) + .sslContext(GrpcSslContexts.forClient().trustManager(tlsCert).build()).overrideAuthority(OVERRIDE_AUTH) .build(); } private static Identity newIdentity() throws IOException, CertificateException { - var certReader = Files.newBufferedReader(certPath); + var certReader = Files.newBufferedReader(CERT_PATH); var certificate = Identities.readX509Certificate(certReader); - return new X509Identity(mspID, certificate); + return new X509Identity(MSP_ID, certificate); } private static Signer newSigner() throws IOException, InvalidKeyException { @@ -98,7 +98,7 @@ public final class App { } private static Path getPrivateKeyPath() throws IOException { - try (var keyFiles = Files.list(keyDirPath)) { + try (var keyFiles = Files.list(KEY_DIR_PATH)) { return keyFiles.findFirst().orElseThrow(); } } @@ -106,10 +106,10 @@ public final class App { public App(final Gateway gateway) { // Get a network instance representing the channel where the smart contract is // deployed. - var network = gateway.getNetwork(channelName); + var network = gateway.getNetwork(CHANNEL_NAME); // Get the smart contract from the network. - contract = network.getContract(chaincodeName); + contract = network.getContract(CHAINCODE_NAME); } public void run() throws GatewayException, CommitException { diff --git a/asset-transfer-basic/application-go/assetTransfer.go b/asset-transfer-basic/application-go/assetTransfer.go index eb102656..7eb62017 100644 --- a/asset-transfer-basic/application-go/assetTransfer.go +++ b/asset-transfer-basic/application-go/assetTransfer.go @@ -55,12 +55,24 @@ func main() { } defer gw.Close() - network, err := gw.GetNetwork("mychannel") + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + + log.Println("--> Connecting to channel", channelName) + network, err := gw.GetNetwork(channelName) if err != nil { log.Fatalf("Failed to get network: %v", err) } - contract := network.GetContract("basic") + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + log.Println("--> Using chaincode", chaincodeName) + contract := network.GetContract(chaincodeName) log.Println("--> Submit Transaction: InitLedger, function creates the initial set of assets on the ledger") result, err := contract.SubmitTransaction("InitLedger") diff --git a/asset-transfer-basic/application-typescript-hsm/src/app.ts b/asset-transfer-basic/application-typescript-hsm/src/app.ts index 46b8ffed..84ffbe58 100644 --- a/asset-transfer-basic/application-typescript-hsm/src/app.ts +++ b/asset-transfer-basic/application-typescript-hsm/src/app.ts @@ -12,9 +12,10 @@ import { enrollUserToWallet, registerUser, UserToEnroll, UserToRegister } from ' const walletPath = path.join(__dirname, 'wallet'); -// define information about the channel and chaincode id that will be driven by this application -const channelName = 'mychannel'; -const chaincodeId = 'basic'; +// define information about the channel and chaincode that will be driven by this application +const channelName = envOrDefault('CHANNEL_NAME', 'mychannel'); +const chaincodeName = envOrDefault('CHAINCODE_NAME', 'default-basic'); + // define the CA Registrar const mspOrg1 = 'Org1MSP'; @@ -192,7 +193,7 @@ async function main() { const network = await gateway.getNetwork(channelName); // Get the contract from the network. - const contract = network.getContract(chaincodeId); + const contract = network.getContract(chaincodeName); // loop around all transactions to send, each one will be sent sequentially // through the same gateway/network/contract as subsequent transations expect the @@ -274,5 +275,12 @@ async function interactWithFabric(contract: Contract, transactionToPerform: Tran } } +/** + * envOrDefault() will return the value of an environment variable, or a default value if the variable is undefined. + */ +function envOrDefault(key: string, defaultValue: string): string { + return process.env[key] || defaultValue; +} + // execute the main function main(); diff --git a/asset-transfer-basic/chaincode-java/settings.gradle b/asset-transfer-basic/chaincode-java/settings.gradle index 2633c4b9..b8074969 100644 --- a/asset-transfer-basic/chaincode-java/settings.gradle +++ b/asset-transfer-basic/chaincode-java/settings.gradle @@ -1,5 +1,4 @@ /* * SPDX-License-Identifier: Apache-2.0 */ - -rootProject.name = 'basic' +rootProject.name = System.getenv('CHAINCODE_NAME') ?: 'basic' \ No newline at end of file diff --git a/ci/azure-pipelines.yml b/ci/azure-pipelines.yml index 1fbb352c..35794db6 100644 --- a/ci/azure-pipelines.yml +++ b/ci/azure-pipelines.yml @@ -23,6 +23,7 @@ variables: - group: credentials jobs: + - job: REST_Sample displayName: REST Server Sample pool: @@ -86,51 +87,6 @@ jobs: - template: templates/install-deps.yml - template: templates/fabcar/azure-pipelines-typescript.yml - - job: Lint - displayName: Lint - pool: - vmImage: ubuntu-20.04 - steps: - - task: GoTool@0 - inputs: - goBin: $(GO_BIN) - version: $(GO_VER) - displayName: Install GoLang - - task: NodeTool@0 - inputs: - versionSpec: $(NODE_VER) - displayName: Install Node.js - - script: ./ci/scripts/shellcheck.sh - displayName: Lint Shell Scripts - - script: ./ci/scripts/lint.sh - displayName: Lint Code - - - job: TestNetworkBasic - displayName: Test Network - pool: - vmImage: ubuntu-20.04 - strategy: - matrix: - Basic-Go: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: go - Basic-Java: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: java - Basic-Javascript: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: javascript - Basic-Typescript: - CHAINCODE_NAME: basic - CHAINCODE_LANGUAGE: typescript - - steps: - - template: templates/install-deps.yml - - template: templates/install-deps-hsm.yml - - script: ../ci/scripts/run-test-network-basic.sh - workingDirectory: test-network - displayName: Run Test Network Basic Chaincode - - job: KubeTestNetworkBasic displayName: Kube Test Network pool: diff --git a/ci/scripts/lint-go.sh b/ci/scripts/lint-go.sh new file mode 100755 index 00000000..e869ef1d --- /dev/null +++ b/ci/scripts/lint-go.sh @@ -0,0 +1,34 @@ +#!/bin/bash +set -euo pipefail + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +dirs=("$(find . -name "*-go" -type d -not -path '*/.*')") +for dir in $dirs; do + print "Linting $dir" + pushd $dir + + print "Running go vet" + go vet -tags pkcs11 ./... + + print "Running gofmt" + output=$(gofmt -l -s $(go list -tags pkcs11 -f '{{.Dir}}' ./...)) + if [[ "${output}" != "" ]]; then + print "The following files contain formatting errors, please run 'gofmt -l -w ' to fix these issues:" + echo "${output}" + fi + + print "Running goimports" + output=$(goimports -l $(go list -tags pkcs11 -f '{{.Dir}}' ./...)) + if [[ "${output}" != "" ]]; then + print "The following files contain import errors, please run 'goimports -l -w ' to fix these issues:" + echo "${output}" + fi + + popd +done \ No newline at end of file diff --git a/ci/scripts/lint-java.sh b/ci/scripts/lint-java.sh new file mode 100755 index 00000000..327a91e7 --- /dev/null +++ b/ci/scripts/lint-java.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -euo pipefail + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +dirs=("$(find . -name "*-java" -type d -not -path '*/.*')") +for dir in $dirs; do + print "Linting $dir" + pushd $dir + + if [[ -f "pom.xml" ]]; then + print "Running Maven Build" + mvn clean package + else + print "Running Gradle Build" + ./gradlew build + fi + popd +done \ No newline at end of file diff --git a/ci/scripts/lint-javascript.sh b/ci/scripts/lint-javascript.sh new file mode 100755 index 00000000..0279b125 --- /dev/null +++ b/ci/scripts/lint-javascript.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -euo pipefail + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +dirs=("$(find . -name "*-javascript" -type d -not -path '*/.*')") +for dir in $dirs; do + print "Linting $dir" + pushd $dir + + print "Installing node modules" + npm install + print "Running Lint" + npm run lint + + popd +done \ No newline at end of file diff --git a/ci/scripts/shellcheck.sh b/ci/scripts/lint-shell.sh similarity index 100% rename from ci/scripts/shellcheck.sh rename to ci/scripts/lint-shell.sh diff --git a/ci/scripts/lint-typescript.sh b/ci/scripts/lint-typescript.sh new file mode 100755 index 00000000..86bcc36b --- /dev/null +++ b/ci/scripts/lint-typescript.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -euo pipefail + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +dirs=("$(find . -name "*-typescript" -type d -not -path '*/.*')") +for dir in $dirs; do + print "Linting $dir" + pushd $dir + + print "Installing node modules" + npm install + print "Running Lint" + npm run lint + + popd +done \ No newline at end of file diff --git a/ci/scripts/lint.sh b/ci/scripts/lint.sh index 74b4f495..842be747 100755 --- a/ci/scripts/lint.sh +++ b/ci/scripts/lint.sh @@ -1,50 +1,10 @@ #!/bin/bash set -euo pipefail -function print() { - GREEN='\033[0;32m' - NC='\033[0m' - echo - echo -e "${GREEN}${1}${NC}" -} +cd "$(dirname "$0")/../.." -go install golang.org/x/tools/cmd/goimports@latest - -dirs=("$(find . -name "*-go" -o -name "*-java" -o -name "*-javascript" -o -name "*-typescript" -not -path '*/.*')") -for dir in $dirs; do - if [[ -d $dir ]] && [[ ! $dir =~ node_modules ]]; then - print "Linting $dir" - pushd $dir - if [[ "$dir" =~ "-go" ]]; then - print "Running go vet" - go vet -tags pkcs11 ./... - print "Running gofmt" - output=$(gofmt -l -s $(go list -tags pkcs11 -f '{{.Dir}}' ./...)) - if [[ "${output}" != "" ]]; then - print "The following files contain formatting errors, please run 'gofmt -l -w ' to fix these issues:" - echo "${output}" - fi - - print "Running goimports" - output=$(goimports -l $(go list -tags pkcs11 -f '{{.Dir}}' ./...)) - if [[ "${output}" != "" ]]; then - print "The following files contain import errors, please run 'goimports -l -w ' to fix these issues:" - echo "${output}" - fi - elif [[ "$dir" =~ "-javascript" || "$dir" =~ "-typescript" ]]; then - print "Installing node modules" - npm install - print "Running Lint" - npm run lint - elif [[ "$dir" =~ "-java" ]]; then - if [[ -f "pom.xml" ]]; then - print "Running Maven Build" - mvn clean package - else - print "Running Gradle Build" - ./gradlew build - fi - fi - popd - fi -done +ci/scripts/lint-go.sh +ci/scripts/lint-javascript.sh +ci/scripts/lint-typescript.sh +ci/scripts/lint-java.sh +ci/scripts/lint-shell.sh \ No newline at end of file diff --git a/ci/scripts/run-test-network-basic.sh b/ci/scripts/run-test-network-basic.sh index 5194fbd0..3f323eb1 100755 --- a/ci/scripts/run-test-network-basic.sh +++ b/ci/scripts/run-test-network-basic.sh @@ -1,7 +1,8 @@ +#!/bin/bash + set -euo pipefail CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} -CHAINCODE_NAME=${CHAINCODE_NAME:-basic} CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} function print() { @@ -17,6 +18,9 @@ function createNetwork() { cd addOrg3 ./addOrg3.sh up -ca -s couchdb cd .. +} + +function deployChaincode() { print "Deploying ${CHAINCODE_NAME} chaincode" ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" } @@ -26,46 +30,43 @@ function stopNetwork() { ./network.sh down } -# Run Go application +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. createNetwork + + +# Run Go application print "Initializing Go application" +export CHAINCODE_NAME=basic_go +deployChaincode pushd ../asset-transfer-basic/application-go print "Executing AssetTransfer.go" go run . popd -stopNetwork # Run Java application -createNetwork print "Initializing Java application" +export CHAINCODE_NAME=basic_java +deployChaincode pushd ../asset-transfer-basic/application-java print "Executing Gradle Run" gradle run popd -stopNetwork - -# Run Java application using gateway -createNetwork -print "Initializing Java application" -pushd ../asset-transfer-basic/application-gateway-java -print "Executing Gradle Run" -./gradlew run -popd -stopNetwork # Run Javascript application -createNetwork print "Initializing Javascript application" +export CHAINCODE_NAME=basic_javascript +deployChaincode pushd ../asset-transfer-basic/application-javascript npm install print "Executing app.js" node app.js popd -stopNetwork # Run typescript application -createNetwork print "Initializing Typescript application" +export CHAINCODE_NAME=basic_typescript +deployChaincode pushd ../asset-transfer-basic/application-typescript npm install print "Building app.ts" @@ -73,95 +74,6 @@ npm run build print "Running the output app" node dist/app.js popd -stopNetwork -# Run gateway typescript application -createNetwork -print "Initializing Typescript gateway application" -pushd ../asset-transfer-basic/application-gateway-typescript -npm install -print "Building app.ts" -npm run build -print "Running the output app" -node dist/app.js -popd -stopNetwork -# Run typescript HSM application -createNetwork -print "Initializing Typescript HSM application" -pushd ../asset-transfer-basic/application-typescript-hsm -print "Setup SoftHSM" -export SOFTHSM2_CONF=$PWD/softhsm2.conf -print "install dependencies" -npm install -print "Building app.ts" -npm run build -print "Running the output app" -node dist/app.js -popd -stopNetwork - -# Run Typescript HSM gateway application -echo 'Delete fabric-ca-client from samples bin' -rm ../bin/fabric-ca-client -echo 'go install pkcs11 enabled fabric-ca-client' -go install -tags pkcs11 github.com/hyperledger/fabric-ca/cmd/fabric-ca-client@latest -createNetwork -print "Initializing Typescript HSM gateway application" -pushd ../hardware-security-module/scripts/ -print "Enroll and register User in HSM" -./generate-hsm-user.sh HSMUser -pushd ../application-typescript/ -print "install dependencies and prepare for running" -npm install -print "Running the output app" -npm run start -popd -popd -stopNetwork - -# Run Go HSM gateway application -createNetwork -print "Initializing Go HSM gateway application" -pushd ../hardware-security-module/scripts/ -print "Register and enroll user in HSM" -./generate-hsm-user.sh HSMUser -pushd ../application-go -print "Running the output app" -go run -tags pkcs11 . -popd -popd -stopNetwork - -# Run Go gateway application -createNetwork -print "Initializing Go gateway application" -pushd ../asset-transfer-basic/application-gateway-go -print "Executing AssetTransfer.go" -go run . -popd -stopNetwork - -# Run off-chain data TypeScript application -createNetwork -print "Initializing Typescript off-chain data application" -pushd ../off_chain_data/application-typescript -rm -f checkpoint.json store.log -npm install -print "Running the output app" -SIMULATED_FAILURE_COUNT=1 npm start getAllAssets transact getAllAssets listen -SIMULATED_FAILURE_COUNT=1 npm start listen -popd -stopNetwork - -# Run off-chain data Java application -createNetwork -print "Initializing Typescript off-chain data application" -pushd ../off_chain_data/application-java -rm -f app/checkpoint.json app/store.log -print "Running the output app" -SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args='getAllAssets transact getAllAssets listen' -SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args=listen -popd stopNetwork diff --git a/ci/scripts/run-test-network-gateway.sh b/ci/scripts/run-test-network-gateway.sh new file mode 100755 index 00000000..3b3def77 --- /dev/null +++ b/ci/scripts/run-test-network-gateway.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + + +# Run Go gateway application +print "Initializing Go gateway application" +export CHAINCODE_NAME=go_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-go +print "Executing AssetTransfer.go" +go run . +popd + + +# Run gateway typescript application +print "Initializing Typescript gateway application" +export CHAINCODE_NAME=typescript_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-typescript +npm install +print "Building app.ts" +npm run build +print "Running the output app" +node dist/app.js +popd + + +# Run Java application using gateway +print "Initializing Java application" +export CHAINCODE_NAME=java_gateway +deployChaincode +pushd ../asset-transfer-basic/application-gateway-java +print "Executing Gradle Run" +./gradlew run +popd + + +stopNetwork diff --git a/ci/scripts/run-test-network-hsm.sh b/ci/scripts/run-test-network-hsm.sh new file mode 100755 index 00000000..5ecd1715 --- /dev/null +++ b/ci/scripts/run-test-network-hsm.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + + +# Run typescript HSM application +print "Initializing Typescript HSM application" +export CHAINCODE_NAME=typescript_hsm +deployChaincode +pushd ../asset-transfer-basic/application-typescript-hsm +print "Setup SoftHSM" +export SOFTHSM2_CONF=$PWD/softhsm2.conf +print "install dependencies" +npm install +print "Building app.ts" +npm run build +print "Running the output app" +node dist/app.js +popd + +# Run Typescript HSM gateway application +print "Initializing Typescript HSM Gateway application" +export CHAINCODE_NAME=ts_hsm_gateway +deployChaincode +echo 'Delete fabric-ca-client from samples bin' +rm ../bin/fabric-ca-client +echo 'go install pkcs11 enabled fabric-ca-client' +GOBIN=${PWD}/../bin go install -tags pkcs11 github.com/hyperledger/fabric-ca/cmd/fabric-ca-client@latest +fabric-ca-client version +print "Initializing Typescript HSM gateway application" +pushd ../hardware-security-module/scripts/ +print "Enroll and register User in HSM" +./generate-hsm-user.sh HSMUser +pushd ../application-typescript/ +print "install dependencies and prepare for running" +npm install +print "Running the output app" +npm run start +popd +popd + +# Run Go HSM gateway application +print "Initializing Go HSM gateway application" +export CHAINCODE_NAME=go_hsm +deployChaincode +pushd ../hardware-security-module/scripts/ +print "Register and enroll user in HSM" +./generate-hsm-user.sh HSMUser +pushd ../application-go +print "Running the output app" +go run -tags pkcs11 . +popd +popd + + + +stopNetwork diff --git a/ci/scripts/run-test-network-off-chain.sh b/ci/scripts/run-test-network-off-chain.sh new file mode 100755 index 00000000..497aef26 --- /dev/null +++ b/ci/scripts/run-test-network-off-chain.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +set -euo pipefail + +CHAINCODE_LANGUAGE=${CHAINCODE_LANGUAGE:-go} +CHAINCODE_PATH=${CHAINCODE_PATH:-../asset-transfer-basic} + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +function createNetwork() { + print "Creating 3 Org network" + ./network.sh up createChannel -ca -s couchdb + cd addOrg3 + ./addOrg3.sh up -ca -s couchdb + cd .. +} + +function deployChaincode() { + print "Deploying ${CHAINCODE_NAME} chaincode" + ./network.sh deployCC -ccn "${CHAINCODE_NAME}" -ccp "${CHAINCODE_PATH}/chaincode-${CHAINCODE_LANGUAGE}" -ccv 1 -ccs 1 -ccl "${CHAINCODE_LANGUAGE}" +} + +function stopNetwork() { + print "Stopping network" + ./network.sh down +} + +# Set up one test network to run each test scenario. +# Each test will create an independent scope by installing a new chaincode contract to the channel. +createNetwork + +# Run off-chain data TypeScript application +export CHAINCODE_NAME=ts_off_chain_data +deployChaincode +print "Initializing Typescript off-chain data application" +pushd ../off_chain_data/application-typescript +rm -f checkpoint.json store.log +npm install +print "Running the output app" +SIMULATED_FAILURE_COUNT=1 npm start getAllAssets transact getAllAssets listen +SIMULATED_FAILURE_COUNT=1 npm start listen +popd + +# Run off-chain data Java application +#createNetwork +export CHAINCODE_NAME=off_chain_data +deployChaincode +print "Initializing off-chain data application" +pushd ../off_chain_data/application-java +rm -f app/checkpoint.json app/store.log +print "Running the output app" +SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args='getAllAssets transact getAllAssets listen' +SIMULATED_FAILURE_COUNT=1 ./gradlew run --quiet --args=listen +popd + +stopNetwork diff --git a/hardware-security-module/application-go/hsm-sample.go b/hardware-security-module/application-go/hsm-sample.go index 9663f764..54c92eed 100644 --- a/hardware-security-module/application-go/hsm-sample.go +++ b/hardware-security-module/application-go/hsm-sample.go @@ -76,8 +76,20 @@ func main() { } func exampleTransaction(gateway *client.Gateway) { - network := gateway.GetNetwork("mychannel") - contract := network.GetContract("basic") + + // Override default values for chaincode and channel name as they may differ in testing contexts. + channelName := "mychannel" + if cname := os.Getenv("CHANNEL_NAME"); cname != "" { + channelName = cname + } + + chaincodeName := "basic" + if ccname := os.Getenv("CHAINCODE_NAME"); ccname != "" { + chaincodeName = ccname + } + + network := gateway.GetNetwork(channelName) + contract := network.GetContract(chaincodeName) fmt.Printf("Submit Transaction: CreateAsset, creates new asset with ID, Color, Size, Owner and AppraisedValue arguments \n") diff --git a/hardware-security-module/application-typescript/src/hsm-sample.ts b/hardware-security-module/application-typescript/src/hsm-sample.ts index c99fcd68..40490718 100644 --- a/hardware-security-module/application-typescript/src/hsm-sample.ts +++ b/hardware-security-module/application-typescript/src/hsm-sample.ts @@ -62,8 +62,12 @@ async function main() { } async function exampleTransaction(gateway: Gateway):Promise { - const network = gateway.getNetwork('mychannel'); - const contract = network.getContract('basic'); + + const channelName = envOrDefault('CHANNEL_NAME', 'mychannel'); + const chaincodeName = envOrDefault('CHAINCODE_NAME', 'default-basic'); + + const network = gateway.getNetwork(channelName); + const contract = network.getContract(chaincodeName); console.log('\n--> Submit Transaction: CreateAsset, creates new asset with ID, Color, Size, Owner and AppraisedValue arguments'); @@ -154,4 +158,11 @@ function getUncompressedPointOnCurve(key: jsrsa.KJUR.crypto.ECDSA): Buffer { return uncompressedPoint; } +/** + * envOrDefault() will return the value of an environment variable, or a default value if the variable is undefined. + */ +function envOrDefault(key: string, defaultValue: string): string { + return process.env[key] || defaultValue; +} + main().catch(console.error);