From 76c9985613071cbd4c9b4c98e3f243f539aeef79 Mon Sep 17 00:00:00 2001 From: myxmaster Date: Sat, 23 Sep 2023 20:30:19 +0200 Subject: [PATCH 1/2] fixed hexToBase64, refactored base64ToHex --- utils/Base64Utils.test.ts | 31 +++++++++++++++++++++++++++++++ utils/Base64Utils.ts | 36 +++--------------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/utils/Base64Utils.test.ts b/utils/Base64Utils.test.ts index f3758f50f..17c8fa27b 100644 --- a/utils/Base64Utils.test.ts +++ b/utils/Base64Utils.test.ts @@ -56,6 +56,37 @@ describe('Base64Utils', () => { '4465636f64696e672074686973204261736536343f2051756974652073706f6f6b792e' ); expect(Base64Utils.base64ToHex('WkVVUw==')).toEqual('5a455553'); + expect( + Base64Utils.base64ToHex( + 'AoJXmNvi6wDqzNnB9rrptOrK1T+20beEtJ/HCtFt4gWE' + ) + ).toEqual( + '02825798dbe2eb00eaccd9c1f6bae9b4eacad53fb6d1b784b49fc70ad16de20584' + ); + }); + }); + + describe('hexToBase64', () => { + it('Converts hexadecimal string to base64 string', () => { + expect( + Base64Utils.hexToBase64( + '54686973206973207468652066697273742074657374' + ) + ).toEqual('VGhpcyBpcyB0aGUgZmlyc3QgdGVzdA=='); + expect(Base64Utils.hexToBase64('456e642074686520466564')).toEqual( + 'RW5kIHRoZSBGZWQ=' + ); + expect( + Base64Utils.hexToBase64( + '4465636f64696e672074686973204261736536343f2051756974652073706f6f6b792e' + ) + ).toEqual('RGVjb2RpbmcgdGhpcyBCYXNlNjQ/IFF1aXRlIHNwb29reS4='); + expect(Base64Utils.hexToBase64('5a455553')).toEqual('WkVVUw=='); + expect( + Base64Utils.hexToBase64( + '02825798dbe2eb00eaccd9c1f6bae9b4eacad53fb6d1b784b49fc70ad16de20584' + ) + ).toEqual('AoJXmNvi6wDqzNnB9rrptOrK1T+20beEtJ/HCtFt4gWE'); }); }); }); diff --git a/utils/Base64Utils.ts b/utils/Base64Utils.ts index 9c4937443..748eec8fc 100644 --- a/utils/Base64Utils.ts +++ b/utils/Base64Utils.ts @@ -4,30 +4,7 @@ class Base64Utils { decodeBase64ToString = (input = '') => Buffer.from(input, 'base64').toString('utf8'); - hexStringToByte = (str = '') => { - if (!str) { - return new Uint8Array(); - } - - const a = []; - for (let i = 0, len = str.length; i < len; i += 2) { - a.push(parseInt(str.substring(i, i + 2), 16)); - } - - return new Uint8Array(a); - }; - - byteToBase64 = (buffer: Uint8Array) => { - let binary = ''; - const bytes = new Uint8Array(buffer); - const len = bytes.byteLength; - for (let i = 0; i < len; i++) { - binary += String.fromCharCode(bytes[i]); - } - return this.encodeStringToBase64(binary); - }; - - hexToBase64 = (str = '') => this.byteToBase64(this.hexStringToByte(str)); + hexToBase64 = (str = '') => Buffer.from(str, 'hex').toString('base64'); stringToUint8Array = (str: string) => Uint8Array.from(str, (x) => x.charCodeAt(0)); @@ -46,15 +23,8 @@ class Base64Utils { utf8ToHexString = (hexString: string) => Buffer.from(hexString, 'utf8').toString('hex'); - base64ToHex = (base64String: string) => { - const raw = this.decodeBase64ToString(base64String); - let result = ''; - for (let i = 0; i < raw.length; i++) { - const hex = raw.charCodeAt(i).toString(16); - result += hex.length === 2 ? hex : '0' + hex; - } - return result; - }; + base64ToHex = (base64String: string) => + Buffer.from(base64String, 'base64').toString('hex'); // from https://coolaj86.com/articles/unicode-string-to-a-utf-8-typed-array-buffer-in-javascript/ unicodeStringToUint8Array = (s: string) => { From bc1e12902089031dfde7bee27ecb57ad60215918 Mon Sep 17 00:00:00 2001 From: Evan Kaloudis Date: Tue, 26 Sep 2023 15:33:51 -0400 Subject: [PATCH 2/2] Merge branch 'master' into fix-keysend --- backends/LndHub.ts | 8 ++++---- components/AmountInput.tsx | 2 +- components/WalletHeader.tsx | 8 ++++++-- stores/InvoicesStore.ts | 35 +++++++++++++++++------------------ stores/UnitsStore.ts | 17 ++++++++--------- utils/Base64Utils.ts | 1 + 6 files changed, 37 insertions(+), 34 deletions(-) diff --git a/backends/LndHub.ts b/backends/LndHub.ts index a7ebfe132..bbe71157d 100644 --- a/backends/LndHub.ts +++ b/backends/LndHub.ts @@ -119,12 +119,12 @@ export default class LndHub extends LND { supportsOnchainSends = () => false; supportsOnchainReceiving = () => !( - stores.settingsStore.lndhubUrl.includes('lnbank/api/lndhub') || - stores.settingsStore.lndhubUrl.includes('lntxbot') || + stores?.settingsStore?.lndhubUrl?.includes('lnbank/api/lndhub') || + stores?.settingsStore?.lndhubUrl?.includes('lntxbot') || // Alby - stores.settingsStore.lndhubUrl.includes('ln.getalby.com') || + stores?.settingsStore?.lndhubUrl?.includes('ln.getalby.com') || // LNBits - stores.settingsStore.lndhubUrl.includes('/lndhub/ext/') + stores?.settingsStore?.lndhubUrl?.includes('/lndhub/ext/') ); supportsKeysend = () => false; supportsChannelManagement = () => false; diff --git a/components/AmountInput.tsx b/components/AmountInput.tsx index 950dac90c..265087946 100644 --- a/components/AmountInput.tsx +++ b/components/AmountInput.tsx @@ -179,7 +179,7 @@ export default class AmountInput extends React.Component< onPress={() => !locked && this.onChangeUnits()} style={{ marginTop: 22, marginLeft: 15 }} > - {fiatEnabled && units !== 'fiat' ? ( + {UnitsStore!.getNextUnit() === 'fiat' ? ( ( navigation.navigate('Send', { destination: clipboard })} + onPress={async () => { + const response = await handleAnything(clipboard); + const [route, props] = response; + navigation.navigate(route, props); + }} > diff --git a/stores/InvoicesStore.ts b/stores/InvoicesStore.ts index 4c540bfa7..11c39f70e 100644 --- a/stores/InvoicesStore.ts +++ b/stores/InvoicesStore.ts @@ -260,11 +260,24 @@ export default class InvoicesStore { if (!unified) this.creatingInvoice = false; + let jit_bolt11: string = ''; + if ( + BackendUtils.supportsLSPs() && + this.settingsStore.settings?.enableLSP && + value !== '0' + ) { + await this.lspStore + .getZeroConfInvoice(invoice.getPaymentRequest) + .then((response: any) => { + jit_bolt11 = response; + }); + } + if (lnurl) { const u = url.parse(lnurl.callback); const qs = querystring.parse(u.query); qs.k1 = lnurl.k1; - qs.pr = invoice.getPaymentRequest; + qs.pr = jit_bolt11 || invoice.getPaymentRequest; u.search = querystring.stringify(qs); u.query = querystring.stringify(qs); @@ -301,25 +314,11 @@ export default class InvoicesStore { }); } - if ( - BackendUtils.supportsLSPs() && - this.settingsStore.settings?.enableLSP && - value !== '0' - ) { - return await this.lspStore - .getZeroConfInvoice(invoice.getPaymentRequest) - .then((response: any) => { - const jit_bolt11: string = response; - return { - rHash: invoice.getFormattedRhash, - paymentRequest: jit_bolt11 - }; - }); - } - return { rHash: invoice.getFormattedRhash, - paymentRequest: invoice.getPaymentRequest + paymentRequest: jit_bolt11 + ? jit_bolt11 + : invoice.getPaymentRequest }; }) .catch((error: any) => { diff --git a/stores/UnitsStore.ts b/stores/UnitsStore.ts index 45062f38a..75a77220e 100644 --- a/stores/UnitsStore.ts +++ b/stores/UnitsStore.ts @@ -29,23 +29,22 @@ export default class UnitsStore { } @action - public changeUnits = () => { + public changeUnits = () => (this.units = this.getNextUnit()); + + public getNextUnit = () => { const { settings } = this.settingsStore; const { fiatEnabled } = settings; if (!fiatEnabled) { - this.units = this.units == 'sats' ? 'BTC' : 'sats'; + return this.units === 'sats' ? 'BTC' : 'sats'; } else { switch (this.units) { case 'sats': - this.units = 'BTC'; - break; + return 'BTC'; case 'BTC': - this.units = 'fiat'; - break; - case 'fiat': - this.units = 'sats'; - break; + return 'fiat'; + default: + return 'sats'; } } }; diff --git a/utils/Base64Utils.ts b/utils/Base64Utils.ts index 748eec8fc..56c22e0a9 100644 --- a/utils/Base64Utils.ts +++ b/utils/Base64Utils.ts @@ -1,6 +1,7 @@ class Base64Utils { encodeStringToBase64 = (input = '') => Buffer.from(input).toString('base64'); + decodeBase64ToString = (input = '') => Buffer.from(input, 'base64').toString('utf8');