@@ -41,6 +41,23 @@ function _toFutureSegwitAddress(output, network) {
4141 console . warn ( FUTURE_SEGWIT_VERSION_WARNING ) ;
4242 return toBech32 ( data , version , network . bech32 ) ;
4343}
44+ /**
45+ * decode address with base58 specification, return address version and address hash if valid
46+ * @example
47+ * ```ts
48+ * // valid case
49+ * fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH')
50+ * // => {version: 0, hash: <Buffer 75 1e 76 e8 19 91 96 d4 54 94 1c 45 d1 b3 a3 23 f1 43 3b d6>}
51+ *
52+ * // invalid case: address is too short
53+ * fromBase58Check('7SeEnXWPaCCALbVrTnszCVGfRU8cGfx')
54+ * // => throw new TypeError('7SeEnXWPaCCALbVrTnszCVGfRU8cGfx is too short')
55+ *
56+ * // invalid case: address is too long
57+ * fromBase58Check('j9ywUkWg2fTQrouxxh5rSZhRvrjMkEUfuiKe')
58+ * // => throw new TypeError('j9ywUkWg2fTQrouxxh5rSZhRvrjMkEUfuiKe is too long')
59+ * ```
60+ */
4461function fromBase58Check ( address ) {
4562 const payload = Buffer . from ( bs58check . decode ( address ) ) ;
4663 // TODO: 4.0.0, move to "toOutputScript"
@@ -51,6 +68,43 @@ function fromBase58Check(address) {
5168 return { version, hash } ;
5269}
5370exports . fromBase58Check = fromBase58Check ;
71+ /**
72+ * decode address with bech32 specification, return address version、address prefix and address data if valid
73+ * @example
74+ * ```ts
75+ * // valid case
76+ * fromBech32('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')
77+ * // => {version: 0, prefix: 'bc', data: <Buffer 75 1e 76 e8 19 91 96 d4 54 94 1c 45 d1 b3 a3 23 f1 43 3b d6>}
78+ *
79+ * // invalid case
80+ * fromBase58Check('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5')
81+ * // => Invalid checksum
82+ *
83+ * // invalid case
84+ * fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7')
85+ * // => Mixed-case string
86+ *
87+ * // invalid case
88+ * fromBase58Check('tb1pw508d6qejxtdg4y5r3zarquvzkan')
89+ * // => Excess padding
90+ *
91+ * // invalid case
92+ * fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
93+ * // => Excess padding
94+ *
95+ * // invalid case
96+ * fromBase58Check('bc1zw508d6qejxtdg4y5r3zarvaryvq37eag7')
97+ * // => Non-zero padding
98+ *
99+ * // invalid case
100+ * fromBase58Check('tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv')
101+ * // => uses wrong encoding
102+ *
103+ * // invalid case
104+ * fromBase58Check('bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd')
105+ * // => uses wrong encoding
106+ * ```
107+ */
54108function fromBech32 ( address ) {
55109 let result ;
56110 let version ;
@@ -73,6 +127,16 @@ function fromBech32(address) {
73127 } ;
74128}
75129exports . fromBech32 = fromBech32 ;
130+ /**
131+ * encode address hash to base58 address with version
132+ *
133+ * @example
134+ * ```ts
135+ * // valid case
136+ * toBase58Check('751e76e8199196d454941c45d1b3a323f1433bd6', 0)
137+ * // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
138+ * ```
139+ */
76140function toBase58Check ( hash , version ) {
77141 ( 0 , types_1 . typeforce ) (
78142 ( 0 , types_1 . tuple ) ( types_1 . Hash160bit , types_1 . UInt8 ) ,
@@ -84,6 +148,16 @@ function toBase58Check(hash, version) {
84148 return bs58check . encode ( payload ) ;
85149}
86150exports . toBase58Check = toBase58Check ;
151+ /**
152+ * encode address hash to bech32 address with version and prefix
153+ *
154+ * @example
155+ * ```ts
156+ * // valid case
157+ * toBech32('000000c4a5cad46221b2a187905e5266362b99d5e91c6ce24d165dab93e86433', 0, 'tb)
158+ * // => tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy
159+ * ```
160+ */
87161function toBech32 ( data , version , prefix ) {
88162 const words = bech32_1 . bech32 . toWords ( data ) ;
89163 words . unshift ( version ) ;
@@ -92,6 +166,48 @@ function toBech32(data, version, prefix) {
92166 : bech32_1 . bech32m . encode ( prefix , words ) ;
93167}
94168exports . toBech32 = toBech32 ;
169+ /**
170+ * decode address from output script with network, return address if matched
171+ * @example
172+ * ```ts
173+ * // valid case
174+ * fromOutputScript('OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG', 'bicoin)
175+ * // => 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
176+ *
177+ * // invalid case
178+ * fromOutputScript('031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG', undefined)
179+ * // => has no matching Address
180+ *
181+ * // invalid case
182+ * fromOutputScript('OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG', undefined)
183+ * // => has no matching Address
184+ *
185+ * // invalid case
186+ * fromOutputScript('OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474', undefined)
187+ * // => has no matching Address
188+ *
189+ * // invalid case
190+ * fromOutputScript('OP_0 75', undefined)
191+ * // => has no matching Address
192+ *
193+ * // invalid case
194+ * fromOutputScript('OP_0 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
195+ * // => has no matching Address
196+ *
197+ * // invalid case
198+ * fromOutputScript('OP_1 75', undefined)
199+ * // => has no matching Address
200+ *
201+ * // invalid case
202+ * fromOutputScript('OP_1 751e76e8199196d454941c45d1b3a323f1433bd6751e76e8199196d454941c45d1b3a323f1433bd675', undefined)
203+ * // => has no matching Address
204+ *
205+ * // invalid case
206+ * fromOutputScript('OP_1 fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f', undefined)
207+ * // => has no matching Address
208+ *
209+ * ```
210+ */
95211function fromOutputScript ( output , network ) {
96212 // TODO: Network
97213 network = network || networks . bitcoin ;
@@ -116,6 +232,27 @@ function fromOutputScript(output, network) {
116232 throw new Error ( bscript . toASM ( output ) + ' has no matching Address' ) ;
117233}
118234exports . fromOutputScript = fromOutputScript ;
235+ /**
236+ * encodes address to output script with network, return output script if address matched
237+ * @example
238+ * ```ts
239+ * // valid case
240+ * toOutputScript('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 'bicoin)
241+ * // => OP_DUP OP_HASH160 751e76e8199196d454941c45d1b3a323f1433bd6 OP_EQUALVERIFY OP_CHECKSIG
242+ *
243+ * // invalid case
244+ * toOutputScript('24kPZCmVgzfkpGdXExy56234MRHrsqQxNWE', undefined)
245+ * // => has no matching Script
246+ *
247+ * // invalid case
248+ * toOutputScript('BC1SW50QGDZ25J', { "bech32": "foo" })
249+ * // => has an invalid prefix
250+ *
251+ * // invalid case
252+ * toOutputScript('bc1rw5uspcuh', undefined)
253+ * // => has no matching Script
254+ * ```
255+ */
119256function toOutputScript ( address , network ) {
120257 network = network || networks . bitcoin ;
121258 let decodeBase58 ;
0 commit comments