1 | 'use strict';
|
2 | Object.defineProperty(exports, '__esModule', { value: true });
|
3 | const networks = require('./networks');
|
4 | const payments = require('./payments');
|
5 | const bscript = require('./script');
|
6 | const types = require('./types');
|
7 | const bech32 = require('bech32');
|
8 | const bs58check = require('bs58check');
|
9 | const typeforce = require('typeforce');
|
10 | function fromBase58Check(address) {
|
11 | const payload = bs58check.decode(address);
|
12 |
|
13 | if (payload.length < 21) throw new TypeError(address + ' is too short');
|
14 | if (payload.length > 21) throw new TypeError(address + ' is too long');
|
15 | const version = payload.readUInt8(0);
|
16 | const hash = payload.slice(1);
|
17 | return { version, hash };
|
18 | }
|
19 | exports.fromBase58Check = fromBase58Check;
|
20 | function fromBech32(address) {
|
21 | const result = bech32.decode(address);
|
22 | const data = bech32.fromWords(result.words.slice(1));
|
23 | return {
|
24 | version: result.words[0],
|
25 | prefix: result.prefix,
|
26 | data: Buffer.from(data),
|
27 | };
|
28 | }
|
29 | exports.fromBech32 = fromBech32;
|
30 | function toBase58Check(hash, version) {
|
31 | typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments);
|
32 | const payload = Buffer.allocUnsafe(21);
|
33 | payload.writeUInt8(version, 0);
|
34 | hash.copy(payload, 1);
|
35 | return bs58check.encode(payload);
|
36 | }
|
37 | exports.toBase58Check = toBase58Check;
|
38 | function toBech32(data, version, prefix) {
|
39 | const words = bech32.toWords(data);
|
40 | words.unshift(version);
|
41 | return bech32.encode(prefix, words);
|
42 | }
|
43 | exports.toBech32 = toBech32;
|
44 | function fromOutputScript(output, network) {
|
45 |
|
46 | network = network || networks.bitcoin;
|
47 | try {
|
48 | return payments.p2pkh({ output, network }).address;
|
49 | } catch (e) {}
|
50 | try {
|
51 | return payments.p2sh({ output, network }).address;
|
52 | } catch (e) {}
|
53 | try {
|
54 | return payments.p2wpkh({ output, network }).address;
|
55 | } catch (e) {}
|
56 | try {
|
57 | return payments.p2wsh({ output, network }).address;
|
58 | } catch (e) {}
|
59 | throw new Error(bscript.toASM(output) + ' has no matching Address');
|
60 | }
|
61 | exports.fromOutputScript = fromOutputScript;
|
62 | function toOutputScript(address, network) {
|
63 | network = network || networks.bitcoin;
|
64 | let decodeBase58;
|
65 | let decodeBech32;
|
66 | try {
|
67 | decodeBase58 = fromBase58Check(address);
|
68 | } catch (e) {}
|
69 | if (decodeBase58) {
|
70 | if (decodeBase58.version === network.pubKeyHash)
|
71 | return payments.p2pkh({ hash: decodeBase58.hash }).output;
|
72 | if (decodeBase58.version === network.scriptHash)
|
73 | return payments.p2sh({ hash: decodeBase58.hash }).output;
|
74 | } else {
|
75 | try {
|
76 | decodeBech32 = fromBech32(address);
|
77 | } catch (e) {}
|
78 | if (decodeBech32) {
|
79 | if (decodeBech32.prefix !== network.bech32)
|
80 | throw new Error(address + ' has an invalid prefix');
|
81 | if (decodeBech32.version === 0) {
|
82 | if (decodeBech32.data.length === 20)
|
83 | return payments.p2wpkh({ hash: decodeBech32.data }).output;
|
84 | if (decodeBech32.data.length === 32)
|
85 | return payments.p2wsh({ hash: decodeBech32.data }).output;
|
86 | }
|
87 | }
|
88 | }
|
89 | throw new Error(address + ' has no matching Script');
|
90 | }
|
91 | exports.toOutputScript = toOutputScript;
|