Skip to content

Commit

Permalink
Stage/bridge btc (paritytech#75)
Browse files Browse the repository at this point in the history
* Add b58

* Fix b58 build

* Change 'TxSet' interface parameters order

* Add b58 test

* Add Tx proposal

* Add extract_multi_scriptsig function & multi script

* Support withdraw return
  • Loading branch information
gguoss committed Nov 5, 2018
1 parent dc17385 commit 5093527
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 244 deletions.
67 changes: 67 additions & 0 deletions cxrml/bridge/btc/src/b58.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 Chainpool

use rstd::prelude::Vec;

static BASE58_CHARS: &'static [u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

static BASE58_DIGITS: [Option<u8>; 128] = [
None, None, None, None, None, None, None, None, // 0-7
None, None, None, None, None, None, None, None, // 8-15
None, None, None, None, None, None, None, None, // 16-23
None, None, None, None, None, None, None, None, // 24-31
None, None, None, None, None, None, None, None, // 32-39
None, None, None, None, None, None, None, None, // 40-47
None, Some(0), Some(1), Some(2), Some(3), Some(4), Some(5), Some(6), // 48-55
Some(7), Some(8), None, None, None, None, None, None, // 56-63
None, Some(9), Some(10), Some(11), Some(12), Some(13), Some(14), Some(15), // 64-71
Some(16), None, Some(17), Some(18), Some(19), Some(20), Some(21), None, // 72-79
Some(22), Some(23), Some(24), Some(25), Some(26), Some(27), Some(28), Some(29), // 80-87
Some(30), Some(31), Some(32), None, None, None, None, None, // 88-95
None, Some(33), Some(34), Some(35), Some(36), Some(37), Some(38), Some(39), // 96-103
Some(40), Some(41), Some(42), Some(43), None, Some(44), Some(45), Some(46), // 104-111
Some(47), Some(48), Some(49), Some(50), Some(51), Some(52), Some(53), Some(54), // 112-119
Some(55), Some(56), Some(57), None, None, None, None, None, // 120-127
];

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];
// Build in base 256
for d58 in data.clone() {
// Compute "X = X * 58 + next_digit" in base 256
if d58 as usize > BASE58_DIGITS.len() {
return Err("BadByte");
}
let mut carry = match BASE58_DIGITS[d58 as usize] {
Some(d58) => d58 as u32,
None => { return Err("BadByte"); }
};
for d256 in scratch.iter_mut().rev() {
carry += *d256 as u32 * 58;
*d256 = carry as u8;
carry /= 256;
}
assert_eq!(carry, 0);
}

// Copy leading zeroes directly
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)
}

#[cfg(test)]
mod tests {
use super::from;
#[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];
assert_eq!(from(s.as_bytes().to_vec()).unwrap(), v);
}
}

13 changes: 13 additions & 0 deletions cxrml/bridge/btc/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,16 @@ impl From<H520> for CompactSignature {
CompactSignature(h)
}
}

#[cfg(test)]
mod tests {
use super::Address;
use super::DisplayLayout;

#[test]
fn test_layout() {
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];
Address::from_layout(v).unwrap();
}
}
28 changes: 25 additions & 3 deletions cxrml/bridge/btc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ mod blockchain;
mod tx;
mod keys;
mod script;
mod b58;

use codec::Decode;
use rstd::prelude::*;
//use rstd::result::Result as StdResult;
use runtime_support::dispatch::Result;
use runtime_support::dispatch::{Result, Parameter};
use runtime_support::{StorageValue, StorageMap};
use runtime_primitives::traits::OnFinalise;

use system::ensure_signed;

use ser::deserialize;
use chain::{BlockHeader, Transaction};
use chain::{BlockHeader, Transaction as BTCTransaction};
use primitives::{hash::H256, compact::Compact};
use primitives::hash;

Expand Down Expand Up @@ -92,6 +93,7 @@ decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn push_header(origin, header: Vec<u8>) -> Result;
fn push_transaction(origin, tx: Vec<u8>) -> Result;
fn propose_transaction(origin, tx: Vec<u8>) -> Result;
}
}

Expand Down Expand Up @@ -153,6 +155,13 @@ pub enum TxType {
RegisterDeposit,
}

#[derive(PartialEq, Clone, Encode, Decode)]
pub struct Proposal<AccountId: Parameter + Ord + Default > {
proposer: Vec<AccountId>,
tx: BTCTransaction,
perfection: bool,
}

impl Default for TxType {
fn default() -> Self { TxType::Deposit }
}
Expand Down Expand Up @@ -182,9 +191,10 @@ decl_storage! {

pub UTXOSet get(utxo_set): map u64 => UTXO;
pub UTXOMaxIndex get(utxo_max_index) config(): u64;
pub TxSet get(tx_set): map H256 => Option<(Transaction, T::AccountId, keys::Address, TxType, u64)>; // Address, type, balance
pub TxSet get(tx_set): map H256 => Option<(T::AccountId, keys::Address, TxType, u64, BTCTransaction)>; // Address, type, balance
pub BlockTxids get(block_txids): map H256 => Vec<H256>;
pub AddressMap get(address_map): map keys::Address => T::AccountId;
pub TxProposal get(tx_proposal): map H256 => Option<Proposal<T::AccountId>>;
// pub AccountMap get(account_map): map T::AccountId => keys::Address;

// =====
Expand Down Expand Up @@ -236,6 +246,14 @@ impl<T: Trait> Module<T> {
Self::process_tx(tx, &from)?;
Ok(())
}

pub fn propose_transaction(origin: T::Origin, tx: Vec<u8>) -> Result {
let from = ensure_signed(origin)?;

let tx: BTCTransaction = Decode::decode(&mut tx.as_slice()).ok_or("parse transaction err")?;
Self::process_btc_tx(tx, &from)?;
Ok(())
}
}


Expand Down Expand Up @@ -283,4 +301,8 @@ impl<T: Trait> Module<T> {

Ok(())
}

pub fn process_btc_tx(tx: BTCTransaction, who: &T::AccountId) -> Result {
Ok(())
}
}
Loading

0 comments on commit 5093527

Please sign in to comment.