Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / App.js
Last active September 27, 2020 23:53
React useContext, state, dispatch with thunk for namespaced state management
import React, { useContext, useEffect } from 'react';
import { store } from './state/store';
import { onMount } from './state/test';
const ExampleComponent = () => {
const { state, dispatch, update: updateStore } = useContext(store)
console.log(state)
const update = async () => {
// dispatch thunk wraps function with state, dispatch
const res = await dispatch(initNear())
@mattlockyer
mattlockyer / check-hash.js
Created September 21, 2020 00:31
Check a NEAR contract hash using near-api-js
import * as nearlib from 'near-api-js'
const contractBytes = await fetch('./contract.wasm').then((r) => r.arrayBuffer())
const hash = await crypto.subtle.digest('SHA-256', contractBytes)
const hash58 = nearlib.utils.serialize.base_encode(hash)
console.log(hash58)
@mattlockyer
mattlockyer / sane-localStorage.js
Last active January 14, 2021 03:46
Sane localStorage wrappers for JSON
export const get = (k, d = {}) => {
let v = localStorage.getItem(k)
try {
return JSON.parse(v || JSON.stringify(d))
} catch (e) {
return v
}
}
export const set = (k, v) => localStorage.setItem(k, typeof v === 'string' ? v : JSON.stringify(v))
export const del = (k) => localStorage.removeItem(k)
@mattlockyer
mattlockyer / console.js
Created October 23, 2020 22:23
Quick Modal for Developer Console
modal = document.createElement('div')
modal.innerHTML = '<span id="close-modal">x</span>'
style = modal.style
modal.style.zIndex = 1000
modal.style.width = modal.style.height = '90%'
modal.style.background = 'white'
modal.style.position = 'absolute'
modal.style.top = modal.style.left = '5%'
modal.style.padding = '16px'
document.body.appendChild(modal)
@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 / index.html
Created December 19, 2020 23:18
Image Link Preview Meta Tags
Use square image or 1200x628
<head>
<meta property="og:title" content="">
<meta property="og:image" content="">
<meta property="og:type" content="website">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@mattdlockyer">
<meta name="twitter:creator" content="@mattdlockyer">
<meta property="twitter:image:alt" content="">
@mattlockyer
mattlockyer / sign-with-near-cli.js
Last active January 21, 2021 05:41
Signing Messages with near-cli
// set the network (if you want mainnet)
export NEAR_ENV=mainnet
// choose 1 or 2
// 1. NEAR wallet login
near login
// 2. LEDGER replace ACCOUNT_ID
// change useLedgerKey value if you are using custom ledger path
near repl --accountId="ACCOUNT_ID" --useLedgerKey="44'/397'/0'/0'/1'"
// REPL
@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) => {
@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 / 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()