commit 6c1d4ca7adca4b34b07263e1c4802866c106ea29
parent aa09283930b59044ef422678c5c01c09714586e9
Author: Shivakumar <[email protected]>
Date: Sat, 1 Aug 2026 03:56:00 +0530
feat(blockchain): add KeyResolver as an Option for now.
Signed-off-by: Shivakumar <[email protected]>
Diffstat:
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/internal/blockchain/blockchain.go b/internal/blockchain/blockchain.go
@@ -36,6 +36,7 @@ var (
ErrInvalidHash = errors.New("blockchain: invalid hash length")
ErrUnsignedTx = errors.New("blockchain: cannot add block with unsigned transaction")
ErrInvalidTxID = errors.New("blockchain: transaction has invalid TxID")
+ ErrInvalidSignature = errors.New("blockchain: invalid transaction signature")
)
type KeyResolver interface {
@@ -50,6 +51,7 @@ type Blockchain struct {
store storage.Store
lastHash []byte
height uint64
+ resolver KeyResolver
}
// Option defines a function signature for configuring the Blockchain.
@@ -57,6 +59,7 @@ type Option func(*config)
type config struct {
genesisTime int64
+ keyResolver KeyResolver
}
// WithGenesisTime sets a custom timestamp for the Genesis block.
@@ -67,6 +70,13 @@ func WithGenesisTime(t int64) Option {
}
}
+// WithKeyResolver configures a resolver used to verify transaction signature during AddBlock.
+func WithKeyResolver(r KeyResolver) Option {
+ return func(c *config) {
+ c.keyResolver = r
+ }
+}
+
// New initializes a new Blockchain instance.
// If the store is empty, it creates and persists the Genesis block.
func New(store storage.Store, opts ...Option) (*Blockchain, error) {
@@ -139,6 +149,7 @@ func New(store storage.Store, opts ...Option) (*Blockchain, error) {
store: store,
lastHash: bytes.Clone(lastHash),
height: lastBlock.Header.Height,
+ resolver: cfg.keyResolver,
}, nil
}
@@ -184,14 +195,27 @@ func (bc *Blockchain) AddBlock(txs []*transaction.Transaction) (*Block, error) {
if tx == nil {
return nil, ErrNilTransactionEntry
}
-
if !tx.IsSigned() {
return nil, ErrUnsignedTx
}
-
if len(tx.TxID) != crypto.HashSize {
return nil, ErrInvalidTxID
}
+
+ if bc.resolver != nil {
+ pubKey, err := bc.resolver.Get(tx.IssuerDID)
+ if err != nil {
+ return nil, fmt.Errorf("blockchain: failed to resolve issuer key for did %s: %w",
+ tx.IssuerDID, err)
+ }
+ valid, err := tx.Verify(pubKey)
+ if err != nil {
+ return nil, fmt.Errorf("blockchain: failed to verify singnature: %w", err)
+ }
+ if !valid {
+ return nil, ErrInvalidSignature
+ }
+ }
}
bc.mu.Lock()