Skip to content

Instantly share code, notes, and snippets.

View changwu-tw's full-sized avatar

changwu changwu-tw

View GitHub Profile
/* package to generate, import and export Ethereum keys.
https://github.com/ethereumjs/keythereum
*/
var fs = require('fs');
var keythereum = require("keythereum");
var dir = './keystore';
/* key creation
*/
@changwu-tw
changwu-tw / snarktest.solidity
Created September 19, 2017 13:45 — forked from chriseth/snarktest.solidity
zkSNARKs test code
pragma solidity ^0.4.14;
library Pairing {
struct G1Point {
uint X;
uint Y;
}
// Encoding of field elements is: X[0] * z + X[1]
struct G2Point {
uint[2] X;
@changwu-tw
changwu-tw / rawTx.js
Created February 5, 2017 16:43
Create raw transaction & sign transaction
// 地址:0x7754cea59bffd5b3638d617121fac3e224e507b3
// 私鑰:ec359d9f260fa5281b4f41c0001be1df1b0e81a742b19f69cd51fe8ed9d20dc7
> var privKey1 = '7f1a49ce75b524b81dbb0964db5da64d5b588abc4fd3f60535a97a3f8115ba43'
undefined
> var EthTx = require("ethereumjs-tx")
undefined
> var privKey1x = new Buffer(privKey1, 'hex')
undefined
> privKey1x
@changwu-tw
changwu-tw / tx.js
Created February 5, 2017 16:39
交易流程與手續費
> var acct1 = web3.eth.accounts[0]
> var acct2 = web3.eth.accounts[1]
> var acct3 = web3.eth.accounts[2]
> var balance = (acct) => { return web3.fromWei(web3.eth.getBalance(acct), "ether").toNumber() }
> web3.eth.sendTransaction({from: acct1, to: acct2, value: web3.toWei(1, "ether"), gasLimit: 21000, gasPrice: 2000000000})
'0xc597fb44d8bc1da0e3bdd8eebc45b642f49301869dc707beb5c381b8401b59bc'
> web3.eth.sendTransaction({from: acct1, to: acct2, value: web3.toWei(1, "ether"), gasLimit: 21000, gasPrice: 2000000000})
'0x94b02ec108f157c24adc213dafabdfc851d18c6064435066ce35d70687247626'
> var txHash = _
undefined
@changwu-tw
changwu-tw / sendTx.js
Created February 5, 2017 16:36
傳送交易
> web3.eth.sendTransaction({from: acct1, to: acct2, value: web3.toWei(1, "ether"), gasLimit: 21000, gasPrice: 2000000000})
>
@changwu-tw
changwu-tw / unit_convert.js
Created February 5, 2017 16:30
單位換算
> web3.toWei(20, 'gwei')
'20000000000'
> web3.toWei(20, 'gwei') * 21000
420000000000000
> web3.fromWei(web3.toWei(20, 'gwei') * 21000, 'ether')
'0.00042'
>
@changwu-tw
changwu-tw / async.md
Created October 10, 2016 08:03 — forked from chriseth/async.md
Async Solidity Contracts

Having seen @pirapira's sketch of Bamboo ( https://github.com/pirapira/bamboo/ ), which proposed to add better control about the "smart contract program flow", even across calls, I thought that this should certainly be added to Solidity, and actually, it might even be possible now to a certain degree using inline assembly.

The problem is that with many functions in a contract, it is not always clear which can be called at which stage in the contract's lifetime. Certain smart contracts would be easier to understand if written as follows:

Keybase proof

I hereby claim:

  • I am changwu-tw on github.
  • I am changwu (https://keybase.io/changwu) on keybase.
  • I have a public key ASDH0GP9RZwmnxFrdfhW2AySTrLfsfrE36E43tn57V8w7Qo

To claim this, I am signing this object:

@changwu-tw
changwu-tw / gas.md
Last active September 8, 2016 02:45

gas

gas:用來衡量交易 (transaction) 或合約 (contract) 在 Ethereum 上被執行的工作量,即多少個指令或多少個動作所需要耗費的單位;由於每一個操作都會消耗某些數量的 gas,越複雜的操作需要消耗較多的計算資源,從而所需的 gas 也越高。

而之所以要設計 gas 這個機制乃是因為一個 smart contract 在給予 input 之後會 run 多久才會停止 或者甚至會不會停止都是無法事先預測的 , 所以要求使用者自己設定願意花多少 gas 在 run 這個 contract 上面 。 (由 afool41 補充)

在 Bitcoin 中,每一筆交易需要手續費,衡量標準是看交易的大小 (transaction size);但站在 Ethereum 的角度則認為,手續費的計算應該取決於在區塊鏈平台上所需要的工作量。

舉例來說,在 Ethereum 當中,兩個合約,程式碼大小不同,有可能短的程式碼,耗費的 gas 高,因為裡面執行的動作很多牽涉到 sha3;但在 Bitcoin 世界,交易包含的資料越多,佔的大小越大,手續費較高。

# https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
# http://gobittest.appspot.com
# -*- coding: utf-8 -*-
import hashlib
import base58
prefix = "04"
version_main = "00"