-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.go
182 lines (166 loc) · 4.61 KB
/
account.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
package libbtc
import (
"context"
"crypto/ecdsa"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
type account struct {
PrivKey *btcec.PrivateKey
Client
}
// Account is an Bitcoin external account that can sign and submit transactions
// to the Bitcoin blockchain. An Account is an abstraction over the Bitcoin
// blockchain.
type Account interface {
Client
Address() (btcutil.Address, error)
SerializedPublicKey() ([]byte, error)
Transfer(ctx context.Context, to string, value, fee int64, sendAll bool) (string, error)
SendTransaction(
ctx context.Context,
script []byte,
fee int64,
updateTxIn func(*wire.TxIn),
preCond func(*wire.MsgTx) bool,
f func(*txscript.ScriptBuilder),
postCond func(*wire.MsgTx) bool,
) error
}
// NewAccount returns a user account for the provided private key which is
// connected to a Bitcoin client.
func NewAccount(client Client, privateKey *ecdsa.PrivateKey) Account {
return &account{
(*btcec.PrivateKey)(privateKey),
client,
}
}
// Address returns the address of the given private key
func (account *account) Address() (btcutil.Address, error) {
pubKeyBytes, err := account.SerializedPublicKey()
if err != nil {
return nil, err
}
pubKey, err := btcutil.NewAddressPubKey(pubKeyBytes, account.NetworkParams())
if err != nil {
return nil, err
}
addrString := pubKey.EncodeAddress()
return btcutil.DecodeAddress(addrString, account.NetworkParams())
}
// Transfer bitcoins to the given address
func (account *account) Transfer(ctx context.Context, to string, value, fee int64, sendAll bool) (string, error) {
if sendAll {
me, err := account.Address()
if err != nil {
return "", err
}
balance, err := account.Balance(ctx, me.EncodeAddress(), 0)
if err != nil {
return "", err
}
value = balance - fee
}
address, err := btcutil.DecodeAddress(to, account.NetworkParams())
if err != nil {
return "", err
}
var txHash string
return txHash, account.SendTransaction(
ctx,
nil,
fee,
nil,
func(tx *wire.MsgTx) bool {
P2PKHScript, err := txscript.PayToAddrScript(address)
if err != nil {
return false
}
tx.AddTxOut(wire.NewTxOut(value, P2PKHScript))
return true
},
nil,
func(tx *wire.MsgTx) bool {
txHash = tx.TxHash().String()
return true
},
)
}
// SendTransaction builds, signs, verifies and publishes a transaction to the
// corresponding blockchain. If contract is provided then the transaction uses
// the contract's unspent outputs for the transaction, otherwise uses the
// account's unspent outputs to fund the transaction. preCond is executed in
// the starting of the process, if it returns false SendTransaction returns
// ErrPreConditionCheckFailed and stops the process. This function can be used
// to modify how the unspent outputs are spent, this can be nil. f is supposed
// to be used with non empty contracts, to modify the signature script. preCond
// is executed in the starting of the process, if it returns false
// SendTransaction returns ErrPreConditionCheckFailed and stops the process.
func (account *account) SendTransaction(
ctx context.Context,
contract []byte,
fee int64,
updateTxIn func(*wire.TxIn),
preCond func(*wire.MsgTx) bool,
f func(*txscript.ScriptBuilder),
postCond func(*wire.MsgTx) bool,
) error {
// Current Bitcoin Transaction Version (2).
tx := account.newTx(ctx, wire.NewMsgTx(2))
if preCond != nil && !preCond(tx.msgTx) {
return ErrPreConditionCheckFailed
}
var address btcutil.Address
var err error
if contract == nil {
address, err = account.Address()
if err != nil {
return err
}
} else {
address, err = btcutil.NewAddressScriptHash(contract, account.NetworkParams())
if err != nil {
return err
}
}
if err := tx.fund(address, fee); err != nil {
return err
}
if err := tx.sign(f, updateTxIn, contract); err != nil {
return err
}
if err := tx.verify(); err != nil {
return err
}
for {
select {
case <-ctx.Done():
return ErrPostConditionCheckFailed
default:
if err := tx.submit(); err != nil {
return err
}
for i := 0; i < 60; i++ {
if postCond == nil || postCond(tx.msgTx) {
return nil
}
time.Sleep(5 * time.Second)
}
}
}
}
func (account *account) SerializedPublicKey() ([]byte, error) {
pubKey := account.PrivKey.PubKey()
switch account.NetworkParams() {
case &chaincfg.MainNetParams:
return pubKey.SerializeCompressed(), nil
case &chaincfg.TestNet3Params:
return pubKey.SerializeUncompressed(), nil
default:
return nil, NewErrUnsupportedNetwork(account.NetworkParams().Name)
}
}