千家信息网

Java版本离线签名怎么实现

发表于:2024-11-24 作者:千家信息网编辑
千家信息网最后更新 2024年11月24日,本篇内容介绍了"Java版本离线签名怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Build
千家信息网最后更新 2024年11月24日Java版本离线签名怎么实现

本篇内容介绍了"Java版本离线签名怎么实现"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Build jar
  1. first get source code

    git clone https://github.com/successli/tx_signer.git
  2. get jar package

    $ mvn assembly:assembly -Dmaven.test.skip=true


    You can get a jar with dependencies, and you can use it in your project.

Test cases

Need 3 Parameters:

  • Private Keys Array

  • Template Object

  • After call build transaction api return a Template json object. build transaction api

  • use bytom java sdk return a Template object.

  • Raw Transaction

  • call decode raw-transaction api from dev branch. decode raw-transaction api

Call method:

// return a Template object signed offline basically.Template result = signatures.generateSignatures(privates, template, rawTransaction);// use result's raw_transaction call sign transaction api to build another data but not need password or private key.

Single-key Example:

@Test// 使用 SDK 来构造 Template 对象参数, 单签public void testSignSingleKey() throws BytomException {    Client client = Client.generateClient();    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";    String address = "sm1qvyus3s5d7jv782syuqe3qrh75fx23lgpzf33em";    // build transaction obtain a Template object    Template template = new Transaction.Builder()        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G0NLBNU00A02")        .setAssetId(asset_id)        .setAmount(40000000)    )        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G0NLBNU00A02")        .setAssetId(asset_id)        .setAmount(300000000)    )        .addAction(        new Transaction.Action.ControlWithAddress()        .setAddress(address)        .setAssetId(asset_id)        .setAmount(30000000)    ).build(client);    logger.info("template: " + template.toJson());    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);    logger.info("decodeTx: " + decodedTx.toJson());    // need a private key array    String[] privateKeys = new String[]{"10fdbc41a4d3b8e5a0f50dd3905c1660e7476d4db3dbd9454fa4347500a633531c487e8174ffc0cfa76c3be6833111a9b8cd94446e37a76ee18bb21a7d6ea66b"};    logger.info("private key:" + privateKeys[0]);    // call offline sign method to obtain a basic offline signed template    Signatures signatures = new SignaturesImpl();    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);    logger.info("basic signed raw: " + basicSigned.toJson());    // call sign transaction api to calculate whole raw_transaction id    // sign password is None or another random String    Template result = new Transaction.SignerBuilder().sign(client,                                                           basicSigned, "");    logger.info("result raw_transaction: " + result.toJson());    // success to submit transaction}

Multi-keys Example:

Need an account has two or more keys.

@Test// 使用 SDK 来构造 Template 对象参数, 多签public void testSignMultiKeys() throws BytomException {    Client client = Client.generateClient();    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";    String address = "sm1qvyus3s5d7jv782syuqe3qrh75fx23lgpzf33em";    // build transaction obtain a Template object    // account 0G1RPP6OG0A06 has two keys    Template template = new Transaction.Builder()        .setTtl(10)        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1RPP6OG0A06")        .setAssetId(asset_id)        .setAmount(40000000)    )        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1RPP6OG0A06")        .setAssetId(asset_id)        .setAmount(300000000)    )        .addAction(        new Transaction.Action.ControlWithAddress()        .setAddress(address)        .setAssetId(asset_id)        .setAmount(30000000)    ).build(client);    logger.info("template: " + template.toJson());    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);    logger.info("decodeTx: " + decodedTx.toJson());    // need a private key array    String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",                                        "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b"};    logger.info("private key 1:" + privateKeys[0]);    logger.info("private key 2:" + privateKeys[1]);    // call offline sign method to obtain a basic offline signed template    Signatures signatures = new SignaturesImpl();    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);    logger.info("basic signed raw: " + basicSigned.toJson());    // call sign transaction api to calculate whole raw_transaction id    // sign password is None or another random String    Template result = new Transaction.SignerBuilder().sign(client,                                                           basicSigned, "");    logger.info("result raw_transaction: " + result.toJson());    // success to submit transaction}

Multi-keys and Multi-inputs Example:

@Test// 使用 SDK 来构造 Template 对象参数, 多签, 多输入public void testSignMultiKeysMultiInputs() throws BytomException {    Client client = Client.generateClient();    String asset_id = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";    String address = "sm1qvyus3s5d7jv782syuqe3qrh75fx23lgpzf33em";    // build transaction obtain a Template object    Template template = new Transaction.Builder()        .setTtl(10)        // 1 input        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1RPP6OG0A06") // Multi-keys account        .setAssetId(asset_id)        .setAmount(40000000)    )        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1RPP6OG0A06")        .setAssetId(asset_id)        .setAmount(300000000)    )    // 2 input        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1Q6V1P00A02") // Multi-keys account        .setAssetId(asset_id)        .setAmount(40000000)    )        .addAction(        new Transaction.Action.SpendFromAccount()        .setAccountId("0G1Q6V1P00A02")        .setAssetId(asset_id)        .setAmount(300000000)    )        .addAction(        new Transaction.Action.ControlWithAddress()        .setAddress(address)        .setAssetId(asset_id)        .setAmount(60000000)    ).build(client);    logger.info("template: " + template.toJson());    // use Template object's raw_transaction id to decode raw_transaction obtain a RawTransaction object    RawTransaction decodedTx = RawTransaction.decode(client, template.rawTransaction);    logger.info("decodeTx: " + decodedTx.toJson());    // need a private key array    String[] privateKeys = new String[]{"08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67",                                        "40c821f736f60805ad59b1fea158762fa6355e258601dfb49dda6f672092ae5adf072d5cab2ceaaa0d68dd3fe7fa04869d95afed8c20069f446a338576901e1b",                                        "08bdbd6c22856c5747c930f64d0e5d58ded17c4473910c6c0c3f94e485833a436247976253c8e29e961041ad8dfad9309744255364323163837cbef2483b4f67"};    logger.info("private key 1:" + privateKeys[0]);    logger.info("private key 2:" + privateKeys[1]);    // call offline sign method to obtain a basic offline signed template    Signatures signatures = new SignaturesImpl();    Template basicSigned = signatures.generateSignatures(privateKeys, template, decodedTx);    logger.info("basic signed raw: " + basicSigned.toJson());    // call sign transaction api to calculate whole raw_transaction id    // sign password is None or another random String    Template result = new Transaction.SignerBuilder().sign(client,                                                           basicSigned, "");    logger.info("result raw_transaction: " + result.toJson());    // success to submit transaction}

"Java版本离线签名怎么实现"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

参数 对象 版本 内容 更多 知识 实用 学有所成 接下来 困境 实际 情况 文章 案例 编带 网站 行业 过程 高质量 学习 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全保障框架模型 网络技术在幼儿园科学教育中 剪辑键盘软件开发 湖州电脑软件开发就业公司 苹果8P退ID时无服务器 vf数据库中显示部分字段数据 数据库如何存字符串数组 网络安全进校园项目成果简介 软件开发项目风险如何折算 网络安全鉴别小妙招 视频监控系统流媒体服务器搭建 杭州立盛软件开发公司 思迅商云8关联数据库 软件开发后期维护免税怎么开票 无线网络技术数字信息技术 血站网络安全的相关知识 软件开发过程中会有哪些风险 数据库大小写错误怎么办 更改数据库密码 文明重启推荐买服务器吗 新九善互联网科技有限 当代网络安全作文800字 网络安全 考研参考教程 网络爬虫软件开发 山西数字化城管软件开发公司 民法典如何保护网络安全 广州探图网络技术公司 网络安全法关于运营商 和龙软件开发哪里靠谱 psql语录连接数据库
0