-
Notifications
You must be signed in to change notification settings - Fork 2
/
diamonds.go
98 lines (85 loc) · 3.48 KB
/
diamonds.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
package toolslib
import (
"bytes"
"encoding/hex"
"encoding/json"
"github.com/btcsuite/btcd/btcec"
"github.com/deso-smart/deso-backend/v2/routes"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
)
// _generateUnsignedGiveDiamonds...
func _generateUnsignedSendDiamonds(senderPubKey *btcec.PublicKey, postHashHex string, receiverPublicKeyBase58Check string,
diamondLevel int64, params *lib.DeSoParams, node string) (*routes.SendDiamondsResponse, error) {
endpoint := node + routes.RoutePathSendDiamonds
// Setup request
payload := &routes.SendDiamondsRequest{}
payload.SenderPublicKeyBase58Check = lib.PkToString(senderPubKey.SerializeCompressed(), params)
payload.ReceiverPublicKeyBase58Check = receiverPublicKeyBase58Check
payload.DiamondPostHashHex = postHashHex
payload.DiamondLevel = diamondLevel
payload.MinFeeRateNanosPerKB = 1000
postBody, err := json.Marshal(payload)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedSendDiamonds() failed to marshal struct")
}
postBuffer := bytes.NewBuffer(postBody)
// Execute request
resp, err := http.Post(endpoint, "application/json", postBuffer)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedSendDiamonds() failed to execute request")
}
if resp.StatusCode != 200 {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, errors.Errorf("_generateUnsignedSendDiamonds(): Received non 200 response code: "+
"Status Code: %v Body: %v", resp.StatusCode, string(bodyBytes))
}
// Process response
sendDiamondsResponse := routes.SendDiamondsResponse{}
err = json.NewDecoder(resp.Body).Decode(&sendDiamondsResponse)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedSendDiamonds(): failed decoding body")
}
err = resp.Body.Close()
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedSendDiamonds(): failed closing body")
}
// TODO: Figure out why Decode() loses ExtraData field
diamondPostHashBytes, err := hex.DecodeString(postHashHex)
if err != nil {
return nil, errors.Wrap(err, "_generateUnsignedSendDiamonds(): failed decoding post hash")
}
diamondPostHash := &lib.BlockHash{}
copy(diamondPostHash[:], diamondPostHashBytes[:])
// Append extra data to the transaction. The fees and everything was already computed correctly server side.
diamondsExtraData := make(map[string][]byte)
diamondsExtraData[lib.DiamondLevelKey] = lib.IntToBuf(diamondLevel)
diamondsExtraData[lib.DiamondPostHashKey] = diamondPostHash[:]
sendDiamondsResponse.Transaction.ExtraData = diamondsExtraData
return &sendDiamondsResponse, nil
}
// SendDiamonds
func SendDiamonds(senderPubKey *btcec.PublicKey, senderPrivKey *btcec.PrivateKey, postHashHex string,
receiverPublicKeyBase58Check string, diamondLevel int64, params *lib.DeSoParams, node string) error {
// Request an unsigned transaction from the node
unsignedSendDiamonds, err := _generateUnsignedSendDiamonds(senderPubKey, postHashHex, receiverPublicKeyBase58Check,
diamondLevel, params, node)
if err != nil {
return errors.Wrap(err, "SendDiamonds() failed to call _generateUnsignedSendDiamonds()")
}
txn := unsignedSendDiamonds.Transaction
// Sign the transaction
signature, err := txn.Sign(senderPrivKey)
if err != nil {
return errors.Wrap(err, "SendDiamonds() failed to sign transaction")
}
txn.Signature = signature
// Submit the transaction to the node
err = SubmitTransactionToNode(txn, node)
if err != nil {
return errors.Wrap(err, "SendDiamonds() failed to submit transaction")
}
return nil
}