Skip to content

Commit

Permalink
add vk check (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored Apr 2, 2024
1 parent b435845 commit a44ef4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions go/common/viewingkey/viewing_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ type RPCSignedViewingKey struct {
SignatureWithAccountKey []byte
}

const (
pubKeyLen = 33
sigLen = 65
)

func (vk RPCSignedViewingKey) Validate() error {
// todo - remove this when merging to main
if vk.Account == nil {
return fmt.Errorf("invalid account in viewing key")
}
if len(vk.PublicKey) != pubKeyLen {
return fmt.Errorf("invalid viewing key")
}
if len(vk.SignatureWithAccountKey) != sigLen {
return fmt.Errorf("invalid viewing key signature")
}
return nil
}

// GenerateViewingKeyForWallet takes an account wallet, generates a viewing key and signs the key with the acc's private key
// uses the same method of signature handling as Metamask/geth
func GenerateViewingKeyForWallet(wal wallet.Wallet) (*ViewingKey, error) {
Expand Down
5 changes: 5 additions & 0 deletions go/enclave/vkhandler/vk_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type AuthenticatedViewingKey struct {
}

func VerifyViewingKey(rpcVK *viewingkey.RPCSignedViewingKey, chainID int64) (*AuthenticatedViewingKey, error) {
err := rpcVK.Validate()
if err != nil {
return nil, err
}

vkPubKey, err := crypto.DecompressPubkey(rpcVK.PublicKey)
if err != nil {
return nil, fmt.Errorf("could not decompress viewing key bytes - %w", err)
Expand Down

0 comments on commit a44ef4c

Please sign in to comment.