-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (169 loc) · 5.13 KB
/
index.js
File metadata and controls
185 lines (169 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Address as BTCAddress } from '@scure/btc-signer';
import { cashaddr } from 'bech32cashaddr';
/**
* @typedef {Object} Network
* @property {number} pubKeyHash
* @property {number} scriptHash
* @property {string} prefix
*/
/**
* @typedef {Object} CashaddrDecoded
* @property {string} prefix
* @property {number[]} words
*/
/**
* @typedef {Object} CashOutScript
* @property {string} type
* @property {Uint8Array} hash
*/
/**
* @typedef {Object} OutScript
* @property {string} type
* @property {string} [format=cashaddr]
* @property {Uint8Array} hash
*/
/**
* @type {Network}
*/
export const NETWORK = {
pubKeyHash: 0x00,
scriptHash: 0x05,
prefix: 'bitcoincash',
};
/**
* @type {Network}
*/
export const TEST_NETWORK = {
pubKeyHash: 0x6f,
scriptHash: 0xc4,
prefix: 'bchtest',
};
export const CASHADDR_TYPE_BITS = ['pkh', 'sh'];
export const CASHADDR_SIZE_BITS = [20, 24, 28, 32, 40, 48, 56, 64];
/**
* @param {Network} network
*/
export function CashAddress(network = NETWORK) {
if (network.prefix?.length === 0) {
throw new TypeError(`CashAddress: invalid prefix length ${network.prefix?.length}`);
}
const prefix = network.prefix.toLowerCase();
if (network.prefix !== prefix && network.prefix !== network.prefix.toUpperCase()) {
throw new TypeError(`CashAddress: invalid prefix, string must be lowercase or uppercase ${network.prefix}`);
}
function encodeRaw({ typeBits, sizeBits, hash }, includePrefix) {
if ((typeBits >>> 0) !== typeBits || typeBits < 0b0000 || typeBits > 0b1111) {
throw new Error(`Invalid type bits=${typeBits}`);
}
if ((sizeBits >>> 0) !== sizeBits || sizeBits < 0b0000 || sizeBits > 0b111) {
throw new Error(`Invalid size bits=${sizeBits}`);
}
const versionByte = [(typeBits << 3) | sizeBits];
const data = new Uint8Array(hash.length + 1);
data.set(versionByte);
data.set(hash, 1);
return cashaddr.encode(prefix, cashaddr.toWords(data), includePrefix);
}
/**
* @param {CashOutScript} from
* @param {boolean} [includePrefix=true]
* @returns {string}
*/
function encode(from, includePrefix = true) {
const typeBits = CASHADDR_TYPE_BITS.indexOf(from.type);
if (typeBits === -1) throw new Error(`Invalid hash type=${from.type}`);
const sizeBits = CASHADDR_SIZE_BITS.indexOf(from.hash.length);
if (sizeBits === -1) throw new Error(`Invalid hash size=${from.hash.length}`);
return encodeRaw({ typeBits, sizeBits, hash: from.hash }, includePrefix);
}
function decodeRaw(address) {
const res = cashaddr.decode(address.includes(':') ? address : `${prefix}:${address}`);
if (res.prefix !== prefix) throw new Error(`Invalid cashaddr prefix ${res.prefix}`);
const data = cashaddr.fromWords(res.words);
const versionByte = data[0];
const hash = data.subarray(1);
const typeBits = versionByte >>> 3;
const sizeBits = versionByte & 0x07;
if (typeBits < 0b0000 || typeBits > 0b1111) {
throw new Error(`Invalid type bits=${typeBits}`);
}
if (sizeBits < 0b0000 || sizeBits > 0b111) {
throw new Error(`Invalid size bits=${sizeBits}`);
}
return { typeBits, sizeBits, hash };
}
/**
* @param {string} address
* @returns {CashOutScript}
*/
function decode(address) {
const { typeBits, sizeBits, hash } = decodeRaw(address);
const type = CASHADDR_TYPE_BITS[typeBits];
if (!type) throw new Error('Invalid hash type');
const size = CASHADDR_SIZE_BITS[sizeBits];
if (!size) throw new Error('Invalid hash size');
if (size !== hash.length) throw new Error('Invalid hash size');
return { type, hash };
}
return { encode, encodeRaw, decode, decodeRaw };
}
/**
* @param {Network} network
*/
export function Address(network = NETWORK) {
/**
* @param {OutScript} from
* @param {boolean} [includePrefix=true]
* @returns {string}
*/
function encode(from, includePrefix = true) {
const { type, format = 'cashaddr' } = from;
if (!['pkh', 'sh'].includes(type)) throw new TypeError(`Unknown address type=${type}`);
if (format === 'legacy') return BTCAddress(network).encode(from);
if (format === 'cashaddr') return CashAddress(network).encode(from, includePrefix);
throw new TypeError(`Unknown address format=${format}`);
}
/**
* @param {string} address
* @returns {OutScript}
*/
function decode(address) {
try {
const res = BTCAddress(network).decode(address);
return {
...res,
format: 'legacy',
};
} catch {
const res = CashAddress(network).decode(address);
return {
...res,
format: 'cashaddr',
};
}
}
/**
* @param {string} address
* @param {boolean} [includePrefix=true]
* @returns {string}
*/
function toCashAddress(address, includePrefix = true) {
const res = decode(address);
return encode({
...res,
format: 'cashaddr',
}, includePrefix);
}
/**
* @param {string} address
* @returns {string}
*/
function toLegacyAddress(address) {
const res = decode(address);
return encode({
...res,
format: 'legacy',
});
}
return { decode, encode, toCashAddress, toLegacyAddress };
}