/
payment.go
375 lines (340 loc) · 10.1 KB
/
payment.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package payment
import (
"crypto/sha256"
"errors"
"fmt"
"hash"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/txscript"
"github.com/vulpemventures/go-elements/address"
"github.com/vulpemventures/go-elements/network"
"golang.org/x/crypto/ripemd160"
)
// Payment defines the structure that holds the information different addresses
type Payment struct {
Hash []byte
WitnessHash []byte
Script []byte
WitnessScript []byte
Redeem *Payment
PublicKey *btcec.PublicKey
BlindingKey *btcec.PublicKey
Network *network.Network
Taproot *TaprootPaymentData
}
// FromPublicKey creates a Payment struct from a btcec.publicKey
func FromPublicKey(
pubkey *btcec.PublicKey,
net *network.Network,
blindingKey *btcec.PublicKey,
) *Payment {
var tmpNet *network.Network
if net == nil {
tmpNet = &network.Liquid
} else {
tmpNet = net
}
publicKeyBytes := pubkey.SerializeCompressed()
pkHash := Hash160(publicKeyBytes)
script := buildScript(pkHash, "p2pkh")
witnessScript := buildScript(pkHash, "p2wpkh")
return &Payment{
Hash: pkHash,
WitnessHash: pkHash,
Script: script,
WitnessScript: witnessScript,
Network: tmpNet,
PublicKey: pubkey,
BlindingKey: blindingKey,
}
}
// FromPublicKeys creates a multi-signature Payment struct from list of public key's
func FromPublicKeys(
pubkeys []*btcec.PublicKey,
nrequired int,
net *network.Network,
blindingKey *btcec.PublicKey,
) (*Payment, error) {
if len(pubkeys) < nrequired {
errorMsg := fmt.Sprintf("unable to generate multisig script with "+
"%d required signatures when there are only %d public "+
"keys available", nrequired, len(pubkeys))
return nil, errors.New(errorMsg)
}
var tmpNet *network.Network
if net == nil {
tmpNet = &network.Liquid
} else {
tmpNet = net
}
builder := txscript.NewScriptBuilder().AddInt64(int64(nrequired))
for _, key := range pubkeys {
builder.AddData(key.SerializeCompressed())
}
builder.AddInt64(int64(len(pubkeys)))
builder.AddOp(txscript.OP_CHECKMULTISIG)
multiSigScript, err := builder.Script()
if err != nil {
return nil, err
}
redeem, err := FromScript(multiSigScript, tmpNet, blindingKey)
if err != nil {
return nil, err
}
return FromPayment(redeem)
}
// FromPayment creates a Payment struct from a another Payment
func FromPayment(payment *Payment) (*Payment, error) {
if payment.Script == nil || len(payment.Script) == 0 {
return nil, errors.New("payment's script can't be empty or nil")
}
redeem := payment.copy()
scriptToHash := make([]byte, 0)
// the only case where the witnessScript is null is when wrapping multisig
if len(redeem.WitnessScript) > 0 {
scriptToHash = redeem.WitnessScript
} else {
scriptToHash = redeem.Script
}
scriptHash := Hash160(scriptToHash)
witnessScriptHash := sha256.Sum256(scriptToHash)
script := buildScript(scriptHash, "p2sh")
witnessScript := buildScript(witnessScriptHash[:], "p2wsh")
return &Payment{
Hash: scriptHash,
WitnessHash: witnessScriptHash[:],
Script: script,
WitnessScript: witnessScript,
Redeem: redeem,
Network: redeem.Network,
BlindingKey: redeem.BlindingKey,
Taproot: redeem.Taproot,
}, nil
}
// FromScript creates parses a script into a Payment struct
func FromScript(
outputScript []byte,
net *network.Network,
blindingKey *btcec.PublicKey,
) (*Payment, error) {
if len(outputScript) == 0 {
return nil, errors.New("payment's script can't be empty or nil")
}
var tmpNet *network.Network
if net == nil {
tmpNet = &network.Liquid
} else {
tmpNet = net
}
var script, scriptHash, witnessScript, witnessScriptHash, tweakedKey []byte
switch address.GetScriptType(outputScript) {
case address.P2WpkhScript:
scriptHash = outputScript[2:]
script = buildScript(scriptHash, "p2pkh")
witnessScriptHash = scriptHash
witnessScript = outputScript
case address.P2WshScript:
witnessScriptHash = outputScript[2:]
witnessScript = outputScript
case address.P2ShScript:
scriptHash = outputScript[2 : len(outputScript)-1]
script = outputScript
case address.P2PkhScript:
scriptHash = outputScript[3 : len(outputScript)-2]
script = outputScript
case address.P2TRScript:
tweakedKey = outputScript[2:]
script = outputScript
// multisig, here we do not calculate the hashes because this payment
// must be wrapped into another one
default:
script = outputScript
}
return &Payment{
Hash: scriptHash,
WitnessHash: witnessScriptHash,
Script: script,
WitnessScript: witnessScript,
Network: tmpNet,
BlindingKey: blindingKey,
Taproot: &TaprootPaymentData{
XOnlyTweakedKey: tweakedKey,
},
}, nil
}
// PubKeyHash is a method of the Payment struct to derive a base58 p2pkh address
func (p *Payment) PubKeyHash() (string, error) {
if p.Hash == nil || len(p.Hash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
payload := &address.Base58{p.Network.PubKeyHash, p.Hash}
addr := address.ToBase58(payload)
return addr, nil
}
// ConfidentialPubKeyHash is a method of the Payment struct to derive a
// base58 confidential p2pkh address
func (p *Payment) ConfidentialPubKeyHash() (string, error) {
if p.Hash == nil || len(p.Hash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
if p.BlindingKey == nil {
return "", errors.New("payment's blinding key can't be nil")
}
prefix := [1]byte{p.Network.PubKeyHash}
confidentialAddress := append(
append(
prefix[:],
p.BlindingKey.SerializeCompressed()...,
),
p.Hash...,
)
return base58.CheckEncode(confidentialAddress, p.Network.Confidential), nil
}
// ScriptHash is a method of the Payment struct to derive a base58 p2sh address
func (p *Payment) ScriptHash() (string, error) {
if p.Hash == nil || len(p.Hash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
payload := &address.Base58{p.Network.ScriptHash, p.Hash}
addr := address.ToBase58(payload)
return addr, nil
}
// ConfidentialScriptHash is a method of the Payment struct to derive a
// base58 confidential p2sh address
func (p *Payment) ConfidentialScriptHash() (string, error) {
if p.Hash == nil || len(p.Hash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
if p.BlindingKey == nil {
return "", errors.New("payment's blinding key can't be nil")
}
prefix := [1]byte{p.Network.ScriptHash}
confidentialAddress := append(
append(
prefix[:],
p.BlindingKey.SerializeCompressed()...,
),
p.Hash...,
)
return base58.CheckEncode(confidentialAddress, p.Network.Confidential), nil
}
// WitnessPubKeyHash is a method of the Payment struct to derive a base58 p2wpkh address
func (p *Payment) WitnessPubKeyHash() (string, error) {
if p.WitnessHash == nil || len(p.WitnessHash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
//Here the Version for wpkh is always 0
version := byte(0x00)
payload := &address.Bech32{p.Network.Bech32, version, p.WitnessHash}
addr, err := address.ToBech32(payload)
if err != nil {
return "", nil
}
return addr, nil
}
// ConfidentialWitnessPubKeyHash is a method of the Payment struct to derive
// a confidential blech32 p2wpkh address
func (p *Payment) ConfidentialWitnessPubKeyHash() (string, error) {
if p.WitnessHash == nil || len(p.WitnessHash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
//Here the Version for wpkh is always 0
version := byte(0x00)
payload := &address.Blech32{
p.Network.Blech32,
version,
p.BlindingKey.SerializeCompressed(),
p.WitnessHash,
}
addr, err := address.ToBlech32(payload)
if err != nil {
return "", err
}
return addr, nil
}
// WitnessScriptHash is a method of the Payment struct to derive a base58 p2wsh address
func (p *Payment) WitnessScriptHash() (string, error) {
if p.WitnessHash == nil || len(p.WitnessHash) == 0 {
return "", errors.New("payment's witnessHash can't be empty or nil")
}
version := byte(0x00)
payload := &address.Bech32{p.Network.Bech32, version, p.WitnessHash}
addr, err := address.ToBech32(payload)
if err != nil {
return "", err
}
return addr, nil
}
// ConfidentialWitnessScriptHash is a method of the Payment struct to derive
// a confidential blech32 p2wsh address
func (p *Payment) ConfidentialWitnessScriptHash() (string, error) {
if p.WitnessHash == nil || len(p.WitnessHash) == 0 {
return "", errors.New("payment's hash can't be empty or nil")
}
//Here the Version for wpkh is always 0
version := byte(0x00)
payload := &address.Blech32{
p.Network.Blech32,
version,
p.BlindingKey.SerializeCompressed(),
p.WitnessHash,
}
addr, err := address.ToBlech32(payload)
if err != nil {
return "", nil
}
return addr, nil
}
func (p *Payment) copy() *Payment {
var redeem *Payment
var pubkey *btcec.PublicKey
var blindkey *btcec.PublicKey
if p.Redeem != nil {
redeem = &Payment{}
*redeem = *p.Redeem
}
if p.PublicKey != nil {
pubkey = &btcec.PublicKey{}
*pubkey = *p.PublicKey
}
if p.BlindingKey != nil {
blindkey = &btcec.PublicKey{}
*blindkey = *p.BlindingKey
}
return &Payment{
Hash: p.Hash,
WitnessHash: p.WitnessHash,
Script: p.Script,
WitnessScript: p.WitnessScript,
Redeem: redeem,
PublicKey: pubkey,
BlindingKey: blindkey,
Network: p.Network,
}
}
// Calculate the hash of hasher over buf.
func calcHash(buf []byte, hasher hash.Hash) []byte {
hasher.Write(buf)
return hasher.Sum(nil)
}
// Hash160 calculates the hash ripemd160(sha256(b)).
func Hash160(buf []byte) []byte {
return calcHash(calcHash(buf, sha256.New()), ripemd160.New())
}
// buildScript returns the requested scriptType script with the provided hash
func buildScript(hash []byte, scriptType string) []byte {
builder := txscript.NewScriptBuilder()
switch scriptType {
case "p2pkh":
builder.AddOp(txscript.OP_DUP).AddOp(txscript.OP_HASH160)
builder.AddData(hash)
builder.AddOp(txscript.OP_EQUALVERIFY).AddOp(txscript.OP_CHECKSIG)
case "p2sh":
builder.AddOp(txscript.OP_HASH160).AddData(hash).AddOp(txscript.OP_EQUAL)
case "p2wpkh", "p2wsh":
builder.AddOp(txscript.OP_0).AddData(hash)
}
script, _ := builder.Script()
return script
}