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, quorum int) {
17 keyIDs := make([]keyID, 0, len(xpubs))
18 for _, xpub := range xpubs {
19 keyIDs = append(keyIDs, keyID{XPub: xpub})
22 sw := &SignatureWitness{
26 si.WitnessComponents = append(si.WitnessComponents, sw)
29 // AddRawWitnessKeys adds a SignatureWitness with the given quorum and
30 // list of keys derived by applying the derivation path to each of the
32 func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
33 hexPath := make([]chainjson.HexBytes, 0, len(path))
34 for _, p := range path {
35 hexPath = append(hexPath, p)
38 keyIDs := make([]keyID, 0, len(xpubs))
39 for _, xpub := range xpubs {
40 keyIDs = append(keyIDs, keyID{xpub, hexPath})
43 sw := &RawTxSigWitness{
47 si.WitnessComponents = append(si.WitnessComponents, sw)
50 func (si *SigningInstruction) AddRawWitnessKeysWithoutPath(xpubs []chainkd.XPub, quorum int) {
52 keyIDs := make([]keyID, 0, len(xpubs))
53 for _, xpub := range xpubs {
54 keyIDs = append(keyIDs, keyID{XPub: xpub})
57 sw := &RawTxSigWitness{
61 si.WitnessComponents = append(si.WitnessComponents, sw)
64 // SigningInstruction gives directions for signing inputs in a TxTemplate.
65 type SigningInstruction struct {
66 Position uint32 `json:"position"`
67 WitnessComponents []witnessComponent `json:"witness_components,omitempty"`
70 // witnessComponent is the abstract type for the parts of a
71 // SigningInstruction. Each witnessComponent produces one or more
72 // arguments for a VM program via its materialize method. Concrete
73 // witnessComponent types include SignatureWitness and dataWitness.
74 type witnessComponent interface {
75 Materialize(*[][]byte) error
78 // UnmarshalJSON unmarshal SigningInstruction
79 func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
81 Position uint32 `json:"position"`
82 WitnessComponents []json.RawMessage `json:"witness_components"`
84 err := json.Unmarshal(b, &pre)
89 si.Position = pre.Position
90 for i, wc := range pre.WitnessComponents {
94 err = json.Unmarshal(wc, &t)
96 return errors.Wrapf(err, "unmarshaling error on witness component %d, input %s", i, wc)
101 Value chainjson.HexBytes
103 err = json.Unmarshal(wc, &d)
105 return errors.Wrapf(err, "unmarshaling error on witness component %d, type data, input %s", i, wc)
107 si.WitnessComponents = append(si.WitnessComponents, DataWitness(d.Value))
110 var s SignatureWitness
111 err = json.Unmarshal(wc, &s)
113 return errors.Wrapf(err, "unmarshaling error on witness component %d, type signature, input %s", i, wc)
115 si.WitnessComponents = append(si.WitnessComponents, &s)
117 case "raw_tx_signature":
118 var s RawTxSigWitness
119 err = json.Unmarshal(wc, &s)
121 return errors.Wrapf(err, "unmarshaling error on witness component %d, type raw_signature, input %s", i, wc)
123 si.WitnessComponents = append(si.WitnessComponents, &s)
126 return errors.WithDetailf(txbuilder.ErrBadWitnessComponent, "witness component %d has unknown type '%s'", i, t.Type)