UNPKG

3.09 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, '__esModule', { value: true });
3const networks = require('./networks');
4const payments = require('./payments');
5const bscript = require('./script');
6const types = require('./types');
7const bech32 = require('bech32');
8const bs58check = require('bs58check');
9const typeforce = require('typeforce');
10function fromBase58Check(address) {
11 const payload = bs58check.decode(address);
12 // TODO: 4.0.0, move to "toOutputScript"
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}
19exports.fromBase58Check = fromBase58Check;
20function 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}
29exports.fromBech32 = fromBech32;
30function 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}
37exports.toBase58Check = toBase58Check;
38function toBech32(data, version, prefix) {
39 const words = bech32.toWords(data);
40 words.unshift(version);
41 return bech32.encode(prefix, words);
42}
43exports.toBech32 = toBech32;
44function fromOutputScript(output, network) {
45 // TODO: Network
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}
61exports.fromOutputScript = fromOutputScript;
62function 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}
91exports.toOutputScript = toOutputScript;