摘要:作者羋橙比原項(xiàng)目倉(cāng)庫(kù)地址地址本文解析的為比原提供的幣幣合約模板如下導(dǎo)讀初次接觸比原只能合約的請(qǐng)點(diǎn)擊比原智能合約入門和語(yǔ)言入門學(xué)習(xí),方便更好的理解該文檔鎖定合約第一步調(diào)用生成以下是相關(guān)代碼片段第二步調(diào)用獲取以下是相關(guān)代碼片段第三
作者:羋橙
比原項(xiàng)目倉(cāng)庫(kù):
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockc...
本文解析的為比原提供的幣幣合約 模板如下:
contract TradeOffer(assetRequested: Asset, amountRequested: Amount, seller: Program, cancelKey: PublicKey) locks offered { clause trade() requires payment: amountRequested of assetRequested { lock payment with seller unlock offered } clause cancel(sellerSig: Signature) { verify checkTxSig(cancelKey, sellerSig) unlock offered } }
導(dǎo)讀:
初次接觸比原只能合約的請(qǐng)點(diǎn)擊[比原智能合約入門](https://bbs.bbug.org.cn/thread-26.htm) 和 [
Equity 語(yǔ)言入門](https://bbs.bbug.org.cn/threa... 學(xué)習(xí),方便更好的理解該文檔
鎖定合約 第一步:調(diào)用create-account-receiver 生成 control_program以下是相關(guān)代碼片段:
sendHttpPost("{"account_id":"0IJVD7MNG0A02"}","create-account-receiver","http://127.0.0.1:9888","");第二步調(diào)用list-pubkeys 獲取 pubkey
以下是相關(guān)代碼片段:
sendHttpPost("{"account_id":"0IJVD7MNG0A02"}","list-pubkeys","http://127.0.0.1:9888","");第三步: 將1 2步獲取的值調(diào)用compile接口編譯合約獲得program 合約程序
以下是相關(guān)代碼片段:
JSONObject param=new JSONObject(); JSONArray agrs=new JSONArray(); //合約的四個(gè)參數(shù)值 JSONObject assetParam=new JSONObject(); assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a"); agrs.put(assetParam); JSONObject amountParam=new JSONObject(); amountParam.put("integer",200000000l); agrs.put(amountParam); JSONObject programParam=new JSONObject(); programParam.put("string",control_program); agrs.put(programParam); JSONObject publicKeyParam=new JSONObject(); publicKeyParam.put("string",pubkey); agrs.put(publicKeyParam); param.put("agrs",agrs); param.put("contract","contract TradeOffer(assetRequested: Asset, amountRequested: Amount, seller: Program, cancelKey: PublicKey) locks offered { clause trade() requires payment: amountRequested of assetRequested { lock payment with seller unlock offered } clause cancel(sellerSig: Signature) { verify checkTxSig(cancelKey, sellerSig) unlock offered } }"); //調(diào)用編譯合約 sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");第四步:將program 傳入build-transaction接口去build一個(gè)交易的到data
以下是相關(guān)代碼片段:
param=new JSONObject(); agrs=new JSONArray(); JSONObject spendAccount=new JSONObject(); spendAccount.put("account_id","0H757LPD00A02"); spendAccount.put("amount",9909099090000l); spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652"); spendAccount.put("type","spend_account"); agrs.put(spendAccount); JSONObject controlAccount=new JSONObject(); controlAccount.put("control_program",program); controlAccount.put("amount",9909099090000l); controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652"); controlAccount.put("type","control_program"); agrs.put(controlAccount); JSONObject spendAccount2=new JSONObject(); spendAccount2.put("account_id","0H757LPD00A02"); spendAccount2.put("amount",6000000l); spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); spendAccount2.put("type","spend_account"); agrs.put(spendAccount2); param.put("actions",agrs); param.put("ttl",0); sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");第五步:輸入密碼調(diào)用sign-transaction簽名第四步build的data 得到raw_transaction
以下是相關(guān)代碼片段:
param=new JSONObject(); param.put("password","xxx"); param.put("transaction",data); sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");第六步:調(diào)用submit-transactions提交交易
以下是相關(guān)代碼片段:
param=new JSONObject(); param.put("raw_transaction",raw_transaction); sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");解鎖/取消合約 首先需要decode出生成合約時(shí)候的參數(shù) 調(diào)用list-unspent-outputs 獲取生成的合約信息獲取program
以下是相關(guān)代碼片段:
param=new JSONObject(); param.put("id",outputid); param.put("smart_contract",true); sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");調(diào)用decode-program 傳入獲取生成的合約參數(shù)信息
以下是相關(guān)代碼片段:
param=new JSONObject(); param.put("program",program); sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");
需要注意的是decode出來(lái)的為值是逆序的(后續(xù)會(huì)有文章詳細(xì)介紹)
解鎖/取消其實(shí)就是把生成合約的步驟中的第三步去掉,替換調(diào)用生成合約第四步的參數(shù)即可取消合約的構(gòu)造參數(shù)如下:
spendAccountUnspentOutput = arguments: [{ type: "raw_tx_signature", // 生成合約第二步的pubkeylist 詳情 raw_data: { derivation_path: pubkeylist.pubkey_infos[0].derivation_path, xpub: pubkeylist.root_xpub } }, { type: "data", raw_data: { // 參數(shù)偏移量 在一個(gè)合約里是固定的 value: "13000000" } }], output_id: output_id, type: "spend_account_unspent_output" } const controlAction = { type: "control_program", amount: 100000000, asset_id: asset_id, control_program:control_program } const gasAction = { type: "spend_account", account_id:account_id, asset_alias: "BTM", amount: 50000000 }
執(zhí)行合約的參數(shù)構(gòu)造如下:
const spendAccountUnspentOutput = { arguments: [{ type: "data", raw_data: { // 00000000 指的是第一個(gè) clause,表示直接執(zhí)行,無(wú)需跳轉(zhuǎn) value: "00000000" } }], output_id: output_id, type: "spend_account_unspent_output" } // 合約執(zhí)行提供的資產(chǎn) const issueControlAction = { control_program: control_program, amount: 100000000, asset_id: asset_id, type: "control_program" } // 合約執(zhí)行提供的資產(chǎn) const issueSpendAction = { account_id: account_id, amount: 100000000, asset_id: asset_id, type: "spend_account" } // 礦工費(fèi) const gasAction = { type: "spend_account", account_id: account_id, asset_alias: "BTM", amount: 50000000 } // 合約執(zhí)行獲得資產(chǎn)對(duì)象 const controlAction = { type: "control_program", amount: 100000000, asset_id: asset_id, control_program: compileData.control_program }
build 操作其實(shí)就是指定輸入輸出的過(guò)程,詳情請(qǐng)查看 官方build文檔 和 官方api文檔
備注調(diào)用比原基于okhttp接口javautil 如下:
public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException { OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, bodyStr); Request request = new Request.Builder() .url(bytomApiserverUrl+"/"+method) .post(body) .addHeader("cache-control", "no-cache") .addHeader("Connection", "close") .build(); if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){ }else { byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII"))); String authHeader = "Basic " + new String(encodedAuth); request = new Request.Builder() .url(bytomApiserverUrl+"/"+method) .post(body) .addHeader("authorization", authHeader) .addHeader("cache-control", "no-cache") .addHeader("Connection", "close") .build(); } Response response = client.newCall(request).execute(); return response.body().string(); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/24296.html
摘要:究竟區(qū)塊鏈具有多大的魔力能讓人如此信任,或者說(shuō),我們?cè)谡f(shuō)信的時(shí)候究竟信的是什么。那么我們說(shuō)信區(qū)塊鏈時(shí),信的是什么呢信密碼學(xué)算法區(qū)塊鏈?zhǔn)怯盟惴ㄟ_(dá)成信任的,其中最重要的算法之一,就是密碼學(xué)。信博弈論區(qū)塊鏈中最玄妙的部分是共識(shí)算法。 FISCOBCOS是完全開源的聯(lián)盟區(qū)塊鏈底層技術(shù)平臺(tái),由金融區(qū)塊鏈合作聯(lián)盟(深圳)(簡(jiǎn)稱金鏈盟)成立開源工作組通力打造。開源工作組成員包括博彥科技、華為、深證通...
摘要:前言以太坊是一個(gè)運(yùn)行智能合約的平臺(tái),被稱作可編程的區(qū)塊鏈,允許用戶將編寫的智能合約部署在區(qū)塊鏈上運(yùn)行。交易執(zhí)行以太坊是一個(gè)基于交易的狀態(tài)機(jī),一筆交易可以使以太坊從一個(gè)狀態(tài)切換到另一個(gè)狀態(tài),即交易的執(zhí)行伴隨著狀態(tài)的改變。 前言 以太坊是一個(gè)運(yùn)行智能合約的平臺(tái),被稱作可編程的區(qū)塊鏈,允許用戶將編寫的智能合約部署在區(qū)塊鏈上運(yùn)行。而運(yùn)行合約的主體便是以太坊虛擬機(jī)(EVM) 區(qū)塊 交易 合約 ...
摘要:模塊鏈的共識(shí)配置,該配置會(huì)寫入創(chuàng)世塊。主要指責(zé)是記錄和更新本地累計(jì)工作量最高的鏈,并維護(hù)鏈上數(shù)據(jù)的索引。消息使用序列化。協(xié)議是節(jié)點(diǎn)之間用來(lái)處理廣播和轉(zhuǎn)發(fā)新的交易。 by Nervos CKB Team 在 2017 年底,我們感到心里的一些想法,包括分層的網(wǎng)絡(luò)以及一個(gè)作為共同知識(shí)庫(kù)(Common Knowledge Base)的區(qū)塊鏈,都已經(jīng)成熟。因此 2018 年元旦一過(guò)我們就迫不及...
摘要:引言給迷失在如何學(xué)習(xí)區(qū)塊鏈技術(shù)的同學(xué)一個(gè)指引,區(qū)塊鏈技術(shù)是隨比特幣誕生,因此要搞明白區(qū)塊鏈技術(shù),應(yīng)該先了解下比特幣。但區(qū)塊鏈技術(shù)不單應(yīng)用于比特幣,還有非常多的現(xiàn)實(shí)應(yīng)用場(chǎng)景,想做區(qū)塊鏈應(yīng)用開發(fā),可進(jìn)一步閱讀以太坊系列。 本文始發(fā)于深入淺出區(qū)塊鏈社區(qū), 原文:區(qū)塊鏈技術(shù)學(xué)習(xí)指引 原文已更新,請(qǐng)讀者前往原文閱讀 本章的文章越來(lái)越多,本文是一個(gè)索引帖,方便找到自己感興趣的文章,你也可以使用左側(cè)...
摘要:年,包括分層的網(wǎng)絡(luò)以及一個(gè)作為共同知識(shí)庫(kù)的區(qū)塊鏈,都已經(jīng)成熟。是一個(gè)在設(shè)計(jì)上非常不同的公有鏈協(xié)議,也是網(wǎng)絡(luò)中的基礎(chǔ)層,是整個(gè)加密經(jīng)濟(jì)網(wǎng)絡(luò)的信任引擎。主要指責(zé)是記錄和更新本地累計(jì)工作量最高的鏈,并維護(hù)鏈上數(shù)據(jù)的索引。 說(shuō)到猿起,這些心里的想法能追溯到 2016 年,甚至更早。2017 年,包括分層的網(wǎng)絡(luò)以及一個(gè)作為共同知識(shí)庫(kù)(Common Knowledge Base)的區(qū)塊鏈,都已經(jīng)成...
閱讀 2105·2021-11-23 10:06
閱讀 3456·2021-11-11 16:54
閱讀 3337·2019-08-29 17:31
閱讀 3563·2019-08-29 17:05
閱讀 2166·2019-08-26 13:36
閱讀 2155·2019-08-26 12:17
閱讀 520·2019-08-26 12:12
閱讀 1668·2019-08-26 10:19