Skip to content

Instantly share code, notes, and snippets.

@adyliu
Created September 28, 2018 09:29
Show Gist options
  • Save adyliu/492503b94d0306371298f24e15481da4 to your computer and use it in GitHub Desktop.
Save adyliu/492503b94d0306371298f24e15481da4 to your computer and use it in GitHub Desktop.
sign transaction offline for eos transfer
package eos.sample;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.BasicConfigurator;
import io.jafka.jeos.EosApi;
import io.jafka.jeos.EosApiFactory;
import io.jafka.jeos.LocalApi;
import io.jafka.jeos.core.common.SignArg;
import io.jafka.jeos.core.common.transaction.PackedTransaction;
import io.jafka.jeos.core.common.transaction.SignedPackedTransaction;
import io.jafka.jeos.core.request.chain.transaction.PushTransactionRequest;
import io.jafka.jeos.core.response.chain.transaction.PushedTransaction;
/**
*
* @author adyliu ([email protected])
* @since Sep 28, 2018
*/
public class OfflineEOSTransfer {
public static void main(String[] args) throws Exception{
// --- get the current state of blockchain
EosApi eosApi = EosApiFactory.create("http://jungle.cryptolions.io:18888");
SignArg arg = eosApi.getSignArg(120);
System.out.println(eosApi.getObjectMapper().writeValueAsString(arg));
// --- sign the transation of token tansfer
String privateKey = "5JTKZ------";//replace the real private key
String from = "shijiebangmm";
String to = "womenshi1111";
String quantity = "0.2345 EOS";
String memo = "sent by eos sdk (https://github.com/adyliu/jeos";
LocalApi localApi = EosApiFactory.createLocalApi();
PushTransactionRequest req = localApi.transfer(arg, privateKey, from, to, quantity, memo);
System.out.println(localApi.getObjectMapper().writeValueAsString(req));
// --- push the signed-transaction to the blockchain
PushedTransaction pts = eosApi.pushTransaction(req);
System.out.println(localApi.getObjectMapper().writeValueAsString(pts));
}
}
@adyliu
Copy link
Author

adyliu commented Sep 28, 2018

got the result

{
  "head_block_num" : 16659499,
  "last_irreversible_block_num" : 16659166,
  "ref_block_prefix" : 4253062493,
  "head_block_time" : "2018-09-28T09:26:40.5",
  "chain_id" : "038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca",
  "expired_second" : 120
}
{
  "compression" : "none",
  "transaction" : {
    "expiration" : "2018-09-28T09:28:40.5",
    "ref_block_num" : 16659166,
    "ref_block_prefix" : 4253062493,
    "max_net_usage_words" : 0,
    "max_cpu_usage_ms" : 0,
    "delay_sec" : 0,
    "context_free_actions" : [ ],
    "actions" : [ {
      "account" : "eosio.token",
      "name" : "transfer",
      "authorization" : [ {
        "actor" : "shijiebangmm",
        "permission" : "active"
      } ],
      "data" : "20259be628f75cc3104208aee1a924e5290900000000000004454f53000000002f73656e7420627920656f732073646b202868747470733a2f2f6769746875622e636f6d2f6164796c69752f6a656f73"
    } ],
    "transaction_extensions" : [ ],
    "context_free_data" : [ ]
  },
  "signatures" : [ "SIG_K1_KjY8Cs8zVdg4MNjGdJ51v252V6YVUVnhyDuDGjLuhfsuv4GQTnmu9uV59SiLMmNAAiaSmT7orb1iyZfXYHES15MFWRiBvy" ]
}
{
  "processed" : {
    "action_traces" : [ {
      "act" : {
        "account" : "eosio.token",
        "authorization" : [ {
          "actor" : "shijiebangmm",
          "permission" : "active"
        } ],
        "data" : {
          "from" : "shijiebangmm",
          "to" : "womenshi1111",
          "quantity" : "0.2345 EOS",
          "memo" : "sent by eos sdk (https://github.com/adyliu/jeos"
        },
        "name" : "transfer",
        "hex_data" : "20259be628f75cc3104208aee1a924e5290900000000000004454f53000000002f73656e7420627920656f732073646b202868747470733a2f2f6769746875622e636f6d2f6164796c69752f6a656f73"
      },
      "console" : "",
      "cpu_usage" : 0,
      "elapsed" : 393,
      "inline_traces" : [ {
        "receipt" : {
          "receiver" : "shijiebangmm",
          "act_digest" : "f38ee0ae7efe03e4df18366b6693d6110151d34ed930d3a2336acb474397910e",
          "global_sequence" : 38956877,
          "recv_sequence" : 22,
          "auth_sequence" : [ [ "shijiebangmm", 26 ] ],
          "code_sequence" : 4,
          "abi_sequence" : 4
        },
        "act" : {
          "account" : "eosio.token",
          "name" : "transfer",
          "authorization" : [ {
            "actor" : "shijiebangmm",
            "permission" : "active"
          } ],
          "data" : {
            "from" : "shijiebangmm",
            "to" : "womenshi1111",
            "quantity" : "0.2345 EOS",
            "memo" : "sent by eos sdk (https://github.com/adyliu/jeos"
          },
          "hex_data" : "20259be628f75cc3104208aee1a924e5290900000000000004454f53000000002f73656e7420627920656f732073646b202868747470733a2f2f6769746875622e636f6d2f6164796c69752f6a656f73"
        },
        "context_free" : false,
        "elapsed" : 12,
        "cpu_usage" : 0,
        "console" : "",
        "total_cpu_usage" : 0,
        "trx_id" : "9cb5b98d2cda1bd9b0b226431acb2b72eaec7c82bc26f818aced022d82b0cd6c",
        "block_num" : 16659504,
        "block_time" : "2018-09-28T09:26:43.000",
        "producer_block_id" : null,
        "account_ram_deltas" : [ ],
        "inline_traces" : [ ]
      }, {
        "receipt" : {
          "receiver" : "womenshi1111",
          "act_digest" : "f38ee0ae7efe03e4df18366b6693d6110151d34ed930d3a2336acb474397910e",
          "global_sequence" : 38956878,
          "recv_sequence" : 9,
          "auth_sequence" : [ [ "shijiebangmm", 27 ] ],
          "code_sequence" : 4,
          "abi_sequence" : 4
        },
        "act" : {
          "account" : "eosio.token",
          "name" : "transfer",
          "authorization" : [ {
            "actor" : "shijiebangmm",
            "permission" : "active"
          } ],
          "data" : {
            "from" : "shijiebangmm",
            "to" : "womenshi1111",
            "quantity" : "0.2345 EOS",
            "memo" : "sent by eos sdk (https://github.com/adyliu/jeos"
          },
          "hex_data" : "20259be628f75cc3104208aee1a924e5290900000000000004454f53000000002f73656e7420627920656f732073646b202868747470733a2f2f6769746875622e636f6d2f6164796c69752f6a656f73"
        },
        "context_free" : false,
        "elapsed" : 21,
        "cpu_usage" : 0,
        "console" : "",
        "total_cpu_usage" : 0,
        "trx_id" : "9cb5b98d2cda1bd9b0b226431acb2b72eaec7c82bc26f818aced022d82b0cd6c",
        "block_num" : 16659504,
        "block_time" : "2018-09-28T09:26:43.000",
        "producer_block_id" : null,
        "account_ram_deltas" : [ ],
        "inline_traces" : [ ]
      } ],
      "receipt" : {
        "abi_sequence" : 4,
        "act_digest" : "f38ee0ae7efe03e4df18366b6693d6110151d34ed930d3a2336acb474397910e",
        "auth_sequence" : [ [ "shijiebangmm", "25" ] ],
        "code_sequence" : 4,
        "global_sequence" : 38956876,
        "receiver" : "eosio.token",
        "recv_sequence" : 1799678
      },
      "total_cpu_usage" : 0,
      "trx_id" : "9cb5b98d2cda1bd9b0b226431acb2b72eaec7c82bc26f818aced022d82b0cd6c"
    } ],
    "elapsed" : 956,
    "except" : null,
    "id" : "9cb5b98d2cda1bd9b0b226431acb2b72eaec7c82bc26f818aced022d82b0cd6c",
    "net_usage" : 176,
    "receipt" : {
      "cpu_usage_us" : 956,
      "net_usage_words" : 22,
      "status" : "executed"
    },
    "scheduled" : false
  },
  "transaction_id" : "9cb5b98d2cda1bd9b0b226431acb2b72eaec7c82bc26f818aced022d82b0cd6c"
}

@adyliu
Copy link
Author

adyliu commented Sep 28, 2018

@BhumikaB
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment