-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
67 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/0xsequence/ethkit/ethwallet" | ||
"github.com/0xsequence/ethkit/go-ethereum/common" | ||
"github.com/0xsequence/go-sequence" | ||
proto_wallet "github.com/0xsequence/waas-authenticator/proto/waas" | ||
"github.com/0xsequence/waas-authenticator/rpc/tenant" | ||
) | ||
|
||
func (s *RPC) signSessionAuthProof( | ||
ctx context.Context, | ||
userWallet string, | ||
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(userWallet), 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 | ||
} |