Set up the service

You need to build a service for signature generation.

Here, the Node example is used to show how to generate a valid signature. Please note that the parameters should be obtained in the same way as in the example.

The signature:

// generateSign.js
const CryptoJS = require("crypto-js");
const config = require("../config/base");

const appKey = config.appKey;
const appSecret = config.appSecret;

module.exports = (paramJson = {}, headers = {}) => {
  let strBody = "";
  let keysBody = Object.keys(paramJson);
  keysBody = keysBody.sort();
  for (const key of keysBody) {
    const val = paramJson[key];
    strBody += `${key}=${val}`;
  }

  let param = Object.assign({}, headers, {
    appKey,
  });

  let strHeader = "";
  let headerKeys = Object.keys(param);
  headerKeys = headerKeys.sort();
  for (const key of headerKeys) {
    if (key !== "signature") {
      const val = param[key];
      strHeader += `${key}=${val}`;
    }
  }

  str = strBody + strHeader;

  const sign = CryptoJS.HmacSHA256(str, appSecret);
  return sign;
};

The paramJson and headers parameter are required.

Request method:

// controller.js

const generateSign = require('../../utils/generateSign')
const config = require("../../config/base")

// post /getSign
const getSign = async (ctx) => {
    const headers = ctx.headers
    const bodyParams = ctx.request.body

    const sign = generateSign(bodyParams, {
        expireTime: headers.expiretime,
        timestamp: headers.timestamp
    })

    ctx.body = {
        appKey: config.appKey,
        sign: sign.toString()
    }
}

module.exports = {
    getSign

The request method getSign must be POST. The accepted arguments must be of the form body and headers. Method return values must be appKey and sign.

The second parameter of the generateSign signature method must be named expireTime and timestamp.

When you after the success of the service deployment, you may get a url like this: http://localhost:3000/getSign. You can then use the resulting URL for signUrl in Set Up the SDK:

import Chainge from '@chainge/sdk';

const config = {
    signUrl: 'http://localhost:3000/getSign', // required. 
    expireTime: 5000, // optional. The default value is 5000 ms, range 1000 ~ 20000
}

const chainge = new Chainge(config);

If you follow this procedure you get to here. Congratulations, your SDK instance is now available.

Next Steps

Check out how to use.

Last updated