Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #78 from comit-network/some-cleanup
Browse files Browse the repository at this point in the history
A little polish before release of version 0.4.0
  • Loading branch information
thomaseizinger authored Sep 14, 2020
2 parents 32d2465 + 72c2cdc commit 0ca72c3
Show file tree
Hide file tree
Showing 56 changed files with 410 additions and 755 deletions.
53 changes: 2 additions & 51 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,51 +1,2 @@
[package]
name = "blockchain_contracts"
version = "0.3.2"
authors = ["CoBloX developers <[email protected]>"]
edition = "2018"
description = "Blockchain contracts used by COMIT-network daemons to execute cryptographic protocols."
homepage = "https://comit.network/"
repository = "https://github.com/comit-network/blockchain-contracts"
keywords = ["atomic-swaps", "blockchain", "cryptocurrencies", "comit", "htlc"]
categories = ["cryptography::cryptocurrencies"]
readme = "./README.md"
license-file = "./LICENSE.md"

[dependencies]
byteorder = "1.3"
hex = "0.4"
hex-literal = "0.2"
itertools = "0.8.0"
regex = "1.3"
rust_bitcoin = { version = "0.23", package = "bitcoin", features = ["use-serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tiny-keccak = "2.0"

[dev-dependencies]
failure = "0.1"
lazy_static = "1.4"
log = "0.4"
pretty_env_logger = "0.4"
reqwest = { version = "0.10", features = ["json", "blocking"] }
rust-crypto = "0.2"
spectral = "0.6"
testcontainers = "0.9"
tiny-keccak = { version = "2.0", features = ["keccak"] }
web3 = { version = "0.10", default-features = false, features = ["http"] }

# These versions need to be changed together with web3, depends on what version of primitive-types ships with web3
[dev-dependencies.primitive-types]
features = ["rlp"]
version = "0.5.0"
[dev-dependencies.rlp]
version = "0.4.2"

# This dependency version is set by rust-bitcoin but we need the "recovery" feature on
[dev-dependencies.secp256k1]
features = ["recovery"]
version = "0.17.1"


[build-dependencies]
regex = "1.3"
[workspace]
members = ["lib", "print_offsets"]
47 changes: 47 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "blockchain_contracts"
version = "0.4.0"
authors = ["CoBloX developers <[email protected]>"]
edition = "2018"
description = "Blockchain contracts used by COMIT-network daemons to execute cryptographic protocols."
homepage = "https://comit.network/"
repository = "https://github.com/comit-network/blockchain-contracts"
keywords = ["atomic-swaps", "blockchain", "cryptocurrencies", "comit", "htlc"]
categories = ["cryptography::cryptocurrencies"]
readme = "../README.md"
license-file = "../LICENSE.md"

[dependencies]
byteorder = "1"
hex-literal = "0.2"
regex = "1"
rust_bitcoin = { version = "0.25", package = "bitcoin" }

[dev-dependencies]
failure = "0.1"
lazy_static = "1"
log = "0.4"
hex = "0.4"
pretty_env_logger = "0.4"
reqwest = { version = "0.10", features = ["json", "blocking"] }
rust-crypto = "0.2"
spectral = "0.6"
testcontainers = "0.9"
tiny-keccak = { version = "2", features = ["keccak"] }
web3 = { version = "0.10", default-features = false, features = ["http"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
rust_bitcoin = { version = "0.25", package = "bitcoin", features = ["use-serde"] }

# These versions need to be changed together with web3, depends on what version of primitive-types ships with web3
[dev-dependencies.primitive-types]
features = ["rlp"]
version = "0.5.0"
[dev-dependencies.rlp]
version = "0.4.2"

# This dependency version is set by rust-bitcoin but we need the "recovery" feature on
[dev-dependencies.secp256k1]
features = ["recovery"]
version = "0.19"
22 changes: 5 additions & 17 deletions src/bitcoin/bitcoin_htlc.rs → lib/src/bitcoin/hbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,13 @@ use rust_bitcoin::{
// contract template RFC: https://github.com/comit-network/RFCs/blob/master/RFC-005-SWAP-Basic-Bitcoin.adoc#contract
pub const CONTRACT_TEMPLATE: [u8;97] = hex!("6382012088a82010000000000000000000000000000000000000000000000000000000000000018876a9143000000000000000000000000000000000000003670420000002b17576a91440000000000000000000000000000000000000046888ac");

#[derive(Clone, Copy, Debug)]
pub enum UnlockingError {
WrongSecret {
got: SecretHash,
expected: SecretHash,
},
WrongPubkeyHash {
got: [u8; 20],
expected: [u8; 20],
},
}

#[derive(Debug)]
pub struct BitcoinHtlc {
pub struct Htlc {
script: Vec<u8>,
expiry: u32,
}

impl BitcoinHtlc {
impl Htlc {
pub fn new(
expiry: u32,
refund_identity: hash160::Hash,
Expand All @@ -45,7 +33,7 @@ impl BitcoinHtlc {
BitcoinTimestamp(expiry).fit_into_placeholder_slice(&mut contract[65..69]);
refund_identity.fit_into_placeholder_slice(&mut contract[74..94]);

BitcoinHtlc {
Htlc {
script: contract,
expiry,
}
Expand Down Expand Up @@ -115,7 +103,7 @@ mod tests {

#[test]
fn compiled_contract_is_same_length_as_template() {
let htlc = BitcoinHtlc::new(
let htlc = Htlc::new(
3_000_000,
hash160::Hash::default(),
hash160::Hash::default(),
Expand All @@ -131,7 +119,7 @@ mod tests {

#[test]
fn given_input_data_when_compiled_should_contain_given_data() {
let htlc = BitcoinHtlc::new(
let htlc = Htlc::new(
2_000_000_000,
hash160::Hash::default(),
hash160::Hash::default(),
Expand Down
2 changes: 2 additions & 0 deletions lib/src/bitcoin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod hbit;
pub mod witness;
2 changes: 0 additions & 2 deletions src/bitcoin/witness/mod.rs → lib/src/bitcoin/witness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ pub const SEQUENCE_DISALLOW_NTIMELOCK_NO_RBF: u32 = 0xFFFF_FFFF;

mod p2wpkh;
mod primed_transaction;
mod pubkey_hash;

pub use p2wpkh::UnlockP2wpkh;
pub use primed_transaction::{Error, PrimedInput, PrimedTransaction};
pub use pubkey_hash::PubkeyHash;

use rust_bitcoin::{
secp256k1::{PublicKey, SecretKey},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::bitcoin::witness::{PubkeyHash, UnlockParameters, Witness};
use crate::bitcoin::witness::{UnlockParameters, Witness};
use rust_bitcoin::{
hashes::hash160,
hashes::Hash,
secp256k1::{self, PublicKey, SecretKey},
Script,
};
Expand All @@ -12,9 +13,7 @@ use rust_bitcoin::{
/// 19 76 a9 14 <public_key_hash> 88 ac
/// in the unlocking script. See BIP 143.
/// This function simply returns the latter as a Script.
fn generate_prev_script(public_key_hash: PubkeyHash) -> Script {
let public_key_hash: hash160::Hash = public_key_hash.into();

fn generate_prev_script(public_key_hash: hash160::Hash) -> Script {
let mut prev_script = vec![0x76, 0xa9, 0x14];

prev_script.append(&mut public_key_hash[..].to_vec());
Expand All @@ -41,7 +40,7 @@ impl UnlockP2wpkh for SecretKey {
witness: vec![Witness::Signature(self), Witness::PublicKey(public_key)],
sequence: super::SEQUENCE_ALLOW_NTIMELOCK_NO_RBF,
locktime: 0,
prev_script: generate_prev_script(public_key.into()),
prev_script: generate_prev_script(hash160::Hash::hash(&public_key.serialize())),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@ use crate::bitcoin::witness::{UnlockParameters, Witness};
use rust_bitcoin::{
hashes::Hash,
secp256k1::{self, Message, Secp256k1},
util::bip143::SighashComponents,
util::bip143::SigHashCache,
Address, Amount, OutPoint, Script, SigHashType, Transaction, TxIn, TxOut,
};
use std::fmt;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Error {
OverflowingFee,
FeeHigherThanInputValue,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::OverflowingFee => write!(f, "provided fee causes overflow"),
Error::FeeHigherThanInputValue => write!(f, "fee is higher than input value"),
}
}
}

impl std::error::Error for Error {}

#[derive(Clone, Debug, PartialEq)]
pub struct PrimedInput {
input_parameters: UnlockParameters,
Expand Down Expand Up @@ -78,11 +90,12 @@ impl PrimedTransaction {
let input_parameters = primed_input.input_parameters;
for (j, witness) in input_parameters.witness.iter().enumerate() {
if let Witness::Signature(secret_key) = witness {
let sighash_components = SighashComponents::new(transaction);
let hash_to_sign = sighash_components.sighash_all(
&transaction.input[i],
let mut sighash_cache = SigHashCache::new(transaction as &Transaction);
let hash_to_sign = sighash_cache.signature_hash(
i,
&input_parameters.prev_script,
primed_input.value.as_sat(),
SigHashType::All,
);
// `from` should be used instead of `from_slice` once `ThirtyTwoByteHash` is
// implemented for Hashes See https://github.com/rust-bitcoin/rust-secp256k1/issues/106
Expand Down
16 changes: 8 additions & 8 deletions src/ethereum/erc20_htlc.rs → lib/src/ethereum/herc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use hex_literal::hex;
pub const CONTRACT_TEMPLATE: [u8;411] = hex!("61018c61000f60003961018c6000f3361561007957602036141561004f57602060006000376020602160206000600060026048f17f100000000000000000000000000000000000000000000000000000000000000160215114166100ae575b7f696e76616c69645365637265740000000000000000000000000000000000000060005260206000fd5b426320000002106100f1577f746f6f4561726c7900000000000000000000000000000000000000000000000060005260206000fd5b7f72656465656d656400000000000000000000000000000000000000000000000060206000a1733000000000000000000000000000000000000003602052610134565b7f726566756e64656400000000000000000000000000000000000000000000000060006000a1734000000000000000000000000000000000000004602052610134565b63a9059cbb6000527f5000000000000000000000000000000000000000000000000000000000000005604052602060606044601c6000736000000000000000000000000000000000000006620186a05a03f150602051ff");

#[derive(Debug, Clone)]
pub struct Erc20Htlc(Vec<u8>);
pub struct Htlc(Vec<u8>);

impl From<Erc20Htlc> for Vec<u8> {
fn from(htlc: Erc20Htlc) -> Self {
impl From<Htlc> for Vec<u8> {
fn from(htlc: Htlc) -> Self {
htlc.0
}
}

impl Erc20Htlc {
impl Htlc {
pub fn new(
expiry: u32,
refund_identity: Address,
Expand All @@ -31,7 +31,7 @@ impl Erc20Htlc {
token_quantity.fit_into_placeholder_slice(&mut contract[333..365]);
token_contract_address.fit_into_placeholder_slice(&mut contract[379..399]);

Erc20Htlc(contract)
Htlc(contract)
}

pub fn deploy_tx_gas_limit() -> u64 {
Expand Down Expand Up @@ -89,7 +89,7 @@ mod tests {

#[test]
fn compiled_contract_is_same_length_as_template() {
let htlc = Erc20Htlc::new(
let htlc = Htlc::new(
3_000_000,
Address([0u8; 20]),
Address([0u8; 20]),
Expand All @@ -107,7 +107,7 @@ mod tests {

#[test]
fn given_input_data_when_compiled_should_contain_given_data() {
let htlc = Erc20Htlc::new(
let htlc = Htlc::new(
2_000_000_000,
Address([0u8; 20]),
Address([0u8; 20]),
Expand Down Expand Up @@ -145,7 +145,7 @@ mod tests {
.unwrap();
let expiry = 1_552_263_040;

let htlc = Erc20Htlc::new(
let htlc = Htlc::new(
expiry,
Address(refund_identity),
Address(redeem_identity),
Expand Down
16 changes: 8 additions & 8 deletions src/ethereum/ether_htlc.rs → lib/src/ethereum/heth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use hex_literal::hex;
pub const CONTRACT_TEMPLATE: [u8;311] = hex!("61012861000f6000396101286000f3361561007957602036141561004f57602060006000376020602160206000600060026048f17f100000000000000000000000000000000000000000000000000000000000000160215114166100ae575b7f696e76616c69645365637265740000000000000000000000000000000000000060005260206000fd5b426320000002106100eb577f746f6f4561726c7900000000000000000000000000000000000000000000000060005260206000fd5b7f72656465656d656400000000000000000000000000000000000000000000000060206000a1733000000000000000000000000000000000000003ff5b7f726566756e64656400000000000000000000000000000000000000000000000060006000a1734000000000000000000000000000000000000004ff");

#[derive(Debug)]
pub struct EtherHtlc(Vec<u8>);
pub struct Htlc(Vec<u8>);

impl From<EtherHtlc> for Vec<u8> {
fn from(htlc: EtherHtlc) -> Self {
impl From<Htlc> for Vec<u8> {
fn from(htlc: Htlc) -> Self {
htlc.0
}
}

impl EtherHtlc {
impl Htlc {
pub fn new(
expiry: u32,
refund_identity: Address,
Expand All @@ -27,7 +27,7 @@ impl EtherHtlc {
redeem_identity.fit_into_placeholder_slice(&mut contract[229..249]);
refund_identity.fit_into_placeholder_slice(&mut contract[290..310]);

EtherHtlc(contract)
Htlc(contract)
}

pub fn deploy_tx_gas_limit() -> u64 {
Expand Down Expand Up @@ -64,7 +64,7 @@ mod tests {

#[test]
fn compiled_contract_is_same_length_as_template() {
let htlc = EtherHtlc::new(
let htlc = Htlc::new(
3_000_000,
Address([0u8; 20]),
Address([0u8; 20]),
Expand All @@ -80,7 +80,7 @@ mod tests {

#[test]
fn given_input_data_when_compiled_should_contain_given_data() {
let htlc = EtherHtlc::new(
let htlc = Htlc::new(
2_000_000_000,
Address([0u8; 20]),
Address([0u8; 20]),
Expand Down Expand Up @@ -110,7 +110,7 @@ mod tests {
.unwrap();
let expiry = 1_552_263_040;

let htlc = EtherHtlc::new(
let htlc = Htlc::new(
expiry,
Address(refund_identity),
Address(redeem_identity),
Expand Down
7 changes: 2 additions & 5 deletions src/ethereum/mod.rs → lib/src/ethereum/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod erc20_htlc;
pub mod ether_htlc;

pub use erc20_htlc::*;
pub use ether_htlc::*;
pub mod herc20;
pub mod heth;

/// The log message emitted when the HTLC is redeemed.
///
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0ca72c3

Please sign in to comment.