Skip to content

Commit e855326

Browse files
committed
refactor(ecash): update background handlers, keyring and filters
1 parent 9699fd6 commit e855326

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

packages/extension/src/libs/background/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ class BackgroundHandler {
108108
error: JSON.stringify(getCustomError('Enkrypt: not implemented')),
109109
};
110110
}
111+
if (_provider === ProviderName.ecash) {
112+
return {
113+
error: JSON.stringify(
114+
getCustomError(
115+
'Enkrypt: eCash does not support external requests in this wallet',
116+
),
117+
),
118+
};
119+
}
111120
if (this.#geoRestricted !== undefined && this.#geoRestricted) {
112121
return {
113122
error: JSON.stringify(

packages/extension/src/libs/background/internal/ecash-sign.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ const ecashSign = async (
3838
return { error: getCustomError('ecash-sign: missing required parameters') };
3939
}
4040

41-
if (!isValidECashAddress(params.toAddress)) {
42-
return { error: getCustomError('ecash-sign: invalid destination address') };
43-
}
44-
4541
if (keyring.isLocked()) {
4642
return { error: getCustomError('ecash-sign: keyring is locked') };
4743
}
@@ -66,9 +62,14 @@ const ecashSign = async (
6662
return { error: getCustomError('ecash-sign: unknown network') };
6763
}
6864

69-
privateKeyBuffer = await keyring.getPrivateKeyForECash(
70-
params.account,
71-
);
65+
const cashAddrPrefix = (network as any).cashAddrPrefix ?? 'ecash';
66+
if (!isValidECashAddress(params.toAddress, cashAddrPrefix)) {
67+
return {
68+
error: getCustomError('ecash-sign: invalid destination address'),
69+
};
70+
}
71+
72+
privateKeyBuffer = await keyring.getPrivateKeyForECash(params.account);
7273
pkBytes = new Uint8Array(privateKeyBuffer);
7374
const chronik = new ChronikClient([network.node]);
7475
const wallet = Wallet.fromSk(pkBytes, chronik);
@@ -97,8 +98,10 @@ const ecashSign = async (
9798
result.errors?.length ? result.errors.join(', ') : 'Broadcast failed',
9899
);
99100
}
100-
101-
const txid = result.broadcasted[0] || '';
101+
const txid = result.broadcasted[0];
102+
if (!txid) {
103+
throw new Error('Broadcast succeeded but no txid returned');
104+
}
102105
return { result: JSON.stringify({ txid }) };
103106
} catch (e: any) {
104107
console.error('[ecash-sign] Error:', e);

packages/extension/src/ui/action/utils/filters.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,27 @@ export const parseCurrency = (value: string | number): string => {
3939

4040
const notation = BigNumber(finalValue).gt(999999) ? 'compact' : 'standard';
4141

42-
let minimumFractionDigits = 2;
43-
let maximumFractionDigits = 2;
42+
const decimalStr = new BigNumber(finalValue).toFixed();
43+
const decimalPlaces = Math.min(
44+
(decimalStr.split('.')[1] ?? '').replace(/0+$/, '').length,
45+
8,
46+
);
4447

45-
if (finalValue > 0 && finalValue < 0.01) {
46-
minimumFractionDigits = 2;
47-
maximumFractionDigits = 8;
48-
} else if (finalValue >= 0.01 && finalValue < 1) {
49-
minimumFractionDigits = 2;
50-
maximumFractionDigits = 4;
51-
}
48+
const minimumFractionDigits = 2;
49+
const maximumFractionDigits = Math.max(2, decimalPlaces);
5250

53-
const formatted = new Intl.NumberFormat(locale, {
51+
const formatter = new Intl.NumberFormat(locale, {
5452
style: 'currency',
5553
currency: currency,
5654
notation,
5755
minimumFractionDigits,
5856
maximumFractionDigits,
59-
}).format(finalValue);
57+
});
58+
59+
const formatted = formatter.format(finalValue);
60+
const zeroFormatted = formatter.format(0);
6061

61-
return `${amount.lt(0.0000001) && amount.gt(0) ? '< ' : ''}${formatted}`;
62+
return `${finalValue > 0 && formatted === zeroFormatted ? '< ' : ''}${formatted}`;
6263
};
6364

6465
export const truncate = (value: string, length: number): string => {

packages/keyring/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,13 @@ class KeyRing {
426426
);
427427
}
428428

429-
return hexToBuffer(keypair.privateKey);
429+
const privKeyHex = keypair.privateKey;
430+
assert(
431+
/^(0x)?[0-9a-fA-F]{64}$/.test(privKeyHex),
432+
"Invalid private key format: expected 32-byte hex string",
433+
);
434+
435+
return hexToBuffer(privKeyHex);
430436
}
431437
}
432438

0 commit comments

Comments
 (0)