bytom中怎么锁定合约

免费建站   2024年05月09日 10:45  

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

模板如下:

contractTradeOffer(assetRequested:Asset,amountRequested:Amount,seller:Program,cancelKey:PublicKey)locksoffered{clausetrade()requirespayment:amountRequestedofassetRequested{lockpaymentwithsellerunlockoffered}clausecancel(sellerSig:Signature){verifycheckTxSig(cancelKey,sellerSig)unlockoffered}}锁定合约第一步:调用create-account-receiver 生成 control_program

以下是相关代码片段:

sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");第二步调用list-pubkeys 获取 pubkey

以下是相关代码片段:

sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序

以下是相关代码片段:

JSONObjectparam=newJSONObject();JSONArrayagrs=newJSONArray();//合约的四个参数值JSONObjectassetParam=newJSONObject();assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");agrs.put(assetParam);JSONObjectamountParam=newJSONObject();amountParam.put("integer",200000000l);agrs.put(amountParam);JSONObjectprogramParam=newJSONObject();programParam.put("string",control_program);agrs.put(programParam);JSONObjectpublicKeyParam=newJSONObject();publicKeyParam.put("string",pubkey);agrs.put(publicKeyParam);param.put("agrs",agrs);param.put("contract","contractTradeOffer(assetRequested:Asset,amountRequested:Amount,seller:Program,cancelKey:PublicKey)locksoffered{clausetrade()requirespayment:amountRequestedofassetRequested{lockpaymentwithsellerunlockoffered}clausecancel(sellerSig:Signature){verifycheckTxSig(cancelKey,sellerSig)unlockoffered}}");//调用编译合约sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");第四步:将program 传入build-transaction接口去build一个交易的到data

以下是相关代码片段:

param=newJSONObject();agrs=newJSONArray();JSONObjectspendAccount=newJSONObject();spendAccount.put("account_id","0H757LPD00A02");spendAccount.put("amount",9909099090000l);spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");spendAccount.put("type","spend_account");agrs.put(spendAccount);JSONObjectcontrolAccount=newJSONObject();controlAccount.put("control_program",program);controlAccount.put("amount",9909099090000l);controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");controlAccount.put("type","control_program");agrs.put(controlAccount);JSONObjectspendAccount2=newJSONObject();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

以下是相关代码片段:

param=newJSONObject();param.put("password","xxx");param.put("transaction",data);sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");第六步:调用submit-transactions提交交易

以下是相关代码片段:

param=newJSONObject();param.put("raw_transaction",raw_transaction);sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");解锁/取消合约首先需要decode出生成合约时候的参数调用list-unspent-outputs 获取生成的合约信息获取program

以下是相关代码片段:

param=newJSONObject();param.put("id",outputid);param.put("smart_contract",true);sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");调用decode-program 传入获取生成的合约参数信息

以下是相关代码片段:

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

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

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

取消合约的构造参数如下:

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'}constcontrolAction={type:'control_program',amount:100000000,asset_id:asset_id,control_program:control_program}constgasAction={type:'spend_account',account_id:account_id,asset_alias:'BTM',amount:50000000}

执行合约的参数构造如下:

constspendAccountUnspentOutput={arguments:[{type:'data',raw_data:{//00000000指的是第一个clause,表示直接执行,无需跳转value:'00000000'}}],output_id:output_id,type:'spend_account_unspent_output'}//合约执行提供的资产constissueControlAction={control_program:control_program,amount:100000000,asset_id:asset_id,type:'control_program'}//合约执行提供的资产constissueSpendAction={account_id:account_id,amount:100000000,asset_id:asset_id,type:'spend_account'}//矿工费constgasAction={type:'spend_account',account_id:account_id,asset_alias:'BTM',amount:50000000}//合约执行获得资产对象constcontrolAction={type:'control_program',amount:100000000,asset_id:asset_id,control_program:compileData.control_program}

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

备注

调用比原基于okhttp接口javautil 如下:

publicstaticStringsendHttpPost(StringbodyStr,Stringmethod,StringbytomApiserverUrl,StringbytomApiserverToken)throwsIOException{OkHttpClientclient=newOkHttpClient();MediaTypemediaType=MediaType.parse("application/json");RequestBodybody=RequestBody.create(mediaType,bodyStr);Requestrequest=newRequest.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")));StringauthHeader="Basic"+newString(encodedAuth);request=newRequest.Builder().url(bytomApiserverUrl+"/"+method).post(body).addHeader("authorization",authHeader).addHeader("cache-control","no-cache").addHeader("Connection","close").build();}Responseresponse=client.newCall(request).execute();returnresponse.body().string();}

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

域名注册
购买VPS主机

您或许对下面这些文章有兴趣:                    本月吐槽辛苦排行榜

看贴要回贴有N种理由!看帖不回贴的后果你懂得的!


评论内容 (*必填):
(Ctrl + Enter提交)   

部落快速搜索栏

各类专题梳理

网站导航栏

X
返回顶部