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() {
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) {

View file

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

View file

@ -365,6 +365,14 @@ describe('unit - DeepLinkSchemaMatch', function () {
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', () => {
assert.deepStrictEqual(
DeeplinkSchemaMatch.decodeBitcoinUri(