bytom中怎么锁定合约
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"bytom中怎么锁定合约",在日常操作中,相信很多人在bytom中怎么锁定合约问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"bytom中怎么锁定合约"
千家信息网最后更新 2026年11月02日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
以下是相关代码片段:
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 合约程序
以下是相关代码片段:
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
以下是相关代码片段:
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
以下是相关代码片段:
param=new JSONObject(); param.put("password","xxx"); param.put("transaction",data); sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");第六步:调用submit-transactions提交交易
以下是相关代码片段:
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
以下是相关代码片段:
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 传入获取生成的合约参数信息
以下是相关代码片段:
param=new JSONObject(); 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' } 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 }执行合约的参数构造如下:
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中怎么锁定合约"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
合约
代码
片段
参数
生成
学习
接口
资产
信息
官方
就是
文档
文章
更多
详情
交易
帮助
编译
输入
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
伟宏互联网科技
西安纸贵互联网科技有限公司
互联网科技行业发展
厦门中拓互联网络科技有限公司
南京欣网互联网科技
互联网 新科技
上海亿帐通互联网科技
左右互联网络科技
科技类的互联网平台
科技大学互联网专业好不好
互联网科技公司招聘
现代科技和互联网有关系吗
北京大学互联网科技怎么样
金融科技与互联网金融相同和不同
互联网还是高科技吗
智腾互联网科技有限公司
华夏互联网科技业
滨湖区互联网智能科技产品有哪些
易企秀互联网科技是做什么的
互联网的黑科技实验室
科技互联网企业
北京互联网科技
贷财互联网科技
长亮科技 互联网金融
上海电发互联网科技有限公司
消费医药和互联网科技
想开互联网科技公司怎么样
阿里互联网科技企业
烟台灵鸽互联网科技有限公司
重庆科技产业互联网有哪些