Skip to content

Commit

Permalink
refactor passkey
Browse files Browse the repository at this point in the history
  • Loading branch information
fanhousanbu committed Aug 31, 2024
1 parent 342aec6 commit 0c4c594
Show file tree
Hide file tree
Showing 29 changed files with 3,355 additions and 355 deletions.
6 changes: 5 additions & 1 deletion conf/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
dbClient *gorm.DB
dbOnce = sync.Once{}
dbOnce = sync.Once{}
)

func GetDbClient() *gorm.DB {
Expand All @@ -20,6 +20,10 @@ func GetDbClient() *gorm.DB {
panic(err)
}
dbClient = configDBVar

if Environment.IsDevelopment() {
dbClient = dbClient.Debug()
}
})
return dbClient
}
126 changes: 88 additions & 38 deletions docs/passkey-er.drawio

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/webauthn-relay-test/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"license": "MIT",
"scripts": {
"dev": "next dev --turbo",
"build": "next build",
Expand Down
2,958 changes: 2,958 additions & 0 deletions example/webauthn-relay-test/yarn.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions internal/common_util/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package common_util
import (
"another_node/internal/community/account"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/crypto"
"testing"

"github.com/ethereum/go-ethereum/crypto"
)

func TestEthereumSignHexStr(t *testing.T) {
Expand All @@ -13,8 +14,8 @@ func TestEthereumSignHexStr(t *testing.T) {
if err != nil {
t.Fatal(err)
}
privateKeyStr := hdWallet.PrivateKey()
address := hdWallet.Address()
privateKeyStr := hdWallet.PrivateKey
address := hdWallet.Address
t.Logf("address: %s", address)
t.Logf("privateKeyStr: %s", privateKeyStr)
privateKeyECDSA, err := crypto.HexToECDSA(privateKeyStr)
Expand Down
32 changes: 6 additions & 26 deletions internal/community/account/hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,9 @@ type HierarchicalPath string
const HierarchicalPath_ETH HierarchicalPath = "m/44'/60'/0'/0/0"

type HdWallet struct {
mnemonic string
address string
privateKey string
}

func (w *HdWallet) Mnemonic() string {
return w.mnemonic
}

func (w *HdWallet) PrivateKey() string {
return w.privateKey
}

func (w *HdWallet) Address() string {
return w.address
}

func RecoverHdWallet(mnemonic, addr, prv *string) *HdWallet {
return &HdWallet{
mnemonic: *mnemonic,
address: *addr,
privateKey: *prv,
}
Mnemonic string `json:"mnemonic"`
Address string `json:"address"`
PrivateKey string `json:"privateKey"`
}

func NewHdWallet(hierarchicalPath HierarchicalPath) (*HdWallet, error) {
Expand Down Expand Up @@ -62,8 +42,8 @@ func NewHdWallet(hierarchicalPath HierarchicalPath) (*HdWallet, error) {
}

return &HdWallet{
mnemonic: mnemonic,
address: account.Address.Hex(),
privateKey: privateKey,
Mnemonic: mnemonic,
Address: account.Address.Hex(),
PrivateKey: privateKey,
}, nil
}
6 changes: 3 additions & 3 deletions internal/community/account/hdwallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestNewHdWallet(t *testing.T) {
return
}

if wallet.mnemonic == "" {
if wallet.Mnemonic == "" {
t.Error("expected mnemonic to be set, but got empty string")
}

if wallet.address == "" {
if wallet.Address == "" {
t.Error("expected address to be set, but got empty string")
}

if len(wallet.privateKey) == 0 {
if len(wallet.PrivateKey) == 0 {
t.Error("expected privateKey to be set, but got empty slice")
}
}
2 changes: 1 addition & 1 deletion internal/community/account/impl/alchemy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a *AlchemyProvider) GetRpc() string {

func (a *AlchemyProvider) CreateAccount(wallet *account.HdWallet) (string, error) {

pk := "0x" + wallet.PrivateKey()
pk := "0x" + wallet.PrivateKey

eth := big.NewFloat(0.01)
wei := new(big.Float)
Expand Down
4 changes: 2 additions & 2 deletions internal/community/chain/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func init() {

}
func CreateSmartAccount(wallet *account.HdWallet, network seedworks.Chain) (accountAddress string, initCodeStr string, err error) {
pk := "0x" + wallet.PrivateKey()
pk := "0x" + wallet.PrivateKey
networkConfig := conf.GetNetworkConfigByNetwork(network)
if networkConfig == nil {
return "", "", xerrors.Errorf("Failed to get network config for network: %s", network)
Expand All @@ -81,7 +81,7 @@ func CreateSmartAccount(wallet *account.HdWallet, network seedworks.Chain) (acco
if err != nil {
return "", "", err
}
eoaAddress := common.HexToAddress(wallet.Address())
eoaAddress := common.HexToAddress(wallet.Address)
factoryAddress := common.HexToAddress(factoryAddressStr)
initCodeStr, err = getAccountInitCode(eoaAddress, factoryAddress, salt)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/community/chain/account_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package chain

import (
"another_node/conf"
"another_node/internal/community/account"
"another_node/internal/seedworks"
"os"
"testing"
)

func TestCreateAccount(t *testing.T) {
os.Setenv("Env", "dev")
defer os.Unsetenv("Env")

conf.Environment.Name = "dev"

if testing.Short() {
return
}

os.Chdir("../../../")

w, err := account.NewHdWallet(account.HierarchicalPath(account.HierarchicalPath_ETH))
if err != nil {
t.Errorf("Failed to create account: %v", err)
Expand Down
14 changes: 13 additions & 1 deletion plugins/passkey_relay_party/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ func (relay *RelayParty) accountInfo(ctx *gin.Context) {
chain = consts.OptimismSepolia
}

if initCode, addr, eoaAddr, err := relay.db.GetAccounts(email, string(chain)); err != nil {
if user, err := relay.db.FindUser(email); err != nil {
response.NotFound(ctx, err.Error())
} else {
initCode, aaAddr, eoaAddr := user.GetChainAddresses(chain)
response.GetResponse().WithDataSuccess(ctx, seedworks.AccountInfo{
InitCode: *initCode,
AA: *aaAddr,
EOA: *eoaAddr,
Email: email,
})
}

if initCode, addr, eoaAddr, err := relay.db.GetAccountsByEmail(email, string(chain)); err != nil {
response.NotFound(ctx, err.Error())
} else {
response.GetResponse().WithDataSuccess(ctx, seedworks.AccountInfo{
Expand Down
5 changes: 3 additions & 2 deletions plugins/passkey_relay_party/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package plugin_passkey_relay_party

import (
"another_node/plugins/passkey_relay_party/seedworks"
"another_node/plugins/passkey_relay_party/storage/model"
"strings"
)

func (rp *RelayParty) emailStartChallenge(mail, acceptLanguage string) error {

captcha := seedworks.GenCaptcha(6)

if err := rp.db.SaveChallenge(mail, captcha); err != nil {
if err := rp.db.SaveChallenge(model.Email, mail, captcha); err != nil {
return err
}

Expand Down Expand Up @@ -49,7 +50,7 @@ Invalidate in <b>10</b> minutes, ignore it if you were confused about this mail<

func (rp *RelayParty) emailChallenge(mail, code string) error {
if !strings.HasSuffix(mail, "@aastar.org") && code != "111111" {
if !rp.db.Challenge(mail, code) {
if !rp.db.Challenge(model.Email, mail, code) {
return seedworks.ErrInvalidCaptcha{}
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/passkey_relay_party/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *RelayParty) findUserByEmail(email string) (*seedworks.User, error) {
return nil, seedworks.ErrEmailEmpty{}
}

u, err := r.db.Find(email)
u, err := r.db.FindUser(email)

if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
Expand Down
40 changes: 0 additions & 40 deletions plugins/passkey_relay_party/seedworks/encrypt_test.go

This file was deleted.

12 changes: 12 additions & 0 deletions plugins/passkey_relay_party/seedworks/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ type ErrUserNotFound struct{}
type ErrEmailEmpty struct{}
type ErrInvalidCaptcha struct{}
type ErrUserAlreadyExists struct{}
type ErrWalletNotFound struct{}
type ErrChainNotFound struct{}

var _ error = ErrUserNotFound{}
var _ error = ErrEmailEmpty{}
var _ error = ErrInvalidCaptcha{}
var _ error = ErrUserAlreadyExists{}
var _ error = ErrWalletNotFound{}
var _ error = ErrChainNotFound{}

func (e ErrUserNotFound) Error() string {
return string("user not found")
Expand All @@ -25,3 +29,11 @@ func (e ErrInvalidCaptcha) Error() string {
func (e ErrUserAlreadyExists) Error() string {
return string("user already exists")
}

func (e ErrWalletNotFound) Error() string {
return string("wallet not found")
}

func (e ErrChainNotFound) Error() string {
return string("chain not found")
}
Loading

0 comments on commit 0c4c594

Please sign in to comment.