forked from nbd-wtf/light-bolt11-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt12.d.ts
228 lines (206 loc) · 6.06 KB
/
bolt12.d.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const { bech32, hex, utf8 } = require('@scure/base');
// defaults for encode; default timestamp is current time at call
const DEFAULTNETWORK = {
// default network is bitcoin
bech32: 'bc',
pubKeyHash: 0x00,
scriptHash: 0x05,
validWitnessVersions: [0],
};
const TESTNETWORK = {
bech32: 'tb',
pubKeyHash: 0x6f,
scriptHash: 0xc4,
validWitnessVersions: [0],
};
const SIGNETNETWORK = {
bech32: 'tbs',
pubKeyHash: 0x6f,
scriptHash: 0xc4,
validWitnessVersions: [0],
};
const REGTESTNETWORK = {
bech32: 'bcrt',
pubKeyHash: 0x6f,
scriptHash: 0xc4,
validWitnessVersions: [0],
};
const SIMNETWORK = {
bech32: 'sb',
pubKeyHash: 0x3f,
scriptHash: 0x7b,
validWitnessVersions: [0],
};
const FEATUREBIT_ORDER = [
'option_data_loss_protect',
'initial_routing_sync',
'option_upfront_shutdown_script',
'gossip_queries',
'var_onion_optin',
'gossip_queries_ex',
'option_static_remotekey',
'payment_secret',
'basic_mpp',
'option_support_large_channel',
];
const DIVISORS = {
m: BigInt(1e3),
u: BigInt(1e6),
n: BigInt(1e9),
p: BigInt(1e12),
};
const MAX_MILLISATS = BigInt('2100000000000000000');
const MILLISATS_PER_BTC = BigInt(1e11);
const TAGCODES = {
offer_id: 1,
path_offer: 2,
offer_issuer_id: 3,
offer_issuer_node_id: 4,
offer_issuer_signature: 5,
onion_message: 10,
invoice_request: 7,
invreq_metadata: 8,
payment_hash: 3,
payment_hash: 1,
payment_secret: 16,
description: 13,
payee: 19,
description_hash: 23, // commit to longer descriptions (used by lnurl-pay)
expiry: 6, // default: 3600 (1 hour)
min_final_cltv_expiry: 24, // default: 9
fallback_address: 9,
route_hint: 3, // for extra routing info (private etc.)
feature_bits: 5,
metadata: 27,
};
// reverse the keys and values of TAGCODES and insert into TAGNAMES
const TAGNAMES = {};
for (let i = 0, keys = Object.keys(TAGCODES); i < keys.length; i++) {
const currentName = keys[i];
const currentCode = TAGCODES[keys[i]].toString();
TAGNAMES[currentCode] = currentName;
}
const TAGPARSERS = {
1: (words) => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
16: (words) => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
13: (words) => utf8.encode(bech32.fromWordsUnsafe(words)), // string variable length
19: (words) => hex.encode(bech32.fromWordsUnsafe(words)), // 264 bits
23: (words) => hex.encode(bech32.fromWordsUnsafe(words)), // 256 bits
27: (words) => hex.encode(bech32.fromWordsUnsafe(words)), // variable
6: wordsToIntBE, // default: 3600 (1 hour)
24: wordsToIntBE, // default: 9
3: routingInfoParser, // for extra routing info (private etc.)
5: featureBitsParser, // keep feature bits as array of 5 bit words
};
function getUnknownParser(tagCode) {
return (words) => ({
tagCode: parseInt(tagCode),
words: bech32.encode('unknown', words, Number.MAX_SAFE_INTEGER),
});
}
function wordsToIntBE(words) {
return words.reverse().reduce((total, item, index) => {
return total + item * Math.pow(32, index);
}, 0);
}
// first convert from words to buffer, trimming padding where necessary
// parse in 51 byte chunks. See encoder for details.
function routingInfoParser(words) {
const routes = [];
let pubkey,
shortChannelId,
feeBaseMSats,
feeProportionalMillionths,
cltvExpiryDelta;
let routesBuffer = bech32.fromWordsUnsafe(words);
while (routesBuffer.length > 0) {
pubkey = hex.encode(routesBuffer.slice(0, 33)); // 33 bytes
shortChannelId = hex.encode(routesBuffer.slice(33, 41)); // 8 bytes
feeBaseMSats = parseInt(hex.encode(routesBuffer.slice(41, 45)), 16); // 4 bytes
feeProportionalMillionths = parseInt(
hex.encode(routesBuffer.slice(45, 49)),
16
); // 4 bytes
cltvExpiryDelta = parseInt(hex.encode(routesBuffer.slice(49, 51)), 16); // 2 bytes
routesBuffer = routesBuffer.slice(51);
routes.push({
pubkey,
short_channel_id: shortChannelId,
fee_base_msat: feeBaseMSats,
fee_proportional_millionths: feeProportionalMillionths,
cltv_expiry_delta: cltvExpiryDelta,
});
}
return routes;
}
function featureBitsParser(words) {
const bools = words
.slice()
.reverse()
.map((word) => [
!!(word & 0b1),
!!(word & 0b10),
!!(word & 0b100),
!!(word & 0b1000),
!!(word & 0b10000),
])
.reduce((finalArr, itemArr) => finalArr.concat(itemArr), []);
while (bools.length < FEATUREBIT_ORDER.length * 2) {
bools.push(false);
}
const featureBits: {
[key: string]: string | {
start_bit: number;
bits: boolean[];
has_required: boolean;
};
} = {};
FEATUREBIT_ORDER.forEach((featureName, index) => {
let status;
if (bools[index * 2]) {
status = 'required';
} else if (bools[index * 2 + 1]) {
status = 'supported';
} else {
status = 'unsupported';
}
featureBits[featureName] = status;
});
const extraBits = bools.slice(FEATUREBIT_ORDER.length * 2);
featureBits.extra_bits = {
start_bit: FEATUREBIT_ORDER.length * 2,
bits: extraBits,
has_required: extraBits.reduce(
(result, bit, index) =>
index % 2 !== 0 ? result || false : result || bit,
false
),
};
return featureBits;
}
function hrpToMillisat(hrpString, outputString) {
let divisor, value;
if (hrpString.slice(-1).match(/^[munp]$/)) {
divisor = hrpString.slice(-1);
value = hrpString.slice(0, -1);
} else if (hrpString.slice(-1).match(/^[^munp0-9]$/)) {
throw new Error('Not a valid multiplier for the amount');
} else {
value = hrpString;
}
if (!value.match(/^\d+$/))
throw new Error('Not a valid human readable amount');
const valueBN = BigInt(value);
const millisatoshisBN = divisor
? (valueBN * MILLISATS_PER_BTC) / DIVISORS[divisor]
: valueBN * MILLISATS_PER_BTC;
if (
(divisor === 'p' && !(valueBN % BigInt(10) === BigInt(0))) ||
millisatoshisBN > MAX_MILLISATS
) {
throw new Error('Amount is outside of valid range');
}
if (millisatoshisBN < 0) {
throw new Error('Amount is outside of valid range');
}
return outputString ? millisatoshisBN.toString() : millisatoshisBN;