Skip to content

Instantly share code, notes, and snippets.

View Breta01's full-sized avatar
🤹
All over the place

Bretislav Hajek Breta01

🤹
All over the place
View GitHub Profile
@Breta01
Breta01 / Changes to brownie-config.yaml
Last active February 1, 2022 04:54
Changes to brownie-config.yaml for deployment on Polygon
# I recommend using .env file for managing secrets
dotenv: .env
# set a custom mnemonic for the development network
networks:
# Polygon
polygon-test:
link_token: '0x326C977E6efc84E512bB9C30f76E30c160eD06FB'
vrf_coordinator: '0x8C7382F9D8f56b33781fE506E897a4F1e2d17255'
keyhash: '0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4'
@Breta01
Breta01 / Example of .env
Created February 1, 2022 04:44
Example of .env file for deployment to polygon
### Private key of account deploying the smart contracts
export PRIVATE_KEY='0xasdfasdfasdfasdfasdfasdfasdfas'
### Infura project ID for communicating with network
export WEB3_INFURA_PROJECT_ID='aaa5aa5a5a5a55555aaa555a5a5555a'
### Polygonscan API token for verifying contracts
export POLYGONSCAN_TOKEN='asdfadfasdfsf'
@Breta01
Breta01 / Defining Custome Model.py
Created February 26, 2022 15:29
Example of obtaining CID of custom model for feltoken.
from sklearn.linear_model import Ridge
from felt.builder import upload_model
model = Ridge(alpha=0.7)
# Make sure you have WEB3_STORAGE_TOKEN environment variable defined
cid = upload_model(model)
print("CID:", cid)
@Breta01
Breta01 / Using FELToken Final Model.py
Created February 26, 2022 15:39
Example of using final model produced by FELToken.
import numpy as np
import joblib
# Load the model, use the correct path to the model.joblib file
model = joblib.load("model.joblib")
# X should contain the data you want to use for prediction
X = np.array([...])
result = model.predict(X)
@Breta01
Breta01 / get public key.ts
Last active March 17, 2022 05:15
Requesting the public key from MetaMask
// Account is account address provided as string
// App must have access to the specified account
const account = "0x012...bc"
// Key is returned as base64
const keyB64 = await window.ethereum.request({
method: 'eth_getEncryptionPublicKey',
params: [account],
}) as string;
const publicKey = Buffer.from(keyB64, 'base64');
@Breta01
Breta01 / Encryption function.ts
Last active March 11, 2022 14:58
Encryption function used by FELToken
import { encrypt } from '@metamask/eth-sig-util';
const ascii85 = require('ascii85');
function encryptData(publicKey: Buffer, data: Buffer): number[] {
// Returned object contains 4 properties: version, ephemPublicKey, nonce, ciphertext
// Each contains data encoded using base64, version is always the same string
const enc = encrypt({
publicKey: publicKey.toString('base64'),
data: ascii85.encode(data).toString(),
version: 'x25519-xsalsa20-poly1305',
@Breta01
Breta01 / Decryption function.ts
Last active March 11, 2022 15:04
Decrypting data with MetaMas
const ascii85 = require('ascii85');
async function decryptData(account: string, data: Buffer): Promise<Buffer> {
// Reconstructing the original object outputed by encryption
const structuredData = {
version: 'x25519-xsalsa20-poly1305',
ephemPublicKey: data.slice(0, 32).toString('base64'),
nonce: data.slice(32, 56).toString('base64'),
ciphertext: data.slice(56).toString('base64'),
};
@Breta01
Breta01 / Python implementation of Encryption - Decryption.py
Created March 11, 2022 15:26
Python implementation of Encryption and Decryption functions matching with MetaMask
"""Module for encryption and decryption compatible with MetaMask."""
from base64 import a85decode, a85encode
# PyNaCl==1.5.0
from nacl.public import Box, PrivateKey, PublicKey
def _hex_to_bytes(hex: str) -> bytes:
return bytes.fromhex(hex[2:] if hex[:2] == "0x" else hex)
@Breta01
Breta01 / Github action deploy.yaml
Created April 23, 2022 03:55
Example of action for deploying smart contracts.
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "**.sol"
name: Deploy to testnet
@Breta01
Breta01 / File server with smart contracts deployment.py
Last active April 23, 2022 05:31
File server with smart contracts deployment
import http.server
from pathlib import Path
# Get root directory
ROOT = Path(__file__).parent.parent
# Class for serving build directory
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):