發表文章

目前顯示的是有「web3j」標籤的文章

簡單聊聊ECDSA與Ethereum的sign message

圖片
    本篇不會牽涉到數學公式或是算法,只是簡單解釋格式上的差異     在我們應用中,會有兩個場景需要對訊息做簽章的動作,一個是簽章完送到smart contract裡用ecrecover做身份確認,另一個是單純把訊息(例如:個人資料)做簽章,確保訊息確實是本人的(就像簽名一樣)。     我們產品中會有iOS, Android或是後端的C#或JAVA,每種語言對ECDSA的使用方式都有所不同,有原生也有third party library,加上Web3也有提供sign message的功能,一開始大家一知半解,不太知道差異,所以在開發的時候,搞得人仰馬翻。 本篇就是針對標準ECDSA跟Web3提供的sign message做一點“ 簡單” 的解釋 。     首先,你要先知道 r s v,不用知道他背後的意義(因為我也不清楚 XD),但是你需要知道sign(本篇的sign都是指ECDSA)過的message可以被分解為 r s,咦?! 說好的 v 勒?? 先破題, 這就是標準ECDSA跟Ethereum用來可以recover signed message的差異 (其實Ethereum是沿用Bitcoin的)。     在ECDSA sign完的訊息可以分解成 r 跟 s ,不過只靠rs所反解的public key會有四組,不過實際上大家都只看兩組,因為另外兩組出現機率很低, 這篇解答 中的推文有解釋。 27 = lower X even Y. 28 = lower X odd Y. 29 = higher X even Y. 30 = higher X odd Y. Note that 29 and 30 are exceedingly rarely, and will in practice only ever be seen in specifically generated examples. There are only two possible X values if r is between 1 and (p mod n), which has a chance of about 0.0000000000000000000000...

Web3j Transaction Receipt Processor 簡介與使用

  之前的文章有聊過Transaction Manager,當時對於一個參數TransactionReceiptProcessor 不太能了解,這次因為一些需求,找到了這個物件,剛好可以解決我們的問題。所以這邊要聊聊Web3中,TransactionReceiptProcessor 的運用。      在我們實際開發的場景中,在smart contract的function最後會留下一個event,作為紀錄,方便當下的驗證,在未來的稽核跟調閱歷史紀錄也比較方便。最一開始,我們選擇了web3j wrapper裡提供的event observable去對event做註冊的方式,如何使用可以參考 這篇文章 的How to monitor events。     不過,在實際的應用上會發現,observable回來的物件,資訊不太足夠,只有單純event的參數內容,還是會希望有transaction receipt會比較方便作後面的應用。但是也不希望每次送transaction都等在那邊,或是用aync放到queue再開thread做polling。簡單來說,就是希望簡單用,不要只是送個交易,還要做這麼多管理。     此次我們的應用,是要把前端資料,藉由後端sign過再push到鏈上,所以原則上是send raw transaction的方式,這次單純使用 RawTransactionManager 這個物件。在 RawTransactionManager 預設是使用 PollingTransactionReceiptProcessor ,根據官方解釋: is the default processor used in web3j, which polls periodically for a transaction receipt for each individual pending transaction.  此時發現另一種process叫做, QueuingTransactionReceiptProcessor has an internal queue of all pending transactions. It contains a...

使用Web3j 快速實作 Oracle - II 使用smart contract wrapper

Part II 拖得有點久... 其實產生出wrapper後,直接看source code,就大概懂怎麼使用了,web3j 直接把smart contract的function轉成Java function。接下來會解釋的就是怎麼load contract跟怎麼subscribe events。 備註:這裡的範例是用 web3j 3.1.1 所產生 How to load smart contract wrapper 首先,看到load的方法有兩個,其實只有差在Credentials跟TransactionManager public static Ballot load (String contractAddress , Web3j web3j , Credentials credentials , BigInteger gasPrice , BigInteger gasLimit) { return new Ballot(contractAddress , web3j , credentials , gasPrice , gasLimit) ; } public static Ballot load (String contractAddress , Web3j web3j , TransactionManager transactionManager , BigInteger gasPrice , BigInteger gasLimit) { return new Ballot(contractAddress , web3j , transactionManager , gasPrice , gasLimit) ;}   Credentials :     這是Web3j的基本元件,大概沒什麼好說的(被揍飛 XD),還是附一下範例,基本就是帶入一串私鑰即可 Credentials cred = Credentials. create ( privatekey ); 如果,你的應用沒有需要快速連續送交易,或是不要求在listen event的間隔,其實這樣就可以了。但若有其中一項需求,這個方式就無法滿足,需要用TransactionManager。 TransactionMa...

使用Web3j 快速實作 Oracle -I建立smart contract wrapper

本篇是在介紹,如何使用Web3j能快速開發Oracle,不用一個一個刻跟ABI互動的function 因為整個ethereum都還在發展中,相關的工具也是,所以變動很快,這邊我使用的是 Web3j 2.3.1 ,搭配 Solidity 0.4.17 不清楚Oracle的,可以參考 這篇 ,寫得相當詳細跟清楚 簡單來說,EVM是一個封閉環境,不可跟外界接觸,不過event(可以想作是一般寫程式的log)是會被寫到鏈上的,寫到鏈上就代表是公開網路,大家都可以去讀取,再透過程式(也就是Oracle)去監聽特地的event。然後smart contract裡的function本來就是可以從外部被執行的。所以就是透過監聽event跟執行smart contract的function,進而從外界可以跟smart contract 做互動。 首先,先撰寫你自己的smart contract,下面這是我從官方copy過來的範例,然後加了兩個event,完整的smart contract可以看 這裡 pragma solidity ^ 0 . 4 . 17 ; contract Ballot { event EventVote ( uint id, address voter, uint time); event EventNewVoter ( address voter); function giveRightToVote ( address voter) { require (( msg . sender == chairperson) && ! voters[voter].voted && (voters[voter].weight == 0 )); voters[voter].weight = 1 ; EventNewVoter (voter); } function vote ( uint proposal) { Voter storage sender = voters[ msg . sender ]; require ( ! sender.voted); ...