OSDN Git Service

Handle the abnormal exit situation
[bytom/vapor.git] / claim / bytom / mainchain / rawtxsig_witness.go
1 package mainchain
2
3 import (
4         "encoding/json"
5
6         "github.com/vapor/crypto/ed25519/chainkd"
7         chainjson "github.com/vapor/encoding/json"
8 )
9
10 // TODO(bobg): most of the code here is duplicated from
11 // signature_witness.go and needs refactoring.
12
13 // RawTxSigWitness is like SignatureWitness but doesn't involve
14 // signature programs.
15 type RawTxSigWitness struct {
16         Quorum int                  `json:"quorum"`
17         Keys   []keyID              `json:"keys"`
18         Sigs   []chainjson.HexBytes `json:"signatures"`
19 }
20
21 func (sw *RawTxSigWitness) Sign(tpl *Template, index uint32, xprv chainkd.XPrv) error {
22         if len(sw.Sigs) < len(sw.Keys) {
23                 // Each key in sw.Keys may produce a signature in sw.Sigs. Make
24                 // sure there are enough slots in sw.Sigs and that we preserve any
25                 // sigs already present.
26                 newSigs := make([]chainjson.HexBytes, len(sw.Keys))
27                 copy(newSigs, sw.Sigs)
28                 sw.Sigs = newSigs
29         }
30         for i, keyID := range sw.Keys {
31                 if len(sw.Sigs[i]) > 0 {
32                         // Already have a signature for this key
33                         continue
34                 }
35                 if keyID.XPub.String() != xprv.XPub().String() {
36                         continue
37                 }
38                 data := tpl.Hash(index).Byte32()
39                 sigBytes := xprv.Sign(data[:])
40
41                 // This break is ordered to avoid signing transaction successfully only once for a multiple-sign account
42                 // that consist of different keys by the same password. Exit immediately when the signature is success,
43                 // it means that only one signature will be successful in the loop for this multiple-sign account.
44                 sw.Sigs[i] = sigBytes
45                 break
46         }
47         return nil
48 }
49
50 func (sw RawTxSigWitness) Materialize(args *[][]byte) error {
51         var nsigs int
52         for i := 0; i < len(sw.Sigs) && nsigs < sw.Quorum; i++ {
53                 if len(sw.Sigs[i]) > 0 {
54                         *args = append(*args, sw.Sigs[i])
55                         nsigs++
56                 }
57         }
58         return nil
59 }
60
61 // MarshalJSON convert struct to json
62 func (sw RawTxSigWitness) MarshalJSON() ([]byte, error) {
63         obj := struct {
64                 Type   string               `json:"type"`
65                 Quorum int                  `json:"quorum"`
66                 Keys   []keyID              `json:"keys"`
67                 Sigs   []chainjson.HexBytes `json:"signatures"`
68         }{
69                 Type:   "raw_tx_signature",
70                 Quorum: sw.Quorum,
71                 Keys:   sw.Keys,
72                 Sigs:   sw.Sigs,
73         }
74         return json.Marshal(obj)
75 }