diff --git a/.github/workflows/test_asset-exchange-besu.yml b/.github/workflows/test_asset-exchange-besu.yml index 00bbcb6a2c..fe6f4afb31 100644 --- a/.github/workflows/test_asset-exchange-besu.yml +++ b/.github/workflows/test_asset-exchange-besu.yml @@ -40,13 +40,6 @@ jobs: run: | curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/protoc-3.15.6-linux-x86_64.zip unzip protoc-3.15.6-linux-x86_64.zip -d protoc - - # PROTOS - - name: Build JS Protos - run: | - export PATH="$PATH:${GITHUB_WORKSPACE}/protoc/bin" - make build - working-directory: common/protos-js # PROTOS - name: Build Solidity Protos @@ -54,13 +47,6 @@ jobs: export PATH="$PATH:${GITHUB_WORKSPACE}/protoc/bin" make build working-directory: common/protos-sol - - # BESU SDK - - name: Setup BESU SDK - run: | - npm link - make build-local - working-directory: sdks/besu/interoperation-node-sdk # BESU NETWORK - name: Start Besu Network @@ -83,16 +69,23 @@ jobs: working-directory: samples/besu/simpleasset # BESU CLI + - name: Setup BESU CLI .npmrc + run: | + cp .npmrc.template .npmrc + sed -i "s//${{ secrets.GITHUB_TOKEN }}/g" .npmrc + cat .npmrc + working-directory: samples/besu/besu-cli + - name: Setup BESU CLI init run: | - make build-local + make build cp config.template.json tmp.json cp config.template.json tmp.json jq -r '.network1.tokenContract |= "../simpleasset/build/contracts/${{ matrix.app_contract }}.json"' tmp.json > config.json cp config.json tmp.json jq -r '.network2.tokenContract |= "../simpleasset/build/contracts/BobERC20.json"' tmp.json > config.json rm tmp.json - working-directory: samples/besu/besu-cli + working-directory: samples/besu/besu-cli - name: Asset Exchange Besu CLI Tests run: | @@ -196,4 +189,185 @@ jobs: exit 1 fi working-directory: samples/besu/besu-cli + + asset-exchange-besu-local: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + app_contract: ["AliceERC1155", "AliceERC20", "AliceERC721"] + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v3 + + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'adopt' + + - name: Use Protoc 3.15 + run: | + curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/protoc-3.15.6-linux-x86_64.zip + unzip protoc-3.15.6-linux-x86_64.zip -d protoc + + # PROTOS + - name: Build JS Protos + run: | + export PATH="$PATH:${GITHUB_WORKSPACE}/protoc/bin" + make build + working-directory: common/protos-js + + # PROTOS + - name: Build Solidity Protos + run: | + export PATH="$PATH:${GITHUB_WORKSPACE}/protoc/bin" + make build + working-directory: common/protos-sol + + # BESU SDK + - name: Setup BESU SDK + run: | + npm link + make build-local + working-directory: sdks/besu/interoperation-node-sdk + + # BESU NETWORK + - name: Start Besu Network + run: | + sudo apt install -y tmux jq + wget https://hyperledger.jfrog.io/artifactory/besu-binaries/besu/22.7.0/besu-22.7.0.zip + unzip besu-22.7.0.zip + export PATH="${PWD}/besu-22.7.0/bin:$PATH" + wget https://artifacts.consensys.net/public/ethsigner/raw/names/ethsigner.zip/versions/22.1.3/ethsigner-22.1.3.zip + unzip ethsigner-22.1.3.zip + export PATH="${PWD}/ethsigner-22.1.3/bin:$PATH" + make start + sleep 100 + working-directory: tests/network-setups/besu + + # Deploy contracts + - name: Deploy contracts + run: | + make deploy-contracts + working-directory: samples/besu/simpleasset + + # BESU CLI + - name: Setup BESU CLI init + run: | + make build-local + cp config.template.json tmp.json + cp config.template.json tmp.json + jq -r '.network1.tokenContract |= "../simpleasset/build/contracts/${{ matrix.app_contract }}.json"' tmp.json > config.json + cp config.json tmp.json + jq -r '.network2.tokenContract |= "../simpleasset/build/contracts/BobERC20.json"' tmp.json > config.json + rm tmp.json + working-directory: samples/besu/besu-cli + + - name: Asset Exchange Besu CLI Tests + run: | + export PATH="${PWD}/bin:$PATH" + COUNT=0 + TOTAL=10 + + # HTLC Params + timeout=1000 + hashBase64="ivHErp1x4bJDKuRo6L5bApO/DdoyD/dG0mAZrzLZEIs=" + preimage="secrettext" + + # issue tokens to account 1 + initAmount=10 + transferAmount=5 + tokenId=0 + # & lock 5 token for account 2 in network 1 + if [ ${{ matrix.app_contract }} == "AliceERC20" ]; then + besu-cli asset issue --network=network1 --account=1 --amount=${initAmount} --asset_type=ERC20 + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --amount=${transferAmount} --timeout=${timeout} --hash_base64=${hashBase64} 1> tmp.out + elif [ ${{ matrix.app_contract }} == "AliceERC1155" ]; then + besu-cli asset issue --network=network1 --account=1 --amount=${initAmount} --token_id=${tokenId} --token_data="" --asset_type=ERC1155 + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --amount=${transferAmount} --token_id=${tokenId} --timeout=${timeout} --asset_type=ERC1155 --hash_base64=${hashBase64} 1> tmp.out + else + initAmount=1 + transferAmount=1 + besu-cli asset issue --network=network1 --account=1 --token_id=${tokenId} --asset_type=ERC721 1> tmp.out + tokenId=$(cat tmp.out | grep "New ERC721 token minted with id " | sed -e 's/New ERC721 token minted with id //') + cat tmp.out + echo $tokenId + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --token_id=${tokenId} --asset_type=ERC721 --timeout=${timeout} --hash_base64=${hashBase64} 1> tmp.out + fi + + balanceRem=$((initAmount-transferAmount)) + cat tmp.out | grep "Account balance of the sender in Network network1: ${initAmount} " && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "Account balance of the sender in Network network1: ${balanceRem}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "Account balance of the recipient in Network network1: 0" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out + + tmp_string=$(cat tmp.out | grep "Lock contract ID:") + arrIN=(${tmp_string//:/ }) + LOCK_ID=${arrIN[3]} + + # Is Asset locked + besu-cli asset is-locked --network=network1 --lock_contract_id=$LOCK_ID 1> tmp.out + cat tmp.out | grep "in Network network1: true" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out + + # Claim asset + besu-cli asset claim --network=network1 --recipient_account=2 --preimage=${preimage} --token_id=${tokenId} --lock_contract_id=$LOCK_ID 1> tmp.out + cat tmp.out | grep "Account balance of the recipient in Network network1 before claiming: 0" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "Account balance of the recipient in Network network1 after claiming: ${transferAmount}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out + + ###### test unlock ####### + timeout=20 + initAmount=5 + recipientAmt=0 + transferAmount=2 + tokenId=1 + if [ ${{ matrix.app_contract }} == "AliceERC20" ]; then + recipientAmt=5 + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --amount=${transferAmount} --timeout=${timeout} --hash_base64=${hashBase64} 1> tmp.out + elif [ ${{ matrix.app_contract }} == "AliceERC1155" ]; then + besu-cli asset issue --network=network1 --account=1 --amount=${initAmount} --token_id=${tokenId} --token_data="" --asset_type=ERC1155 + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --amount=${transferAmount} --token_id=${tokenId} --timeout=${timeout} --asset_type=ERC1155 --hash_base64=${hashBase64} 1> tmp.out + else + initAmount=1 + transferAmount=1 + recipientAmt=1 + besu-cli asset issue --network=network1 --account=1 --token_id=${tokenId} --asset_type=ERC721 1> tmp.out + tokenId=$(cat tmp.out | grep "New ERC721 token minted with id " | sed -e 's/New ERC721 token minted with id //') + cat tmp.out + echo $tokenId + besu-cli asset lock --network=network1 --sender_account=1 --recipient_account=2 --token_id=${tokenId} --asset_type=ERC721 --timeout=${timeout} --hash_base64=${hashBase64} 1> tmp.out + fi + + balanceRem=$((initAmount-transferAmount)) + cat tmp.out | grep "Account balance of the sender in Network network1: ${initAmount}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "Account balance of the sender in Network network1: ${balanceRem}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "Account balance of the recipient in Network network1: ${recipientAmt}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out + + tmp_string=$(cat tmp.out | grep "Lock contract ID:") + arrIN=(${tmp_string//:/ }) + LOCK_ID=${arrIN[3]} + + sleep $timeout + + besu-cli asset unlock --network=network1 --lock_contract_id=$LOCK_ID --sender_account=1 --token_id=${tokenId} 1> tmp.out + cat tmp.out | grep "before unlocking: ${balanceRem}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out | grep "after unlocking: ${initAmount}" && COUNT=$(( COUNT + 1 )) && echo "PASS" + cat tmp.out + + # RESULT + echo "Passed $COUNT/$TOTAL Tests." + + if [ $COUNT == $TOTAL ]; then + exit 0 + else + exit 1 + fi + working-directory: samples/besu/besu-cli diff --git a/samples/besu/besu-cli/.gitignore b/samples/besu/besu-cli/.gitignore index eabb15ca21..719a29f805 100644 --- a/samples/besu/besu-cli/.gitignore +++ b/samples/besu/besu-cli/.gitignore @@ -1,3 +1,4 @@ +.npmrc package-lock.json config.json node_modules diff --git a/samples/besu/besu-cli/.npmrc.template b/samples/besu/besu-cli/.npmrc.template new file mode 100644 index 0000000000..b3197a2f00 --- /dev/null +++ b/samples/besu/besu-cli/.npmrc.template @@ -0,0 +1,2 @@ +@hyperledger-labs:registry=https://npm.pkg.github.com/hyperledger-labs +//npm.pkg.github.com/:_authToken= diff --git a/samples/besu/besu-cli/package-local.json b/samples/besu/besu-cli/package-local.json index b8e0889516..f38ad08744 100644 --- a/samples/besu/besu-cli/package-local.json +++ b/samples/besu/besu-cli/package-local.json @@ -35,20 +35,19 @@ "@hyperledger-labs/weaver-protos-js": "file:../../../common/protos-js", "@truffle/contract": "^4.2.14", "gluegun": "latest", - "winston": "^3.3.3", - "logform": "~2.4.2" + "winston": "^3.3.3" }, "devDependencies": { "@types/jest": "^24.0.18", "@types/node": "^12.7.11", - "jest": "^24.1.0", + "jest": "^29.4.2", "prettier": "^1.12.1", - "ts-jest": "^24.1.0", - "ts-node": "^8.4.1", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.1", "tslint": "^5.12.0", "tslint-config-prettier": "^1.17.0", "tslint-config-standard": "^8.0.1", - "typescript": "^3.6.3" + "typescript": "^4.9.5" }, "jest": { "preset": "ts-jest", diff --git a/samples/besu/besu-cli/package.json b/samples/besu/besu-cli/package.json index 492449d34b..1642f19435 100644 --- a/samples/besu/besu-cli/package.json +++ b/samples/besu/besu-cli/package.json @@ -35,20 +35,19 @@ "@hyperledger-labs/weaver-protos-js": "~1.5.0", "@truffle/contract": "^4.2.14", "gluegun": "latest", - "winston": "^3.3.3", - "logform": "~2.4.2" + "winston": "^3.3.3" }, "devDependencies": { "@types/jest": "^24.0.18", "@types/node": "^12.7.11", - "jest": "^24.1.0", + "jest": "^29.4.2", "prettier": "^1.12.1", - "ts-jest": "^24.1.0", - "ts-node": "^8.4.1", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.1", "tslint": "^5.12.0", "tslint-config-prettier": "^1.17.0", "tslint-config-standard": "^8.0.1", - "typescript": "^3.6.3" + "typescript": "^4.9.5" }, "jest": { "preset": "ts-jest", diff --git a/samples/besu/besu-cli/src/commands/asset/claim.ts b/samples/besu/besu-cli/src/commands/asset/claim.ts index 3d9c37b1ed..44a443d1d1 100644 --- a/samples/besu/besu-cli/src/commands/asset/claim.ts +++ b/samples/besu/besu-cli/src/commands/asset/claim.ts @@ -1,8 +1,8 @@ import { GluegunCommand } from 'gluegun' import { getNetworkConfig, commandHelp } from '../../helper/helper' import { getContractInstance } from '../../helper/besu-functions' -import * as assetManager from '@hyperledger-labs/weaver-besu-interop-sdk/src/AssetManager' -import { SHA256 } from "@hyperledger-labs/weaver-besu-interop-sdk/src/HashFunctions"; +import { AssetManager } from '@hyperledger-labs/weaver-besu-interop-sdk' +import { HashFunctions } from "@hyperledger-labs/weaver-besu-interop-sdk"; const Web3 = require('web3') @@ -157,9 +157,9 @@ const command: GluegunCommand = { } before claiming: ${recipientBalance.toString()}` ) - const hash = new SHA256() + const hash = new HashFunctions.SHA256() hash.setPreimage(options.preimage) - await assetManager + await AssetManager .claimAssetInHTLC( interopContract, lockContractId, diff --git a/samples/besu/besu-cli/src/commands/asset/is-locked.ts b/samples/besu/besu-cli/src/commands/asset/is-locked.ts index 09260b66c5..28ffac3b99 100644 --- a/samples/besu/besu-cli/src/commands/asset/is-locked.ts +++ b/samples/besu/besu-cli/src/commands/asset/is-locked.ts @@ -1,7 +1,7 @@ import { GluegunCommand } from 'gluegun' import { getNetworkConfig, commandHelp } from '../../helper/helper' import { getContractInstance } from '../../helper/besu-functions' -import * as assetManager from '@hyperledger-labs/weaver-besu-interop-sdk/src/AssetManager' +import { AssetManager } from '@hyperledger-labs/weaver-besu-interop-sdk' const Web3 = require('web3') const command: GluegunCommand = { @@ -81,7 +81,7 @@ const command: GluegunCommand = { // const accounts = await web3N.eth.getAccounts() // var sender = accounts[networkConfig.senderAccountIndex] - var isLocked = await assetManager + var isLocked = await AssetManager .isAssetLockedInHTLC(interopContract, lockContractId) .catch(function() { console.log('isAssetLocked threw an error') diff --git a/samples/besu/besu-cli/src/commands/asset/lock.ts b/samples/besu/besu-cli/src/commands/asset/lock.ts index 60c1aa9b9d..83f431160d 100644 --- a/samples/besu/besu-cli/src/commands/asset/lock.ts +++ b/samples/besu/besu-cli/src/commands/asset/lock.ts @@ -1,8 +1,8 @@ import { GluegunCommand } from 'gluegun' import { getNetworkConfig, commandHelp } from '../../helper/helper' import { getContractInstance } from '../../helper/besu-functions' -import * as assetManager from '@hyperledger-labs/weaver-besu-interop-sdk/src/AssetManager' -import { SHA256 } from '@hyperledger-labs/weaver-besu-interop-sdk/src/HashFunctions' +import { AssetManager } from '@hyperledger-labs/weaver-besu-interop-sdk' +import { HashFunctions } from "@hyperledger-labs/weaver-besu-interop-sdk"; const Web3 = require('web3') const crypto = require('crypto') @@ -210,7 +210,7 @@ const command: GluegunCommand = { console.log('Hash (base64): ', hash_base64) console.log('Preimage: ', preimage) - const hash = new SHA256(); + const hash = new HashFunctions.SHA256(); hash.setSerializedHashBase64(hash_base64) // Balances of sender and receiver before locking @@ -242,7 +242,7 @@ const command: GluegunCommand = { await tokenContract .approve(tokenContract.address, options.token_id, { from: sender }) try { - lockTx = await assetManager.createHTLC( + lockTx = await AssetManager.createHTLC( interopContract, tokenContract, options.asset_type, @@ -261,7 +261,7 @@ const command: GluegunCommand = { await tokenContract .setApprovalForAll(tokenContract.address, true, { from: sender }) try { - lockTx = await assetManager.createHybridHTLC( + lockTx = await AssetManager.createHybridHTLC( interopContract, tokenContract, options.asset_type, @@ -282,7 +282,7 @@ const command: GluegunCommand = { await tokenContract .approve(tokenContract.address, amount, { from: sender }) try { - lockTx = await assetManager.createFungibleHTLC( + lockTx = await AssetManager.createFungibleHTLC( interopContract, tokenContract, options.asset_type, diff --git a/samples/besu/besu-cli/src/commands/asset/unlock.ts b/samples/besu/besu-cli/src/commands/asset/unlock.ts index 1d98d308e3..e59cb559e8 100644 --- a/samples/besu/besu-cli/src/commands/asset/unlock.ts +++ b/samples/besu/besu-cli/src/commands/asset/unlock.ts @@ -1,7 +1,8 @@ import { GluegunCommand } from 'gluegun' import { getNetworkConfig, commandHelp } from '../../helper/helper' import { getContractInstance } from '../../helper/besu-functions' -import * as assetManager from '@hyperledger-labs/weaver-besu-interop-sdk/src/AssetManager' +import { AssetManager } from '@hyperledger-labs/weaver-besu-interop-sdk' + // import { send } from 'process' const Web3 = require('web3') async function getBalances( @@ -152,7 +153,7 @@ const command: GluegunCommand = { } before unlocking: ${senderBalance.toString()}` ) - await assetManager + await AssetManager .reclaimAssetInHTLC(interopContract, lockContractId, sender) .catch(function(e) { console.log(e) diff --git a/sdks/besu/interoperation-node-sdk/package.json b/sdks/besu/interoperation-node-sdk/package.json index 7ddaa74cfc..1dbc1905ec 100644 --- a/sdks/besu/interoperation-node-sdk/package.json +++ b/sdks/besu/interoperation-node-sdk/package.json @@ -16,7 +16,7 @@ }, "license": "ISC", "dependencies": { - "@hyperledger-labs/weaver-protos-js": "^1.5.2", + "@hyperledger-labs/weaver-protos-js": "^1.5.7", "log4js": "^6.7.0", "web3": "^1.8.1" },