Skip to content

Commit

Permalink
feat(send): add unified onchain/lightning switch
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Oct 31, 2024
1 parent d4a2924 commit 590e75b
Show file tree
Hide file tree
Showing 46 changed files with 1,733 additions and 1,579 deletions.
63 changes: 29 additions & 34 deletions __tests__/scanner.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,82 @@
import { findlnurl } from '../src/utils/lnurl';
import { TBitcoinUrl, decodeQRData } from '../src/utils/scanner';
import { findLnUrl } from '../src/utils/lnurl';
import { parseUri } from '../src/utils/scanner/scanner';
import { TBitcoinData } from '../src/utils/scanner/types';

describe('QR codes', () => {
it('decodes a bitcoin URI with params', async () => {
const res = await decodeQRData(
const res = await parseUri(
'bitcoin:1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ?amount=0.0005&label=Nakamoto&message=Donation%20for%20project%20xyz',
);
if (res.isErr()) {
throw res.error;
}
const qrData = res.value[0] as TBitcoinUrl;
const qrData = res.value[0] as TBitcoinData;
expect(qrData.network).toEqual('bitcoin');
expect(qrData.qrDataType).toEqual('bitcoinAddress');
expect(qrData.sats).toEqual(50000);
expect(qrData.type).toEqual('onchain');
expect(qrData.amount).toEqual(50000);
expect(qrData.message).toEqual('Donation for project xyz');
});

it('decodes a bitcoin legacy address URI', async () => {
const res = await decodeQRData(
'bitcoin:1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ',
);
const res = await parseUri('bitcoin:1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ');
if (res.isErr()) {
throw res.error;
}
const qrData = res.value[0] as TBitcoinUrl;
const qrData = res.value[0] as TBitcoinData;
expect(qrData.network).toEqual('bitcoin');
expect(qrData.qrDataType).toEqual('bitcoinAddress');
expect(qrData.type).toEqual('onchain');
});

it('decodes a bitcoin wrapped segwit address URI', async () => {
const res = await decodeQRData(
'bitcoin:3DrziWGfPSYWZpmGxL4WytNeXA2mwzEwWJ',
);
const res = await parseUri('bitcoin:3DrziWGfPSYWZpmGxL4WytNeXA2mwzEwWJ');
if (res.isErr()) {
throw res.error;
}
const qrData = res.value[0] as TBitcoinUrl;
const qrData = res.value[0] as TBitcoinData;
expect(qrData.network).toEqual('bitcoin');
expect(qrData.qrDataType).toEqual('bitcoinAddress');
expect(qrData.type).toEqual('onchain');
});

it('decodes a bitcoin native segwit address URI', async () => {
const res = await decodeQRData(
const res = await parseUri(
'bitcoin:bc1qkk0vs43wzsundw37f8xslw69eddwfe24w9pyrg',
);
if (res.isErr()) {
throw res.error;
}
const qrData = res.value[0] as TBitcoinUrl;
const qrData = res.value[0] as TBitcoinData;
expect(qrData.network).toEqual('bitcoin');
expect(qrData.qrDataType).toEqual('bitcoinAddress');
expect(qrData.type).toEqual('onchain');
});

it('decodes a plain bitcoin native segwit address', async () => {
const res = await decodeQRData(
'bc1qkk0vs43wzsundw37f8xslw69eddwfe24w9pyrg',
);
const res = await parseUri('bc1qkk0vs43wzsundw37f8xslw69eddwfe24w9pyrg');
if (res.isErr()) {
throw res.error;
}
const qrData = res.value[0] as TBitcoinUrl;
const qrData = res.value[0] as TBitcoinData;
expect(qrData.network).toEqual('bitcoin');
expect(qrData.qrDataType).toEqual('bitcoinAddress');
expect(qrData.type).toEqual('onchain');
});

it('finds lnurl', async () => {
const base =
'lnurl1dp68gurn8ghj7mrww3uxymm59e3xjemnw4hzu7re0ghkcmn4wfkz7urp0ylh2um9wf5kg0fhxycnv9g9w58';
expect(findlnurl(base)).toEqual(base);
expect(findlnurl(base.toUpperCase())).toEqual(base);
expect(findlnurl('https://site.com/?lightning=' + base)).toEqual(base);
expect(findLnUrl(base)).toEqual(base);
expect(findLnUrl(base.toUpperCase())).toEqual(base);
expect(findLnUrl('https://site.com/?lightning=' + base)).toEqual(base);
expect(
findlnurl('https://site.com/?lightning=' + base.toUpperCase()),
findLnUrl('https://site.com/?lightning=' + base.toUpperCase()),
).toEqual(base);
expect(findlnurl('https://site.com/?nada=nada&lightning=' + base)).toEqual(
expect(findLnUrl('https://site.com/?nada=nada&lightning=' + base)).toEqual(
base,
);
expect(
findlnurl('https://site.com/?nada=nada&lightning=' + base.toUpperCase()),
findLnUrl('https://site.com/?nada=nada&lightning=' + base.toUpperCase()),
).toEqual(base);
expect(findlnurl('bs')).toEqual(null);
expect(findlnurl('https://site.com')).toEqual(null);
expect(findlnurl('https://site.com/?bs=' + base)).toEqual(null);
expect(findlnurl('bitcoin:site.com/?lightning=' + base)).toEqual(base);
expect(findLnUrl('bs')).toEqual(null);
expect(findLnUrl('https://site.com')).toEqual(null);
expect(findLnUrl('https://site.com/?bs=' + base)).toEqual(null);
expect(findLnUrl('bitcoin:site.com/?lightning=' + base)).toEqual(base);
});
});
36 changes: 21 additions & 15 deletions e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,9 @@ export const sleep = (ms) => {
});
};

export const isVisible = async (id) => {
try {
await expect(element(by.id(id))).toBeVisible();
return true;
} catch (e) {
return false;
}
};

export const isButtonEnabled = async (element) => {
try {
await expect(element).tap();
return true;
} catch (e) {
return false;
}
const attributes = await element.getAttributes();
return attributes.label !== 'disabled';
};

export async function waitForElementAttribute(
Expand Down Expand Up @@ -143,6 +130,25 @@ export const launchAndWait = async () => {
}
};

export const receiveOnchainFunds = async (rpc, amount = '0.001') => {
await element(by.id('Receive')).tap();
// Wait for animation
await sleep(1000);
// Get address from QR code
let { label: wAddress } = await element(by.id('QRCode')).getAttributes();
wAddress = wAddress.replace('bitcoin:', '');

// Send and mine
await rpc.sendToAddress(wAddress, amount);
await rpc.generateToAddress(1, await rpc.getNewAddress());

await waitFor(element(by.id('NewTxPrompt')))
.toBeVisible()
.withTimeout(10000);
await element(by.id('NewTxPrompt')).swipe('down');
await sleep(1000);
};

export const waitForPeerConnection = async (lnd, nodeId, maxRetries = 20) => {
let retries = 0;

Expand Down
4 changes: 2 additions & 2 deletions e2e/onchain.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ d('Onchain', () => {
await element(by.id('AddressContinue')).tap();

// Amount / NumberPad
await element(by.id('SendNumberPadMax')).tap();
await element(by.id('AvailableAmount')).tap();
// cat't use .multitap here, doesn't work properly
// maybe some race condition in beignet library ?
await element(
Expand All @@ -112,7 +112,7 @@ d('Onchain', () => {
by.id('NRemove').withAncestor(by.id('SendAmountNumberPad')),
).tap();
await expect(element(by.text('199 999'))).toBeVisible();
await element(by.id('SendNumberPadMax')).tap();
await element(by.id('AvailableAmount')).tap();
await element(by.id('ContinueAmount')).tap();

// Review & Send
Expand Down
Loading

0 comments on commit 590e75b

Please sign in to comment.