-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from klkvr/klkvr/examples
feat: EIP-7702 + Secp256r1 example
- Loading branch information
Showing
5 changed files
with
161 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,7 @@ docs/ | |
|
||
# Dotenv file | ||
.env | ||
|
||
# Key files | ||
private.pem | ||
public.pem |
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,53 @@ | ||
import sys | ||
import subprocess | ||
|
||
args = sys.argv[1:] | ||
if args[0] == "gen": | ||
subprocess.run( | ||
"openssl ecparam -name prime256v1 -genkey -noout -out private.pem", | ||
shell=True, | ||
stdout=subprocess.DEVNULL, | ||
stderr=subprocess.DEVNULL, | ||
) | ||
subprocess.run( | ||
"openssl ec -in private.pem -pubout -out public.pem", | ||
shell=True, | ||
stdout=subprocess.DEVNULL, | ||
stderr=subprocess.DEVNULL, | ||
) | ||
data = ( | ||
subprocess.check_output( | ||
"openssl ec -in private.pem -text", shell=True, stderr=subprocess.DEVNULL | ||
) | ||
.decode() | ||
.rstrip() | ||
.replace("\n", "") | ||
.replace(" ", "") | ||
) | ||
priv = data.split("priv:")[1].split("pub:")[0].replace(":", "") | ||
pub = data.split("pub:")[1].split("ASN1")[0].replace(":", "")[2:] | ||
pub_x, pub_y = pub[:64], pub[64:] | ||
|
||
print(f"Private key: 0x{priv}") | ||
print(f"Public key: 0x{pub_x}, 0x{pub_y}") | ||
elif args[0] == "sign": | ||
payload = bytearray.fromhex(args[1].replace("0x", "")) | ||
|
||
proc = subprocess.Popen( | ||
["openssl", "dgst", "-keccak-256", "-sign", "private.pem"], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
) | ||
output = proc.communicate(payload)[0] | ||
proc = subprocess.Popen( | ||
["openssl", "asn1parse", "-inform", "DER"], | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, | ||
) | ||
output = proc.communicate(output)[0].decode().replace(" ", "").replace("\n", "") | ||
|
||
sig_r = output.split("INTEGER:")[1][:64] | ||
sig_s = output.split("INTEGER:")[2][:64] | ||
|
||
print(f"Signature r: 0x{sig_r}") | ||
print(f"Signature s: 0x{sig_s}") |
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,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.23; | ||
|
||
import {Secp256r1} from "./sign/Secp256r1.sol"; | ||
|
||
/// @notice Contract designed for being delegated to by EOAs to authorize a secp256r1 key to transact on their behalf. | ||
contract P256Delegation { | ||
/// @notice The x coordinate of the authorized public key | ||
uint256 authorizedPublicKeyX; | ||
/// @notice The y coordinate of the authorized public key | ||
uint256 authorizedPublicKeyY; | ||
|
||
/// @notice Internal nonce used for replay protection, must be tracked and included into prehashed message. | ||
uint256 public nonce; | ||
|
||
/// @notice Authorizes provided public key to transact on behalf of this account. Only callable by EOA itself. | ||
function authorize(uint256 publicKeyX, uint256 publicKeyY) public { | ||
require(msg.sender == address(this)); | ||
|
||
authorizedPublicKeyX = publicKeyX; | ||
authorizedPublicKeyY = publicKeyY; | ||
} | ||
|
||
/// @notice Main entrypoint for authorized transactions. Accepts transaction parameters (to, data, value) and a secp256r1 signature. | ||
function transact(address to, bytes memory data, uint256 value, bytes32 r, bytes32 s) public { | ||
bytes32 digest = keccak256(abi.encode(nonce++, to, data, value)); | ||
require(Secp256r1.verify(digest, r, s, authorizedPublicKeyX, authorizedPublicKeyY), "Invalid signature"); | ||
|
||
(bool success,) = to.call{value: value}(data); | ||
require(success); | ||
} | ||
} |
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