-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
45 lines (37 loc) · 1.15 KB
/
chain.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
// Package chain contains implementation of the chain interface communicating
// with [Block Cypher API](https://www.blockcypher.com/dev/bitcoin/).
package chain
import (
"encoding/hex"
"github.com/blockcypher/gobcy"
)
type BTC struct {
api gobcy.API
}
// Config contains configuration for Block Cypher API.
type Config struct {
// Token is Block Cypher's user token required for access to POST and DELETE
// calls on the API.
Token string
Coin string // Options: "btc", "bcy", "ltc", "doge"
Chain string // Options: "main", "test3", "test"
}
// PublishTransaction sends a raw transaction to Block Cypher's API. It returns
// a transaction hash as a hexadecimal string.
func (b *BTC) PublishTransaction(rawTransaction []byte) (string, error) {
tx, err := b.api.PushTX(hex.EncodeToString(rawTransaction))
if err != nil {
return "", err
}
return tx.Trans.Hash, nil
}
// Connect performs initialization for communication with Block Cypher based on
// provided config.
func Connect(config *Config) (*BTC, error) {
blockCypherAPI := gobcy.API{
Token: config.Token,
Coin: config.Coin,
Chain: config.Chain,
}
return &BTC{api: blockCypherAPI}, nil
}