錢包系統
基於區塊鏈的錢包項目代,配備充幣、做歸集和提幣三大塊服務。並可以使用多種(目前只實現了RPC)API調用,使用前需配置各個幣種的錢包節點,並在該項目的配置信息中指明。一個項目實例只能跑一個幣種,充提歸三個服務可以按自身需求開關。
使用前最好具備一定的區塊鏈知
環境配置
如果使用docker配置服務,可不手動做如下配置
- golang1.10.2
- mysql:存儲流水和交易
- redis:緩存交易狀態
安裝和配置
下載項目文件
> git clone [email protected]:opteacher/golang-wallet.git
配置項目
config/settings.json
全局配置
{ "env": "dev", "services": [ "withdraw", "deposit" ] }
- | - | - |
---|---|---|
env | 使用環境 | |
services | 開啓服務 | withdraw(提幣) deposit(充幣) collect(歸集) |
config/coin.json
幣種相關配置
{ "name": "ETH", "url": "http://127.0.0.1:8545", "decimal": 8, "stable": 2, "collect": "0xb78f085e2759baf782c705cd3a9fcb5d39fa7b3c", "minCollect": 0.0001, "collectInterval": 30, "tradePassword": "FROM", "unlockDuration": 20000, "withdraw": "0x1c6f567e577a351917615fb1c8f1222dc96ba18d" }
- | - |
---|---|
name | 幣種簡稱 |
url | 錢包節點 |
decimal | 幣種精度 |
stable | 轉賬到賬最低穩定塊高(防止支鏈追趕主鏈) |
collect | 歸集地址 |
minCollect | 最小歸集金額 |
collectInterval | 歸集間隔 |
tradePassword | 充值賬戶的交易密鑰 |
unlockDuration | 解鎖充值賬戶的時間 |
withdraw | 提幣賬戶/地址 |
config/(dev/prod/..).json
環境配置,可以自定義名字,並在settings.json指定
{ "db": { "url": "127.0.0.1:3306", "name": "test", "username": "root", "password": "12345", "max_conn": 20 }, "redis": { "password": "12345", "time_format": "RFC3339", "clusters": [ { "name": "main", "url": "127.0.0.1:6379" } ] } }
- | - | - | - |
---|---|---|---|
db | 數據庫配置 | ||
- | url | 數據庫位置 | |
- | name | 數據庫名 | |
- | username | 登陸用戶名 | |
- | password | 登陸用戶密碼 | |
- | max_conn | 連接池最大連接數 | |
redis | redis緩存配置 | ||
- | password | 查詢操作密碼 | |
- | time_format | 存儲的時間格式 | |
- | clusters | 集羣列表 | |
- | - | name | 節點名 |
- | - | url | 節點URL |
如果集羣列表clusters只有一個節點,則會以單客戶端形式調用redis
config/(dev/prod/..).json
同上
{ "apis": { "rpc": { "active": true, "port": 8037 } }, "callbacks": { "redis": { "active": true }, "rpc": { "active": false, "deposit_url": "", "withdraw_url": "" } } }
- | - | - | - |
---|---|---|---|
apis | 外部接口配置 | ||
- | rpc | http請求 | 具體接口參照API接口表 |
- | - | active | 激活 |
- | - | port | 佔用端口 |
callbacks | 交易進度提示的回調配置 | ||
- | redis | redis發佈響應 | |
- | - | active | 激活(使用的是上面定義的redis配置) |
- | rpc | http回調 | |
- | - | active | 激活 |
- | - | deposit_url | 充幣URL(格式:[Method] URL) |
- | - | withdraw_url | 提幣URL(同上) |
- | - | collect_url | 歸集URL(同上) |
下載依賴包
go get -u github.com/go-sql-driver/mysql
go get -u github.com/stretchr/testify
go get -u github.com/go-redis/redis
使用說明
運行錢包
go run wallet.go
API接口表
Method | URL | Parameters | - |
---|---|---|---|
GET | /api/deposit/{asset}/address | - | 獲取新地址 |
GET | /api/deposit/{asset}/height | - | 獲取鏈最大高度 |
GET | /api/deposit/{asset} | tx_hash 交易hash(可選) address 地址(可選) |
獲取充幣交易 |
POST | /api/withdraw/{asset} | id 提幣id value 金額 target 目標地址 |
提幣 |
GET | /api/withdraw/{asset} | tx_hash 交易hash id 交易id(二選一) |
獲取提幣交易 |
GET | /api/withdraw/{asset}/valid_address/{address} | - | 驗證地址有效性 |
GET | /api/process/{asset}/txid/{tx_hash} | - | 查詢交易進度 |
參數的傳遞方式:POST和PUT請求都是通過application/json方式傳遞,GET請求通過query方式傳遞