SPB Git

Repository Public

BitcoinChangeAuditRegressionTests.swift 5.3 KB 99 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Tests/QyvoraWalletTests/BitcoinChangeAuditRegressionTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0512// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42@testable import QyvoraBitcoin43import QyvoraCore44import QyvoraMath45import Testing4647@testable import QyvoraWallet4849/// Regression test of QYV-SVC-066 (multi-chain audit, 2026-12-05): the Bitcoin path leaves the change output out of50/// the value ceiling of an unaudited build, so that output must be a script of the wallet's own change chain.51extension BitcoinWalletTests {52    @Test func aPlanWhoseChangePaysSomeoneElseIsNotSigned() async throws {53        let s = try await Self.scenario()54        defer { s.h.cleanup() }55        let service = s.h.service56        _ = try await service.syncBitcoin()57        let honest = try await service.prepareBitcoinSend(to: externalAddress(), amount: 20_000, feeRate: FeeRate(satPerVByte: 2))58        let changeIndex = try #require(honest.plan.changeIndex)59        let ownChange = honest.plan.transaction.outputs[changeIndex].scriptPubKey60        let foreign = try BitcoinAddress(externalAddress(0x33), network: .signet).scriptPubKey61        let own = try BitcoinAddress(try await s.h.change(7), network: .signet).scriptPubKey6263        func wrap(_ plan: BitcoinTransactionPlan, changeValue: UInt64? = nil) -> BitcoinSendPlan {64            BitcoinSendPlan(id: UUID(), purpose: honest.purpose, network: honest.network, createdAt: honest.createdAt, plan: plan, recipient: honest.recipient,65                            amount: honest.amount, fee: plan.fee, virtualSize: plan.estimatedVSize, targetFeeRate: plan.targetFeeRate,66                            effectiveFeeRate: plan.effectiveFeeRate, inputs: honest.inputs, outputs: honest.outputs, changeAddress: honest.changeAddress,67                            changeValue: changeValue ?? plan.changeValue, isSendMax: false, warnings: honest.warnings)68        }69        func build(payTo script: Bytes, change: Bytes) throws -> BitcoinTransactionPlan {70            var request = BitcoinSpendRequest(network: .signet, recipients: [.init(value: 20_000, scriptPubKey: script)], utxos: honest.plan.inputs,71                                              feeRate: honest.targetFeeRate, changeScript: change)72            request.seed = 773            return try BitcoinTransactionBuilder.build(request)74        }7576        // the "change" of the plan pays a foreign script: nothing is signed, nothing is broadcast77        let disguised = try build(payTo: own, change: foreign)78        #expect(disguised.changeIndex != nil)79        let before = s.h.backend.requests.count80        guard case .stalePlan(let message)? = await walletError({ try await service.signAndBroadcastBitcoin(wrap(disguised)) }) else {81            Issue.record("a plan whose change pays someone else was signed")82            return83        }84        #expect(message.contains("change"))85        #expect(s.h.backend.requests.count == before)86        // a change index outside the transaction is refused as well87        let outside = BitcoinTransactionPlan(transaction: disguised.transaction, inputs: disguised.inputs, changeIndex: 9, sendMaxIndex: nil, fee: disguised.fee,88                                             estimatedWeight: disguised.estimatedWeight, targetFeeRate: disguised.targetFeeRate, algorithm: disguised.algorithm,89                                             network: .signet)90        guard case .stalePlan? = await walletError({ try await service.signAndBroadcastBitcoin(wrap(outside, changeValue: 0)) }) else {91            Issue.record("a change index outside the transaction was accepted")92            return93        }94        // the same transaction shape with the wallet's own change script is accepted (the check refuses nothing honest)95        let rebuilt = try build(payTo: foreign, change: ownChange)96        _ = try await service.signAndBroadcastBitcoin(wrap(rebuilt))97    }98}99
Files main
Menu

Theme