charms-js
Version:
TypeScript SDK for decoding Bitcoin transactions containing Charms data
44 lines (43 loc) • 1.5 kB
JavaScript
/**
* Wallet adapter for charms-js
* Filters CharmObj objects by wallet outpoints
*/
/**
* Convert CharmObj to wallet-compatible format (filtering by wallet outpoints)
*/
export function normalizeCharmForWallet(charmObj, txId, walletOutpoints, utxoValue) {
const outIndex = charmObj.outputIndex;
const belongsToWallet = outIndex !== undefined && walletOutpoints.has(`${txId}:${outIndex}`);
if (!belongsToWallet) {
return null;
}
// Return the CharmObj as-is since it's already in the correct format
return charmObj;
}
/**
* Extract charms for wallet with built-in normalization
*/
export async function extractCharmsForWallet(txHex, txId, walletOutpoints, network = 'testnet4') {
// Import here to avoid circular dependencies
const { extractAndVerifySpell } = await import('../node.js');
try {
const extractionResult = await extractAndVerifySpell(txHex, network);
if (!extractionResult.success) {
return [];
}
if (!extractionResult.charms || extractionResult.charms.length === 0) {
return [];
}
const processedCharms = [];
for (const charmObj of extractionResult.charms) {
const processedCharm = normalizeCharmForWallet(charmObj, txId, walletOutpoints, 0);
if (processedCharm) {
processedCharms.push(processedCharm);
}
}
return processedCharms;
}
catch (error) {
return [];
}
}