Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple REST API #69

Merged
merged 30 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
233166a
Add Fablo-rest tool
dzikowski Feb 29, 2024
603d4e0
Add sample script with REST API calls
dzikowski Feb 29, 2024
bfe8614
Add Fablo REST client and rework RestApiClientBuilder
dzikowski Mar 1, 2024
75415a0
Add missing copyright
dzikowski Mar 1, 2024
5a4807a
Run template e2e tests in CI using Fablo REST
dzikowski Mar 1, 2024
8107ff0
Fix linter errors
dzikowski Mar 1, 2024
f0a5c0a
Run e2e tests in band
dzikowski Mar 1, 2024
5f82c6a
Add network:up command in template
dzikowski Mar 1, 2024
618a0e5
Add missing api config file
dzikowski Mar 1, 2024
36d68d4
Update Fablo REST host
dzikowski Mar 1, 2024
0163338
Add debug information
dzikowski Mar 1, 2024
34858cc
Disable Fablo REST for dev mode
dzikowski Mar 4, 2024
b30a74f
Unify userId/secret names
dzikowski Mar 4, 2024
c953afd
Add information about Fablo REST, unify userId/secret params
dzikowski Mar 4, 2024
ca8715e
Rework current GH Actions and add REST API tests
dzikowski Mar 4, 2024
2333b93
Merge branch 'main' into add-simple-rest-api
dzikowski Mar 4, 2024
21b4443
Remove non-required CI file (also removed in a different PR)
dzikowski Mar 4, 2024
7cde24d
Change file permissions
dzikowski Mar 4, 2024
601743f
Make dependencies between tasks more strict
dzikowski Mar 4, 2024
d1dedf2
Fix script
dzikowski Mar 4, 2024
70a38f6
Merge branch 'main' into add-simple-rest-api
dzikowski Mar 4, 2024
2fe1110
Don't use prefix for network:up
dzikowski Mar 4, 2024
978a57b
Unset npm_config_prefix
dzikowski Mar 4, 2024
cd71c88
Merge branch 'main' into add-simple-rest-api
dzikowski Mar 4, 2024
0ba78b7
Add missing setup node step
dzikowski Mar 4, 2024
2372413
Merge remote-tracking branch 'origin/add-simple-rest-api' into add-si…
dzikowski Mar 4, 2024
d4fdb86
Add missing prefix
dzikowski Mar 4, 2024
0f92f3d
Remove template-e2e-rest check
dzikowski Mar 5, 2024
40c6554
Merge branch 'main' into add-simple-rest-api
dzikowski Mar 5, 2024
0a8c622
Fix JSON.stringify of circular object
dzikowski Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/scripts/install-local-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

#
# Copyright (c) Gala Games Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -eu

cd "$GITHUB_WORKSPACE"

npm i
(cd chain-cli && ../npm-pack-and-replace.sh --skipConfirmation && npm i -g gala-chain-cli-*)

galachain --help
132 changes: 62 additions & 70 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,51 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install jq
run: |
sudo apt-get update
sudo apt-get install -y jq
- name: Check Copyright
- name: Verify copyright
run: ./verify_copyright.sh
- name: Install Node 18
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm i
- name: Lint
run: |
npm i
npm run lint
run: npm run lint
- name: Build
run: npm run build
- name: Test
run: npm run test

template-ci:
name: Chaincode template CI
runs-on: ubuntu-22.04
needs: [ ci ]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install local CLI
run: .github/scripts/install-local-cli.sh
- name: Create test project
run: galachain init ./project-test
- name: Install dependencies
run: |
npm i --prefix ./project-test
(cd ./project-test && ../npm-pack-and-replace.sh --skipConfirmation)
- name: Lint
run: npm run lint --prefix ./project-test
- name: Build
run: npm run build --prefix ./project-test
- name: Test
run: npm run test --prefix ./project-test

template-e2e:
needs: [ci]
name: Test Project Template
name: Chaincode template E2E (watch mode)
runs-on: ubuntu-22.04
needs: [ ci ]
env:
GALA_CLIENT_DEV_MODE: "true"
steps:
Expand All @@ -55,40 +77,38 @@ jobs:
docker version
docker-compose version
npm i -g nx

- name: Build template
run: |
npm i
cd chain-cli
../npm-pack-and-replace.sh --skipConfirmation
npm i -g gala-chain-cli-*
galachain --help
cd ..
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install local CLI
run: .github/scripts/install-local-cli.sh
- name: Create test project
run: galachain init ./project-test
- name: Install dependencies
run: |
galachain init ./project-test
cd ./project-test

../npm-pack-and-replace.sh --skipConfirmation
cat package.json
npm run network:start & # Run network in background and wait for it to start
npm run test

npm i --prefix ./project-test
(cd ./project-test && ../npm-pack-and-replace.sh --skipConfirmation)
- name: Run network in watch mode and wait for it to start
run: |
npm run network:start --prefix ./project-test &
sleep 120
npm run test:e2e

- name: Run E2E tests
run: npm run test:e2e --prefix ./project-test
- name: Verify chain browser blocks
run: |
MAX_BLOCK_INDEX=$(curl --location 'http://localhost:3010/product-channel/blocks' --header 'Content-Type: application/json' | jq '.info.fromBlock')
if [ $MAX_BLOCK_INDEX -lt 56]; then
echo "The number of blocks is less than 56"
exit 1
else
echo "There are at least 56 blocks after the tests ($$MAX_BLOCK_INDEX)"
fi
npm run network:prune
npm run network:prune --prefix ./project-test

template-image-check:
name: Template Image Check
needs: [ci]
name: Chaincode template image check
needs: [ ci ]
runs-on: ubuntu-22.04
steps:
- name: Checkout
Expand All @@ -97,17 +117,17 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install tools
- name: Install local CLI
run: .github/scripts/install-local-cli.sh
- name: Create test project
run: galachain init ./project-test
- name: Install dependencies
run: |
npm i --prefix ./project-test
(cd ./project-test && ../npm-pack-and-replace.sh --skipConfirmation)
- name: Verify image
run: |
npm i
cd chain-cli
npm run build
npm link ./
cd ..
galachain init ./project-test
cd ./project-test
../npm-pack-and-replace.sh --skipConfirmation
npm i
docker build . -t chaincode-test
run_output=$(docker run --rm chaincode-test 2>&1 || true)
echo "$run_output"
Expand All @@ -121,37 +141,9 @@ jobs:
exit 1
fi

chaincode-template-lint:
name: Chaincode Template Lint
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm install
- name: Build CLI and link it
run: |
cd chain-cli/chaincode-template
../../npm-pack-and-replace.sh --skipConfirmation
cd ..
npm install
npm run build
npm link --force
- name: Chaincode lint
run: |
galachain init test-project
cd test-project
npm install
npm run build
npm run lint

publish:
name: Publish Release
needs: [template-e2e, template-image-check]
needs: [ ci, template-ci, template-e2e, template-image-check ]
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
runs-on: ubuntu-22.04
steps:
Expand Down
6 changes: 3 additions & 3 deletions chain-cli/chaincode-template/e2e/gcclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("Chaincode client (PartnerOrg1)", () => {
const params = {
orgMsp: "PartnerOrg1",
userId: "admin",
userPass: "adminpw",
userSecret: "adminpw",
connectionProfilePath: path.resolve(networkRoot(), "connection-profiles/cpp-partner.json")
};

Expand Down Expand Up @@ -83,7 +83,7 @@ describe("Chaincode client (CuratorOrg)", () => {
const params: HFClientConfig = {
orgMsp: "CuratorOrg",
userId: "admin",
userPass: "adminpw",
userSecret: "adminpw",
connectionProfilePath: path.resolve(networkRoot(), "connection-profiles/cpp-curator.json")
};

Expand Down Expand Up @@ -132,7 +132,7 @@ describe.skip("REST API client", () => {
beforeAll(() => {
const params: RestApiClientConfig = {
orgMsp: "CuratorOrg",
userKey: "GC_ADMIN_CURATOR",
userId: "GC_ADMIN_CURATOR",
userSecret: "abc",
apiUrl: "http://localhost:3000/api",
configPath: path.resolve(__dirname, "api-config.json")
Expand Down
3 changes: 2 additions & 1 deletion chain-cli/chaincode-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"format": "prettier --config .prettierrc 'src/**/*.ts' 'e2e/**/*.ts' --write",
"prepublishOnly": "npm run format && npm run build && npm run lint && npm run test",
"network:start": "galachain network:up -C=product-channel -t=curator -n=basic-product -d=./ --envConfig=./.dev-env --watch",
"network:up": "galachain network:up -C=product-channel -t=curator -n=basic-product -d=./ --envConfig=./.dev-env",
"network:prune": "galachain network:prune",
"network:recreate": "npm run network:prune && rm -rf ./test-network && npm run network:start",
"test": "jest",
"test:e2e": "jest --config=e2e/jest.config.js",
"test:e2e": "jest --config=e2e/jest.config.js --runInBand",
"update-snapshot": "jest --updateSnapshot"
},
"dependencies": {
Expand Down
56 changes: 56 additions & 0 deletions chain-cli/chaincode-template/rest-api-samples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash

#
# Copyright (c) Gala Games Inc. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -eu

# Enroll a user (default admin credentials for local env)
enroll_response="$(
curl --request POST \
--url http://localhost:8801/user/enroll \
--data '{"id": "admin", "secret": "adminpw"}'
)"
echo "Enroll response: $enroll_response"

token="$(echo "$enroll_response" | jq -r '.token')"

# Call chaincode (GetChaincodeVersion - the simplest call)
version_response="$(
curl --request POST \
--url http://localhost:8801/invoke/product-channel/basic-product \
--header "Authorization: Bearer $token" \
--data '{"method": "PublicKeyContract:GetChaincodeVersion", "args": []}'
)"
echo "Version response: $version_response"


# Call chaincode (GetMyProfile - requires signing of DTO with user's private key
get_my_profile_dto="$(galachain dto-sign test-network/dev-admin-key/dev-admin.priv.hex.txt '{}')"
echo "get_my_profile_dto: $get_my_profile_dto"
get_my_profile_payload="$(
echo '{}' |
jq '.method="PublicKeyContract:GetMyProfile"' |
jq ".args=[\"${get_my_profile_dto//\"/\\\"}\"]"
)"

profile_response="$(
curl --request POST \
--url http://localhost:8801/invoke/product-channel/basic-product \
--header "Authorization: Bearer $token" \
--header 'Content-Type: application/json' \
--data "$get_my_profile_payload"
)"
echo "Profile response: $profile_response"
26 changes: 26 additions & 0 deletions chain-cli/network/api-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"channels": [
{
"pathFragment": "product",
"channelName": "product-channel",
"asLocalHost": true,
"contracts": [
{
"pathFragment": "apple",
"chaincodeName": "basic-product",
"contractName": "AppleContract"
},
{
"pathFragment": "public-key-contract",
"chaincodeName": "basic-product",
"contractName": "PublicKeyContract"
},
{
"pathFragment": "token-contract",
"chaincodeName": "basic-product",
"contractName": "GalaChainToken"
}
]
}
]
}
9 changes: 9 additions & 0 deletions chain-cli/network/fablo-config-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
},
"peer": {
"instances": 1
},
"tools": {
"fabloRest": true
}
},
{
Expand All @@ -40,13 +43,19 @@
},
"peer": {
"instances": 1
},
"tools": {
"fabloRest": true
}
},
{
"organization": {
"name": "UsersOrg1",
"mspName": "UsersOrg1",
"domain": "users1.local"
},
"tools": {
"fabloRest": true
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions chain-client/src/gcclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("forConnectionProfile", () => {
const org = {
orgMsp: "CuratorOrg",
userId: "admin",
userPass: "adminpw",
userSecret: "adminpw",
connectionProfilePath: "some/path"
};

Expand All @@ -51,7 +51,7 @@ describe("forConnectionProfile", () => {
const org = {
orgMsp: "MissingOrg",
userId: "admin",
userPass: "adminpw",
userSecret: "adminpw",
connectionProfilePath: "some/path"
};

Expand Down Expand Up @@ -79,7 +79,7 @@ describe("forApiConfig", () => {
const api = {
orgMsp: "CuratorOrg",
apiUrl: "http://localhost:3000",
userKey: "GC_ADMIN_KEY",
userId: "GC_ADMIN_ID",
userSecret: "GC_ADMIN_SECRET",
configPath: "some/path"
};
Expand Down
Loading
Loading