11var Buffer = require ( 'safe-buffer' ) . Buffer
2+ var bech32 = require ( 'bech32' )
23var bs58check = require ( 'bs58check' )
34var bscript = require ( './script' )
5+ var btemplates = require ( './templates' )
46var networks = require ( './networks' )
57var typeforce = require ( 'typeforce' )
68var types = require ( './types' )
79
810function fromBase58Check ( address ) {
911 var payload = bs58check . decode ( address )
12+
13+ // TODO: 4.0.0, move to "toOutputScript"
1014 if ( payload . length < 21 ) throw new TypeError ( address + ' is too short' )
1115 if ( payload . length > 21 ) throw new TypeError ( address + ' is too long' )
1216
1317 var version = payload . readUInt8 ( 0 )
1418 var hash = payload . slice ( 1 )
1519
16- return { hash : hash , version : version }
20+ return { version : version , hash : hash }
21+ }
22+
23+ function fromBech32 ( address ) {
24+ var result = bech32 . decode ( address )
25+ var data = bech32 . fromWords ( result . words . slice ( 1 ) )
26+
27+ return {
28+ version : result . words [ 0 ] ,
29+ prefix : result . prefix ,
30+ data : Buffer . from ( data )
31+ }
1732}
1833
1934function toBase58Check ( hash , version ) {
@@ -26,28 +41,57 @@ function toBase58Check (hash, version) {
2641 return bs58check . encode ( payload )
2742}
2843
44+ function toBech32 ( data , version , prefix ) {
45+ var words = bech32 . toWords ( data )
46+ words . unshift ( version )
47+
48+ return bech32 . encode ( prefix , words )
49+ }
50+
2951function fromOutputScript ( outputScript , network ) {
3052 network = network || networks . bitcoin
3153
32- if ( bscript . pubKeyHash . output . check ( outputScript ) ) return toBase58Check ( bscript . compile ( outputScript ) . slice ( 3 , 23 ) , network . pubKeyHash )
33- if ( bscript . scriptHash . output . check ( outputScript ) ) return toBase58Check ( bscript . compile ( outputScript ) . slice ( 2 , 22 ) , network . scriptHash )
54+ if ( btemplates . pubKeyHash . output . check ( outputScript ) ) return toBase58Check ( bscript . compile ( outputScript ) . slice ( 3 , 23 ) , network . pubKeyHash )
55+ if ( btemplates . scriptHash . output . check ( outputScript ) ) return toBase58Check ( bscript . compile ( outputScript ) . slice ( 2 , 22 ) , network . scriptHash )
56+ if ( btemplates . witnessPubKeyHash . output . check ( outputScript ) ) return toBech32 ( bscript . compile ( outputScript ) . slice ( 2 , 22 ) , 0 , network . bech32 )
57+ if ( btemplates . witnessScriptHash . output . check ( outputScript ) ) return toBech32 ( bscript . compile ( outputScript ) . slice ( 2 , 34 ) , 0 , network . bech32 )
3458
3559 throw new Error ( bscript . toASM ( outputScript ) + ' has no matching Address' )
3660}
3761
3862function toOutputScript ( address , network ) {
3963 network = network || networks . bitcoin
4064
41- var decode = fromBase58Check ( address )
42- if ( decode . version === network . pubKeyHash ) return bscript . pubKeyHash . output . encode ( decode . hash )
43- if ( decode . version === network . scriptHash ) return bscript . scriptHash . output . encode ( decode . hash )
65+ var decode
66+ try {
67+ decode = fromBase58Check ( address )
68+ } catch ( e ) { }
69+
70+ if ( decode ) {
71+ if ( decode . version === network . pubKeyHash ) return btemplates . pubKeyHash . output . encode ( decode . hash )
72+ if ( decode . version === network . scriptHash ) return btemplates . scriptHash . output . encode ( decode . hash )
73+ } else {
74+ try {
75+ decode = fromBech32 ( address )
76+ } catch ( e ) { }
77+
78+ if ( decode ) {
79+ if ( decode . prefix !== network . bech32 ) throw new Error ( address + ' has an invalid prefix' )
80+ if ( decode . version === 0 ) {
81+ if ( decode . data . length === 20 ) return btemplates . witnessPubKeyHash . output . encode ( decode . data )
82+ if ( decode . data . length === 32 ) return btemplates . witnessScriptHash . output . encode ( decode . data )
83+ }
84+ }
85+ }
4486
4587 throw new Error ( address + ' has no matching Script' )
4688}
4789
4890module . exports = {
4991 fromBase58Check : fromBase58Check ,
92+ fromBech32 : fromBech32 ,
5093 fromOutputScript : fromOutputScript ,
5194 toBase58Check : toBase58Check ,
95+ toBech32 : toBech32 ,
5296 toOutputScript : toOutputScript
5397}
0 commit comments