1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
// Copyright 2018 The Exonum Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! BTC transaction utils is a small library that will help to create multisig addresses //! and sign a some types of segwit transactions. //! //! By using this library you can make the following things: //! //! - [Create][redeem-script] a redeem script and a corresponding multisig address (3 of 4). //! - [Sign][p2wpk] the `P2WPK` inputs. //! - [Sign][p2wsh] the `P2WSH` inputs. //! //! # Examples //! //! ## Create a redeem script and a corresponding multisig address (3 of 4). //! //! ``` //! extern crate bitcoin; //! extern crate btc_transaction_utils; //! //! use bitcoin::network::constants::Network; //! use btc_transaction_utils::multisig::RedeemScriptBuilder; //! use btc_transaction_utils::test_data::secp_gen_keypair; //! use btc_transaction_utils::p2wsh; //! //! fn main() { //! // Generate four key pairs. //! let keypairs = (0..4) //! .map(|_| secp_gen_keypair()) //! .collect::<Vec<_>>(); //! // Create a corresponding redeem script. //! let public_keys = keypairs.iter().map(|keypair| keypair.0); //! let script = RedeemScriptBuilder::with_public_keys(public_keys) //! .quorum(3) //! .to_script() //! .unwrap(); //! // Create a corresponding testnet address for the given redeem script. //! let address = p2wsh::address(&script, Network::Testnet); //! println!("{}", address.to_string()); //! } //! ``` //! //! ## Sign P2WPK input //! //! ``` //! extern crate bitcoin; //! extern crate btc_transaction_utils; //! extern crate rand; //! //! use bitcoin::blockdata::opcodes::All; //! use bitcoin::blockdata::script::{Builder, Script}; //! use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut}; //! use bitcoin::network::constants::Network; //! use btc_transaction_utils::p2wpk; //! use btc_transaction_utils::test_data::{secp_gen_keypair_with_rng, btc_tx_from_hex}; //! use btc_transaction_utils::TxInRef; //! use rand::{SeedableRng, StdRng}; //! //! fn main() { //! // Take a transaction with the unspent P2WPK output. //! let prev_tx = btc_tx_from_hex( //! "02000000000101beccab33bc72bfc81b63fdec8a4a9a4719e4418bdb7b20e47b0\ //! 2074dc42f2d800000000017160014f3b1b3819c1290cd5d675c1319dc7d9d98d5\ //! 71bcfeffffff02dceffa0200000000160014368c6b7c38f0ff0839bf78d77544d\ //! a96cb685bf28096980000000000160014284175e336fa10865fb4d1351c9e18e7\ //! 30f5d6f90247304402207c893c85d75e2230dde04f5a1e2c83c4f0b7d93213372\ //! 746eb2227b068260d840220705484b6ec70a8fc0d1f80c3a98079602595351b7a\ //! 9bca7caddb9a6adb0a3440012103150514f05f3e3f40c7b404b16f8a09c2c71ba\ //! d3ba8da5dd1e411a7069cc080a004b91300", //! ); //! // Take the corresponding key pair. //! let mut rng: StdRng = SeedableRng::from_seed([1, 2, 3, 4].as_ref()); //! let keypair = secp_gen_keypair_with_rng(&mut rng); //! // Create an unsigned transaction //! let mut transaction = Transaction { //! version: 2, //! lock_time: 0, //! input: vec![ //! TxIn { //! prev_hash: prev_tx.txid(), //! prev_index: 1, //! script_sig: Script::default(), //! sequence: 0xFFFFFFFF, //! witness: Vec::default(), //! }, //! ], //! output: vec![ //! TxOut { //! value: 0, //! script_pubkey: Builder::new() //! .push_opcode(All::OP_RETURN) //! .push_slice(b"Hello Exonum!") //! .into_script(), //! }, //! ], //! }; //! // Create a signature for the given input. //! let mut signer = p2wpk::InputSigner::new(keypair.0, Network::Testnet); //! let signature = signer //! .sign_input(TxInRef::new(&transaction, 0), &prev_tx, &keypair.1) //! .unwrap(); //! // Finalize the transaction. //! signer.spend_input(&mut transaction.input[0], signature); //! } //! ``` //! //! ## Sign P2WSH input //! //! ``` //! extern crate bitcoin; //! extern crate btc_transaction_utils; //! extern crate rand; //! //! use bitcoin::blockdata::opcodes::All; //! use bitcoin::blockdata::script::{Builder, Script}; //! use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut}; //! use bitcoin::network::constants::Network; //! use btc_transaction_utils::multisig::RedeemScriptBuilder; //! use btc_transaction_utils::p2wsh; //! use btc_transaction_utils::test_data::{secp_gen_keypair_with_rng, btc_tx_from_hex}; //! use btc_transaction_utils::TxInRef; //! use rand::{SeedableRng, StdRng}; //! //! fn main() { //! // Take a transaction with the unspent P2WSH output. //! let prev_tx = btc_tx_from_hex( //! "02000000000101f8c16000cc59f9505046303944d42a6c264a322f80b46bb4361\ //! 15b6e306ba9950000000000feffffff02f07dc81600000000160014f65eb9d72a\ //! 8475dd8e26f4fa748796e211aa8869102700000000000022002001fb25c3db04c\ //! a5580da43a7d38dd994650d9aa6d6ee075b4578388deed338ed0247304402206b\ //! 5f211cd7f9b89e80c734b61113c33f437ba153e7ba6bc275eed857e54fcb26022\ //! 0038562e88b805f0cdfd4873ab3579d52268babe6af9c49086c00343187cdf28a\ //! 012103979dff5cd9045f4b6fa454d2bc5357586a85d4789123df45f83522963d9\ //! 4e3217fb91300", //! ); //! // Take the corresponding key pairs and the redeem script. //! let total_count = 18; //! let quorum = 12; //! //! let mut rng: StdRng = SeedableRng::from_seed([1, 2, 3, 4].as_ref()); //! let keypairs = (0..total_count) //! .into_iter() //! .map(|_| secp_gen_keypair_with_rng(&mut rng)) //! .collect::<Vec<_>>(); //! let public_keys = keypairs.iter().map(|keypair| keypair.0); //! let redeem_script = RedeemScriptBuilder::with_public_keys(public_keys) //! .quorum(quorum) //! .to_script() //! .unwrap(); //! // Create an unsigned transaction. //! let mut transaction = Transaction { //! version: 2, //! lock_time: 0, //! input: vec![ //! TxIn { //! prev_hash: prev_tx.txid(), //! prev_index: 1, //! script_sig: Script::default(), //! sequence: 0xFFFFFFFF, //! witness: Vec::default(), //! }, //! ], //! output: vec![ //! TxOut { //! value: 0, //! script_pubkey: Builder::new() //! .push_opcode(All::OP_RETURN) //! .push_slice(b"Hello Exonum with multisig!") //! .into_script(), //! }, //! ], //! }; //! // Create signatures for the given input. //! let mut signer = p2wsh::InputSigner::new(redeem_script.clone()); //! let signatures = keypairs[0..quorum] //! .iter() //! .map(|keypair| { //! let txin = TxInRef::new(&transaction, 0); //! signer.sign_input(txin, &prev_tx, &keypair.1).unwrap() //! }) //! .collect::<Vec<_>>(); //! // Finalize the transaction. //! signer.spend_input(&mut transaction.input[0], signatures); //! } //! ``` //! //! [redeem-script]: #create-a-redeem-script-and-a-corresponding-multisig-address-3-of-4 //! [p2wpk]: #sign-p2wpk-input //! [p2wsh]: #sign-p2wsh-input #![deny(missing_docs, missing_debug_implementations)] extern crate bitcoin; #[macro_use] extern crate display_derive; extern crate failure; #[macro_use] extern crate failure_derive; extern crate hex; #[cfg(test)] #[macro_use] extern crate pretty_assertions; extern crate rand; extern crate secp256k1; extern crate serde; extern crate serde_str; #[macro_use] mod macros; pub mod multisig; pub mod p2wpk; pub mod p2wsh; mod sign; pub mod test_data; use bitcoin::blockdata::transaction::{Transaction, TxIn, TxOut}; pub use sign::{InputSignature, InputSignatureRef}; /// A borrowed reference to a transaction input. #[derive(Debug, Copy, Clone)] pub struct TxInRef<'a> { transaction: &'a Transaction, index: usize, } impl<'a> TxInRef<'a> { /// Constructs a reference to the input with the given index of the given transaction. pub fn new(transaction: &'a Transaction, index: usize) -> TxInRef<'a> { assert!(transaction.input.len() > index); TxInRef { transaction, index } } /// Returns a reference to the borrowed transaction. pub fn transaction(&self) -> &Transaction { self.transaction } /// Returns a reference to the input. pub fn input(&self) -> &TxIn { &self.transaction.input[self.index] } /// Returns the index of input. pub fn index(&self) -> usize { self.index } } impl<'a> AsRef<TxIn> for TxInRef<'a> { fn as_ref(&self) -> &TxIn { self.input() } } /// An auxiliary enumeration that helps to get the balance of the previous /// unspent transaction output. #[derive(Debug, Copy, Clone)] pub enum UnspentTxOutValue<'a> { /// The output balance. Balance(u64), /// A reference to the transaction with the required unspent output. PrevTx(&'a Transaction), /// A reference to the transaction output to be spent. PrevOut(&'a TxOut), } impl<'a> UnspentTxOutValue<'a> { /// Returns the output balance value. pub fn balance(self, txin: TxInRef) -> u64 { match self { UnspentTxOutValue::Balance(value) => value, UnspentTxOutValue::PrevTx(prev_tx) => { prev_tx.output[txin.input().prev_index as usize].value } UnspentTxOutValue::PrevOut(out) => out.value, } } } impl<'a> From<u64> for UnspentTxOutValue<'a> { fn from(balance: u64) -> UnspentTxOutValue<'a> { UnspentTxOutValue::Balance(balance) } } impl<'a> From<&'a Transaction> for UnspentTxOutValue<'a> { fn from(tx_ref: &'a Transaction) -> UnspentTxOutValue { UnspentTxOutValue::PrevTx(tx_ref) } } impl<'a> From<&'a TxOut> for UnspentTxOutValue<'a> { fn from(tx_out: &'a TxOut) -> UnspentTxOutValue<'a> { UnspentTxOutValue::PrevOut(tx_out) } }