Skip to content

Commit 390ebd0

Browse files
committed
ask user to enter tpubs if testnet inscription
1 parent 225ee17 commit 390ebd0

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

src/components/Recover.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ const RECOVER_URL = import.meta.env.VITE_RECOVER_URL || 'https://api.multisigbac
1919
const ORD_URL = import.meta.env.VITE_ORD_URL || 'https://ordinals.com';
2020
const MEMPOOL_URL = import.meta.env.VITE_MEMPOOL_URL || 'https://mempool.space/testnet4';
2121

22+
interface InscriptionValue {
23+
value: string;
24+
isTestnet: boolean;
25+
}
26+
2227
export interface RecoverState {
2328
showXfps: boolean;
2429
xfp0: string;
2530
xfp1: string;
2631
xpubs: string[];
2732
input: string;
28-
inscriptions: string[];
33+
inscriptions: InscriptionValue[];
2934
selectedInscription: string;
3035
decryptedDescriptor: string;
3136
}
@@ -100,7 +105,7 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
100105
const newXpubs = xpubs;
101106
try {
102107
// Add xpubs if requiredSigs exceeds xpubs.length
103-
const { groupedEncryptedShares } = parseEncryptedDescriptor(value);
108+
const { groupedEncryptedShares } = parseEncryptedDescriptor(stripTestnetIndicator(value));
104109
const requiredSigs = groupedEncryptedShares.reduce((max, obj) =>
105110
Math.max(max, obj.requiredSigs
106111
), 1);
@@ -157,7 +162,7 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
157162
}
158163

159164
let requiredSigs = 1;
160-
const inscriptions: string[] = [];
165+
const inscriptions: InscriptionValue[] = [];
161166
const remainingInscriptionIds: string[] = [];
162167
for (let i = 0; i < inscriptionIds.length; i++) {
163168
try {
@@ -170,7 +175,10 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
170175
requiredSigs = Math.max(requiredSigs, g.requiredSigs);
171176
})
172177
}
173-
inscriptions.push(encryptedText);
178+
inscriptions.push({
179+
value: encryptedText,
180+
isTestnet: false,
181+
});
174182
} catch {
175183
remainingInscriptionIds.push(inscriptionIds[i]);
176184
}
@@ -189,7 +197,7 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
189197
const tx = Transaction.fromRaw(bytes, {
190198
allowUnknownOutputs: true,
191199
disableScriptCheck: true,
192-
})
200+
});
193201

194202
const txInscriptions: Inscription[] = [];
195203
for (let i = 0; i < tx.inputsLength; i++) {
@@ -214,7 +222,10 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
214222
requiredSigs = Math.max(requiredSigs, g.requiredSigs);
215223
})
216224
}
217-
inscriptions.push(encryptedText);
225+
inscriptions.push({
226+
value: encryptedText,
227+
isTestnet: true,
228+
});
218229
}
219230
} catch {
220231
// Skip if failed to fetch or if there's an error parsing the descriptor
@@ -238,7 +249,7 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
238249
showXfps: false,
239250
xpubs: newXpubs,
240251
inscriptions,
241-
selectedInscription: inscriptions[0],
252+
selectedInscription: displayInscription(inscriptions[0]),
242253
}));
243254
} catch (err) {
244255
setRecoverError(((err as Error)?.message || "unknown error"));
@@ -258,7 +269,10 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
258269
throw new Error('Please enter at least one xpub');
259270
}
260271

261-
const { descriptor, decryptedShares, requiredShares } = await decrypt(textToDecrypt, xpubs);
272+
const { descriptor, decryptedShares, requiredShares } = await decrypt(
273+
stripTestnetIndicator(textToDecrypt),
274+
xpubs
275+
);
262276

263277
setDecryptedShares(decryptedShares);
264278
setRequiredShares(requiredShares);
@@ -285,6 +299,16 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
285299
return `${split[0]})${split[2].substring(0,8)}...`
286300
}
287301

302+
const testnetText = '(testnet4) ';
303+
const stripTestnetIndicator = (value: string) => value.startsWith(testnetText) ? value.split(testnetText)[1] : value;
304+
305+
const displayInscription = (inscription: InscriptionValue, shorten: boolean = false) => {
306+
const text = shorten ? shortenInscription(inscription.value) : inscription.value;
307+
return (inscription.isTestnet) ? `${testnetText}${text}` : text;
308+
}
309+
310+
const xpubLabel = selectedInscription.startsWith(testnetText) ? 'tpub' : 'xpub';
311+
288312
return showXfps ? (
289313
<>
290314
<p className='text-sm'>
@@ -357,8 +381,8 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
357381
</SelectTrigger>
358382
<SelectContent className="w-full">
359383
{inscriptions.map((inscription, index) => (
360-
<SelectItem key={index} value={inscription}>
361-
{shortenInscription(inscription)}
384+
<SelectItem key={index} value={displayInscription(inscription)}>
385+
{displayInscription(inscription, /** shorten */ true)}
362386
</SelectItem>
363387
))}
364388
</SelectContent>
@@ -374,7 +398,7 @@ const Recover: React.FC<RecoverProps> = ({ state, setState }) => {
374398
{xpubs.map((xpub, index) => (
375399
<div key={index}>
376400
<Input
377-
placeholder={`Enter xpub ${index + 1}`}
401+
placeholder={`Enter ${xpubLabel} ${index + 1}`}
378402
value={xpub}
379403
onChange={(e) => updateXpub(index, e.target.value)}
380404
/>

0 commit comments

Comments
 (0)