6 "github.com/vapor/crypto/ed25519/chainkd"
7 chainjson "github.com/vapor/encoding/json"
8 "github.com/vapor/errors"
11 // AddWitnessKeys adds a SignatureWitness with the given quorum and
12 // list of keys derived by applying the derivation path to each of the
14 func (si *SigningInstruction) AddWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
15 hexPath := make([]chainjson.HexBytes, 0, len(path))
16 for _, p := range path {
17 hexPath = append(hexPath, p)
20 keyIDs := make([]keyID, 0, len(xpubs))
21 for _, xpub := range xpubs {
22 keyIDs = append(keyIDs, keyID{xpub, hexPath})
25 sw := &SignatureWitness{
29 si.WitnessComponents = append(si.WitnessComponents, sw)
32 func (si *SigningInstruction) AddWitnessKeysWithOutPath(xpubs []chainkd.XPub, quorum int) {
34 keyIDs := make([]keyID, 0, len(xpubs))
35 for _, xpub := range xpubs {
36 keyIDs = append(keyIDs, keyID{XPub: xpub})
39 sw := &SignatureWitness{
43 si.WitnessComponents = append(si.WitnessComponents, sw)
46 // AddRawWitnessKeys adds a SignatureWitness with the given quorum and
47 // list of keys derived by applying the derivation path to each of the
49 func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
50 hexPath := make([]chainjson.HexBytes, 0, len(path))
51 for _, p := range path {
52 hexPath = append(hexPath, p)
55 keyIDs := make([]keyID, 0, len(xpubs))
56 for _, xpub := range xpubs {
57 keyIDs = append(keyIDs, keyID{xpub, hexPath})
60 sw := &RawTxSigWitness{
64 si.WitnessComponents = append(si.WitnessComponents, sw)
67 func (si *SigningInstruction) AddRawWitnessKeysWithoutPath(xpubs []chainkd.XPub, quorum int) {
69 keyIDs := make([]keyID, 0, len(xpubs))
70 for _, xpub := range xpubs {
71 keyIDs = append(keyIDs, keyID{XPub: xpub})
74 sw := &RawTxSigWitness{
78 si.WitnessComponents = append(si.WitnessComponents, sw)
81 // SigningInstruction gives directions for signing inputs in a TxTemplate.
82 type SigningInstruction struct {
83 Position uint32 `json:"position"`
84 WitnessComponents []witnessComponent `json:"witness_components,omitempty"`
87 // witnessComponent is the abstract type for the parts of a
88 // SigningInstruction. Each witnessComponent produces one or more
89 // arguments for a VM program via its materialize method. Concrete
90 // witnessComponent types include SignatureWitness and dataWitness.
91 type witnessComponent interface {
92 Materialize(*[][]byte) error
95 // UnmarshalJSON unmarshal SigningInstruction
96 func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
98 Position uint32 `json:"position"`
99 WitnessComponents []json.RawMessage `json:"witness_components"`
101 err := json.Unmarshal(b, &pre)
106 si.Position = pre.Position
107 for i, wc := range pre.WitnessComponents {
111 err = json.Unmarshal(wc, &t)
113 return errors.Wrapf(err, "unmarshaling error on witness component %d, input %s", i, wc)
118 Value chainjson.HexBytes
120 err = json.Unmarshal(wc, &d)
122 return errors.Wrapf(err, "unmarshaling error on witness component %d, type data, input %s", i, wc)
124 si.WitnessComponents = append(si.WitnessComponents, DataWitness(d.Value))
127 var s SignatureWitness
128 err = json.Unmarshal(wc, &s)
130 return errors.Wrapf(err, "unmarshaling error on witness component %d, type signature, input %s", i, wc)
132 si.WitnessComponents = append(si.WitnessComponents, &s)
134 case "raw_tx_signature":
135 var s RawTxSigWitness
136 err = json.Unmarshal(wc, &s)
138 return errors.Wrapf(err, "unmarshaling error on witness component %d, type raw_signature, input %s", i, wc)
140 si.WitnessComponents = append(si.WitnessComponents, &s)
143 return errors.WithDetailf(ErrBadWitnessComponent, "witness component %d has unknown type '%s'", i, t.Type)