You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB

  1. package multisig
  2. import (
  3. "errors"
  4. "github.com/tendermint/tendermint/crypto"
  5. )
  6. // Multisignature is used to represent the signature object used in the multisigs.
  7. // Sigs is a list of signatures, sorted by corresponding index.
  8. type Multisignature struct {
  9. BitArray *CompactBitArray
  10. Sigs [][]byte
  11. }
  12. // NewMultisig returns a new Multisignature of size n.
  13. func NewMultisig(n int) *Multisignature {
  14. // Default the signature list to have a capacity of two, since we can
  15. // expect that most multisigs will require multiple signers.
  16. return &Multisignature{NewCompactBitArray(n), make([][]byte, 0, 2)}
  17. }
  18. // GetIndex returns the index of pk in keys. Returns -1 if not found
  19. func getIndex(pk crypto.PubKey, keys []crypto.PubKey) int {
  20. for i := 0; i < len(keys); i++ {
  21. if pk.Equals(keys[i]) {
  22. return i
  23. }
  24. }
  25. return -1
  26. }
  27. // AddSignature adds a signature to the multisig, at the corresponding index.
  28. // If the signature already exists, replace it.
  29. func (mSig *Multisignature) AddSignature(sig []byte, index int) {
  30. newSigIndex := mSig.BitArray.NumTrueBitsBefore(index)
  31. // Signature already exists, just replace the value there
  32. if mSig.BitArray.GetIndex(index) {
  33. mSig.Sigs[newSigIndex] = sig
  34. return
  35. }
  36. mSig.BitArray.SetIndex(index, true)
  37. // Optimization if the index is the greatest index
  38. if newSigIndex == len(mSig.Sigs) {
  39. mSig.Sigs = append(mSig.Sigs, sig)
  40. return
  41. }
  42. // Expand slice by one with a dummy element, move all elements after i
  43. // over by one, then place the new signature in that gap.
  44. mSig.Sigs = append(mSig.Sigs, make([]byte, 0))
  45. copy(mSig.Sigs[newSigIndex+1:], mSig.Sigs[newSigIndex:])
  46. mSig.Sigs[newSigIndex] = sig
  47. }
  48. // AddSignatureFromPubkey adds a signature to the multisig,
  49. // at the index in keys corresponding to the provided pubkey.
  50. func (mSig *Multisignature) AddSignatureFromPubkey(sig []byte, pubkey crypto.PubKey, keys []crypto.PubKey) error {
  51. index := getIndex(pubkey, keys)
  52. if index == -1 {
  53. return errors.New("provided key didn't exist in pubkeys")
  54. }
  55. mSig.AddSignature(sig, index)
  56. return nil
  57. }
  58. // Marshal the multisignature with amino
  59. func (mSig *Multisignature) Marshal() []byte {
  60. return cdc.MustMarshalBinaryBare(mSig)
  61. }