OSDN Git Service

update master (#487)
[bytom/bytom.git] / blockchain / txbuilder / rawtxsig_witness.go
1 package txbuilder
2
3 import (
4         "context"
5         "encoding/json"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/bytom/crypto/ed25519/chainkd"
10         chainjson "github.com/bytom/encoding/json"
11 )
12
13 // TODO(bobg): most of the code here is duplicated from
14 // signature_witness.go and needs refactoring.
15
16 // RawTxSigWitness is like SignatureWitness but doesn't involve
17 // signature programs.
18 type RawTxSigWitness struct {
19         Quorum int                  `json:"quorum"`
20         Keys   []keyID              `json:"keys"`
21         Sigs   []chainjson.HexBytes `json:"signatures"`
22 }
23
24 func (sw *RawTxSigWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
25         if len(sw.Sigs) < len(sw.Keys) {
26                 // Each key in sw.Keys may produce a signature in sw.Sigs. Make
27                 // sure there are enough slots in sw.Sigs and that we preserve any
28                 // sigs already present.
29                 newSigs := make([]chainjson.HexBytes, len(sw.Keys))
30                 copy(newSigs, sw.Sigs)
31                 sw.Sigs = newSigs
32         }
33         for i, keyID := range sw.Keys {
34                 if len(sw.Sigs[i]) > 0 {
35                         // Already have a signature for this key
36                         continue
37                 }
38                 var found bool
39                 for _, xpub := range xpubs {
40                         if keyID.XPub == xpub {
41                                 found = true
42                                 break
43                         }
44                 }
45                 if xpubs != nil && !found {
46                         continue
47                 }
48                 path := make([]([]byte), len(keyID.DerivationPath))
49                 for i, p := range keyID.DerivationPath {
50                         path[i] = p
51                 }
52                 sigBytes, err := signFn(ctx, keyID.XPub, path, tpl.Hash(index).Byte32(), auth)
53                 if err != nil {
54                         log.WithField("err", err).Warningf("computing signature %d", i)
55                         return nil
56                 }
57                 sw.Sigs[i] = sigBytes
58         }
59         return nil
60 }
61
62 func (sw RawTxSigWitness) materialize(args *[][]byte) error {
63         var nsigs int
64         for i := 0; i < len(sw.Sigs) && nsigs < sw.Quorum; i++ {
65                 if len(sw.Sigs[i]) > 0 {
66                         *args = append(*args, sw.Sigs[i])
67                         nsigs++
68                 }
69         }
70         return nil
71 }
72
73 // MarshalJSON convert struct to json
74 func (sw RawTxSigWitness) MarshalJSON() ([]byte, error) {
75         obj := struct {
76                 Type   string               `json:"type"`
77                 Quorum int                  `json:"quorum"`
78                 Keys   []keyID              `json:"keys"`
79                 Sigs   []chainjson.HexBytes `json:"signatures"`
80         }{
81                 Type:   "raw_tx_signature",
82                 Quorum: sw.Quorum,
83                 Keys:   sw.Keys,
84                 Sigs:   sw.Sigs,
85         }
86         return json.Marshal(obj)
87 }