-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
61 lines (54 loc) · 2.08 KB
/
auth.go
File metadata and controls
61 lines (54 loc) · 2.08 KB
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
package gocalibur
import (
"crypto/ecdsa"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
)
// CreateAuthorization creates an unsigned EIP-7702 authorization for delegating to Calibur.
func CreateAuthorization(chainID *big.Int, caliburAddr common.Address, nonce uint64) types.SetCodeAuthorization {
chainID256, _ := uint256.FromBig(chainID)
return types.SetCodeAuthorization{
ChainID: *chainID256,
Address: caliburAddr,
Nonce: nonce,
}
}
// SignAuthorization signs an EIP-7702 authorization.
func SignAuthorization(auth types.SetCodeAuthorization, key *ecdsa.PrivateKey) (types.SetCodeAuthorization, error) {
return types.SignSetCode(key, auth)
}
// CreateAndSignAuthorization creates and signs an EIP-7702 authorization in one step.
func CreateAndSignAuthorization(key *ecdsa.PrivateKey, chainID *big.Int, caliburAddr common.Address, nonce uint64) (types.SetCodeAuthorization, error) {
auth := CreateAuthorization(chainID, caliburAddr, nonce)
return SignAuthorization(auth, key)
}
// SignSecp256k1 signs a SignedBatchedCall with secp256k1 and returns a wrapped signature
// ready for Calibur's execute function. This is the most common signing flow.
//
// Usage:
//
// domain := gocalibur.CaliburDomain(chainID, userAddr, caliburAddr)
// sig, err := gocalibur.SignSecp256k1(privateKey, domain, signedCall, nil)
func SignSecp256k1(
privateKey *ecdsa.PrivateKey,
domain EIP712Domain,
signedCall SignedBatchedCall,
hookData []byte,
) ([]byte, error) {
digest := ComputeDigest(domain, signedCall)
sig, err := crypto.Sign(digest[:], privateKey)
if err != nil {
return nil, err
}
// crypto.Sign returns V as 0 or 1, but Solidity's ecrecover requires 27 or 28
sig[64] += 27
return WrapSignature(sig, hookData), nil
}
// GetKeyHashForAddress returns the key hash for a secp256k1 address (root key).
// For root keys, this is keccak256(KeyTypeSecp256k1 || left-padded address).
func GetKeyHashForAddress(addr common.Address) [32]byte {
return NewSecp256k1Key(addr).KeyHash()
}