-
Notifications
You must be signed in to change notification settings - Fork 126
/
signer.go
80 lines (67 loc) · 2.66 KB
/
signer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package userop
import (
"fmt"
"log/slog"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/layer-3/clearsync/pkg/signer"
)
func SignerForBiconomy(ecdsaSigner signer.Signer) Signer {
return func(op UserOperation, entryPoint common.Address, chainID *big.Int) ([]byte, error) {
slog.Debug("signing user operation")
hash, err := op.UserOpHash(entryPoint, chainID)
if err != nil {
return nil, fmt.Errorf("failed to calculate user operation hash: %w", err)
}
signature, err := signer.SignEthMessage(ecdsaSigner, hash.Bytes())
if err != nil {
return nil, fmt.Errorf("failed to sign user operation: %w", err)
}
// The address is correct for all chains as for Biconomy v2.0
// see https://docs.biconomy.io/contracts
ecdsaOwnershipValidationModuleAddress := common.HexToAddress("0x0000001c5b32F37F5beA87BDD5374eB2aC54eA8e")
args := abi.Arguments{
{Type: bytes},
{Type: address},
}
// Pack the signature and the ecdsaOwnershipValidationModuleAddress
// to be used as the signature for the user operation
// See more: https://github.com/bcnmy/scw-contracts/blob/v2-deployments/contracts/smart-account/SmartAccount.sol#L337
fullSignature, err := args.Pack(signature, ecdsaOwnershipValidationModuleAddress)
if err != nil {
return nil, fmt.Errorf("failed to pack signature: %w", err)
}
slog.Debug("signed user operation for Biconomy",
"signature", hexutil.Encode(fullSignature),
"hash", hash.String())
return fullSignature, nil
}
}
func SignerForKernel(ecdsaSigner signer.Signer) Signer {
return func(op UserOperation, entryPoint common.Address, chainID *big.Int) ([]byte, error) {
// FIXME: not with session key
slog.Debug("signing user operation with session key")
hash, err := op.UserOpHash(entryPoint, chainID)
if err != nil {
return nil, fmt.Errorf("failed to calculate user operation hash: %w", err)
}
signature, err := signer.SignEthMessage(ecdsaSigner, hash.Bytes())
if err != nil {
return nil, fmt.Errorf("failed to sign user operation: %w", err)
}
// Add 'use sudo validator' mode to signature
// See more: https://github.com/zerodevapp/kernel/blob/807b75a4da6fea6311a3573bc8b8964a34074d94/src/Kernel.sol#L127
modifiedSig := strings.Replace(signature.String(), "0x", "0x00000000", 1)
fullSignature, err := hexutil.Decode(modifiedSig)
if err != nil {
return nil, fmt.Errorf("failed to decode signature: %w", err)
}
slog.Debug("signed user operation with session key for Kernel",
"signature", hexutil.Encode(fullSignature),
"hash", hash.String())
return fullSignature, nil
}
}