Skip to content

Commit

Permalink
move signSessionAuthProof to sessions_auth_proof.go
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 committed Mar 21, 2024
1 parent 7b3fa37 commit 63ebc66
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 55 deletions.
46 changes: 46 additions & 0 deletions rpc/sessions_auth_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"fmt"

"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/go-sequence"
"github.com/0xsequence/go-sequence/intents"
"github.com/0xsequence/waas-authenticator/data"
"github.com/0xsequence/waas-authenticator/proto"
proto_wallet "github.com/0xsequence/waas-authenticator/proto/waas"
"github.com/0xsequence/waas-authenticator/rpc/tenant"
)

func (s *RPC) sessionAuthProof(
Expand Down Expand Up @@ -39,3 +42,46 @@ func (s *RPC) sessionAuthProof(

return convertIntentResponse(res), nil
}

func (s *RPC) signSessionAuthProof(
ctx context.Context,
proof *proto_wallet.SessionAuthProof,
) error {
tntData := tenant.FromContext(ctx)

parentWallet, err := ethwallet.NewWalletFromPrivateKey(tntData.PrivateKey)
if err != nil {
return fmt.Errorf("recovering parent wallet: %w", err)
}

// Make sure the message is EIP191 encoded
msgBytes := sequence.MessageToEIP191(common.FromHex(proof.Message.Message))

// Validate that message match intent
digest := sequence.MessageDigest(msgBytes)

chainID, ok := sequence.ParseHexOrDec(proof.Message.ChainID)
if !ok {
return fmt.Errorf("invalid chain id: %s", proof.Message.ChainID)
}

subdigest, err := sequence.SubDigest(chainID, common.HexToAddress(proof.Wallet), digest)
if err != nil {
return fmt.Errorf("calculating digest: %w", err)
}

// Our EOA belongs to the *parent* wallet, so we need to sign the subdigest with the parent key
sig, parentSubdigest, err := s.signUsingParent(parentWallet, tntData.ParentAddress, subdigest, chainID)
if err != nil {
return fmt.Errorf("signing subdigest using parent wallet: %w", err)
}

proof.Signatures = []*proto_wallet.ProvidedSignature{
{
Digest: "0x" + common.Bytes2Hex(parentSubdigest),
Signature: "0x" + common.Bytes2Hex(sig),
Address: parentWallet.Address().String(),
},
}
return nil
}
55 changes: 0 additions & 55 deletions rpc/sign_session_auth_proof.go

This file was deleted.

0 comments on commit 63ebc66

Please sign in to comment.