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

BIP-353: grok out on-chain addresses #2286

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 deletions components/LayerBalances/LightningSwipeableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Send from './../../assets/images/SVG/Send.svg';
interface LightningSwipeableRowProps {
navigation: StackNavigationProp<any, any>;
lightning?: string;
offer?: string;
locked?: boolean;
}

Expand Down Expand Up @@ -192,8 +193,15 @@ export default class LightningSwipeableRow extends Component<
};

private fetchLnInvoice = () => {
const { lightning } = this.props;
if (lightning?.toLowerCase().startsWith('lnurl')) {
const { lightning, offer } = this.props;
if (offer) {
this.props.navigation.navigate('Send', {
destination: offer,
bolt12: offer,
transactionType: 'BOLT 12',
isValid: true
});
} else if (lightning?.toLowerCase().startsWith('lnurl')) {
return getlnurlParams(lightning)
.then((params: any) => {
if (
Expand Down Expand Up @@ -243,8 +251,8 @@ export default class LightningSwipeableRow extends Component<
};

render() {
const { children, lightning, locked } = this.props;
if (locked && lightning) {
const { children, lightning, offer, locked } = this.props;
if (locked && (lightning || offer)) {
return (
<TouchableOpacity
onPress={() => this.fetchLnInvoice()}
Expand All @@ -266,7 +274,7 @@ export default class LightningSwipeableRow extends Component<
>
<TouchableOpacity
onPress={() =>
lightning ? this.fetchLnInvoice() : this.open()
lightning || offer ? this.fetchLnInvoice() : this.open()
}
activeOpacity={1}
>
Expand Down
6 changes: 6 additions & 0 deletions components/LayerBalances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface LayerBalancesProps {
value?: string;
amount?: string;
lightning?: string;
offer?: string;
locked?: boolean;
consolidated?: boolean;
editMode?: boolean;
Expand Down Expand Up @@ -160,6 +161,7 @@ const SwipeableRow = ({
value,
amount,
lightning,
offer,
locked,
editMode
}: {
Expand All @@ -170,6 +172,7 @@ const SwipeableRow = ({
value?: string;
amount?: string;
lightning?: string;
offer?: string;
locked?: boolean;
editMode?: boolean;
}) => {
Expand All @@ -178,6 +181,7 @@ const SwipeableRow = ({
<LightningSwipeableRow
navigation={navigation}
lightning={lightning}
offer={offer}
locked={locked}
>
<Row item={item} />
Expand Down Expand Up @@ -259,6 +263,7 @@ export default class LayerBalances extends Component<LayerBalancesProps, {}> {
value,
amount,
lightning,
offer,
onRefresh,
locked,
consolidated,
Expand Down Expand Up @@ -340,6 +345,7 @@ export default class LayerBalances extends Component<LayerBalancesProps, {}> {
value={value}
amount={amount}
lightning={lightning}
offer={offer}
locked={locked || editMode}
editMode={editMode}
/>
Expand Down
9 changes: 3 additions & 6 deletions utils/AddressUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,23 @@ describe('AddressUtils', () => {
)
).toEqual({
value: '',
lightning:
'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
offer: 'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
});
expect(
AddressUtils.processSendAddress(
'BITCOIN:?lno=lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
)
).toEqual({
value: '',
lightning:
'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
offer: 'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
});
expect(
AddressUtils.processSendAddress(
'BITCOIN:?LNO=lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
)
).toEqual({
value: '',
lightning:
'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
offer: 'lno1pgqpvggr3l9u9ppv79mzn7g9v98cf8zw900skucuz53zr5vvjss454zrnyes'
});
});

Expand Down
24 changes: 15 additions & 9 deletions utils/AddressUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const CUSTODIAL_LNDHUBS = [
];

const bitcoinQrParser = (input: string, prefix: string) => {
let amount, lightning;
let amount, lightning, offer;
const btcAddressAndParams = input.split(prefix)[1];
const [btcAddress, params] = btcAddressAndParams.split('?');

Expand All @@ -89,25 +89,27 @@ const bitcoinQrParser = (input: string, prefix: string) => {
amount = amount.toString();
}

if (result.lightning || result.LIGHTNING || result.lno || result.LNO) {
console.log('result', result.lno);
lightning =
result.lightning || result.LIGHTNING || result.lno || result.LNO;
if (result.lightning || result.LIGHTNING) {
lightning = result.lightning || result.LIGHTNING;
}

return [value, amount, lightning];
if (result.lno || result.LNO) {
offer = result.lno || result.LNO;
}

return [value, amount, lightning, offer];
};

class AddressUtils {
processSendAddress = (input: string) => {
let value, amount, lightning;
let value, amount, lightning, offer;

// handle addresses prefixed with 'bitcoin:' and
// payment requests prefixed with 'lightning:'

// handle BTCPay invoices with amounts embedded
if (input.includes('bitcoin:') || input.includes('BITCOIN:')) {
const [parsedValue, parsedAmount, parsedLightning] =
const [parsedValue, parsedAmount, parsedLightning, parsedOffer] =
bitcoinQrParser(
input,
input.includes('BITCOIN:') ? 'BITCOIN:' : 'bitcoin:'
Expand All @@ -121,6 +123,10 @@ class AddressUtils {
if (parsedLightning) {
lightning = parsedLightning;
}

if (parsedOffer) {
offer = parsedOffer;
}
} else if (input.includes('lightning:')) {
value = input.split('lightning:')[1];
} else if (input.includes('LIGHTNING:')) {
Expand All @@ -131,7 +137,7 @@ class AddressUtils {
value = input;
}

return { value, amount, lightning };
return { value, amount, lightning, offer };
};

processLNDHubAddress = (input: string) => {
Expand Down
18 changes: 17 additions & 1 deletion utils/handleAnything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,26 @@ const handleAnything = async (
bolt12 = bolt12.replace(/("|\\)/g, '');
bolt12 = bolt12.replace(/bitcoin:b12=/, '');

const { value, amount, lightning, offer }: any =
AddressUtils.processSendAddress(bolt12);

if (value) {
return [
'Accounts',
{
value,
amount,
lightning,
offer,
locked: true
}
];
}

return [
'Send',
{
destination: value,
destination: value || offer,
bolt12,
transactionType: 'BOLT 12',
isValid: true
Expand Down
20 changes: 17 additions & 3 deletions views/Accounts/Accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ interface AccountsProps {
SettingsStore: SettingsStore;
route: Route<
'Accounts',
{ value: string; amount: string; lightning: string; locked: boolean }
{
value: string;
amount: string;
lightning: string;
offer: string;
locked: boolean;
}
>;
}

interface AccountsState {
value: string;
amount: string;
lightning: string;
offer: string;
locked: boolean;
editMode: boolean;
}
Expand All @@ -53,6 +60,7 @@ export default class Accounts extends React.Component<
value: '',
amount: '',
lightning: '',
offer: '',
locked: false,
editMode: false
};
Expand All @@ -64,7 +72,7 @@ export default class Accounts extends React.Component<

componentDidMount() {
const { route } = this.props;
const { value, amount, lightning, locked } = route.params ?? {};
const { value, amount, lightning, offer, locked } = route.params ?? {};

if (value) {
this.setState({ value });
Expand All @@ -78,6 +86,10 @@ export default class Accounts extends React.Component<
this.setState({ lightning });
}

if (offer) {
this.setState({ offer });
}

if (locked) {
this.setState({ locked });
}
Expand All @@ -91,7 +103,8 @@ export default class Accounts extends React.Component<
SettingsStore,
navigation
} = this.props;
const { value, amount, lightning, locked, editMode } = this.state;
const { value, amount, lightning, offer, locked, editMode } =
this.state;
const { loadingAccounts, accounts } = UTXOsStore;

const FilterButton = () => (
Expand Down Expand Up @@ -173,6 +186,7 @@ export default class Accounts extends React.Component<
value={value}
amount={amount}
lightning={lightning}
offer={offer}
locked={locked}
editMode={editMode}
/>
Expand Down
Loading