diff options
Diffstat (limited to 'src/ark/refresh.js')
| -rw-r--r-- | src/ark/refresh.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/ark/refresh.js b/src/ark/refresh.js index 6e4453d..44c8321 100644 --- a/src/ark/refresh.js +++ b/src/ark/refresh.js @@ -355,6 +355,32 @@ export async function roundParticipationStatus(ark, unlockHash) { return out; } +// The round's new coins, checked before we cosign their leaves and forfeit +// our inputs. The forfeits hand the server our inputs in exchange for the +// preimage of OUR unlock hash, so a new coin locked by any other hash could +// never be unlocked: forfeiting for it gives the money away. Port of bark +// !2803 (server-0.9.0's client fix, reported by MrHodl: "Bark didn't check +// that the unlock hash of its forfeits matched the one locking its new +// VTXOs"), plus what bark's check_round_matches_participation also asks: +// every coin a hark leaf anchored in this round's transaction and paid to +// our key, and together at least what we asked for. Throws on the first +// mismatch; nothing is signed then. +export function checkRoundOutputs(vtxos, { unlockHash, fundingTxid, ownerPubkey, minTotalSat = 0 }) { + if (!vtxos.length) throw new Error('the round returned no coins for this participation'); + let total = 0; + for (const v of vtxos) { + const last = v.genesis[v.genesis.length - 1]; + const t = last && last.transition; + if (!t || !['hashLockedCosigned', 'hashLockedCosigned_v0'].includes(t.type)) throw new Error(`new coin ${v.id} is not a hash-locked round leaf`); + const lockedBy = t.unlock && (t.unlock.hash ?? (t.unlock.preimage != null ? sha256Hex(t.unlock.preimage) : null)); + if (lockedBy !== unlockHash) throw new Error(`new coin ${v.id} is locked by unlock hash ${lockedBy}, expected ${unlockHash}`); + if (v.anchorPoint.txid !== fundingTxid) throw new Error(`new coin ${v.id} is anchored in ${v.anchorPoint.txid}, not this round's ${fundingTxid}`); + if (v.policy.type !== 'pubkey' || v.policy.userPubkey !== ownerPubkey) throw new Error(`new coin ${v.id} is not paid to our key`); + total += v.amountSat; + } + if (total < minTotalSat) throw new Error(`the round's new coins hold ${total} sat, ${minTotalSat} were asked for`); +} + // MuSig2 script-spend cosign of the hash-locked leaf (NO taptweak) export async function cosignHarkLeaf(ark, vtxo, fundingTx, vtxoKeys, serverPub) { const last = vtxo.genesis[vtxo.genesis.length - 1]; |
