-
Notifications
You must be signed in to change notification settings - Fork 0
/
witness.go
73 lines (62 loc) · 1.91 KB
/
witness.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
package txbuilder
import (
"context"
"github.com/bytom-gm/crypto/sm2/chainkd"
chainjson "github.com/bytom-gm/encoding/json"
"github.com/bytom-gm/errors"
)
// SignFunc is the function passed into Sign that produces
// a signature for a given xpub, derivation path, and hash.
type SignFunc func(context.Context, chainkd.XPub, [][]byte, [32]byte, string) ([]byte, error)
// MaterializeWitnesses takes a filled in Template and "materializes"
// each witness component, turning it into a vector of arguments for
// the tx's input witness, creating a fully-signed transaction.
func materializeWitnesses(txTemplate *Template) error {
msg := txTemplate.Transaction
if msg == nil {
return errors.Wrap(ErrMissingRawTx)
}
if len(txTemplate.SigningInstructions) > len(msg.Inputs) {
return errors.Wrap(ErrBadInstructionCount)
}
for i, sigInst := range txTemplate.SigningInstructions {
if msg.Inputs[sigInst.Position] == nil {
return errors.WithDetailf(ErrBadTxInputIdx, "signing instruction %d references missing tx input %d", i, sigInst.Position)
}
var witness [][]byte
for j, wc := range sigInst.WitnessComponents {
err := wc.materialize(&witness)
if err != nil {
return errors.WithDetailf(err, "error in witness component %d of input %d", j, i)
}
}
msg.SetInputArguments(sigInst.Position, witness)
}
return nil
}
func signedCount(signs []chainjson.HexBytes) (count int) {
for _, sign := range signs {
if len(sign) > 0 {
count++
}
}
return
}
// SignProgress check is all the sign requirement are satisfy
func SignProgress(txTemplate *Template) bool {
for _, sigInst := range txTemplate.SigningInstructions {
for _, wc := range sigInst.WitnessComponents {
switch sw := wc.(type) {
case *SignatureWitness:
if signedCount(sw.Sigs) < sw.Quorum {
return false
}
case *RawTxSigWitness:
if signedCount(sw.Sigs) < sw.Quorum {
return false
}
}
}
}
return true
}