|
| 1 | +import {formatAssetAmount} from "../views/util"; |
| 2 | + |
| 3 | +export const getSupply = (asset, t) => { |
| 4 | + let { chain_stats = {}, mempool_stats = {} } = asset |
| 5 | + let has_blinded_issuances = |
| 6 | + chain_stats.has_blinded_issuances || mempool_stats.has_blinded_issuances |
| 7 | + let is_native_asset = !asset.issuance_txin |
| 8 | + let circulating = is_native_asset |
| 9 | + ? chain_stats.peg_in_amount + |
| 10 | + mempool_stats.peg_in_amount - |
| 11 | + chain_stats.peg_out_amount - |
| 12 | + mempool_stats.peg_out_amount - |
| 13 | + chain_stats.burned_amount - |
| 14 | + mempool_stats.burned_amount |
| 15 | + : has_blinded_issuances |
| 16 | + ? null |
| 17 | + : chain_stats.issued_amount + |
| 18 | + mempool_stats.issued_amount - |
| 19 | + chain_stats.burned_amount - |
| 20 | + mempool_stats.burned_amount; |
| 21 | + |
| 22 | + let totalSupply = circulating == null ? t`Confidential` |
| 23 | + : formatAssetAmount(circulating, asset.precision, t) |
| 24 | + return totalSupply |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +// Simplicity helpers (Elements only) |
| 29 | + |
| 30 | +// Helper to check if witness has an annex (per BIP 341) |
| 31 | +const hasAnnex = (witness) => witness && witness.length >= 2 && witness[witness.length - 1].startsWith('50') |
| 32 | + |
| 33 | +// Get the control block element from a witness stack (accounting for optional annex) |
| 34 | +const getControlBlock = witness => { |
| 35 | + if (!witness || witness.length < 3) return null |
| 36 | + const hasAnnexBlock = hasAnnex(witness) |
| 37 | + if (hasAnnexBlock) { |
| 38 | + return witness[witness.length - 2] |
| 39 | + } |
| 40 | + return witness[witness.length - 1] |
| 41 | +} |
| 42 | + |
| 43 | +export const isSimplicityTapleafVersion = (controlBlock) => { |
| 44 | + return controlBlock && (controlBlock.startsWith('be') || controlBlock.startsWith('bf')) |
| 45 | +} |
| 46 | + |
| 47 | +// Check if a vin is a P2TR Simplicity spend |
| 48 | +// A P2TR Simplicity spend has: |
| 49 | +// - 4 witness elements (or 5 with optional annex) |
| 50 | +// - is a simplicity Tapleaf |
| 51 | +export const isSimplicitySpend = (vin) => { |
| 52 | + if (!process.env.IS_ELEMENTS || !vin.witness) return false |
| 53 | + |
| 54 | + const witnessLen = vin.witness.length |
| 55 | + const hasAnnexBlock = hasAnnex(vin.witness) |
| 56 | + |
| 57 | + // Must be 4 elements without annex, or 5 elements with annex |
| 58 | + if (witnessLen !== 4 && !(witnessLen === 5 && hasAnnexBlock)) return false |
| 59 | + |
| 60 | + const controlBlock = getControlBlock(vin.witness) |
| 61 | + return isSimplicityTapleafVersion(controlBlock) |
| 62 | +} |
| 63 | + |
| 64 | +// Extract Simplicity witness components from a vin |
| 65 | +// Returns: { witnessData, program, cmr, controlBlock, annex } |
| 66 | +export const getSimplicityWitness = (vin) => { |
| 67 | + if (!isSimplicitySpend(vin)) return null |
| 68 | + |
| 69 | + const witness = vin.witness |
| 70 | + const hasAnnexBlock = hasAnnex(witness) |
| 71 | + |
| 72 | + return { |
| 73 | + witnessData: witness[0], |
| 74 | + program: witness[1], |
| 75 | + cmr: witness[2], |
| 76 | + controlBlock: witness[3], |
| 77 | + annex: hasAnnexBlock && witness.length === 5 ? witness[4] : null |
| 78 | + } |
| 79 | +} |
0 commit comments