bytom中怎么鎖定合約

這篇文章主要介紹“bytom中怎么鎖定合約”,在日常操作中,相信很多人在bytom中怎么鎖定合約問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”bytom中怎么鎖定合約”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

目前創(chuàng)新互聯(lián)已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運營、企業(yè)網(wǎng)站設(shè)計、牧野網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

模板如下:

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)用create-account-receiver 生成 control_program

bytom中怎么鎖定合約 bytom中怎么鎖定合約

以下是相關(guān)代碼片段:

sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");

第二步調(diào)用list-pubkeys 獲取 pubkey

bytom中怎么鎖定合約

bytom中怎么鎖定合約

以下是相關(guān)代碼片段:

sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");

第三步: 將1 2步獲取的值調(diào)用compile接口編譯合約獲得program 合約程序

bytom中怎么鎖定合約

bytom中怎么鎖定合約

以下是相關(guān)代碼片段:

            JSONObject param=new JSONObject();
            JSONArray agrs=new JSONArray();
            //合約的四個參數(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一個交易的到data

bytom中怎么鎖定合約 bytom中怎么鎖定合約

以下是相關(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

bytom中怎么鎖定合約 bytom中怎么鎖定合約

以下是相關(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提交交易

bytom中怎么鎖定合約

以下是相關(guān)代碼片段:

            param=new JSONObject();
            param.put("raw_transaction",raw_transaction);
            sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");

解鎖/取消合約

首先需要decode出生成合約時候的參數(shù)

調(diào)用list-unspent-outputs 獲取生成的合約信息獲取program

bytom中怎么鎖定合約

以下是相關(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ù)信息

bytom中怎么鎖定合約

以下是相關(guān)代碼片段:

param=new JSONObject();
        param.put("program",program);
        sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");

需要注意的是decode出來的為值是逆序的(后續(xù)會有文章詳細(xì)介紹)

解鎖/取消其實就是把生成合約的步驟中的第三步去掉,替換調(diào)用生成合約第四步的參數(shù)即可

取消合約的構(gòu)造參數(shù)如下: bytom中怎么鎖定合約

                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ù)偏移量 在一個合約里是固定的 
                    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)造如下: bytom中怎么鎖定合約

           const spendAccountUnspentOutput = {
                arguments: [{
                  type: 'data',
                  raw_data: {
                    //  00000000 指的是第一個 clause,表示直接執(zhí)行,無需跳轉(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'
              }
              // 礦工費
              const gasAction = {
                type: 'spend_account',
                account_id: account_id,
                asset_alias: 'BTM',
                amount: 50000000
              }
              // 合約執(zhí)行獲得資產(chǎn)對象
              const controlAction = {
                type: 'control_program',
                amount:  100000000,
                asset_id: asset_id,
                control_program: compileData.control_program
              }

build 操作其實就是指定輸入輸出的過程,詳情請查看 官方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();
    }

到此,關(guān)于“bytom中怎么鎖定合約”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

分享文章:bytom中怎么鎖定合約
當(dāng)前URL:http://muchs.cn/article42/jpiiec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名品牌網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站策劃、網(wǎng)頁設(shè)計公司、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名