diff options
| author | Fuwn <[email protected]> | 2026-02-26 15:41:45 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-26 15:41:45 -0800 |
| commit | fec9114caaa7d274e524793d5eb0cf2ef2c5af11 (patch) | |
| tree | 0897394ccdfaf6633e1a4ca8eb02bff49bb93c00 /internal/verify/verifier_test.go | |
| parent | feat: add read-only PLC API compatibility endpoints (diff) | |
| download | plutia-test-fec9114caaa7d274e524793d5eb0cf2ef2c5af11.tar.xz plutia-test-fec9114caaa7d274e524793d5eb0cf2ef2c5af11.zip | |
feat: Apply Iku formatting
Diffstat (limited to 'internal/verify/verifier_test.go')
| -rw-r--r-- | internal/verify/verifier_test.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/internal/verify/verifier_test.go b/internal/verify/verifier_test.go index 0b38411..e4f46fa 100644 --- a/internal/verify/verifier_test.go +++ b/internal/verify/verifier_test.go @@ -9,7 +9,6 @@ import ( "encoding/base64" "encoding/json" "testing" - "github.com/Fuwn/plutia/internal/config" "github.com/Fuwn/plutia/internal/types" "github.com/decred/dcrd/dcrec/secp256k1/v4" @@ -20,9 +19,11 @@ import ( func TestVerifyOperationValidSignature(t *testing.T) { pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { t.Fatalf("generate key: %v", err) } + payloadDoc := []byte(`{"did":"did:plc:alice","didDoc":{"id":"did:plc:alice"}}`) sig := ed25519.Sign(priv, payloadDoc) opJSON := map[string]any{ @@ -38,10 +39,13 @@ func TestVerifyOperationValidSignature(t *testing.T) { DID: "did:plc:alice", Operation: raw, }) + if err != nil { t.Fatalf("parse operation: %v", err) } + v := New(config.VerifyFull) + if err := v.VerifyOperation(op, nil); err != nil { t.Fatalf("verify operation: %v", err) } @@ -49,9 +53,11 @@ func TestVerifyOperationValidSignature(t *testing.T) { func TestVerifyOperationPrevMismatch(t *testing.T) { pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { t.Fatalf("generate key: %v", err) } + payloadDoc := []byte(`{"did":"did:plc:alice","didDoc":{"id":"did:plc:alice"},"prev":"sha256:wrong"}`) sig := ed25519.Sign(priv, payloadDoc) opJSON := map[string]any{ @@ -64,11 +70,14 @@ func TestVerifyOperationPrevMismatch(t *testing.T) { } raw, _ := json.Marshal(opJSON) op, err := types.ParseOperation(types.ExportRecord{Seq: 2, DID: "did:plc:alice", Operation: raw}) + if err != nil { t.Fatalf("parse operation: %v", err) } + v := New(config.VerifyFull) existing := &types.StateV1{DID: "did:plc:alice", ChainTipHash: "sha256:right", LatestOpSeq: 1} + if err := v.VerifyOperation(op, existing); err == nil { t.Fatalf("expected prev mismatch error") } @@ -76,13 +85,14 @@ func TestVerifyOperationPrevMismatch(t *testing.T) { func TestVerifyOperationSecp256k1(t *testing.T) { priv, err := secp256k1.GeneratePrivateKey() + if err != nil { t.Fatalf("generate secp256k1 key: %v", err) } + pubKey := priv.PubKey() didKeyBytes := append([]byte{0xE7, 0x01}, pubKey.SerializeCompressed()...) didKey := "did:key:z" + base58.Encode(didKeyBytes) - unsigned := map[string]any{ "type": "create", "prev": nil, @@ -91,16 +101,19 @@ func TestVerifyOperationSecp256k1(t *testing.T) { "signingKey": didKey, } enc, err := cbor.CanonicalEncOptions().EncMode() + if err != nil { t.Fatalf("init cbor encoder: %v", err) } + payload, err := enc.Marshal(unsigned) + if err != nil { t.Fatalf("marshal cbor: %v", err) } + sum := sha256.Sum256(payload) sig := secpECDSA.Sign(priv, sum[:]).Serialize() - opJSON := map[string]any{ "type": "create", "prev": nil, @@ -115,10 +128,13 @@ func TestVerifyOperationSecp256k1(t *testing.T) { DID: "did:plc:alice", Operation: raw, }) + if err != nil { t.Fatalf("parse operation: %v", err) } + v := New(config.VerifyFull) + if err := v.VerifyOperation(op, nil); err != nil { t.Fatalf("verify operation: %v", err) } @@ -126,13 +142,14 @@ func TestVerifyOperationSecp256k1(t *testing.T) { func TestVerifyOperationP256(t *testing.T) { priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { t.Fatalf("generate p256 key: %v", err) } + compressed := elliptic.MarshalCompressed(priv.Curve, priv.PublicKey.X, priv.PublicKey.Y) didKeyBytes := append([]byte{0x80, 0x24}, compressed...) didKey := "did:key:z" + base58.Encode(didKeyBytes) - unsigned := map[string]any{ "type": "create", "prev": nil, @@ -141,21 +158,28 @@ func TestVerifyOperationP256(t *testing.T) { "signingKey": didKey, } enc, err := cbor.CanonicalEncOptions().EncMode() + if err != nil { t.Fatalf("init cbor encoder: %v", err) } + payload, err := enc.Marshal(unsigned) + if err != nil { t.Fatalf("marshal cbor: %v", err) } + sum := sha256.Sum256(payload) r, s, err := ecdsa.Sign(rand.Reader, priv, sum[:]) + if err != nil { t.Fatalf("sign p256: %v", err) } + sig := make([]byte, 64) rb := r.Bytes() sb := s.Bytes() + copy(sig[32-len(rb):32], rb) copy(sig[64-len(sb):], sb) @@ -173,10 +197,13 @@ func TestVerifyOperationP256(t *testing.T) { DID: "did:plc:alice", Operation: raw, }) + if err != nil { t.Fatalf("parse operation: %v", err) } + v := New(config.VerifyFull) + if err := v.VerifyOperation(op, nil); err != nil { t.Fatalf("verify operation: %v", err) } |