Skip to content

Commit

Permalink
Add secp256k1 (paritytech#77)
Browse files Browse the repository at this point in the history
* Add script/builder

* Impl select_utxo function

* Add parse_redeem_script

* Impl handle_proposal function

* Fix ProposalTx store

* Refactor keys module

* Refractor script module

* Support btc bridge in no_std env

* Add tx signer function

* Format code
  • Loading branch information
gguoss committed Nov 5, 2018
1 parent 5093527 commit 608b45f
Show file tree
Hide file tree
Showing 19 changed files with 1,032 additions and 2,972 deletions.
119 changes: 96 additions & 23 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ slog = "^2"
log = "0.3"

# bitcoin-rust
primitives = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050" }
chain = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050" }
primitives = { git = "https://github.com/chainx-org/bitcoin-rust" }
chain = { git = "https://github.com/chainx-org/bitcoin-rust" }

[workspace]
members = [
Expand Down
12 changes: 8 additions & 4 deletions cxrml/bridge/btc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ srml-consensus = { git = "https://github.com/paritytech/substrate", default-feat
srml-timestamp = { git = "https://github.com/paritytech/substrate", default-features = false }

# bitcoin-rust
chain = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050", default-features = false }
primitives = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050", default-features = false }
serialization = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050", default-features = false }
bitcrypto = { git = "https://github.com/chainx-org/bitcoin-rust", rev = "8b2dab29c12d19bcd75f268deb01ff19cccd7050", default-features = false }
chain = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
primitives = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
serialization = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
bitcrypto = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
script = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
keys = { git = "https://github.com/chainx-org/bitcoin-rust", default-features = false }
merkle = { git = "https://github.com/chainx-org/merkle.git", default-features = false }
bit-vec = { git = "https://github.com/chainx-org/bit-vec.git", default-features = false }
rustc-hex = { version = "2", optional = true }
Expand Down Expand Up @@ -52,9 +54,11 @@ std=[

# bitcoin-rust
"chain/std",
"keys/std",
"primitives/std",
"serialization/std",
"bitcrypto/std",
"script/std",
"merkle/std",
"bit-vec/std",
]
46 changes: 38 additions & 8 deletions cxrml/bridge/btc/src/b58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ static BASE58_DIGITS: [Option<u8>; 128] = [

pub fn from(data: Vec<u8>) -> Result<Vec<u8>, &'static str> {
// 11/15 is just over log_256(58)
let mut scratch = vec![0u8; 1 + data.len() * 11 / 15];
let mut scratch = Vec::new();
for _i in 0..1 + data.len() * 11 / 15 {
scratch.push(0);
}
// Build in base 256
for d58 in data.clone() {
// Compute "X = X * 58 + next_digit" in base 256
Expand All @@ -34,7 +37,9 @@ pub fn from(data: Vec<u8>) -> Result<Vec<u8>, &'static str> {
}
let mut carry = match BASE58_DIGITS[d58 as usize] {
Some(d58) => d58 as u32,
None => { return Err("BadByte"); }
None => {
return Err("BadByte");
}
};
for d256 in scratch.iter_mut().rev() {
carry += *d256 as u32 * 58;
Expand All @@ -45,9 +50,10 @@ pub fn from(data: Vec<u8>) -> Result<Vec<u8>, &'static str> {
}

// Copy leading zeroes directly
let mut ret: Vec<u8> = data.iter().take_while(|&x| *x == BASE58_CHARS[0])
.map(|_| 0)
.collect();
let mut ret: Vec<u8> = data.iter()
.take_while(|&x| *x == BASE58_CHARS[0])
.map(|_| 0)
.collect();
// Copy rest of string
ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
Ok(ret)
Expand All @@ -59,9 +65,33 @@ mod tests {
#[test]
fn test_from() {
let s = String::from("mjKE11gjVN4JaC9U8qL6ZB5vuEBgmwik7b");
let v = &[111, 41, 168, 159, 89, 51, 97, 179, 153, 104, 9, 74,
184, 193, 251, 6, 131, 166, 121, 3, 1, 241, 112, 101, 146];
let v = &[
111,
41,
168,
159,
89,
51,
97,
179,
153,
104,
9,
74,
184,
193,
251,
6,
131,
166,
121,
3,
1,
241,
112,
101,
146,
];
assert_eq!(from(s.as_bytes().to_vec()).unwrap(), v);
}
}

45 changes: 27 additions & 18 deletions cxrml/bridge/btc/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use primitives::hash::H256;
use chain::BlockHeader;

use super::{Trait,
// OrphanIndexFor,
// OrphanIndexFor,
BlockHeaderFor,
BestIndex,
HeaderNumberFor, HashForNumber,
ParamsInfo, Params};
HeaderNumberFor,
HashForNumber,
ParamsInfo,
Params};

use tx::{TxStorage, RollBack};

Expand Down Expand Up @@ -63,9 +65,7 @@ pub struct SideChainOrigin {
#[derive(Clone)]
pub enum BlockOrigin {
KnownBlock,
CanonChain {
block_number: u32,
},
CanonChain { block_number: u32 },
SideChain(SideChainOrigin),
SideChainBecomingCanonChain(SideChainOrigin),
}
Expand Down Expand Up @@ -105,7 +105,7 @@ impl ChainErr {
}
}

pub struct Chain<T: Trait> (PhantomData<T>);
pub struct Chain<T: Trait>(PhantomData<T>);

impl<T: Trait> Chain<T> {
pub fn insert_best_header(header: BlockHeader) -> Result<(), ChainErr> {
Expand All @@ -126,9 +126,7 @@ impl<T: Trait> Chain<T> {
Ok(())
}
// case 3: block has been added to the side branch without reorganization to this branch
BlockOrigin::SideChain(_origin) => {
Ok(())
}
BlockOrigin::SideChain(_origin) => Ok(()),
}
}

Expand All @@ -138,17 +136,24 @@ impl<T: Trait> Chain<T> {
let best_bumber = best_index.number;

//todo change unwrap
let (best_header, _): (BlockHeader, T::AccountId) = <BlockHeaderFor<T>>::get(&best_hash).unwrap();
let (best_header, _): (BlockHeader, T::AccountId) = <BlockHeaderFor<T>>::get(&best_hash)
.unwrap();
let new_best_header = BestHeader {
hash: best_header.previous_header_hash.clone(),
number: if best_bumber > 0 { best_bumber - 1 } else {
if best_header.previous_header_hash != Default::default() { return Err(ChainErr::DecanonizeMustZero); }
number: if best_bumber > 0 {
best_bumber - 1
} else {
if best_header.previous_header_hash != Default::default() {
return Err(ChainErr::DecanonizeMustZero);
}
0
},
};

// remove related tx
TxStorage::<T>::rollback_tx(&best_hash).map_err(|s| ChainErr::OtherErr(s))?;
TxStorage::<T>::rollback_tx(&best_hash).map_err(|s| {
ChainErr::OtherErr(s)
})?;

<HeaderNumberFor<T>>::remove(&best_hash);
// do not need to remove HashForNumber
Expand All @@ -172,7 +177,9 @@ impl<T: Trait> Chain<T> {
let new_best_header = BestHeader {
hash: hash.clone(),
number: if header.previous_header_hash == Default::default() {
if best_number != 0 { return Err(ChainErr::CanonizeMustZero); }
if best_number != 0 {
return Err(ChainErr::CanonizeMustZero);
}
0
} else {
best_number + 1
Expand Down Expand Up @@ -212,14 +219,15 @@ impl<T: Trait> Chain<T> {
fn block_origin(header: &BlockHeader) -> Result<BlockOrigin, ChainErr> {
let best_index: BestHeader = <BestIndex<T>>::get();
// TODO change unwrap
let (best_header, _): (BlockHeader, T::AccountId) = <BlockHeaderFor<T>>::get(&best_index.hash).unwrap();
let (best_header, _): (BlockHeader, T::AccountId) =
<BlockHeaderFor<T>>::get(&best_index.hash).unwrap();
if <HeaderNumberFor<T>>::exists(header.hash()) {
return Ok(BlockOrigin::KnownBlock);
}

if best_header.hash() == header.previous_header_hash {
return Ok(BlockOrigin::CanonChain {
block_number: best_index.number + 1
block_number: best_index.number + 1,
});
}

Expand Down Expand Up @@ -248,7 +256,8 @@ impl<T: Trait> Chain<T> {
let origin = SideChainOrigin {
ancestor: number,
canonized_route: sidechain_route.into_iter().rev().collect(),
decanonized_route: (number + 1..best_index.number + 1).into_iter()
decanonized_route: (number + 1..best_index.number + 1)
.into_iter()
.filter_map(|decanonized_bn| {
let hash = <HashForNumber<T>>::get(decanonized_bn);
if hash == Default::default() {
Expand Down
Loading

0 comments on commit 608b45f

Please sign in to comment.