Merge pull request #4179 from BlueWallet/receiveundefined

FIX: 0 amounts in custom receive would result in undefined
This commit is contained in:
GLaDOS 2021-11-09 11:10:13 +00:00 committed by GitHub
commit acf6c7949b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 15 deletions

View file

@ -446,7 +446,16 @@ class DeeplinkSchemaMatch {
} }
static bip21encode() { static bip21encode() {
return bip21.encode.apply(bip21, arguments); const argumentsArray = Array.from(arguments);
for (const argument of argumentsArray) {
if (String(argument.label).replace(' ', '').length === 0) {
delete argument.label;
}
if (!(Number(argument.amount) > 0)) {
delete argument.amount;
}
}
return bip21.encode.apply(bip21, argumentsArray);
} }
static decodeBitcoinUri(uri) { static decodeBitcoinUri(uri) {

View file

@ -311,12 +311,16 @@ const ReceiveDetails = () => {
<View style={stylesHook.scrollBody}> <View style={stylesHook.scrollBody}>
{isCustom && ( {isCustom && (
<> <>
{getDisplayAmount() && (
<BlueText testID="CustomAmountText" style={stylesHook.amount} numberOfLines={1}> <BlueText testID="CustomAmountText" style={stylesHook.amount} numberOfLines={1}>
{getDisplayAmount()} {getDisplayAmount()}
</BlueText> </BlueText>
)}
{customLabel?.length > 0 && (
<BlueText testID="CustomAmountDescriptionText" style={stylesHook.label} numberOfLines={1}> <BlueText testID="CustomAmountDescriptionText" style={stylesHook.label} numberOfLines={1}>
{customLabel} {customLabel}
</BlueText> </BlueText>
)}
</> </>
)} )}
@ -493,6 +497,7 @@ const ReceiveDetails = () => {
* @returns {string} BTC amount, accounting for current `customUnit` and `customUnit` * @returns {string} BTC amount, accounting for current `customUnit` and `customUnit`
*/ */
const getDisplayAmount = () => { const getDisplayAmount = () => {
if (Number(customAmount) > 0) {
switch (customUnit) { switch (customUnit) {
case BitcoinUnit.BTC: case BitcoinUnit.BTC:
return customAmount + ' BTC'; return customAmount + ' BTC';
@ -502,6 +507,9 @@ const ReceiveDetails = () => {
return currency.fiatToBTC(customAmount) + ' BTC'; return currency.fiatToBTC(customAmount) + ' BTC';
} }
return customAmount + ' ' + customUnit; return customAmount + ' ' + customUnit;
} else {
return null;
}
}; };
return ( return (

View file

@ -365,6 +365,14 @@ describe('unit - DeepLinkSchemaMatch', function () {
assert.strictEqual(encoded, 'bitcoin:1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH?amount=20.3&label=Foobar'); assert.strictEqual(encoded, 'bitcoin:1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH?amount=20.3&label=Foobar');
}); });
it('encodes bip21 and discards empty arguments', () => {
const encoded = DeeplinkSchemaMatch.bip21encode('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', {
label: ' ',
amoount: undefined,
});
assert.strictEqual(encoded, 'bitcoin:1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
});
it('can decodeBitcoinUri', () => { it('can decodeBitcoinUri', () => {
assert.deepStrictEqual( assert.deepStrictEqual(
DeeplinkSchemaMatch.decodeBitcoinUri( DeeplinkSchemaMatch.decodeBitcoinUri(