创建 Ethereum 与 Tron 地址
你只需一个请求就能创建地址:POST /api/v2/{chain}/addresses。如果请求体中包含 password,Chaingateway 会加密存储私钥,响应中只返回地址。不带密码时,响应中会包含私钥。本教程为 Ethereum 和 Tron 演示这两种方式。同样的请求也适用于 BNB Smart Chain、Polygon 和 Arbitrum。
下面的调用都需要 API token。如何创建 token,请参阅快速入门。
如何创建受密码保护的地址?
Section titled “如何创建受密码保护的地址?”在 JSON 请求体中传入 password 字段即可。对于 EVM 链,Chaingateway 会创建一个 keystore,这是一种加密保存私钥的文件格式,定义见 Web3 secret storage definition。keystore 用你的密码加密。只拿到 keystore 或只知道密码的人都无法访问该地址,你发起交易时提供的是密码,而不是私钥。
password 由你自己设定。Chaingateway 不会存储它,也无法找回它。一旦丢失密码,你就无法再访问这个地址,所以请把密码放进密码管理器或密钥存储中。
curl --request POST \ --url https://app.chaingateway.io/api/v2/ethereum/addresses \ --header 'Accept: application/json' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN' \ --data '{"password":"choose-a-strong-password"}'import requests
url = "https://app.chaingateway.io/api/v2/ethereum/addresses"payload = {"password": "choose-a-strong-password"}headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'}
response = requests.post(url, json=payload, headers=headers)print(response.json())const axios = require('axios');
const url = 'https://app.chaingateway.io/api/v2/ethereum/addresses';const payload = { password: 'choose-a-strong-password' };const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'};
axios.post(url, payload, { headers }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error.response?.data ?? error); });<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://app.chaingateway.io/api/v2/ethereum/addresses', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer YOUR_API_TOKEN', ], 'json' => ['password' => 'choose-a-strong-password']]);
echo $response->getBody();响应状态码为 201。OpenAPI 文件把地址的键写成了 adderess:
{ "status": 201, "ok": true, "message": "Address created", "data": { "adderess": "0x7d3bC832A0860fD882FF4C2359e10268f4E5CCf8" }}如何创建不带密码的地址?
Section titled “如何创建不带密码的地址?”不传 password 即可。此时响应中会包含私钥,之后从该地址发起的每笔交易都要在 privatekey 字段中提供这个私钥。请只对一次性地址使用此模式,因为每个请求都携带私钥,很容易泄露。示例创建的是 Tron 地址。
curl --request POST \ --url https://app.chaingateway.io/api/v2/tron/addresses \ --header 'Accept: application/json' \ --header 'Authorization: Bearer YOUR_API_TOKEN'import requests
url = "https://app.chaingateway.io/api/v2/tron/addresses"headers = { 'Accept': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'}
response = requests.post(url, headers=headers)print(response.json())const axios = require('axios');
const url = 'https://app.chaingateway.io/api/v2/tron/addresses';const headers = { 'Accept': 'application/json', 'Authorization': 'Bearer YOUR_API_TOKEN'};
axios.post(url, {}, { headers }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error.response?.data ?? error); });<?phprequire 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();$response = $client->post('https://app.chaingateway.io/api/v2/tron/addresses', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer YOUR_API_TOKEN', ]]);
echo $response->getBody();不带密码创建的 Tron 地址会直接返回密钥信息:
{ "status": 201, "ok": true, "message": "Address created", "data": { "privateKey": "59f1c6200e01a2ca9471411f10198bfa63678d0e87cc2ca30f9c4a68dee78edc", "publicKey": "040fe6e677442c36f7fdd5535ba3ff1cef0110d78363420f7e24b65298990e3467aed68b9a67e1396d84753db25b32a2fa3e1fc0a673f01638e9bcfab2d8f4ceb5", "hexAddress": "41a56a6505ffbee78eb916dd44f4846874deddaebd", "address": "TR3qx91smURF3R455V1ubqtoUsZbgqikfg" }}请把 privateKey 和 address 一起保存在安全的位置。本例中的私钥只是示例值,切勿向它充值。Tron 的账户文档说明了 base58 地址与 hex 地址之间的关系。
地址创建好之后做什么?
Section titled “地址创建好之后做什么?”先查询余额,再给地址充值,然后从该地址发送。GET /api/v2/ethereum/balances/{address} 以字符串形式返回该地址的余额:
{ "status": 200, "ok": true, "message": "Balance fetched", "data": "0.000000000000000000"}新地址的余额为零。你可以从自己控制的钱包向它转入币,在使用 X-Network: testnet 时也可以从该链的测试网水龙头获取。GET /api/v2/ethereum/addresses 会列出你创建且尚未删除的所有地址。如果希望在资金到账时收到通知,请为该地址创建 webhook。从该地址转出资金的方法见创建交易。
Bitcoin 和 Solana 有所不同。在 Bitcoin 上,你要先用 POST /api/v2/bitcoin/wallets 创建钱包,再向其中添加地址。在 Solana 上,POST /api/v2/solana/addresses 直接创建地址。两者的细节见钱包管理。
创建地址可能返回哪些错误?
Section titled “创建地址可能返回哪些错误?”地址相关的调用使用标准响应结构,失败时 ok 为 false。缺少 token 或 token 无效时,返回状态码 401 和 {"message":"Unauthenticated."}。请求体未通过校验时,返回状态码 422 和 Data validation failed,字段名列在 errors 中。当套餐的地址数量上限用完时,API 返回状态码 422 和 Your address limit exceeded. Please upgrade your plan。OpenAPI 文件在 Bitcoin 钱包处给出了这条消息。
DELETE /api/v2/ethereum/addresses/{address} 会删除一个地址,并减少计入你套餐的地址数量。请删除不再需要的地址,但如果该地址可能仍有资金,请先导出私钥。
各条链的地址格式是怎样的?
Section titled “各条链的地址格式是怎样的?”Ethereum 和其他 EVM 链使用 20 字节的地址,写作带 0x 前缀的 40 个十六进制字符。Ethereum 账户文档说明了外部拥有账户如何由密钥对派生。Tron 地址以 T 开头,共 34 个字符,API 用模式 T[a-zA-Z0-9]{33} 校验这一格式。该格式的权威参考是 TRON 账户文档。