6 "github.com/vapor/crypto/ed25519/chainkd"
7 chainjson "github.com/vapor/encoding/json"
8 "github.com/vapor/errors"
11 func (si *SigningInstruction) AddDataWitness(data chainjson.HexBytes) {
12 dw := DataWitness(data)
13 si.WitnessComponents = append(si.WitnessComponents, &dw)
16 // AddWitnessKeys adds a SignatureWitness with the given quorum and
17 // list of keys derived by applying the derivation path to each of the
19 func (si *SigningInstruction) AddWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
20 hexPath := make([]chainjson.HexBytes, 0, len(path))
21 for _, p := range path {
22 hexPath = append(hexPath, p)
25 keyIDs := make([]keyID, 0, len(xpubs))
26 for _, xpub := range xpubs {
27 keyIDs = append(keyIDs, keyID{xpub, hexPath})
30 sw := &SignatureWitness{
34 si.WitnessComponents = append(si.WitnessComponents, sw)
37 // AddRawWitnessKeys adds a SignatureWitness with the given quorum and
38 // list of keys derived by applying the derivation path to each of the
40 func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
41 hexPath := make([]chainjson.HexBytes, 0, len(path))
42 for _, p := range path {
43 hexPath = append(hexPath, p)
46 keyIDs := make([]keyID, 0, len(xpubs))
47 for _, xpub := range xpubs {
48 keyIDs = append(keyIDs, keyID{xpub, hexPath})
51 sw := &RawTxSigWitness{
55 si.WitnessComponents = append(si.WitnessComponents, sw)
58 // SigningInstruction gives directions for signing inputs in a TxTemplate.
59 type SigningInstruction struct {
60 Position uint32 `json:"position"`
61 WitnessComponents []witnessComponent `json:"witness_components,omitempty"`
64 // witnessComponent is the abstract type for the parts of a
65 // SigningInstruction. Each witnessComponent produces one or more
66 // arguments for a VM program via its materialize method. Concrete
67 // witnessComponent types include SignatureWitness and dataWitness.
68 type witnessComponent interface {
69 materialize(*[][]byte) error
72 // UnmarshalJSON unmarshal SigningInstruction
73 func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
75 Position uint32 `json:"position"`
76 WitnessComponents []json.RawMessage `json:"witness_components"`
78 err := json.Unmarshal(b, &pre)
83 si.Position = pre.Position
84 for i, wc := range pre.WitnessComponents {
88 err = json.Unmarshal(wc, &t)
90 return errors.Wrapf(err, "unmarshaling error on witness component %d, input %s", i, wc)
95 Value chainjson.HexBytes
97 err = json.Unmarshal(wc, &d)
99 return errors.Wrapf(err, "unmarshaling error on witness component %d, type data, input %s", i, wc)
101 si.WitnessComponents = append(si.WitnessComponents, DataWitness(d.Value))
104 var s SignatureWitness
105 err = json.Unmarshal(wc, &s)
107 return errors.Wrapf(err, "unmarshaling error on witness component %d, type signature, input %s", i, wc)
109 si.WitnessComponents = append(si.WitnessComponents, &s)
111 case "raw_tx_signature":
112 var s RawTxSigWitness
113 err = json.Unmarshal(wc, &s)
115 return errors.Wrapf(err, "unmarshaling error on witness component %d, type raw_signature, input %s", i, wc)
117 si.WitnessComponents = append(si.WitnessComponents, &s)
120 return errors.WithDetailf(ErrBadWitnessComponent, "witness component %d has unknown type '%s'", i, t.Type)