Skip to content

Commit

Permalink
feat(htlc-coordinator): new htlc coordinator
Browse files Browse the repository at this point in the history
        Primary Change
        ---
        1. Added new package extensions/cactus-plugin-htlc-coordinator
        2. Added keychainId to cactus-plugin-htlc-eth-besu-erc20 plugin

        Refactorings that were also necessary to incorporate 1) and 2)
        ---
        3. Updated learna.json
        4. Updated tsconfig.json

Resolves #953

Signed-off-by: jagpreetsinghsaan <jagpreet.singh.sasan@accenture.com>
  • Loading branch information
jagpreetsinghsasan authored and petermetz committed Oct 21, 2021
1 parent 4db540e commit 28c97d3
Show file tree
Hide file tree
Showing 32 changed files with 3,678 additions and 1 deletion.
104 changes: 104 additions & 0 deletions extensions/cactus-plugin-htlc-coordinator-besu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# `@hyperledger/cactus-plugin-htlc-coordinator-besu`

Allows Cactus nodes to interact beetwen diferent networks. Using this we can perform:
* Instantiate an existing HTLC plugin, also the own HTLC and the counterparty HTLC.
* Interact with the HTLC, deploying, checking and withdrawing the funds.
## Summary

- [Usage](#usage)
- [Development](#development)
- [Getting Started](#getting-started)
- [Flow](#flow)
- [Contributing](#contributing)
- [License](#license)
- [Acknowledgments](#acknowledgments)

## Usage

To use this import public-api and create new **PluginFactoryHTLCCoordinatorBesu*. Then use it to create a HTLC Coordinator.
```typescript
const factoryHTLC = new PluginFactoryHTLCCoordinatorBesu({
pluginImportType: PluginImportType.Local,
});
const pluginHTLCCoordinatorBesu = await factoryHTLC.create(pluginOptions);
```
You can make calls through the htlc coordinator to the plugin API:

```typescript
async ownHTLC(ownHTLCRequest: OwnHTLCRequest): Promise<InvokeContractV1Response>;
async counterpartyHTLC(counterpartyHTLCRequest: CounterpartyHTLCRequest): Promise<InvokeContractV1Response>;
async withdrawCounterparty(withdrawCounterpartyRequest: WithdrawCounterpartyRequest): Promise<InvokeContractV1Response>;
```

Call example to create an ownHTLC and instantiate a HTLC contract:
```typescript
const ownHTLCRequest: OwnHTLCRequest = {
htlcPackage: HtlcPackage.BesuErc20,
connectorInstanceId,
keychainId,
constructorArgs: [],
web3SigningCredential,
inputAmount: 10,
outputAmount: 1,
expiration,
hashLock,
tokenAddress,
receiver,
outputNetwork: "BTC",
outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8",
gas: estimatedGas,
};
const response = await coordinator.ownCoordinator(ownHTLCRequest);
});
```
The field "htlcPackage" can have the following values:
```typescript
enum HtlcPackage {
Besu = 'BESU',
BesuErc20 = 'BESU_ERC20'
}
```

## Development

### Getting Started

Clone the git repository on your local machine. Follow these instructions that will get you a copy of the project up and running on
your local machine for development and testing purposes.

#### Prerequisites

In the root of the project to install the dependencies execute the command:
```sh
yarn run configure
```

#### Compiling

In the project root folder, run this command to compile the plugin and create the dist directory:
```sh
yarn run watch
```

### Flow

#### Alice flow

The [Alice diagram](docs/flow/htlc-coordinator-alice-flow.md, "Alice Flow") shows the sequence diagram of a complete flow for a participant who wants exchange funds with another participant. She doesn't start the withdraw flow becuase she doesn't know the secret to withdraw them.

#### Bob flow

The next [Bob diagram](docs/flow/htlc-coordinator-bob-flow.md, "Bob Flow")
shows the sequence diagram of a complete flow for a participant who wants exchange funds with another participant, but he knows the secret and starts the flow to withdraw the funds.


## Contributing

We welcome contributions to Hyperledger Cactus in many forms, and there’s always plenty to do!

Please review [CONTIRBUTING.md](../../CONTRIBUTING.md) to get started.

## License

This distribution is published under the Apache License Version 2.0 found in the [LICENSE](../../LICENSE) file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
```mermaid
sequenceDiagram
autonumber
participant Alice
participant HTLC Coordinator Alice
participant HTLC Plugin Besu
participant HTLC Plugin Counterparty
participant HTLC Contract Alice
participant HTLC Contract Counterparty
Note over Alice: Bob has deployed his contract and share the information with Alice
Alice ->> HTLC Coordinator Alice: newCounterparty(HTLC Plugin Counterparty)
activate HTLC Coordinator Alice
HTLC Coordinator Alice ->> HTLC Plugin Counterparty: newInstance()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty-->> HTLC Coordinator Alice: plugin instance
deactivate HTLC Plugin Counterparty
HTLC Coordinator Alice ->> HTLC Plugin Counterparty: getSingleStatus()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty -->> HTLC Contract Counterparty: getSingleStatus()
activate HTLC Contract Counterparty
HTLC Contract Counterparty -->> HTLC Plugin Counterparty: ok
deactivate HTLC Contract Counterparty
HTLC Plugin Counterparty -->> HTLC Coordinator Alice: ok
deactivate HTLC Plugin Counterparty
HTLC Coordinator Alice ->> Alice: HTLC Plugin Counterparty instance
deactivate HTLC Coordinator Alice
Note over Alice: Alice has validated that the Bob contract information is correct
Alice ->> HTLC Coordinator Alice: newCoordinator(HTLC Plugin Besu)
activate HTLC Coordinator Alice
HTLC Coordinator Alice ->> HTLC Plugin Besu: newInstance()
activate HTLC Plugin Besu
HTLC Plugin Besu -->> HTLC Coordinator Alice: plugin instance
deactivate HTLC Plugin Besu
HTLC Coordinator Alice ->> HTLC Plugin Besu: newContract()
activate HTLC Plugin Besu
HTLC Plugin Besu ->> HTLC Contract Alice: deployContract()
activate HTLC Contract Alice
HTLC Contract Alice -->> HTLC Plugin Besu: ok
deactivate HTLC Contract Alice
HTLC Plugin Besu -->> HTLC Coordinator Alice: ok
HTLC Plugin Besu -->> HTLC Plugin Besu: Listening contract events
HTLC Coordinator Alice ->> Alice: Alice HTLC Coordinator instance
deactivate HTLC Coordinator Alice
Note over Alice: Alice send all information about her contract to Bob
Note over Alice: Her counterparty invoke the withdraw function
HTLC Plugin Besu -->> HTLC Coordinator Alice: Event - CounterParty withdraw
deactivate HTLC Plugin Besu
activate HTLC Coordinator Alice
HTLC Coordinator Alice -->> Alice: Counterparty withdraw notification
deactivate HTLC Coordinator Alice
Note over Alice: Alice can call the withdrawCounterparty because now she can see the secret
Alice ->> HTLC Coordinator Alice: WithdrawCounterparty()
activate HTLC Coordinator Alice
HTLC Coordinator Alice ->> HTLC Plugin Counterparty: WithdrawCounterparty()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty ->> HTLC Contract Counterparty: withdraw()
activate HTLC Contract Counterparty
HTLC Contract Counterparty -->> HTLC Plugin Counterparty: ok
deactivate HTLC Contract Counterparty
HTLC Plugin Counterparty -->> HTLC Coordinator Alice: ok
deactivate HTLC Plugin Counterparty
HTLC Coordinator Alice -->> Alice: ok
deactivate HTLC Coordinator Alice
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
```mermaid
sequenceDiagram
autonumber
participant Bob
participant HTLC Coordinator Bob
participant HTLC Plugin Besu ERC20
participant HTLC Plugin Counterparty
participant HTLC Contract Bob
participant HTLC Contract Counterparty
Note over Bob: We assumped that the offer take place on the front end
Note over Bob: Bob is the first to deploy the HTLC contract
Bob ->> HTLC Coordinator Bob: newCoordinator(HTLC Plugin Besu)
activate HTLC Coordinator Bob
HTLC Coordinator Bob ->> HTLC Plugin Besu ERC20:newInstance()
activate HTLC Plugin Besu ERC20
HTLC Plugin Besu ERC20-->> HTLC Coordinator Bob: plugin instance
deactivate HTLC Plugin Besu ERC20
HTLC Coordinator Bob ->> HTLC Plugin Besu ERC20:newContract()
activate HTLC Plugin Besu ERC20
HTLC Plugin Besu ERC20->> HTLC Contract Bob: deployContract()
activate HTLC Contract Bob
HTLC Contract Bob -->> HTLC Plugin Besu ERC20:ok
deactivate HTLC Contract Bob
HTLC Plugin Besu ERC20-->> HTLC Coordinator Bob: ok
HTLC Plugin Besu ERC20-->> HTLC Plugin Besu ERC20:Listening contract events
HTLC Coordinator Bob ->> Bob: Bob HTLC Coordinator instance
deactivate HTLC Coordinator Bob
Note over Bob: Bob share with Alice all the information about his contract
Bob ->> HTLC Coordinator Bob: newCounterparty(HTLC Plugin Counterparty)
activate HTLC Coordinator Bob
HTLC Coordinator Bob ->> HTLC Plugin Counterparty: newInstance()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty-->> HTLC Coordinator Bob: plugin instance
deactivate HTLC Plugin Counterparty
HTLC Coordinator Bob ->> HTLC Plugin Counterparty: getSingleStatus()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty -->> HTLC Contract Counterparty: getSingleStatus()
activate HTLC Contract Counterparty
HTLC Contract Counterparty -->> HTLC Plugin Counterparty: ok
deactivate HTLC Contract Counterparty
HTLC Plugin Counterparty -->> HTLC Coordinator Bob: ok
deactivate HTLC Plugin Counterparty
HTLC Coordinator Bob ->> Bob: HTLC Plugin Counterparty instance
deactivate HTLC Coordinator Bob
Note over Bob: Bob knows his secret, so he can start the withdrawCounterparty
Bob ->> HTLC Coordinator Bob: WithdrawCounterparty()
activate HTLC Coordinator Bob
HTLC Coordinator Bob ->> HTLC Plugin Counterparty: WithdrawCounterparty()
activate HTLC Plugin Counterparty
HTLC Plugin Counterparty ->> HTLC Contract Counterparty: withdraw()
activate HTLC Contract Counterparty
HTLC Contract Counterparty -->> HTLC Plugin Counterparty: ok
deactivate HTLC Contract Counterparty
HTLC Plugin Counterparty -->> HTLC Coordinator Bob: ok
deactivate HTLC Plugin Counterparty
HTLC Coordinator Bob -->> Bob: ok
deactivate HTLC Coordinator Bob
HTLC Plugin Besu ERC20-->> HTLC Coordinator Bob: Event - CounterParty withdraw
deactivate HTLC Plugin Besu ERC20
activate HTLC Coordinator Bob
HTLC Coordinator Bob -->> Bob: Counterparty withdraw notification
deactivate HTLC Coordinator Bob
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "5.2.0"
}
}
96 changes: 96 additions & 0 deletions extensions/cactus-plugin-htlc-coordinator-besu/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"name": "@hyperledger/cactus-plugin-htlc-coordinator-besu",
"version": "1.0.0-rc.1",
"description": "HTLC Coordinator to exchange tokens between networks.",
"main": "dist/lib/main/typescript/index.js",
"mainMinified": "dist/cactus-plugin-htlc-coordinator-besu.node.umd.min.js",
"browser": "dist/cactus-plugin-htlc-coordinator-besu.web.umd.js",
"browserMinified": "dist/cactus-plugin-htlc-coordinator-besu.web.umd.min.js",
"module": "dist/lib/main/typescript/index.js",
"types": "dist/types/main/typescript/index.d.ts",
"files": [
"dist/*"
],
"scripts": {
"generate-sdk": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g typescript-axios -o ./src/main/typescript/generated/openapi/typescript-axios/",
"codegen:openapi": "run-p generate-sdk",
"codegen": "run-p 'codegen:*'",
"watch": "npm-watch",
"webpack": "npm-run-all webpack:dev webpack:prod",
"webpack:dev": "npm-run-all webpack:dev:node webpack:dev:web",
"webpack:dev:web": "webpack --env=dev --target=web --config ../../webpack.config.js",
"webpack:dev:node": "webpack --env=dev --target=node --config ../../webpack.config.js",
"webpack:prod": "npm-run-all webpack:prod:node webpack:prod:web",
"webpack:prod:web": "webpack --env=prod --target=web --config ../../webpack.config.js",
"webpack:prod:node": "webpack --env=prod --target=node --config ../../webpack.config.js"
},
"watch": {
"codegen:openapi": {
"patterns": [
"./src/main/json/openapi.json"
]
}
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=10",
"npm": ">=6"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hyperledger/cactus.git"
},
"keywords": [
"Hyperledger",
"Cactus",
"Integration",
"Blockchain",
"Distributed Ledger Technology"
],
"author": {
"name": "Hyperledger Cactus Contributors",
"email": "cactus@lists.hyperledger.org",
"url": "https://www.hyperledger.org/use/cactus"
},
"contributors": [
{
"name": "Please add yourself to the list of contributors",
"email": "your.name@example.com",
"url": "https://example.com"
},
{
"name": "Peter Somogyvari",
"email": "peter.somogyvari@accenture.com",
"url": "https://accenture.com"
}
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/hyperledger/cactus/issues"
},
"homepage": "https://github.com/hyperledger/cactus#readme",
"dependencies": {
"@hyperledger/cactus-common": "1.0.0-rc.1",
"@hyperledger/cactus-core": "1.0.0-rc.1",
"@hyperledger/cactus-core-api": "1.0.0-rc.1",
"@hyperledger/cactus-plugin-htlc-eth-besu": "1.0.0-rc.1",
"@hyperledger/cactus-plugin-htlc-eth-besu-erc20": "1.0.0-rc.1",
"@hyperledger/cactus-plugin-ledger-connector-besu": "1.0.0-rc.1",
"@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20": "1.0.0-rc.1",
"axios": "0.21.1",
"body-parser": "1.19.0",
"joi": "14.3.1",
"openapi-types": "7.0.1",
"prom-client": "13.1.0",
"socket.io-client": "4.1.3",
"typescript-optional": "2.0.1"
},
"devDependencies": {
"@hyperledger/cactus-plugin-keychain-memory": "1.0.0-rc.1",
"@hyperledger/cactus-test-tooling": "1.0.0-rc.1",
"@types/express": "4.17.8",
"socket.io": "4.1.3"
}
}
Loading

0 comments on commit 28c97d3

Please sign in to comment.