Skip to content

Instantly share code, notes, and snippets.

View jimmychu0807's full-sized avatar
🎯
Focusing

Jimmy Chu jimmychu0807

🎯
Focusing
View GitHub Profile
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
// 引入
import { ApiPromise, WsProvider } from '@polkadot/api';
// 創建 api 對象
const wsProvider = new WsProvider('ws://localhost:9944');
const api = await ApiPromise.create({ provider: wsProvider });
// 簡單測試-讀取常量
console.log(api.consts.balances.transactionByteFee.toNumber());
@jimmychu0807
jimmychu0807 / Markdium-Shell.bash
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
yarn add @polkadot/keyring
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
// 創建 api
// const api = await ...;
api.query.system.events(events => {
events.forEach(record => {
// 遍歷所有事件記錄
const { event, phase } = record;
const types = event.typeDef;
// 過濾掉我們不關注的事件
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
// 訂閱著該數值
const unsub = await api.query.balances.freeBalance(ADDR, balance => {
console.log(`balance of ${balance}`);
});
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
const unsub = await api.queryMulti([
// 一個 getter function
api.query.timestamp.now,
// 另一個 getter function,及所需參數
[api.query.balances.freeBalance, ADDR],
[api.query.system.accountNonce, ADDR],
], ([now, balance, nonce]) => { // 回調函數
console.log(`${now}: balance of ${balance} and a nonce of ${nonce}`);
});
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
// 從 mnemonic 來生成,建議方法:
const PHRASE = 'entire material egg meadow latin bargain dutch coral blood melt acoustic thought';
const newPair = keyring.addFromUri(PHRASE);
// 只限開發時使用,即運行 Substrate 節點時加了 `--dev` 參數:
const alice = keyring.addFromUri('//Alice', { name: 'Alice default' });
// 用 32 位的 16 進制數字生成
const hexPair = keyring.addFromUri('0x1234567890123456789012345678901234567890123456789012345678901234');
@jimmychu0807
jimmychu0807 / Markdium-javascript.js
Created March 4, 2020 11:58
Markdium-用 Polkadot-JS API 輕鬆搭建 Substrate 前端
// babe 組件內的常量
console.log(api.consts.babe.epochDuration.toNumber());
// balances 組件內的常量
console.log(api.consts.balances.creationFee.toNumber());
console.log(api.consts.balances.transferFee.toNumber());
@jimmychu0807
jimmychu0807 / 0_General_Instructions.md
Created December 16, 2019 07:19 — forked from shawntabrizi/0_General_Instructions.md
Sub 0.1 Workshop Setup Instructions

General Instructions

Most workshops at Sub0 will require that you have set up your computer to compile Substrate.

You can use this one-liner to do many of these steps for you automatically:

curl https://getsubstrate.io -sSf | bash -s -- --fast
@jimmychu0807
jimmychu0807 / string-conversion.rs
Created November 21, 2019 10:20
Conversion between String, str, Vec<u8>, Vec<char> in Rust
use std::str;
fn main() {
// -- FROM: vec of chars --
let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// to String
let string1: String = src1.iter().collect::<String>();
// to str
let str1: &str = &src1.iter().collect::<String>();
// to vec of byte
@jimmychu0807
jimmychu0807 / openssl.md
Created September 2, 2019 03:58 — forked from NoMan2000/openssl.md
Common OpenSSL Commands with Keys and Certificates

Common OpenSSL Commands with Keys and Certificates

SSL Info

Generate RSA private key with certificate in a single command

openssl req -x509 -newkey rsa:4096 -sha256 -keyout example.key -out example.crt -subj "/CN=example.com" -days 3650 -passout pass:foobar

Generate Certificate Signing Request (CSR) from private key with passphrase

openssl x509 -x509toreq -in example.crt -out example.csr -signkey example.key -passin pass:foobar