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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright (c) 2017 Clark Moody
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#![warn(missing_docs)]

//! Encoding and decoding Bech32 Bitcoin Segwit Addresses
//! 
//! Encoding and decoding for Bitcoin Segregated Witness addresses. Bech32 is an
//! encoding scheme described in [BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki),
//! and segregated witness addresses encoded by Bech32 simply combine a coin-specific
//! human-readable part with the data of the witness program as the Bech32 data
//! payload.
//! 
//! # Examples
//! 
//! ```rust
//! use bitcoin_bech32::WitnessProgram;
//! use bitcoin_bech32::constants::Network;
//! 
//! let witness_program = WitnessProgram {
//!     version: 0,
//!     program: vec![
//!                 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 
//!                 0x21, 0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 
//!                 0x36, 0x2b, 0x99, 0xd5, 0xe9, 0x1c, 0x6c, 0xe2, 
//!                 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64, 0x33],
//!     network: Network::Testnet
//! };
//! 
//! let address = witness_program.to_address().unwrap();
//! assert_eq!(address, 
//!     "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy".to_string());
//!
//! let decoded = WitnessProgram::from_address(address).unwrap();
//! assert_eq!(decoded, witness_program);
//! ```

#![deny(missing_docs)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]

extern crate bech32;
use bech32::Bech32;

use std::{error, fmt};

pub mod constants;
use constants::Network;

/// Witness version and program data
#[derive(PartialEq, Debug, Clone)]
pub struct WitnessProgram {
    /// Witness program version
    pub version: u8,
    /// Witness program content
    pub program: Vec<u8>,
    /// Cryptocurrency network
    pub network: Network,
}

type EncodeResult = Result<String, Error>;
type DecodeResult = Result<WitnessProgram, Error>;
type PubKeyResult = Result<WitnessProgram, ScriptPubKeyError>;
type ValidationResult = Result<(), WitnessProgramError>;

impl WitnessProgram {
    /// Converts a Witness Program to a SegWit Address
    pub fn to_address(&self) -> EncodeResult {
        // Verify that the program is valid
        let val_result = self.validate();
        if val_result.is_err() {
            return Err(Error::WitnessProgram(val_result.unwrap_err()))
        }
        let mut data: Vec<u8> = vec![self.version];
        // Convert 8-bit program into 5-bit
        let p5 = match convert_bits(self.program.to_vec(), 8, 5, true) {
            Ok(p) => p,
            Err(e) => return Err(Error::Conversion(e))
        };
        let hrp = constants::hrp(&self.network);
        data.extend_from_slice(&p5);
        let b32 = Bech32 {hrp: hrp.clone(), data: data};
        let address = match b32.to_string() {
            Ok(s) => s,
            Err(e) => return Err(Error::Bech32(e))
        };
        // Ensure that the address decodes into a program properly
        WitnessProgram::from_address(address.clone())?;
        Ok(address)
    }

    /// Decodes a segwit address into a Witness Program
    ///
    /// Verifies that the `address` contains a known human-readable part 
    /// `hrp` and decodes as proper Bech32-encoded string. Allowed values of
    /// the human-readable part correspond to the defined types in `constants`
    pub fn from_address(address: String) -> DecodeResult {
        let b32 = match Bech32::from_string(address) {
            Ok(b) => b,
            Err(e) => return Err(Error::Bech32(e)),
        };
        let network_classified = constants::classify(&b32.hrp);
        if network_classified.is_none() {
            return Err(Error::InvalidHumanReadablePart)
        }
        if b32.data.len() == 0 || b32.data.len() > 65 {
            return Err(Error::Bech32(bech32::Error::InvalidLength))
        }
        // Get the script version and 5-bit program
        let (v, p5) = b32.data.split_at(1);
        let wp = WitnessProgram {
            version: v.to_vec()[0],
            // Convert to 8-bit program and assign
            program: match convert_bits(p5.to_vec(), 5, 8, false) {
                Ok(p) => p,
                Err(e) => return Err(Error::Conversion(e))
            },
            network: network_classified.unwrap()
        };
        match wp.validate() {
            Ok(_) => Ok(wp),
            Err(e) => Err(Error::WitnessProgram(e))
        }
    }

    /// Converts a `WitnessProgram` to a script public key
    ///
    /// The format for the output is 
    /// `[version, program length, <program>]`
    pub fn to_scriptpubkey(&self) -> Vec<u8> {
        let mut pubkey: Vec<u8> = Vec::new();
        let mut v = self.version;
        if v > 0 {
            v += 0x50;
        }
        pubkey.push(v);
        pubkey.push(self.program.len() as u8);
        pubkey.extend_from_slice(&self.program);
        pubkey
    }

    /// Extracts a WitnessProgram out of a provided script public key
    pub fn from_scriptpubkey(pubkey: &[u8], network: Network) -> PubKeyResult {
        // We need a version byte and a program length byte, with a program at 
        // least 2 bytes long.
        if pubkey.len() < 4 {
            return Err(ScriptPubKeyError::TooShort)
        }
        let proglen: usize = pubkey[1] as usize;
        // Check that program length byte is consistent with pubkey length
        if pubkey.len() != 2 + proglen {
            return Err(ScriptPubKeyError::InvalidLengthByte)
        }
        // Process script version
        let mut v: u8 = pubkey[0];
        if v > 0x50 {
            v -= 0x50;
        }
        let program = &pubkey[2..];
        Ok(WitnessProgram {
            version: v,
            program: program.to_vec(),
            network: network,
        })
    }

    /// Validates the WitnessProgram against version and length constraints
    pub fn validate(&self) -> ValidationResult {
        if self.version > 16 {
            // Invalid script version
            return Err(WitnessProgramError::InvalidScriptVersion)
        }
        if self.program.len() < 2 || self.program.len() > 40 {
            return Err(WitnessProgramError::InvalidLength)
        }
        // Check proper script length
        if self.version == 0 && 
                self.program.len() != 20 && self.program.len() != 32 {
            return Err(WitnessProgramError::InvalidVersionLength)
        }
        Ok(())
    }
}

type ConvertResult = Result<Vec<u8>, BitConversionError>;

/// Convert between bit sizes
///
/// # Panics
/// Function will panic if attempting to convert `from` or `to` a bit size that
/// is larger than 8 bits.
fn convert_bits(data: Vec<u8>, from: u32, to: u32, pad: bool) -> ConvertResult {
    if from > 8 || to > 8 {
        panic!("convert_bits `from` and `to` parameters greater than 8");
    }
    let mut acc: u32 = 0;
    let mut bits: u32 = 0;
    let mut ret: Vec<u8> = Vec::new();
    let maxv: u32 = (1<<to) - 1;
    for value in data {
        let v: u32 = value as u32;
        if (v >> from) != 0 {
            // Input value exceeds `from` bit size
            return Err(BitConversionError::InvalidInputValue(v as u8))
        }
        acc = (acc << from) | v;
        bits += from;
        while bits >= to {
            bits -= to;
            ret.push(((acc >> bits) & maxv) as u8);
        }
    }
    if pad {
        if bits > 0 {
            ret.push(((acc << (to - bits)) & maxv) as u8);
        }
    } else if bits >= from || ((acc << (to - bits)) & maxv) != 0 {
        return Err(BitConversionError::InvalidPadding)
    }
    Ok(ret)
}

/// Error types while encoding and decoding SegWit addresses
#[derive(PartialEq, Debug)]
pub enum Error {
    /// Some Bech32 conversion error
    Bech32(bech32::Error),
    /// Some witness program error
    WitnessProgram(WitnessProgramError),
    /// Some 5-bit <-> 8-bit conversion error
    Conversion(BitConversionError),
    /// The human-readable part is invalid (must be "bc" or "tb")
    InvalidHumanReadablePart,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Bech32(ref e) => write!(f, "{}", e),
            Error::WitnessProgram(ref e) => write!(f, "{}", e),
            Error::Conversion(ref e) => write!(f, "{}", e),
            Error::InvalidHumanReadablePart => write!(f, "invalid human-readable part"),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Bech32(_) => "Bech32 error",
            Error::WitnessProgram(_) => "witness program error",
            Error::Conversion(_) => "bit conversion error",
            Error::InvalidHumanReadablePart => "invalid human-readable part",
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            Error::Bech32(ref e) => Some(e),
            Error::WitnessProgram(ref e) => Some(e),
            Error::Conversion(ref e) => Some(e),
            _ => None,
        }
    }
}

/// Error types for validating scriptpubkeys
#[derive(PartialEq, Debug)]
pub enum ScriptPubKeyError {
    /// scriptpubkeys does not have enough data
    TooShort,
    /// The provided length byte does not match the data
    InvalidLengthByte,
}

/// Error types for witness programs
///
/// BIP141 specifies Segregated Witness and defines valid program lengths
/// for Version 0 scripts. Script version is also limited to values 0-16.
#[derive(PartialEq, Debug)]
pub enum WitnessProgramError {
    /// Denotes that the WitnessProgram is too long or too short
    ///
    /// Programs must be between 2 and 40 bytes
    InvalidLength,
    /// Given the program version, the length is invalid
    ///
    /// Version 0 scripts must be either 20 or 32 bytes
    InvalidVersionLength,
    /// Script version must be 0 to 16 inclusive
    InvalidScriptVersion,
}

impl fmt::Display for WitnessProgramError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            WitnessProgramError::InvalidLength => write!(f, "invalid length"),
            WitnessProgramError::InvalidVersionLength => write!(f, "program length incompatible with version"),
            WitnessProgramError::InvalidScriptVersion => write!(f, "invalid script versio"),
        }
    }
}

impl error::Error for WitnessProgramError {
    fn description(&self) -> &str {
        match *self {
            WitnessProgramError::InvalidLength => "invalid length",
            WitnessProgramError::InvalidVersionLength => "program length incompatible with version",
            WitnessProgramError::InvalidScriptVersion => "invalid script version"
        }
    }
}

/// Error types during bit conversion
#[derive(PartialEq, Debug)]
pub enum BitConversionError {
    /// Input value exceeds "from bits" size
    InvalidInputValue(u8),
    /// Invalid padding values in data
    InvalidPadding,
}

impl fmt::Display for BitConversionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            BitConversionError::InvalidInputValue(b) => write!(f, "invalid input value ({})", b),
            BitConversionError::InvalidPadding => write!(f, "invalid padding"),
        }
    }
}

impl error::Error for BitConversionError {
    fn description(&self) -> &str {
        match *self {
            BitConversionError::InvalidInputValue(_) => "invalid input value",
            BitConversionError::InvalidPadding => "invalid padding",
        }
    }
}

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

    #[test]
    fn valid_address() {
        let pairs: Vec<(&str, Vec<u8>)> = vec![
            (
                "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4",
                vec![
                    0x00, 0x14, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54,
                    0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6
                ]
            ),
            (
                "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7",
                vec![
                    0x00, 0x20, 0x18, 0x63, 0x14, 0x3c, 0x14, 0xc5, 0x16, 0x68, 0x04,
                    0xbd, 0x19, 0x20, 0x33, 0x56, 0xda, 0x13, 0x6c, 0x98, 0x56, 0x78,
                    0xcd, 0x4d, 0x27, 0xa1, 0xb8, 0xc6, 0x32, 0x96, 0x04, 0x90, 0x32,
                    0x62
                ]
            ),
            (
                "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx",
                vec![
                    0x51, 0x28, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54,
                    0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6,
                    0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c,
                    0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6
                ]
            ),
            (
                "BC1SW50QA3JX3S",
                vec![
                   0x60, 0x02, 0x75, 0x1e
                ]
            ),
            (
                "bc1zw508d6qejxtdg4y5r3zarvaryvg6kdaj",
                vec![
                    0x52, 0x10, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54,
                    0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23
                ]
            ),
            (
                "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy",
                vec![
                    0x00, 0x20, 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 0x21,
                    0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 0x36, 0x2b, 0x99, 0xd5,
                    0xe9, 0x1c, 0x6c, 0xe2, 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64,
                    0x33
                ]
            ),
        ];
        for p in pairs {
            let (address, scriptpubkey) = p;
            let dec_result = WitnessProgram::from_address(address.to_string());
            assert!(dec_result.is_ok());

            let prog = dec_result.unwrap();
            let pubkey = prog.clone().to_scriptpubkey();
            assert_eq!(pubkey, scriptpubkey);

            let spk_result = WitnessProgram::from_scriptpubkey(&scriptpubkey, prog.network.clone());
            assert!(spk_result.is_ok());
            assert_eq!(prog, spk_result.unwrap());

            let enc_result = prog.to_address();
            assert!(enc_result.is_ok());

            let enc_address = enc_result.unwrap();
            assert_eq!(address.to_lowercase(), enc_address.to_lowercase());
        }
    }

    #[test]
    fn invalid_address() {
        let pairs: Vec<(&str, Error)> = vec!(
            ("tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty",
                Error::InvalidHumanReadablePart),
            ("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5",
                Error::Bech32(bech32::Error::InvalidChecksum)),
            ("BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2",
                Error::WitnessProgram(WitnessProgramError::InvalidScriptVersion)),
            ("bc1rw5uspcuh",
                Error::WitnessProgram(WitnessProgramError::InvalidLength)),
            ("bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90",
                Error::Bech32(bech32::Error::InvalidLength)),
            ("BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P",
                Error::WitnessProgram(WitnessProgramError::InvalidVersionLength)),
            ("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7",
                Error::Bech32(bech32::Error::MixedCase)),
            ("tb1pw508d6qejxtdg4y5r3zarqfsj6c3",
                Error::Conversion(BitConversionError::InvalidPadding)),
            ("bc1zw508d6qejxtdg4y5r3zarvaryvqyzf3du",
                Error::Conversion(BitConversionError::InvalidPadding)),
            ("tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv",
                Error::Conversion(BitConversionError::InvalidPadding)),
            ("bc1gmk9yu",
                Error::Bech32(bech32::Error::InvalidLength)),
        );
        for p in pairs {
            let (address, desired_error) = p;
            let dec_result = WitnessProgram::from_address(address.to_string());
            println!("{:?}", address.to_string());
            if dec_result.is_ok() {
                println!("{:?}", dec_result.unwrap());
                panic!("Should be invalid: {:?}", address);
            }
            assert_eq!(dec_result.unwrap_err(), desired_error);
        }
    }
}