Files
n_signer/examples/get_pubkey_fips.py

65 lines
1.6 KiB
Python

#!/usr/bin/env python3
import hashlib
import json
import socket
import struct
import time
from coincurve import PrivateKey
HOST = "npub15uqyclnr3er7r8uhka7f0ae2yt4gkjat8gxdan04q0e6xrnwmtjswcyla3.fips"
PORT = 11111
# Demo caller key (32 bytes). Replace with your stable caller key in real use.
PRIVKEY = bytes(range(1, 33))
params = []
body_hash = hashlib.sha256(json.dumps(params, separators=(",", ":")).encode()).hexdigest()
sk = PrivateKey(PRIVKEY)
pubkey_x = sk.public_key.format(compressed=False)[1:33].hex()
created_at = int(time.time())
tags = [
["nsigner_rpc", "1"],
["nsigner_method", "get_public_key"],
["nsigner_body_hash", body_hash],
]
content = "py-min"
serialized_event = json.dumps(
[0, pubkey_x, created_at, 27235, tags, content], separators=(",", ":"), ensure_ascii=False
).encode()
event_id = hashlib.sha256(serialized_event).hexdigest()
sig = sk.sign_schnorr(bytes.fromhex(event_id), aux_randomness=b"\x00" * 32).hex()
request = {
"id": "1",
"method": "get_public_key",
"params": params,
"auth": {
"id": event_id,
"pubkey": pubkey_x,
"created_at": created_at,
"kind": 27235,
"tags": tags,
"content": content,
"sig": sig,
},
}
payload = json.dumps(request, separators=(",", ":")).encode()
frame = struct.pack(">I", len(payload)) + payload
with socket.create_connection((HOST, PORT), timeout=10) as s:
s.sendall(frame)
hdr = b""
while len(hdr) < 4:
hdr += s.recv(4 - len(hdr))
ln = struct.unpack(">I", hdr)[0]
body = b""
while len(body) < ln:
body += s.recv(ln - len(body))
print(json.loads(body.decode())["result"])