@@ -2,11 +2,15 @@ import { Network } from './networks';
22import * as networks from './networks' ;
33import * as payments from './payments' ;
44import * as bscript from './script' ;
5- import * as types from './types' ;
5+ import {
6+ typeforce ,
7+ tuple ,
8+ Hash160bit ,
9+ UInt8 ,
10+ TinySecp256k1Interface ,
11+ } from './types' ;
612import { bech32 , bech32m } from 'bech32' ;
713import * as bs58check from 'bs58check' ;
8- const { typeforce } = types ;
9-
1014export interface Base58CheckResult {
1115 hash : Buffer ;
1216 version : number ;
@@ -21,7 +25,7 @@ export interface Bech32Result {
2125const FUTURE_SEGWIT_MAX_SIZE : number = 40 ;
2226const FUTURE_SEGWIT_MIN_SIZE : number = 2 ;
2327const FUTURE_SEGWIT_MAX_VERSION : number = 16 ;
24- const FUTURE_SEGWIT_MIN_VERSION : number = 1 ;
28+ const FUTURE_SEGWIT_MIN_VERSION : number = 2 ;
2529const FUTURE_SEGWIT_VERSION_DIFF : number = 0x50 ;
2630const FUTURE_SEGWIT_VERSION_WARNING : string =
2731 'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
@@ -93,7 +97,7 @@ export function fromBech32(address: string): Bech32Result {
9397}
9498
9599export function toBase58Check ( hash : Buffer , version : number ) : string {
96- typeforce ( types . tuple ( types . Hash160bit , types . UInt8 ) , arguments ) ;
100+ typeforce ( tuple ( Hash160bit , UInt8 ) , arguments ) ;
97101
98102 const payload = Buffer . allocUnsafe ( 21 ) ;
99103 payload . writeUInt8 ( version , 0 ) ;
@@ -115,7 +119,11 @@ export function toBech32(
115119 : bech32m . encode ( prefix , words ) ;
116120}
117121
118- export function fromOutputScript ( output : Buffer , network ?: Network ) : string {
122+ export function fromOutputScript (
123+ output : Buffer ,
124+ network ?: Network ,
125+ eccLib ?: TinySecp256k1Interface ,
126+ ) : string {
119127 // TODO: Network
120128 network = network || networks . bitcoin ;
121129
@@ -131,14 +139,22 @@ export function fromOutputScript(output: Buffer, network?: Network): string {
131139 try {
132140 return payments . p2wsh ( { output, network } ) . address as string ;
133141 } catch ( e ) { }
142+ try {
143+ if ( eccLib )
144+ return payments . p2tr ( { output, network } , { eccLib } ) . address as string ;
145+ } catch ( e ) { }
134146 try {
135147 return _toFutureSegwitAddress ( output , network ) ;
136148 } catch ( e ) { }
137149
138150 throw new Error ( bscript . toASM ( output ) + ' has no matching Address' ) ;
139151}
140152
141- export function toOutputScript ( address : string , network ?: Network ) : Buffer {
153+ export function toOutputScript (
154+ address : string ,
155+ network ?: Network ,
156+ eccLib ?: TinySecp256k1Interface ,
157+ ) : Buffer {
142158 network = network || networks . bitcoin ;
143159
144160 let decodeBase58 : Base58CheckResult | undefined ;
@@ -165,6 +181,10 @@ export function toOutputScript(address: string, network?: Network): Buffer {
165181 return payments . p2wpkh ( { hash : decodeBech32 . data } ) . output as Buffer ;
166182 if ( decodeBech32 . data . length === 32 )
167183 return payments . p2wsh ( { hash : decodeBech32 . data } ) . output as Buffer ;
184+ } else if ( decodeBech32 . version === 1 ) {
185+ if ( decodeBech32 . data . length === 32 && eccLib )
186+ return payments . p2tr ( { pubkey : decodeBech32 . data } , { eccLib } )
187+ . output as Buffer ;
168188 } else if (
169189 decodeBech32 . version >= FUTURE_SEGWIT_MIN_VERSION &&
170190 decodeBech32 . version <= FUTURE_SEGWIT_MAX_VERSION &&
0 commit comments