1.0.11 • Published 5 years ago
drep-wallet-client-sdk v1.0.11
欢迎使用 DREP Blockchain SDK For PC
如果您是一位 PC 端区块链游戏开发者,并且想在游戏中开发基于区块链的支付体系,那么这篇教程很适合您。DREP Blockchain SDK将为您提供现实可用的开发环境,相信通过这篇教程您可以开发出优质的区块链游戏。本教程将以 DREP 测试网为例(SDK 同样支持 ETH 和 BTC)手把手教您使用 DREP SDK,开发教程分为以下几个步骤:
- 下载 DREP 区块链钱包,并获取 DREP 测试网代币(Windows/Mac)
- 申请 apikey 和 secretkey
- 使用 websocket 和本地 DREP 区块链钱包通信
- 正式环境如何使用
DREP 区块链钱包
DREP 区块链钱包是基于区块链的离线版钱包,目前支持的币种有 BTC、ETH、DREP(Test net)。DREP Blockchain 的详细信息请访问DREP 官网
1. 下载 DREP 钱包
下载 DREP wallet 并安装.
2. 创建 ETH(ropsten)私钥对并获取 ETH 测试网代币
请参考Wallet introductions 获取测试网代币:open https://faucet.ropsten.be/ ,and enter your eth testnet address
3.获取 apikey 与 secretkey
\$ npm i drep-wallet-client-sdk -S 开发环境下:
const SDK = require('drep-wallet-client-sdk');
//dev
async function getKeys() {
let result = SDK.getKeys('http://120.27.12.168:3018').data;
const { apikey, secretkey } = result;
console.log(result); //here are your keys
}
getKeys();
正式环境
const SDK = require('drep-wallet-client-sdk');
//prod
async function getKeys() {
let result = SDK.getKeys('http://149.129.172.91:3018').data;
const { apikey, secretkey } = result;
console.log(result); //here are your keys
}
getKeys();
4. 本地测试环境中使用 websocket 连接 DREP PC 版钱包
javascript example
const SDK = require('./index');
const uuid = SDK.uuid;
let apikey =
'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI7PLWeDmIV/kqa/v44WUK0JDNJEub5l\nHd2+l0QdKEJZJETafTTXHM115LSdfcuaWf25LOhOAKkLHhaNqicOuKMCAwEAAQ==';
let privKey =
'\nMIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAjs8tZ4OYhX+Spr+/\njhZQrQkM0kS5vmUd3b6XRB0oQlkkRNp9NNcczXXktJ19y5pZ/bks6E4AqQseFo2q\nJw64owIDAQABAkBtenPjVCXbybXiMUMnKQJF9JcoptkCFcB0msSZygRv/4MuJUP5\nSjoIMjT/jDfPRc/KphA60TahFOtC52buKO2hAiEAwzFEJY3KUe5GWI1mZlD/kf9Z\nQqMjsSnh/fNsncntR1ECIQC7TFFQWRLMYL8LVN6Gq0shBNI2KDx36YkM8EIILCdr\nswIhALQKo1MIYFAGOni/pG5H1BS0tX6Tv/5QJZX6jHtWoDihAiBqQD8Yr48kdfWq\nvfiGuQGjt5Jwup6i/86JknpDHJk4VQIhALnEgA0jgKkvtdbvQkanXMeoH/2Rfel3\npZhTyjlQZzEC\n';
const sdk = new SDK(apikey, privKey, 'ws://127.0.0.1:12131/sdk'); //yourApiKey is the apikey you get in step 3, yourPrivKey is the secretKey you get in step 3
const client = sdk.client;
const sign = sdk.sign;
sdk.connect(); //connect wallet client
//add event handler
client.on('connectFailed', function(error) {
console.log('Connect Error:' + error.toString());
});
let ws;
client.on('connect', function(connection) {
ws = connection;
//send transaction request to wallet client if user send a transfer request
ws.send(
sign({
token_type: 'dreperc20',
amount: '0.00001',
to: '0xf54c3929B14D2deaf34A6572828D98338F43B789',
id: uuid()
})
);
console.log('WebSocket Client a Connected');
connection.on('error', function(error) {
console.log('Connection Error: ' + error.toString());
});
connection.on('close', function() {
console.log('wallet Connection Closed');
});
connection.on('message', data => {
let resJson = JSON.parse(data.utf8Data);
console.log(resJson);
if (resJson.authenticated) {
if (Number(resJson.authenticated) === 1) {
console.log('you have been authenticated');
} else {
throw new Error(
'you are not be authenticated,please check your apikey and prikey'
);
}
} else {
throw new Error('wallet error');
}
});
});
5. 正式环境
example:
- 1.node drep-wallet-client-sdk/example-prod/server.js
- 2.node drep-wallet-client-sdk/example-prod/example-prod.js
Tips:
- 获取的 secretkey 必需放在服务器上对交易信息签名
6. websocket API
####Request API
Key | Value | Description |
---|---|---|
token_type | string | the type of token you will transfer |
amount | string | the amount of token you will transfer |
to | string | the address of token you will send |
id | string | the id of this transaction |
####Response API
Key | Value | Description |
---|---|---|
authenticated | number | 0:authenticated successfully ,1:authenticated failed |
hash | string | the hash of transaction you had sent |
error | string | the error when transferring |
id | string | the id of this transaction |
7. Error Detail
code | detail |
---|---|
1000 | user cancel |
1001 | Server validation failed(game developer) |
1002 | internet server error |
1003 | Transaction signature failed |
1004 | user password incorrect |
1005 | local wallet error |
1006 | balance is not enough |
1007 | the address of this token is not exist,please create it later! |
1008 | error when send transaction. |