Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments and minor improvements for utils.ts #83

Open
wants to merge 16 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,31 @@ async function invoiceHasBeenPaid() {

Contributions are very welcome.

If you want to contribute, please open an Issue or a PR.
If you want to contribute, please open an Issue or a PR.
If you open a PR, please do so from the `development` branch as the base branch.

### Version

```
* `main`
|\
|\ \
| | * `hotfix`
| |
| * `staging`
| |\
| |\ \
| | | * `bugfix`
| | |
| | * `development`
| | |\
| | | * `feature1`
| | | |
| | |/
| | *
| | |\
| | | * `feature2`
| | |/
| |/
|/ (create new version)
```
42 changes: 36 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,42 @@ export function checkResponse(data: { error?: string; detail?: string }): void {
}
}

/**
* Joins URL parts into a single URL string.
* @param parts The parts of the URL to join
* @returns The joined URL
*/
export function joinUrls(...parts: string[]): string {
export function joinUrls(...parts: Array<string>): string {
return parts.map((part) => part.replace(/(^\/+|\/+$)/g, '')).join('/');
}

export function decodeInvoice(bolt11Invoice: string): InvoiceData {
const invoiceData: InvoiceData = {} as InvoiceData;
const decodeResult = decode(bolt11Invoice);
invoiceData.paymentRequest = decodeResult.paymentRequest;
for (let i = 0; i < decodeResult.sections.length; i++) {
const decodedSection = decodeResult.sections[i];
if (decodedSection.name === 'amount') {
invoiceData.amountInSats = Number(decodedSection.value) / 1000;
invoiceData.amountInMSats = Number(decodedSection.value);
}
if (decodedSection.name === 'timestamp') {
invoiceData.timestamp = decodedSection.value;
}
if (decodedSection.name === 'description') {
invoiceData.memo = decodedSection.value;
}
if (decodedSection.name === 'expiry') {
invoiceData.expiry = decodedSection.value;
}
if (decodedSection.name === 'payment_hash') {
invoiceData.paymentHash = decodedSection.value.toString('hex');
}
}
return invoiceData;
}

export {
bigIntStringify,
bytesToNumber,
getDecodedToken,
getEncodedToken,
hexToNumber,
splitAmount,
getDefaultAmountPreference
};