-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcy.go
188 lines (168 loc) · 4.57 KB
/
bcy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"math/big"
"net/http"
"github.com/blockcypher/gobcy/v2"
"github.com/gin-gonic/gin"
)
type BcyFaucetRequestModel struct {
Addreess string `json:"address"`
Amount float64 `json:"amount"`
}
func bcyFaucetHandler(c *gin.Context) {
var model BcyFaucetRequestModel
if err := c.BindJSON(&model); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
addr, err := BcApi.GetAddr(model.Addreess, nil)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
// Genetate random wallet
genAddr, err := BcApi.GenAddrKeychain()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
// Get Chain for transaction fees
chain, err := BcApi.GetChain()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
// Faucet
var btcAmount = int(model.Amount * 100000000)
_, err = BcApi.Faucet(genAddr, btcAmount+chain.MediumFee)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
fmt.Printf("Address: %v Fee %v\n", chain.MediumFee, genAddr.Address)
// TX Sceleton
tx := gobcy.TempNewTX(genAddr.Address, addr.Address, *big.NewInt(int64(btcAmount)))
tx.Fees = *big.NewInt(int64(chain.MediumFee))
txSkeleton, err := BcApi.NewTX(tx, false)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
// Sign TX
if err := txSkeleton.Sign([]string{genAddr.Private}); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
// Send TX
if _, err := BcApi.SendTX(txSkeleton); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"from": genAddr.Address,
"to": addr.Address,
"amount": model.Amount,
})
}
func bcyBankWalletData() gin.H {
const (
address = "C16ZKtLMjo1e3KxxEaD6uewtdkYeojFQo1"
privateKey = "c617d15889ba6b350da66a7a371beec20a99460760382cb9a0ac382382e56fd8"
publicKey = "02e07efec6e927ea007bd870969e93fef00c10780da5b6d75e2a0b9fbd2838736f"
)
addr, err := BcApi.GetAddr(address, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return gin.H{}
}
type Transaction struct {
Addresses []string
Status bool
}
var (
transactions = make([]Transaction, 0)
)
for _, txRef := range addr.TXRefs {
tx, err := BcApi.GetTX(txRef.TXHash, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return gin.H{}
}
transactions = append(transactions, Transaction{
Addresses: tx.Addresses,
Status: tx.BlockHeight > 0,
})
}
return gin.H{
"wallet": map[string]interface{}{
"address": address,
"private": privateKey,
"public": publicKey,
"unconfirmed": float64(addr.UnconfirmedBalance.Int64()) / 100000000,
"balance": float64(addr.Balance.Int64()) / 100000000,
"balanceSatoshis": addr.Balance.Int64(),
},
"transactions": transactions,
}
}
func bcyAccounts() gin.H {
var (
wallets = []struct {
Address string
Private string
Public string
Unconfirmed float64
Balance float64
BalanceSatoshis int64
Transactions string
}{
{
Address: "C8psDNmwR88b6fapZhHhjDiT16em9Wdqso",
Private: "8a0149f1662eeabc063f0f565b27ed15f11281c6bd07805f09ea328e01f95e6b",
Public: "03a2590f54fb8e55cee0053d4c9f48efd6eb3cc6b6277901409e5cba809cade6ce",
},
{
Address: "C99p1B5FdAsZbjPcPfQ3vpdidUgJtPBjKy",
Private: "fb54ca81541111d62d59d9cb48b583da4fe92a939f8a9bf40cf3f64e6aecb25b",
Public: "03a35c9f16e94b1efdd0f5eabd6610741c6688fc7b143586224c4a7b9625e2eaf0",
},
{
Address: "CBcZZYsVbo1APihkkW8NWVfAwW4gSRxGv7",
Private: "f61f4bbd3aa6028b83fbbe99b0d6651a82184880ae5d964878eca5ebff3609db",
Public: "02475adc7c7170a5a145a7d16725007595e6221f2e126feeb7cde92dded9dff53a",
},
}
)
for i, wallet := range wallets {
wallet.Transactions = fmt.Sprintf("%s/%s/", "https://live.blockcypher.com/bcy/address", wallet.Address)
addr, err := BcApi.GetAddr(wallet.Address, nil)
if err != nil {
fmt.Printf("Error: %v\n", err)
return gin.H{}
}
wallet.Balance = float64(addr.Balance.Int64()) / 100000000
wallet.BalanceSatoshis = addr.Balance.Int64()
wallet.Unconfirmed = float64(addr.UnconfirmedBalance.Int64()) / 100000000
wallets[i] = wallet
}
return gin.H{
"wallets": wallets,
}
}