幣幣交易平臺開發-Ripple(XRP)瑞波錢包對接
幣幣交易平臺開發-Ripple(XRP)瑞波錢包對接
前言
對接Ripple(XRP),不需要本地部署錢包,直接訪問Ripple API,本文包括訪問Ripple API及如何免費獲取測試的XRP。
比融雲對接流程
1. 安裝Ripple API
2. Ripple API 介面說明
3. 免費獲取測試的XRP
一、 安裝Ripple API
Ripple API GitHub地址:https://github.com/ripple/ripple-lib/
需要的環境:NodeJS
安裝命令:yarn add ripple-lib 或 npm install ripple-lib
引用Ripple API:const RippleAPI = require(ripple-lib).RippleAPI;
實例化Ripple API:api = new RippleAPI({server:host});
Host 測試網路地址:wss://http://s.altnet.rippletest.net:51233
Host 主網地址:wss://http://s2.ripple.com
二、 Ripple API 介面說明
Ripple API 說明文檔:https://github.com/ripple/ripple-lib/blob/develop/docs/index.md
需要需要的API方法:
connect:連接到ripple 伺服器
api.connect().then(() => {}).catch(console.error);
getFee:獲取交易手續費
api.getFee().then(fee => {console.log(fee)});
getBalances:獲取賬戶餘額信息
api.getBalances(address).then(balances => {console.log(balances)});
generateAddress:生成地址及私鑰方法
api.generateAddress();
getTransactions:獲取賬戶交易記錄列表
api.getTransactions(address).then(transactions => { console.log(transactions)});
getTransaction:獲取交易記錄信息
api.getTransaction(txid).then(transaction => { console.log(transaction)});
preparePayment:創建未簽名的交易信息
const address = r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59;const payment = { "source": { "address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "maxAmount": { "value": "0.01", "currency": "XRP" } }, "destination": { "address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", "amount": { "value": "0.01", "currency": " XRP " } }};return api.preparePayment(address, payment).then(prepared => {console.log(prepared)});
sign:簽名交易信息
const txJSON = {"Flags":2147483648,"TransactionType":"AccountSet","Account":"r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59","Domain":"726970706C652E636F6D","LastLedgerSequence":8820051,"Fee":"12","Sequence":23};const secret = shsWGZcmZz6YsWWmcnpfr6fLTdtFV;return api.sign(txJSON, secret);
submit:廣播發送交易
const signedTransaction = 12000322800000002400000017201B0086955368400000000000000C732102F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D874473045022100BDE09A1F6670403F341C21A77CF35BA47E45CDE974096E1AA5FC39811D8269E702203D60291B9A27F1DCABA9CF5DED307B4F23223E0B6F156991DB601DFB9C41CE1C770A726970706C652E636F6D81145E7B112523F68D2F5E879DB4EAC51C6698A69304;return api.submit(signedTransaction) .then(result => {console.log(result)});Result:{ "resultCode": "tesSUCCESS", "resultMessage": "The transaction was applied. Only final in a validated ledger."}
三、 免費獲取測試的XRP
訪問Ripple Testnet 水龍頭網站:https://developers.ripple.com/xrp-test-net-faucet.html
點擊 Generate credentials 按鈕,就會得到一個Ripple賬戶,包含地址及私鑰,另外這個賬戶中將有10000個測試的XRP
總結
以上就可以進行Ripple的開發測試了,在生產環境中一定要配置好伺服器的安全策略、管理好用戶的私鑰,防止錢包被盜,比融雲幣幣交易平臺、場外交易平臺解決方案,基於NodeJS/Redis/MySQL/Socket提供全新的解決方案,為不同的應用場景提供安全可靠的解決方案。
推薦閱讀: