Skip to content

Commit

Permalink
Move config consts to top of files, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-belcher committed Feb 17, 2022
1 parent 85610a0 commit 77961a7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Project design document: [Design for a CoinSwap Implementation for Massively Imp
## Contents

- [State of the project](#state-of-the-project)
- [How to create a CoinSwap on regtest or testnet](#how-to-create-a-coinswap-on-regtest-or-testnet)
- [How to create a CoinSwap on regtest](#how-to-create-a-coinswap-on-regtest)
- [Developer resources](#developer-resources)
- [Protocol between takers and makers](#protocol-between-takers-and-makers)
- [Notes on architecture](#notes-on-architecture)
Expand All @@ -24,13 +24,13 @@ Project design document: [Design for a CoinSwap Implementation for Massively Imp

The project is nowhere near usable. The code written so far is published for developers to play around with. It doesn't have config files yet so you have to edit the source files to configure stuff.

## How to create a CoinSwap on regtest or testnet
## How to create a CoinSwap on regtest

* Install [rust](https://www.rust-lang.org/) on your machine.

* Start up Bitcoin Core in regtest mode. Make sure the RPC server is enabled with `server=1` and that rpc username and password are set with `rpcuser=yourrpcusername` and `rpcpassword=yourrpcpassword` in the configuration file.

* Download this git repository. Open the file `src/main.rs` and edit the RPC username and password in the function `get_bitcoin_rpc`. Make sure your Bitcoin Core has a wallet called `teleport`, or edit the name in the same function.
* Download this git repository. Open the file `src/lib.rs` and edit the RPC username and password right at the top of the file. Make sure your Bitcoin Core has a wallet called `teleport`, or edit the name in the same place.

* Create three teleport wallets by running `cargo run -- --wallet-file-name=<wallet-name> generate-wallet` thrice. Instead of `<wallet-name>`, use something like `maker1.teleport`, `maker2.teleport` and `taker.teleport`.

Expand Down Expand Up @@ -117,7 +117,13 @@ coin count = 6
total balance = 0.14974828 BTC
```

* To switch between regtest and testnet, edit the constant `NETWORK` which is found near the top of the file `src/wallet_sync.rs`. To edit the coinswap send amount, or the number of taker and maker transactions, look in the file `src/taker_protocol.rs` near the top of the function `send_coinswap`.
## How to create a CoinSwap on networks other than regtest

* To switch between networks like regtest, signet, testnet or mainnet (for the brave), edit the constant `NETWORK` which is found at the top of the file `src/wallet_sync.rs`.

* To run a yield generator (maker) on any network apart from regtest, you will need to create a tor hidden service for your maker. Search the web for "setup tor hidden service", a good article is [this one](https://www.linuxjournal.com/content/tor-hidden-services). When you have your hidden service hostname, copy it into the field near the top of the file `src/maker_protocol.rs`.

* After a successful coinswap created with `do-coinswap`, the coins will still be in the wallet. You can send them out somewhere else using the command `direct-send` and providing the coin(s). For example `cargo run -- --wallet-file-name=taker.teleport direct-send sweep <destination-address> 9bfeec..0cc468:0`. Coins in the wallet can be found by running `wallet-balance` as above.


## Developer resources
Expand Down
38 changes: 19 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const RPC_CREDENTIALS: Option<(&str, &str)> = Some(("regtestrpcuser", "regtestrpcpass"));
//None; // use Bitcoin Core cookie-based authentication

const RPC_HOSTPORT: &str = "localhost:18443";
const RPC_WALLET: &str = "teleport";

extern crate bitcoin;
extern crate bitcoin_wallet;
extern crate bitcoincore_rpc;
Expand Down Expand Up @@ -40,24 +46,7 @@ pub mod watchtower_protocol;

static INIT: Once = Once::new();

/// Setup function that will only run once, even if called multiple times.
pub fn setup_logger() {
INIT.call_once(|| {
env_logger::Builder::from_env(
env_logger::Env::default()
.default_filter_or("teleport=info,main=info,wallet=info")
.default_write_style_or("always"),
)
.init();
});
}

pub fn get_bitcoin_rpc() -> Result<Client, Error> {
//TODO put all this in a config file
const RPC_CREDENTIALS: Option<(&str, &str)> = Some(("regtestrpcuser", "regtestrpcpass"));
//Some(("btcrpcuser", "btcrpcpass"));
//None; // use Bitcoin Core cookie-based authentication

let auth = match RPC_CREDENTIALS {
Some((user, pass)) => Auth::UserPass(user.to_string(), pass.to_string()),
None => {
Expand All @@ -68,14 +57,25 @@ pub fn get_bitcoin_rpc() -> Result<Client, Error> {
}
};
let rpc = Client::new(
"http://localhost:18443/wallet/teleport",
//"http://localhost:18332/wallet/teleport",
&format!("http://{}/wallet/{}", RPC_HOSTPORT, RPC_WALLET),
auth,
)?;
rpc.get_blockchain_info()?;
Ok(rpc)
}

/// Setup function that will only run once, even if called multiple times.
pub fn setup_logger() {
INIT.call_once(|| {
env_logger::Builder::from_env(
env_logger::Env::default()
.default_filter_or("teleport=info,main=info,wallet=info")
.default_write_style_or("always"),
)
.init();
});
}

pub fn generate_wallet(wallet_file_name: &PathBuf) -> std::io::Result<()> {
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Expand Down
2 changes: 1 addition & 1 deletion src/maker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ABSOLUTE_FEE_SAT: u64 = 1000;
const AMOUNT_RELATIVE_FEE_PPB: u64 = 10_000_000;
const TIME_RELATIVE_FEE_PPB: u64 = 100_000;
const REQUIRED_CONFIRMS: i32 = 1;
const MINIMUM_LOCKTIME: u16 = 3;
const MINIMUM_LOCKTIME: u16 = 48;
const MIN_SIZE: u64 = 10000;

//TODO this goes in the config file
Expand Down
7 changes: 4 additions & 3 deletions src/taker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ use crate::watchtower_protocol::{
check_for_broadcasted_contract_txes, ContractTransaction, ContractsInfo,
};

//relatively low value for now so that its easier to test on regtest
pub const REFUND_LOCKTIME: u16 = 3; //in blocks
pub const REFUND_LOCKTIME_STEP: u16 = 3; //in blocks
//relatively low value for now so that its easier to test without having to wait too much
//right now only the very brave will try coinswap out on mainnet with non-trivial amounts
pub const REFUND_LOCKTIME: u16 = 48; //in blocks
pub const REFUND_LOCKTIME_STEP: u16 = 48; //in blocks

//first connect means the first time you're ever connecting, without having gotten any txes
// confirmed yet, so the taker will not be very persistent since there should be plenty of other
Expand Down
9 changes: 7 additions & 2 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//TODO this goes in the config file
pub const NETWORK: Network = Network::Regtest;
//other options: Network::Signet, Network::Bitcoin, Network::Testnet
//Signet is probably the best for this early stage of the project if
// you want to coinswap with other people
//Mainnet/Bitcoin only for the brave

// this file contains code handling the wallet and sync'ing the wallet
// for now the wallet is only sync'd via bitcoin core's RPC
// makers will only ever sync this way, but one day takers may sync in other
Expand Down Expand Up @@ -57,8 +64,6 @@ use crate::messages::Preimage;
//data in the bitcoin core wallet
//for example which privkey corresponds to a scriptpubkey is stored in hd paths

//TODO this goes in the config file
pub const NETWORK: Network = Network::Regtest; //not configurable for now
const DERIVATION_PATH: &str = "m/84'/1'/0'";
const WALLET_FILE_VERSION: u32 = 0;

Expand Down
5 changes: 3 additions & 2 deletions src/watchtower_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const WATCHTOWER_HOSTPORT: &str = "localhost:6103";

use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;

Expand Down Expand Up @@ -47,8 +49,7 @@ async fn send_message_to_watchtowers(message: &MakerToWatchtowerMessage) -> Resu
//TODO add support for registering with multiple watchtowers concurrently
//TODO add timeouts to deal with indefinite hangs

let host = "localhost:6103";
let mut socket = TcpStream::connect(host).await?;
let mut socket = TcpStream::connect(WATCHTOWER_HOSTPORT).await?;

let (socket_reader, mut socket_writer) = socket.split();
let mut socket_reader = BufReader::new(socket_reader);
Expand Down

0 comments on commit 77961a7

Please sign in to comment.