@ coinsenda/ sdk
节点.js SDK 用于 科因森达 ——密码交换平台.
认证用途 RSA 公钥签名在调用 SDK 之前,您必须在当地生成一对密钥并注册 公钥 在您的 Coinsenda 账户配置设置中。 这个 私钥 从不离开你的环境.
安装
npm install @coinsenda/sdk jsonwebtoken node-rsa
先决条件
1. 生成 RSA 密钥对
在你的机器上创建一对2048位的RSA密钥对. 存储 私钥 安全地说,每一次登录都使用它.
const NodeRSA = require('node-rsa');
let key = new NodeRSA({ b: 2048 });
let public_key = key.exportKey('pkcs8-public-pem');
let private_key = key.exportKey('pkcs8-private-pem');
// Save private_key in a secrets manager or env variable — never commit it
您也可以用 OpenSSL 生成对 :
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem
2. 在您的 Coinsenda 账户中注册公用钥匙
签入您的 Coinsenda 账户并打开 配置文件设置 ~ API 公钥。 。 。 .
粘贴 公钥 (PEM格式)并保存. Coinsenda将它存储在您的配置文件中,并用它来验证未来的基于签名的登录.
这个步骤是必要的 每对密钥一次。如果您旋转了密钥,请在用新的私人密钥认证前登记新的公钥.
认证
每届会议需要:
| 所需经费 | 说明 |
|---|---|
| 注册的公用钥匙 | 保存在您的 Coinsenda 配置文件设置中 |
| 私钥 | 本地持有; 用于签名登录符 |
完整实例
const NodeRSA = require('node-rsa');
const jwt = require('jsonwebtoken');
const { CoinsendaClient, isHttpSuccess } = require('@coinsenda/sdk');
async function authenticate(email, private_key_pem) {
// 1. Create client
let client = new CoinsendaClient({ domain: 'coinsenda.com' });
// 2. Sign a short-lived token with your private key
let rsa_key = new NodeRSA(private_key_pem, 'pkcs8-private-pem');
let signed_token = jwt.sign(
{ email: email, iat: Math.floor(Date.now() / 1000) },
rsa_key.exportKey('private'),
{ algorithm: 'RS256', expiresIn: '5m' }
);
// 3. Authenticate via POST /auth/pubkey
let auth_result = await client.auth.passport.authPubkey({
data: { signed_token }
});
if (!isHttpSuccess(auth_result.status)) {
throw new Error(`Authentication failed: ${auth_result.status}`);
}
let response_data = auth_result.data?.data || auth_result.data;
client.setJwt(response_data.jwt);
client.setRefreshToken(response_data.refresh_token);
return client;
}
// Usage
let client = await authenticate('[email protected]', process.env.COINSENDA_PRIVATE_KEY);
let profile = await client.transaction.user.__get__profile();
console.log(profile.data);
签名代号中的标识符
您签名的有效载荷必须识别您的 Coinsenda 账户:
| 外地 | 何时使用 |
|---|---|
email | 默认 — 匹配您 Coinsenda 账户上的电子邮件 |
userId | 当你知道你的Coinsenda用户身份 |
phone_number | 电话账户 |
总是包含 iat (发布时间戳. 该令牌在5分钟后过期——每次登录尝试都会生成新的令牌.
环境
// Production
new CoinsendaClient({ domain: 'coinsenda.com' });
// Staging
new CoinsendaClient({ domain: 'bitsenda.com' });
// Local development (requires config/environments.local.js)
new CoinsendaClient({ environment: 'local' });
复制 config/environments.local.example.js 改为 config/environments.local.js 并在需要时调整端口.
调用终点
认证后,所有请求都包含 Authorization: Bearer <jwt> 自动.
采用的方法 client.{service}.{model}.{method}(data)数字 :
await client.account.user.__get__accounts();
await client.deposit.user.__get__deposits();
await client.withdraw.withdraw.addNewWithdrawPublic({ ... });
响应是原始服务器对象 : { status, data, headers }启用 isHttpSuccess(result.status) 检查2xx.
许可证
阿帕奇 - 2.0