大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

bytom中怎么锁定合约

这篇文章主要介绍“bytom中怎么锁定合约”,在日常操作中,相信很多人在bytom中怎么锁定合约问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”bytom中怎么锁定合约”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

目前创新互联已为上千家的企业提供了网站建设、域名、虚拟空间、网站托管运营、企业网站设计、牧野网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

模板如下:

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
  }
}

锁定合约

第一步:调用create-account-receiver 生成 control_program

bytom中怎么锁定合约 bytom中怎么锁定合约

以下是相关代码片段:

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

第二步调用list-pubkeys 获取 pubkey

bytom中怎么锁定合约

bytom中怎么锁定合约

以下是相关代码片段:

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

第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序

bytom中怎么锁定合约

bytom中怎么锁定合约

以下是相关代码片段:

            JSONObject param=new JSONObject();
            JSONArray agrs=new JSONArray();
            //合约的四个参数值
            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 } }");
            //调用编译合约
            sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");

第四步:将program 传入build-transaction接口去build一个交易的到data

bytom中怎么锁定合约 bytom中怎么锁定合约

以下是相关代码片段:

            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","");

第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction

bytom中怎么锁定合约 bytom中怎么锁定合约

以下是相关代码片段:

            param=new JSONObject();
            param.put("password","xxx");
            param.put("transaction",data);
            sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");

第六步:调用submit-transactions提交交易

bytom中怎么锁定合约

以下是相关代码片段:

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

解锁/取消合约

首先需要decode出生成合约时候的参数

调用list-unspent-outputs 获取生成的合约信息获取program

bytom中怎么锁定合约

以下是相关代码片段:

param=new JSONObject();
        param.put("id",outputid);
        param.put("smart_contract",true);
        sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");

调用decode-program 传入获取生成的合约参数信息

bytom中怎么锁定合约

以下是相关代码片段:

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

需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)

解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可

取消合约的构造参数如下: 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: {
                    // 参数偏移量 在一个合约里是固定的 
                    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
              }

执行合约的参数构造如下: bytom中怎么锁定合约

           const spendAccountUnspentOutput = {
                arguments: [{
                  type: 'data',
                  raw_data: {
                    //  00000000 指的是第一个 clause,表示直接执行,无需跳转
                    value: '00000000'
                  }
                }],
                output_id: output_id,
                type: 'spend_account_unspent_output'
              }
              // 合约执行提供的资产
              const issueControlAction = {
                control_program: control_program,
                amount:  100000000,
                asset_id: asset_id,
                type: 'control_program'
              }
              // 合约执行提供的资产
              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
              }
              // 合约执行获得资产对象
              const controlAction = {
                type: 'control_program',
                amount:  100000000,
                asset_id: asset_id,
                control_program: compileData.control_program
              }

build 操作其实就是指定输入输出的过程,详情请查看 官方build文档 和 官方api文档

备注

调用比原基于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();
    }

到此,关于“bytom中怎么锁定合约”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前名称:bytom中怎么锁定合约
标题网址:http://dzwzjz.com/article/jpiiec.html
在线咨询
服务热线
服务热线:028-86922220
TOP