This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc.go
117 lines (96 loc) · 3.37 KB
/
btc.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
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package btc defines primitives to work with Bitcoin.
package btc
import (
"context"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/pkg/errors"
"github.com/stratumn/go-core/monitoring/errorcode"
"github.com/stratumn/go-core/types"
)
// Network represents a Bitcoin network.
type Network string
const (
// Component name for monitoring.
Component = "btc"
// NetworkTest3 is an identified for the test Bitcoin network.
NetworkTest3 Network = "bitcoin:test3"
// NetworkMain is an identified for the main Bitcoin network.
NetworkMain Network = "bitcoin:main"
)
var (
// ErrUnknownBitcoinNetwork is returned when the network ID associated to the WIF is unknown.
ErrUnknownBitcoinNetwork = errors.New("WIF encoded private key uses unknown Bitcoin network")
// ErrBadWIF is returned when the WIF encoded private key could not be decoded.
ErrBadWIF = errors.New("Failed to decode WIF encoded private key")
)
// GetNetworkFromWIF returns the network ID associated to a bitcoin wallet.
func GetNetworkFromWIF(key string) (Network, error) {
WIF, err := btcutil.DecodeWIF(key)
if err != nil {
return "", types.WrapError(err, errorcode.InvalidArgument, Component, ErrBadWIF.Error())
}
var network Network
if WIF.IsForNet(&chaincfg.TestNet3Params) {
network = NetworkTest3
} else if WIF.IsForNet(&chaincfg.MainNetParams) {
network = NetworkMain
} else {
return "", types.WrapError(ErrUnknownBitcoinNetwork, errorcode.InvalidArgument, Component, "invalid network")
}
return network, nil
}
// String implements fmt.Stringer.
func (n Network) String() string {
return string(n)
}
// ID returns the byte ID of the network.
func (n Network) ID() byte {
switch n {
case NetworkTest3:
return chaincfg.TestNet3Params.PubKeyHashAddrID
case NetworkMain:
return chaincfg.MainNetParams.PubKeyHashAddrID
}
return 0
}
// Output represents a transaction output.
type Output struct {
TXHash types.ReversedBytes32
PKScript []byte
Index int
Value int
}
// UnspentResult contains the result of a call to UnspentFinder.FindUnspent.
type UnspentResult struct {
// Outputs contains enough outputs to cover the requested amount.
Outputs []Output
// Sum is the sum of the balance of the returned outputs.
Sum int64
// Total is the sum all of the outputs including the returned outputs.
Total int64
}
// UnspentFinder is used to find unspent outputs.
type UnspentFinder interface {
// FindUnspent find unspent outputs for the given address and the
// required amount.
FindUnspent(ctx context.Context, address *types.ReversedBytes20, amount int64) (UnspentResult, error)
}
// Broadcaster is able to broadcast raw Bitcoin transactions.
type Broadcaster interface {
// Broadcast broadcasts a raw transaction.
Broadcast(ctx context.Context, raw []byte) error
}