Skip to content

Instantly share code, notes, and snippets.

@blluv
Created July 24, 2024 15:25
Show Gist options
  • Save blluv/f80fdb2b541e862acf20bc4cfa4bc3ae to your computer and use it in GitHub Desktop.
Save blluv/f80fdb2b541e862acf20bc4cfa4bc3ae to your computer and use it in GitHub Desktop.
0x15F1FCFD63656620BF58E2347B3504D6B92C70ED로 1wei를 보내는 코드
import rlp
import secp256k1
from Crypto.Hash import keccak
import requests
GWEI = 1000000000
PRIV_KEY = bytes.fromhex(
""
)
MY_ADDR = "0x"
#####
ecdsa = secp256k1.ECDSA()
def keccak256(data: bytes):
k = keccak.new(digest_bits=256)
k.update(data)
return k.digest()
def getEthereumAccountAddress(pubKey: secp256k1.PublicKey):
# uncompressed, 0x04 제거, 하위 20바이트
return keccak256(pubKey.serialize(False)[1:])[-20:].hex()
def sign(priv: secp256k1.PrivateKey, data: bytes, chainId):
sig = priv.ecdsa_sign_recoverable(keccak256(data), True)
A, v = priv.ecdsa_recoverable_serialize(sig)
assert len(A) == 64
# EIP155 (LegacyTx)
v += chainId * 2 + 35
r = A[:32]
s = A[32:]
return v, r, s
def ecrecover(msg, v, r, s, chainId):
v -= 35
v -= chainId * 2
sig = ecdsa.ecdsa_recoverable_deserialize(r + s, v)
pubKey = ecdsa.ecdsa_recover(keccak256(msg), sig, True)
pubKey = secp256k1.PublicKey(pubKey)
return pubKey
def ecrecoverTx(nonce, gasPrice, gasLimit, to, value, data, v, r, s, chainId):
# EIP155 (LegacyTx)
tx = rlp.encode([nonce, gasPrice, gasLimit, to, value, data, chainId, 0, 0])
return ecrecover(tx, v, r, s, chainId)
def makeLegacyTx(priv, nonce, gasPrice, gasLimit, to, value, data, chainId):
tx = rlp.encode([nonce, gasPrice, gasLimit, to, value, data, chainId, 0, 0])
v, r, s = sign(priv, tx, chainId)
return rlp.encode([nonce, gasPrice, gasLimit, to, value, data, v, r, s])
#####
#####
def getNonce(add):
res = requests.get(
"https://rpc2.sepolia.org",
json={
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [add, "latest"],
"id": 1,
},
).json()
return int(res["result"], 16)
#####
pk = secp256k1.PrivateKey(PRIV_KEY)
nonce = getNonce(MY_ADDR)
tx = makeLegacyTx(
pk,
nonce,
2 * GWEI,
21000,
0x15F1FCFD63656620BF58E2347B3504D6B92C70ED,
1,
0,
0xAA36A7,
)
res = requests.get(
"https://rpc2.sepolia.org",
json={
"jsonrpc": "2.0",
"method": "eth_sendRawTransaction",
"params": ["0x" + tx.hex()],
"id": 1,
},
)
print(res.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment