forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b4ae67
commit f105acb
Showing
8 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import React, { useState, useContext, useCallback, useMemo } from 'react'; | ||
import { I18nManager, ScrollView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; | ||
import { Icon } from 'react-native-elements'; | ||
|
||
import { BlueButton, BlueCard, BlueLoading, BlueSpacing20, BlueSpacing40, BlueText, SafeBlueArea } from '../../BlueComponents'; | ||
|
||
import navigationStyle from '../../components/navigationStyle'; | ||
import Lnurl from '../../class/lnurl'; | ||
import { Chain } from '../../models/bitcoinUnits'; | ||
import loc from '../../loc'; | ||
import { BlueStorageContext } from '../../blue_modules/storage-context'; | ||
import { useNavigation, useRoute, useTheme } from '@react-navigation/native'; | ||
import selectWallet from '../../helpers/select-wallet'; | ||
import LottieView from 'lottie-react-native'; | ||
import url from 'url'; | ||
|
||
const AuthState = { | ||
USER_PROMPT: 0, | ||
IN_PROGRESS: 1, | ||
SUCCESS: 2, | ||
ERROR: 3, | ||
}; | ||
|
||
const LnurlAuth = () => { | ||
const { wallets } = useContext(BlueStorageContext); | ||
const { name } = useRoute(); | ||
const { walletID, lnurl } = useRoute().params; | ||
const wallet = useMemo(() => wallets.find(w => w.getID() === walletID), [wallets, walletID]); | ||
const LN = useMemo(() => new Lnurl(lnurl), [lnurl]); | ||
const parsedLnurl = useMemo( | ||
() => (lnurl ? url.parse(Lnurl.getUrlFromLnurl(lnurl), true) : {}), // eslint-disable-line node/no-deprecated-api | ||
[lnurl], | ||
); | ||
const [authState, setAuthState] = useState(AuthState.USER_PROMPT); | ||
const [errMsg, setErrMsg] = useState(''); | ||
const { setParams, navigate } = useNavigation(); | ||
const { colors } = useTheme(); | ||
const stylesHook = StyleSheet.create({ | ||
root: { | ||
backgroundColor: colors.background, | ||
}, | ||
walletWrapLabel: { | ||
color: colors.buttonAlternativeTextColor, | ||
}, | ||
}); | ||
|
||
const showSelectWalletScreen = useCallback(() => { | ||
selectWallet(navigate, name, Chain.OFFCHAIN).then(wallet => setParams({ walletID: wallet.getID() })); | ||
}, [navigate, name, setParams]); | ||
|
||
const authenticate = useCallback(() => { | ||
wallet | ||
.authenticate(LN) | ||
.then(() => { | ||
setAuthState(AuthState.SUCCESS); | ||
setErrMsg(''); | ||
}) | ||
.catch(err => { | ||
setAuthState(AuthState.ERROR); | ||
setErrMsg(err); | ||
}); | ||
}, [wallet, LN]); | ||
|
||
if (!parsedLnurl || !wallet || authState === AuthState.IN_PROGRESS) | ||
return ( | ||
<View style={[styles.root, stylesHook.root]}> | ||
<BlueLoading /> | ||
</View> | ||
); | ||
|
||
const renderWalletSelectionButton = authState === AuthState.USER_PROMPT && ( | ||
<View style={styles.walletSelectRoot}> | ||
{authState !== AuthState.IN_PROGRESS && ( | ||
<TouchableOpacity accessibilityRole="button" style={styles.walletSelectTouch} onPress={showSelectWalletScreen}> | ||
<Text style={styles.walletSelectText}>{loc.wallets.select_wallet.toLowerCase()}</Text> | ||
<Icon name={I18nManager.isRTL ? 'angle-left' : 'angle-right'} size={18} type="font-awesome" color="#9aa0aa" /> | ||
</TouchableOpacity> | ||
)} | ||
<View style={styles.walletWrap}> | ||
<TouchableOpacity accessibilityRole="button" style={styles.walletWrapTouch} onPress={showSelectWalletScreen}> | ||
<Text style={[styles.walletWrapLabel, stylesHook.walletWrapLabel]}>{wallet.getLabel()}</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</View> | ||
); | ||
|
||
return ( | ||
<SafeBlueArea style={styles.root}> | ||
{authState === AuthState.USER_PROMPT && ( | ||
<> | ||
<ScrollView> | ||
<BlueCard> | ||
<BlueText style={styles.alignSelfCenter}>{loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_question_part_1`]}</BlueText> | ||
<BlueText style={styles.domainName}>{parsedLnurl.hostname}</BlueText> | ||
<BlueText style={styles.alignSelfCenter}>{loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_question_part_2`]}</BlueText> | ||
<BlueSpacing40 /> | ||
<BlueButton title={loc.lnurl_auth.authenticate} onPress={authenticate} /> | ||
<BlueSpacing40 /> | ||
</BlueCard> | ||
</ScrollView> | ||
{renderWalletSelectionButton} | ||
</> | ||
)} | ||
|
||
{authState === AuthState.SUCCESS && ( | ||
<BlueCard> | ||
<View style={styles.iconContainer}> | ||
<LottieView style={styles.icon} source={require('../../img/bluenice.json')} autoPlay loop={false} /> | ||
</View> | ||
<BlueSpacing20 /> | ||
<BlueText style={styles.alignSelfCenter}> | ||
{loc.formatString(loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_answer`], { hostname: parsedLnurl.hostname })} | ||
</BlueText> | ||
<BlueSpacing20 /> | ||
</BlueCard> | ||
)} | ||
|
||
{authState === AuthState.ERROR && ( | ||
<BlueCard> | ||
<BlueSpacing20 /> | ||
<BlueText style={styles.alignSelfCenter}> | ||
{loc.formatString(loc.lnurl_auth.could_not_auth, { hostname: parsedLnurl.hostname })} | ||
</BlueText> | ||
<BlueText style={styles.alignSelfCenter}>{errMsg}</BlueText> | ||
<BlueSpacing20 /> | ||
</BlueCard> | ||
)} | ||
</SafeBlueArea> | ||
); | ||
}; | ||
|
||
export default LnurlAuth; | ||
|
||
const styles = StyleSheet.create({ | ||
alignSelfCenter: { | ||
alignSelf: 'center', | ||
}, | ||
domainName: { | ||
alignSelf: 'center', | ||
fontWeight: 'bold', | ||
fontSize: 25, | ||
paddingVertical: 10, | ||
}, | ||
root: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
}, | ||
iconContainer: { | ||
backgroundColor: '#ccddf9', | ||
width: 120, | ||
height: 120, | ||
maxWidth: 120, | ||
maxHeight: 120, | ||
padding: 0, | ||
borderRadius: 60, | ||
alignSelf: 'center', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
icon: { | ||
width: 400, | ||
height: 400, | ||
}, | ||
walletSelectRoot: { | ||
alignItems: 'center', | ||
justifyContent: 'flex-end', | ||
}, | ||
walletSelectTouch: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
}, | ||
walletSelectText: { | ||
color: '#9aa0aa', | ||
fontSize: 14, | ||
marginRight: 8, | ||
}, | ||
walletWrap: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
marginVertical: 4, | ||
}, | ||
walletWrapTouch: { | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
}, | ||
walletWrapLabel: { | ||
fontSize: 14, | ||
}, | ||
}); | ||
|
||
LnurlAuth.navigationOptions = navigationStyle({ | ||
title: '', | ||
closeButton: true, | ||
closeButtonFunc: ({ navigation }) => navigation.dangerouslyGetParent().popToTop(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,6 +226,48 @@ describe('lightning address', function () { | |
assert.ok(!Lnurl.isLightningAddress('a@')); | ||
}); | ||
|
||
it('can authenticate', async () => { | ||
const LN = new Lnurl( | ||
'LNURL1DP68GURN8GHJ7MRFVA58GMNFDENKCMM8D9HZUMRFWEJJ7MR0VA5KU0MTXY7NYVFEX93X2DFK8P3KVEFKVSEXZWR98PSNJVRRV5CRGCE3X4JKGE3HXPNXGCMPV5MXXVTZ89NXZENXXCURGCTRV93RVE35XQCXVCFSVSN8GCT884KX7EMFDCDKKXQ0', | ||
); | ||
|
||
// poor-man's mock: | ||
LN._fetchGet = LN.fetchGet; | ||
let requestedUri = -1; | ||
LN.fetchGet = actuallyRequestedUri => { | ||
requestedUri = actuallyRequestedUri; | ||
return { | ||
status: 'OK', | ||
}; | ||
}; | ||
|
||
await assert.doesNotReject(LN.authenticate('lndhub://dc56b8cf8ef3b60060cf:94eac57510de2738451d')); | ||
assert.strictEqual( | ||
requestedUri, | ||
'https://lightninglogin.live/login?k1=2191be568cfe6d2a8e8a90ce04c15edf70fdcae6c1b9faff684acab6f400fa0d&tag=login&sig=304502210093ab4ead8dd619f2ddb3d52bd4bb01725badcb2a3daa3870fb41a38096f9a37d0220464a32e94e13dcec20ea94b94df0fa52f45cd88b01d7247042136ad0c71752d2&key=03e7b61e57efff1925ab9082625400cae2c8ad88a984e7aa4987abb77818570018', | ||
); | ||
}); | ||
|
||
it('returns the server error response as the reject error from lnurl-auth', async () => { | ||
const LN = new Lnurl( | ||
'LNURL1DP68GURN8GHJ7MRFVA58GMNFDENKCMM8D9HZUMRFWEJJ7MR0VA5KU0MTXY7NYVFEX93X2DFK8P3KVEFKVSEXZWR98PSNJVRRV5CRGCE3X4JKGE3HXPNXGCMPV5MXXVTZ89NXZENXXCURGCTRV93RVE35XQCXVCFSVSN8GCT884KX7EMFDCDKKXQ0', | ||
); | ||
|
||
// poor-man's mock: | ||
LN._fetchGet = LN.fetchGet; | ||
LN.fetchGet = () => { | ||
return { | ||
reason: 'Invalid signature', | ||
status: 'ERROR', | ||
}; | ||
}; | ||
|
||
await assert.rejects(LN.authenticate('lndhub://dc56b8cf8ef3b60060cf:94eac57510de2738451d'), err => { | ||
assert.strictEqual(err, 'Invalid signature'); | ||
return true; | ||
}); | ||
}); | ||
|
||
it('works', async () => { | ||
const LN = new Lnurl('[email protected]'); | ||
|
||
|