Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 79x 41x 11x 7x 4x 16x 15x 12x 14x 8x 5x 3x 2x 11x 14x 14x 2x 2x 2x 1x 7x 11x 11x 10x 9x 9x 9x 3x 3x 3x 2x 2x 10x 32x 10x 12x 5x 5x 7x 3x 3x 4x 4x 4x 4x 12x 12x 40x 1x 2x 2x 1x 50x 36x 41x 41x 39x 20x | /* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import bech32 from "bech32";
import bs58check from "bs58check";
import { ADDRESS_VERSION } from "./address-version";
import { EXPIRY_DEFAULT, MIN_FINAL_CLTV_EXPIRY_DEFAULT } from "./constants";
import * as crypto from "./crypto";
import { FallbackAddress } from "./fallback-address";
import { FIELD_TYPE } from "./field-type";
import { Route } from "./route";
import { Signature } from "./signature";
const picoToMsat = BigInt(10);
const picoToSat = picoToMsat * BigInt(1000);
const picoToBtc = BigInt(1e12);
const MAX_SHORT_DESC_BYTES = 639;
/**
* Invoice is the state container used for invoice data. It is used
* when building an invoice or contains the results from decoded
* invoices. The Invoice type does not perform validation on
* data it contains but does contain helper methods to help
* construct proper invoices.
*/
export class Invoice {
public network: string;
public timestamp: number = 0;
public fields: any[] = [];
public unknownFields: any[] = [];
/**
* ECDSA signature used to sign the invoice.
*/
public signature: Signature;
/**
* Compressed public key on elliptic curve secp256k1 corresponding
* to the node that generated and signed the invoice. Returned
* as 33-bytes.
*/
public pubkey: Buffer;
/**
* Buffer containing the buffer of the data used to generate the
* hash used in the signature.
*/
public hashData: Buffer;
/**
* Inidicates if signature recovery was used when decoding and
* performing signature verification for an invoice. This value
* will be false if a payee node field was provided.
*/
public usedSigRecovery: boolean;
/**
* Invoice value stored in pico bitcoin
*/
public _value: bigint;
/**
* Returns true when the invoice has a value
* associated with it. Invoices may optionally contain a value.
* When there is no value, the invoice is for the receipt of
* any value.
*/
public get hasValue(): boolean {
return typeof this._value === "bigint";
}
/**
* Warning: there is the possibility of precision loss!
*
* Gets the value in bitcoin as a string by converting from pico btc
* into bitcoin. Returns null if the invoice has no amount.
*
* Sets the value from a number or string that represeents a bitcoin
* value, such as 0.0001 to represent 10000 satoshi. Setting a falsy
* value will remove the value from the invoice.
*
* @deprecated This property is maintained for backwards compaibility.
* Use property `valueSat` or `valueMsat` instead.
*/
public get amount(): string {
return this.hasValue ? (Number(this._value) / Number(picoToBtc)).toFixed(11) : null;
}
public set amount(val) {
if (!val) this._value = null;
else this._value = BigInt(Math.trunc(parseFloat(val) * Number(picoToBtc)));
}
/**
* Warning: Msat fractions are truncated!
*
* Gets the value in satoshi as a string by converting from pico btc
* into satoshi. Returns null if the invoice has no amount.
*
* Sets the value in satoshi from a string or number, such as 10000 satoshi.
* Setting a falsy value will remove the value from the invoice.
*/
public get valueSat() {
return this.hasValue ? (this._value / picoToSat).toString() : null;
}
public set valueSat(val) {
if (!val) this._value = null;
else this._value = BigInt(val) * picoToSat;
}
/**
* Gets the value in milli-sataoshi as a string or returns null
* if the invoice has no amount.
*
* Sets the value in millisatoshi from a string or number. Setting a falsy
* value will remove the value from the invoice.
*/
public get valueMsat(): string {
return this.hasValue ? (this._value / picoToMsat).toString() : null;
}
public set valueMsat(val) {
if (!val || Number(val) === 0) this._value = null;
else this._value = BigInt(val) * picoToMsat;
}
/**
* Get the expiry time for the invoice as a big endian number
* of seconds. The defualt is one hour (3600).
*
* Sets the expiry time in seconds for the invoice. Only a single
* expiry field is valid in the invoice.
*/
public get expiry(): number {
return this._getFieldValue(FIELD_TYPE.EXPIRY, EXPIRY_DEFAULT);
}
public set expiry(value) {
this._setFieldValue(FIELD_TYPE.EXPIRY, value);
}
/**
* Gets the 256-bit payment hash. The preimage of this value
* will provide proof of payment.
*
* Sets the 256-bit payment hash for the invoice from a Buffer
* or hex-encoded string. Only a single field of this type is
* valid in the invoice.
*/
public get paymentHash(): Buffer {
return this._getFieldValue(FIELD_TYPE.PAYMENT_HASH);
}
public set paymentHash(value) {
Iif (typeof value === "string") value = Buffer.from(value, "hex");
this._setFieldValue(FIELD_TYPE.PAYMENT_HASH, value);
}
/**
* Gets the description as either a shortDesc or hashDesc
* value. If it is the former it is returned as a string.
* hashDesc is returned as a buffer of the hash.
*
* Sets the description for the invoice. An invoice must use hash
* description for messages longer than 639 bytes. If the string is
* longer than 639 bytes, the description will be hashed and stored
* in hashDesc. Otherwise, the raw string will be stored in the
* short desc.
*/
public get desc(): string | Buffer {
return this.shortDesc || this.hashDesc;
}
public set desc(desc) {
const len = Buffer.byteLength(desc);
if (len > MAX_SHORT_DESC_BYTES) this.hashDesc = crypto.sha256(desc as Buffer);
else this.shortDesc = desc as string;
}
/**
* Gets the short description text. Returns null when the invoice
* does not contain a short description. An invoice must set
* either a short description or a hash description.
*
* Sets the short description text. Maximum valid length is 639
* bytes. Only a single short desc or hash desc field is allowed.
* Setting this field will remove the hashDesc field value.
*/
public get shortDesc(): string {
return this._getFieldValue(FIELD_TYPE.SHORT_DESC);
}
public set shortDesc(value) {
this._removeFieldByType(FIELD_TYPE.HASH_DESC);
this._setFieldValue(FIELD_TYPE.SHORT_DESC, value);
}
/**
* Gets the 256-bit hash of the description. Returns
* null when an invoice does not contain a hash description.
* An invoice must contain either a shortDesc or hashDesc.
*
* Sets the hash description to the hex-encoded string or
* Buffer containing the the hashed description.
* This must be used for descriptions that are over 639 bytes
* long. Setting this field will remove any short desc fields.
*/
public get hashDesc(): Buffer {
return this._getFieldValue(FIELD_TYPE.HASH_DESC);
}
public set hashDesc(value) {
Iif (typeof value === "string") value = Buffer.from(value, "hex");
this._removeFieldByType(FIELD_TYPE.SHORT_DESC);
this._setFieldValue(FIELD_TYPE.HASH_DESC, value);
}
/**
* Gets the 33-byte public key of the payee node. This is
* used to explicitly describe the payee node instead of
* relying on pub key recovery from the signature.
*
* Sets the 33-byte public key of the payee node. This is
* used to set the public key explicitly instead of relying
* on signature recovery. This field must match the pubkey
* used to generate the signature.
*/
public get payeeNode(): Buffer {
return this._getFieldValue(FIELD_TYPE.PAYEE_NODE);
}
public set payeeNode(value) {
Iif (typeof value === "string") value = Buffer.from(value, "hex");
this._setFieldValue(FIELD_TYPE.PAYEE_NODE, value);
}
/**
* Gets the min final route CLTV expiry. If none is provided,
* the default is 9.
*
* Sets the min final route CLTV expiry used in the final route.
*/
public get minFinalCltvExpiry(): number {
return this._getFieldValue(FIELD_TYPE.MIN_FINAL_CLTV_EXPIRY, MIN_FINAL_CLTV_EXPIRY_DEFAULT);
}
public set minFinalCltvExpiry(value) {
this._setFieldValue(FIELD_TYPE.MIN_FINAL_CLTV_EXPIRY, value);
}
/**
* Gets a list of fall back addresses. An invoice can include
* multiple fallback addresses to send to an on-chain address
* in the event of failure.
*/
public get fallbackAddresses(): FallbackAddress[] {
return this.fields
.filter(p => p.type === FIELD_TYPE.FALLBACK_ADDRESS)
.map(p => p.value) as FallbackAddress[];
}
/**
* Adds a fallback address to the invoice. An invoice can include
* one or more fallback addresses to send to an on-chain address
* in the event of failure. This field may not make sense for small
* or time-sensitive payments.
*
* The address string will be parsed and the appropriate address
* type is added to the field metadata. The address will be
* converted into a buffer containing the string values of the
* address.
*/
public addFallbackAddress(addrStr: string) {
let version: ADDRESS_VERSION;
let address: number[] | Buffer;
// TODO - externalize magic strings!!!
if (addrStr.startsWith("1") || addrStr.startsWith("m") || addrStr.startsWith("n")) {
version = ADDRESS_VERSION.P2PKH;
address = bs58check.decode(addrStr).slice(1); // remove prefix
} else if (addrStr.startsWith("3") || addrStr.startsWith("2")) {
version = ADDRESS_VERSION.P2SH;
address = bs58check.decode(addrStr).slice(1); // remove prefix
} else Eif (addrStr.startsWith("bc1") || addrStr.startsWith("tb1")) {
const words = bech32.decode(addrStr).words;
version = words[0];
address = bech32.fromWords(words.slice(1));
}
if (Array.isArray(address)) address = Buffer.from(address);
this.fields.push({ type: FIELD_TYPE.FALLBACK_ADDRESS, value: { version, address } });
}
/**
* Gets the list of routes that are specified in the invoice.
* Route information is necessary to route payments to private
* nodes.
*/
public get routes(): Route[] {
return this.fields.filter(p => p.type === FIELD_TYPE.ROUTE).map(p => p.value);
}
/**
* Adds a collection of routes to the invoice. A route entry must
* be provided by private nodes so that a public addressible node
* can be found by the recipient.
*
* Multiple route fields can be added to an invoice in according
* with BOLT 11 to give the routing options.
*/
public addRoute(routes: Route[]) {
for (const route of routes) {
Iif (typeof route.pubkey === "string") route.pubkey = Buffer.from(route.pubkey, "hex");
Iif (typeof route.short_channel_id === "string") {
route.short_channel_id = Buffer.from(route.short_channel_id, "hex");
}
}
this.fields.push({ type: FIELD_TYPE.ROUTE, value: routes });
}
///////////////////////////////////////////////////////////////////
/**
* Gets the value of thee first matching field that matches the field
* type. If no result is found, the default value will be used.
*/
private _getFieldValue<T>(type: FIELD_TYPE, def?: T): T {
const field = this.fields.find(p => p.type === type);
return (field ? field.value : def) as T;
}
/**
* Sets the field value for the first matching field. If no
* field exists it will insert a new field with the type and value
* supplied.
*/
private _setFieldValue<T>(type: FIELD_TYPE, value: T) {
const field = this.fields.find(p => p.type === type);
if (field) field.value = value;
else this.fields.push({ type, value });
}
/**
* Removes the fields that match the supplied type.
*/
private _removeFieldByType(type: FIELD_TYPE) {
this.fields = this.fields.filter(p => p.type !== type);
}
}
|