forked from blockcypher/gobcy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microtx.go
53 lines (49 loc) · 1.43 KB
/
microtx.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
package gobcy
import (
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcec"
)
//SendMicro sends a Micro through the Coin/Chain
//network. It will return a Micro with a proper hash
//if it successfully sent. If using public (instead of
//private) keys, you'll need to sign the returned Micro
//(using the *Micro.Sign method) and run SendMicro
//again with the signed data, which will then return
//a proper hash.
func (api *API) SendMicro(mic MicroTX) (result MicroTX, err error) {
u, err := api.buildURL("/txs/micro", nil)
if err != nil {
return
}
err = postResponse(u, &mic, &result)
return
}
//Sign takes a hex-encoded string slice of private
//keys and uses them to sign the ToSign data in a
//MicroTX, generating the proper hex-encoded Signatures.
//This is meant as a helperfunction, and leverages
//btcd's btcec library.
func (mic *MicroTX) Sign(priv string) (err error) {
privDat, err := hex.DecodeString(priv)
if err != nil {
return
}
privkey, pubkey := btcec.PrivKeyFromBytes(btcec.S256(), privDat)
if mic.Pubkey != hex.EncodeToString(pubkey.SerializeCompressed()) {
err = errors.New("*MicroTX.Sign error: public key does not match private key")
return
}
for _, k := range mic.ToSign {
tosign, err := hex.DecodeString(k)
if err != nil {
return err
}
sig, err := privkey.Sign(tosign)
if err != nil {
return err
}
mic.Signatures = append(mic.Signatures, hex.EncodeToString(sig.Serialize()))
}
return
}