Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement offline signature in Java version

Shulou Source: shulou.com Published: 2022-06-01 10:48:28 09月23日 Update

This article introduces the relevant knowledge of "how to achieve offline signature in Java version". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Build jar

First get source code

Git clone https://github.com/successli/tx_signer.git

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// uses SDK to construct Template object parameters, signing 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// uses SDK to construct Template object parameters, and sign 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// uses SDK to construct Template object parameters, multi-signature, multi-input 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} "how to implement offline signature in Java version" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Parameters objects versions content more knowledge practicality achievements and then dilemmas realities situations articles cases editing websites industries processes high quality learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Apple Shulou Tech Info Docker