Encoding

Due to the large amount of information submitted and contract optimization considerations, we store the data of the check using a single uint256 field...

cheque data encoding

Field NameLengthBit IndexReverseMaskNote

distributionId

32

0+32

224

4294967295

Bound distribution ID; uniquely identifies a distribution activity.

participationLimit

8

32+8

216

255

Participant restriction: 1. Any user, 2. Checked, 3. Others. Determine who can participate in the distribution activity.

minHardCap

8

40+8

208

255

Minimum hard cap value for the system, a positive integer from 1 to 100, where 1 equals 1/100; minimum funding target for the distribution activity.

maxHardCap

8

48+8

200

255

Maximum hard cap value for the system, a positive integer from 1 to 100, where 1 equals 1/100; maximum funding target for the distribution activity.

releaseRatio

8

56+8

192

255

Release ratio, a positive integer from 1 to 100, where 1 equals 1/100; determines the proportion of funds released to users.

releaseCycle

32

64+32

160

18446744073709551615

Unlocking period, 10-digit timestamp; time period for fund unlocking.

padding

64

96+64

128

18446744073709551615

Reserved for future extension; reserved field, currently unused, for future expansion.

financingAmount

64

128+64

64

18446744073709551615

Funding capital; total amount of funds involved in the distribution activity.

maxPerUser

64

192+64

0

18446744073709551615

Maximum amount of funds a single user can participate with during the check process.

Explanation:

  • Bit Index and Reverse Column: Provide the position and offset of each field within the bit field.

  • Mask Column: Provides the mask values used for bitwise operations to extract specific fields from the overall data.

  • Annotation Column: Provides detailed descriptions of each field and their roles within the system.

  • Length in Table: Indicates the number of bits each field occupies within the bit field.

  • Bit Index and Reverse Column: Used to calculate the exact position of each field within the bit field.

  • Mask Column: Used for bitwise operations on the bit field to ensure extraction or modification of relevant bits only.

JavaScript Encoding and Decoding Test


/** Struct ChequeStruct is used to store information about the cheque.
struct ChequeStruct {
    uint32 distributionId; // Distribution ID to which the cheque is linked
    uint8 participationLimit; // Participant limit: 1 any user, 2 expert, 3 other
    uint8 minHardCap; // Minimum system hard cap, integer from 1 to 100 where 1=1/100
    uint8 maxHardCap; // Maximum system hard cap, integer from 1 to 100 where 1=1/100
    uint8 releaseRatio; // Release ratio, integer from 1 to 100 where 1=1/100
    uint32 releaseCycle; // Unlock cycle in seconds; 1 minute = 60 seconds, 1 day = 86400 seconds
    uint32 padding; // Padding data, meaningless, set to 0
    uint64 financingAmount; // Financing amount; set to 0 if not needed, 100 tokens = 100 * 1e18 / 1e10
    uint64 maxPerUser; // Maximum per user check limit, 100 tokens = 100 * 1e18 / 1e10
}
*/


var bigInt = require("big-integer");

function hideDataDis1(distribution) {
    let targetId = bigInt(distribution.targetId);
    let participationLimit = bigInt(distribution.participationLimit);
    let minHardCap = bigInt(distribution.minHardCap);
    let maxHardCap = bigInt(distribution.maxHardCap);
    let releaseRatio = bigInt(distribution.releaseRatio);
    let releaseCycle = bigInt(distribution.releaseCycle);
    let padding = bigInt(distribution.padding);
    let financingAmount = bigInt(distribution.financingAmount);
    let maxPerUser = bigInt(distribution.maxPerUser);
    // Incorrect bit shifting direction
    let hiddenData = targetId.shiftLeft(8)
        .or(participationLimit).shiftLeft(8)
        .or(minHardCap).shiftLeft(8)
        .or(maxHardCap).shiftLeft(8)
        .or(releaseRatio).shiftLeft(32)
        .or(releaseCycle).shiftLeft(32)
        .or(padding).shiftLeft(64)
        .or(financingAmount).shiftLeft(64)
        .or(maxPerUser);

    return hiddenData.toString(10);
}

function revealDataDis1(hiddenData) {
    let data = bigInt(hiddenData);

    let maxPerUser = data.and('18446744073709551615');
    data = data.shiftRight(64);

    let financingAmount = data.and('18446744073709551615');
    data = data.shiftRight(64);

    let padding = data.and('4294967295');
    data = data.shiftRight(32);

    let releaseCycle = data.and('4294967295');
    data = data.shiftRight(32);

    let releaseRatio = data.and('255');
    data = data.shiftRight(8);

    let maxHardCap = data.and('255');
    data = data.shiftRight(8);

    let minHardCap = data.and('255');
    data = data.shiftRight(8);

    let participationLimit = data.and('255');
    data = data.shiftRight(8);

    let targetIdLower = data.and('4294967295').toString();
    data = data.shiftRight(32);

    let targetIdUpper = data.and('4294967295').toString();
    let targetId = targetIdUpper + targetIdLower;

    return {
        targetId: parseInt(targetId),
        participationLimit: parseInt(participationLimit.toString()),
        minHardCap: parseInt(minHardCap.toString()),
        maxHardCap: parseInt(maxHardCap.toString()),
        releaseRatio: parseInt(releaseRatio.toString()),
        releaseCycle: parseInt(releaseCycle.toString()),
        padding: parseInt(padding.toString()),
        financingAmount: parseInt(financingAmount.toString()),
        maxPerUser: parseInt(maxPerUser.toString())
    };
}

// Test data
let chequeData = {
    targetId: 12345,
    participationLimit: 1,
    minHardCap: 5,
    maxHardCap: 10,
    releaseRatio: 20,
    releaseCycle: 1000000000,
    padding: 123,
    financingAmount: 5000000000,
    maxPerUser: 10000000000
};

let encodedData = hideDataDis1(chequeData);
// 332820648991343397452793519134652897661010861321068696474042534556591104
console.log("Encoded Data:", encodedData);

let decodedData = revealDataDis1(encodedData);
console.log("Decoded Data:", decodedData);


Last updated