Skip to content

Commit 66f06b4

Browse files
authored
Merge pull request #154 from MeshJS/feature/CBOR-Integration-wallet-import-api
improve fallback logic for deriving reward addresses
2 parents 352dcf2 + 3ce4b75 commit 66f06b4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/utils/validateMultisigImport.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ export async function validateMultisigImportPayload(payload: unknown): Promise<V
425425
if (m?.matched && typeof m.signerIndex === "number") {
426426
const idx = m.signerIndex;
427427
const hex = (signerStakeKeys[idx] || "").toLowerCase();
428-
return hex && /^[0-9a-f]{56}$/.test(hex) ? hex : (stakeSigKeyHashes[i] || "").toLowerCase();
428+
return hex && /^[0-9a-f]{56}$/.test(hex) ? hex : "";
429429
}
430-
// Fallback to stake script's key hash at same position, or empty string
431-
return (stakeSigKeyHashes[i] || "").toLowerCase();
430+
// Fallback to empty string when no aligned stake key is available
431+
return "";
432432
});
433433
// Recompute stakeAddressesUsed to stay positionally aligned with the reordered stake keys
434434
stakeAddressesUsedFinal = signerStakeKeysOrdered.map((hex) => {
@@ -459,7 +459,7 @@ export async function validateMultisigImportPayload(payload: unknown): Promise<V
459459
// Fallback: use the keyHash itself where no stake key was provided
460460
return sig.toLowerCase();
461461
});
462-
// Build stakeAddressesUsed positionally: reward addresses for matched keys, else raw key hash placeholders
462+
// Build stakeAddressesUsed positionally: reward addresses for matched keys, else derive from script key hash
463463
stakeAddressesUsedFinal = stakeSigKeyHashes.map((sig) => {
464464
const m = lowerMatches.get(sig.toLowerCase());
465465
const providedStakeHex = m?.signerStakeKey;
@@ -481,8 +481,22 @@ export async function validateMultisigImportPayload(payload: unknown): Promise<V
481481
// If we couldn't build a valid reward address, fall back to the hex itself
482482
return hex;
483483
}
484-
// No match: use the script key hash in this position
485-
return sig.toLowerCase();
484+
// No match: attempt to build a reward address directly from the script key hash
485+
const hex = sig.toLowerCase();
486+
const tryNetworks: Array<0 | 1> = network === undefined ? [0, 1] : [network as 0 | 1];
487+
for (const netId of tryNetworks) {
488+
try {
489+
const addr = serializeRewardAddress(hex, false, netId);
490+
const resolved = stakeKeyHash(addr)?.toLowerCase();
491+
if (resolved === hex) {
492+
return addr;
493+
}
494+
} catch {
495+
// continue to next
496+
}
497+
}
498+
// If unable to derive, fall back to the raw key hash
499+
return hex;
486500
});
487501
}
488502

0 commit comments

Comments
 (0)