forked from Layr-Labs/eigenda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
55 lines (44 loc) · 1.48 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package core
import (
"bytes"
"errors"
"fmt"
geth "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
type BlobRequestAuthenticator interface {
AuthenticateBlobRequest(header BlobAuthHeader) error
}
type BlobRequestSigner interface {
SignBlobRequest(header BlobAuthHeader) ([]byte, error)
GetAccountID() (string, error)
}
func VerifySignature(message []byte, accountAddr geth.Address, sig []byte) error {
// Ensure the signature is 65 bytes (Recovery ID is the last byte)
if len(sig) != 65 {
return fmt.Errorf("signature length is unexpected: %d", len(sig))
}
publicKeyBytes, err := hexutil.Decode(accountAddr.Hex())
if err != nil {
return fmt.Errorf("failed to decode public key (%v): %v", accountAddr.Hex(), err)
}
// Decode public key
pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
if err != nil {
return fmt.Errorf("failed to decode public key (%v): %v", accountAddr.Hex(), err)
}
// Verify the signature
sigPublicKeyECDSA, err := crypto.SigToPub(message, sig)
if err != nil {
return fmt.Errorf("failed to recover public key from signature: %v", err)
}
if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) {
return errors.New("signature doesn't match with provided public key")
}
return nil
}
type PaymentSigner interface {
SignBlobPayment(header *PaymentMetadata) ([]byte, error)
GetAccountID() string
}