From f7edbb47c0fdf208f7da6f5698d1a2ffdaa3aa5f Mon Sep 17 00:00:00 2001 From: Dr D Cagara Date: Sat, 5 Jan 2019 14:34:24 +1000 Subject: [PATCH] Tilde was not shell escaped in config parser --- Cargo.lock | 11 + LICENSE.md | 149 +++--- README.md | 21 +- api_tests/dry/rfc003/test.js | 26 +- api_tests/e2e/rfc003/btc_eth/test.js | 3 +- api_tests/e2e/rfc003/eth_btc/config.toml | 14 + api_tests/e2e/rfc003/eth_btc/test.js | 429 ++++++++++++++++++ application/comit_node/Cargo.toml | 1 + .../comit_node/src/bam_api/rfc003/swap.rs | 23 + application/comit_node/src/bin/comit_node.rs | 25 +- .../comit_node/src/http_api/rfc003/action.rs | 26 +- .../comit_node/src/http_api/rfc003/swap.rs | 35 ++ .../src/http_api/rfc003/with_swap_types.rs | 36 ++ .../src/swap_protocols/rfc003/actions/mod.rs | 8 - .../rfc003/alice/actions/eth_btc.rs | 105 +++++ .../rfc003/alice/actions/mod.rs | 1 + .../rfc003/alice/swap_request.rs | 12 + .../rfc003/bob/actions/btc_erc20.rs | 4 +- .../rfc003/bob/actions/btc_eth.rs | 21 +- .../rfc003/bob/actions/eth_btc.rs | 103 +++++ .../swap_protocols/rfc003/bob/actions/mod.rs | 1 + .../swap_protocols/rfc003/bob/swap_request.rs | 12 + .../rfc003/ethereum/ether_htlc.rs | 2 +- .../swap_protocols/rfc003/lightning/mod.rs | 0 .../src/swap_protocols/rfc003/role.rs | 26 -- .../src/swap_protocols/rfc003/secret.rs | 12 +- 26 files changed, 939 insertions(+), 167 deletions(-) create mode 100644 api_tests/e2e/rfc003/eth_btc/config.toml create mode 100644 api_tests/e2e/rfc003/eth_btc/test.js delete mode 100644 application/comit_node/src/swap_protocols/rfc003/actions/mod.rs create mode 100644 application/comit_node/src/swap_protocols/rfc003/alice/actions/eth_btc.rs create mode 100644 application/comit_node/src/swap_protocols/rfc003/bob/actions/eth_btc.rs delete mode 100644 application/comit_node/src/swap_protocols/rfc003/lightning/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 22c1b07d64..22826b7f6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -349,6 +349,7 @@ dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "debug_stub_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "enum-display-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum_support 0.1.0", @@ -549,6 +550,15 @@ dependencies = [ "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "directories" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dotenv" version = "0.8.0" @@ -2840,6 +2850,7 @@ dependencies = [ "checksum derive_state_machine_future 0.1.8 (git+https://github.com/coblox/state_machine_future.git)" = "" "checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" +"checksum directories 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "72d337a64190607d4fcca2cb78982c5dd57f4916e19696b48a575fa746b6cb0f" "checksum dotenv 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eea1395d2df3b5344dc577809296d9578303296e8d105c408aa80ed67d598ef1" "checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd" "checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" diff --git a/LICENSE.md b/LICENSE.md index 0ad25db4bd..f288702d2f 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,5 +1,5 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies @@ -7,15 +7,17 @@ Preamble - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. + The GNU General Public License is a free, copyleft license for +software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to +the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free -software for all its users. +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you @@ -24,34 +26,44 @@ them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. @@ -60,7 +72,7 @@ modification follow. 0. Definitions. - "This License" refers to version 3 of the GNU Affero General Public License. + "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. @@ -537,45 +549,35 @@ to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. + 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single +under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General +Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published +GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's +versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. @@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. + GNU General Public License for more details. - You should have received a copy of the GNU Affero General Public License + You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see +For more information on this, and how to apply and follow the GNU GPL, see . + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index 49eef9054c..93927677aa 100644 --- a/README.md +++ b/README.md @@ -21,14 +21,17 @@ Contains crates specific to our application. Can depend on libraries located in ## Setup build environment -- Install `rustup`: `curl https://sh.rustup.rs -sSf | sh` -- Install `cargo-make`: `cargo install cargo-make` -- Install libzmq: - - Ubuntu/Debian: `apt install libzmq3-dev` - - Mac ([Homebrew](https://brew.sh/)) `brew install zeromq` -- Run `cargo make` in the root folder of the repository, this will install various crates & tools such as rustfmt & clippy -- Install `docker` & `docker-compose` -- Install `nvm` +1. Install `rustup`: `curl https://sh.rustup.rs -sSf | sh` +2. Install SSL libraries + - Ubuntu/Debian: `apt install libssl-dev` + - Mac ([Homebrew](https://brew.sh/)) `brew install openssl` +3. Install libzmq: + - Ubuntu/Debian: `apt install libzmq3-dev` + - Mac ([Homebrew](https://brew.sh/)) `brew install zeromq` +4. Install `docker` & `docker-compose` +5. Install `nvm` +6. Install `cargo-make`: `cargo install cargo-make` +7. Run `cargo make` in the root folder of the repository, this will install various crates & tools such as rustfmt & clippy ## Testing @@ -46,4 +49,4 @@ Contributions are welcome, please visit [CONTRIBUTING](CONTRIBUTING.md) for more ## License -This project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE v3](LICENSE.md). +This project is licensed under the terms of the [GNU GENERAL PUBLIC LICENSE v3](LICENSE.md). diff --git a/api_tests/dry/rfc003/test.js b/api_tests/dry/rfc003/test.js index cfc91e9765..0526931208 100644 --- a/api_tests/dry/rfc003/test.js +++ b/api_tests/dry/rfc003/test.js @@ -305,20 +305,12 @@ describe("RFC003 HTTP API", () => { decline_res.should.have.status(200); }); - it("[Bob] Should be in the Rejected State after declining a swap request providing a reason", async () => { - let res = await chai - .request(bob.comit_node_url()) - .get(bob_stingy_swap_href); - res.should.have.status(200); - res.body.state.should.equal("Rejected"); + it("[Bob] Should be in the Rejected State after declining a swap request providing a reason", async function() { + await bob.poll_comit_node_until(chai, bob_stingy_swap_href, "Rejected"); }); it("[Alice] Should be in the Rejected State after Bob declines a swap request providing a reason", async () => { - let res = await chai - .request(alice.comit_node_url()) - .get(alice_stingy_swap_href); - res.should.have.status(200); - res.body.state.should.equal("Rejected"); + await alice.poll_comit_node_until(chai, alice_stingy_swap_href, "Rejected"); }); it("[Bob] Can execute a decline action, without providing a reason", async () => { @@ -344,18 +336,10 @@ describe("RFC003 HTTP API", () => { }); it("[Bob] Should be in the Rejected State after declining a swap request without a reason", async () => { - let res = await chai - .request(bob.comit_node_url()) - .get(bob_reasonable_swap_href); - res.should.have.status(200); - res.body.state.should.equal("Rejected"); + await bob.poll_comit_node_until(chai, bob_reasonable_swap_href, "Rejected"); }); it("[Alice] Should be in the Rejected State after Bob declines a swap request without a reason", async () => { - let res = await chai - .request(alice.comit_node_url()) - .get(alice_reasonable_swap_href); - res.should.have.status(200); - res.body.state.should.equal("Rejected"); + await alice.poll_comit_node_until(chai, alice_reasonable_swap_href, "Rejected"); }); }); diff --git a/api_tests/e2e/rfc003/btc_eth/test.js b/api_tests/e2e/rfc003/btc_eth/test.js index e7950cee4e..2f93fe1e60 100644 --- a/api_tests/e2e/rfc003/btc_eth/test.js +++ b/api_tests/e2e/rfc003/btc_eth/test.js @@ -282,7 +282,8 @@ describe("RFC003: Bitcoin for Ether", () => { ); await bob.wallet.deploy_eth_contract( bob_funding_action.data, - new ethutil.BN(bob_funding_action.value, 10) + new ethutil.BN(bob_funding_action.value, 10), + bob_funding_action.gas_limit ); }); diff --git a/api_tests/e2e/rfc003/eth_btc/config.toml b/api_tests/e2e/rfc003/eth_btc/config.toml new file mode 100644 index 0000000000..124a6d910d --- /dev/null +++ b/api_tests/e2e/rfc003/eth_btc/config.toml @@ -0,0 +1,14 @@ +ledgers = ["bitcoin", "ethereum"] + +[comit_node.alice] +host = "127.0.0.1" +config_dir = "./regtest/alice/" + +[comit_node.bob] +host = "127.0.0.1" +config_dir = "./regtest/bob/" + +[ledger_query_service.main.env] +LEDGER_QUERY_SERVICE_CONFIG_PATH = "./regtest/ledger_query_service" +ETHEREUM_POLLING_TIME_SEC = "1" +RUST_LOG = "warn,ledger_query_service=debug,warp=info" diff --git a/api_tests/e2e/rfc003/eth_btc/test.js b/api_tests/e2e/rfc003/eth_btc/test.js new file mode 100644 index 0000000000..502c41966f --- /dev/null +++ b/api_tests/e2e/rfc003/eth_btc/test.js @@ -0,0 +1,429 @@ +const chai = require("chai"); +chai.use(require("chai-http")); +const test_lib = require("../../../test_lib.js"); +const should = chai.should(); +const ethutil = require("ethereumjs-util"); + +const web3 = test_lib.web3(); +const logger = test_lib.logger(); + +const bob_initial_eth = "0.1"; +const alice_initial_eth = "11"; + +const alice = test_lib.comit_conf("alice", {}); +const bob = test_lib.comit_conf("bob", {}); + +const alice_final_address = + "bcrt1qs2aderg3whgu0m8uadn6dwxjf7j3wx97kk2qqtrum89pmfcxknhsf89pj0"; +const bob_final_address = "0x03a329c0248369a73afac7f9381e02fb43d2ea72"; + +const alpha_asset = new ethutil.BN(web3.utils.toWei("10", "ether"), 10); +const beta_asset = 100000000; +const beta_max_fee = 5000; // Max 5000 satoshis fee + +describe("RFC003: Ether for Bitcoin", () => { + before(async function() { + this.timeout(5000); + await test_lib.btc_activate_segwit(); + await alice.wallet.fund_eth(alice_initial_eth); + await alice.wallet.fund_btc(0.1); + await bob.wallet.fund_eth(bob_initial_eth); + await bob.wallet.fund_btc(10); + await test_lib.btc_import_address(alice_final_address); // Watch only import + await test_lib.btc_import_address(bob.wallet.btc_identity().address); // Watch only import + await test_lib.btc_import_address(alice.wallet.btc_identity().address); // Watch only import + await test_lib.btc_generate(); + + await test_lib.log_btc_balance( + "Before", + "Alice", + alice_final_address, + "final" + ); + await test_lib.log_btc_balance( + "Before", + "Alice", + alice.wallet.btc_identity().address, + "wallet" + ); + await test_lib.log_eth_balance( + "Before", + "Alice", + alice.wallet.eth_address(), + "wallet" + ); + + await test_lib.log_eth_balance( + "Before", + "Bob", + bob_final_address, + "final" + ); + await test_lib.log_btc_balance( + "Before", + "Bob", + bob.wallet.btc_identity().address, + "wallet" + ); + await test_lib.log_eth_balance( + "Before", + "Bob", + bob.wallet.eth_address(), + "wallet" + ); + }); + + after(async function() { + await test_lib.log_btc_balance( + "After", + "Alice", + alice_final_address, + "final" + ); + await test_lib.log_btc_balance( + "After", + "Alice", + alice.wallet.btc_identity().address, + "wallet" + ); + await test_lib.log_eth_balance( + "After", + "Alice", + alice.wallet.eth_address(), + "wallet" + ); + + await test_lib.log_eth_balance( + "After", + "Bob", + bob_final_address, + "final" + ); + await test_lib.log_btc_balance( + "After", + "Bob", + bob.wallet.btc_identity().address, + "wallet" + ); + await test_lib.log_eth_balance( + "After", + "Bob", + bob.wallet.eth_address(), + "wallet" + ); + }); + + let swap_location; + let alice_swap_href; + + it("[Alice] Should be able to make first swap request via HTTP api", async () => { + await chai + .request(alice.comit_node_url()) + .post("/swaps/rfc003") + .send({ + alpha_ledger: { + name: "Ethereum", + }, + beta_ledger: { + name: "Bitcoin", + network: "regtest", + }, + alpha_asset: { + name: "Ether", + quantity: alpha_asset.toString(), + }, + beta_asset: { + name: "Bitcoin", + quantity: beta_asset.toString(), + }, + alpha_ledger_refund_identity: alice.wallet.eth_address(), + beta_ledger_redeem_identity: null, + alpha_ledger_lock_duration: 144, + }) + .then(res => { + res.should.have.status(201); + swap_location = res.headers.location; + logger.info("Alice created a new swap at %s", swap_location); + swap_location.should.be.a("string"); + alice_swap_href = swap_location; + }); + }); + + it("[Alice] Should be in Start state after sending the swap request to Bob", async function() { + await alice.poll_comit_node_until(chai, alice_swap_href, "Start"); + }); + + let bob_swap_href; + + it("[Bob] Shows the Swap as Start in /swaps", async () => { + let res = await chai.request(bob.comit_node_url()).get("/swaps"); + + let embedded = res.body._embedded; + let swap_embedded = embedded.swaps[0]; + swap_embedded.protocol.should.equal("rfc003"); + swap_embedded.state.should.equal("Start"); + let swap_link = swap_embedded._links; + swap_link.should.be.a("object"); + bob_swap_href = swap_link.self.href; + bob_swap_href.should.be.a("string"); + logger.info("Bob discovered a new swap at %s", bob_swap_href); + }); + + let bob_accept_href; + + it("[Bob] Can get the accept action", async () => { + let res = await chai.request(bob.comit_node_url()).get(bob_swap_href); + res.should.have.status(200); + res.body.state.should.equal("Start"); + res.body._links.accept.href.should.be.a("string"); + bob_accept_href = res.body._links.accept.href; + }); + + it("[Bob] Can execute the accept action", async () => { + let bob_response = { + beta_ledger_refund_identity: null, + alpha_ledger_redeem_identity: bob_final_address, + beta_ledger_lock_duration: 43200, + }; + + logger.info( + "Bob is accepting the swap via %s with the following parameters", + bob_accept_href, + bob_response + ); + + let accept_res = await chai + .request(bob.comit_node_url()) + .post(bob_accept_href) + .send(bob_response); + + accept_res.should.have.status(200); + }); + + it("[Bob] Should be in the Accepted State after accepting", async () => { + await bob.poll_comit_node_until(chai, bob_swap_href, "Accepted"); + }); + + let alice_funding_href; + + it("[Alice] Can get the HTLC fund action", async () => { + let res = await chai + .request(alice.comit_node_url()) + .get(alice_swap_href); + res.should.have.status(200); + res.body.state.should.equal("Accepted"); + let links = res.body._links; + links.should.have.property("fund"); + alice_funding_href = links.fund.href; + }); + + let alice_funding_action; + + it("[Alice] Can get the funding action from the ‘fund’ link", async () => { + let res = await chai + .request(alice.comit_node_url()) + .get(alice_funding_href); + res.should.have.status(200); + alice_funding_action = res.body; + + logger.info( + "Alice retrieved the following funding parameters", + alice_funding_action + ); + }); + + it("[Alice] Can execute the funding action", async () => { + alice_funding_action.should.include.all.keys( + "data", + "value", + "gas_limit" + ); + + let result = await alice.wallet.deploy_eth_contract( + alice_funding_action.data, + alice_funding_action.value, + alice_funding_action.gas_limit + ); + }); + + it("[Alice] Should be in AlphaFunded state after executing the funding action", async function() { + this.timeout(10000); + await alice.poll_comit_node_until(chai, alice_swap_href, "AlphaFunded"); + }); + + let bob_funding_href; + + it("[Bob] Should be in AlphaFunded state after Alice executes the funding action", async function() { + this.timeout(10000); + let swap = await bob.poll_comit_node_until( + chai, + bob_swap_href, + "AlphaFunded" + ); + swap.should.have.property("_links"); + swap._links.should.have.property("fund"); + bob_funding_href = swap._links.fund.href; + }); + + let bob_funding_action; + + it("[Bob] Can get the funding action from the ‘fund’ link", async () => { + let res = await chai + .request(bob.comit_node_url()) + .get(bob_funding_href); + res.should.have.status(200); + bob_funding_action = res.body; + + logger.info( + "Bob retrieved the following funding parameters", + bob_funding_action + ); + }); + + it("[Bob] Can execute the funding action", async () => { + bob_funding_action.should.include.all.keys("address", "value"); + await bob.wallet.send_btc_to_address( + bob_funding_action.address, + parseInt(bob_funding_action.value) + ); + }); + + let alice_redeem_href; + + it("[Alice] Should be in BothFunded state after Bob executes the funding action", async function() { + this.timeout(10000); + let swap = await alice.poll_comit_node_until( + chai, + alice_swap_href, + "BothFunded" + ); + swap.should.have.property("_links"); + swap._links.should.have.property("redeem"); + alice_redeem_href = swap._links.redeem.href; + }); + + it("[Bob] Should be in BothFunded state after executing the funding action", async function() { + this.timeout(10000); + await bob.poll_comit_node_until(chai, bob_swap_href, "BothFunded"); + }); + + let alice_redeem_action; + + it("[Alice] Can get the redeem action from the ‘redeem’ link", async () => { + let res = await chai + .request(alice.comit_node_url()) + .get( + alice_redeem_href + + "?address=" + + alice_final_address + + "&fee_per_byte=20" + ); + res.should.have.status(200); + alice_redeem_action = res.body; + + logger.info( + "Alice retrieved the following redeem parameters", + alice_redeem_action + ); + }); + + let alice_btc_balance_before; + + it("[Alice] Can execute the redeem action", async function() { + alice_redeem_action.should.include.all.keys("hex"); + alice_btc_balance_before = await test_lib.btc_balance( + alice_final_address + ); + await alice.wallet.send_raw_tx(alice_redeem_action.hex); + await test_lib.btc_generate(); + }); + + it("[Alice] Should be in AlphaFundedBetaRedeemed state after executing the redeem action", async function() { + this.timeout(10000); + await alice.poll_comit_node_until( + chai, + alice_swap_href, + "AlphaFundedBetaRedeemed" + ); + }); + + it("[Alice] Should have received the beta asset after the redeem", async function() { + let alice_btc_balance_after = await test_lib.btc_balance( + alice_final_address + ); + + const alice_btc_balance_expected = + alice_btc_balance_before + beta_asset - beta_max_fee; + alice_btc_balance_after.should.be.at.least(alice_btc_balance_expected); + }); + + let bob_redeem_href; + + it("[Bob] Should be in AlphaFundedBetaRedeemed state after Alice executes the redeem action", async function() { + this.timeout(10000); + let swap = await bob.poll_comit_node_until( + chai, + bob_swap_href, + "AlphaFundedBetaRedeemed" + ); + swap.should.have.property("_links"); + swap._links.should.have.property("redeem"); + bob_redeem_href = swap._links.redeem.href; + }); + + let bob_redeem_action; + + it("[Bob] Can get the redeem action from the ‘redeem’ link", async () => { + let res = await chai.request(bob.comit_node_url()).get(bob_redeem_href); + res.should.have.status(200); + bob_redeem_action = res.body; + + logger.info( + "Bob retrieved the following redeem parameters", + bob_redeem_action + ); + }); + + let bob_eth_balance_before; + + it("[Bob] Can execute the redeem action", async function() { + bob_redeem_action.should.include.all.keys( + "to", + "data", + "gas_limit", + "value" + ); + bob_eth_balance_before = await test_lib.eth_balance(bob_final_address); + await bob.wallet.send_eth_transaction_to( + bob_redeem_action.to, + bob_redeem_action.data, + bob_redeem_action.value, + bob_redeem_action.gas_limit + ); + }); + + it("[Bob] Should have received the alpha asset after the redeem", async function() { + let bob_eth_balance_after = await test_lib.eth_balance( + bob_final_address + ); + + let bob_eth_balance_expected = bob_eth_balance_before.add(alpha_asset); + bob_eth_balance_after + .toString() + .should.be.equal(bob_eth_balance_expected.toString()); + }); + + it("[Alice] Should be in BothRedeemed state after Bob executes the redeem action", async function() { + this.timeout(10000); + await alice.poll_comit_node_until( + chai, + alice_swap_href, + "BothRedeemed" + ); + }); + + it("[Bob] Should be in BothRedeemed state after executing the redeem action", async function() { + this.timeout(10000); + await bob.poll_comit_node_until(chai, bob_swap_href, "BothRedeemed"); + }); +}); diff --git a/application/comit_node/Cargo.toml b/application/comit_node/Cargo.toml index e34c6b36a0..ceb0faf82c 100644 --- a/application/comit_node/Cargo.toml +++ b/application/comit_node/Cargo.toml @@ -13,6 +13,7 @@ bitcoin_rpc_client = "0.4" chrono = "0.4" config = "0.9" debug_stub_derive = "0.3" +directories = "1.0" either = "1.5" enum-display-derive = "0.1" failure = "0.1" diff --git a/application/comit_node/src/bam_api/rfc003/swap.rs b/application/comit_node/src/bam_api/rfc003/swap.rs index 1a21ea04f6..776e702a9a 100644 --- a/application/comit_node/src/bam_api/rfc003/swap.rs +++ b/application/comit_node/src/bam_api/rfc003/swap.rs @@ -80,6 +80,29 @@ pub fn swap_config(bob_spawner: Arc) -> Config( + &request, + ) + { + let response_future = match bob_spawner.spawn(swap_id, swap_request) { + Ok(response_future) => response_future, + Err(e) => { + error!("Unable to spawn Bob: {:?}", e); + return Box::new(future::ok(Response::new(Status::RE(0)))); + } + }; + + Box::new(response_future.then(move |result| match result { + Ok(response) => Ok(to_bam_response::(response)), + Err(_) => { + warn!( + "Failed to receive from oneshot channel for swap {}", + swap_id + ); + Ok(Response::new(Status::SE(0))) + } + })) } else { // TODO: Specify and implement response code Box::new(future::ok(Response::new(Status::SE(0)))) diff --git a/application/comit_node/src/bin/comit_node.rs b/application/comit_node/src/bin/comit_node.rs index 3c49ae4593..58d16dd511 100755 --- a/application/comit_node/src/bin/comit_node.rs +++ b/application/comit_node/src/bin/comit_node.rs @@ -3,6 +3,7 @@ #[macro_use] extern crate log; +use directories; use comit_node::{ comit_client, comit_server, @@ -56,13 +57,23 @@ fn main() -> Result<(), failure::Error> { } fn load_settings() -> Result { - let comit_config_path = var_or_default("COMIT_NODE_CONFIG_PATH", "~/.config/comit_node".into()); - let run_mode_config = var_or_default("RUN_MODE", "development".into()); - let default_config = format!("{}/{}", comit_config_path.trim(), "default"); - let run_mode_config = format!("{}/{}", comit_config_path.trim(), run_mode_config); - - let settings = ComitNodeSettings::create(default_config, run_mode_config)?; - Ok(settings) + match directories::UserDirs::new() { + None => Err(config::ConfigError::Message( + "could not determine user's home directory".to_string(), + )), + Some(dirs) => { + let default_config = std::path::Path::join(dirs.home_dir(), ".config/comit_node"); + let comit_config_path = var_or_default( + "COMIT_NODE_CONFIG_PATH", + default_config.to_string_lossy().to_string(), + ); + let run_mode_config = var_or_default("RUN_MODE", "development".into()); + let default_config = format!("{}/{}", comit_config_path.trim(), "default"); + let run_mode_config = format!("{}/{}", comit_config_path.trim(), run_mode_config); + let settings = ComitNodeSettings::create(default_config, run_mode_config)?; + Ok(settings) + } + } } fn create_ledger_query_service_api_client( diff --git a/application/comit_node/src/http_api/rfc003/action.rs b/application/comit_node/src/http_api/rfc003/action.rs index ac0818c0b0..56151618af 100644 --- a/application/comit_node/src/http_api/rfc003/action.rs +++ b/application/comit_node/src/http_api/rfc003/action.rs @@ -106,6 +106,30 @@ impl FromAcceptSwapRequestHttpBody } } +impl FromAcceptSwapRequestHttpBody + for StateMachineResponse< + ethereum_support::Address, + secp256k1_support::KeyPair, + bitcoin_support::Blocks, + > +{ + fn from_accept_swap_request_http_body( + body: AcceptSwapRequestHttpBody, + id: SwapId, + secret_source: &dyn SecretSource, + ) -> Result { + match body { + AcceptSwapRequestHttpBody::OnlyRefund { .. } | AcceptSwapRequestHttpBody::RefundAndRedeem { .. } => Err(HttpApiProblem::with_title_and_type_from_status(400).set_detail("The refund identity for swaps where Bitcoin is the BetaLedger has to be provided on-demand, i.e. when the refund action is executed.")), + AcceptSwapRequestHttpBody::None { .. } => Err(HttpApiProblem::with_title_and_type_from_status(400).set_detail("Missing beta_ledger_redeem_identity")), + AcceptSwapRequestHttpBody::OnlyRedeem { alpha_ledger_redeem_identity, beta_ledger_lock_duration } => Ok(StateMachineResponse { + alpha_ledger_redeem_identity, + beta_ledger_lock_duration, + beta_ledger_refund_identity: secret_source.new_secp256k1_refund(id), + }), + } + } +} + trait ExecuteDecline { fn execute(&self, reason: Option) -> Result<(), HttpApiProblem>; } @@ -560,10 +584,10 @@ fn handle_get, S: StateStore>( let metadata = metadata_store .get(id)? .ok_or_else(problem::swap_not_found)?; + with_swap_types!( &metadata, (|| { - trace!("Fetched metadata of swap with id {}: {:?}", id, metadata); let state = state_store .get::(id)? .ok_or_else(problem::state_store)?; diff --git a/application/comit_node/src/http_api/rfc003/swap.rs b/application/comit_node/src/http_api/rfc003/swap.rs index d8317cd772..ccda5597ae 100644 --- a/application/comit_node/src/http_api/rfc003/swap.rs +++ b/application/comit_node/src/http_api/rfc003/swap.rs @@ -37,6 +37,9 @@ pub enum SwapRequestBodyKind { BitcoinEthereumBitcoinQuantityErc20Quantity( SwapRequestBody, ), + EthereumBitcoinEtherQuantityBitcoinQuantity( + SwapRequestBody, + ), // It is important that these two come last because untagged enums are tried in order UnsupportedCombination(Box), MalformedRequest(serde_json::Value), @@ -111,6 +114,33 @@ impl FromSwapRequestBodyIdentities } } +impl FromSwapRequestBodyIdentities + for rfc003::alice::SwapRequestIdentities +{ + fn from_swap_request_body_identities( + identities: SwapRequestBodyIdentities< + ethereum_support::Address, + bitcoin_support::PubkeyHash, + >, + id: SwapId, + secret_source: &dyn SecretSource, + ) -> Result { + match identities { + SwapRequestBodyIdentities::RefundAndRedeem { .. } + | SwapRequestBodyIdentities::OnlyRedeem { .. } + | SwapRequestBodyIdentities::None {} => { + Err(HttpApiProblem::with_title_and_type_from_status(400)) + } + SwapRequestBodyIdentities::OnlyRefund { + alpha_ledger_refund_identity, + } => Ok(rfc003::alice::SwapRequestIdentities { + alpha_ledger_refund_identity, + beta_ledger_redeem_identity: secret_source.new_secp256k1_redeem(id), + }), + } + } +} + trait FromSwapRequestBody where Self: Sized, @@ -205,6 +235,11 @@ fn handle_post_swap( id, rfc003::alice::SwapRequest::from_swap_request_body(body, id, secret_source)?, )?, + SwapRequestBodyKind::EthereumBitcoinEtherQuantityBitcoinQuantity(body) => alice_spawner + .spawn( + id, + rfc003::alice::SwapRequest::from_swap_request_body(body, id, secret_source)?, + )?, SwapRequestBodyKind::UnsupportedCombination(body) => { error!( "Swapping {:?} for {:?} from {:?} to {:?} is not supported", diff --git a/application/comit_node/src/http_api/rfc003/with_swap_types.rs b/application/comit_node/src/http_api/rfc003/with_swap_types.rs index 087ea193ee..697a3b7600 100644 --- a/application/comit_node/src/http_api/rfc003/with_swap_types.rs +++ b/application/comit_node/src/http_api/rfc003/with_swap_types.rs @@ -60,6 +60,24 @@ macro_rules! with_swap_types { _match_role!(role, $fn) } + Metadata { + alpha_ledger: LedgerKind::Ethereum, + beta_ledger: LedgerKind::Bitcoin, + alpha_asset: AssetKind::Ether, + beta_asset: AssetKind::Bitcoin, + role, + } => { + #[allow(dead_code)] + type AL = Ethereum; + #[allow(dead_code)] + type BL = Bitcoin; + #[allow(dead_code)] + type AA = EtherQuantity; + #[allow(dead_code)] + type BA = BitcoinQuantity; + + _match_role!(role, $fn) + } _ => unimplemented!(), } }}; @@ -124,6 +142,24 @@ macro_rules! with_swap_types_bob { _match_role_bob!(role, $fn) } + Metadata { + alpha_ledger: LedgerKind::Ethereum, + beta_ledger: LedgerKind::Bitcoin, + alpha_asset: AssetKind::Ether, + beta_asset: AssetKind::Bitcoin, + role, + } => { + #[allow(dead_code)] + type AL = Ethereum; + #[allow(dead_code)] + type BL = Bitcoin; + #[allow(dead_code)] + type AA = EtherQuantity; + #[allow(dead_code)] + type BA = BitcoinQuantity; + + _match_role_bob!(role, $fn) + } _ => unimplemented!(), } }}; diff --git a/application/comit_node/src/swap_protocols/rfc003/actions/mod.rs b/application/comit_node/src/swap_protocols/rfc003/actions/mod.rs deleted file mode 100644 index a88feb186f..0000000000 --- a/application/comit_node/src/swap_protocols/rfc003/actions/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod alice; -pub mod bob; - -pub trait Actions { - type ActionKind; - - fn actions(&self) -> Vec; -} diff --git a/application/comit_node/src/swap_protocols/rfc003/alice/actions/eth_btc.rs b/application/comit_node/src/swap_protocols/rfc003/alice/actions/eth_btc.rs new file mode 100644 index 0000000000..dabf5de02a --- /dev/null +++ b/application/comit_node/src/swap_protocols/rfc003/alice/actions/eth_btc.rs @@ -0,0 +1,105 @@ +use crate::swap_protocols::{ + ledger::{Bitcoin, Ethereum}, + rfc003::{ + alice, bitcoin, + ethereum::{self, EtherHtlc, Htlc}, + state_machine::*, + Actions, Alice, + }, +}; +use bitcoin_support::{BitcoinQuantity, OutPoint}; +use bitcoin_witness::PrimedInput; +use ethereum_support::{Bytes, EtherQuantity}; + +impl OngoingSwap> { + pub fn fund_action(&self) -> ethereum::ContractDeploy { + let htlc = EtherHtlc::from(self.alpha_htlc_params()); + let data = htlc.compile_to_hex().into(); + let gas_limit = htlc.deployment_gas_limit(); + + ethereum::ContractDeploy { + data, + value: self.alpha_asset, + gas_limit, + } + } + + pub fn refund_action( + &self, + alpha_htlc_location: ethereum_support::Address, + ) -> ethereum::SendTransaction { + let data = Bytes::default(); + let gas_limit = EtherHtlc::tx_gas_limit(); + + ethereum::SendTransaction { + to: alpha_htlc_location, + data, + gas_limit, + value: EtherQuantity::zero(), + } + } + + pub fn redeem_action(&self, beta_htlc_location: OutPoint) -> bitcoin::SpendOutput { + let htlc: bitcoin::Htlc = self.beta_htlc_params().into(); + + bitcoin::SpendOutput { + output: PrimedInput::new( + beta_htlc_location, + self.beta_asset, + htlc.unlock_with_secret(self.beta_ledger_redeem_identity, &self.secret), + ), + } + } +} + +impl Actions for SwapStates> { + type ActionKind = alice::ActionKind< + (), + ethereum::ContractDeploy, + bitcoin::SpendOutput, + ethereum::SendTransaction, + >; + + fn actions(&self) -> Vec { + use self::SwapStates as SS; + match *self { + SS::Accepted(Accepted { ref swap, .. }) => { + vec![alice::ActionKind::Fund(swap.fund_action())] + } + SS::BothFunded(BothFunded { + ref alpha_htlc_location, + ref beta_htlc_location, + ref swap, + .. + }) => vec![ + alice::ActionKind::Redeem(swap.redeem_action(*beta_htlc_location)), + alice::ActionKind::Refund(swap.refund_action(*alpha_htlc_location)), + ], + SS::AlphaFundedBetaRefunded(AlphaFundedBetaRefunded { + ref swap, + ref alpha_htlc_location, + .. + }) + | SS::AlphaFundedBetaRedeemed(AlphaFundedBetaRedeemed { + ref swap, + ref alpha_htlc_location, + .. + }) => vec![alice::ActionKind::Refund( + swap.refund_action(*alpha_htlc_location), + )], + SS::AlphaRefundedBetaFunded(AlphaRefundedBetaFunded { + ref beta_htlc_location, + ref swap, + .. + }) + | SS::AlphaRedeemedBetaFunded(AlphaRedeemedBetaFunded { + ref beta_htlc_location, + ref swap, + .. + }) => vec![alice::ActionKind::Redeem( + swap.redeem_action(*beta_htlc_location), + )], + _ => vec![], + } + } +} diff --git a/application/comit_node/src/swap_protocols/rfc003/alice/actions/mod.rs b/application/comit_node/src/swap_protocols/rfc003/alice/actions/mod.rs index 8a9c22197b..d5ad76c271 100644 --- a/application/comit_node/src/swap_protocols/rfc003/alice/actions/mod.rs +++ b/application/comit_node/src/swap_protocols/rfc003/alice/actions/mod.rs @@ -1,5 +1,6 @@ mod btc_erc20; mod btc_eth; +mod eth_btc; #[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] pub enum ActionKind { diff --git a/application/comit_node/src/swap_protocols/rfc003/alice/swap_request.rs b/application/comit_node/src/swap_protocols/rfc003/alice/swap_request.rs index 4e2d47a165..fbdb96feac 100644 --- a/application/comit_node/src/swap_protocols/rfc003/alice/swap_request.rs +++ b/application/comit_node/src/swap_protocols/rfc003/alice/swap_request.rs @@ -45,3 +45,15 @@ impl From> for Me } } } + +impl From> for Metadata { + fn from(_: SwapRequest) -> Self { + Self { + alpha_ledger: LedgerKind::Ethereum, + beta_ledger: LedgerKind::Bitcoin, + alpha_asset: AssetKind::Ether, + beta_asset: AssetKind::Bitcoin, + role: RoleKind::Alice, + } + } +} diff --git a/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_erc20.rs b/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_erc20.rs index ee6a529c5d..fc2c414f06 100644 --- a/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_erc20.rs +++ b/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_erc20.rs @@ -61,12 +61,12 @@ impl OngoingSwap> { pub fn redeem_action( &self, - beta_htlc_location: OutPoint, + alpha_htlc_location: OutPoint, secret: Secret, ) -> bitcoin::SpendOutput { bitcoin::SpendOutput { output: PrimedInput::new( - beta_htlc_location, + alpha_htlc_location, self.alpha_asset, bitcoin::Htlc::from(self.alpha_htlc_params()) .unlock_with_secret(self.alpha_ledger_redeem_identity, &secret), diff --git a/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_eth.rs b/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_eth.rs index 95d0d7f864..c4b95a27aa 100644 --- a/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_eth.rs +++ b/application/comit_node/src/swap_protocols/rfc003/bob/actions/btc_eth.rs @@ -31,13 +31,13 @@ impl OngoingSwap> { pub fn refund_action( &self, - alpha_htlc_location: ethereum_support::Address, + beta_htlc_location: ethereum_support::Address, ) -> ethereum::SendTransaction { let data = Bytes::default(); let gas_limit = EtherHtlc::tx_gas_limit(); ethereum::SendTransaction { - to: alpha_htlc_location, + to: beta_htlc_location, data, gas_limit, value: EtherQuantity::zero(), @@ -46,14 +46,14 @@ impl OngoingSwap> { pub fn redeem_action( &self, - beta_htlc_location: OutPoint, + alpha_htlc_location: OutPoint, secret: Secret, ) -> bitcoin::SpendOutput { let htlc: bitcoin::Htlc = self.alpha_htlc_params().into(); bitcoin::SpendOutput { output: PrimedInput::new( - beta_htlc_location, + alpha_htlc_location, self.alpha_asset, htlc.unlock_with_secret(self.alpha_ledger_redeem_identity, &secret), ), @@ -85,18 +85,13 @@ impl Actions for SwapStates vec![bob::ActionKind::Refund( - swap.refund_action(*beta_htlc_location), - )], - SS::AlphaFundedBetaRefunded { .. } => vec![], - SS::AlphaRedeemedBetaFunded(AlphaRedeemedBetaFunded { + }) + | SS::AlphaRedeemedBetaFunded(AlphaRedeemedBetaFunded { ref beta_htlc_location, ref swap, .. - }) => vec![bob::ActionKind::Refund( - swap.refund_action(*beta_htlc_location), - )], - SS::AlphaRefundedBetaFunded(AlphaRefundedBetaFunded { + }) + | SS::AlphaRefundedBetaFunded(AlphaRefundedBetaFunded { ref beta_htlc_location, ref swap, .. diff --git a/application/comit_node/src/swap_protocols/rfc003/bob/actions/eth_btc.rs b/application/comit_node/src/swap_protocols/rfc003/bob/actions/eth_btc.rs new file mode 100644 index 0000000000..1e22587670 --- /dev/null +++ b/application/comit_node/src/swap_protocols/rfc003/bob/actions/eth_btc.rs @@ -0,0 +1,103 @@ +use crate::swap_protocols::{ + ledger::{Bitcoin, Ethereum}, + rfc003::{ + bitcoin, + bob::{ + self, + actions::{Accept, Decline}, + }, + ethereum::{self, EtherHtlc}, + secret::Secret, + state_machine::*, + Actions, Bob, + }, +}; +use bitcoin_support::{BitcoinQuantity, OutPoint}; +use bitcoin_witness::PrimedInput; +use ethereum_support::{Bytes, EtherQuantity, U256}; + +impl OngoingSwap> { + pub fn fund_action(&self) -> bitcoin::SendToAddress { + bitcoin::SendToAddress { + address: self.beta_htlc_params().compute_address(), + value: self.beta_asset, + } + } + + pub fn refund_action(&self, beta_htlc_location: OutPoint) -> bitcoin::SpendOutput { + bitcoin::SpendOutput { + output: PrimedInput::new( + beta_htlc_location, + self.beta_asset, + bitcoin::Htlc::from(self.beta_htlc_params()) + .unlock_after_timeout(self.beta_ledger_refund_identity), + ), + } + } + + pub fn redeem_action( + &self, + alpha_htlc_location: ethereum_support::Address, + secret: Secret, + ) -> ethereum::SendTransaction { + let data = Bytes::from(secret.raw_secret().to_vec()); + let gas_limit = EtherHtlc::tx_gas_limit(); + + ethereum::SendTransaction { + to: alpha_htlc_location, + data, + gas_limit, + value: EtherQuantity::from_wei(U256::zero()), + } + } +} + +impl Actions for SwapStates> { + type ActionKind = bob::ActionKind< + Accept, + Decline, + (), + bitcoin::SendToAddress, + ethereum::SendTransaction, + bitcoin::SpendOutput, + >; + + fn actions(&self) -> Vec { + use self::SwapStates as SS; + match *self { + SS::Start(Start { ref role, .. }) => vec![ + bob::ActionKind::Accept(role.accept_action()), + bob::ActionKind::Decline(role.decline_action()), + ], + SS::AlphaFunded(AlphaFunded { ref swap, .. }) => { + vec![bob::ActionKind::Fund(swap.fund_action())] + } + SS::BothFunded(BothFunded { + ref beta_htlc_location, + ref swap, + .. + }) + | SS::AlphaRedeemedBetaFunded(AlphaRedeemedBetaFunded { + ref beta_htlc_location, + ref swap, + .. + }) + | SS::AlphaRefundedBetaFunded(AlphaRefundedBetaFunded { + ref beta_htlc_location, + ref swap, + .. + }) => vec![bob::ActionKind::Refund( + swap.refund_action(*beta_htlc_location), + )], + SS::AlphaFundedBetaRedeemed(AlphaFundedBetaRedeemed { + ref swap, + ref alpha_htlc_location, + ref beta_redeemed_tx, + .. + }) => vec![bob::ActionKind::Redeem( + swap.redeem_action(*alpha_htlc_location, beta_redeemed_tx.secret), + )], + _ => vec![], + } + } +} diff --git a/application/comit_node/src/swap_protocols/rfc003/bob/actions/mod.rs b/application/comit_node/src/swap_protocols/rfc003/bob/actions/mod.rs index 7944175d03..3315f572e2 100644 --- a/application/comit_node/src/swap_protocols/rfc003/bob/actions/mod.rs +++ b/application/comit_node/src/swap_protocols/rfc003/bob/actions/mod.rs @@ -1,5 +1,6 @@ mod btc_erc20; mod btc_eth; +mod eth_btc; use crate::{ comit_client::{SwapDeclineReason, SwapReject}, diff --git a/application/comit_node/src/swap_protocols/rfc003/bob/swap_request.rs b/application/comit_node/src/swap_protocols/rfc003/bob/swap_request.rs index 82a6b62151..34fa8358f5 100644 --- a/application/comit_node/src/swap_protocols/rfc003/bob/swap_request.rs +++ b/application/comit_node/src/swap_protocols/rfc003/bob/swap_request.rs @@ -41,3 +41,15 @@ impl From> for Me } } } + +impl From> for Metadata { + fn from(_: SwapRequest) -> Self { + Self { + alpha_ledger: LedgerKind::Ethereum, + beta_ledger: LedgerKind::Bitcoin, + alpha_asset: AssetKind::Ether, + beta_asset: AssetKind::Bitcoin, + role: RoleKind::Bob, + } + } +} diff --git a/application/comit_node/src/swap_protocols/rfc003/ethereum/ether_htlc.rs b/application/comit_node/src/swap_protocols/rfc003/ethereum/ether_htlc.rs index e49ab5a402..31fa0a1f94 100644 --- a/application/comit_node/src/swap_protocols/rfc003/ethereum/ether_htlc.rs +++ b/application/comit_node/src/swap_protocols/rfc003/ethereum/ether_htlc.rs @@ -49,7 +49,7 @@ impl EtherHtlc { let n_bytes = bytes.0.len(); let gas_per_byte = 200; - U256::from(50_000 + n_bytes * gas_per_byte) + U256::from(75_000 + n_bytes * gas_per_byte) } pub fn tx_gas_limit() -> U256 { diff --git a/application/comit_node/src/swap_protocols/rfc003/lightning/mod.rs b/application/comit_node/src/swap_protocols/rfc003/lightning/mod.rs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/application/comit_node/src/swap_protocols/rfc003/role.rs b/application/comit_node/src/swap_protocols/rfc003/role.rs index d30b6a9b3d..4a8a2aecf0 100644 --- a/application/comit_node/src/swap_protocols/rfc003/role.rs +++ b/application/comit_node/src/swap_protocols/rfc003/role.rs @@ -49,32 +49,6 @@ pub struct Initiation { pub secret: R::Secret, } -#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] -pub enum ActionKind { - Accept(Accept), - Decline(Decline), - Deploy(Deploy), - Fund(Fund), - Redeem(Redeem), - Refund(Refund), -} - -impl - ActionKind -{ - pub fn name(&self) -> String { - use self::ActionKind::*; - match *self { - Accept(_) => String::from("accept"), - Decline(_) => String::from("decline"), - Deploy(_) => String::from("deploy"), - Fund(_) => String::from("fund"), - Redeem(_) => String::from("redeem"), - Refund(_) => String::from("refund"), - } - } -} - pub trait Actions { type ActionKind; diff --git a/application/comit_node/src/swap_protocols/rfc003/secret.rs b/application/comit_node/src/swap_protocols/rfc003/secret.rs index c2574bc0fb..737123c7c8 100644 --- a/application/comit_node/src/swap_protocols/rfc003/secret.rs +++ b/application/comit_node/src/swap_protocols/rfc003/secret.rs @@ -128,13 +128,6 @@ impl Secret { pub const LENGTH: usize = 32; pub const LENGTH_U8: u8 = 32; - pub fn generate(rng: &mut T) -> Secret { - let random_bytes = rng.gen_random_bytes(Self::LENGTH); - let mut secret = [0; Self::LENGTH]; - secret.copy_from_slice(&random_bytes[..]); - Secret::from(secret) - } - pub fn from_vec(vec: &[u8]) -> Result { if vec.len() != Self::LENGTH { return Err(FromErr::InvalidLength { @@ -274,9 +267,8 @@ mod tests { #[test] fn round_trip_secret_serialization() { - let mut rng = rand::thread_rng(); - - let secret = Secret::generate(&mut rng); + let bytes = b"hello world, you are beautiful!!"; + let secret = Secret::from(*bytes); let json_secret = serde_json::to_string(&secret).unwrap(); let deser_secret = serde_json::from_str::(json_secret.as_str()).unwrap();