Skip to main content

· 2 min read

隨著 Firefox 56,57 版的推出,我參與製作的 Firefox Onboarding 功能也正式和大眾見面了。

Firefox 56

在 Firefox 56 版中當新使用者開啟瀏覽器時,會看到一個可愛的狐狸頭。 Imgur

點進去可以看到一些功能導覽頁面。 Imgur

點擊導覽頁面右下方的按鈕的話,會聚焦到瀏覽器對應的功能區塊上,使用者可以快速嘗試這些功能。 Imgur

我們也加入了 Web assessibility,使用者可以只用鍵盤來瀏覽整個使用者導覽頁面。

Firefox Quantum (57)

經過使用者研究 (User research),在 Firefox Quantum (57) 版上我們針對 Onboarding 的體驗又做了不小的修改。

Imgur

這次的更新除了主視覺與一些互動元素都變得不一樣之外,也加了個小彩蛋:新使用者 (全新的 profile) 和從過去版本升級的使用者,所看到的功能導覽項目是不盡相同的喔。

Imgur

有興趣進一步了解我們怎麼製作 Onboarding Tour,可參考Onboarding 文件

· 3 min read

在前一篇文章中我已經設定好了雙系統(Elementary OS/Windows 10)開機。在這篇中將繼續把系統的基礎中文環境(中文界面 + 輸入法)設定起來。感謝前路上的貢獻者讓整個過程變得相當容易。

設定好的中文環境如下 Imgur

為什麼要用 Elementary OS?

其實 Ubuntu 等發行版的中文環境已經做得很好,如果使用 Ubuntu 等更成熟的發行版,基礎的中文環境都是預設好開箱即用的。

但我就是任性想用基於 Ubuntu,但是界面看起來更養眼的Elementary OS

調整語系

Imgur

首先,打開Settings中的Language & Region,在左側Installed Languages選單中將界面切換到中文。並選擇完全安裝(Complete Installation)。

等待安裝好後重新登入,就可以看到全中文界面了。

Imgur

安裝新酷音輸入法

在安裝好 Elementary OS 0.4.1 後,預設並沒有輸入法選項,但其實內部已安裝了 ibus 輸入法框架。

因為沒有內建輸入法,所以若要使用輸入法,需要自行安裝。

在命令列中輸入以下命令以安裝新酷音輸入法

$ sudo apt update
$ sudo apt install ibus-chewing

安裝好後再輸入ibus-setup,可開啟輸入法設定畫面

$ ibus-setup

在 「輸入法」 分頁底下的 「漢語」 子選單當中可以加入剛安裝的新酷音輸入法。

Imgur

按「確定」,在設定畫面中可以看到的新酷音輸入法。

Imgur

最後重要的步驟,就是將切換方式改成個人較習慣的Ctrl+Space

Imgur

如此一來,就可以在 Elementary OS 上輸入中文啦!這篇文章就是從截圖到文章編輯 / 上傳,全程在已照上述方式安裝好 Elmentary OS 基礎中文環境的筆電上完成的。

fcitx 版

我也試了安裝 fcitx 版本的新酷音

$ sudo apt install fcitx fcitx-chewing

安裝好後使用im-config命令將預設輸入法框架切換成fcitx,重新開機後就可以看到輸入法圖示了。

參考資料

· 3 min read

I just successfully installed Elementary OS Luna (0.4.1) along side with Windows 10 on my new Dell XPS 9360 machine.

Comparing this XPS 13 9360 (8th Gen i7) with my previous model 9343 (5th Gen i7), the CPU is way faster(feels like 3 times faster), the wireless works well and the signal is stronger (which I was encountered some issues in XPS 13 9343), the keyboard feedback is pleasent, and the battery life did last longer in 9360.

Several settings are trickier than early days when I try to install *nux on Notebook. Here's the self reference guide and hope it could help others.

Settings on Windows

Login to Microsoft account

It's necessary to bind your account with the device, so you can get recovery key later.

Flash Elementary OS to the USB disk

I download the OS image from Elementary OS web site and use Unetbootin to flash the image into the disk.

Shrink the disk space

I have the 256GB SSD, I use Partition Wizard to slice ~100GB for Elementary OS.

I have 8GB RAM so I reference the recommend swap size and left ~8GB for swap.

Disable fast startup

I also need to follow the instructions to disable the fast startup on Windows.

Switch SATA operation mode from RAID to AHCI

Here's the most tricky part. It takes me a while to figure out how to switch the SATA storage from RAID to AHCI. The trick is doing this procedure with Windows safe mode.

Settings on BIOS

Press F12 during booting.

  • Don't need to turn off the security boot.
  • Follow above link's instructions to config SATA storage.
  • Save the configureation before exit.

Reboot to windows and make sure windows runs nicely in safemode. Then follow above link's instructions to jump out of safemode.

Now I'm prepared and able to install Elementary OS.

Install Elementary OS through USB disk

Install Elementary OS as usual Ubuntu distribution.

First connect to wifi and allow install 3rd party softwares.

Add the left space as /(root) with Ext4 format. And set the left ~8GB as SWAP format.

Then continue the auto install process.

Now I have a clean Elementary OS.

Recover Windows with BitLocker recovery keys

After restart I can see an option menu with Elementary OS and Windows Boot Manager.

I can boot to Elementary OS without major issue.

Well, I met the problem that AppCenter can't start correctly, so I do sudo apt update && sudo apt upgrade then sudo apt purge appcenter && sudo apt purge packagekit then restart and run sudo apt install appcenter && sudo apt install packagekit to make it work normally.

The input method and bluetooth pairing seems not work out of box, but I can live with that.

Then when I boot to Windows, I need to enter the BitLocker recovery keys. That could be solved by following the FAQ from microsoft. I need to login to Microsoft Account and find the recovery key.

Enter the recovery key then everything works.

· 10 min read

上一篇中我們已寫好並部署完成了簡單的加密代幣🔒💵合約。在閱讀完本文後,你將學會建立一個可以放到乙太幣錢包👛 的加密代幣🔒💵。

開發前的準備

延續上一篇的內容,在開發的過程中,我們將繼續使用testrpc1工具在電腦上模擬智能合約所需的乙太坊區塊鏈測試環境。

首先確保已啟動 testrpc。若尚未啟動,可以使用以下命令啟動:

$ testrpc
...

這邊有個值得一學的小技巧:在啟動 testrpc 時加上--seed參數,例如

testrpc --seed apple banana cherry

這樣之後重新啟動 testrpc 時可以產生一樣的帳號 (accounts) 和私鑰 (private key)。

ERC20 標準

建立的代幣若要能透過乙太幣錢包👛 來收送,必須相容於以太坊的 ERC20 標準2。ERC20 標準定義了支援錢包所必須的合約介面。

本篇將使用OpenZeppelin2函式庫來簡化建立加密代幣🔒💵的過程。OpenZeppelin是一套協助撰寫安全的加密合約的函式庫,裡面也提供了相容 ERC20 標準的智能合約。可以透過 npm 工具安裝到專案目錄node_modules/zeppelin-solodity/中:

$ npm install zeppelin-solidity

準備完成,我們可以開始建立新的加密代幣智能合約了。

建立一個標準代幣合約

contracts/目錄下建立一個HelloToken.sol檔案。也可以使用以下命令來產生檔案:

$ truffle create contract HelloToken

HelloToken.sol檔案內容如下:

pragma solidity ^0.4.11;
import "zeppelin-solidity/contracts/token/StandardToken.sol";

contract HelloToken is StandardToken {
string public name = "HelloCoin";
string public symbol = "H@";
uint8 public decimals = 2;
uint256 public INITIAL_SUPPLY = 88888;

function HelloToken() public {
totalSupply = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}

講解

pragma solidity ^0.4.11;

第一行指名目前使用的 solidity 版本,不同版本的 solidity 可能會編譯出不同的 bytecode。

import "zeppelin-solidity/contracts/token/StandardToken.sol";

接著我們使用import語句,來讀入zeppelin-solidity提供的StandardToken合約。

contract HelloToken is StandardToken {
}

建立HelloToken合約時,使用is語句繼承了StandardToken合約。因此HelloToken合約繼承了StandardToken合約所包含的資料與呼叫方法。

當我們繼承了StandardToken合約,也就支援了以下 ERC-20 標準中2規定的函式

函式描述
totalSupply()代幣發行的總量
balanceOf(A)查詢 A 帳戶下的代幣數目
transfer(A,x)傳送 x 個代幣到 A 帳戶
transferFrom(A,x)從 A 帳戶提取 x 個代幣
approve(A,x)同意 A 帳戶從我的帳戶中提取代幣
allowance(A,B)查詢 B 帳戶可以從 A 帳戶提取多少代幣

和前一篇一樣,後面驗證時會用到balanceOftransfer兩個函式。因為StandardToken合約中已經幫我們實做了這些函式,因此我們不需要自己從頭再寫一次。

string public name = "HelloCoin";
string public symbol = "H@";
uint8 public decimals = 2;
uint256 public INITIAL_SUPPLY = 100000;

這邊設定參數的目的是指定這個代幣的一些特性。以美元為例,美元的名稱 (name) 是dollar,美元的代號為$,拿一美元去找零最小可以拿到零錢是一美分 (cent),也就是 0.01 元。因為一美元最小可分割到小數點後 2 位 (0.01),因此最小交易單位 (decimals) 為 2。

這邊將這個加密代幣取名 (name) 為HelloCoin(哈囉幣),代幣的代號 (symbol) 為H@,最小分割單位是 2 (最小可以找 0.01 個哈囉幣)。

以下為美金,比特幣,以太幣,哈囉幣的對照表供參考:

NameSymboldecimals
Dollar$2
BitcoinBTC8
EthereumETH18
HelloCoinH@2

最後也定義了初始代幣數目INITIAL_SUPPLY100000。當我們把全域變數設為public(公開),編譯時就會自動新增一個讀取公開變數的 ABI 接口,我們在truffle console中也可以讀取這些變數。

function HelloToken() public {
totalSupply = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}

和合約同名的HelloToken方法,就是HelloToken合約的建構函式 (constructor)。 在建構式裡指定了totalSupply數目,並將所有的初始代幣INITIAL_SUPPLY都指定給msg.sender帳號,也就是用來部署這個合約的帳號。‵totalSupply定義於ERC20Basic.sol中,balances定義於BasicToken.sol中。

import '../math/SafeMath.sol';
...
using SafeMath for uint256;
...
balances[msg.sender] = balances[msg.sender].sub(_value);

進一步追去看BasicToken.sol合約的內容,可以發現BasicToken.sol合約中匯入了SafeMath.sol合約7SafeMath對各種數值運算加入了必要的驗證,讓合約中的數字計算更安全。

如此一來,我們已寫好一個可透過以太幣錢包交易的新加密代幣🔒💵合約。如果將這個合約部署到乙太坊公開區塊鍊上,這個代幣合約就會一直存在,世界上從此也就多了一種新的加密代幣。只要你能找到人想擁有這種代幣,這種代幣就有交易的價值。

編譯與部署

migrations/目錄下建立一個4_deploy_hellotoken.js檔案,內容如下:

var HelloToken = artifacts.require("HelloToken");

module.exports = function(deployer) {
deployer.deploy(HelloToken);
};

現在執行 compile 與 migrate 命令

$ truffle compile
...
$ truffle migrate --reset
Using network 'development'.

Running migration: 4_deploy_hellotoken.js
Deploying HelloToken...
... 0x2c4659528c68b4e43d1edff6c989fba05e8e7e56cc4085d408426d339b4e9ba4
HelloToken: 0x352fa9aa18106f269d944935503afe57a00a9a0d
Saving successful migration to network...
... 0x1434c1b61e9719f410fc6090ce37c0ec141a1738aba278dd320738e4a5d229fa
Saving artifacts...

如此一來我們已將 HelloCoin 代幣合約部署到 testrpc 上。

驗證

我們一樣可以透過truffle console來驗證HelloToken是否部署成功。

$ truffle console
> let contract
> HelloToken.deployed().then(instance => contract = instance)
> contract.address
'0x352fa9aa18106f269d944935503afe57a00a9a0d'
> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 5, c: [ 100000 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }
> contract.transfer(web3.eth.accounts[1], 123)
...
> contract.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 4, c: [ 99877 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 2, c: [ 123 ] }
>

講解

> let contract
> SimpleToken.deployed().then(instance => contract = instance)

這邊使用HelloToken.deployed().then語句來取得 HelloToken 合約的 Instance (實例),並存到contract變數中,以方便後續的呼叫。

> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 5, c: [ 100000 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }

web3.eth.coinbase 代表操作者預設的帳號,即 testrpc 所提供的 10 個帳號中的第一個帳號,也可以透過web3.eth.accounts[0]取得。 這兩句的目的是在進行轉帳操作前,先查詢前兩個帳號所擁有的代幣餘額。透過呼叫balanceOf函式,可以看到第一個帳號 (部署合約的帳號) 裡存著所有的代幣。

> contract.transfer(web3.eth.accounts[1], 123)
...

接著使用transfer函式來傳送123個代幣到第二個帳號web3.eth.accounts[1]。如果轉帳成功,傳送者預設帳號中會減少123個代幣,接收者帳號中會增加123個代幣。

> contract.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 4, c: [ 99877 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 2, c: [ 123 ] }
>

我們再次透過呼叫balanceOf函式,查詢傳送者帳號和接收者帳號各自剩下的 HelloToken 數目。發現轉帳真的成功了。

結語

我們用到 OpenZeppelin提供的函式庫來簡化撰寫加密代幣合約的工作。要實際投入使用前,還是建議將呼叫到的程式碼都再審查幾遍。可以從 OpenZeppelin 自己提供的 Audit 開始看4,可以學到一些觀念。

參考資料

· 11 min read

Update: 12/28/2017 更新教程,使用 require 取代 throw。

上一篇中我們已寫好並部署完成了第一個智能合約,也驗證了合約確實可以運作。在閱讀完本篇後,你將學會建立一個簡易的加密代幣🔒💵。本篇目的並非為了寫出一個安全可用的加密代幣,而是以介紹代幣合約的相關概念為主, 是以對合約做了適當地簡化,好更易於理解。

開發前的準備

延續上一篇的內容,在開發的過程中,我們將繼續使用testrpc1工具在電腦上模擬智能合約所需的乙太坊區塊鏈測試環境。

首先確保已啟動 testrpc,若尚未啟動,可以使用以下命令啟動

$ testrpc
...

這樣我們就可以開始建立加密代幣智能合約專案了。

代幣合約的基礎概念

代幣合約扮演像是銀行🏦 的角色。使用者在代幣合約中,用自己的乙太幣帳戶地址當作銀行帳戶,可以透過代幣合約執行轉帳 (transfer,將代幣由一個帳戶傳送到另一個帳戶),查詢餘額 (balanceOf,查詢指定帳戶中擁有的代幣) 等原本由銀行負責的工作。因為合約部署在公開區塊鏈上,所有的交易都是公開透明,可供檢驗的。

建立一個代幣合約

contracts/目錄下建立一個SimpleToken.sol檔案。也可以使用以下命令來產生檔案:

$ truffle create contract SimpleToken

SimpleToken.sol檔案內容如下:

pragma solidity ^0.4.11;

contract SimpleToken {
uint256 INITIAL_SUPPLY = 10000;
mapping(address => uint256) balances;

function SimpleToken() public {
balances[msg.sender] = INITIAL_SUPPLY;
}

// transfer token for a specified address
function transfer(address _to, uint256 _amount) public {
require(balances[msg.sender] > _amount);
balances[msg.sender] -= _amount;
balances[_to] += _amount;
}

// Gets the balance of the specified address
function balanceOf(address _owner) public constant returns (uint256) {
return balances[_owner];
}
}

講解

pragma solidity ^0.4.11;

第一行指名目前使用的 solidity 版本,不同版本的 solidity 可能會編譯出不同的 bytecode。

uint256 INITIAL_SUPPLY = 10000;
mapping(address => unit256) balances;

我們定義了初始代幣數目INITIAL_SUPPLY。這邊隨意選擇了一個數字10000

我們用mapping來定義一個可以儲存鍵值對 (key-value pair) 的資料結構 (類似 Javascript 中的{"0xaabbccddeeff": 888}),同時也需要分別指定address作為鍵的型別,指定uint256作為值的型別。和 Javascript 不同,型別定義好後不能隨時更改要儲存的型別。

contract SimpleToken {
function SimpleToken() public {
balances[msg.sender] = INITIAL_SUPPLY;
}
}

和合約同名的SimpleToken函式,就是SimpleToken這個合約的建構函式 (constructor)。函式中我們拿msg.sender當作 key,INITIAL_SUPPLY當作值,將所有的初始代幣INITIAL_SUPPLY都指定給msg.sender帳號。 msg是一個全域 (Global) 物件2msg.sender表示用作呼叫當前函式的帳號。由於建構函式只有在合約部署時會被執行,因此這邊用到的msg.sender,也就代表著用來部署這個合約的帳號。

function transfer(address _to, uint256 _amount) public {
require(balances[msg.sender] > _amount);
balances[msg.sender] -= _amount;
balances[_to] += _amount;
}

transfer函式定義了如何轉帳,只要指定要傳送的帳號與數目,就會從呼叫者手上把對應數目的代幣移轉到指定的帳號上。

graph LR 傳送者 -- 轉帳 --> 代幣合約 代幣合約 -.-> 修改傳送者和接收者餘額

require(balances[msg.sender] > _amount);語句判斷帳戶中是否還有足夠轉出的餘額,若存款小於想轉出的數目,就丟出錯誤。

這個函式這麼寫當然還是過度簡化了,若要能實際使用,需要檢查更多可能的狀況。但就先這樣吧。

function balanceOf(address _owner) public constant returns (uint256) {
return balances[_owner];
}

balanceOf函式的作用,是讓使用者可以查詢任一帳號的餘額。透過傳入_owner帳號,可以查詢_owner帳號儲存在balances對照表中的值。

graph LR 傳送者 --> 代幣合約 代幣合約 -. 查詢結果 .-> 傳送者

如此一來,我們就寫好一個新加密代幣🔒💵合約囉!接下來將要編譯合約並部署到區塊鏈上。

編譯與部署

migrations/目錄下建立一個3_deploy_token.js檔案,內容如下:

var SimpleToken = artifacts.require("SimpleToken");

module.exports = function(deployer) {
deployer.deploy(SimpleToken);
};

現在可執行 compile 與 migrate 命令

$ truffle compile
...
$ truffle migrate
Using network 'development'.

Running migration: 3_deploy_token.js
Deploying HelloToken...
... 0x2c4659528c68b4e43d1edff6c989fba05e8e7e56cc4085d408426d339b4e9ba4
SimpleToken: 0x352fa9aa18106f269d944935503afe57a00a9a0d
Saving successful migration to network...
... 0x1434c1b61e9719f410fc6090ce37c0ec141a1738aba278dd320738e4a5d229fa
Saving artifacts...

如此一來我們已將SimpleToken代幣合約部署到 testrpc 上。

驗證

合約部署完成後,我們可以使用truffle console命令開啟 console,輸入以下命令來驗證合約是否能照我們設計的方式運作。

$ truffle console
> let contract
> SimpleToken.deployed().then(instance => contract = instance)
> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 4, c: [ 10000 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }
> contract.transfer(web3.eth.accounts[1], 123)
...
> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 3, c: [ 9877 ] }
> contract.balanceOf.call(web3.eth.accounts[1])
BigNumber { s: 1, e: 2, c: [ 123 ] }
>

講解

> let contract
> SimpleToken.deployed().then(instance => contract = instance)

這邊使用SimpleToken.deployed().then語句來取得 SimpleToken 合約的 Instance (實例),並存到contract變數中,以方便後續的呼叫。

> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 4, c: [ 10000 ] }
> contract.balanceOf(web3.eth.accounts[1])
BigNumber { s: 1, e: 0, c: [ 0 ] }

還記得啟動 testrpc 後預設會產生 10 個帳號 (Accounts) 嗎?。web3.eth.coinbase 代表操作者預設的帳號,即 10 個帳號中的第一個帳號web3.eth.accounts[0],所以這邊呼叫web3.eth.coinbaseweb3.eth.accounts[0]結果是一樣的。

> contract.balanceOf(web3.eth.accounts[0])
BigNumber { s: 1, e: 4, c: [ 10000 ] }

這兩句的目的是在進行轉帳操作前,先查詢前兩個帳號所擁有的代幣餘額。透過呼叫balanceOf函式,可以看到第一個帳號 (部署合約的帳號) 裡存著所有的代幣。

> contract.transfer(web3.eth.accounts[1], 123)
...

接著使用transfer函式來傳送123個代幣到第二個帳號web3.eth.accounts[1]。如果轉帳成功,傳送者預設帳號中會減少 123 個代幣,接收者帳號中會增加 123 個代幣。

> contract.balanceOf(web3.eth.coinbase)
BigNumber { s: 1, e: 3, c: [ 9877 ] }
> contract.balanceOf.call(web3.eth.accounts[1])
BigNumber { s: 1, e: 2, c: [ 123 ] }
>

我們再次透過呼叫balanceOf函式,查詢傳送者帳號和接收者帳號各自剩下的 SimpleToken 數目。發現轉帳真的成功了。

你知道剛剛的程式碼裡有一堆安全漏洞💣 嗎?

寫智能合約看起來並不困難吧?但因為智能合約的運作是透明公開的,而且其中牽涉了代幣或金錢的流動,這提供了駭客很強的挑戰動機。

因此如果要妥善處理智能合約,會遇到的諸多安全問題。即使單純如本篇中的SimpleToken,也至少會遇到幾個問題:例如transfer函式中沒有禁止傳入負數金額,因此傳送者反過來可以從接收者那邊取得代幣。同時也沒有檢查接收者帳號是否合法,因此傳送者可能會傳送失敗或因為送到黑洞中,白白損失了代幣。

有著一堆安全漏洞的合約,輕則執行失敗白花交易費用,嚴重則影響到合約中的代幣或以太幣。已有多起因為合約的漏洞,造成儲存在合約中的代幣或以太幣被駭客轉走,使得 ICO 失敗的案例。

有興趣的人可以進一步查看參考資料45了解智能合約當前的一些最佳實現。

結語

看完這篇除了學到本篇講解的SimpleToken外,應該也可以大致看得懂 truffle 預設的MetaCoin.sol合約了。不同的細節可以查看 solidity 相關語法2。 下一篇會接著介紹如何使用經過驗證的函式庫,來建立一份可以放到乙太幣錢包👛 的加密代幣🔒💵合約。

參考資料