2024-01-26 11:16:55 +00:00
|
|
|
from common import H_, await_result, unittest # isort:skip
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2021-03-30 08:57:07 +00:00
|
|
|
import storage.cache
|
2020-08-03 15:28:20 +00:00
|
|
|
from trezor import wire
|
2022-10-19 16:26:02 +00:00
|
|
|
from trezor.crypto import bip32
|
|
|
|
from trezor.crypto.curve import bip340, secp256k1
|
2021-01-13 14:14:06 +00:00
|
|
|
from trezor.crypto.hashlib import sha256
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.enums import InputScriptType, OutputScriptType
|
2023-06-28 10:58:54 +00:00
|
|
|
from trezor.messages import (
|
|
|
|
AuthorizeCoinJoin,
|
|
|
|
CoinJoinRequest,
|
|
|
|
SignTx,
|
|
|
|
TxInput,
|
|
|
|
TxOutput,
|
|
|
|
)
|
2021-01-13 14:14:06 +00:00
|
|
|
from trezor.utils import HashWriter
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2023-06-28 10:58:54 +00:00
|
|
|
from apps.bitcoin import writers
|
2022-10-27 16:06:26 +00:00
|
|
|
from apps.bitcoin.authorization import FEE_RATE_DECIMALS, CoinJoinAuthorization
|
2020-08-03 15:28:20 +00:00
|
|
|
from apps.bitcoin.sign_tx.approvers import CoinJoinApprover
|
2020-09-30 16:50:25 +00:00
|
|
|
from apps.bitcoin.sign_tx.bitcoin import Bitcoin
|
|
|
|
from apps.bitcoin.sign_tx.tx_info import TxInfo
|
2023-06-28 10:58:54 +00:00
|
|
|
from apps.common import coins
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestApprover(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2023-06-28 10:46:29 +00:00
|
|
|
self.coin = coins.by_name("Bitcoin")
|
2022-10-19 16:26:02 +00:00
|
|
|
self.fee_rate_percent = 0.3
|
2023-06-28 10:46:29 +00:00
|
|
|
self.no_fee_threshold = 1000000
|
|
|
|
self.min_registrable_amount = 5000
|
2021-01-13 14:14:06 +00:00
|
|
|
self.coordinator_name = "www.example.com"
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
# Private key for signing and masking CoinJoin requests.
|
|
|
|
# m/0h for "all all ... all" seed.
|
2023-06-28 10:46:29 +00:00
|
|
|
self.private_key = b"?S\ti\x8b\xc5o{,\xab\x03\x194\xea\xa8[_:\xeb\xdf\xce\xef\xe50\xf17D\x98`\xb9dj"
|
2022-10-19 16:26:02 +00:00
|
|
|
|
|
|
|
self.node = bip32.HDNode(
|
|
|
|
depth=0,
|
|
|
|
fingerprint=0,
|
|
|
|
child_num=0,
|
|
|
|
chain_code=bytearray(32),
|
|
|
|
private_key=b"\x01" * 32,
|
|
|
|
curve_name="secp256k1",
|
|
|
|
)
|
2023-06-28 10:46:29 +00:00
|
|
|
self.tweaked_node_pubkey = b"\x02" + bip340.tweak_public_key(
|
|
|
|
self.node.public_key()[1:]
|
|
|
|
)
|
2022-10-19 16:26:02 +00:00
|
|
|
|
2020-08-03 15:28:20 +00:00
|
|
|
self.msg_auth = AuthorizeCoinJoin(
|
2021-01-13 14:14:06 +00:00
|
|
|
coordinator=self.coordinator_name,
|
2022-03-15 18:29:39 +00:00
|
|
|
max_rounds=10,
|
2023-06-28 10:46:29 +00:00
|
|
|
max_coordinator_fee_rate=int(
|
|
|
|
self.fee_rate_percent * 10**FEE_RATE_DECIMALS
|
|
|
|
),
|
2022-03-15 18:29:39 +00:00
|
|
|
max_fee_per_kvbyte=7000,
|
2022-10-19 16:26:02 +00:00
|
|
|
address_n=[H_(10025), H_(0), H_(0), H_(1)],
|
2020-08-03 15:28:20 +00:00
|
|
|
coin_name=self.coin.coin_name,
|
2022-10-19 16:26:02 +00:00
|
|
|
script_type=InputScriptType.SPENDTAPROOT,
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
2021-03-30 08:57:07 +00:00
|
|
|
storage.cache.start_session()
|
2020-08-03 15:28:20 +00:00
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
def make_coinjoin_request(self, inputs):
|
|
|
|
mask_public_key = secp256k1.publickey(self.private_key)
|
|
|
|
coinjoin_flags = bytearray()
|
|
|
|
for txi in inputs:
|
2023-06-28 10:46:29 +00:00
|
|
|
shared_secret = secp256k1.multiply(
|
|
|
|
self.private_key, self.tweaked_node_pubkey
|
|
|
|
)[1:33]
|
2022-10-19 16:26:02 +00:00
|
|
|
h_mask = HashWriter(sha256())
|
|
|
|
writers.write_bytes_fixed(h_mask, shared_secret, 32)
|
|
|
|
writers.write_bytes_reversed(h_mask, txi.prev_hash, writers.TX_HASH_SIZE)
|
|
|
|
writers.write_uint32(h_mask, txi.prev_index)
|
|
|
|
mask = h_mask.get_digest()[0] & 1
|
|
|
|
signable = txi.script_type == InputScriptType.SPENDTAPROOT
|
|
|
|
txi.coinjoin_flags = signable ^ mask
|
|
|
|
coinjoin_flags.append(txi.coinjoin_flags)
|
|
|
|
|
|
|
|
# Compute CoinJoin request signature.
|
|
|
|
h_request = HashWriter(sha256(b"CJR1"))
|
2023-06-28 10:46:29 +00:00
|
|
|
writers.write_bytes_prefixed(h_request, self.coordinator_name.encode())
|
2022-10-19 16:26:02 +00:00
|
|
|
writers.write_uint32(h_request, self.coin.slip44)
|
2023-06-28 10:46:29 +00:00
|
|
|
writers.write_uint32(
|
|
|
|
h_request, int(self.fee_rate_percent * 10**FEE_RATE_DECIMALS)
|
|
|
|
)
|
2022-10-19 16:26:02 +00:00
|
|
|
writers.write_uint64(h_request, self.no_fee_threshold)
|
|
|
|
writers.write_uint64(h_request, self.min_registrable_amount)
|
|
|
|
writers.write_bytes_fixed(h_request, mask_public_key, 33)
|
|
|
|
writers.write_bytes_prefixed(h_request, coinjoin_flags)
|
|
|
|
writers.write_bytes_fixed(h_request, sha256().digest(), 32)
|
|
|
|
writers.write_bytes_fixed(h_request, sha256().digest(), 32)
|
|
|
|
signature = secp256k1.sign(self.private_key, h_request.get_digest())
|
|
|
|
|
|
|
|
return CoinJoinRequest(
|
2022-10-27 16:06:26 +00:00
|
|
|
fee_rate=int(self.fee_rate_percent * 10**FEE_RATE_DECIMALS),
|
2022-10-19 16:26:02 +00:00
|
|
|
no_fee_threshold=self.no_fee_threshold,
|
|
|
|
min_registrable_amount=self.min_registrable_amount,
|
|
|
|
mask_public_key=mask_public_key,
|
|
|
|
signature=signature,
|
|
|
|
)
|
|
|
|
|
2020-08-03 15:28:20 +00:00
|
|
|
def test_coinjoin_lots_of_inputs(self):
|
2022-10-19 16:26:02 +00:00
|
|
|
denomination = 10_000_000
|
|
|
|
coordinator_fee = int(self.fee_rate_percent / 100 * denomination)
|
2022-03-15 18:29:39 +00:00
|
|
|
fees = coordinator_fee + 500
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
# Other's inputs.
|
|
|
|
inputs = [
|
2020-09-23 11:08:26 +00:00
|
|
|
TxInput(
|
2022-10-19 16:26:02 +00:00
|
|
|
prev_hash=bytes(32),
|
2020-09-16 12:51:19 +00:00
|
|
|
prev_index=0,
|
2022-03-15 18:29:39 +00:00
|
|
|
amount=denomination,
|
2021-10-29 19:28:37 +00:00
|
|
|
script_pubkey=bytes(22),
|
2020-08-03 15:28:20 +00:00
|
|
|
script_type=InputScriptType.EXTERNAL,
|
2023-06-28 10:46:29 +00:00
|
|
|
sequence=0xFFFFFFFF,
|
2022-03-03 18:02:53 +00:00
|
|
|
witness="",
|
2023-06-28 10:46:29 +00:00
|
|
|
)
|
|
|
|
for i in range(99)
|
2020-08-03 15:28:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Our input.
|
2023-06-28 10:46:29 +00:00
|
|
|
inputs.insert(
|
|
|
|
30,
|
2020-09-23 11:08:26 +00:00
|
|
|
TxInput(
|
2022-10-19 16:26:02 +00:00
|
|
|
prev_hash=bytes(32),
|
2020-09-16 12:51:19 +00:00
|
|
|
prev_index=0,
|
2022-10-19 16:26:02 +00:00
|
|
|
address_n=[H_(10025), H_(0), H_(0), H_(1), 0, 1],
|
2022-03-15 18:29:39 +00:00
|
|
|
amount=denomination,
|
2022-10-19 16:26:02 +00:00
|
|
|
script_type=InputScriptType.SPENDTAPROOT,
|
2023-06-28 10:46:29 +00:00
|
|
|
sequence=0xFFFFFFFF,
|
|
|
|
),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Other's CoinJoined outputs.
|
|
|
|
outputs = [
|
2020-09-23 11:08:26 +00:00
|
|
|
TxOutput(
|
2021-01-13 14:14:06 +00:00
|
|
|
address="",
|
2023-06-28 10:46:29 +00:00
|
|
|
amount=denomination - fees,
|
2022-09-22 11:20:58 +00:00
|
|
|
script_type=OutputScriptType.PAYTOTAPROOT,
|
2021-01-13 14:14:06 +00:00
|
|
|
payment_req_index=0,
|
2023-06-28 10:46:29 +00:00
|
|
|
)
|
|
|
|
for i in range(99)
|
2020-08-03 15:28:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Our CoinJoined output.
|
|
|
|
outputs.insert(
|
|
|
|
40,
|
2020-09-23 11:08:26 +00:00
|
|
|
TxOutput(
|
2021-01-13 14:14:06 +00:00
|
|
|
address="",
|
2022-10-19 16:26:02 +00:00
|
|
|
address_n=[H_(10025), H_(0), H_(0), H_(1), 0, 2],
|
2023-06-28 10:46:29 +00:00
|
|
|
amount=denomination - fees,
|
2022-09-22 11:20:58 +00:00
|
|
|
script_type=OutputScriptType.PAYTOTAPROOT,
|
2021-01-13 14:14:06 +00:00
|
|
|
payment_req_index=0,
|
2023-06-28 10:46:29 +00:00
|
|
|
),
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Coordinator's output.
|
|
|
|
outputs.append(
|
2020-09-23 11:08:26 +00:00
|
|
|
TxOutput(
|
2021-01-13 14:14:06 +00:00
|
|
|
address="",
|
2022-03-15 18:29:39 +00:00
|
|
|
amount=coordinator_fee * len(outputs),
|
2022-09-22 11:20:58 +00:00
|
|
|
script_type=OutputScriptType.PAYTOTAPROOT,
|
2021-01-13 14:14:06 +00:00
|
|
|
payment_req_index=0,
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
coinjoin_req = self.make_coinjoin_request(inputs)
|
2023-06-28 10:46:29 +00:00
|
|
|
tx = SignTx(
|
|
|
|
outputs_count=len(outputs),
|
|
|
|
inputs_count=len(inputs),
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
lock_time=0,
|
|
|
|
coinjoin_request=coinjoin_req,
|
|
|
|
)
|
2021-03-30 08:57:07 +00:00
|
|
|
authorization = CoinJoinAuthorization(self.msg_auth)
|
2020-08-03 15:28:20 +00:00
|
|
|
approver = CoinJoinApprover(tx, self.coin, authorization)
|
2020-09-30 16:50:25 +00:00
|
|
|
signer = Bitcoin(tx, None, self.coin, approver)
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
for txi in inputs:
|
|
|
|
if txi.script_type == InputScriptType.EXTERNAL:
|
|
|
|
approver.add_external_input(txi)
|
|
|
|
else:
|
2022-10-19 16:26:02 +00:00
|
|
|
await_result(approver.add_internal_input(txi, self.node))
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
for txo in outputs:
|
|
|
|
if txo.address_n:
|
2022-09-22 11:20:58 +00:00
|
|
|
await_result(approver.add_change_output(txo, script_pubkey=bytes(22)))
|
2020-08-03 15:28:20 +00:00
|
|
|
else:
|
|
|
|
await_result(approver.add_external_output(txo, script_pubkey=bytes(22)))
|
|
|
|
|
2023-12-19 11:07:33 +00:00
|
|
|
await_result(approver.approve_tx(TxInfo(signer, tx), [], None))
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
def test_coinjoin_input_account_depth_mismatch(self):
|
2020-09-23 11:08:26 +00:00
|
|
|
txi = TxInput(
|
2022-10-19 16:26:02 +00:00
|
|
|
prev_hash=bytes(32),
|
2020-09-16 12:51:19 +00:00
|
|
|
prev_index=0,
|
2022-10-19 16:26:02 +00:00
|
|
|
address_n=[H_(10025), H_(0), H_(0), H_(1), 0],
|
2020-08-03 15:28:20 +00:00
|
|
|
amount=10000000,
|
2023-06-28 10:46:29 +00:00
|
|
|
script_type=InputScriptType.SPENDTAPROOT,
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
coinjoin_req = self.make_coinjoin_request([txi])
|
2023-06-28 10:46:29 +00:00
|
|
|
tx = SignTx(
|
|
|
|
outputs_count=201,
|
|
|
|
inputs_count=100,
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
lock_time=0,
|
|
|
|
coinjoin_request=coinjoin_req,
|
|
|
|
)
|
2021-03-30 08:57:07 +00:00
|
|
|
authorization = CoinJoinAuthorization(self.msg_auth)
|
2020-08-03 15:28:20 +00:00
|
|
|
approver = CoinJoinApprover(tx, self.coin, authorization)
|
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
with self.assertRaises(wire.ProcessError):
|
|
|
|
await_result(approver.add_internal_input(txi, self.node))
|
|
|
|
|
|
|
|
def test_coinjoin_input_account_path_mismatch(self):
|
2020-09-23 11:08:26 +00:00
|
|
|
txi = TxInput(
|
2022-10-19 16:26:02 +00:00
|
|
|
prev_hash=bytes(32),
|
2020-09-16 12:51:19 +00:00
|
|
|
prev_index=0,
|
2022-10-19 16:26:02 +00:00
|
|
|
address_n=[H_(10025), H_(0), H_(1), H_(1), 0, 0],
|
2020-08-03 15:28:20 +00:00
|
|
|
amount=10000000,
|
2023-06-28 10:46:29 +00:00
|
|
|
script_type=InputScriptType.SPENDTAPROOT,
|
2020-08-03 15:28:20 +00:00
|
|
|
)
|
|
|
|
|
2022-10-19 16:26:02 +00:00
|
|
|
coinjoin_req = self.make_coinjoin_request([txi])
|
2023-06-28 10:46:29 +00:00
|
|
|
tx = SignTx(
|
|
|
|
outputs_count=201,
|
|
|
|
inputs_count=100,
|
|
|
|
coin_name=self.coin.coin_name,
|
|
|
|
lock_time=0,
|
|
|
|
coinjoin_request=coinjoin_req,
|
|
|
|
)
|
2022-10-19 16:26:02 +00:00
|
|
|
authorization = CoinJoinAuthorization(self.msg_auth)
|
|
|
|
approver = CoinJoinApprover(tx, self.coin, authorization)
|
|
|
|
|
2020-08-03 15:28:20 +00:00
|
|
|
with self.assertRaises(wire.ProcessError):
|
2022-10-19 16:26:02 +00:00
|
|
|
await_result(approver.add_internal_input(txi, self.node))
|
2020-08-03 15:28:20 +00:00
|
|
|
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2020-08-03 15:28:20 +00:00
|
|
|
unittest.main()
|