# Transaction Signature

You need to serialize the transaction object before signing.

# Serialize Transaction Object

Serialize the trade object, that is, convert the parameters in the transaction object into a binary array in a fixed order.

  • There are four basic types of parameters in the transaction object: Uint16, Uint32, Uint64, and String.
  • Since there is no Unsigned type in Java, BigInteger is used for all Numbers and is converted when serialized.
  • Serialization takes the form of little endian.
  • Other combination types need to be separated into basic types. For example, in the voting interface of JavaSDK, Map<String, BigInteger> objects are passed in, which are separated into String and joined together with Uint64.

Serialize the parameters in the order shown in the following table:

Parameter Name Parameter Type The Sample Value The Serialized Value(Hex)
tx_type Uint16 4 0x0400
tx_len Uint16 0 0x0000
tx_structure_version Uint32 0 0x00000000
to_ledger_id Uint16 0 0x0000
from_ledger_id Uint16 0 0x0000
tx_deposit Uint32 100000 0xa0860100
tx_expire_duration Uint16 100 0x6400
send_time_stamp Uint64 1602820177 0x5118895f00000000
tx_random_nonce Uint32 0 0x00000000
last_tx_nonce Uint64 2 0x0200000000000000
last_tx_hash String 0x2a21b09155cad8aa 0xaad8ca5591b0212a
challenge_proof String "" 0x00000000
ext String "" 0x00000000
note String "1231fsd" 0x0700000031323331667364
sender_action Object - -
receiver_action Object - -

sender_action parameters serialization:

Parameter Name Parameter Type The Sample Value The Serialized Value(Hex)
action_hash Uint32 0 0x00000000
action_type Uint16 0 0x0000
action_size Uint16 0 0x0000
tx_sender_account_addr String T8000066ab344963eaa071f9636faac26b0d1a39900325 0x26000000542d302d4c5261467961475a31697366634b6e684c78504c6f655046556d5932697946477636
action_name String "" 0x00000000
action_param String 0x000000008c00000000000000 0x0c000000000000008c00000000000000
action_ext String "" 0x00000000
action_authorization String "" 0x00000000

receiver_action parameters serialization:

Parameter Name Parameter Type The Sample Value The Serialized Value(Hex)
action_hash Uint32 0 0x00000000
action_type Uint16 6 0x0000
action_size Uint16 0 0x0000
tx_sender_account_addr String T8000085a8e8acd53c72dca85dcb002a6710796975b4ba 0x26000000542d302d4c617a4e7a7679484c70747a6450466b61796e4e484b7144593471585a3267435668
action_name String "" 0x00000000
action_param String 0x000000008c00000000000000 0x0c000000000000008c00000000000000
action_ext String "" 0x00000000
action_authorization String
Non-deployed application smart contract transactions:
""
0x00000000
To deploy user contract transactions, you need to pass in the user contract public key:
{"authorization":"0x0426dc404815d13f7d08017818825b5df1bd3bb61c3a84d44579427cac43bce4b906498135dfa42adf7ae73bf9885250f88188207974c5cb965611bb23464591eb"}
980000007b22617574686f72697a6174696f6e223a22307830343236646334303438313564313366376430383031373831383832356235646631626433626236316333613834643434353739343237636163343362636534623930363439383133356466613432616466376165373362663938383532353066383831383832303739373463356362393635363131626232333436343539316562227d

action_param serialization please refer to action_param serialization.

# Sign Transaction

Step

  1. Hash the serialized binary array by SHA256.
  2. Sign the result of step 1 with private key.

Algorithm

ECDSA(secp256k1)

Demo

public static String signData(byte[] dataBytes, BigInteger privateKey) throws Exception {
        ECKey ceKey = ECKey.fromPrivate(privateKey, false);
        Sha256Hash sha256Hash = Sha256Hash.wrap(dataBytes);
        ECKey.ECDSASignature sig = ceKey.sign(sha256Hash);

        byte recId = ceKey.findRecoveryId(sha256Hash, sig);

        String authHex = TextUtils.bytesToHex(sig.r.toByteArray()) + TextUtils.bytesToHex(sig.s.toByteArray());
        if (authHex.length() == 130) {
            if(recId == 1 && "00".equals(authHex.subText(0, 2))) {
                authHex = authHex.replaceFirst("00", "01");
            }
            return "0x" + authHex;
        }
        if(recId == 1) {
            authHex = "01" + authHex;
        } else {
            authHex = "00" + authHex;
        }
        return "0x" + authHex;
    }

Result of Signature

0x015c5be7876376b09297f9247e34e1e921474f2f66c61700e57832546e6c1de918136abe1a56c346ebf2dce9358941df89d02fd5274222435e7bd0f756de1adf83