|
| 1 | +package firmware |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// TODO: Add integration test for CoinPurchaseMemo once the firmware UI is implemented. |
| 12 | +// potentially in psbt_test.go |
| 13 | + |
| 14 | +func TestComputePaymentRequestSighash_CoinPurchaseMemo(t *testing.T) { |
| 15 | + // This test verifies that our Go sighash computation matches the firmware's Rust implementation. |
| 16 | + // The expected hash comes from the firmware test at: |
| 17 | + // bitbox02-firmware/src/rust/bitbox02-rust/src/hww/api/bitcoin/payment_request.rs:366-369 |
| 18 | + |
| 19 | + paymentRequest := &messages.BTCPaymentRequestRequest{ |
| 20 | + RecipientName: "Merchant", |
| 21 | + Memos: []*messages.BTCPaymentRequestRequest_Memo{ |
| 22 | + { |
| 23 | + Memo: &messages.BTCPaymentRequestRequest_Memo_CoinPurchaseMemo_{ |
| 24 | + CoinPurchaseMemo: &messages.BTCPaymentRequestRequest_Memo_CoinPurchaseMemo{ |
| 25 | + CoinType: 60, // Ethereum |
| 26 | + Amount: "0.25 ETH", |
| 27 | + Address: "0xabc1234567890", |
| 28 | + // address_derivation is intentionally nil, since it's not part of the sighash |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + Nonce: []byte{}, |
| 34 | + TotalAmount: 123456, |
| 35 | + Signature: []byte{}, |
| 36 | + } |
| 37 | + |
| 38 | + sighash, err := ComputePaymentRequestSighash( |
| 39 | + paymentRequest, |
| 40 | + 1, // SLIP-44 coin type for Bitcoin Testnet |
| 41 | + 123456, |
| 42 | + "tb1q2q0j6gmfxynj40p0kxsr9jkagcvgpuqvqynnup", |
| 43 | + ) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + expectedHash := "1806caf7c518aad69eb38f25fd418d507c6a3e01719a7d77be94cd50a2790872" |
| 47 | + actualHash := hex.EncodeToString(sighash) |
| 48 | + |
| 49 | + require.Equal(t, expectedHash, actualHash, "Sighash mismatch: Go implementation doesn't match firmware") |
| 50 | +} |
0 commit comments