Skip to content

Commit 93b18cf

Browse files
add draft code
1 parent 8b13272 commit 93b18cf

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

draft_code/modwebsocket_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#import network
2+
import socket
3+
import ubinascii
4+
from websocket import websocket
5+
6+
7+
# Create and connect socket
8+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9+
#sock.connect(('echo.websocket.org', 80))
10+
sock.connect(socket.getaddrinfo('echo.websocket.org', 80)[0][-1])
11+
#getaddrinfo('localhost', 5000)[0][-1]
12+
13+
# Perform WebSocket handshake
14+
key = ubinascii.b2a_base64(b'random_bytes_here').strip()
15+
handshake = (
16+
'GET / HTTP/1.1\r\n'
17+
'Host: echo.websocket.org\r\n'
18+
'Upgrade: websocket\r\n'
19+
'Connection: Upgrade\r\n'
20+
'Sec-WebSocket-Key: {}\r\n'
21+
'Sec-WebSocket-Version: 13\r\n'
22+
'\r\n'
23+
).format(key.decode())
24+
sock.send(handshake.encode())
25+
response = sock.recv(1024).decode()
26+
print(f"reponse: {response}")
27+
if '101 Switching Protocols' not in response:
28+
raise Exception('Handshake failed')
29+
30+
# Create WebSocket object
31+
ws = websocket(sock, True)
32+
33+
# Send and receive data
34+
ws.write('Hello, WebSocket!')
35+
data = ws.read(1024)
36+
print('Received:', data)
37+
38+
# Close connection
39+
ws.close()

draft_code/secp256k1.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import secp256k1
2+
import hashlib
3+
from binascii import hexlify
4+
5+
def secp256k1_example():
6+
"""Usage example for secp256k1 usermodule"""
7+
8+
# randomize context from time to time
9+
# - it helps against sidechannel attacks
10+
# secp256k1.context_randomize(os.urandom(32))
11+
12+
# some random secret key
13+
secret = hashlib.sha256(b"secret key").digest()
14+
15+
print("Secret key:", hexlify(secret).decode())
16+
17+
# Makes sense to check if secret key is valid.
18+
# It will be ok in most cases, only if secret > N it will be invalid
19+
if not secp256k1.ec_seckey_verify(secret):
20+
raise ValueError("Secret key is invalid")
21+
22+
# computing corresponding pubkey
23+
pubkey = secp256k1.ec_pubkey_create(secret)
24+
25+
# serialize the pubkey in compressed format
26+
sec = secp256k1.ec_pubkey_serialize(pubkey, secp256k1.EC_COMPRESSED)
27+
print("Public key:", hexlify(sec).decode())
28+
29+
# this is how you parse the pubkey
30+
pubkey = secp256k1.ec_pubkey_parse(sec)
31+
32+
# Signature generation:
33+
34+
# hash of the string "hello"
35+
msg = hashlib.sha256(b"hello").digest()
36+
# signing
37+
sig = secp256k1.ecdsa_sign(msg, secret)
38+
39+
# serialization
40+
der = secp256k1.ecdsa_signature_serialize_der(sig)
41+
42+
print("Signature:", hexlify(der).decode())
43+
44+
# verification
45+
if secp256k1.ecdsa_verify(sig, msg, pubkey):
46+
print("Signature is valid")
47+
else:
48+
printf("Invalid signature")
49+
50+
if __name__ == '__main__':
51+
secp256k1_example()

0 commit comments

Comments
 (0)