-
Notifications
You must be signed in to change notification settings - Fork 782
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
59d3354
commit 897b945
Showing
14 changed files
with
364 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Frisbee from 'frisbee'; | ||
import url from 'url'; | ||
|
||
export default class AOPP { | ||
static typeAny = 'any'; | ||
static typeP2wpkh = 'p2wpkh'; | ||
static typeP2sh = 'p2sh'; | ||
static typeP2pkh = 'p2pkh'; | ||
|
||
static getSegwitByAddressFormat(addressType) { | ||
if (![AOPP.typeP2wpkh, AOPP.typeP2sh, AOPP.typeP2pkh].includes(addressType)) { | ||
throw new Error('Work only for limited types'); | ||
} | ||
switch (addressType) { | ||
case 'p2wpkh': | ||
return 'p2wpkh'; | ||
case 'p2sh': | ||
return 'p2sh(p2wpkh)'; | ||
case 'p2pkh': | ||
return undefined; | ||
} | ||
} | ||
|
||
constructor(uri) { | ||
this.uri = uri; | ||
const { protocol, query } = url.parse(uri, true); // eslint-disable-line node/no-deprecated-api | ||
|
||
if (protocol !== 'aopp:') throw new Error('Unsupported protocol'); | ||
if (query.v !== '0') throw new Error('Unsupported version'); | ||
if (!query.msg) throw new Error('Message required'); | ||
if (query.msg.lenth > 1024) throw new Error('Message is too big'); | ||
if (query.asset && query.asset !== 'btc') throw new Error('Unsupported asset'); | ||
if (query.format) { | ||
if (![AOPP.typeAny, AOPP.typeP2wpkh, AOPP.typeP2sh, AOPP.typeP2pkh].includes(query.format)) { | ||
throw new Error('Unsupported address format'); | ||
} | ||
} else { | ||
query.format = 'any'; | ||
} | ||
if (!query.callback) throw new Error('Callback required'); | ||
|
||
this.v = Number(query.v); | ||
this.msg = query.msg; | ||
this.format = query.format; | ||
this.callback = query.callback; | ||
|
||
// parse callback url | ||
const { hostname } = url.parse(this.callback, true); // eslint-disable-line node/no-deprecated-api | ||
if (!hostname) throw new Error('Wrong callback'); | ||
|
||
this.callbackHostname = hostname; | ||
|
||
this._api = new Frisbee({ | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
} | ||
|
||
async send({ address, signature }) { | ||
const res = await this._api.post(this.callback, { | ||
body: { | ||
version: this.v, | ||
address, | ||
signature, | ||
}, | ||
}); | ||
|
||
if (res.err) throw res.err; | ||
} | ||
} |
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,33 @@ | ||
import { Alert } from 'react-native'; | ||
import loc from '../loc'; | ||
|
||
/** | ||
* Helper function that throws un-cancellable dialog to confirm user's action. | ||
* Promise resolves to TRUE if user confirms, FALSE otherwise | ||
* | ||
* @param title {string} | ||
* @param text {string} | ||
* | ||
* @return {Promise<boolean>} | ||
*/ | ||
module.exports = function (title = 'Are you sure?', text = '') { | ||
return new Promise(resolve => { | ||
Alert.alert( | ||
title, | ||
text, | ||
[ | ||
{ | ||
text: loc._.yes, | ||
onPress: () => resolve(true), | ||
style: 'default', | ||
}, | ||
{ | ||
text: loc._.cancel, | ||
onPress: () => resolve(false), | ||
style: 'cancel', | ||
}, | ||
], | ||
{ cancelable: false }, | ||
); | ||
}); | ||
}; |
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,32 @@ | ||
/** | ||
* Helper function to select wallet. | ||
* Navigates to selector screen, and then navigates back while resolving promise with selected wallet. | ||
* | ||
* @param navigateFunc {function} Function that does navigatino should be passed from outside | ||
* @param currentScreenName {string} Current screen name, so we know to what screen to get back to | ||
* @param chainType {string} One of `Chain.` constant to be used to filter wallet pannels to show | ||
* @param availableWallets {array} Wallets to be present in selector. If set, overrides `chainType` | ||
* @param noWalletExplanationText {string} Text that is displayed when there are no wallets to select from | ||
* | ||
* @returns {Promise<AbstractWallet>} | ||
*/ | ||
module.exports = function (navigateFunc, currentScreenName, chainType, availableWallets, noWalletExplanationText = '') { | ||
return new Promise((resolve, reject) => { | ||
if (!currentScreenName) return reject(new Error('currentScreenName is not provided')); | ||
|
||
const params = {}; | ||
if (chainType) params.chainType = chainType; | ||
if (availableWallets) params.availableWallets = availableWallets; | ||
if (noWalletExplanationText) params.noWalletExplanationText = noWalletExplanationText; | ||
|
||
params.onWalletSelect = function (selectedWallet) { | ||
if (!selectedWallet) return; | ||
|
||
setTimeout(() => resolve(selectedWallet), 1); | ||
console.warn('trying to navigate back to', currentScreenName); | ||
navigateFunc(currentScreenName); | ||
}; | ||
|
||
navigateFunc('SelectWallet', params); | ||
}); | ||
}; |
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,66 @@ | ||
import React, { useEffect, useContext } from 'react'; | ||
import { ActivityIndicator, Alert, StyleSheet } from 'react-native'; | ||
import { useRoute, useTheme, useNavigation } from '@react-navigation/native'; | ||
import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; | ||
|
||
import { BlueStorageContext } from '../../blue_modules/storage-context'; | ||
import { SafeBlueArea } from '../../BlueComponents'; | ||
import navigationStyle from '../../components/navigationStyle'; | ||
import loc from '../../loc'; | ||
import AOPPClient from '../../class/aopp'; | ||
import selectWallet from '../../helpers/select-wallet'; | ||
|
||
const AOPP = () => { | ||
const { colors } = useTheme(); | ||
const navigation = useNavigation(); | ||
const { uri } = useRoute().params; | ||
const { name } = useRoute(); | ||
const { wallets } = useContext(BlueStorageContext); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
let aopp; | ||
try { | ||
aopp = new AOPPClient(uri); | ||
} catch (e) { | ||
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false }); | ||
Alert.alert(loc.errors.error, e.message); | ||
return navigation.pop(); | ||
} | ||
|
||
let availableWallets = wallets.filter(w => w.allowSignVerifyMessage()); | ||
if (aopp.format !== AOPPClient.typeAny) { | ||
const segwitType = AOPPClient.getSegwitByAddressFormat(aopp.format); | ||
availableWallets = availableWallets.filter(w => w.segwitType === segwitType); | ||
} | ||
|
||
const wallet = await selectWallet(navigation.navigate, name, false, availableWallets, 'Onchain wallet is required to sign a message'); | ||
if (!wallet) return navigation.pop(); | ||
|
||
const address = wallet.getAddressAsync ? await wallet.getAddressAsync() : wallet.getAddress(); | ||
navigation.navigate('SignVerify', { | ||
walletID: wallet.getID(), | ||
address, | ||
message: aopp.msg, | ||
aoppURI: uri, | ||
}); | ||
})(); | ||
}, []); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
return ( | ||
<SafeBlueArea style={[styles.center, { backgroundColor: colors.elevated }]}> | ||
<ActivityIndicator /> | ||
</SafeBlueArea> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
center: { | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
}); | ||
|
||
AOPP.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.aopp.title })); | ||
|
||
export default AOPP; |
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
Oops, something went wrong.