Ramp 签名说明
适用于 On Ramp/Off Ramp 接口
签名参数
参数名 | 说明 | 备注 |
---|---|---|
timestamp | ⼗三位时间戳 | 1538054050234 |
httpMethod | 请求方式 GET /POST | 请求方式须大写 |
requestPath | 请求路径 | buy:/index/rampPageBuy sell: /index/rampPageSell |
bodyString | 请求参数 | GET:为空 POST:请求参数 |
第一步:生成加密串
签名字符串固定为:timestamp + httpMethod + requestPath + bodyString
其中 requestPath 中的参数,依据参数名使⽤字典顺序排序,去掉空值。
最后,使用 HMAC SHA256 ⽅法⽤ SecretKey 加密,通过 Base64 编码得到 sign。
On Ramp 签名示例
请求链接:
https://ramptest.alchemypay.org?appId=f83Is2y7L425rxl8&crypto=USDT&network=ETH&showTable=buy&fiat=USD&fiatAmount=30×tamp=1538054050234&sign=JY9JcOwBosncT19Nn9DIfTH%2BvfSt6xL%2BI%2BRVCl9YGgE%3D
- timestamp:
1538054050234
- httpMethod:
GET
- requestPath:
/index/rampPageBuy
- bodyString: 为空
签名字符串为:
1538054050234GET/index/rampPageBuy?appId=f83Is2y7L425rxl8&crypto=USDT&fiat=USD&fiatAmount=30&network=ETH&showTable=buy×tamp=1538054050234
Off Ramp 签名示例
请求链接:
https://ramptest.alchemypay.org?appId=f83Is2y7L425rxl8&crypto=USDT&network=ETH&showTable=sell&fiat=USD&cryptoAmount=30×tamp=1538054050234&sign=615hNootKL4aScndVHxqRnuZzoLDCJU%2FBzhHj913qlk%3D
- timestamp:
1538054050234
- httpMethod:
GET
- requestPath:
/index/rampPageSell
- bodyString: 为空
签名字符串为:
1538054050234GET/index/rampPageSell?appId=f83Is2y7L425rxl8&crypto=USDT&cryptoAmount=30&fiat=USD&network=ETH&showTable=sell×tamp=1538054050234
第二步:生成您的签名
将加密串和 appsecret 作为参数按照以下方法生成签名
package v4.onramp;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
//example for onramp
public class V4OnrampPage{
public static void main(String[] args) throws Exception {
// Generate current timestamp in milliseconds
String timestamp = String.valueOf(System.currentTimeMillis());
String httpMethod = "GET";
String requestPath = "/index/rampPageBuy";// onramo:/index/rampPageBuy offramp:/index/rampPageSell
String secretKey = "*****"; // relpace with your secretKey
String appId = "*****"; // relpace with your appId
//Replace with the token from the getToken API.
//String token = "****";
// Request parameters
Map<String, String> paramsToSign = new TreeMap<>();
paramsToSign.put("crypto", "USDT");
//paramsToSign.put("token", token);
paramsToSign.put("fiatAmount", "200");
paramsToSign.put("fiat", "USD");
paramsToSign.put("merchantOrderNo", "1731231234567890123455");//Cannot be repeated
paramsToSign.put("network", "BSC");
paramsToSign.put("callbackUrl", "https://google.com");
paramsToSign.put("redirectUrl", "https://google.com");
paramsToSign.put("timestamp", timestamp);
paramsToSign.put("appId", appId);
// Create the string to sign
String rawDataToSign = getStringToSign(paramsToSign);
requestPath += "?" + rawDataToSign;
// Generate the signature
String signature = generateSignature(timestamp, httpMethod, requestPath, secretKey);
// urlencode token parameter
//paramsToSign.put("token",URLEncoder.encode(token, "UTF-8"));
String rawData = getStringToSign(paramsToSign);
// Print the results
System.out.println("On Ramp Signature: " + signature);
System.out.println("final link: " + "https://ramptest.alchemypay.org?" + rawData + "&sign=" + signature);
}
private static String generateSignature(String timestamp, String httpMethod, String requestPath, String secretKey) throws Exception {
String signatureString = timestamp + httpMethod + requestPath;
System.out.println("sign-content: " + signatureString);
Mac sha256Hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256Hmac.init(secretKeySpec);
byte[] hash = sha256Hmac.doFinal(signatureString.getBytes(StandardCharsets.UTF_8));
String signature = java.util.Base64.getEncoder().encodeToString(hash);
System.out.println("sign-String: " + signature);
return URLEncoder.encode(signature, StandardCharsets.UTF_8.toString());
}
private static String getStringToSign(Map<String, String> params) {
return params.entrySet().stream()
.filter(entry -> entry.getValue() != null && !entry.getValue().isEmpty())
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
}
}
<?php
function generateSignature($timestamp, $httpMethod, $requestPath, $secretKey) {
// Concatenate parameters for signature string
$signatureString = $timestamp . $httpMethod . $requestPath ;
echo "sign-content:".$signatureString.PHP_EOL;
// Generate HMAC SHA256 signature using the secret key
$signature = base64_encode(hash_hmac('sha256', $signatureString, $secretKey, true));
echo "sign-String:".$signature.PHP_EOL;
return urlencode($signature);
}
//Sort the parameters in lexicographical order
function getStringToSign($params) {
ksort($params);
$s2s = '';
foreach ($params as $k => $v) {
if (is_array($v)) {
continue;
} else if (!empty($v)) {
$s2s .= "{$k}={$v}&";
}
}
return rtrim($s2s, '&');
}
// Example for On Ramp
$onRampHttpMethod = "GET";
$onRampRequestPath = "/index/rampPageBuy";//onRamp use this: '/index/rampPageBuy' and offRamp use this:' /index/rampPageSell'
$timestamp = strval(time() * 1000);
$appId= "*****";//replace it with your appId
$appSecret = "*****";// replace it with your appSecret
//request parameters
$paramsToSign = array(
'crypto' => 'USDT',
'network' => 'TRX',
'showTable' => 'buy',
'fiat' => 'USD',
'fiatAmount' => '30',
'merchantOrderNo' => '123123',//Modify it, and it must not be repeated.
'timestamp' => $timestamp,
'appId' => $appId,
);
$rawDataToSign = getStringToSign($paramsToSign);
$onRampRequestPath .= "?".$rawDataToSign;
$onRampSignature = generateSignature($timestamp, $onRampHttpMethod, $onRampRequestPath, $appSecret);
echo "On Ramp Signature: " . $onRampSignature .PHP_EOL;
echo "final link: "."https://ramptest.alchemypay.org?".$rawDataToSign."&sign=".$onRampSignature.PHP_EOL;
?>
const crypto = require('crypto');
//const querystring = require('querystring');
// Function to generate HMAC SHA256 signature
function generateSignature(timestamp, httpMethod, requestPath, secretKey) {
// Concatenate parameters for signature string
const signatureString = timestamp + httpMethod + requestPath;
console.log("sign-content:" + signatureString);
// Generate HMAC SHA256 signature using the secret key
const hmac = crypto.createHmac('sha256', secretKey);
hmac.update(signatureString);
const signature = hmac.digest('base64');
return encodeURIComponent(signature);
}
// Function to sort parameters and return a string to sign
function getStringToSign(params) {
const sortedKeys = Object.keys(params).sort();
const s2s = sortedKeys
.map(key => {
const value = params[key];
if (Array.isArray(value) || value === '') {
return null;
}
return `${key}=${value}`;
})
.filter(Boolean)
.join('&');
return s2s;
}
// Example for On Ramp
const onRampHttpMethod = 'GET';
const onRampRequestPath = '/index/rampPageBuy';
const timestamp = String(Date.now());
const appId = '******';//Replace it with your appID
const appSecret = '*****';//Replace it with your appSecret
// Request parameters
const paramsToSign = {
crypto: 'USDT',
fiat: 'USD',
fiatAmount: '30',
merchantOrderNo: '133123123456',// Modify it, and it must not be repeated.
network: 'TRX',
timestamp: timestamp,
appId: appId
};
const rawDataToSign = getStringToSign(paramsToSign);
const requestPathWithParams = onRampRequestPath + '?' + rawDataToSign;
const onRampSignature = generateSignature(timestamp, onRampHttpMethod, requestPathWithParams, appSecret);
console.log("On Ramp Signature: " + onRampSignature);
console.log("Final link: " + "https://ramptest.alchemypay.org?" + rawDataToSign + "&sign=" + onRampSignature);
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
func main() {
// Example: On Ramp
onRampHttpMethod := "GET"
onRampRequestPath := "/index/rampPageBuy"
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
appId := "******"//replace with your appId
onRampSecretKey := "*******" //replace with your appSecret
// Request parameters
paramsToSign := map[string]string{
"crypto": "USDT",
"fiatAmount": "2",
"fiat": "USD",
"merchantOrderNo": "1631231234567890123451",//replace with your merchantOrderNo
"network": "BSC",
"callbackUrl": "https://google.com",
"redirectUrl": "https://google.com",
"timestamp": timestamp,
"appId": appId,
}
rawDataToSign := getStringToSign(paramsToSign)
onRampRequestPath += "?" + rawDataToSign
onRampSignature := generateSignature(timestamp, onRampHttpMethod, onRampRequestPath, onRampSecretKey)
fmt.Printf("On Ramp Signature: %s\n", onRampSignature)
fmt.Printf("Final link: https://ramptest.alchemypay.org?%s&sign=%s\n", rawDataToSign, onRampSignature)
}
func generateSignature(timestamp, httpMethod, requestPath, secretKey string) string {
// Concatenate the string for signing
signatureString := timestamp + httpMethod + requestPath
fmt.Printf("sign-content: %s\n", signatureString)
// Generate HMAC SHA256 signature using the key
mac := hmac.New(sha256.New, []byte(secretKey))
mac.Write([]byte(signatureString))
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return url.QueryEscape(signature)
}
func getStringToSign(params map[string]string) string {
var keys []string
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
var s2s strings.Builder
for _, k := range keys {
if v, ok := params[k]; ok && v != "" {
s2s.WriteString(fmt.Sprintf("%s=%s&", k, v))
}
}
return strings.TrimRight(s2s.String(), "&")
}
Updated 1 day ago