Files
to_session/ton/test/text.py
Administrator a0720d80dc fefdwef
2025-11-12 12:54:37 +08:00

31 lines
1.2 KiB
Python

from bip_utils import Bip39SeedGenerator, Bip39MnemonicGenerator, Bip44, Bip44Coins
from ecdsa import SECP256k1, SigningKey
import hashlib
# 1. 从助记词生成种子
mnemonic = "right mechanic pony notable wine bind night surround degree resist flat armed absurd uphold original give thrive tone patrol patch absent tuition shift else" # 替换为你的助记词
seed = Bip39SeedGenerator(mnemonic).Generate()
# 2. 从种子生成 BIP44 钱包
bip44_mst = Bip44.FromSeed(seed, Bip44Coins.TON) # 使用 TON 钱包
bip44_acc = bip44_mst.Purpose().Coin().Account(0).Change(0).AddressIndex(0)
# 3. 获取私钥和公钥
private_key_bytes = bip44_acc.PrivateKey().Raw().ToBytes()
public_key_bytes = bip44_acc.PublicKey().RawCompressed().ToBytes()
# 4. 使用 ecdsa 库进行签名
def sign_data(private_key, data):
# 创建 ECDSA 签名对象
sk = SigningKey.from_string(private_key, curve=SECP256k1)
# 签名
signature = sk.sign(data.encode("utf-8"))
return signature.hex()
# 示例数据
data_to_sign = "Your data here" # 替换为你要签名的数据
signature = sign_data(private_key_bytes, data_to_sign)
print("Private Key:", private_key_bytes.hex())
print("Public Key:", public_key_bytes.hex())
print("Signature:", signature)