transaction_test.go (9585B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package transaction 16 17 import ( 18 "bytes" 19 "crypto/ed25519" 20 "encoding/hex" 21 "testing" 22 23 "certichain/internal/crypto" 24 ) 25 26 // TestTransactionLifecycle validates the standard "happy path" of a transaction: 27 // creation, signing, verification, and serialization/deserialization. 28 func TestTransactionLifecycle(t *testing.T) { 29 t.Parallel() 30 31 // Setup 32 pub, priv, err := crypto.GenerateKeyPair() 33 if err != nil { 34 t.Fatalf("failed to generate key: %v", err) 35 } 36 37 // Generate a valid DID 38 issuer, err := crypto.NewDID(pub) 39 if err != nil { 40 t.Fatalf("failed to generate DID: %v", err) 41 } 42 43 // Create a dummy Merkle root. 44 root := make([]byte, 32) 45 for i := range root { 46 root[i] = byte(i) 47 } 48 49 // Test Creation 50 tx, err := New(issuer, root) 51 if err != nil { 52 t.Fatalf("New() failed: %v", err) 53 } 54 55 if tx.IssuerDID != issuer { 56 t.Errorf("expected issuer %s, got %s", issuer, tx.IssuerDID) 57 } 58 if !bytes.Equal(tx.MerkleRoot, root) { 59 t.Errorf("merkle root mismatch") 60 } 61 if len(tx.TxID) != 32 { 62 t.Errorf("invalid TxID length: %d", len(tx.TxID)) 63 } 64 if tx.IsSigned() { 65 t.Errorf("transaction should not be signed yet") 66 } 67 68 // Test Signing 69 err = tx.Sign(priv) 70 if err != nil { 71 t.Fatalf("Sign() failed: %v", err) 72 } 73 if !tx.IsSigned() { 74 t.Errorf("transaction should be signed") 75 } 76 77 // Test Verification 78 valid, err := tx.Verify(pub) 79 if err != nil { 80 t.Fatalf("Verify() errored: %v", err) 81 } 82 if !valid { 83 t.Errorf("Verify() returned false for valid transaction") 84 } 85 86 // Test Serialization 87 data, err := tx.Serialize() 88 if err != nil { 89 t.Fatalf("Serialize() failed: %v", err) 90 } 91 92 tx2, err := Deserialize(data) 93 if err != nil { 94 t.Fatalf("Deserialize() failed: %v", err) 95 } 96 97 // Verify that the restored transaction matches the original. 98 if !bytes.Equal(tx.TxID, tx2.TxID) { 99 t.Errorf("TxID mismatch after serialization") 100 } 101 if !bytes.Equal(tx.Signature, tx2.Signature) { 102 t.Errorf("Signature mismatch after serialization") 103 } 104 105 // Final verification of the deserialized transaction. 106 valid2, err := tx2.Verify(pub) 107 if err != nil { 108 t.Fatalf("Verify() on deserialized tx failed: %v", err) 109 } 110 if !valid2 { 111 t.Errorf("Verify() returned false for deserialized transaction") 112 } 113 } 114 115 // TestTransactionTampering ensures that any modification to a signed transaction's 116 // fields results in a verification failure. Each subtest uses a fresh transaction 117 // to avoid shared mutable state issues. 118 func TestTransactionTampering(t *testing.T) { 119 t.Parallel() 120 121 // Helper to create a signed transaction for tampering tests. 122 setup := func(t *testing.T) (ed25519.PublicKey, ed25519.PrivateKey, string, *Transaction) { 123 t.Helper() 124 pub, priv, err := crypto.GenerateKeyPair() 125 if err != nil { 126 t.Fatalf("failed to generate key: %v", err) 127 } 128 issuer, err := crypto.NewDID(pub) 129 if err != nil { 130 t.Fatalf("failed to generate DID: %v", err) 131 } 132 root := make([]byte, 32) 133 tx, err := New(issuer, root) 134 if err != nil { 135 t.Fatalf("New() failed: %v", err) 136 } 137 if err := tx.Sign(priv); err != nil { 138 t.Fatalf("Sign() failed: %v", err) 139 } 140 return pub, priv, issuer, tx 141 } 142 143 t.Run("TamperMerkleRoot", func(t *testing.T) { 144 t.Parallel() 145 pub, _, _, tx := setup(t) 146 tx.MerkleRoot[0] ^= 0xFF 147 valid, err := tx.Verify(pub) 148 if err != nil { 149 t.Fatalf("Verify() errored: %v", err) 150 } 151 if valid { 152 t.Errorf("Verify() should fail for tampered MerkleRoot") 153 } 154 }) 155 156 t.Run("TamperIssuer", func(t *testing.T) { 157 t.Parallel() 158 pub, _, _, tx := setup(t) 159 160 otherPub, _, err := crypto.GenerateKeyPair() 161 if err != nil { 162 t.Fatalf("failed to generate key: %v", err) 163 } 164 otherDID, err := crypto.NewDID(otherPub) 165 if err != nil { 166 t.Fatalf("failed to generate DID: %v", err) 167 } 168 tx.IssuerDID = otherDID 169 170 valid, err := tx.Verify(pub) 171 if err != nil { 172 t.Fatalf("Verify() errored: %v", err) 173 } 174 if valid { 175 t.Errorf("Verify() should fail for tampered IssuerDID") 176 } 177 }) 178 179 t.Run("TamperNonce", func(t *testing.T) { 180 t.Parallel() 181 pub, _, _, tx := setup(t) 182 tx.Nonce[0] ^= 0xFF 183 valid, err := tx.Verify(pub) 184 if err != nil { 185 t.Fatalf("Verify() errored: %v", err) 186 } 187 if valid { 188 t.Errorf("Verify() should fail for tampered Nonce") 189 } 190 }) 191 192 t.Run("TamperTimestamp", func(t *testing.T) { 193 t.Parallel() 194 pub, _, _, tx := setup(t) 195 tx.Timestamp += 1 196 valid, err := tx.Verify(pub) 197 if err != nil { 198 t.Fatalf("Verify() errored: %v", err) 199 } 200 if valid { 201 t.Errorf("Verify() should fail for tampered Timestamp") 202 } 203 }) 204 205 t.Run("TamperSignature", func(t *testing.T) { 206 t.Parallel() 207 pub, _, _, tx := setup(t) 208 tx.Signature[0] ^= 0xFF 209 valid, err := tx.Verify(pub) 210 if err != nil { 211 t.Fatalf("Verify() errored: %v", err) 212 } 213 if valid { 214 t.Errorf("Verify() should fail for tampered Signature") 215 } 216 }) 217 } 218 219 // TestInvalidInputs verifies the sanitization logic in the New() constructor. 220 func TestInvalidInputs(t *testing.T) { 221 t.Parallel() 222 223 t.Run("EmptyIssuer", func(t *testing.T) { 224 t.Parallel() 225 _, err := New("", make([]byte, 32)) 226 if err == nil { 227 t.Error("expected error for empty issuer") 228 } 229 }) 230 231 t.Run("InvalidDIDFormat", func(t *testing.T) { 232 t.Parallel() 233 _, err := New("invalid-did", make([]byte, 32)) 234 if err == nil { 235 t.Error("expected error for invalid DID format") 236 } 237 }) 238 239 t.Run("InvalidDIDLength", func(t *testing.T) { 240 t.Parallel() 241 shortHash := hex.EncodeToString(make([]byte, 31)) 242 _, err := New(crypto.DIDPrefix+shortHash, make([]byte, 32)) 243 if err == nil { 244 t.Error("expected error for short DID hash") 245 } 246 }) 247 248 t.Run("InvalidMerkleRootSize", func(t *testing.T) { 249 t.Parallel() 250 _, err := New("did:certchain:0x"+hex.EncodeToString(make([]byte, 32)), make([]byte, 31)) 251 if err == nil { 252 t.Error("expected error for invalid merkle root size") 253 } 254 }) 255 } 256 257 // TestNonceUniqueness verifies that two transactions created with the same inputs 258 // have different nonces and therefore different TxIDs, providing replay protection. 259 func TestNonceUniqueness(t *testing.T) { 260 t.Parallel() 261 262 pub, _, err := crypto.GenerateKeyPair() 263 if err != nil { 264 t.Fatalf("failed to generate key: %v", err) 265 } 266 issuer, err := crypto.NewDID(pub) 267 if err != nil { 268 t.Fatalf("failed to generate DID: %v", err) 269 } 270 root := make([]byte, 32) 271 272 tx1, err := New(issuer, root) 273 if err != nil { 274 t.Fatalf("New() 1 failed: %v", err) 275 } 276 277 tx2, err := New(issuer, root) 278 if err != nil { 279 t.Fatalf("New() 2 failed: %v", err) 280 } 281 282 if bytes.Equal(tx1.Nonce, tx2.Nonce) { 283 t.Error("nonces should be unique") 284 } 285 if bytes.Equal(tx1.TxID, tx2.TxID) { 286 t.Error("TxIDs should be unique") 287 } 288 } 289 290 // TestDoubleSignGuard verifies that Sign() cannot be called twice on the same transaction. 291 func TestDoubleSignGuard(t *testing.T) { 292 t.Parallel() 293 294 pub, priv, err := crypto.GenerateKeyPair() 295 if err != nil { 296 t.Fatalf("failed to generate key: %v", err) 297 } 298 issuer, err := crypto.NewDID(pub) 299 if err != nil { 300 t.Fatalf("failed to generate DID: %v", err) 301 } 302 tx, err := New(issuer, make([]byte, 32)) 303 if err != nil { 304 t.Fatalf("New() failed: %v", err) 305 } 306 307 if err := tx.Sign(priv); err != nil { 308 t.Fatalf("first Sign() failed: %v", err) 309 } 310 311 if err := tx.Sign(priv); err == nil { 312 t.Error("expected error on second Sign() call") 313 } 314 } 315 316 // TestVerify_WrongKey verifies that a valid signed transaction returns false (and no error) 317 // when verified against a different public key. 318 func TestVerify_WrongKey(t *testing.T) { 319 t.Parallel() 320 321 pubA, privA, err := crypto.GenerateKeyPair() 322 if err != nil { 323 t.Fatalf("failed to generate key A: %v", err) 324 } 325 issuerA, err := crypto.NewDID(pubA) 326 if err != nil { 327 t.Fatalf("failed to generate DID A: %v", err) 328 } 329 330 pubB, _, err := crypto.GenerateKeyPair() 331 if err != nil { 332 t.Fatalf("failed to generate key B: %v", err) 333 } 334 335 tx, err := New(issuerA, make([]byte, 32)) 336 if err != nil { 337 t.Fatalf("New() failed: %v", err) 338 } 339 if err := tx.Sign(privA); err != nil { 340 t.Fatalf("Sign() failed: %v", err) 341 } 342 343 valid, err := tx.Verify(pubB) 344 if err != nil { 345 t.Fatalf("Verify() errored: %v", err) 346 } 347 if valid { 348 t.Error("Verify() should return false when using the wrong public key") 349 } 350 } 351 352 // TestVerifyUnsigned verifies that Verify() returns false for a transaction that hasn't been signed. 353 func TestVerifyUnsigned(t *testing.T) { 354 t.Parallel() 355 356 pub, _, err := crypto.GenerateKeyPair() 357 if err != nil { 358 t.Fatalf("failed to generate key: %v", err) 359 } 360 issuer, err := crypto.NewDID(pub) 361 if err != nil { 362 t.Fatalf("failed to generate DID: %v", err) 363 } 364 tx, err := New(issuer, make([]byte, 32)) 365 if err != nil { 366 t.Fatalf("New() failed: %v", err) 367 } 368 369 valid, err := tx.Verify(pub) 370 if err != nil { 371 t.Fatalf("Verify() errored: %v", err) 372 } 373 if valid { 374 t.Error("Verify() should return false for unsigned transaction") 375 } 376 } 377 378 // TestDeserializeInvalidJSON verifies that Deserialize() returns an error for malformed JSON. 379 func TestDeserializeInvalidJSON(t *testing.T) { 380 t.Parallel() 381 382 _, err := Deserialize([]byte(`{invalid json`)) 383 if err == nil { 384 t.Error("expected error for invalid JSON") 385 } 386 }