7 log "github.com/sirupsen/logrus"
9 "github.com/bytom/crypto/ed25519/chainkd"
10 chainjson "github.com/bytom/encoding/json"
13 // TODO(bobg): most of the code here is duplicated from
14 // signature_witness.go and needs refactoring.
16 // RawTxSigWitness is like SignatureWitness but doesn't involve
17 // signature programs.
18 type RawTxSigWitness struct {
19 Quorum int `json:"quorum"`
20 Keys []keyID `json:"keys"`
21 Sigs []chainjson.HexBytes `json:"signatures"`
24 func (sw *RawTxSigWitness) sign(ctx context.Context, tpl *Template, index uint32, xpubs []chainkd.XPub, auth string, signFn SignFunc) error {
25 if len(sw.Sigs) < len(sw.Keys) {
26 // Each key in sw.Keys may produce a signature in sw.Sigs. Make
27 // sure there are enough slots in sw.Sigs and that we preserve any
28 // sigs already present.
29 newSigs := make([]chainjson.HexBytes, len(sw.Keys))
30 copy(newSigs, sw.Sigs)
33 for i, keyID := range sw.Keys {
34 if len(sw.Sigs[i]) > 0 {
35 // Already have a signature for this key
39 for _, xpub := range xpubs {
40 if keyID.XPub == xpub {
45 if xpubs != nil && !found {
48 path := make([]([]byte), len(keyID.DerivationPath))
49 for i, p := range keyID.DerivationPath {
52 sigBytes, err := signFn(ctx, keyID.XPub, path, tpl.Hash(index).Byte32(), auth)
54 log.WithField("err", err).Warningf("computing signature %d", i)
62 func (sw RawTxSigWitness) materialize(args *[][]byte) error {
64 for i := 0; i < len(sw.Sigs) && nsigs < sw.Quorum; i++ {
65 if len(sw.Sigs[i]) > 0 {
66 *args = append(*args, sw.Sigs[i])
73 // MarshalJSON convert struct to json
74 func (sw RawTxSigWitness) MarshalJSON() ([]byte, error) {
76 Type string `json:"type"`
77 Quorum int `json:"quorum"`
78 Keys []keyID `json:"keys"`
79 Sigs []chainjson.HexBytes `json:"signatures"`
81 Type: "raw_tx_signature",
86 return json.Marshal(obj)