From 640c89aa877172d1f9502a20f798392b4eae65ac Mon Sep 17 00:00:00 2001 From: duriancrepe <94508990+duriancrepe@users.noreply.github.com> Date: Sat, 8 Jan 2022 05:21:32 -0800 Subject: [PATCH] Add API functions to register arbitrator and mediator keys (#58) * Add API functions to register arbitrator and mediator keys --- README.md | 10 ++++++---- src/HavenoDaemon.test.ts | 34 ++++++++++++++++++++++++++++++---- src/HavenoDaemon.ts | 25 +++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ea0bc1f2..7f334321 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,10 @@ This application is a lightly modified [create-react-app](https://github.com/fac 1. [Run a local Haveno test network](https://github.com/haveno-dex/haveno/blob/master/docs/installing.md), running Alice as a daemon with `make alice-daemon`. 2. Clone this project to the same parent directory as the haveno project: `git clone https://github.com/haveno-dex/haveno-ui-poc` 3. In a new terminal, start envoy with the config in haveno-ui-poc/config/envoy.yaml (change absolute path for your system): `docker run --rm --add-host host.docker.internal:host-gateway -it -v ~/git/haveno-ui-poc/config/envoy.yaml:/envoy.yaml -p 8080:8080 envoyproxy/envoy-dev:8a2143613d43d17d1eb35a24b4a4a4c432215606 -c /envoy.yaml` -4. Install protobuf compiler 3.19.1 or later for your system:
+4. Install protobuf compiler v3.19.1 or later for your system:
mac: `brew install protobuf`
linux: `apt install protobuf-compiler` + NOTE: You may need to upgrade to v3.19.1 manually if your package manager installs an older version. 5. Download `protoc-gen-grpc-web` plugin and make executable as [shown here](https://github.com/grpc/grpc-web#code-generator-plugin). 6. `cd haveno-ui-poc` 7. `npm install` @@ -28,14 +29,15 @@ Running the [top-level API tests](./src/HavenoDaemon.test.ts) is a great way to [`HavenoDaemon`](./src/HavenoDaemon.ts) provides the interface to the Haveno daemon's gRPC API. -1. [Run a local Haveno test network](https://github.com/haveno-dex/haveno/blob/master/docs/installing.md) and then shut down the arbitrator, Alice, and Bob or run them as daemons, e.g. `make alice-daemon`. +1. [Run a local Haveno test network](https://github.com/haveno-dex/haveno/blob/master/docs/installing.md) and then shut down the arbitrator, Alice, and Bob or run them as daemons, e.g. `make alice-daemon`. You may omit the arbitrator registration steps since it is done automatically in the tests. 2. Clone this project to the same parent directory as the haveno project: `git clone https://github.com/haveno-dex/haveno-ui-poc` 3. In a new terminal, start envoy with the config in haveno-ui-poc/config/envoy.test.yaml (change absolute path for your system): `docker run --rm --add-host host.docker.internal:host-gateway -it -v ~/git/haveno-ui-poc/config/envoy.test.yaml:/envoy.test.yaml -p 8079:8079 -p 8080:8080 -p 8081:8081 -p 8082:8082 -p 8083:8083 -p 8084:8084 -p 8085:8085 -p 8086:8086 envoyproxy/envoy-dev:8a2143613d43d17d1eb35a24b4a4a4c432215606 -c /envoy.test.yaml` 4. In a new terminal, start the funding wallet. This wallet will be automatically funded in order to fund Alice and Bob during the tests.
For example: `cd ~/git/haveno && make funding-wallet`. -5. Install protobuf compiler 3.19.1 or later for your system:
+5. Install protobuf compiler v3.19.1 or later for your system:
mac: `brew install protobuf`
linux: `apt install protobuf-compiler` + NOTE: You may need to upgrade to v3.19.1 manually if your package manager installs an older version. 6. Download `protoc-gen-grpc-web` plugin and make executable as [shown here](https://github.com/grpc/grpc-web#code-generator-plugin). 7. `cd haveno-ui-poc` 8. `npm install` -9. `npm test` to run all tests or `npm run test -- -t 'my test'` to run tests by name. +9. `npm test` to run all tests or `npm run test -- -t 'my test'` to run tests by name. \ No newline at end of file diff --git a/src/HavenoDaemon.test.ts b/src/HavenoDaemon.test.ts index 9e600d68..9c63ce1a 100644 --- a/src/HavenoDaemon.test.ts +++ b/src/HavenoDaemon.test.ts @@ -88,7 +88,8 @@ const TestConfig = { ["8084", ["10003", "7779"]], ["8085", ["10004", "7780"]], ["8086", ["10005", "7781"]], - ]) + ]), + devPrivilegePrivKey: "6ac43ea1df2a290c1c8391736aa42e4339c5cb4f110ff0257a13b63211977b7a" // from DEV_PRIVILEGE_PRIV_KEY }; interface TxContext { @@ -127,6 +128,10 @@ beforeAll(async () => { if (daemons[2].status === "fulfilled") bob = (daemons[2] as PromiseFulfilledResult).value; else throw new Error((daemons[2] as PromiseRejectedResult).reason); + // register arbitrator as dispute agents + await arbitrator.registerDisputeAgent("mediator", TestConfig.devPrivilegePrivKey); + await arbitrator.registerDisputeAgent("refundagent", TestConfig.devPrivilegePrivKey); + // connect monero clients monerod = await monerojs.connectToDaemonRpc(TestConfig.monerod.url, TestConfig.monerod.username, TestConfig.monerod.password); aliceWallet = await monerojs.connectToWalletRpc(TestConfig.alice.walletUrl, TestConfig.alice.walletUsername, TestConfig.alice.walletPassword); @@ -148,8 +153,8 @@ beforeEach(async() => { afterAll(async () => { let stopPromises = []; if (arbitrator && arbitrator.getProcess()) stopPromises.push(stopHavenoProcess(arbitrator)); - if (alice && alice.getProcess())stopPromises.push(stopHavenoProcess(alice)); - if (bob && bob.getProcess())stopPromises.push(stopHavenoProcess(bob)); + if (alice && alice.getProcess()) stopPromises.push(stopHavenoProcess(alice)); + if (bob && bob.getProcess()) stopPromises.push(stopHavenoProcess(bob)); return Promise.all(stopPromises); }); @@ -162,6 +167,27 @@ test("Can get the version", async () => { expect(version).toEqual(TestConfig.haveno.version); }); +test("Can register as dispute agents", async () => { + await arbitrator.registerDisputeAgent("mediator", TestConfig.devPrivilegePrivKey); // TODO: bisq mediator = haveno arbitrator + await arbitrator.registerDisputeAgent("refundagent", TestConfig.devPrivilegePrivKey); // TODO: no refund agent in haveno + + // test bad dispute agent type + try { + await arbitrator.registerDisputeAgent("unsupported type", TestConfig.devPrivilegePrivKey); + throw new Error("should have thrown error registering bad type"); + } catch (err) { + if (err.message !== "unknown dispute agent type 'unsupported type'") throw new Error("Unexpected error: " + err.message); + } + + // test bad key + try { + await arbitrator.registerDisputeAgent("mediator", "bad key"); + throw new Error("should have thrown error registering bad key"); + } catch (err) { + if (err.message !== "invalid registration key") throw new Error("Unexpected error: " + err.message); + } +}); + test("Can get market prices", async () => { // get all market prices @@ -425,7 +451,7 @@ test("Handles unexpected errors during trade initialization", async () => { offer = await traders[0].getMyOffer(offer.getId()); assert.equal(offer.getState(), "AVAILABLE"); - // wait for offer for offer to be seen + // wait for offer to be seen await wait(TestConfig.walletSyncPeriodMs * 2); // trader 1 spends trade funds after initializing trade diff --git a/src/HavenoDaemon.ts b/src/HavenoDaemon.ts index 143115f6..bbd9196b 100644 --- a/src/HavenoDaemon.ts +++ b/src/HavenoDaemon.ts @@ -1,7 +1,7 @@ import {HavenoUtils} from "./HavenoUtils"; import * as grpcWeb from 'grpc-web'; -import {GetVersionClient, PriceClient, WalletsClient, OffersClient, PaymentAccountsClient, TradesClient} from './protobuf/GrpcServiceClientPb'; -import {GetVersionRequest, GetVersionReply, MarketPriceRequest, MarketPriceReply, MarketPricesRequest, MarketPricesReply, MarketPriceInfo, GetBalancesRequest, GetBalancesReply, XmrBalanceInfo, GetOffersRequest, GetOffersReply, OfferInfo, GetPaymentAccountsRequest, GetPaymentAccountsReply, CreateCryptoCurrencyPaymentAccountRequest, CreateCryptoCurrencyPaymentAccountReply, CreateOfferRequest, CreateOfferReply, CancelOfferRequest, TakeOfferRequest, TakeOfferReply, TradeInfo, GetTradeRequest, GetTradeReply, GetTradesRequest, GetTradesReply, GetNewDepositSubaddressRequest, GetNewDepositSubaddressReply, ConfirmPaymentStartedRequest, ConfirmPaymentReceivedRequest, XmrTx, GetXmrTxsRequest, GetXmrTxsReply, XmrDestination, CreateXmrTxRequest, CreateXmrTxReply, RelayXmrTxRequest, RelayXmrTxReply} from './protobuf/grpc_pb'; +import {GetVersionClient, DisputeAgentsClient, PriceClient, WalletsClient, OffersClient, PaymentAccountsClient, TradesClient} from './protobuf/GrpcServiceClientPb'; +import {GetVersionRequest, GetVersionReply, RegisterDisputeAgentRequest, MarketPriceRequest, MarketPriceReply, MarketPricesRequest, MarketPricesReply, MarketPriceInfo, GetBalancesRequest, GetBalancesReply, XmrBalanceInfo, GetOffersRequest, GetOffersReply, OfferInfo, GetPaymentAccountsRequest, GetPaymentAccountsReply, CreateCryptoCurrencyPaymentAccountRequest, CreateCryptoCurrencyPaymentAccountReply, CreateOfferRequest, CreateOfferReply, CancelOfferRequest, TakeOfferRequest, TakeOfferReply, TradeInfo, GetTradeRequest, GetTradeReply, GetTradesRequest, GetTradesReply, GetNewDepositSubaddressRequest, GetNewDepositSubaddressReply, ConfirmPaymentStartedRequest, ConfirmPaymentReceivedRequest, XmrTx, GetXmrTxsRequest, GetXmrTxsReply, XmrDestination, CreateXmrTxRequest, CreateXmrTxReply, RelayXmrTxRequest, RelayXmrTxReply} from './protobuf/grpc_pb'; import {PaymentAccount, AvailabilityResult} from './protobuf/pb_pb'; const console = require('console'); @@ -17,6 +17,7 @@ class HavenoDaemon { _processLogging: boolean = false; _walletRpcPort: number|undefined; _getVersionClient: GetVersionClient; + _disputeAgentsClient: DisputeAgentsClient; _priceClient: PriceClient; _walletsClient: WalletsClient; _paymentAccountsClient: PaymentAccountsClient; @@ -36,6 +37,7 @@ class HavenoDaemon { this._url = url; this._password = password; this._getVersionClient = new GetVersionClient(this._url); + this._disputeAgentsClient = new DisputeAgentsClient(this._url); this._priceClient = new PriceClient(this._url); this._walletsClient = new WalletsClient(this._url); this._paymentAccountsClient = new PaymentAccountsClient(this._url); @@ -199,6 +201,25 @@ class HavenoDaemon { }); } + /** + * Register as a dispute agent. + * + * @param {string} disputeAgentType - type of dispute agent to register, e.g. mediator, refundagent + * @param {string} registrationKey - registration key + */ + async registerDisputeAgent(disputeAgentType: string, registrationKey: string): Promise { + let that = this; + let request = new RegisterDisputeAgentRequest() + .setDisputeAgentType(disputeAgentType) + .setRegistrationKey(registrationKey); + return new Promise(function(resolve, reject) { + that._disputeAgentsClient.registerDisputeAgent(request, {password: that._password}, function(err: grpcWeb.RpcError) { + if (err) reject(err); + else resolve(); + }); + }); + } + /** * Get the current market price per 1 XMR in the given currency. *