fix(wallet): read a PSBT's key fingerprint as unsigned
bitcoin-kmp reads a taproot derivation's fingerprint back from a serialised PSBT as a signed int, so for any account whose fingerprint has its top bit set -- half of them -- every member but the proposer refused every spend as not the wallet's own. Compare the fingerprint as an unsigned 32-bit value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e7a1ad260f
commit
634f51f915
2 files changed
+76
-3
No files matched your search
@@ -169,7 +169,7 @@ object GroupSpend {
|
||||
// 5. Change is change only if it derives.
|
||||
val outputs = psbt.outputs.mapIndexed { index, output ->
|
||||
val txOut = tx.txOut[index]
|
||||
val ours = output.taprootDerivationPaths.values.filter { it.masterKeyFingerprint == account.fingerprint }
|
||||
val ours = output.taprootDerivationPaths.values.filter { isOurs(it, account) }
|
||||
val change = if (ours.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
@@ -204,7 +204,7 @@ object GroupSpend {
|
||||
fun claimedChangeIndices(psbt: Psbt, account: GroupWalletDerivation.Account): Set<Long> =
|
||||
psbt.outputs
|
||||
.flatMap { it.taprootDerivationPaths.values }
|
||||
.filter { it.masterKeyFingerprint == account.fingerprint }
|
||||
.filter { isOurs(it, account) }
|
||||
.mapNotNull { path -> path.keyPath.path.takeIf { it.size == 2 && it[0] == GroupWalletDerivation.CHANGE }?.get(1) }
|
||||
.toSet()
|
||||
|
||||
@@ -219,7 +219,7 @@ object GroupSpend {
|
||||
paths: Map<fr.acinq.bitcoin.XonlyPublicKey, TaprootBip32DerivationPath>,
|
||||
account: GroupWalletDerivation.Account,
|
||||
): GroupWalletDerivation.Derived? {
|
||||
val (internalKey, path) = paths.entries.singleOrNull { it.value.masterKeyFingerprint == account.fingerprint }
|
||||
val (internalKey, path) = paths.entries.singleOrNull { isOurs(it.value, account) }
|
||||
?.toPair()
|
||||
?: return null
|
||||
if (path.leaves.isNotEmpty()) return null
|
||||
@@ -230,6 +230,18 @@ object GroupSpend {
|
||||
return GroupWalletDerivation.derive(account, change, index).takeIf { it.internalKey == internalKey }
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether [path] names [account] as its master key.
|
||||
*
|
||||
* Compared as an unsigned 32-bit value. bitcoin-kmp reads a taproot
|
||||
* derivation's fingerprint back from a serialised PSBT as a *signed* int, so
|
||||
* an account whose fingerprint has its top bit set -- half of them -- would
|
||||
* write `3578966523` and read back `-716000773`, and every member but the
|
||||
* proposer would refuse every spend from that wallet as not its own.
|
||||
*/
|
||||
private fun isOurs(path: TaprootBip32DerivationPath, account: GroupWalletDerivation.Account): Boolean =
|
||||
(path.masterKeyFingerprint and 0xFFFFFFFFL) == account.fingerprint
|
||||
|
||||
/**
|
||||
* The PSBT a proposer sends: [tx] with every input's spent output and
|
||||
* derivation, and every change output's derivation, so that a signer is
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.machankura.bennu.compose.wallet
|
||||
|
||||
import com.machankura.bennu.compose.managers.GroupWalletDerivation
|
||||
import fr.acinq.bitcoin.ByteVector
|
||||
import fr.acinq.bitcoin.ByteVector32
|
||||
import fr.acinq.bitcoin.Chain
|
||||
import fr.acinq.bitcoin.OutPoint
|
||||
import fr.acinq.bitcoin.PrivateKey
|
||||
import fr.acinq.bitcoin.Satoshi
|
||||
import fr.acinq.bitcoin.Script
|
||||
import fr.acinq.bitcoin.Transaction
|
||||
import fr.acinq.bitcoin.TxId
|
||||
import fr.acinq.bitcoin.TxIn
|
||||
import fr.acinq.bitcoin.TxOut
|
||||
import fr.acinq.secp256k1.Hex
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertIs
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* A spend checked the way every member but the proposer checks it: after the
|
||||
* PSBT has been through the wire.
|
||||
*
|
||||
* Half of all accounts have a fingerprint with its top bit set, and bitcoin-kmp
|
||||
* reads a taproot derivation's fingerprint back from a serialised PSBT as a
|
||||
* signed int. Compared as written, every such wallet's spends were refused by
|
||||
* every member who did not build them.
|
||||
*/
|
||||
class GroupSpendFingerprintTest {
|
||||
/** The first account, counting up from a fixed seed, whose fingerprint has its top bit set. */
|
||||
private val account: GroupWalletDerivation.Account = generateSequence(1) { it + 1 }
|
||||
.map { seed ->
|
||||
val key = PrivateKey(ByteVector32(Hex.decode(seed.toString(16).padStart(64, '0')))).publicKey().toString()
|
||||
GroupWalletDerivation.account(key, Chain.Regtest)
|
||||
}
|
||||
.first { it.fingerprint >= 0x80000000L }
|
||||
|
||||
@Test
|
||||
fun `a spend from a wallet with a high fingerprint checks out after the wire`() {
|
||||
val coin = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, 0)
|
||||
val change = GroupWalletDerivation.derive(account, GroupWalletDerivation.CHANGE, 0)
|
||||
val funding = Transaction(
|
||||
2, listOf(TxIn(OutPoint(TxId(ByteVector32(ByteArray(32) { 0x51 })), 0), ByteVector.empty, 0xffffffffL)),
|
||||
listOf(TxOut(Satoshi(50_000), coin.pubkeyScript)), 0,
|
||||
)
|
||||
val elsewhere = ByteVector(Script.write(Script.pay2wpkh(PrivateKey(ByteVector32(ByteArray(32) { 9 })).publicKey())))
|
||||
val tx = Transaction(
|
||||
2, listOf(TxIn(OutPoint(funding, 0), ByteVector.empty, GroupSpend.MAX_SEQUENCE)),
|
||||
listOf(TxOut(Satoshi(30_000), elsewhere), TxOut(Satoshi(19_000), change.pubkeyScript)), 0,
|
||||
)
|
||||
val proposed = GroupSpend.psbtOf(tx, listOf(funding.txOut[0] to coin), mapOf(1 to change), account)
|
||||
|
||||
assertTrue(account.fingerprint >= 0x80000000L)
|
||||
val received = GroupSpend.decode(GroupSpend.encode(proposed))!!
|
||||
|
||||
val checked = assertIs<GroupSpend.Result.Ok>(GroupSpend.check(received, account)).checked
|
||||
assertEquals(listOf(false, true), checked.outputs.map { it.isChange })
|
||||
assertEquals(setOf(0L), GroupSpend.claimedChangeIndices(received, account))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user