6 "github.com/vapor/blockchain/txbuilder"
7 "github.com/vapor/crypto/ed25519/chainkd"
8 chainjson "github.com/vapor/encoding/json"
9 "github.com/vapor/errors"
12 // AddWitnessKeys adds a SignatureWitness with the given quorum and
13 // list of keys derived by applying the derivation path to each of the
15 func (si *SigningInstruction) AddWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
16 hexPath := make([]chainjson.HexBytes, 0, len(path))
17 for _, p := range path {
18 hexPath = append(hexPath, p)
21 keyIDs := make([]keyID, 0, len(xpubs))
22 for _, xpub := range xpubs {
23 keyIDs = append(keyIDs, keyID{xpub, hexPath})
26 sw := &SignatureWitness{
30 si.WitnessComponents = append(si.WitnessComponents, sw)
33 // AddRawWitnessKeys adds a SignatureWitness with the given quorum and
34 // list of keys derived by applying the derivation path to each of the
36 func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
37 hexPath := make([]chainjson.HexBytes, 0, len(path))
38 for _, p := range path {
39 hexPath = append(hexPath, p)
42 keyIDs := make([]keyID, 0, len(xpubs))
43 for _, xpub := range xpubs {
44 keyIDs = append(keyIDs, keyID{xpub, hexPath})
47 sw := &RawTxSigWitness{
51 si.WitnessComponents = append(si.WitnessComponents, sw)
54 // SigningInstruction gives directions for signing inputs in a TxTemplate.
55 type SigningInstruction struct {
56 Position uint32 `json:"position"`
57 WitnessComponents []witnessComponent `json:"witness_components,omitempty"`
60 // witnessComponent is the abstract type for the parts of a
61 // SigningInstruction. Each witnessComponent produces one or more
62 // arguments for a VM program via its materialize method. Concrete
63 // witnessComponent types include SignatureWitness and dataWitness.
64 type witnessComponent interface {
65 Materialize(*[][]byte) error
68 // UnmarshalJSON unmarshal SigningInstruction
69 func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
71 Position uint32 `json:"position"`
72 WitnessComponents []json.RawMessage `json:"witness_components"`
74 err := json.Unmarshal(b, &pre)
79 si.Position = pre.Position
80 for i, wc := range pre.WitnessComponents {
84 err = json.Unmarshal(wc, &t)
86 return errors.Wrapf(err, "unmarshaling error on witness component %d, input %s", i, wc)
91 Value chainjson.HexBytes
93 err = json.Unmarshal(wc, &d)
95 return errors.Wrapf(err, "unmarshaling error on witness component %d, type data, input %s", i, wc)
97 si.WitnessComponents = append(si.WitnessComponents, DataWitness(d.Value))
100 var s SignatureWitness
101 err = json.Unmarshal(wc, &s)
103 return errors.Wrapf(err, "unmarshaling error on witness component %d, type signature, input %s", i, wc)
105 si.WitnessComponents = append(si.WitnessComponents, &s)
107 case "raw_tx_signature":
108 var s RawTxSigWitness
109 err = json.Unmarshal(wc, &s)
111 return errors.Wrapf(err, "unmarshaling error on witness component %d, type raw_signature, input %s", i, wc)
113 si.WitnessComponents = append(si.WitnessComponents, &s)
116 return errors.WithDetailf(txbuilder.ErrBadWitnessComponent, "witness component %d has unknown type '%s'", i, t.Type)