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 // AddRawWitnessKeys adds a SignatureWitness with the given quorum and
33 // list of keys derived by applying the derivation path to each of the
35 func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
36 hexPath := make([]chainjson.HexBytes, 0, len(path))
37 for _, p := range path {
38 hexPath = append(hexPath, p)
41 keyIDs := make([]keyID, 0, len(xpubs))
42 for _, xpub := range xpubs {
43 keyIDs = append(keyIDs, keyID{xpub, hexPath})
46 sw := &RawTxSigWitness{
50 si.WitnessComponents = append(si.WitnessComponents, sw)
53 // SigningInstruction gives directions for signing inputs in a TxTemplate.
54 type SigningInstruction struct {
55 Position uint32 `json:"position"`
56 WitnessComponents []witnessComponent `json:"witness_components,omitempty"`
59 // witnessComponent is the abstract type for the parts of a
60 // SigningInstruction. Each witnessComponent produces one or more
61 // arguments for a VM program via its materialize method. Concrete
62 // witnessComponent types include SignatureWitness and dataWitness.
63 type witnessComponent interface {
64 materialize(*[][]byte) error
67 // UnmarshalJSON unmarshal SigningInstruction
68 func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
70 Position uint32 `json:"position"`
71 WitnessComponents []json.RawMessage `json:"witness_components"`
73 err := json.Unmarshal(b, &pre)
78 si.Position = pre.Position
79 for i, wc := range pre.WitnessComponents {
83 err = json.Unmarshal(wc, &t)
85 return errors.Wrapf(err, "unmarshaling error on witness component %d, input %s", i, wc)
90 Value chainjson.HexBytes
92 err = json.Unmarshal(wc, &d)
94 return errors.Wrapf(err, "unmarshaling error on witness component %d, type data, input %s", i, wc)
96 si.WitnessComponents = append(si.WitnessComponents, DataWitness(d.Value))
99 var s SignatureWitness
100 err = json.Unmarshal(wc, &s)
102 return errors.Wrapf(err, "unmarshaling error on witness component %d, type signature, input %s", i, wc)
104 si.WitnessComponents = append(si.WitnessComponents, &s)
106 case "raw_tx_signature":
107 var s RawTxSigWitness
108 err = json.Unmarshal(wc, &s)
110 return errors.Wrapf(err, "unmarshaling error on witness component %d, type raw_signature, input %s", i, wc)
112 si.WitnessComponents = append(si.WitnessComponents, &s)
115 return errors.WithDetailf(ErrBadWitnessComponent, "witness component %d has unknown type '%s'", i, t.Type)