Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / nondeterm-ecdsa-recovery.py
Created February 17, 2024 18:58
Produce the same Ethereum address using non-deterministic ECDSA signatures (r,s)
# references:
# https://ethereum.stackexchange.com/questions/114680/is-it-possible-to-use-non-deterministic-ecdsa-signatures-schemes-for-signing-eth
# https://eth-account.readthedocs.io/en/stable/eth_account.html#eth_account.account.Account.recover_message
from ecdsa import SigningKey, SECP256k1
from eth_account import Account
from eth_account.messages import encode_defunct, defunct_hash_message
message = encode_defunct(text="I♥SF")
mh = defunct_hash_message(text="I♥SF")
@mattlockyer
mattlockyer / conditional-json.js
Created June 19, 2019 15:25
Using spread syntax to conditionally add JSON key, value to object. Fetch example using POST and Authorization.
export const POST = (auth) => ({
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
headers: {
"Content-Type": "application/json",
...(auth ? { "Authorization": auth } : {})
},
})
@mattlockyer
mattlockyer / near-base64.js
Last active April 16, 2023 23:23
NEAR Protocol - matching base64 strings in JS and Rust
// JS (node - for browser you need to use btoa(String.fromCharCode(...new Uint8Array(...)))
const hash = sha256.create();
const hashesBase64 = [
'some string'
].map((src) => {
return Buffer.from(hash.update(src).array()).toString('base64')
})
@mattlockyer
mattlockyer / client.js
Last active February 22, 2023 18:30
s3 upload as stream using req stream (extra info in request header)
const file = this.input.files[0];
//console.log(file);
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', (e) => {
console.log(e.target.response);
});
xhr.open('POST', host + 'fileuploadstream', true);
xhr.setRequestHeader('body', JSON.stringify({ id: 'somebucketfolderid', fn: file.name }));
xhr.send(file);
@mattlockyer
mattlockyer / jwt.js
Created November 2, 2019 04:01
JWT Token Module for Cloudflare Workers / Browser
/********************************
* Module for generating and verifying JWT tokens
* Designed to work on Cloudflare Workers
* Should work in the browser or any env that supports Web Crypto API
********************************/
/********************************
* Key secret is a random Uint8Array of length 64
* See below for instructions on generating random values / keys
********************************/
@mattlockyer
mattlockyer / random.rs
Last active June 2, 2022 17:40
NEAR Random Number (u128)
// anywhere in your contract
fn random_u128() -> u128 {
let random_seed = env::random_seed(); // len 32
// using first 16 bytes (doesn't affect randomness)
as_u128(random_seed.get(..16).unwrap())
}
fn as_u128(arr: &[u8]) -> u128 {
@mattlockyer
mattlockyer / steps.js
Last active March 12, 2022 22:28
near-cli and near-api-js - key rotation steps for accounts
// go to https://near.github.io/seed-recovery/ and click the random button at the bottom
// copy publicKey AND seed phrase somewhere SAFE
// use the same tool to find the public key of an existing seed phrase OR call
near keys [accountId]
// once you have both public keys AND the new seed phrase SAFELY STORED
near add-key [accountId] [newPublicKey]
near delete-key [accountId] [oldPublicKey]
// Reference: https://www.npmjs.com/package/near-cli#near-add-key
@mattlockyer
mattlockyer / do.ts
Last active December 12, 2021 05:12
Durable Object Generic Boilerplate
const META_KEY: string = '_M'
export class Object {
state: DurableObjectState
meta: any
data: any
timeouts: any
constructor(state: DurableObjectState, env: Env) {
this.state = state;
@mattlockyer
mattlockyer / basics.js
Created June 5, 2021 00:03
NEAR Basics, Connect, Wallet, Account, Call Methods
// new near instance (for NodeJS use InMemoryKeyStore)
// do this once somewhere when app "mounts" or loads
const near = new Near({
networkId,
nodeUrl,
walletUrl,
deps: {
keyStore: new keyStores.BrowserLocalStorageKeyStore()
@mattlockyer
mattlockyer / sign-for-user.js
Created April 17, 2021 21:00
NEAR Protocol - Sign TX as another account using an access key added to that account
import * as nearAPI from 'near-api-js';
const {
Account,
KeyPair,
} = nearAPI;
// near is your current near connection instance (somewhere in your app init)
export const getAccessKeyAccount = (near, accountId, secretKey) => {